From d7eeef2c8a1c143cff8f81a522039aebb0c8c04e Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 29 Sep 2018 01:57:51 -0400 Subject: [PATCH] Separate fingerprint authentication dialog code --- .../magisk/fragments/SettingsFragment.java | 59 ++--------------- .../magisk/utils/FingerprintHelper.java | 63 +++++++++++++++++++ 2 files changed, 67 insertions(+), 55 deletions(-) diff --git a/app/src/full/java/com/topjohnwu/magisk/fragments/SettingsFragment.java b/app/src/full/java/com/topjohnwu/magisk/fragments/SettingsFragment.java index 0ce6e9402..6e407078e 100644 --- a/app/src/full/java/com/topjohnwu/magisk/fragments/SettingsFragment.java +++ b/app/src/full/java/com/topjohnwu/magisk/fragments/SettingsFragment.java @@ -3,15 +3,9 @@ package com.topjohnwu.magisk.fragments; import android.annotation.SuppressLint; import android.content.Context; import android.content.SharedPreferences; -import android.content.res.Resources; -import android.content.res.TypedArray; -import android.graphics.Color; -import android.graphics.drawable.Drawable; -import android.hardware.fingerprint.FingerprintManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; -import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -24,7 +18,6 @@ import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.asyncs.CheckUpdates; import com.topjohnwu.magisk.asyncs.PatchAPK; -import com.topjohnwu.magisk.components.CustomAlertDialog; import com.topjohnwu.magisk.receivers.DownloadReceiver; import com.topjohnwu.magisk.utils.Download; import com.topjohnwu.magisk.utils.FingerprintHelper; @@ -277,54 +270,10 @@ public class SettingsFragment extends PreferenceFragmentCompat case Const.Key.SU_FINGERPRINT: boolean checked = ((SwitchPreference) preference).isChecked(); ((SwitchPreference) preference).setChecked(!checked); - CustomAlertDialog dialog = new CustomAlertDialog(requireActivity()); - CustomAlertDialog.ViewHolder vh = dialog.getViewHolder(); - Drawable fingerprint = getResources().getDrawable(R.drawable.ic_fingerprint); - fingerprint.setBounds(0, 0, Utils.dpInPx(50), Utils.dpInPx(50)); - Resources.Theme theme = requireActivity().getTheme(); - TypedArray ta = theme.obtainStyledAttributes(new int[] {R.attr.imageColorTint}); - fingerprint.setTint(ta.getColor(0, Color.GRAY)); - ta.recycle(); - vh.messageView.setCompoundDrawables(null, null, null, fingerprint); - vh.messageView.setCompoundDrawablePadding(Utils.dpInPx(20)); - vh.messageView.setGravity(Gravity.CENTER); - try { - FingerprintHelper helper = new FingerprintHelper() { - @Override - public void onAuthenticationError(int errorCode, CharSequence errString) { - vh.messageView.setTextColor(Color.RED); - vh.messageView.setText(errString); - } - - @Override - public void onAuthenticationHelp(int helpCode, CharSequence helpString) { - vh.messageView.setTextColor(Color.RED); - vh.messageView.setText(helpString); - } - - @Override - public void onAuthenticationFailed() { - vh.messageView.setTextColor(Color.RED); - vh.messageView.setText(R.string.auth_fail); - } - - @Override - public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) { - dialog.dismiss(); - ((SwitchPreference) preference).setChecked(checked); - mm.mDB.setSettings(key, checked ? 1 : 0); - - } - }; - dialog.setMessage(R.string.auth_fingerprint) - .setNegativeButton(R.string.close, (d, w) -> helper.cancel()) - .setOnCancelListener(d -> helper.cancel()) - .show(); - helper.authenticate(); - } catch (Exception e) { - e.printStackTrace(); - Utils.toast(R.string.auth_fail, Toast.LENGTH_SHORT); - } + FingerprintHelper.showAuthDialog(requireActivity(), () -> { + ((SwitchPreference) preference).setChecked(checked); + mm.mDB.setSettings(key, checked ? 1 : 0); + }); break; } return true; diff --git a/app/src/full/java/com/topjohnwu/magisk/utils/FingerprintHelper.java b/app/src/full/java/com/topjohnwu/magisk/utils/FingerprintHelper.java index 40e6bb709..57fa2545d 100644 --- a/app/src/full/java/com/topjohnwu/magisk/utils/FingerprintHelper.java +++ b/app/src/full/java/com/topjohnwu/magisk/utils/FingerprintHelper.java @@ -1,17 +1,28 @@ package com.topjohnwu.magisk.utils; import android.annotation.TargetApi; +import android.app.Activity; +import android.app.Dialog; import android.app.KeyguardManager; +import android.content.Context; +import android.content.res.Resources; +import android.content.res.TypedArray; +import android.graphics.Color; +import android.graphics.drawable.Drawable; import android.hardware.fingerprint.FingerprintManager; import android.os.Build; import android.os.CancellationSignal; import android.security.keystore.KeyGenParameterSpec; import android.security.keystore.KeyPermanentlyInvalidatedException; import android.security.keystore.KeyProperties; +import android.view.Gravity; +import android.widget.Toast; import com.topjohnwu.magisk.Const; import com.topjohnwu.magisk.Data; import com.topjohnwu.magisk.MagiskManager; +import com.topjohnwu.magisk.R; +import com.topjohnwu.magisk.components.CustomAlertDialog; import java.security.KeyStore; @@ -19,6 +30,8 @@ import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; +import androidx.preference.SwitchPreference; + @TargetApi(Build.VERSION_CODES.M) public abstract class FingerprintHelper { @@ -35,6 +48,56 @@ public abstract class FingerprintHelper { return km.isKeyguardSecure() && fm != null && fm.isHardwareDetected() && fm.hasEnrolledFingerprints(); } + public static void showAuthDialog(Activity activity, Runnable onSuccess) { + CustomAlertDialog dialog = new CustomAlertDialog(activity); + CustomAlertDialog.ViewHolder vh = dialog.getViewHolder(); + try { + FingerprintHelper helper = new FingerprintHelper() { + @Override + public void onAuthenticationError(int errorCode, CharSequence errString) { + vh.messageView.setTextColor(Color.RED); + vh.messageView.setText(errString); + } + + @Override + public void onAuthenticationHelp(int helpCode, CharSequence helpString) { + vh.messageView.setTextColor(Color.RED); + vh.messageView.setText(helpString); + } + + @Override + public void onAuthenticationFailed() { + vh.messageView.setTextColor(Color.RED); + vh.messageView.setText(R.string.auth_fail); + } + + @Override + public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) { + dialog.dismiss(); + onSuccess.run(); + } + }; + Drawable fingerprint = activity.getResources().getDrawable(R.drawable.ic_fingerprint); + fingerprint.setBounds(0, 0, Utils.dpInPx(50), Utils.dpInPx(50)); + Resources.Theme theme = activity.getTheme(); + TypedArray ta = theme.obtainStyledAttributes(new int[] {R.attr.imageColorTint}); + fingerprint.setTint(ta.getColor(0, Color.GRAY)); + ta.recycle(); + vh.messageView.setCompoundDrawables(null, null, null, fingerprint); + vh.messageView.setCompoundDrawablePadding(Utils.dpInPx(20)); + vh.messageView.setGravity(Gravity.CENTER); + dialog.setMessage(R.string.auth_fingerprint) + .setNegativeButton(R.string.close, (d, w) -> helper.cancel()) + .setOnCancelListener(d -> helper.cancel()) + .show(); + helper.authenticate(); + } catch (Exception e) { + e.printStackTrace(); + Utils.toast(R.string.auth_fail, Toast.LENGTH_SHORT); + } + + } + protected FingerprintHelper() throws Exception { KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore"); manager = Data.MM().getSystemService(FingerprintManager.class);