Authentication improvements/fixes

This commit is contained in:
M66B 2019-07-10 19:58:02 +02:00
parent 3bb6956bfe
commit e17f313956
2 changed files with 30 additions and 9 deletions

View File

@ -20,8 +20,10 @@ package eu.faircode.email;
*/
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
@ -84,6 +86,8 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc
prefs.registerOnSharedPreferenceChangeListener(this);
registerReceiver(onScreenOff, new IntentFilter(Intent.ACTION_SCREEN_OFF));
super.onCreate(savedInstanceState);
}
@ -139,6 +143,7 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc
@Override
protected void onDestroy() {
Log.i("Destroy " + this.getClass().getName());
unregisterReceiver(onScreenOff);
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy();
}
@ -163,6 +168,17 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc
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) {
return Helper.hasPermission(this, name);
}

View File

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