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;
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);
}
}
}
}