mirror of
https://github.com/beemdevelopment/Aegis.git
synced 2025-04-21 14:29:13 +00:00
Add more assertions to DatabaseManager
This commit is contained in:
parent
2fce0f4dcc
commit
ff214eaa4a
2 changed files with 25 additions and 22 deletions
|
@ -91,14 +91,14 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis
|
||||||
touchHelper.attachToRecyclerView(rvKeyProfiles);
|
touchHelper.attachToRecyclerView(rvKeyProfiles);
|
||||||
rvKeyProfiles.setAdapter(_keyProfileAdapter);
|
rvKeyProfiles.setAdapter(_keyProfileAdapter);
|
||||||
|
|
||||||
if (!_app.isRunning() && !_db.isUnlocked()) {
|
if (!_app.isRunning() && _db.isLocked()) {
|
||||||
if (!_app.getPreferences().getBoolean("pref_intro", false)) {
|
if (!_app.getPreferences().getBoolean("pref_intro", false)) {
|
||||||
Intent intro = new Intent(this, IntroActivity.class);
|
Intent intro = new Intent(this, IntroActivity.class);
|
||||||
startActivityForResult(intro, CODE_DO_INTRO);
|
startActivityForResult(intro, CODE_DO_INTRO);
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
_db.load();
|
_db.load();
|
||||||
if (!_db.isUnlocked()) {
|
if (_db.isLocked()) {
|
||||||
startAuthActivity();
|
startAuthActivity();
|
||||||
}
|
}
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
|
@ -114,7 +114,7 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_db.isUnlocked()) {
|
if (!_db.isLocked()) {
|
||||||
loadKeyProfiles();
|
loadKeyProfiles();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -346,7 +346,7 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis
|
||||||
MasterKey key = (MasterKey) data.getSerializableExtra("key");
|
MasterKey key = (MasterKey) data.getSerializableExtra("key");
|
||||||
try {
|
try {
|
||||||
_db.load();
|
_db.load();
|
||||||
if (!_db.isUnlocked()) {
|
if (_db.isLocked()) {
|
||||||
_db.unlock(key);
|
_db.unlock(key);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -377,7 +377,7 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis
|
||||||
private void doShortcutActions() {
|
private void doShortcutActions() {
|
||||||
Intent intent = getIntent();
|
Intent intent = getIntent();
|
||||||
String mode = intent.getStringExtra("Action");
|
String mode = intent.getStringExtra("Action");
|
||||||
if (mode == null || !_db.isUnlocked()) {
|
if (mode == null || _db.isLocked()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -556,7 +556,7 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis
|
||||||
|
|
||||||
private void updateLockIcon() {
|
private void updateLockIcon() {
|
||||||
// hide the lock icon if the database is not unlocked
|
// hide the lock icon if the database is not unlocked
|
||||||
if (_menu != null && _db.isUnlocked()) {
|
if (_menu != null && !_db.isLocked()) {
|
||||||
MenuItem item = _menu.findItem(R.id.action_lock);
|
MenuItem item = _menu.findItem(R.id.action_lock);
|
||||||
item.setVisible(_db.getFile().isEncrypted());
|
item.setVisible(_db.getFile().isEncrypted());
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,8 @@ public class DatabaseManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void load() throws Exception {
|
public void load() throws Exception {
|
||||||
|
assertState(true, false);
|
||||||
|
|
||||||
byte[] fileBytes;
|
byte[] fileBytes;
|
||||||
FileInputStream file = null;
|
FileInputStream file = null;
|
||||||
|
|
||||||
|
@ -58,14 +60,14 @@ public class DatabaseManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void lock() throws Exception {
|
public void lock() throws Exception {
|
||||||
assertUnlocked();
|
assertState(false, true);
|
||||||
// TODO: properly clear everything
|
// TODO: properly clear everything
|
||||||
_key = null;
|
_key = null;
|
||||||
_db = null;
|
_db = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void unlock(MasterKey key) throws Exception {
|
public void unlock(MasterKey key) throws Exception {
|
||||||
assertLoaded();
|
assertState(true, true);
|
||||||
byte[] encrypted = _file.getContent();
|
byte[] encrypted = _file.getContent();
|
||||||
CryptParameters params = _file.getCryptParameters();
|
CryptParameters params = _file.getCryptParameters();
|
||||||
CryptResult result = key.decrypt(encrypted, params);
|
CryptResult result = key.decrypt(encrypted, params);
|
||||||
|
@ -90,7 +92,7 @@ public class DatabaseManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save() throws Exception {
|
public void save() throws Exception {
|
||||||
assertUnlocked();
|
assertState(false, true);
|
||||||
byte[] dbBytes = _db.serialize();
|
byte[] dbBytes = _db.serialize();
|
||||||
if (!_file.isEncrypted()) {
|
if (!_file.isEncrypted()) {
|
||||||
_file.setContent(dbBytes);
|
_file.setContent(dbBytes);
|
||||||
|
@ -103,7 +105,7 @@ public class DatabaseManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String export(boolean encrypt) throws Exception {
|
public String export(boolean encrypt) throws Exception {
|
||||||
assertUnlocked();
|
assertState(false, true);
|
||||||
byte[] bytes = _db.serialize();
|
byte[] bytes = _db.serialize();
|
||||||
encrypt = encrypt && getFile().isEncrypted();
|
encrypt = encrypt && getFile().isEncrypted();
|
||||||
if (encrypt) {
|
if (encrypt) {
|
||||||
|
@ -136,22 +138,22 @@ public class DatabaseManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addKey(DatabaseEntry entry) throws Exception {
|
public void addKey(DatabaseEntry entry) throws Exception {
|
||||||
assertUnlocked();
|
assertState(false, true);
|
||||||
_db.addKey(entry);
|
_db.addKey(entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeKey(DatabaseEntry entry) throws Exception {
|
public void removeKey(DatabaseEntry entry) throws Exception {
|
||||||
assertUnlocked();
|
assertState(false, true);
|
||||||
_db.removeKey(entry);
|
_db.removeKey(entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void swapKeys(DatabaseEntry entry1, DatabaseEntry entry2) throws Exception {
|
public void swapKeys(DatabaseEntry entry1, DatabaseEntry entry2) throws Exception {
|
||||||
assertUnlocked();
|
assertState(false, true);
|
||||||
_db.swapKeys(entry1, entry2);
|
_db.swapKeys(entry1, entry2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<DatabaseEntry> getKeys() throws Exception {
|
public List<DatabaseEntry> getKeys() throws Exception {
|
||||||
assertUnlocked();
|
assertState(false, true);
|
||||||
return _db.getKeys();
|
return _db.getKeys();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -163,20 +165,21 @@ public class DatabaseManager {
|
||||||
return _file != null;
|
return _file != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isUnlocked() {
|
public boolean isLocked() {
|
||||||
return _db != null;
|
return _db == null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertLoaded() throws Exception {
|
private void assertState(boolean locked, boolean loaded) throws Exception {
|
||||||
if (!isLoaded()) {
|
if (isLoaded() && !loaded) {
|
||||||
throw new Exception("database file has not been loaded yet");
|
throw new Exception("database file has not been loaded yet");
|
||||||
|
} else if (!isLoaded() && loaded) {
|
||||||
|
throw new Exception("database file has is already been loaded");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void assertUnlocked() throws Exception {
|
if (isLocked() && !locked) {
|
||||||
assertLoaded();
|
|
||||||
if (!isUnlocked()) {
|
|
||||||
throw new Exception("database file has not been unlocked yet");
|
throw new Exception("database file has not been unlocked yet");
|
||||||
|
} else if (!isLocked() && locked) {
|
||||||
|
throw new Exception("database file has is already been unlocked");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue