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-05-14 18:45:15 +02:00
|
|
|
import android.support.v7.preference.EditTextPreference;
|
2018-05-10 19:34:42 +02:00
|
|
|
import android.support.v7.app.AlertDialog;
|
2018-05-14 16:53:27 +02:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
2018-05-14 18:45:15 +02:00
|
|
|
import android.support.v7.preference.Preference;
|
|
|
|
import com.takisoft.fix.support.v7.preference.PreferenceFragmentCompat;
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
import me.impy.aegis.R;
|
2018-05-10 18:42:47 +02:00
|
|
|
import me.impy.aegis.crypto.MasterKey;
|
2018-05-10 14:54:58 +02:00
|
|
|
import me.impy.aegis.db.DatabaseEntry;
|
|
|
|
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;
|
2018-06-06 19:38:13 +02:00
|
|
|
import me.impy.aegis.db.slots.SlotList;
|
2018-05-14 16:53:27 +02:00
|
|
|
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-05-14 18:45:15 +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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
EditTextPreference timeoutPreference = (EditTextPreference) findPreference("pref_timeout");
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
timeoutPreference.getOnPreferenceChangeListener().onPreferenceChange(timeoutPreference, timeoutPreference.getText());
|
|
|
|
|
|
|
|
Preference issuerPreference = findPreference("pref_issuer");
|
|
|
|
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) {
|
|
|
|
if (!_db.getFile().isEncrypted()) {
|
|
|
|
PasswordDialogFragment dialog = new PasswordDialogFragment();
|
|
|
|
// TODO: find a less ugly way to obtain the fragment manager
|
|
|
|
dialog.show(((AppCompatActivity)getActivity()).getSupportFragmentManager(), null);
|
|
|
|
} else {
|
|
|
|
new AlertDialog.Builder(getActivity())
|
|
|
|
.setTitle("Disable encryption")
|
|
|
|
.setMessage("Are you sure you want to disable encryption? This will cause the database to be stored in plain text")
|
|
|
|
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
_db.disableEncryption();
|
|
|
|
saveDatabase();
|
|
|
|
updateEncryptionPreference();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.setNegativeButton(android.R.string.no, null)
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
_slotsPreference = findPreference("pref_slots");
|
|
|
|
_slotsPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
|
|
MasterKey masterKey = _db.getMasterKey();
|
|
|
|
Intent intent = new Intent(getActivity(), SlotManagerActivity.class);
|
|
|
|
intent.putExtra("masterKey", masterKey);
|
|
|
|
intent.putExtra("slots", _db.getFile().getSlots());
|
|
|
|
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)) {
|
|
|
|
Toast.makeText(getActivity(), "Permission denied", Toast.LENGTH_SHORT).show();
|
|
|
|
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-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())
|
|
|
|
.setTitle("Select the application you'd like to import a database from")
|
|
|
|
.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]);
|
|
|
|
|
|
|
|
if (PermissionHelper.request(getActivity(), CODE_PERM_IMPORT, Manifest.permission.READ_EXTERNAL_STORAGE)) {
|
|
|
|
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
|
|
|
intent.setType("*/*");
|
|
|
|
startActivityForResult(intent, CODE_IMPORT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.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-05-10 18:42:47 +02:00
|
|
|
MasterKey key = (MasterKey) data.getSerializableExtra("key");
|
2018-05-13 21:58:41 +02:00
|
|
|
((AegisImporter)_importer).setKey(key);
|
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) {
|
|
|
|
e.printStackTrace();
|
|
|
|
Toast.makeText(getActivity(), "An error occurred while trying to parse the file", Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
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;
|
|
|
|
InputStream fileStream = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
fileStream = getActivity().getContentResolver().openInputStream(uri);
|
|
|
|
stream = ByteInputStream.create(fileStream);
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
Toast.makeText(getActivity(), "Error: File not found", Toast.LENGTH_SHORT).show();
|
|
|
|
return;
|
|
|
|
} catch (IOException e) {
|
|
|
|
Toast.makeText(getActivity(), "An error occurred while trying to read the file", Toast.LENGTH_SHORT).show();
|
|
|
|
return;
|
2018-05-10 14:54:58 +02:00
|
|
|
} finally {
|
|
|
|
if (fileStream != null) {
|
|
|
|
try {
|
|
|
|
fileStream.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
intent.putExtra("slots", ((AegisImporter)_importer).getFile().getSlots());
|
|
|
|
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-05-10 14:54:58 +02:00
|
|
|
Toast.makeText(getActivity(), "An error occurred while trying to parse the file", Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
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-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-05-10 18:42:47 +02:00
|
|
|
Toast.makeText(getActivity(), String.format(Locale.getDefault(), "Imported %d entries", 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) {
|
|
|
|
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();
|
|
|
|
}
|
2018-05-10 21:46:27 +02:00
|
|
|
|
|
|
|
private void onSlotManagerResult(int resultCode, Intent data) {
|
|
|
|
if (resultCode != Activity.RESULT_OK) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-06 19:38:13 +02:00
|
|
|
SlotList slots = (SlotList) data.getSerializableExtra("slots");
|
2018-05-10 21:46:27 +02:00
|
|
|
_db.getFile().setSlots(slots);
|
|
|
|
saveDatabase();
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean saveDatabase() {
|
|
|
|
try {
|
|
|
|
_db.save();
|
|
|
|
} catch (DatabaseManagerException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
Toast.makeText(getActivity(), "An error occurred while trying to save the database", Toast.LENGTH_LONG).show();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-14 16:53:27 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSlotResult(Slot slot, Cipher cipher) {
|
|
|
|
MasterKey masterKey = MasterKey.generate();
|
|
|
|
|
2018-06-06 19:38:13 +02:00
|
|
|
SlotList slots = new SlotList();
|
2018-05-14 16:53:27 +02:00
|
|
|
try {
|
2018-06-06 19:38:13 +02:00
|
|
|
slot.setKey(masterKey, cipher);
|
2018-05-14 16:53:27 +02:00
|
|
|
} catch (SlotException e) {
|
|
|
|
onException(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
slots.add(slot);
|
|
|
|
_db.enableEncryption(masterKey, slots);
|
|
|
|
|
|
|
|
saveDatabase();
|
|
|
|
updateEncryptionPreference();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onException(Exception e) {
|
|
|
|
updateEncryptionPreference();
|
|
|
|
Toast.makeText(getActivity(), "An error occurred while trying to set the password: " + e.getMessage(), Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateEncryptionPreference() {
|
|
|
|
boolean encrypted = _db.getFile().isEncrypted();
|
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
|
|
|
}
|