Skip to content

Commit

Permalink
Update Sample
Browse files Browse the repository at this point in the history
  • Loading branch information
hamza-vd committed Feb 9, 2023
1 parent eb00e14 commit 27d52c2
Showing 1 changed file with 44 additions and 21 deletions.
65 changes: 44 additions & 21 deletions sample/src/main/java/org/smartregister/sample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.smartregister.sample;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
Expand Down Expand Up @@ -29,7 +30,11 @@
import org.smartregister.util.AppHealthUtils;
import org.smartregister.util.DateUtil;
import org.smartregister.util.LangUtils;
import org.smartregister.util.PermissionUtils;
import org.smartregister.util.Utils;
import org.smartregister.view.activity.DrishtiApplication;
import org.smartregister.view.activity.MultiLanguageActivity;
import org.smartregister.view.activity.StatsActivity;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
Expand All @@ -41,13 +46,14 @@

import timber.log.Timber;

public class MainActivity extends MultiLanguageActivity {
public class MainActivity extends MultiLanguageActivity implements AppHealthUtils.HealthStatsView {

DatePicker picker;
Button btnGet;
TextView tvw;
CryptographicHelper cryptographicHelper = null;
TextView encDecTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -173,10 +179,9 @@ public void onNothingSelected(AdapterView<?> parent) {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String keyAlias = "sample";
// Get or create a key with the Alias name sample or create one if id does not exist
if(cryptographicHelper.getKey(keyAlias)==null)
{
if (cryptographicHelper.getKey(keyAlias) == null) {
cryptographicHelper.generateKey(keyAlias);
Timber.i("key with alias %s generated",keyAlias);
Timber.i("key with alias %s generated", keyAlias);
}

if (isChecked) {
Expand All @@ -188,7 +193,7 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
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));
Timber.i("encrypted stuff to write %S ", new String(encryptedContents));
encDecTextView.setText(new String(encryptedContents));
fileOutputStream.write((encryptedContents));
fileOutputStream.flush();
Expand All @@ -199,26 +204,26 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}

} else {
try {
//
FileInputStream inputStream = openFileInput(filename);
byte[] inputBytes = new byte[inputStream.available()];
inputStream.read(inputBytes);
Timber.i("before decryption %s", new String(inputBytes));
try {
//
FileInputStream 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));
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.write((decryptedStuff));
fileOutputStream.flush();
FileOutputStream fileOutputStream = openFileOutput(filename, Context.MODE_PRIVATE);
fileOutputStream.write((decryptedStuff));
fileOutputStream.flush();


} catch (IOException e) {
e.printStackTrace();
// Error occurred when opening raw file for reading.
}
} catch (IOException e) {
e.printStackTrace();
// Error occurred when opening raw file for reading.
}


}
Expand Down Expand Up @@ -258,4 +263,22 @@ protected void onDestroy() {
super.onDestroy();
cryptographicHelper = null;
}

@Override
public void performDatabaseDownload() {
if (PermissionUtils.isPermissionGranted(this, Manifest.permission.WRITE_EXTERNAL_STORAGE, PermissionUtils.WRITE_EXTERNAL_STORAGE_REQUEST_CODE)) {
try {
AppHealthUtils.triggerDBCopying(this);
} catch (SecurityException e) {
Utils.showToast(this, this.getString(org.smartregister.R.string.permission_write_external_storage_rationale));
}
}
}

@Override
public void showSyncStats() {
DrishtiApplication.getInstance().setPassword("123".getBytes());
Intent statsActivityIntent = new Intent(this, StatsActivity.class);
this.startActivity(statsActivityIntent);
}
}

0 comments on commit 27d52c2

Please sign in to comment.