Merge pull request #53 from alexbakker/feature-autolock

Add an option to automatically lock the app
This commit is contained in:
Michael Schättgen 2019-04-09 17:34:47 +02:00 committed by GitHub
commit 1ac42d85d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 2 deletions

View file

@ -1,7 +1,10 @@
package com.beemdevelopment.aegis;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon;
@ -24,6 +27,10 @@ public class AegisApplication extends Application {
_manager = new DatabaseManager(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) {
initAppShortcuts();
}
@ -57,4 +64,20 @@ public class AegisApplication extends Application {
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);
}
public boolean isAutoLockEnabled() {
return _prefs.getBoolean("pref_auto_lock", true);
}
public void setIntroDone(boolean done) {
_prefs.edit().putBoolean("pref_intro", done).apply();
}

View file

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