From bdc260f1241d6f429a2a8121628ce219473d43af Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 15 Jan 2022 13:34:23 +0100 Subject: [PATCH] Improved auto/lock --- .../java/eu/faircode/email/ActivityBase.java | 39 +++++++++++++------ .../main/java/eu/faircode/email/Helper.java | 24 ++++++++++++ 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ActivityBase.java b/app/src/main/java/eu/faircode/email/ActivityBase.java index 4acceba09e..8a43f918f2 100644 --- a/app/src/main/java/eu/faircode/email/ActivityBase.java +++ b/app/src/main/java/eu/faircode/email/ActivityBase.java @@ -284,13 +284,9 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); if (pm != null && !pm.isInteractive()) { Log.i("Stop with screen off"); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); - boolean biometrics = prefs.getBoolean("biometrics", false); - String pin = prefs.getString("pin", null); - boolean autolock = prefs.getBoolean("autolock", true); - if (autolock && (biometrics || !TextUtils.isEmpty(pin))) { + if (Helper.shouldAutoLock(this)) { Helper.clearAuthentication(this); - finish(); + lock(); } } } @@ -371,11 +367,11 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc } private void checkAuthentication(boolean auth) { - if (!this.getClass().equals(ActivityMain.class) && - Helper.shouldAuthenticate(this, !auth)) { - finishAndRemoveTask(); - setResult(RESULT_CANCELED); - finishAffinity(); + if (this.getClass().equals(ActivityMain.class)) + return; + + if (Helper.shouldAuthenticate(this, !auth)) { + lock(); if (auth) { if (this instanceof ActivityWidget || @@ -391,9 +387,30 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc startActivity(main); } } + } else { + if (!auth && Helper.shouldAutoLockNav(this)) + getMainHandler().postDelayed(autoLockNav, Helper.AUTOLOCK_GRACE * 1000L); } } + private void lock() { + finishAndRemoveTask(); + setResult(RESULT_CANCELED); + finishAffinity(); + } + + private final Runnable autoLockNav = new Runnable() { + @Override + public void run() { + try { + if (Helper.willAuthenticate(ActivityBase.this)) + lock(); + } catch (Throwable ex) { + Log.e(ex); + } + } + }; + private void processStreams(Intent intent) { intent.setClipData(null); diff --git a/app/src/main/java/eu/faircode/email/Helper.java b/app/src/main/java/eu/faircode/email/Helper.java index 1a8be8f433..970b84066a 100644 --- a/app/src/main/java/eu/faircode/email/Helper.java +++ b/app/src/main/java/eu/faircode/email/Helper.java @@ -2012,6 +2012,30 @@ public class Helper { return false; } + static boolean willAuthenticate(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + long now = new Date().getTime(); + long last_authentication = prefs.getLong("last_authentication", 0); + long biometrics_timeout = prefs.getInt("biometrics_timeout", 2) * 60 * 1000L; + return (last_authentication + biometrics_timeout < now); + } + + static boolean shouldAutoLock(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + boolean biometrics = prefs.getBoolean("biometrics", false); + String pin = prefs.getString("pin", null); + boolean autolock = prefs.getBoolean("autolock", true); + return (autolock && (biometrics || !TextUtils.isEmpty(pin))); + } + + static boolean shouldAutoLockNav(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + boolean biometrics = prefs.getBoolean("biometrics", false); + String pin = prefs.getString("pin", null); + boolean autolock_nav = prefs.getBoolean("autolock_nav", false); + return (autolock_nav && (biometrics || !TextUtils.isEmpty(pin))); + } + static void authenticate(final FragmentActivity activity, final LifecycleOwner owner, Boolean enabled, final Runnable authenticated, final Runnable cancelled) {