Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow main dialog personalisation. (closes #11) #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ buildscript {
ext.kotlin_version = '1.1.2-3'
repositories {
jcenter()
maven {
url 'https://maven.google.com'
}
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.2'
classpath 'com.android.tools.build:gradle:2.3.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand All @@ -20,16 +18,17 @@ buildscript {
allprojects {
repositories {
jcenter()
google()
}
}

ext {
minSdkVersion = 23
targetSdkVersion = 25
compileSdkVersion = 25
buildToolsVersion = '25.0.3'
targetSdkVersion = 26
compileSdkVersion = 26
buildToolsVersion = '26.0.2'

supportLibsVersion = '25.1.0'
supportLibsVersion = '26.0.2'
}

task clean(type: Delete) {
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu May 18 07:41:34 CEST 2017
#Sat Sep 02 09:56:43 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-milestone-1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
2 changes: 1 addition & 1 deletion kfingerprintmanager/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:design:' + parent.ext.supportLibsVersion
compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.2-3"
compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.3-2"

testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:2.7.21'
Expand Down
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 @@ -30,19 +30,19 @@ class FingerprintAuthenticationDialogFragment : FingerprintBaseDialogFragment<Fi
fun onPasswordInserted(password: String)
}

val passwordContainer : View by lazy {
dialogRootView.findViewById(R.id.fingerprint_dialog_backup_content)
private val passwordContainer : View by lazy {
dialogRootView.findViewById<View>(R.id.fingerprint_dialog_backup_content)
}
val password by lazy {
dialogRootView.findViewById(R.id.password) as EditText
val password: EditText by lazy {
dialogRootView.findViewById<EditText>(R.id.password)
}

val textInputLayout by lazy {
dialogRootView.findViewById(R.id.input_layout_password) as TextInputLayout
val textInputLayout: TextInputLayout by lazy {
dialogRootView.findViewById<TextInputLayout>(R.id.input_layout_password)
}

val useFingerprintFutureCheckBox by lazy {
dialogRootView.findViewById(R.id.use_fingerprint_in_future_check) as CheckBox
private val useFingerprintFutureCheckBox by lazy {
dialogRootView.findViewById<CheckBox>(R.id.use_fingerprint_in_future_check)
}

private val sharedPreferences: SharedPreferences by lazy {
Expand All @@ -51,12 +51,6 @@ class FingerprintAuthenticationDialogFragment : FingerprintBaseDialogFragment<Fi

private var startWithNewFingerprintEnrolled: Boolean = false

fun onPresenterChanged(new: FingerprintAuthenticationDialogPresenter) {
if (startWithNewFingerprintEnrolled) {
new.newFingerprintEnrolled()
}
}

override fun inflateViews(rootView: View) {
super.inflateViews(rootView)

Expand Down Expand Up @@ -96,18 +90,17 @@ class FingerprintAuthenticationDialogFragment : FingerprintBaseDialogFragment<Fi
dialogBuilder.setPositiveButton(R.string.use_password, null)
}

override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
return when (actionId) {
EditorInfo.IME_ACTION_GO -> {
dismiss()
true
}
else -> {
textInputLayout.error = null
false
override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean =
when (actionId) {
EditorInfo.IME_ACTION_GO -> {
dismiss()
true
}
else -> {
textInputLayout.error = null
false
}
}
}
}

override fun onFingerprintDisplayed() {
fingerprintContainer.visibility = VISIBLE
Expand Down Expand Up @@ -177,26 +170,24 @@ class FingerprintAuthenticationDialogFragment : FingerprintBaseDialogFragment<Fi
}

class Builder : FingerprintBaseDialogFragment.Builder<FingerprintAuthenticationDialogFragment, FingerprintAuthenticationDialogPresenter>() {
internal var newFingerprintEnrolled: Boolean = false
private var newFingerprintEnrolled: Boolean = false

fun newFingerprintEnrolled(newFingerprintEnrolled: Boolean): Builder {
this.newFingerprintEnrolled = newFingerprintEnrolled
return this
}

override fun createDialogFragment(): FingerprintAuthenticationDialogFragment {
return FingerprintAuthenticationDialogFragment()
}
override fun createDialogFragment(): FingerprintAuthenticationDialogFragment =
FingerprintAuthenticationDialogFragment()

override fun addProperties(dialogFragment: FingerprintAuthenticationDialogFragment) {
if (newFingerprintEnrolled) {
dialogFragment.startWithNewFingerprintEnrolled()
}
}

override fun createPresenter(view: FingerprintAuthenticationDialogFragment): FingerprintAuthenticationDialogPresenter {
return FingerprintAuthenticationDialogPresenter(view)
}
override fun createPresenter(view: FingerprintAuthenticationDialogFragment): FingerprintAuthenticationDialogPresenter =
FingerprintAuthenticationDialogPresenter(view)
}

private open class TextWatcherAdapter : TextWatcher {
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 @@ -54,7 +58,24 @@ abstract class FingerprintBaseDialogFragment<T : FingerprintBaseDialogPresenter>
}

@CallSuper
open fun inflateViews(rootView: View) {
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
Expand Down Expand Up @@ -110,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 @@ -145,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 @@ -16,12 +16,10 @@ class FingerprintEncryptionDialogFragment : FingerprintBaseDialogFragment<Finger

}

override fun createDialogFragment(): FingerprintEncryptionDialogFragment {
return FingerprintEncryptionDialogFragment()
}
override fun createDialogFragment(): FingerprintEncryptionDialogFragment =
FingerprintEncryptionDialogFragment()

override fun createPresenter(view: FingerprintEncryptionDialogFragment): FingerprintEncryptionDialogPresenter {
return FingerprintEncryptionDialogPresenter(view)
}
override fun createPresenter(view: FingerprintEncryptionDialogFragment): FingerprintEncryptionDialogPresenter =
FingerprintEncryptionDialogPresenter(view)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.view.View

fun <T : View> Activity.bind(@IdRes idRes: Int): Lazy<T> {
@Suppress("UNCHECKED_CAST")
return unsafeLazy { findViewById(idRes) as T }
return unsafeLazy { findViewById<T>(idRes)}
}

private fun <T> unsafeLazy(initializer: () -> T) = lazy(LazyThreadSafetyMode.NONE, initializer)
Expand Down
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
2 changes: 1 addition & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ android {
dependencies {
compile 'com.android.support:appcompat-v7:' + parent.ext.supportLibsVersion
compile project(':kfingerprintmanager')
compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.2-4"
compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.3-2"
}

repositories {
Expand Down
Loading