Skip to content

Commit

Permalink
Allow main dialog personalisation. (closes #11)
Browse files Browse the repository at this point in the history
The new `Style.kt` class allow to modify certain string resources and the style of the main dialog.
  • Loading branch information
JesusM committed Oct 12, 2017
1 parent be9f8a3 commit 4f7f01f
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -41,6 +44,7 @@ abstract class FingerprintBaseDialogFragment<T : FingerprintBaseDialogPresenter>
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)
Expand All @@ -56,6 +60,24 @@ abstract class FingerprintBaseDialogFragment<T : FingerprintBaseDialogPresenter>
@CallSuper
open fun inflateViews(rootView: View) {}

private fun applyStyleToView(rootView: View) {
if (style.style != -1) {
customDialogStyle = style.style
}

rootView.apply {
val titleView = findViewById<TextView>(R.id.fingerprint_dialog_title)
titleView.text = getString(style.titleText)
val descriptionView = findViewById<TextView>(R.id.fingerprint_dialog_description)
descriptionView.text = getString(style.descriptionText)
val checkboxView = findViewById<TextView>(R.id.use_fingerprint_in_future_check)
checkboxView.text = getString(style.checkText)
val passwordView = findViewById<EditText>(R.id.password)
passwordView.hint = getString(style.passwordTextDescription)
findViewById<ImageView>(R.id.fingerprint_icon).setImageResource(style.fingerprintIcon)
}
}

@CallSuper
open fun onDialogShown() {
presenter?.onViewShown()
Expand Down Expand Up @@ -88,7 +110,7 @@ abstract class FingerprintBaseDialogFragment<T : FingerprintBaseDialogPresenter>
}

override fun close() {
dismissAllowingStateLoss()
dismiss()
}

override fun onCancel(dialog: DialogInterface?) {
Expand All @@ -109,13 +131,19 @@ abstract class FingerprintBaseDialogFragment<T : FingerprintBaseDialogPresenter>
callback?.onFingerprintNotAvailable()
}

private lateinit var style: Style

fun applyStyle(style: Style) {
this.style = style
}

abstract class Builder<D : FingerprintBaseDialogFragment<P>, 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
}
Expand Down Expand Up @@ -144,9 +172,7 @@ abstract class FingerprintBaseDialogFragment<T : FingerprintBaseDialogPresenter>
val presenter = createPresenter(dialogFragment)
dialogFragment.presenter = presenter
presenter.setFingerprintHardware(fingerPrintHardware, cryptoObject)
if (customStyle != -1) {
dialogFragment.customDialogStyle = customStyle
}
dialogFragment.applyStyle(customStyle)

addProperties(dialogFragment)

Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

Expand All @@ -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" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,28 @@
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fingerprint_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
tools:showIn="@layout/fingerprint_dialog_container">

<TextView
android:id="@+id/fingerprint_description"
android:id="@+id/fingerprint_dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fingerprint_description"
tools:text="@string/fingerprint_title"
android:textColor="?android:attr/textColorPrimary"
android:gravity="center_horizontal"
android:textSize="21sp" />

<TextView
android:id="@+id/fingerprint_status"
android:id="@+id/fingerprint_dialog_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_gravity="center_horizontal"
android:text="@string/fingerprint_hint"
tools:text="@string/fingerprint_description"
android:gravity="center_horizontal"
android:textColor="?android:attr/textColorSecondary"
android:textSize="18sp" />
Expand Down
4 changes: 2 additions & 2 deletions kfingerprintmanager/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

<string name="use_fingerprint_in_future">Use fingerprint in the future</string>
<string name="use_fingerprint_to_authenticate_key" >use_fingerprint_to_authenticate_key</string>
<string name="fingerprint_description">Confirm fingerprint to continue</string>
<string name="fingerprint_hint">Touch sensor</string>
<string name="fingerprint_title">Confirm fingerprint to continue</string>
<string name="fingerprint_description">Touch sensor</string>

<!-- Error messages -->
<string name="fingerprint_auth_not_available_msg"><![CDATA[\"Secure lock screen hasn\'t set up.\\n\" + \"Go to \'Settings -> Security -> Fingerprint\' to set up a fingerprint\"]]></string>
Expand Down

0 comments on commit 4f7f01f

Please sign in to comment.