Improved auto/lock

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

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

@ -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) {

Loading…
Cancel
Save