2019-02-07 22:39:33 +01:00
|
|
|
package com.beemdevelopment.aegis.ui;
|
2016-08-15 21:29:41 +02:00
|
|
|
|
2017-12-13 19:00:58 +01:00
|
|
|
import android.Manifest;
|
2016-08-21 22:32:07 +02:00
|
|
|
import android.content.ClipData;
|
|
|
|
import android.content.ClipboardManager;
|
2016-08-21 22:24:04 +02:00
|
|
|
import android.content.Context;
|
2016-08-15 22:31:28 +02:00
|
|
|
import android.content.Intent;
|
2018-01-01 21:11:40 +01:00
|
|
|
import android.graphics.Rect;
|
2018-09-25 16:26:57 +02:00
|
|
|
import com.google.android.material.bottomsheet.BottomSheetDialog;
|
2016-08-15 21:29:41 +02:00
|
|
|
import android.os.Bundle;
|
2016-08-16 14:14:17 +02:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2018-01-01 21:11:40 +01:00
|
|
|
import android.view.MotionEvent;
|
2018-12-11 11:44:36 +01:00
|
|
|
import android.view.SubMenu;
|
2016-11-01 22:16:54 +01:00
|
|
|
import android.widget.LinearLayout;
|
2016-08-17 01:14:25 +02:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2017-12-30 00:26:16 +01:00
|
|
|
import com.getbase.floatingactionbutton.FloatingActionsMenu;
|
2019-02-07 22:39:33 +01:00
|
|
|
import com.beemdevelopment.aegis.db.DatabaseFileCredentials;
|
|
|
|
import com.beemdevelopment.aegis.helpers.PermissionHelper;
|
|
|
|
import com.beemdevelopment.aegis.ui.views.EntryListView;
|
2017-12-30 00:26:16 +01:00
|
|
|
|
2017-05-03 21:08:38 +02:00
|
|
|
import java.lang.reflect.UndeclaredThrowableException;
|
2018-12-11 11:44:36 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.TreeSet;
|
2016-08-16 20:04:38 +02:00
|
|
|
|
2019-02-07 22:39:33 +01:00
|
|
|
import com.beemdevelopment.aegis.AegisApplication;
|
|
|
|
import com.beemdevelopment.aegis.R;
|
|
|
|
|
|
|
|
import com.beemdevelopment.aegis.db.DatabaseManagerException;
|
|
|
|
import com.beemdevelopment.aegis.db.DatabaseEntry;
|
|
|
|
import com.beemdevelopment.aegis.db.DatabaseManager;
|
2016-08-15 21:29:41 +02:00
|
|
|
|
2018-06-06 16:15:31 +02:00
|
|
|
public class MainActivity extends AegisActivity implements EntryListView.Listener {
|
2017-12-13 19:00:58 +01:00
|
|
|
// activity request codes
|
2018-06-06 16:15:31 +02:00
|
|
|
private static final int CODE_SCAN = 0;
|
|
|
|
private static final int CODE_ADD_ENTRY = 1;
|
|
|
|
private static final int CODE_EDIT_ENTRY = 2;
|
|
|
|
private static final int CODE_ENTER_ENTRY = 3;
|
2017-12-30 00:26:16 +01:00
|
|
|
private static final int CODE_DO_INTRO = 4;
|
|
|
|
private static final int CODE_DECRYPT = 5;
|
2018-05-10 14:50:47 +02:00
|
|
|
private static final int CODE_PREFERENCES = 6;
|
2016-08-24 23:48:25 +02:00
|
|
|
|
2017-12-13 19:00:58 +01:00
|
|
|
// permission request codes
|
2018-05-10 19:34:42 +02:00
|
|
|
private static final int CODE_PERM_CAMERA = 0;
|
2017-12-13 19:00:58 +01:00
|
|
|
|
2017-12-23 22:33:32 +01:00
|
|
|
private AegisApplication _app;
|
2017-08-26 21:15:53 +02:00
|
|
|
private DatabaseManager _db;
|
2018-06-09 20:23:39 +02:00
|
|
|
private boolean _loaded;
|
2018-12-11 11:44:36 +01:00
|
|
|
private String _checkedGroup;
|
2016-09-30 01:08:03 +02:00
|
|
|
|
2017-08-26 21:15:53 +02:00
|
|
|
private Menu _menu;
|
2018-01-01 21:11:40 +01:00
|
|
|
private FloatingActionsMenu _fabMenu;
|
2018-06-09 20:23:39 +02:00
|
|
|
private EntryListView _entryListView;
|
2016-08-16 14:14:17 +02:00
|
|
|
|
2016-08-15 21:29:41 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2017-12-23 22:33:32 +01:00
|
|
|
_app = (AegisApplication) getApplication();
|
|
|
|
_db = _app.getDatabaseManager();
|
2018-06-09 20:23:39 +02:00
|
|
|
_loaded = false;
|
2016-09-29 12:31:55 +02:00
|
|
|
|
2017-12-23 22:33:32 +01:00
|
|
|
// set up the main view
|
2016-08-15 21:29:41 +02:00
|
|
|
setContentView(R.layout.activity_main);
|
2017-12-03 16:47:27 +01:00
|
|
|
|
2018-06-06 16:15:31 +02:00
|
|
|
// set up the entry view
|
|
|
|
_entryListView = (EntryListView) getSupportFragmentManager().findFragmentById(R.id.key_profiles);
|
|
|
|
_entryListView.setListener(this);
|
2018-09-19 00:10:03 +02:00
|
|
|
_entryListView.setShowAccountName(getPreferences().isAccountNameVisible());
|
2019-03-25 21:32:29 +01:00
|
|
|
_entryListView.setTapToReveal(getPreferences().isTapToRevealEnabled());
|
2019-03-26 00:06:39 +01:00
|
|
|
_entryListView.setTapToRevealTime(getPreferences().getTapToRevealTime());
|
2017-12-25 15:36:29 +01:00
|
|
|
|
2017-12-23 22:33:32 +01:00
|
|
|
// set up the floating action button
|
2018-01-01 21:11:40 +01:00
|
|
|
_fabMenu = findViewById(R.id.fab);
|
2017-12-30 00:26:16 +01:00
|
|
|
findViewById(R.id.fab_enter).setOnClickListener(view -> {
|
2018-01-01 21:11:40 +01:00
|
|
|
_fabMenu.collapse();
|
2018-06-09 20:23:39 +02:00
|
|
|
startEditProfileActivity(CODE_ENTER_ENTRY, null, true);
|
2017-12-30 00:26:16 +01:00
|
|
|
});
|
|
|
|
findViewById(R.id.fab_scan).setOnClickListener(view -> {
|
2018-01-01 21:11:40 +01:00
|
|
|
_fabMenu.collapse();
|
2018-06-09 20:23:39 +02:00
|
|
|
startScanActivity();
|
2017-12-30 00:26:16 +01:00
|
|
|
});
|
2016-08-15 21:29:41 +02:00
|
|
|
}
|
2016-08-16 00:08:01 +02:00
|
|
|
|
2018-01-01 21:11:40 +01:00
|
|
|
@Override
|
|
|
|
public boolean dispatchTouchEvent(MotionEvent event) {
|
|
|
|
// collapse the fab menu on touch
|
|
|
|
if (event.getAction() == MotionEvent.ACTION_DOWN) {
|
|
|
|
if (_fabMenu.isExpanded()) {
|
|
|
|
Rect rect = new Rect();
|
|
|
|
_fabMenu.getGlobalVisibleRect(rect);
|
|
|
|
|
|
|
|
if (!rect.contains((int) event.getRawX(), (int) event.getRawY())) {
|
|
|
|
_fabMenu.collapse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.dispatchTouchEvent(event);
|
|
|
|
}
|
|
|
|
|
2017-12-25 20:01:58 +01:00
|
|
|
@Override
|
|
|
|
protected void onNewIntent(Intent intent) {
|
|
|
|
super.onNewIntent(intent);
|
|
|
|
setIntent(intent);
|
|
|
|
|
2017-12-26 19:29:47 +01:00
|
|
|
if (!doShortcutActions() || _db.isLocked()) {
|
2018-06-09 21:40:18 +02:00
|
|
|
unlockDatabase(null);
|
2017-12-25 20:01:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-24 21:42:08 +01:00
|
|
|
@Override
|
2016-08-16 00:08:01 +02:00
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
2017-12-25 20:01:58 +01:00
|
|
|
if (data == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-06 16:03:36 +02:00
|
|
|
switch (requestCode) {
|
2018-06-06 16:15:31 +02:00
|
|
|
case CODE_SCAN:
|
|
|
|
onScanResult(resultCode, data);
|
2017-08-06 16:03:36 +02:00
|
|
|
break;
|
2018-06-06 16:15:31 +02:00
|
|
|
case CODE_ADD_ENTRY:
|
|
|
|
onAddEntryResult(resultCode, data);
|
2017-08-06 16:03:36 +02:00
|
|
|
break;
|
2018-06-06 16:15:31 +02:00
|
|
|
case CODE_EDIT_ENTRY:
|
|
|
|
onEditEntryResult(resultCode, data);
|
2017-12-27 22:04:22 +01:00
|
|
|
break;
|
2018-06-06 16:15:31 +02:00
|
|
|
case CODE_ENTER_ENTRY:
|
|
|
|
onEnterEntryResult(resultCode, data);
|
2017-12-30 00:26:16 +01:00
|
|
|
break;
|
2017-08-06 16:03:36 +02:00
|
|
|
case CODE_DO_INTRO:
|
|
|
|
onDoIntroResult(resultCode, data);
|
|
|
|
break;
|
2017-08-06 18:15:47 +02:00
|
|
|
case CODE_DECRYPT:
|
|
|
|
onDecryptResult(resultCode, data);
|
|
|
|
break;
|
2017-12-10 19:19:48 +01:00
|
|
|
case CODE_PREFERENCES:
|
|
|
|
onPreferencesResult(resultCode, data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 19:00:58 +01: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(this, getString(R.string.permission_denied), Toast.LENGTH_SHORT).show();
|
2017-12-13 19:00:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (requestCode) {
|
|
|
|
case CODE_PERM_CAMERA:
|
2018-06-09 19:23:30 +02:00
|
|
|
startScanActivity();
|
2017-12-13 19:00:58 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-10 19:19:48 +01:00
|
|
|
private void onPreferencesResult(int resultCode, Intent data) {
|
2018-06-06 16:15:31 +02:00
|
|
|
// refresh the entire entry list if needed
|
2018-05-11 19:33:20 +02:00
|
|
|
if (data.getBooleanExtra("needsRecreate", false)) {
|
|
|
|
recreate();
|
|
|
|
} else if (data.getBooleanExtra("needsRefresh", false)) {
|
2018-09-19 00:10:03 +02:00
|
|
|
boolean showAccountName = getPreferences().isAccountNameVisible();
|
2019-03-25 21:40:08 +01:00
|
|
|
boolean tapToReveal = getPreferences().isTapToRevealEnabled();
|
2019-03-26 00:06:39 +01:00
|
|
|
int tapToRevealTime = getPreferences().getTapToRevealTime();
|
2018-09-19 00:10:03 +02:00
|
|
|
_entryListView.setShowAccountName(showAccountName);
|
2019-03-25 21:40:08 +01:00
|
|
|
_entryListView.setTapToReveal(tapToReveal);
|
2019-03-26 00:06:39 +01:00
|
|
|
_entryListView.setTapToRevealTime(tapToRevealTime);
|
2018-06-09 21:40:18 +02:00
|
|
|
_entryListView.refresh(true);
|
2017-12-10 19:19:48 +01:00
|
|
|
}
|
2017-08-06 16:03:36 +02:00
|
|
|
}
|
2016-08-16 00:08:01 +02:00
|
|
|
|
2018-06-06 16:15:31 +02:00
|
|
|
private void startEditProfileActivity(int requestCode, DatabaseEntry entry, boolean isNew) {
|
|
|
|
Intent intent = new Intent(this, EditEntryActivity.class);
|
|
|
|
if (entry != null) {
|
|
|
|
intent.putExtra("entry", entry);
|
2018-01-02 21:50:07 +01:00
|
|
|
}
|
|
|
|
intent.putExtra("isNew", isNew);
|
2018-12-11 11:44:36 +01:00
|
|
|
intent.putExtra("groups", new ArrayList<>(_db.getGroups()));
|
2018-01-02 21:50:07 +01:00
|
|
|
startActivityForResult(intent, requestCode);
|
|
|
|
}
|
|
|
|
|
2018-06-06 16:15:31 +02:00
|
|
|
private void onScanResult(int resultCode, Intent data) {
|
2017-12-03 16:47:27 +01:00
|
|
|
if (resultCode == RESULT_OK) {
|
2018-06-06 16:15:31 +02:00
|
|
|
DatabaseEntry entry = (DatabaseEntry) data.getSerializableExtra("entry");
|
|
|
|
startEditProfileActivity(CODE_ADD_ENTRY, entry, true);
|
2016-08-16 00:08:01 +02:00
|
|
|
}
|
2017-08-06 16:03:36 +02:00
|
|
|
}
|
|
|
|
|
2018-06-06 16:15:31 +02:00
|
|
|
private void onAddEntryResult(int resultCode, Intent data) {
|
2017-12-03 16:47:27 +01:00
|
|
|
if (resultCode == RESULT_OK) {
|
2018-06-06 16:15:31 +02:00
|
|
|
DatabaseEntry entry = (DatabaseEntry) data.getSerializableExtra("entry");
|
|
|
|
addEntry(entry);
|
2017-12-03 16:47:27 +01:00
|
|
|
saveDatabase();
|
2017-08-06 16:03:36 +02:00
|
|
|
}
|
2017-08-26 15:47:57 +02:00
|
|
|
}
|
|
|
|
|
2018-06-06 16:15:31 +02:00
|
|
|
private void onEditEntryResult(int resultCode, Intent data) {
|
2017-12-27 22:04:22 +01:00
|
|
|
if (resultCode == RESULT_OK) {
|
2018-06-06 16:15:31 +02:00
|
|
|
DatabaseEntry entry = (DatabaseEntry) data.getSerializableExtra("entry");
|
2018-12-11 11:44:36 +01:00
|
|
|
if (data.getBooleanExtra("delete", false)) {
|
|
|
|
deleteEntry(entry);
|
|
|
|
} else {
|
2018-01-02 14:36:56 +01:00
|
|
|
// this profile has been serialized/deserialized and is no longer the same instance it once was
|
2018-06-06 16:15:31 +02:00
|
|
|
// to deal with this, the replaceEntry functions are used
|
|
|
|
_db.replaceEntry(entry);
|
|
|
|
_entryListView.replaceEntry(entry);
|
2018-01-01 22:53:10 +01:00
|
|
|
saveDatabase();
|
|
|
|
}
|
2017-12-27 22:04:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-06 16:15:31 +02:00
|
|
|
private void onEnterEntryResult(int resultCode, Intent data) {
|
2017-12-30 00:26:16 +01:00
|
|
|
if (resultCode == RESULT_OK) {
|
2018-06-06 16:15:31 +02:00
|
|
|
DatabaseEntry entry = (DatabaseEntry) data.getSerializableExtra("entry");
|
|
|
|
addEntry(entry);
|
2017-12-30 00:26:16 +01:00
|
|
|
saveDatabase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 11:44:36 +01:00
|
|
|
private void updateGroupFilterMenu() {
|
|
|
|
SubMenu menu = _menu.findItem(R.id.action_filter).getSubMenu();
|
|
|
|
for (int i = menu.size() - 1; i >= 0; i--) {
|
|
|
|
MenuItem item = menu.getItem(i);
|
|
|
|
if (item.getItemId() == R.id.menu_filter_all) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
menu.removeItem(item.getItemId());
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the group no longer exists, switch back to 'All'
|
|
|
|
TreeSet<String> groups = _db.getGroups();
|
|
|
|
if (_checkedGroup != null && !groups.contains(_checkedGroup)) {
|
|
|
|
menu.findItem(R.id.menu_filter_all).setChecked(true);
|
|
|
|
setGroupFilter(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (String group : groups) {
|
|
|
|
MenuItem item = menu.add(R.id.action_filter_group, Menu.NONE, Menu.NONE, group);
|
|
|
|
if (group.equals(_checkedGroup)) {
|
|
|
|
item.setChecked(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
menu.setGroupCheckable(R.id.action_filter_group, true, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setGroupFilter(String group) {
|
2018-12-17 23:09:07 +01:00
|
|
|
getSupportActionBar().setSubtitle(group);
|
2018-12-11 11:44:36 +01:00
|
|
|
_checkedGroup = group;
|
|
|
|
_entryListView.setGroupFilter(group);
|
|
|
|
}
|
|
|
|
|
2018-06-06 16:15:31 +02:00
|
|
|
private void addEntry(DatabaseEntry entry) {
|
|
|
|
_db.addEntry(entry);
|
|
|
|
_entryListView.addEntry(entry);
|
2017-08-06 16:03:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void onDoIntroResult(int resultCode, Intent data) {
|
|
|
|
if (resultCode == IntroActivity.RESULT_EXCEPTION) {
|
|
|
|
// TODO: user feedback
|
|
|
|
Exception e = (Exception) data.getSerializableExtra("exception");
|
|
|
|
throw new UndeclaredThrowableException(e);
|
|
|
|
}
|
2016-11-13 18:21:00 +01:00
|
|
|
|
2018-10-06 22:23:38 +02:00
|
|
|
DatabaseFileCredentials creds = (DatabaseFileCredentials) data.getSerializableExtra("creds");
|
|
|
|
unlockDatabase(creds);
|
2017-08-06 18:15:47 +02:00
|
|
|
}
|
|
|
|
|
2017-12-25 19:02:46 +01:00
|
|
|
private void onDecryptResult(int resultCode, Intent intent) {
|
2018-10-06 22:23:38 +02:00
|
|
|
DatabaseFileCredentials creds = (DatabaseFileCredentials) intent.getSerializableExtra("creds");
|
|
|
|
unlockDatabase(creds);
|
2017-08-06 18:15:47 +02:00
|
|
|
|
2017-12-03 16:47:27 +01:00
|
|
|
doShortcutActions();
|
|
|
|
}
|
|
|
|
|
2017-12-25 19:02:46 +01:00
|
|
|
private void startScanActivity() {
|
2018-06-09 19:23:30 +02:00
|
|
|
if (!PermissionHelper.request(this, CODE_PERM_CAMERA, Manifest.permission.CAMERA)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-25 19:02:46 +01:00
|
|
|
Intent scannerActivity = new Intent(getApplicationContext(), ScannerActivity.class);
|
2018-06-06 16:15:31 +02:00
|
|
|
startActivityForResult(scannerActivity, CODE_SCAN);
|
2017-12-25 19:02:46 +01:00
|
|
|
}
|
|
|
|
|
2017-12-25 20:01:58 +01:00
|
|
|
private boolean doShortcutActions() {
|
2017-12-26 14:18:00 +01:00
|
|
|
// return false if an action was blocked by a locked database
|
|
|
|
// otherwise, always return true
|
2017-12-03 16:47:27 +01:00
|
|
|
Intent intent = getIntent();
|
2017-12-25 19:02:46 +01:00
|
|
|
String action = intent.getStringExtra("action");
|
2017-12-26 14:18:00 +01:00
|
|
|
if (action == null) {
|
|
|
|
return true;
|
|
|
|
} else if (_db.isLocked()) {
|
2017-12-25 20:01:58 +01:00
|
|
|
return false;
|
2017-12-03 16:47:27 +01:00
|
|
|
}
|
|
|
|
|
2017-12-25 19:02:46 +01:00
|
|
|
switch (action) {
|
|
|
|
case "scan":
|
|
|
|
startScanActivity();
|
2017-12-03 16:47:27 +01:00
|
|
|
break;
|
|
|
|
}
|
2017-12-25 20:01:58 +01:00
|
|
|
|
|
|
|
intent.removeExtra("action");
|
|
|
|
return true;
|
2016-08-16 00:08:01 +02:00
|
|
|
}
|
2016-08-16 14:14:17 +02:00
|
|
|
|
2017-12-13 16:39:03 +01:00
|
|
|
public void startActivityForResult(Intent intent, int requestCode) {
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
|
|
|
|
super.startActivityForResult(intent, requestCode);
|
|
|
|
}
|
|
|
|
|
2016-09-30 01:08:03 +02:00
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
2017-12-24 21:42:08 +01:00
|
|
|
|
2018-06-09 20:23:39 +02:00
|
|
|
if (_db.isLocked()) {
|
|
|
|
// start the intro if the database file doesn't exist
|
|
|
|
if (!_db.isLoaded() && !_db.fileExists()) {
|
|
|
|
// the db doesn't exist, start the intro
|
|
|
|
if (getPreferences().isIntroDone()) {
|
2018-10-09 23:13:51 +02:00
|
|
|
Toast.makeText(this, getString(R.string.vault_not_found), Toast.LENGTH_SHORT).show();
|
2018-06-09 20:23:39 +02:00
|
|
|
}
|
|
|
|
Intent intro = new Intent(this, IntroActivity.class);
|
|
|
|
startActivityForResult(intro, CODE_DO_INTRO);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
unlockDatabase(null);
|
|
|
|
}
|
|
|
|
} else if (_loaded) {
|
2018-12-11 11:44:36 +01:00
|
|
|
// update the list of groups in the filter menu
|
|
|
|
if (_menu != null) {
|
|
|
|
updateGroupFilterMenu();
|
|
|
|
}
|
|
|
|
|
2018-06-09 20:23:39 +02:00
|
|
|
// refresh all codes to prevent showing old ones
|
|
|
|
_entryListView.refresh(true);
|
|
|
|
} else {
|
|
|
|
loadEntries();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateLockIcon();
|
2016-09-30 01:08:03 +02:00
|
|
|
}
|
|
|
|
|
2018-06-06 16:15:31 +02:00
|
|
|
private BottomSheetDialog createBottomSheet(final DatabaseEntry entry) {
|
2018-05-11 15:12:36 +02:00
|
|
|
BottomSheetDialog dialog = new BottomSheetDialog(this);
|
2018-06-06 16:15:31 +02:00
|
|
|
dialog.setContentView(R.layout.bottom_sheet_edit_entry);
|
2018-05-11 15:12:36 +02:00
|
|
|
dialog.setCancelable(true);
|
|
|
|
dialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT,
|
2016-11-01 22:16:54 +01:00
|
|
|
LinearLayout.LayoutParams.WRAP_CONTENT);
|
2018-05-11 15:12:36 +02:00
|
|
|
dialog.show();
|
2016-11-01 22:16:54 +01:00
|
|
|
|
2018-05-11 15:12:36 +02:00
|
|
|
dialog.findViewById(R.id.copy_button).setOnClickListener(view -> {
|
|
|
|
dialog.dismiss();
|
2016-11-01 22:16:54 +01:00
|
|
|
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
|
2018-06-06 16:15:31 +02:00
|
|
|
ClipData clip = ClipData.newPlainText("text/plain", entry.getInfo().getOtp());
|
2016-11-01 22:16:54 +01:00
|
|
|
clipboard.setPrimaryClip(clip);
|
2018-10-09 23:13:51 +02:00
|
|
|
Toast.makeText(this, getString(R.string.code_copied), Toast.LENGTH_SHORT).show();
|
2016-11-01 22:16:54 +01:00
|
|
|
});
|
2016-11-01 22:57:21 +01:00
|
|
|
|
2018-05-11 15:12:36 +02:00
|
|
|
dialog.findViewById(R.id.delete_button).setOnClickListener(view -> {
|
|
|
|
dialog.dismiss();
|
|
|
|
Dialogs.showDeleteEntryDialog(this, (d, which) -> {
|
2018-06-06 16:15:31 +02:00
|
|
|
deleteEntry(entry);
|
2018-12-11 11:44:36 +01:00
|
|
|
// update the filter list if the group no longer exists
|
|
|
|
if (!_db.getGroups().contains(entry.getGroup())) {
|
|
|
|
updateGroupFilterMenu();
|
|
|
|
}
|
2018-05-10 23:11:21 +02:00
|
|
|
});
|
2016-11-01 22:57:21 +01:00
|
|
|
});
|
|
|
|
|
2018-05-11 15:12:36 +02:00
|
|
|
dialog.findViewById(R.id.edit_button).setOnClickListener(view -> {
|
|
|
|
dialog.dismiss();
|
2018-06-06 16:15:31 +02:00
|
|
|
startEditProfileActivity(CODE_EDIT_ENTRY, entry, false);
|
2016-11-01 22:57:21 +01:00
|
|
|
});
|
|
|
|
|
2018-05-11 15:12:36 +02:00
|
|
|
return dialog;
|
2016-11-01 22:57:21 +01:00
|
|
|
}
|
2016-11-01 22:16:54 +01:00
|
|
|
|
2018-06-06 16:15:31 +02:00
|
|
|
private void deleteEntry(DatabaseEntry entry) {
|
|
|
|
_db.removeEntry(entry);
|
2018-05-10 23:11:21 +02:00
|
|
|
saveDatabase();
|
2017-12-12 21:08:30 +01:00
|
|
|
|
2018-06-06 16:15:31 +02:00
|
|
|
_entryListView.removeEntry(entry);
|
2016-11-01 22:16:54 +01:00
|
|
|
}
|
|
|
|
|
2016-08-16 14:14:17 +02:00
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
2017-08-26 21:15:53 +02:00
|
|
|
_menu = menu;
|
2016-08-16 14:14:17 +02:00
|
|
|
getMenuInflater().inflate(R.menu.menu_main, menu);
|
2017-08-26 21:15:53 +02:00
|
|
|
updateLockIcon();
|
2018-12-12 16:20:35 +01:00
|
|
|
if (_loaded) {
|
|
|
|
updateGroupFilterMenu();
|
|
|
|
}
|
2016-08-16 14:14:17 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
2017-08-14 00:04:06 +02:00
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.action_settings:
|
2018-02-09 17:31:07 +01:00
|
|
|
Intent intent = new Intent(this, PreferencesActivity.class);
|
|
|
|
startActivityForResult(intent, CODE_PREFERENCES);
|
2017-08-14 00:04:06 +02:00
|
|
|
return true;
|
|
|
|
case R.id.action_lock:
|
2018-06-09 20:23:39 +02:00
|
|
|
lockDatabase();
|
2017-08-14 00:04:06 +02:00
|
|
|
return true;
|
|
|
|
default:
|
2018-12-11 11:44:36 +01:00
|
|
|
if (item.getGroupId() == R.id.action_filter_group) {
|
|
|
|
item.setChecked(true);
|
|
|
|
|
|
|
|
String group = null;
|
|
|
|
if (item.getItemId() != R.id.menu_filter_all) {
|
|
|
|
group = item.getTitle().toString();
|
|
|
|
}
|
|
|
|
setGroupFilter(group);
|
|
|
|
}
|
2017-08-14 00:04:06 +02:00
|
|
|
return super.onOptionsItemSelected(item);
|
2016-08-16 14:14:17 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-30 01:08:03 +02:00
|
|
|
|
2018-06-09 20:23:39 +02:00
|
|
|
private void lockDatabase() {
|
|
|
|
if (_loaded) {
|
|
|
|
_entryListView.clearEntries();
|
|
|
|
_db.lock();
|
|
|
|
_loaded = false;
|
|
|
|
startAuthActivity();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-06 22:23:38 +02:00
|
|
|
private void unlockDatabase(DatabaseFileCredentials creds) {
|
2018-06-09 21:40:18 +02:00
|
|
|
if (_loaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-09 20:23:39 +02:00
|
|
|
try {
|
|
|
|
if (!_db.isLoaded()) {
|
|
|
|
_db.load();
|
|
|
|
}
|
|
|
|
if (_db.isLocked()) {
|
2018-10-06 22:23:38 +02:00
|
|
|
if (creds == null) {
|
2018-06-09 20:23:39 +02:00
|
|
|
startAuthActivity();
|
|
|
|
return;
|
|
|
|
} else {
|
2018-10-06 22:23:38 +02:00
|
|
|
_db.unlock(creds);
|
2018-06-09 20:23:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (DatabaseManagerException e) {
|
2018-10-09 23:13:51 +02:00
|
|
|
Toast.makeText(this, getString(R.string.decryption_error), Toast.LENGTH_LONG).show();
|
2018-06-09 20:23:39 +02:00
|
|
|
startAuthActivity();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
loadEntries();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void loadEntries() {
|
|
|
|
// load all entries
|
|
|
|
_entryListView.addEntries(_db.getEntries());
|
|
|
|
_loaded = true;
|
|
|
|
}
|
|
|
|
|
2017-12-24 21:42:08 +01:00
|
|
|
private void startAuthActivity() {
|
|
|
|
Intent intent = new Intent(this, AuthActivity.class);
|
2018-10-06 22:23:38 +02:00
|
|
|
intent.putExtra("slots", _db.getFileHeader().getSlots());
|
2017-12-24 21:42:08 +01:00
|
|
|
startActivityForResult(intent, CODE_DECRYPT);
|
|
|
|
}
|
|
|
|
|
2017-08-06 18:15:47 +02:00
|
|
|
private void saveDatabase() {
|
2017-05-03 21:08:38 +02:00
|
|
|
try {
|
2017-08-26 21:15:53 +02:00
|
|
|
_db.save();
|
2018-03-19 18:00:53 +01:00
|
|
|
} catch (DatabaseManagerException e) {
|
2018-10-09 23:13:51 +02:00
|
|
|
Toast.makeText(this, getString(R.string.saving_error), Toast.LENGTH_LONG).show();
|
2016-11-13 18:21:00 +01:00
|
|
|
}
|
2017-08-06 18:15:47 +02:00
|
|
|
}
|
2016-11-13 18:21:00 +01:00
|
|
|
|
2017-08-26 21:15:53 +02:00
|
|
|
private void updateLockIcon() {
|
2017-12-24 21:42:08 +01:00
|
|
|
// hide the lock icon if the database is not unlocked
|
2017-12-24 22:29:32 +01:00
|
|
|
if (_menu != null && !_db.isLocked()) {
|
2017-08-26 21:15:53 +02:00
|
|
|
MenuItem item = _menu.findItem(R.id.action_lock);
|
2018-10-06 22:23:38 +02:00
|
|
|
item.setVisible(_db.isEncryptionEnabled());
|
2017-08-26 21:15:53 +02:00
|
|
|
}
|
|
|
|
}
|
2017-12-12 01:50:00 +01:00
|
|
|
|
|
|
|
@Override
|
2018-06-06 16:15:31 +02:00
|
|
|
public void onEntryClick(DatabaseEntry entry) {
|
|
|
|
createBottomSheet(entry).show();
|
2017-12-12 01:50:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-12-25 15:36:29 +01:00
|
|
|
public void onEntryMove(DatabaseEntry entry1, DatabaseEntry entry2) {
|
2018-06-06 16:15:31 +02:00
|
|
|
_db.swapEntries(entry1, entry2);
|
2017-12-12 01:50:00 +01:00
|
|
|
}
|
2017-12-12 03:14:26 +01:00
|
|
|
|
|
|
|
@Override
|
2017-12-25 15:36:29 +01:00
|
|
|
public void onEntryDrop(DatabaseEntry entry) {
|
2017-12-12 03:14:26 +01:00
|
|
|
saveDatabase();
|
|
|
|
}
|
2018-06-06 17:23:40 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onEntryChange(DatabaseEntry entry) {
|
|
|
|
saveDatabase();
|
|
|
|
}
|
2016-08-15 21:29:41 +02:00
|
|
|
}
|