|
|
@ -26,12 +26,12 @@ import javax.crypto.Cipher;
|
|
|
|
|
|
|
|
|
|
|
|
import me.impy.aegis.AegisApplication;
|
|
|
|
import me.impy.aegis.AegisApplication;
|
|
|
|
import me.impy.aegis.R;
|
|
|
|
import me.impy.aegis.R;
|
|
|
|
import me.impy.aegis.crypto.MasterKey;
|
|
|
|
|
|
|
|
import me.impy.aegis.db.DatabaseEntry;
|
|
|
|
import me.impy.aegis.db.DatabaseEntry;
|
|
|
|
|
|
|
|
import me.impy.aegis.db.DatabaseFileCredentials;
|
|
|
|
|
|
|
|
import me.impy.aegis.db.DatabaseFileException;
|
|
|
|
import me.impy.aegis.db.DatabaseManager;
|
|
|
|
import me.impy.aegis.db.DatabaseManager;
|
|
|
|
import me.impy.aegis.db.DatabaseManagerException;
|
|
|
|
import me.impy.aegis.db.DatabaseManagerException;
|
|
|
|
import me.impy.aegis.db.slots.Slot;
|
|
|
|
import me.impy.aegis.db.slots.Slot;
|
|
|
|
import me.impy.aegis.db.slots.SlotList;
|
|
|
|
|
|
|
|
import me.impy.aegis.db.slots.SlotException;
|
|
|
|
import me.impy.aegis.db.slots.SlotException;
|
|
|
|
import me.impy.aegis.helpers.PermissionHelper;
|
|
|
|
import me.impy.aegis.helpers.PermissionHelper;
|
|
|
|
import me.impy.aegis.importers.AegisImporter;
|
|
|
|
import me.impy.aegis.importers.AegisImporter;
|
|
|
@ -138,7 +138,7 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Pas
|
|
|
|
_encryptionPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
|
|
|
_encryptionPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
|
|
|
if (!_db.getFile().isEncrypted()) {
|
|
|
|
if (!_db.isEncryptionEnabled()) {
|
|
|
|
PasswordDialogFragment dialog = new PasswordDialogFragment();
|
|
|
|
PasswordDialogFragment dialog = new PasswordDialogFragment();
|
|
|
|
// TODO: find a less ugly way to obtain the fragment manager
|
|
|
|
// TODO: find a less ugly way to obtain the fragment manager
|
|
|
|
dialog.show(getActivity().getSupportFragmentManager(), null);
|
|
|
|
dialog.show(getActivity().getSupportFragmentManager(), null);
|
|
|
@ -148,8 +148,11 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Pas
|
|
|
|
.setMessage("Are you sure you want to disable encryption? This will cause the database to be stored in plain text")
|
|
|
|
.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() {
|
|
|
|
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
_db.disableEncryption();
|
|
|
|
_db.disableEncryption();
|
|
|
|
saveDatabase();
|
|
|
|
} catch (DatabaseManagerException e) {
|
|
|
|
|
|
|
|
Toast.makeText(getActivity(), "An error occurred while enabling encryption", Toast.LENGTH_SHORT).show();
|
|
|
|
|
|
|
|
}
|
|
|
|
updateEncryptionPreference();
|
|
|
|
updateEncryptionPreference();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
@ -163,10 +166,8 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Pas
|
|
|
|
_slotsPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
|
|
|
_slotsPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
|
|
MasterKey masterKey = _db.getMasterKey();
|
|
|
|
|
|
|
|
Intent intent = new Intent(getActivity(), SlotManagerActivity.class);
|
|
|
|
Intent intent = new Intent(getActivity(), SlotManagerActivity.class);
|
|
|
|
intent.putExtra("masterKey", masterKey);
|
|
|
|
intent.putExtra("creds", _db.getCredentials());
|
|
|
|
intent.putExtra("slots", _db.getFile().getSlots());
|
|
|
|
|
|
|
|
startActivityForResult(intent, CODE_SLOTS);
|
|
|
|
startActivityForResult(intent, CODE_SLOTS);
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -249,13 +250,12 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Pas
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MasterKey key = (MasterKey) data.getSerializableExtra("key");
|
|
|
|
DatabaseFileCredentials creds = (DatabaseFileCredentials) data.getSerializableExtra("creds");
|
|
|
|
((AegisImporter)_importer).setKey(key);
|
|
|
|
((AegisImporter)_importer).setCredentials(creds);
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
importDatabase(_importer);
|
|
|
|
importDatabase(_importer);
|
|
|
|
} catch (DatabaseImporterException e) {
|
|
|
|
} catch (DatabaseImporterException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
Toast.makeText(getActivity(), "An error occurred while trying to parse the file", Toast.LENGTH_SHORT).show();
|
|
|
|
Toast.makeText(getActivity(), "An error occurred while trying to parse the file", Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -269,25 +269,16 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Pas
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ByteInputStream stream;
|
|
|
|
ByteInputStream stream;
|
|
|
|
InputStream fileStream = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try (InputStream fileStream = getActivity().getContentResolver().openInputStream(uri)) {
|
|
|
|
fileStream = getActivity().getContentResolver().openInputStream(uri);
|
|
|
|
|
|
|
|
stream = ByteInputStream.create(fileStream);
|
|
|
|
stream = ByteInputStream.create(fileStream);
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
Toast.makeText(getActivity(), "Error: File not found", Toast.LENGTH_SHORT).show();
|
|
|
|
Toast.makeText(getActivity(), "Error: File not found", Toast.LENGTH_SHORT).show();
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
Toast.makeText(getActivity(), "An error occurred while trying to read the file", Toast.LENGTH_SHORT).show();
|
|
|
|
Toast.makeText(getActivity(), "An error occurred while trying to read the file", Toast.LENGTH_SHORT).show();
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
if (fileStream != null) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
fileStream.close();
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -299,7 +290,7 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Pas
|
|
|
|
_importer = importer;
|
|
|
|
_importer = importer;
|
|
|
|
|
|
|
|
|
|
|
|
Intent intent = new Intent(getActivity(), AuthActivity.class);
|
|
|
|
Intent intent = new Intent(getActivity(), AuthActivity.class);
|
|
|
|
intent.putExtra("slots", ((AegisImporter)_importer).getFile().getSlots());
|
|
|
|
intent.putExtra("slots", ((AegisImporter)_importer).getFile().getHeader().getSlots());
|
|
|
|
startActivityForResult(intent, CODE_IMPORT_DECRYPT);
|
|
|
|
startActivityForResult(intent, CODE_IMPORT_DECRYPT);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -344,7 +335,6 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Pas
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
filename = _db.export(checked[0]);
|
|
|
|
filename = _db.export(checked[0]);
|
|
|
|
} catch (DatabaseManagerException e) {
|
|
|
|
} catch (DatabaseManagerException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
Toast.makeText(getActivity(), "An error occurred while trying to export the database", Toast.LENGTH_SHORT).show();
|
|
|
|
Toast.makeText(getActivity(), "An error occurred while trying to export the database", Toast.LENGTH_SHORT).show();
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -355,7 +345,7 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Pas
|
|
|
|
Toast.makeText(getActivity(), "The database has been exported to: " + filename, Toast.LENGTH_SHORT).show();
|
|
|
|
Toast.makeText(getActivity(), "The database has been exported to: " + filename, Toast.LENGTH_SHORT).show();
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.setNegativeButton(android.R.string.cancel, null);
|
|
|
|
.setNegativeButton(android.R.string.cancel, null);
|
|
|
|
if (_db.getFile().isEncrypted()) {
|
|
|
|
if (_db.isEncryptionEnabled()) {
|
|
|
|
final String[] items = {"Keep the database encrypted"};
|
|
|
|
final String[] items = {"Keep the database encrypted"};
|
|
|
|
final boolean[] checkedItems = {true};
|
|
|
|
final boolean[] checkedItems = {true};
|
|
|
|
builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
|
|
|
|
builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
|
|
|
@ -375,8 +365,8 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Pas
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SlotList slots = (SlotList) data.getSerializableExtra("slots");
|
|
|
|
DatabaseFileCredentials creds = (DatabaseFileCredentials) data.getSerializableExtra("creds");
|
|
|
|
_db.getFile().setSlots(slots);
|
|
|
|
_db.setCredentials(creds);
|
|
|
|
saveDatabase();
|
|
|
|
saveDatabase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -384,7 +374,6 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Pas
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
_db.save();
|
|
|
|
_db.save();
|
|
|
|
} catch (DatabaseManagerException e) {
|
|
|
|
} catch (DatabaseManagerException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
Toast.makeText(getActivity(), "An error occurred while trying to save the database", Toast.LENGTH_LONG).show();
|
|
|
|
Toast.makeText(getActivity(), "An error occurred while trying to save the database", Toast.LENGTH_LONG).show();
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -394,19 +383,17 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Pas
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onSlotResult(Slot slot, Cipher cipher) {
|
|
|
|
public void onSlotResult(Slot slot, Cipher cipher) {
|
|
|
|
MasterKey masterKey = MasterKey.generate();
|
|
|
|
DatabaseFileCredentials creds = new DatabaseFileCredentials();
|
|
|
|
|
|
|
|
|
|
|
|
SlotList slots = new SlotList();
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
slot.setKey(masterKey, cipher);
|
|
|
|
slot.setKey(creds.getKey(), cipher);
|
|
|
|
} catch (SlotException e) {
|
|
|
|
creds.getSlots().add(slot);
|
|
|
|
|
|
|
|
_db.enableEncryption(creds);
|
|
|
|
|
|
|
|
} catch (DatabaseManagerException | SlotException e) {
|
|
|
|
onException(e);
|
|
|
|
onException(e);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
slots.add(slot);
|
|
|
|
|
|
|
|
_db.enableEncryption(masterKey, slots);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
saveDatabase();
|
|
|
|
|
|
|
|
updateEncryptionPreference();
|
|
|
|
updateEncryptionPreference();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -417,7 +404,7 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Pas
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void updateEncryptionPreference() {
|
|
|
|
private void updateEncryptionPreference() {
|
|
|
|
boolean encrypted = _db.getFile().isEncrypted();
|
|
|
|
boolean encrypted = _db.isEncryptionEnabled();
|
|
|
|
_encryptionPreference.setChecked(encrypted, true);
|
|
|
|
_encryptionPreference.setChecked(encrypted, true);
|
|
|
|
_slotsPreference.setEnabled(encrypted);
|
|
|
|
_slotsPreference.setEnabled(encrypted);
|
|
|
|
}
|
|
|
|
}
|
|
|
|