From 4f7f01f4cee852ce09107ab4aebef5cfb57f2cc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Manzano?= Date: Wed, 4 Oct 2017 08:35:43 +0200 Subject: [PATCH] Allow main dialog personalisation. (closes #11) The new `Style.kt` class allow to modify certain string resources and the style of the main dialog. --- .../KFingerprintManager.kt | 9 ++++- .../base/BaseFingerprintManager.kt | 3 +- .../base/ui/FingerprintBaseDialogFragment.kt | 38 ++++++++++++++++--- .../kfingerprintmanager/base/ui/Style.kt | 13 +++++++ .../res/layout/fingerprint_dialog_backup.xml | 4 +- .../res/layout/fingerprint_dialog_content.xml | 9 ++--- .../src/main/res/values/strings.xml | 4 +- 7 files changed, 62 insertions(+), 18 deletions(-) create mode 100644 kfingerprintmanager/src/main/kotlin/com/jesusm/kfingerprintmanager/base/ui/Style.kt diff --git a/kfingerprintmanager/src/main/kotlin/com/jesusm/kfingerprintmanager/KFingerprintManager.kt b/kfingerprintmanager/src/main/kotlin/com/jesusm/kfingerprintmanager/KFingerprintManager.kt index 44d3847..61bcca2 100644 --- a/kfingerprintmanager/src/main/kotlin/com/jesusm/kfingerprintmanager/KFingerprintManager.kt +++ b/kfingerprintmanager/src/main/kotlin/com/jesusm/kfingerprintmanager/KFingerprintManager.kt @@ -6,6 +6,7 @@ import android.support.v4.app.FragmentManager import android.support.v4.hardware.fingerprint.FingerprintManagerCompat import com.jesusm.kfingerprintmanager.authentication.AuthenticationManager import com.jesusm.kfingerprintmanager.base.FingerprintAssetsManager +import com.jesusm.kfingerprintmanager.base.ui.Style import com.jesusm.kfingerprintmanager.base.ui.System import com.jesusm.kfingerprintmanager.base.ui.SystemImpl import com.jesusm.kfingerprintmanager.encryption.Base64Encoder @@ -21,8 +22,12 @@ class KFingerprintManager @JvmOverloads constructor(context: Context, val encryptionManager: EncryptionManager = EncryptionManager(encoder, fingerprintAssetsManager, system)) { fun setAuthenticationDialogStyle(@StyleRes styleRes: Int) { - authenticationManager.authenticationDialogStyle = styleRes - encryptionManager.authenticationDialogStyle = styleRes + setAuthenticationDialogStyle(Style(styleRes)) + } + + fun setAuthenticationDialogStyle(style: Style) { + authenticationManager.authenticationDialogStyle = style + encryptionManager.authenticationDialogStyle = style } fun encrypt(messageToEncrypt: String, callback: EncryptionCallback, diff --git a/kfingerprintmanager/src/main/kotlin/com/jesusm/kfingerprintmanager/base/BaseFingerprintManager.kt b/kfingerprintmanager/src/main/kotlin/com/jesusm/kfingerprintmanager/base/BaseFingerprintManager.kt index dd8e1fe..486524e 100644 --- a/kfingerprintmanager/src/main/kotlin/com/jesusm/kfingerprintmanager/base/BaseFingerprintManager.kt +++ b/kfingerprintmanager/src/main/kotlin/com/jesusm/kfingerprintmanager/base/BaseFingerprintManager.kt @@ -3,12 +3,13 @@ package com.jesusm.kfingerprintmanager.base import android.support.v4.app.FragmentManager import com.jesusm.kfingerprintmanager.KFingerprintManager import com.jesusm.kfingerprintmanager.base.ui.FingerprintBaseDialogFragment.Builder +import com.jesusm.kfingerprintmanager.base.ui.Style import com.jesusm.kfingerprintmanager.base.ui.System abstract class BaseFingerprintManager(val fingerprintAssetsManager: FingerprintAssetsManager, private val system: System) { - var authenticationDialogStyle: Int = -1 + var authenticationDialogStyle: Style = Style() fun showFingerprintDialog(builder: Builder<*, *>, fragmentManager: FragmentManager, diff --git a/kfingerprintmanager/src/main/kotlin/com/jesusm/kfingerprintmanager/base/ui/FingerprintBaseDialogFragment.kt b/kfingerprintmanager/src/main/kotlin/com/jesusm/kfingerprintmanager/base/ui/FingerprintBaseDialogFragment.kt index 6cf56a1..23a32d5 100644 --- a/kfingerprintmanager/src/main/kotlin/com/jesusm/kfingerprintmanager/base/ui/FingerprintBaseDialogFragment.kt +++ b/kfingerprintmanager/src/main/kotlin/com/jesusm/kfingerprintmanager/base/ui/FingerprintBaseDialogFragment.kt @@ -12,6 +12,9 @@ import android.support.v7.app.AppCompatDialogFragment import android.support.v7.view.ContextThemeWrapper import android.view.LayoutInflater import android.view.View +import android.widget.EditText +import android.widget.ImageView +import android.widget.TextView import com.jesusm.kfingerprintmanager.KFingerprintManager import com.jesusm.kfingerprintmanager.R import com.jesusm.kfingerprintmanager.base.FingerprintAssetsManager @@ -41,6 +44,7 @@ abstract class FingerprintBaseDialogFragment dialogRootView = layoutInflater.inflate(R.layout.fingerprint_dialog_container, null, false) fingerprintContainer = dialogRootView.findViewById(R.id.fingerprint_dialog_content) inflateViews(dialogRootView) + applyStyleToView(dialogRootView) val builder = AlertDialog.Builder(context, customDialogStyle) builder.setView(dialogRootView) @@ -56,6 +60,24 @@ abstract class FingerprintBaseDialogFragment @CallSuper open fun inflateViews(rootView: View) {} + private fun applyStyleToView(rootView: View) { + if (style.style != -1) { + customDialogStyle = style.style + } + + rootView.apply { + val titleView = findViewById(R.id.fingerprint_dialog_title) + titleView.text = getString(style.titleText) + val descriptionView = findViewById(R.id.fingerprint_dialog_description) + descriptionView.text = getString(style.descriptionText) + val checkboxView = findViewById(R.id.use_fingerprint_in_future_check) + checkboxView.text = getString(style.checkText) + val passwordView = findViewById(R.id.password) + passwordView.hint = getString(style.passwordTextDescription) + findViewById(R.id.fingerprint_icon).setImageResource(style.fingerprintIcon) + } + } + @CallSuper open fun onDialogShown() { presenter?.onViewShown() @@ -88,7 +110,7 @@ abstract class FingerprintBaseDialogFragment } override fun close() { - dismissAllowingStateLoss() + dismiss() } override fun onCancel(dialog: DialogInterface?) { @@ -109,13 +131,19 @@ abstract class FingerprintBaseDialogFragment callback?.onFingerprintNotAvailable() } + private lateinit var style: Style + + fun applyStyle(style: Style) { + this.style = style + } + abstract class Builder, P : FingerprintBaseDialogPresenter> { - private var customStyle: Int = 0 + private var customStyle: Style = Style() private var callback: KFingerprintManager.FingerprintBaseCallback? = null private lateinit var fingerPrintHardware: FingerprintHardware private lateinit var cryptoObject: FingerprintManagerCompat.CryptoObject - fun withCustomStyle(customStyle: Int): Builder<*, *> { + fun withCustomStyle(customStyle: Style): Builder<*, *> { this.customStyle = customStyle return this } @@ -144,9 +172,7 @@ abstract class FingerprintBaseDialogFragment val presenter = createPresenter(dialogFragment) dialogFragment.presenter = presenter presenter.setFingerprintHardware(fingerPrintHardware, cryptoObject) - if (customStyle != -1) { - dialogFragment.customDialogStyle = customStyle - } + dialogFragment.applyStyle(customStyle) addProperties(dialogFragment) diff --git a/kfingerprintmanager/src/main/kotlin/com/jesusm/kfingerprintmanager/base/ui/Style.kt b/kfingerprintmanager/src/main/kotlin/com/jesusm/kfingerprintmanager/base/ui/Style.kt new file mode 100644 index 0000000..6232578 --- /dev/null +++ b/kfingerprintmanager/src/main/kotlin/com/jesusm/kfingerprintmanager/base/ui/Style.kt @@ -0,0 +1,13 @@ +package com.jesusm.kfingerprintmanager.base.ui + +import android.support.annotation.DrawableRes +import android.support.annotation.StringRes +import android.support.annotation.StyleRes +import com.jesusm.kfingerprintmanager.R + + +data class Style(@StyleRes val style: Int = -1, @StringRes val titleText: Int = R.string.fingerprint_title, + @StringRes val descriptionText: Int = R.string.fingerprint_description, + @StringRes val checkText: Int = R.string.use_fingerprint_in_future, + @StringRes val passwordTextDescription: Int = R.string.fingerprint_description, + @DrawableRes val fingerprintIcon: Int = R.drawable.fingerprint_manager_icon_white_24dp) \ No newline at end of file diff --git a/kfingerprintmanager/src/main/res/layout/fingerprint_dialog_backup.xml b/kfingerprintmanager/src/main/res/layout/fingerprint_dialog_backup.xml index 58795e5..2618a68 100644 --- a/kfingerprintmanager/src/main/res/layout/fingerprint_dialog_backup.xml +++ b/kfingerprintmanager/src/main/res/layout/fingerprint_dialog_backup.xml @@ -38,7 +38,7 @@ android:layout_below="@+id/description" android:layout_marginTop="16dp" android:ems="10" - android:hint="@string/password_description" + tools:hint="@string/password_description" android:imeOptions="actionGo" android:inputType="textPassword" /> @@ -52,7 +52,7 @@ android:layout_marginStart="4dp" android:layout_marginTop="16dp" android:checked="true" - android:text="@string/use_fingerprint_in_future" + tools:text="@string/use_fingerprint_in_future" android:visibility="gone" tools:visibility="visible" /> diff --git a/kfingerprintmanager/src/main/res/layout/fingerprint_dialog_content.xml b/kfingerprintmanager/src/main/res/layout/fingerprint_dialog_content.xml index d601692..b66a632 100644 --- a/kfingerprintmanager/src/main/res/layout/fingerprint_dialog_content.xml +++ b/kfingerprintmanager/src/main/res/layout/fingerprint_dialog_content.xml @@ -15,7 +15,6 @@ --> diff --git a/kfingerprintmanager/src/main/res/values/strings.xml b/kfingerprintmanager/src/main/res/values/strings.xml index 3b9f202..da3bf4d 100644 --- a/kfingerprintmanager/src/main/res/values/strings.xml +++ b/kfingerprintmanager/src/main/res/values/strings.xml @@ -7,8 +7,8 @@ Use fingerprint in the future use_fingerprint_to_authenticate_key - Confirm fingerprint to continue - Touch sensor + Confirm fingerprint to continue + Touch sensor Security -> Fingerprint\' to set up a fingerprint\"]]>