2018-05-10 14:54:58 +02:00
|
|
|
package me.impy.aegis.ui;
|
|
|
|
|
|
|
|
import android.Manifest;
|
|
|
|
import android.app.Activity;
|
2018-05-10 19:34:42 +02:00
|
|
|
import android.content.DialogInterface;
|
2018-05-10 14:54:58 +02:00
|
|
|
import android.content.Intent;
|
2018-05-10 19:34:42 +02:00
|
|
|
import android.media.MediaScannerConnection;
|
2018-05-10 18:42:47 +02:00
|
|
|
import android.net.Uri;
|
2018-05-10 14:54:58 +02:00
|
|
|
import android.os.Bundle;
|
2018-09-25 16:26:57 +02:00
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
|
import androidx.preference.Preference;
|
2018-05-11 19:33:20 +02:00
|
|
|
import android.view.Window;
|
|
|
|
import android.view.WindowManager;
|
2018-05-10 14:54:58 +02:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2018-10-02 21:05:54 +02:00
|
|
|
import com.takisoft.preferencex.PreferenceFragmentCompat;
|
|
|
|
|
2018-05-10 14:54:58 +02:00
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
2018-05-13 21:58:41 +02:00
|
|
|
import java.util.Map;
|
2018-05-10 14:54:58 +02:00
|
|
|
|
2018-05-14 16:53:27 +02:00
|
|
|
import javax.crypto.Cipher;
|
|
|
|
|
2018-05-10 14:54:58 +02:00
|
|
|
import me.impy.aegis.AegisApplication;
|
2018-10-10 21:08:16 +02:00
|
|
|
import me.impy.aegis.BuildConfig;
|
2018-05-10 14:54:58 +02:00
|
|
|
import me.impy.aegis.R;
|
|
|
|
import me.impy.aegis.db.DatabaseEntry;
|
2018-10-06 22:23:38 +02:00
|
|
|
import me.impy.aegis.db.DatabaseFileCredentials;
|
|
|
|
import me.impy.aegis.db.DatabaseFileException;
|
2018-05-10 14:54:58 +02:00
|
|
|
import me.impy.aegis.db.DatabaseManager;
|
|
|
|
import me.impy.aegis.db.DatabaseManagerException;
|
2018-05-14 16:53:27 +02:00
|
|
|
import me.impy.aegis.db.slots.Slot;
|
|
|
|
import me.impy.aegis.db.slots.SlotException;
|
2018-05-10 14:54:58 +02:00
|
|
|
import me.impy.aegis.helpers.PermissionHelper;
|
2018-05-10 18:42:47 +02:00
|
|
|
import me.impy.aegis.importers.AegisImporter;
|
2018-05-10 14:54:58 +02:00
|
|
|
import me.impy.aegis.importers.DatabaseImporter;
|
|
|
|
import me.impy.aegis.importers.DatabaseImporterException;
|
2018-05-14 16:53:27 +02:00
|
|
|
import me.impy.aegis.ui.dialogs.PasswordDialogFragment;
|
2018-05-14 17:26:17 +02:00
|
|
|
import me.impy.aegis.ui.preferences.SwitchPreference;
|
2018-05-10 14:54:58 +02:00
|
|
|
import me.impy.aegis.util.ByteInputStream;
|
|
|
|
|
2018-05-14 18:45:15 +02:00
|
|
|
public class PreferencesFragment extends PreferenceFragmentCompat implements PasswordDialogFragment.Listener {
|
2018-05-10 14:54:58 +02:00
|
|
|
// activity request codes
|
|
|
|
private static final int CODE_IMPORT = 0;
|
2018-05-10 18:42:47 +02:00
|
|
|
private static final int CODE_IMPORT_DECRYPT = 1;
|
2018-05-10 21:46:27 +02:00
|
|
|
private static final int CODE_SLOTS = 2;
|
2018-05-10 14:54:58 +02:00
|
|
|
|
|
|
|
// permission request codes
|
|
|
|
private static final int CODE_PERM_IMPORT = 0;
|
2018-05-10 19:34:42 +02:00
|
|
|
private static final int CODE_PERM_EXPORT = 1;
|
2018-05-10 14:54:58 +02:00
|
|
|
|
2018-05-11 21:30:15 +02:00
|
|
|
private Intent _result;
|
2018-05-10 14:54:58 +02:00
|
|
|
private DatabaseManager _db;
|
|
|
|
|
2018-05-10 18:42:47 +02:00
|
|
|
// this is used to keep a reference to a database converter
|
|
|
|
// while the user provides credentials to decrypt it
|
2018-05-13 21:58:41 +02:00
|
|
|
private DatabaseImporter _importer;
|
|
|
|
private Class<? extends DatabaseImporter> _importerType;
|
2018-05-10 18:42:47 +02:00
|
|
|
|
2018-05-14 16:53:27 +02:00
|
|
|
private SwitchPreference _encryptionPreference;
|
|
|
|
private Preference _slotsPreference;
|
|
|
|
|
2018-05-10 14:54:58 +02:00
|
|
|
@Override
|
2018-10-02 21:05:54 +02:00
|
|
|
public void onCreatePreferencesFix(Bundle savedInstanceState, String rootKey) {
|
2018-05-10 14:54:58 +02:00
|
|
|
addPreferencesFromResource(R.xml.preferences);
|
2018-05-10 18:42:47 +02:00
|
|
|
|
|
|
|
AegisApplication app = (AegisApplication) getActivity().getApplication();
|
|
|
|
_db = app.getDatabaseManager();
|
2018-05-10 14:54:58 +02:00
|
|
|
|
|
|
|
// set the result intent in advance
|
2018-05-11 21:30:15 +02:00
|
|
|
setResult(new Intent());
|
2018-05-10 14:54:58 +02:00
|
|
|
|
2018-05-10 23:30:38 +02:00
|
|
|
Preference darkModePreference = findPreference("pref_dark_mode");
|
|
|
|
darkModePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
2018-05-10 14:54:58 +02:00
|
|
|
@Override
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
2018-05-11 19:33:20 +02:00
|
|
|
_result.putExtra("needsRecreate", true);
|
2018-05-11 21:30:15 +02:00
|
|
|
getActivity().recreate();
|
2018-05-10 14:54:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Preference exportPreference = findPreference("pref_import");
|
|
|
|
exportPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
2018-05-10 19:34:42 +02:00
|
|
|
onImport();
|
2018-05-10 14:54:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Preference importPreference = findPreference("pref_export");
|
|
|
|
importPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
2018-05-10 19:34:42 +02:00
|
|
|
onExport();
|
2018-05-10 14:54:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-09-14 20:54:07 +02:00
|
|
|
/*EditTextPreference timeoutPreference = (EditTextPreference) findPreference("pref_timeout");
|
2018-05-10 14:54:58 +02:00
|
|
|
timeoutPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
|
|
|
preference.setSummary(String.format(getString(R.string.pref_timeout_summary), (String) newValue));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2018-09-14 20:54:07 +02:00
|
|
|
timeoutPreference.getOnPreferenceChangeListener().onPreferenceChange(timeoutPreference, timeoutPreference.getText());*/
|
2018-05-10 14:54:58 +02:00
|
|
|
|
2018-09-19 00:10:03 +02:00
|
|
|
Preference issuerPreference = findPreference("pref_account_name");
|
2018-05-10 14:54:58 +02:00
|
|
|
issuerPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
|
|
|
_result.putExtra("needsRefresh", true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2018-05-11 19:33:20 +02:00
|
|
|
|
|
|
|
Preference screenPreference = findPreference("pref_secure_screen");
|
|
|
|
screenPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
|
|
|
_result.putExtra("needsRecreate", true);
|
|
|
|
Window window = getActivity().getWindow();
|
|
|
|
if ((boolean)newValue) {
|
|
|
|
window.addFlags(WindowManager.LayoutParams.FLAG_SECURE);
|
|
|
|
} else {
|
|
|
|
window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2018-05-14 16:53:27 +02:00
|
|
|
|
|
|
|
_encryptionPreference = (SwitchPreference) findPreference("pref_encryption");
|
|
|
|
_encryptionPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
2018-10-06 22:23:38 +02:00
|
|
|
if (!_db.isEncryptionEnabled()) {
|
2018-05-14 16:53:27 +02:00
|
|
|
PasswordDialogFragment dialog = new PasswordDialogFragment();
|
|
|
|
// TODO: find a less ugly way to obtain the fragment manager
|
2018-06-09 21:40:18 +02:00
|
|
|
dialog.show(getActivity().getSupportFragmentManager(), null);
|
2018-05-14 16:53:27 +02:00
|
|
|
} else {
|
|
|
|
new AlertDialog.Builder(getActivity())
|
2018-10-09 23:13:51 +02:00
|
|
|
.setTitle(getString(R.string.disable_encryption))
|
|
|
|
.setMessage(getString(R.string.disable_encryption_description))
|
2018-05-14 16:53:27 +02:00
|
|
|
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
2018-10-06 22:23:38 +02:00
|
|
|
try {
|
|
|
|
_db.disableEncryption();
|
|
|
|
} catch (DatabaseManagerException e) {
|
2018-10-09 23:13:51 +02:00
|
|
|
Toast.makeText(getActivity(), getString(R.string.encrypting_error), Toast.LENGTH_SHORT).show();
|
2018-10-06 22:23:38 +02:00
|
|
|
}
|
2018-05-14 16:53:27 +02:00
|
|
|
updateEncryptionPreference();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.setNegativeButton(android.R.string.no, null)
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2018-10-10 20:41:37 +02:00
|
|
|
|
|
|
|
Preference setPasswordPreference = findPreference("pref_password");
|
|
|
|
setPasswordPreference.setOnPreferenceClickListener(preference -> {
|
|
|
|
PasswordDialogFragment dialog = new PasswordDialogFragment();
|
|
|
|
// TODO: find a less ugly way to obtain the fragment manager
|
|
|
|
dialog.show(getActivity().getSupportFragmentManager(), null);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2018-05-14 16:53:27 +02:00
|
|
|
_slotsPreference = findPreference("pref_slots");
|
2018-10-10 21:08:16 +02:00
|
|
|
if (BuildConfig.DEBUG) {
|
|
|
|
_slotsPreference.setVisible(true);
|
|
|
|
} else {
|
|
|
|
_slotsPreference.setVisible(false);
|
|
|
|
}
|
2018-05-14 16:53:27 +02:00
|
|
|
_slotsPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
|
|
Intent intent = new Intent(getActivity(), SlotManagerActivity.class);
|
2018-10-06 22:23:38 +02:00
|
|
|
intent.putExtra("creds", _db.getCredentials());
|
2018-05-14 16:53:27 +02:00
|
|
|
startActivityForResult(intent, CODE_SLOTS);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
updateEncryptionPreference();
|
2018-05-10 14:54:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
|
|
|
if (!PermissionHelper.checkResults(grantResults)) {
|
2018-10-09 23:13:51 +02:00
|
|
|
Toast.makeText(getActivity(), getString(R.string.permission_denied), Toast.LENGTH_SHORT).show();
|
2018-05-10 14:54:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (requestCode) {
|
|
|
|
case CODE_PERM_IMPORT:
|
|
|
|
onImport();
|
|
|
|
break;
|
2018-05-10 19:34:42 +02:00
|
|
|
case CODE_PERM_EXPORT:
|
|
|
|
onExport();
|
|
|
|
break;
|
2018-05-10 14:54:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
if (data == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (requestCode) {
|
|
|
|
case CODE_IMPORT:
|
|
|
|
onImportResult(resultCode, data);
|
|
|
|
break;
|
2018-05-10 18:42:47 +02:00
|
|
|
case CODE_IMPORT_DECRYPT:
|
|
|
|
onImportDecryptResult(resultCode, data);
|
|
|
|
break;
|
2018-05-10 21:46:27 +02:00
|
|
|
case CODE_SLOTS:
|
|
|
|
onSlotManagerResult(resultCode, data);
|
|
|
|
break;
|
2018-05-10 14:54:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-11 21:30:15 +02:00
|
|
|
public Intent getResult() {
|
|
|
|
return _result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setResult(Intent result) {
|
|
|
|
_result = result;
|
|
|
|
getActivity().setResult(Activity.RESULT_OK, _result);
|
|
|
|
}
|
|
|
|
|
2018-05-10 14:54:58 +02:00
|
|
|
private void onImport() {
|
2018-06-07 12:34:20 +02:00
|
|
|
if (!PermissionHelper.request(getActivity(), CODE_PERM_IMPORT, Manifest.permission.READ_EXTERNAL_STORAGE)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-13 21:58:41 +02:00
|
|
|
Map<String, Class<? extends DatabaseImporter>> importers = DatabaseImporter.getImporters();
|
|
|
|
String[] names = importers.keySet().toArray(new String[importers.size()]);
|
|
|
|
|
|
|
|
new AlertDialog.Builder(getActivity())
|
2018-10-09 23:13:51 +02:00
|
|
|
.setTitle(getString(R.string.choose_application))
|
2018-05-13 21:58:41 +02:00
|
|
|
.setSingleChoiceItems(names, 0, null)
|
|
|
|
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
int i = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
|
|
|
|
_importerType = importers.get(names[i]);
|
|
|
|
|
2018-06-07 12:34:20 +02:00
|
|
|
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
|
|
|
intent.setType("*/*");
|
|
|
|
startActivityForResult(intent, CODE_IMPORT);
|
2018-05-13 21:58:41 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.show();
|
2018-05-10 14:54:58 +02:00
|
|
|
}
|
|
|
|
|
2018-05-10 18:42:47 +02:00
|
|
|
private void onImportDecryptResult(int resultCode, Intent data) {
|
2018-05-10 14:54:58 +02:00
|
|
|
if (resultCode != Activity.RESULT_OK) {
|
2018-05-13 21:58:41 +02:00
|
|
|
_importer = null;
|
2018-05-10 14:54:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-06 22:23:38 +02:00
|
|
|
DatabaseFileCredentials creds = (DatabaseFileCredentials) data.getSerializableExtra("creds");
|
|
|
|
((AegisImporter)_importer).setCredentials(creds);
|
2018-05-10 14:54:58 +02:00
|
|
|
|
|
|
|
try {
|
2018-05-13 21:58:41 +02:00
|
|
|
importDatabase(_importer);
|
2018-05-10 18:42:47 +02:00
|
|
|
} catch (DatabaseImporterException e) {
|
2018-10-09 23:13:51 +02:00
|
|
|
Toast.makeText(getActivity(), getString(R.string.parsing_file_error), Toast.LENGTH_SHORT).show();
|
2018-05-10 18:42:47 +02:00
|
|
|
}
|
2018-05-10 21:46:27 +02:00
|
|
|
|
2018-05-13 21:58:41 +02:00
|
|
|
_importer = null;
|
2018-05-10 18:42:47 +02:00
|
|
|
}
|
2018-05-10 14:54:58 +02:00
|
|
|
|
2018-05-10 18:42:47 +02:00
|
|
|
private void onImportResult(int resultCode, Intent data) {
|
|
|
|
Uri uri = data.getData();
|
|
|
|
if (resultCode != Activity.RESULT_OK || uri == null) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-10 14:54:58 +02:00
|
|
|
|
2018-05-10 18:42:47 +02:00
|
|
|
ByteInputStream stream;
|
|
|
|
|
2018-10-06 22:23:38 +02:00
|
|
|
try (InputStream fileStream = getActivity().getContentResolver().openInputStream(uri)) {
|
2018-05-10 18:42:47 +02:00
|
|
|
stream = ByteInputStream.create(fileStream);
|
|
|
|
} catch (FileNotFoundException e) {
|
2018-10-09 23:13:51 +02:00
|
|
|
Toast.makeText(getActivity(), getString(R.string.file_not_found), Toast.LENGTH_SHORT).show();
|
2018-05-10 18:42:47 +02:00
|
|
|
return;
|
|
|
|
} catch (IOException e) {
|
2018-10-06 22:23:38 +02:00
|
|
|
e.printStackTrace();
|
2018-10-09 23:13:51 +02:00
|
|
|
Toast.makeText(getActivity(), getString(R.string.reading_file_error), Toast.LENGTH_SHORT).show();
|
2018-05-10 18:42:47 +02:00
|
|
|
return;
|
2018-05-10 14:54:58 +02:00
|
|
|
}
|
|
|
|
|
2018-05-13 21:58:41 +02:00
|
|
|
try {
|
|
|
|
DatabaseImporter importer = DatabaseImporter.create(stream, _importerType);
|
|
|
|
importer.parse();
|
2018-05-10 18:42:47 +02:00
|
|
|
|
2018-05-13 21:58:41 +02:00
|
|
|
// special case to decrypt encrypted aegis databases
|
|
|
|
if (importer.isEncrypted() && importer instanceof AegisImporter) {
|
|
|
|
_importer = importer;
|
2018-05-10 18:42:47 +02:00
|
|
|
|
2018-05-13 21:58:41 +02:00
|
|
|
Intent intent = new Intent(getActivity(), AuthActivity.class);
|
2018-10-06 22:23:38 +02:00
|
|
|
intent.putExtra("slots", ((AegisImporter)_importer).getFile().getHeader().getSlots());
|
2018-05-13 21:58:41 +02:00
|
|
|
startActivityForResult(intent, CODE_IMPORT_DECRYPT);
|
|
|
|
return;
|
2018-05-10 18:42:47 +02:00
|
|
|
}
|
|
|
|
|
2018-05-13 21:58:41 +02:00
|
|
|
importDatabase(importer);
|
|
|
|
} catch (DatabaseImporterException e) {
|
|
|
|
e.printStackTrace();
|
2018-10-09 23:13:51 +02:00
|
|
|
Toast.makeText(getActivity(), getString(R.string.parsing_file_error), Toast.LENGTH_SHORT).show();
|
2018-05-10 14:54:58 +02:00
|
|
|
}
|
2018-05-10 18:42:47 +02:00
|
|
|
}
|
2018-05-10 14:54:58 +02:00
|
|
|
|
2018-05-13 21:58:41 +02:00
|
|
|
private void importDatabase(DatabaseImporter importer) throws DatabaseImporterException {
|
|
|
|
List<DatabaseEntry> entries = importer.convert();
|
2018-05-10 14:54:58 +02:00
|
|
|
for (DatabaseEntry entry : entries) {
|
2018-09-22 14:12:42 +02:00
|
|
|
// temporary: randomize the UUID of duplicate entries and add them anyway
|
|
|
|
if (_db.getEntryByUUID(entry.getUUID()) != null) {
|
|
|
|
entry.resetUUID();
|
|
|
|
}
|
|
|
|
|
2018-06-06 16:15:31 +02:00
|
|
|
_db.addEntry(entry);
|
2018-05-10 14:54:58 +02:00
|
|
|
}
|
|
|
|
|
2018-05-10 21:46:27 +02:00
|
|
|
if (!saveDatabase()) {
|
2018-05-10 14:54:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-11 19:33:20 +02:00
|
|
|
_result.putExtra("needsRecreate", true);
|
2018-10-09 23:13:51 +02:00
|
|
|
Toast.makeText(getActivity(), String.format(Locale.getDefault(), getString(R.string.imported_entries_count), entries.size()), Toast.LENGTH_LONG).show();
|
2018-05-10 14:54:58 +02:00
|
|
|
}
|
2018-05-10 19:34:42 +02:00
|
|
|
|
|
|
|
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) {
|
2018-10-09 23:13:51 +02:00
|
|
|
Toast.makeText(getActivity(), getString(R.string.exporting_database_error), Toast.LENGTH_SHORT).show();
|
2018-05-10 19:34:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure the new file is visible
|
|
|
|
MediaScannerConnection.scanFile(getActivity(), new String[]{filename}, null, null);
|
|
|
|
|
2018-10-09 23:13:51 +02:00
|
|
|
Toast.makeText(getActivity(), getString(R.string.export_database_location) + filename, Toast.LENGTH_SHORT).show();
|
2018-05-10 19:34:42 +02:00
|
|
|
})
|
|
|
|
.setNegativeButton(android.R.string.cancel, null);
|
2018-10-06 22:23:38 +02:00
|
|
|
if (_db.isEncryptionEnabled()) {
|
2018-05-10 19:34:42 +02:00
|
|
|
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 {
|
2018-10-09 23:13:51 +02:00
|
|
|
builder.setMessage(getString(R.string.export_warning));
|
2018-05-10 19:34:42 +02:00
|
|
|
}
|
|
|
|
builder.show();
|
|
|
|
}
|
2018-05-10 21:46:27 +02:00
|
|
|
|
|
|
|
private void onSlotManagerResult(int resultCode, Intent data) {
|
|
|
|
if (resultCode != Activity.RESULT_OK) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-06 22:23:38 +02:00
|
|
|
DatabaseFileCredentials creds = (DatabaseFileCredentials) data.getSerializableExtra("creds");
|
|
|
|
_db.setCredentials(creds);
|
2018-05-10 21:46:27 +02:00
|
|
|
saveDatabase();
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean saveDatabase() {
|
|
|
|
try {
|
|
|
|
_db.save();
|
|
|
|
} catch (DatabaseManagerException e) {
|
2018-10-09 23:13:51 +02:00
|
|
|
Toast.makeText(getActivity(), getString(R.string.saving_error), Toast.LENGTH_LONG).show();
|
2018-05-10 21:46:27 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-14 16:53:27 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSlotResult(Slot slot, Cipher cipher) {
|
2018-10-06 22:23:38 +02:00
|
|
|
DatabaseFileCredentials creds = new DatabaseFileCredentials();
|
2018-05-14 16:53:27 +02:00
|
|
|
|
|
|
|
try {
|
2018-10-06 22:23:38 +02:00
|
|
|
slot.setKey(creds.getKey(), cipher);
|
|
|
|
creds.getSlots().add(slot);
|
|
|
|
_db.enableEncryption(creds);
|
|
|
|
} catch (DatabaseManagerException | SlotException e) {
|
2018-05-14 16:53:27 +02:00
|
|
|
onException(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateEncryptionPreference();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onException(Exception e) {
|
|
|
|
updateEncryptionPreference();
|
2018-10-09 23:13:51 +02:00
|
|
|
Toast.makeText(getActivity(), getString(R.string.encryption_set_password_error) + e.getMessage(), Toast.LENGTH_SHORT).show();
|
2018-05-14 16:53:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void updateEncryptionPreference() {
|
2018-10-06 22:23:38 +02:00
|
|
|
boolean encrypted = _db.isEncryptionEnabled();
|
2018-05-14 17:26:17 +02:00
|
|
|
_encryptionPreference.setChecked(encrypted, true);
|
2018-05-14 16:53:27 +02:00
|
|
|
_slotsPreference.setEnabled(encrypted);
|
|
|
|
}
|
2018-05-10 18:42:47 +02:00
|
|
|
}
|