diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsPrivacy.java b/app/src/main/java/eu/faircode/email/FragmentOptionsPrivacy.java
index fbc194336c..bc671b0a0a 100644
--- a/app/src/main/java/eu/faircode/email/FragmentOptionsPrivacy.java
+++ b/app/src/main/java/eu/faircode/email/FragmentOptionsPrivacy.java
@@ -19,8 +19,12 @@ package eu.faircode.email;
Copyright 2018-2019 by Marcel Bokhorst (M66B)
*/
+import android.app.Dialog;
+import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
+import android.os.Handler;
+import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
@@ -28,12 +32,15 @@ import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
+import android.widget.Button;
import android.widget.CompoundButton;
+import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
+import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.SwitchCompat;
import androidx.lifecycle.Lifecycle;
import androidx.preference.PreferenceManager;
@@ -43,6 +50,7 @@ public class FragmentOptionsPrivacy extends FragmentBase implements SharedPrefer
private SwitchCompat swDisplayHidden;
private SwitchCompat swNoHistory;
private Spinner spBiometricsTimeout;
+ private Button btnPin;
private final static String[] RESET_OPTIONS = new String[]{
"disable_tracking", "display_hidden", "no_history", "biometrics_timeout"
@@ -62,6 +70,7 @@ public class FragmentOptionsPrivacy extends FragmentBase implements SharedPrefer
swDisplayHidden = view.findViewById(R.id.swDisplayHidden);
swNoHistory = view.findViewById(R.id.swNoHistory);
spBiometricsTimeout = view.findViewById(R.id.spBiometricsTimeout);
+ btnPin = view.findViewById(R.id.btnPin);
setOptions();
@@ -104,6 +113,14 @@ public class FragmentOptionsPrivacy extends FragmentBase implements SharedPrefer
}
});
+ btnPin.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ FragmentDialogPin fragment = new FragmentDialogPin();
+ fragment.show(getParentFragmentManager(), "pin");
+ }
+ });
+
PreferenceManager.getDefaultSharedPreferences(getContext()).registerOnSharedPreferenceChangeListener(this);
return view;
@@ -162,4 +179,36 @@ public class FragmentOptionsPrivacy extends FragmentBase implements SharedPrefer
break;
}
}
+
+ public static class FragmentDialogPin extends FragmentDialogBase {
+ @NonNull
+ @Override
+ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
+ final View dview = LayoutInflater.from(getContext()).inflate(R.layout.dialog_pin_set, null);
+ final EditText etPin = dview.findViewById(R.id.etPin);
+
+ new Handler().post(new Runnable() {
+ @Override
+ public void run() {
+ etPin.requestFocus();
+ }
+ });
+
+ return new AlertDialog.Builder(getContext())
+ .setView(dview)
+ .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ String pin = etPin.getText().toString();
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
+ if (TextUtils.isEmpty(pin))
+ prefs.edit().remove("pin").apply();
+ else
+ prefs.edit().putString("pin", pin).apply();
+ }
+ })
+ .setNegativeButton(android.R.string.cancel, null)
+ .create();
+ }
+ }
}
diff --git a/app/src/main/java/eu/faircode/email/Helper.java b/app/src/main/java/eu/faircode/email/Helper.java
index 15159fef28..39879850e4 100644
--- a/app/src/main/java/eu/faircode/email/Helper.java
+++ b/app/src/main/java/eu/faircode/email/Helper.java
@@ -47,12 +47,16 @@ import android.os.PowerManager;
import android.os.StatFs;
import android.text.Spannable;
import android.text.Spanned;
+import android.text.TextUtils;
import android.text.format.DateUtils;
import android.text.format.Time;
import android.util.TypedValue;
+import android.view.KeyEvent;
+import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
+import android.view.inputmethod.EditorInfo;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.CheckBox;
@@ -793,6 +797,11 @@ public class Helper {
}
static boolean canAuthenticate(Context context) {
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
+ String pin = prefs.getString("pin", null);
+ if (!TextUtils.isEmpty(pin))
+ return true;
+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
return false;
else if (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.Q) {
@@ -810,8 +819,9 @@ public class Helper {
static boolean shouldAuthenticate(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean biometrics = prefs.getBoolean("biometrics", false);
+ String pin = prefs.getString("pin", null);
- if (biometrics) {
+ if (biometrics || !TextUtils.isEmpty(pin)) {
long now = new Date().getTime();
long last_authentication = prefs.getLong("last_authentication", 0);
long biometrics_timeout = prefs.getInt("biometrics_timeout", 2) * 60 * 1000L;
@@ -831,74 +841,119 @@ public class Helper {
Runnable authenticated, final Runnable cancelled) {
final Handler handler = new Handler();
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
+ String pin = prefs.getString("pin", null);
- BiometricPrompt.PromptInfo.Builder info = new BiometricPrompt.PromptInfo.Builder()
- .setTitle(activity.getString(enabled == null ? R.string.app_name : R.string.title_setup_biometrics));
+ if (enabled != null || TextUtils.isEmpty(pin)) {
+ BiometricPrompt.PromptInfo.Builder info = new BiometricPrompt.PromptInfo.Builder()
+ .setTitle(activity.getString(enabled == null ? R.string.app_name : R.string.title_setup_biometrics));
- KeyguardManager kgm = (KeyguardManager) activity.getSystemService(Context.KEYGUARD_SERVICE);
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && kgm != null && kgm.isDeviceSecure())
- info.setDeviceCredentialAllowed(true);
- else
- info.setNegativeButtonText(activity.getString(android.R.string.cancel));
+ KeyguardManager kgm = (KeyguardManager) activity.getSystemService(Context.KEYGUARD_SERVICE);
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && kgm != null && kgm.isDeviceSecure())
+ info.setDeviceCredentialAllowed(true);
+ else
+ info.setNegativeButtonText(activity.getString(android.R.string.cancel));
- info.setSubtitle(activity.getString(enabled == null ? R.string.title_setup_biometrics_unlock
- : enabled
- ? R.string.title_setup_biometrics_disable
- : R.string.title_setup_biometrics_enable));
+ info.setSubtitle(activity.getString(enabled == null ? R.string.title_setup_biometrics_unlock
+ : enabled
+ ? R.string.title_setup_biometrics_disable
+ : R.string.title_setup_biometrics_enable));
- BiometricPrompt prompt = new BiometricPrompt(activity, executor,
- new BiometricPrompt.AuthenticationCallback() {
- @Override
- public void onAuthenticationError(final int errorCode, @NonNull final CharSequence errString) {
- Log.w("Biometric error " + errorCode + ": " + errString);
+ BiometricPrompt prompt = new BiometricPrompt(activity, executor,
+ new BiometricPrompt.AuthenticationCallback() {
+ @Override
+ public void onAuthenticationError(final int errorCode, @NonNull final CharSequence errString) {
+ Log.w("Biometric error " + errorCode + ": " + errString);
- handler.post(new Runnable() {
- @Override
- public void run() {
- if (errorCode == BiometricPrompt.ERROR_NEGATIVE_BUTTON ||
- errorCode == BiometricPrompt.ERROR_CANCELED ||
- errorCode == BiometricPrompt.ERROR_USER_CANCELED)
- cancelled.run();
- else
- ToastEx.makeText(activity,
- errString + " (" + errorCode + ")",
- Toast.LENGTH_LONG).show();
- }
- });
- }
-
- @Override
- public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
- Log.i("Biometric succeeded");
-
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
- prefs.edit().putLong("last_authentication", new Date().getTime()).apply();
-
- handler.post(new Runnable() {
- @Override
- public void run() {
- authenticated.run();
- }
- });
- }
-
- @Override
- public void onAuthenticationFailed() {
- Log.w("Biometric failed");
-
- handler.post(new Runnable() {
- @Override
- public void run() {
+ if (errorCode != BiometricPrompt.ERROR_NEGATIVE_BUTTON &&
+ errorCode != BiometricPrompt.ERROR_CANCELED &&
+ errorCode != BiometricPrompt.ERROR_USER_CANCELED)
ToastEx.makeText(activity,
- R.string.title_unexpected_error,
+ errString + " (" + errorCode + ")",
Toast.LENGTH_LONG).show();
- cancelled.run();
- }
- });
- }
- });
- prompt.authenticate(info.build());
+ handler.post(cancelled);
+ }
+
+ @Override
+ public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
+ Log.i("Biometric succeeded");
+
+ setAuthenticated(activity);
+ handler.post(authenticated);
+ }
+
+ @Override
+ public void onAuthenticationFailed() {
+ Log.w("Biometric failed");
+
+ ToastEx.makeText(activity,
+ R.string.title_unexpected_error,
+ Toast.LENGTH_LONG).show();
+ handler.post(cancelled);
+ }
+ });
+
+ prompt.authenticate(info.build());
+ } else {
+ final View dview = LayoutInflater.from(activity).inflate(R.layout.dialog_pin_ask, null);
+ final EditText etPin = dview.findViewById(R.id.etPin);
+
+ new Handler().post(new Runnable() {
+ @Override
+ public void run() {
+ etPin.requestFocus();
+ }
+ });
+
+ AlertDialog dialog = new AlertDialog.Builder(activity)
+ .setView(dview)
+ .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
+ String pin = prefs.getString("pin", "");
+ String entered = etPin.getText().toString();
+
+ if (pin.equals(entered)) {
+ setAuthenticated(activity);
+ handler.post(authenticated);
+ } else
+ handler.post(cancelled);
+ }
+ })
+ .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ handler.post(cancelled);
+ }
+ })
+ .setOnDismissListener(new DialogInterface.OnDismissListener() {
+ @Override
+ public void onDismiss(DialogInterface dialog) {
+ handler.post(cancelled);
+ }
+ })
+ .create();
+
+ etPin.setOnEditorActionListener(new TextView.OnEditorActionListener() {
+ @Override
+ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
+ if (actionId == EditorInfo.IME_ACTION_DONE) {
+ dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
+ return true;
+ } else
+ return false;
+ }
+ });
+
+ dialog.show();
+ }
+ }
+
+ private static void setAuthenticated(Context context) {
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
+ prefs.edit().putLong("last_authentication", new Date().getTime()).apply();
}
static void clearAuthentication(Context context) {
diff --git a/app/src/main/res/layout/dialog_pin_ask.xml b/app/src/main/res/layout/dialog_pin_ask.xml
new file mode 100644
index 0000000000..9d09a0a89a
--- /dev/null
+++ b/app/src/main/res/layout/dialog_pin_ask.xml
@@ -0,0 +1,32 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/src/main/res/layout/dialog_pin_set.xml b/app/src/main/res/layout/dialog_pin_set.xml
new file mode 100644
index 0000000000..bd9c1f0df6
--- /dev/null
+++ b/app/src/main/res/layout/dialog_pin_set.xml
@@ -0,0 +1,28 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/src/main/res/layout/fragment_options_privacy.xml b/app/src/main/res/layout/fragment_options_privacy.xml
index edf28d8a18..5d131928f8 100644
--- a/app/src/main/res/layout/fragment_options_privacy.xml
+++ b/app/src/main/res/layout/fragment_options_privacy.xml
@@ -90,5 +90,17 @@
android:entries="@array/biometricsTimeoutNames"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvBiometricsTimeout" />
+
+