Improved auto/lock

pull/194/merge
M66B 4 years ago
parent 3ffd9540ba
commit bdc260f124

@ -284,13 +284,9 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (pm != null && !pm.isInteractive()) { if (pm != null && !pm.isInteractive()) {
Log.i("Stop with screen off"); Log.i("Stop with screen off");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); if (Helper.shouldAutoLock(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))) {
Helper.clearAuthentication(this); Helper.clearAuthentication(this);
finish(); lock();
} }
} }
} }
@ -371,11 +367,11 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc
} }
private void checkAuthentication(boolean auth) { private void checkAuthentication(boolean auth) {
if (!this.getClass().equals(ActivityMain.class) && if (this.getClass().equals(ActivityMain.class))
Helper.shouldAuthenticate(this, !auth)) { return;
finishAndRemoveTask();
setResult(RESULT_CANCELED); if (Helper.shouldAuthenticate(this, !auth)) {
finishAffinity(); lock();
if (auth) { if (auth) {
if (this instanceof ActivityWidget || if (this instanceof ActivityWidget ||
@ -391,9 +387,30 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc
startActivity(main); 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) { private void processStreams(Intent intent) {
intent.setClipData(null); intent.setClipData(null);

@ -2012,6 +2012,30 @@ public class Helper {
return false; 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, static void authenticate(final FragmentActivity activity, final LifecycleOwner owner,
Boolean enabled, final Boolean enabled, final
Runnable authenticated, final Runnable cancelled) { Runnable authenticated, final Runnable cancelled) {

Loading…
Cancel
Save