mirror of
https://github.com/beemdevelopment/Aegis.git
synced 2025-05-14 14:02:49 +00:00
Don't leave the PreferencesActivity when exporting a database
This commit is contained in:
parent
6770ccd3b1
commit
b9381d13d2
2 changed files with 56 additions and 58 deletions
|
@ -45,8 +45,7 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen
|
||||||
private static final int CODE_SLOTS = 7;
|
private static final int CODE_SLOTS = 7;
|
||||||
|
|
||||||
// permission request codes
|
// permission request codes
|
||||||
private static final int CODE_PERM_EXPORT = 0;
|
private static final int CODE_PERM_CAMERA = 0;
|
||||||
private static final int CODE_PERM_CAMERA = 1;
|
|
||||||
|
|
||||||
private AegisApplication _app;
|
private AegisApplication _app;
|
||||||
private DatabaseManager _db;
|
private DatabaseManager _db;
|
||||||
|
@ -194,9 +193,6 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (requestCode) {
|
switch (requestCode) {
|
||||||
case CODE_PERM_EXPORT:
|
|
||||||
onExport();
|
|
||||||
break;
|
|
||||||
case CODE_PERM_CAMERA:
|
case CODE_PERM_CAMERA:
|
||||||
onScanKeyInfo();
|
onScanKeyInfo();
|
||||||
break;
|
break;
|
||||||
|
@ -229,9 +225,6 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen
|
||||||
// perform any pending actions
|
// perform any pending actions
|
||||||
int action = data.getIntExtra("action", -1);
|
int action = data.getIntExtra("action", -1);
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case PreferencesFragment.ACTION_EXPORT:
|
|
||||||
onExport();
|
|
||||||
break;
|
|
||||||
case PreferencesFragment.ACTION_SLOTS:
|
case PreferencesFragment.ACTION_SLOTS:
|
||||||
MasterKey masterKey = _db.getMasterKey();
|
MasterKey masterKey = _db.getMasterKey();
|
||||||
Intent intent = new Intent(this, SlotManagerActivity.class);
|
Intent intent = new Intent(this, SlotManagerActivity.class);
|
||||||
|
@ -242,46 +235,6 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onExport() {
|
|
||||||
if (!PermissionHelper.request(this, CODE_PERM_EXPORT, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: create a custom layout to show a message AND a checkbox
|
|
||||||
final boolean[] checked = {true};
|
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
|
|
||||||
.setTitle("Export the database")
|
|
||||||
.setPositiveButton(android.R.string.ok, (dialog, which) -> {
|
|
||||||
String filename;
|
|
||||||
try {
|
|
||||||
filename = _db.export(checked[0]);
|
|
||||||
} catch (DatabaseManagerException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Toast.makeText(this, "An error occurred while trying to export the database", Toast.LENGTH_SHORT).show();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// make sure the new file is visible
|
|
||||||
MediaScannerConnection.scanFile(this, new String[]{filename}, null, null);
|
|
||||||
|
|
||||||
Toast.makeText(this, "The database has been exported to: " + filename, Toast.LENGTH_SHORT).show();
|
|
||||||
})
|
|
||||||
.setNegativeButton(android.R.string.cancel, null);
|
|
||||||
if (_db.getFile().isEncrypted()) {
|
|
||||||
final String[] items = {"Keep the database encrypted"};
|
|
||||||
final boolean[] checkedItems = {true};
|
|
||||||
builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(DialogInterface dialog, int index, boolean isChecked) {
|
|
||||||
checked[0] = isChecked;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
builder.setMessage("This action will export the database out of Android's private storage.");
|
|
||||||
}
|
|
||||||
builder.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void startEditProfileActivity(int requestCode, KeyProfile profile, boolean isNew) {
|
private void startEditProfileActivity(int requestCode, KeyProfile profile, boolean isNew) {
|
||||||
Intent intent = new Intent(this, EditProfileActivity.class);
|
Intent intent = new Intent(this, EditProfileActivity.class);
|
||||||
if (profile != null) {
|
if (profile != null) {
|
||||||
|
|
|
@ -2,12 +2,15 @@ package me.impy.aegis.ui;
|
||||||
|
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.media.MediaScannerConnection;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.preference.EditTextPreference;
|
import android.preference.EditTextPreference;
|
||||||
import android.preference.Preference;
|
import android.preference.Preference;
|
||||||
import android.preference.PreferenceFragment;
|
import android.preference.PreferenceFragment;
|
||||||
|
import android.support.v7.app.AlertDialog;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
|
@ -35,10 +38,10 @@ public class PreferencesFragment extends PreferenceFragment {
|
||||||
|
|
||||||
// permission request codes
|
// permission request codes
|
||||||
private static final int CODE_PERM_IMPORT = 0;
|
private static final int CODE_PERM_IMPORT = 0;
|
||||||
|
private static final int CODE_PERM_EXPORT = 1;
|
||||||
|
|
||||||
// action codes
|
// action codes
|
||||||
public static final int ACTION_EXPORT = 0;
|
public static final int ACTION_SLOTS = 0;
|
||||||
public static final int ACTION_SLOTS = 1;
|
|
||||||
|
|
||||||
private Intent _result = new Intent();
|
private Intent _result = new Intent();
|
||||||
private DatabaseManager _db;
|
private DatabaseManager _db;
|
||||||
|
@ -80,9 +83,7 @@ public class PreferencesFragment extends PreferenceFragment {
|
||||||
exportPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
exportPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public boolean onPreferenceClick(Preference preference) {
|
public boolean onPreferenceClick(Preference preference) {
|
||||||
if (PermissionHelper.request(getActivity(), CODE_PERM_IMPORT, Manifest.permission.READ_EXTERNAL_STORAGE)) {
|
onImport();
|
||||||
onImport();
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -91,8 +92,7 @@ public class PreferencesFragment extends PreferenceFragment {
|
||||||
importPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
importPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public boolean onPreferenceClick(Preference preference) {
|
public boolean onPreferenceClick(Preference preference) {
|
||||||
_result.putExtra("action", ACTION_EXPORT);
|
onExport();
|
||||||
finish();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -139,6 +139,9 @@ public class PreferencesFragment extends PreferenceFragment {
|
||||||
case CODE_PERM_IMPORT:
|
case CODE_PERM_IMPORT:
|
||||||
onImport();
|
onImport();
|
||||||
break;
|
break;
|
||||||
|
case CODE_PERM_EXPORT:
|
||||||
|
onExport();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,9 +162,11 @@ public class PreferencesFragment extends PreferenceFragment {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onImport() {
|
private void onImport() {
|
||||||
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
if (PermissionHelper.request(getActivity(), CODE_PERM_IMPORT, Manifest.permission.READ_EXTERNAL_STORAGE)) {
|
||||||
intent.setType("*/*");
|
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
||||||
startActivityForResult(intent, CODE_IMPORT);
|
intent.setType("*/*");
|
||||||
|
startActivityForResult(intent, CODE_IMPORT);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onImportDecryptResult(int resultCode, Intent data) {
|
private void onImportDecryptResult(int resultCode, Intent data) {
|
||||||
|
@ -255,4 +260,44 @@ public class PreferencesFragment extends PreferenceFragment {
|
||||||
_result.putExtra("needsReload", true);
|
_result.putExtra("needsReload", true);
|
||||||
Toast.makeText(getActivity(), String.format(Locale.getDefault(), "Imported %d entries", entries.size()), Toast.LENGTH_LONG).show();
|
Toast.makeText(getActivity(), String.format(Locale.getDefault(), "Imported %d entries", entries.size()), Toast.LENGTH_LONG).show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void onExport() {
|
||||||
|
if (!PermissionHelper.request(getActivity(), CODE_PERM_EXPORT, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: create a custom layout to show a message AND a checkbox
|
||||||
|
final boolean[] checked = {true};
|
||||||
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
|
||||||
|
.setTitle("Export the database")
|
||||||
|
.setPositiveButton(android.R.string.ok, (dialog, which) -> {
|
||||||
|
String filename;
|
||||||
|
try {
|
||||||
|
filename = _db.export(checked[0]);
|
||||||
|
} catch (DatabaseManagerException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
Toast.makeText(getActivity(), "An error occurred while trying to export the database", Toast.LENGTH_SHORT).show();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// make sure the new file is visible
|
||||||
|
MediaScannerConnection.scanFile(getActivity(), new String[]{filename}, null, null);
|
||||||
|
|
||||||
|
Toast.makeText(getActivity(), "The database has been exported to: " + filename, Toast.LENGTH_SHORT).show();
|
||||||
|
})
|
||||||
|
.setNegativeButton(android.R.string.cancel, null);
|
||||||
|
if (_db.getFile().isEncrypted()) {
|
||||||
|
final String[] items = {"Keep the database encrypted"};
|
||||||
|
final boolean[] checkedItems = {true};
|
||||||
|
builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(DialogInterface dialog, int index, boolean isChecked) {
|
||||||
|
checked[0] = isChecked;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
builder.setMessage("This action will export the database out of Android's private storage.");
|
||||||
|
}
|
||||||
|
builder.show();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue