Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

outo backup #646

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
outo backup
  • Loading branch information
marwaothman authored Dec 25, 2018
commit 482b289e42b26e71875ea9afa140cab9a9c3c292
222 changes: 217 additions & 5 deletions omniNotes/src/main/java/it/feio/android/omninotes/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,18 @@ public class MainActivity extends BaseActivity implements OnDateSetListener, OnT
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.OmniNotesTheme_ApiSpec);
setTheme(R.style.OmniNotesTheme_ApiSpec);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
EventBus.getDefault().register(this);
EventBus.getDefault().register(this);

initUI();

if (IntroActivity.mustRun()) {
startActivity(new Intent(this.getApplicationContext(), IntroActivity.class));
}
if (IntroActivity.mustRun()) {
startActivity(new Intent(this.getApplicationContext(), IntroActivity.class));
}else{
loadFromPreferenes();
}

new UpdaterTask(this).execute();
}
Expand Down Expand Up @@ -539,5 +541,215 @@ public void onDateSet(DatePicker view, int year, int monthOfYear,
if (f != null && f.isAdded() && f.onDateSetListener != null) {
f.onDateSetListener.onDateSet(view, year, monthOfYear, dayOfMonth);
}
}

private void importNotes() {
final CharSequence[] backups = StorageHelper.getExternalStoragePublicDir().list();

if (backups != null && backups.length == 0) {
(MainActivity.this).showMessage(R.string.no_backups_available, ONStyle.WARN);
} else {

MaterialDialog importDialog = new MaterialDialog.Builder(MainActivity.this)
.title(R.string.data_import_message)
.items(backups)
.positiveText(R.string.confirm)
.callback(new MaterialDialog.ButtonCallback() {
@Override
public void onPositive(MaterialDialog materialDialog) {

}
}).build();

// OnShow is overridden to allow long-click on item so user can remove them
importDialog.setOnShowListener(dialog -> {

ListView lv = importDialog.getListView();
assert lv != null;
lv.setOnItemClickListener((parent, view, position, id) -> {

// Retrieves backup size
File backupDir = StorageHelper.getBackupDir(backups[position].toString());
long size = StorageHelper.getSize(backupDir) / 1024;
String sizeString = size > 1024 ? size / 1024 + "Mb" : size + "Kb";

// Check preference presence
String prefName = StorageHelper.getSharedPreferencesFile(MainActivity.this).getName();
boolean hasPreferences = (new File(backupDir, prefName)).exists();

String message = backups[position]
+ " (" + sizeString
+ (hasPreferences ? " " + getString(R.string.settings_included) : "")
+ ")";

new MaterialDialog.Builder(MainActivity.this)
.title(R.string.confirm_restoring_backup)
.content(message)
.positiveText(R.string.confirm)
.callback(new MaterialDialog.ButtonCallback() {
@Override
public void onPositive(MaterialDialog materialDialog) {

((OmniNotes)MainActivity.this.getApplication()).getAnalyticsHelper().trackEvent(AnalyticsHelper.CATEGORIES.SETTING,
"settings_import_data");

importDialog.dismiss();

// An IntentService will be launched to accomplish the import task
Intent service = new Intent(MainActivity.this,
DataBackupIntentService.class);
service.setAction(DataBackupIntentService.ACTION_DATA_IMPORT);
service.putExtra(DataBackupIntentService.INTENT_BACKUP_NAME,
backups[position]);
MainActivity.this.startService(service);
}
}).build().show();
});

// Creation of backup removal dialog
lv.setOnItemLongClickListener((parent, view, position, id) -> {

// Retrieves backup size
File backupDir = StorageHelper.getBackupDir(backups[position].toString());
long size = StorageHelper.getSize(backupDir) / 1024;
String sizeString = size > 1024 ? size / 1024 + "Mb" : size + "Kb";

new MaterialDialog.Builder(MainActivity.this)
.title(R.string.confirm_removing_backup)
.content(backups[position] + "" + " (" + sizeString + ")")
.positiveText(R.string.confirm)
.callback(new MaterialDialog.ButtonCallback() {
@Override
public void onPositive(MaterialDialog materialDialog) {
importDialog.dismiss();
// An IntentService will be launched to accomplish the deletion task
Intent service = new Intent(MainActivity.this,
DataBackupIntentService.class);
service.setAction(DataBackupIntentService.ACTION_DATA_DELETE);
service.putExtra(DataBackupIntentService.INTENT_BACKUP_NAME,
backups[position]);
MainActivity.this.startService(service);
}
}).build().show();

return true;
});
});

importDialog.show();
}
}
public void loadFromPreferenes(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to Load your saved data!?");
// add a button
builder.setPositiveButton("yes",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Preference importData = null;
Context context = MainActivity.this;
SharedPreferences sharedPref = context.getSharedPreferences(
getString(R.string.preference_file_key), Context.MODE_PRIVATE);
if (sharedPref != null) {

PermissionsHelper.requestPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, R
.string.permission_external_storage, MainActivity.this.findViewById(R.id.crouton_handle), () -> importNotes());

SharedPreferences sharedPreferences = getSharedPreferences("mypreferences", Context.MODE_PRIVATE);

byte[] bytes = sharedPreferences.getString("selectedNotesList", "{}").getBytes();
if (bytes.length == 0) {
Toast.makeText(MainActivity.this, "No Data stored to be loaded", Toast.LENGTH_LONG).show();
return;
}
ByteArrayInputStream byteArray = new ByteArrayInputStream(bytes);
Base64InputStream base64InputStream = new Base64InputStream(byteArray, Base64.DEFAULT);
ObjectInputStream objectInputStream;
try {
objectInputStream = new ObjectInputStream(base64InputStream);
ListFragment.selectedNotes = (List<Note>) objectInputStream.readObject();
Toast.makeText(MainActivity.this, "Load Successful" + ListFragment.selectedNotes.size(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

}
}
});

builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}

public void saveToPreferences(){
// AlertDialog.Builder builder = new AlertDialog.Builder(this);
// builder.setMessage("Do you want to upload data!?");
// // add a button
// builder.setPositiveButton("yes",new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
// Preference importData = null;
// Context context = MainActivity.this;
// SharedPreferences sharedPref = context.getSharedPreferences(
// getString(R.string.preference_file_key), Context.MODE_PRIVATE);
// if (sharedPref != null) {
//
// PermissionsHelper.requestPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, R
// .string.permission_external_storage, MainActivity.this.findViewById(R.id.crouton_handle), () -> importNotes());
//
//
// }
// }
// });
//
// builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialogInterface, int i) {
// dialogInterface.cancel();
// }
// });
// // create and show the alert dialog
// AlertDialog dialog = builder.create();
// dialog.show();

SharedPreferences sharedPreferences = getSharedPreferences("mypreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();

ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();

ObjectOutputStream objectOutput;

try {
objectOutput = new ObjectOutputStream(arrayOutputStream);
objectOutput.writeObject(ListFragment.selectedNotes);
byte[] data = arrayOutputStream.toByteArray();
objectOutput.close();
arrayOutputStream.close();

ByteArrayOutputStream out = new ByteArrayOutputStream();
Base64OutputStream b64 = new Base64OutputStream(out, Base64.DEFAULT);
b64.write(data);
b64.close();
out.close();

editor.putString("selectedNotesList", new String(out.toByteArray()));

editor.commit();
Toast.makeText(MainActivity.this, "List Saved" + ListFragment.selectedNotes.size(), Toast.LENGTH_LONG).show();

} catch (IOException e) {
e.printStackTrace();
}


}
}