Authentication improvements/fixes

pull/157/head
M66B 6 years ago
parent 3bb6956bfe
commit e17f313956

@ -20,8 +20,10 @@ package eu.faircode.email;
*/ */
import android.Manifest; import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.os.Bundle; import android.os.Bundle;
@ -84,6 +86,8 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc
prefs.registerOnSharedPreferenceChangeListener(this); prefs.registerOnSharedPreferenceChangeListener(this);
registerReceiver(onScreenOff, new IntentFilter(Intent.ACTION_SCREEN_OFF));
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
} }
@ -139,6 +143,7 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc
@Override @Override
protected void onDestroy() { protected void onDestroy() {
Log.i("Destroy " + this.getClass().getName()); Log.i("Destroy " + this.getClass().getName());
unregisterReceiver(onScreenOff);
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this); PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy(); super.onDestroy();
} }
@ -163,6 +168,17 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc
finish(); finish();
} }
private BroadcastReceiver onScreenOff = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(intent.toString());
Log.logExtras(intent);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ActivityBase.this);
prefs.edit().remove("last_authentication").apply();
}
};
public boolean hasPermission(String name) { public boolean hasPermission(String name) {
return Helper.hasPermission(this, name); return Helper.hasPermission(this, name);
} }

@ -658,18 +658,23 @@ public class Helper {
static boolean shouldAuthenticate(Context context) { static boolean shouldAuthenticate(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean biometrics = prefs.getBoolean("biometrics", false); boolean biometrics = prefs.getBoolean("biometrics", false);
if (!biometrics)
return false;
ContentResolver resolver = context.getContentResolver(); if (biometrics) {
int screen_timeout = Settings.System.getInt(resolver, Settings.System.SCREEN_OFF_TIMEOUT, -1); ContentResolver resolver = context.getContentResolver();
Log.i("Screen timeout=" + screen_timeout); int screen_timeout = Settings.System.getInt(resolver, Settings.System.SCREEN_OFF_TIMEOUT, -1);
Log.i("Screen timeout=" + screen_timeout);
long now = new Date().getTime();
long last_authentication = prefs.getLong("last_authentication", 0);
Log.i("Authentication valid until=" + new Date(last_authentication + screen_timeout));
if (last_authentication + screen_timeout < now)
return true;
long now = new Date().getTime(); prefs.edit().putLong("last_authentication", now).apply();
long last_authentication = prefs.getLong("last_authentication", 0); }
prefs.edit().putLong("last_authentication", now).apply();
return (last_authentication + screen_timeout < now); return false;
} }
static void authenticate(final FragmentActivity activity, static void authenticate(final FragmentActivity activity,

Loading…
Cancel
Save