Add more assertions to DatabaseManager

This commit is contained in:
Alexander Bakker 2017-12-24 22:29:32 +01:00
parent 2fce0f4dcc
commit ff214eaa4a
2 changed files with 25 additions and 22 deletions

View file

@ -91,14 +91,14 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis
touchHelper.attachToRecyclerView(rvKeyProfiles);
rvKeyProfiles.setAdapter(_keyProfileAdapter);
if (!_app.isRunning() && !_db.isUnlocked()) {
if (!_app.isRunning() && _db.isLocked()) {
if (!_app.getPreferences().getBoolean("pref_intro", false)) {
Intent intro = new Intent(this, IntroActivity.class);
startActivityForResult(intro, CODE_DO_INTRO);
} else {
try {
_db.load();
if (!_db.isUnlocked()) {
if (_db.isLocked()) {
startAuthActivity();
}
} catch (FileNotFoundException e) {
@ -114,7 +114,7 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis
}
}
if (_db.isUnlocked()) {
if (!_db.isLocked()) {
loadKeyProfiles();
}
}
@ -346,7 +346,7 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis
MasterKey key = (MasterKey) data.getSerializableExtra("key");
try {
_db.load();
if (!_db.isUnlocked()) {
if (_db.isLocked()) {
_db.unlock(key);
}
} catch (Exception e) {
@ -377,7 +377,7 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis
private void doShortcutActions() {
Intent intent = getIntent();
String mode = intent.getStringExtra("Action");
if (mode == null || !_db.isUnlocked()) {
if (mode == null || _db.isLocked()) {
return;
}
@ -556,7 +556,7 @@ public class MainActivity extends AegisActivity implements KeyProfileAdapter.Lis
private void updateLockIcon() {
// 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);
item.setVisible(_db.getFile().isEncrypted());
}

View file

@ -30,6 +30,8 @@ public class DatabaseManager {
}
public void load() throws Exception {
assertState(true, false);
byte[] fileBytes;
FileInputStream file = null;
@ -58,14 +60,14 @@ public class DatabaseManager {
}
public void lock() throws Exception {
assertUnlocked();
assertState(false, true);
// TODO: properly clear everything
_key = null;
_db = null;
}
public void unlock(MasterKey key) throws Exception {
assertLoaded();
assertState(true, true);
byte[] encrypted = _file.getContent();
CryptParameters params = _file.getCryptParameters();
CryptResult result = key.decrypt(encrypted, params);
@ -90,7 +92,7 @@ public class DatabaseManager {
}
public void save() throws Exception {
assertUnlocked();
assertState(false, true);
byte[] dbBytes = _db.serialize();
if (!_file.isEncrypted()) {
_file.setContent(dbBytes);
@ -103,7 +105,7 @@ public class DatabaseManager {
}
public String export(boolean encrypt) throws Exception {
assertUnlocked();
assertState(false, true);
byte[] bytes = _db.serialize();
encrypt = encrypt && getFile().isEncrypted();
if (encrypt) {
@ -136,22 +138,22 @@ public class DatabaseManager {
}
public void addKey(DatabaseEntry entry) throws Exception {
assertUnlocked();
assertState(false, true);
_db.addKey(entry);
}
public void removeKey(DatabaseEntry entry) throws Exception {
assertUnlocked();
assertState(false, true);
_db.removeKey(entry);
}
public void swapKeys(DatabaseEntry entry1, DatabaseEntry entry2) throws Exception {
assertUnlocked();
assertState(false, true);
_db.swapKeys(entry1, entry2);
}
public List<DatabaseEntry> getKeys() throws Exception {
assertUnlocked();
assertState(false, true);
return _db.getKeys();
}
@ -163,20 +165,21 @@ public class DatabaseManager {
return _file != null;
}
public boolean isUnlocked() {
return _db != null;
public boolean isLocked() {
return _db == null;
}
private void assertLoaded() throws Exception {
if (!isLoaded()) {
private void assertState(boolean locked, boolean loaded) throws Exception {
if (isLoaded() && !loaded) {
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 {
assertLoaded();
if (!isUnlocked()) {
if (isLocked() && !locked) {
throw new Exception("database file has not been unlocked yet");
} else if (!isLocked() && locked) {
throw new Exception("database file has is already been unlocked");
}
}
}