From 705812c8bf11e5f7c168eda7e1e56d46a59b361d Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 27 Sep 2017 19:52:24 +0200 Subject: [PATCH] add the actual fingerprint check --- .../commons/adapters/PasswordTypesAdapter.kt | 8 ++++ .../commons/dialogs/SecurityDialog.kt | 23 ++++++++- .../commons/helpers/Constants.kt | 1 + .../commons/interfaces/SecurityTab.kt | 2 + .../commons/views/FingerprintTab.kt | 33 +++++++------ .../commons/views/PatternTab.kt | 48 ++++++++++--------- .../simplemobiletools/commons/views/PinTab.kt | 2 + .../src/main/res/layout/tab_fingerprint.xml | 14 +++++- commons/src/main/res/values/strings.xml | 1 + 9 files changed, 92 insertions(+), 40 deletions(-) diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/PasswordTypesAdapter.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/PasswordTypesAdapter.kt index 535ddd19a..17b3aa5a5 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/PasswordTypesAdapter.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/PasswordTypesAdapter.kt @@ -2,6 +2,7 @@ package com.simplemobiletools.commons.adapters import android.content.Context import android.support.v4.view.PagerAdapter +import android.util.SparseArray import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -11,15 +12,18 @@ import com.simplemobiletools.commons.interfaces.HashListener import com.simplemobiletools.commons.interfaces.SecurityTab class PasswordTypesAdapter(val context: Context, val requiredHash: String, val hashListener: HashListener) : PagerAdapter() { + private val tabs = SparseArray() override fun instantiateItem(container: ViewGroup, position: Int): Any { val view = LayoutInflater.from(context).inflate(layoutSelection(position), container, false) container.addView(view) + tabs.put(position, view as SecurityTab) (view as SecurityTab).initTab(requiredHash, hashListener) return view } override fun destroyItem(container: ViewGroup, position: Int, item: Any) { + tabs.remove(position) container.removeView(item as View) } @@ -33,4 +37,8 @@ class PasswordTypesAdapter(val context: Context, val requiredHash: String, val h 2 -> R.layout.tab_fingerprint else -> throw RuntimeException("Only 3 tabs allowed") } + + fun isTabVisible(position: Int, isVisible: Boolean) { + tabs[position]?.visibilityChanged(isVisible) + } } diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/SecurityDialog.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/SecurityDialog.kt index ebbf62e42..d9dd0cece 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/SecurityDialog.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/SecurityDialog.kt @@ -4,6 +4,7 @@ import android.support.design.widget.TabLayout import android.support.v4.view.ViewPager import android.support.v7.app.AlertDialog import android.view.LayoutInflater +import android.view.ViewTreeObserver import com.simplemobiletools.commons.R import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.adapters.PasswordTypesAdapter @@ -22,12 +23,15 @@ import kotlinx.android.synthetic.main.dialog_security.view.* class SecurityDialog(val activity: BaseSimpleActivity, val requiredHash: String, val showTabIndex: Int, val callback: (hash: String, type: Int) -> Unit) : HashListener { var dialog: AlertDialog? = null val view = LayoutInflater.from(activity).inflate(R.layout.dialog_security, null) + lateinit var tabsAdapter: PasswordTypesAdapter + lateinit var viewPager: MyDialogViewPager init { view.apply { - val viewPager = findViewById(R.id.dialog_tab_view_pager) as MyDialogViewPager + viewPager = findViewById(R.id.dialog_tab_view_pager) as MyDialogViewPager viewPager.offscreenPageLimit = 2 - viewPager.adapter = PasswordTypesAdapter(context, requiredHash, this@SecurityDialog) + tabsAdapter = PasswordTypesAdapter(context, requiredHash, this@SecurityDialog) + viewPager.adapter = tabsAdapter viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener { override fun onPageScrollStateChanged(state: Int) { } @@ -40,6 +44,13 @@ class SecurityDialog(val activity: BaseSimpleActivity, val requiredHash: String, } }) + viewPager.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { + override fun onGlobalLayout() { + viewPager.viewTreeObserver.removeOnGlobalLayoutListener(this) + updateTabVisibility() + } + }) + if (showTabIndex == SHOW_ALL_TABS) { val textColor = context.baseConfig.textColor @@ -61,6 +72,8 @@ class SecurityDialog(val activity: BaseSimpleActivity, val requiredHash: String, tab.text.toString().equals(resources.getString(R.string.pin), true) -> viewPager.currentItem = PROTECTION_PIN else -> viewPager.currentItem = PROTECTION_FINGERPRINT } + + updateTabVisibility() } }) } else { @@ -81,4 +94,10 @@ class SecurityDialog(val activity: BaseSimpleActivity, val requiredHash: String, callback(hash, type) dialog!!.dismiss() } + + private fun updateTabVisibility() { + for (i in 0..2) { + tabsAdapter.isTabVisible(i, viewPager.currentItem == i) + } + } } diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/Constants.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/Constants.kt index edd4df18a..af5aad3cb 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/Constants.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/Constants.kt @@ -60,3 +60,4 @@ val PROTECTION_FINGERPRINT = 2 val SHOW_ALL_TABS = -1 val SHOW_PATTERN = 0 val SHOW_PIN = 1 +val SHOW_FINGERPRINT = 2 diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/interfaces/SecurityTab.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/interfaces/SecurityTab.kt index ef82936c3..87ca89625 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/interfaces/SecurityTab.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/interfaces/SecurityTab.kt @@ -2,4 +2,6 @@ package com.simplemobiletools.commons.interfaces interface SecurityTab { fun initTab(requiredHash: String, listener: HashListener) + + fun visibilityChanged(isVisible: Boolean) } diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/views/FingerprintTab.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/views/FingerprintTab.kt index 405c47944..5b4843a3d 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/views/FingerprintTab.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/views/FingerprintTab.kt @@ -10,35 +10,38 @@ import com.github.ajalt.reprint.core.AuthenticationListener import com.github.ajalt.reprint.core.Reprint import com.simplemobiletools.commons.extensions.baseConfig import com.simplemobiletools.commons.extensions.updateTextColors +import com.simplemobiletools.commons.helpers.PROTECTION_FINGERPRINT import com.simplemobiletools.commons.interfaces.HashListener import com.simplemobiletools.commons.interfaces.SecurityTab import kotlinx.android.synthetic.main.tab_fingerprint.view.* class FingerprintTab(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs), SecurityTab { - private var hash = "" - private var requiredHash = "" lateinit var hashListener: HashListener override fun onFinishInflate() { super.onFinishInflate() val textColor = context.baseConfig.textColor context.updateTextColors(fingerprint_lock_holder) - finterprint_image.colorFilter = PorterDuffColorFilter(textColor, PorterDuff.Mode.SRC_IN) - - Reprint.authenticate(object : AuthenticationListener { - override fun onSuccess(moduleTag: Int) { - - } - - override fun onFailure(failureReason: AuthenticationFailureReason, fatal: Boolean, errorMessage: CharSequence?, moduleTag: Int, errorCode: Int) { - - } - }) + fingerprint_image.colorFilter = PorterDuffColorFilter(textColor, PorterDuff.Mode.SRC_IN) } override fun initTab(requiredHash: String, listener: HashListener) { - this.requiredHash = requiredHash - hash = requiredHash hashListener = listener } + + override fun visibilityChanged(isVisible: Boolean) { + if (isVisible) { + Reprint.authenticate(object : AuthenticationListener { + override fun onSuccess(moduleTag: Int) { + hashListener.receivedHash("", PROTECTION_FINGERPRINT) + } + + override fun onFailure(failureReason: AuthenticationFailureReason, fatal: Boolean, errorMessage: CharSequence?, moduleTag: Int, errorCode: Int) { + + } + }) + } else { + Reprint.cancelAuthentication() + } + } } diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/views/PatternTab.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/views/PatternTab.kt index 28ce5a8c0..2faca0a0c 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/views/PatternTab.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/views/PatternTab.kt @@ -32,11 +32,9 @@ class PatternTab(context: Context, attrs: AttributeSet) : RelativeLayout(context receivedHash(PatternLockUtils.patternToSha1(pattern_lock_view, pattern)) } - override fun onCleared() { - } + override fun onCleared() {} - override fun onStarted() { - } + override fun onStarted() {} override fun onProgress(progressPattern: MutableList?) { } @@ -50,25 +48,31 @@ class PatternTab(context: Context, attrs: AttributeSet) : RelativeLayout(context } private fun receivedHash(newHash: String) { - if (hash.isEmpty()) { - hash = newHash - pattern_lock_view.clearPattern() - pattern_lock_title.setText(R.string.repeat_pattern) - } else if (hash == newHash) { - pattern_lock_view.setViewMode(PatternLockView.PatternViewMode.CORRECT) - Handler().postDelayed({ - hashListener.receivedHash(hash, PROTECTION_PATTERN) - }, 300) - } else { - pattern_lock_view.setViewMode(PatternLockView.PatternViewMode.WRONG) - context.toast(R.string.wrong_pattern) - Handler().postDelayed({ + when { + hash.isEmpty() -> { + hash = newHash pattern_lock_view.clearPattern() - if (requiredHash.isEmpty()) { - hash = "" - pattern_lock_title.setText(R.string.insert_pattern) - } - }, 1000) + pattern_lock_title.setText(R.string.repeat_pattern) + } + hash == newHash -> { + pattern_lock_view.setViewMode(PatternLockView.PatternViewMode.CORRECT) + Handler().postDelayed({ + hashListener.receivedHash(hash, PROTECTION_PATTERN) + }, 300) + } + else -> { + pattern_lock_view.setViewMode(PatternLockView.PatternViewMode.WRONG) + context.toast(R.string.wrong_pattern) + Handler().postDelayed({ + pattern_lock_view.clearPattern() + if (requiredHash.isEmpty()) { + hash = "" + pattern_lock_title.setText(R.string.insert_pattern) + } + }, 1000) + } } } + + override fun visibilityChanged(isVisible: Boolean) {} } diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/views/PinTab.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/views/PinTab.kt index f16af9212..f88754f50 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/views/PinTab.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/views/PinTab.kt @@ -97,4 +97,6 @@ class PinTab(context: Context, attrs: AttributeSet) : RelativeLayout(context, at val bigInteger = BigInteger(1, digest) return String.format(Locale.getDefault(), "%0${digest.size * 2}x", bigInteger).toLowerCase() } + + override fun visibilityChanged(isVisible: Boolean) {} } diff --git a/commons/src/main/res/layout/tab_fingerprint.xml b/commons/src/main/res/layout/tab_fingerprint.xml index 7df149492..10e44754c 100644 --- a/commons/src/main/res/layout/tab_fingerprint.xml +++ b/commons/src/main/res/layout/tab_fingerprint.xml @@ -13,7 +13,7 @@ android:text="@string/add_fingerprint"/> + + diff --git a/commons/src/main/res/values/strings.xml b/commons/src/main/res/values/strings.xml index c33914817..3e1539950 100644 --- a/commons/src/main/res/values/strings.xml +++ b/commons/src/main/res/values/strings.xml @@ -167,6 +167,7 @@ Repeat pattern Fingerprint Add fingerprint + Please place your finger on the fingerprint sensor Password setup successfully. Please reinstall the app in case you forget it.