Skip to content

Commit

Permalink
Merge pull request #904 from opensrp/sample-app-refactor
Browse files Browse the repository at this point in the history
Sample App Cleanup and Refactor
  • Loading branch information
qiarie authored Feb 13, 2023
2 parents 873da91 + a8026c4 commit b880a9a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 33 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=6.1.0-SNAPSHOT
VERSION_NAME=6.1.1-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Core Application
Expand Down
Binary file removed opensrp-app/jacoco.exec
Binary file not shown.
17 changes: 11 additions & 6 deletions opensrp-core/src/main/java/org/smartregister/util/StatsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public Map<String, String> fetchStatsInfo() {
}

private void populateSyncStatistics() {
Cursor cursor = null;

try {
syncInfoMap.put(SYNCED_EVENTS, "-");
syncInfoMap.put(SYNCED_CLIENTS, "-");
Expand All @@ -86,8 +88,6 @@ private void populateSyncStatistics() {
String unsyncedWeightEventsSQL = "SELECT COUNT(*) FROM weights WHERE sync_status = 'Unsynced'";
String unsyncedHeightEventsSQL = "SELECT COUNT(*) FROM heights WHERE sync_status = 'Unsynced'";

Cursor cursor;

cursor = database.rawQuery(eventSyncSql, new String[]{});
while (cursor.moveToNext()) {
populateEventSyncInfo(cursor);
Expand All @@ -110,26 +110,33 @@ private void populateSyncStatistics() {
while (cursor.moveToNext()) {
populateValidatedClientsInfo(cursor);
}
cursor.close();

cursor = database.rawQuery(unsyncedVaccineEventsSQL, new String[]{});
while (cursor.moveToNext()) {
syncInfoMap.put(UNSYNCED_VACCINE_EVENTS, String.valueOf(cursor.getInt(0)));
}
cursor.close();

cursor = database.rawQuery(unsyncedWeightEventsSQL, new String[]{});
while (cursor.moveToNext()) {
syncInfoMap.put(UNSYNCED_WEIGHT_EVENTS, String.valueOf(cursor.getInt(0)));
}
cursor.close();

if (CoreLibrary.getInstance().context().getAppProperties().isTrue("monitor.height")) { // Constant is defined in growth-monitoring module
cursor = database.rawQuery(unsyncedHeightEventsSQL, new String[]{});
while (cursor.moveToNext()) {
syncInfoMap.put(UNSYNCED_HEIGHT_EVENTS, String.valueOf(cursor.getInt(0)));
}
cursor.close();
}

cursor.close();
} catch (Exception e) {
Timber.e(e);
} finally {
if (cursor != null) {
cursor.close();
}
}
}

Expand Down Expand Up @@ -183,7 +190,6 @@ public void populateUserInfo() {
syncInfoMap.put(USER_LOCALITY, StringUtils.isNotBlank(userLocality) ? userLocality : "-");
}


private void populateBuildInfo() {
try {
syncInfoMap.put(APP_VERSION_NAME, Utils.getVersion(CoreLibrary.getInstance().context().applicationContext()));
Expand Down Expand Up @@ -216,6 +222,5 @@ private void populateDeviceInfo() {
} catch (Exception e) {
Timber.e(e);
}

}
}
77 changes: 51 additions & 26 deletions sample/src/main/java/org/smartregister/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,14 @@ protected void onCreate(Bundle savedInstanceState) {
encDecTextView = (TextView) findViewById(R.id.encrypt_decrypt_tv);

picker.setMinDate(new LocalDate().minusYears(2).toDate().getTime());

picker.setMaxDate(new LocalDate().plusYears(3).toDate().getTime());

picker.updateDate(2019, 5, 22);

btnGet = (Button) findViewById(R.id.button1);
btnGet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tvw.setText("Selected Date: " + picker.getDayOfMonth() + "/" + (picker.getMonth() + 1) + "/" + picker.getYear());
tvw.setText(getString(R.string.selected_date_format, picker.getDayOfMonth(), (picker.getMonth() + 1), picker.getYear()));
}
});

Expand All @@ -87,7 +85,8 @@ public void onClick(View v) {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
.setAction("Action", null)
.show();
}
});
SmartRegisterQueryBuilder srqb = new SmartRegisterQueryBuilder();
Expand All @@ -108,7 +107,6 @@ public void onClick(View view) {
// set language from preferences
String langPref = LangUtils.getLanguage(activity.getApplicationContext());
for (int i = 0; i < langArray.size(); i++) {

if (langPref != null && langArray.get(i).toLowerCase().startsWith(langPref)) {
languageSpinner.setSelection(i);
break;
Expand Down Expand Up @@ -169,12 +167,11 @@ public void onNothingSelected(AdapterView<?> parent) {
fos.write(contents.getBytes());
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
Timber.e(e);
} catch (IOException e) {
e.printStackTrace();
Timber.e(e);
}


toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String keyAlias = "sample";
Expand All @@ -184,53 +181,81 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Timber.i("key with alias %s generated", keyAlias);
}

if (isChecked) {
FileInputStream inputStream = null;
FileOutputStream fileOutputStream = null;

if (isChecked) {
try {
// read the text.txt while it is in plain text and write to file
FileInputStream inputStream = openFileInput(filename);
inputStream = openFileInput(filename);
byte[] inputBytes = new byte[inputStream.available()];
inputStream.read(inputBytes);
byte[] encryptedContents = CryptographicHelper.encrypt(inputBytes, keyAlias);
FileOutputStream fileOutputStream = openFileOutput(filename, Context.MODE_PRIVATE);

Timber.i("encrypted stuff to write %S ", new String(encryptedContents));

encDecTextView.setText(new String(encryptedContents));

fileOutputStream = openFileOutput(filename, Context.MODE_PRIVATE);
fileOutputStream.write((encryptedContents));
fileOutputStream.flush();


} catch (IOException e) {
e.printStackTrace();
Timber.e(e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
Timber.e(e);
}
}

if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
Timber.e(e);
}
}
}

} else {
try {
//
FileInputStream inputStream = openFileInput(filename);
inputStream = openFileInput(filename);
byte[] inputBytes = new byte[inputStream.available()];
inputStream.read(inputBytes);

Timber.i("before decryption %s", new String(inputBytes));

byte[] decryptedStuff = CryptographicHelper.decrypt(inputBytes, keyAlias);
encDecTextView.setText(new String(decryptedStuff));

Timber.i("decrypted content %s", new String(decryptedStuff));

FileOutputStream fileOutputStream = openFileOutput(filename, Context.MODE_PRIVATE);
fileOutputStream = openFileOutput(filename, Context.MODE_PRIVATE);
fileOutputStream.write((decryptedStuff));
fileOutputStream.flush();


} catch (IOException e) {
e.printStackTrace();
// Error occurred when opening raw file for reading.
Timber.e(e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
Timber.e(e);
}
}

if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
Timber.e(e);
}
}
}


}
}
});


}

@Override
Expand Down
1 change: 1 addition & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<string name="sample">Night gathers, and now my watch begins</string>
<string name="language_label">Select Language</string>
<string name="action_report_sample">Sample Reports</string>
<string name="selected_date_format">Selected Date: %1$d/%2$d/%3$d</string>
<string name="action_sync_stats">Sync Statistics</string>
<string name="sync_stats_label">Show Sync Stats (Long Press for Dialog)</string>
<string name="encrypt_decrypt_string">Please encrypt or decrypt me by hitting clicking on the switch below</string>
Expand Down

0 comments on commit b880a9a

Please sign in to comment.