add the actual fingerprint check

This commit is contained in:
tibbi 2017-09-27 19:52:24 +02:00
parent 65abda8f20
commit 705812c8bf
9 changed files with 92 additions and 40 deletions

View file

@ -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<SecurityTab>()
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)
}
}

View file

@ -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)
}
}
}

View file

@ -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

View file

@ -2,4 +2,6 @@ package com.simplemobiletools.commons.interfaces
interface SecurityTab {
fun initTab(requiredHash: String, listener: HashListener)
fun visibilityChanged(isVisible: Boolean)
}

View file

@ -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()
}
}
}

View file

@ -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<PatternLockView.Dot>?) {
}
@ -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) {}
}

View file

@ -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) {}
}

View file

@ -13,7 +13,7 @@
android:text="@string/add_fingerprint"/>
<ImageView
android:id="@+id/finterprint_image"
android:id="@+id/fingerprint_image"
android:layout_width="@dimen/fingerprint_icon_size"
android:layout_height="@dimen/fingerprint_icon_size"
android:layout_below="@+id/fingerprint_lock_title"
@ -21,4 +21,16 @@
android:layout_marginTop="@dimen/activity_margin"
android:src="@drawable/ic_fingerprint"/>
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/fingerprint_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/fingerprint_image"
android:layout_centerHorizontal="true"
android:layout_marginLeft="@dimen/activity_margin"
android:layout_marginRight="@dimen/activity_margin"
android:gravity="center"
android:padding="@dimen/activity_margin"
android:text="@string/place_finger"/>
</com.simplemobiletools.commons.views.FingerprintTab>

View file

@ -167,6 +167,7 @@
<string name="repeat_pattern">Repeat pattern</string>
<string name="fingerprint">Fingerprint</string>
<string name="add_fingerprint">Add fingerprint</string>
<string name="place_finger">Please place your finger on the fingerprint sensor</string>
<string name="protection_setup_successfully">Password setup successfully. Please reinstall the app in case you forget it.</string>
<!-- Times -->