Add an option to automatically lock the app

This adds an option to automatically lock the app when:
* The back button is pressed
* The device is locked

It's the first step towards implementing #7
This commit is contained in:
Alexander Bakker 2019-04-07 18:18:50 +02:00
parent 6d93b78f9a
commit 18fd88a441
5 changed files with 45 additions and 2 deletions

View file

@ -1,7 +1,10 @@
package com.beemdevelopment.aegis; package com.beemdevelopment.aegis;
import android.app.Application; import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ShortcutInfo; import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager; import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon; import android.graphics.drawable.Icon;
@ -24,6 +27,10 @@ public class AegisApplication extends Application {
_manager = new DatabaseManager(this); _manager = new DatabaseManager(this);
_prefs = new Preferences(this); _prefs = new Preferences(this);
// listen for SCREEN_OFF events
ScreenOffReceiver receiver = new ScreenOffReceiver();
registerReceiver(receiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
initAppShortcuts(); initAppShortcuts();
} }
@ -57,4 +64,20 @@ public class AegisApplication extends Application {
shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut)); shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut));
} }
public boolean isAutoLockEnabled() {
return _prefs.isAutoLockEnabled() && _manager.isEncryptionEnabled() && !_manager.isLocked();
}
private class ScreenOffReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (isAutoLockEnabled()) {
_manager.lock();
Intent newIntent = new Intent(getApplicationContext(), MainActivity.class);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(newIntent);
}
}
}
} }

View file

@ -27,6 +27,10 @@ public class Preferences {
return _prefs.getBoolean("pref_intro", false); return _prefs.getBoolean("pref_intro", false);
} }
public boolean isAutoLockEnabled() {
return _prefs.getBoolean("pref_auto_lock", true);
}
public void setIntroDone(boolean done) { public void setIntroDone(boolean done) {
_prefs.edit().putBoolean("pref_intro", done).apply(); _prefs.edit().putBoolean("pref_intro", done).apply();
} }

View file

@ -363,6 +363,15 @@ public class MainActivity extends AegisActivity implements EntryListView.Listene
updateLockIcon(); updateLockIcon();
} }
@Override
public void onBackPressed() {
if (_app.isAutoLockEnabled()) {
lockDatabase();
}
super.onBackPressed();
}
private BottomSheetDialog createBottomSheet(final DatabaseEntry entry) { private BottomSheetDialog createBottomSheet(final DatabaseEntry entry) {
BottomSheetDialog dialog = new BottomSheetDialog(this); BottomSheetDialog dialog = new BottomSheetDialog(this);
dialog.setContentView(R.layout.bottom_sheet_edit_entry); dialog.setContentView(R.layout.bottom_sheet_edit_entry);
@ -428,6 +437,7 @@ public class MainActivity extends AegisActivity implements EntryListView.Listene
return true; return true;
case R.id.action_lock: case R.id.action_lock:
lockDatabase(); lockDatabase();
startAuthActivity();
return true; return true;
default: default:
if (item.getGroupId() == R.id.action_filter_group) { if (item.getGroupId() == R.id.action_filter_group) {
@ -475,7 +485,6 @@ public class MainActivity extends AegisActivity implements EntryListView.Listene
_entryListView.clearEntries(); _entryListView.clearEntries();
_db.lock(); _db.lock();
_loaded = false; _loaded = false;
startAuthActivity();
} }
} }

View file

@ -30,6 +30,8 @@
<string name="pref_tap_to_reveal_title">Tap to reveal</string> <string name="pref_tap_to_reveal_title">Tap to reveal</string>
<string name="pref_tap_to_reveal_summary">Tokens will be hidden by default. Tap on the tokens to reveal code.</string> <string name="pref_tap_to_reveal_summary">Tokens will be hidden by default. Tap on the tokens to reveal code.</string>
<string name="pref_tap_to_reveal_time_title">Timeout for tap to reveal</string> <string name="pref_tap_to_reveal_time_title">Timeout for tap to reveal</string>
<string name="pref_auto_lock_title">Auto lock</string>
<string name="pref_auto_lock_summary">Automatically lock when you close the app or lock your device.</string>
<string name="pref_encryption_title">Encryption</string> <string name="pref_encryption_title">Encryption</string>
<string name="pref_encryption_summary">Encrypt the database and unlock it with a password or fingerprint</string> <string name="pref_encryption_summary">Encrypt the database and unlock it with a password or fingerprint</string>
<string name="pref_fingerprint_title">Fingerprint</string> <string name="pref_fingerprint_title">Fingerprint</string>

View file

@ -42,13 +42,18 @@
android:title="@string/pref_secure_screen_title" android:title="@string/pref_secure_screen_title"
android:summary="@string/pref_secure_screen_summary" android:summary="@string/pref_secure_screen_summary"
app:iconSpaceReserved="false"/> app:iconSpaceReserved="false"/>
<androidx.preference.SwitchPreferenceCompat
android:defaultValue="true"
android:key="pref_auto_lock"
android:title="@string/pref_auto_lock_title"
android:summary="@string/pref_auto_lock_summary"
app:iconSpaceReserved="false"/>
<androidx.preference.SwitchPreferenceCompat <androidx.preference.SwitchPreferenceCompat
android:defaultValue="false" android:defaultValue="false"
android:key="pref_tap_to_reveal" android:key="pref_tap_to_reveal"
android:title="@string/pref_tap_to_reveal_title" android:title="@string/pref_tap_to_reveal_title"
android:summary="@string/pref_tap_to_reveal_summary" android:summary="@string/pref_tap_to_reveal_summary"
app:iconSpaceReserved="false"/> app:iconSpaceReserved="false"/>
<Preference <Preference
android:key="pref_tap_to_reveal_time" android:key="pref_tap_to_reveal_time"
android:title="@string/pref_tap_to_reveal_time_title" android:title="@string/pref_tap_to_reveal_time_title"