From fb329658ebc095f204fd3fd81cf56309fb4b1e11 Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Sat, 16 Feb 2019 16:41:02 -0600 Subject: [PATCH] Added the logic to create a new Security Lock pattern and save it encrypted in the preferences, with all used strings translated to Spanish. --- .../fragments/PatternSecurityLockDialog.kt | 94 +++++++++++++++++-- .../layout/dialog_pattern_security_lock.xml | 2 +- app/src/main/res/values-es/strings.xml | 7 ++ app/src/main/res/values/strings.xml | 7 ++ 4 files changed, 102 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/PatternSecurityLockDialog.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/PatternSecurityLockDialog.kt index e9dbd62..5b7f38f 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/PatternSecurityLockDialog.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/PatternSecurityLockDialog.kt @@ -1,6 +1,7 @@ package cy.agorise.bitsybitshareswallet.fragments import android.os.Bundle +import android.preference.PreferenceManager import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -8,7 +9,8 @@ import cy.agorise.bitsybitshareswallet.R import kotlinx.android.synthetic.main.dialog_pattern_security_lock.* import com.andrognito.patternlockview.PatternLockView import com.andrognito.patternlockview.listener.PatternLockViewListener -import cy.agorise.bitsybitshareswallet.utils.toast +import cy.agorise.bitsybitshareswallet.utils.Constants +import cy.agorise.bitsybitshareswallet.utils.CryptoUtils /** @@ -25,34 +27,102 @@ class PatternSecurityLockDialog : BaseSecurityLockDialog() { return inflater.inflate(R.layout.dialog_pattern_security_lock, container, false) } + private var newPattern = "" + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) setupScreen() patternLockView.addPatternLockListener(mPatternLockViewListener) + + btnClear.setOnClickListener { setupScreen() } } private val mPatternLockViewListener = object : PatternLockViewListener { override fun onStarted() { - context?.toast("Pattern started") + // Make sure the button is hidden when the user starts a new pattern when it was incorrect + btnClear.visibility = View.INVISIBLE + when (currentStep) { + STEP_SECURITY_LOCK_VERIFY -> { + // Do something + } + STEP_SECURITY_LOCK_CREATE -> { + tvMessage.text = getString(R.string.msg__release_finger) + } + STEP_SECURITY_LOCK_CONFIRM -> { + tvMessage.text = getString(R.string.msg__release_finger) + } + } } override fun onProgress(progressPattern: List) { - tvMessage.text = getString(R.string.msg__release_finger) + } override fun onComplete(pattern: List) { - if (pattern.size < 4) { - patternLockView.setViewMode(PatternLockView.PatternViewMode.WRONG) + if (currentStep == STEP_SECURITY_LOCK_VERIFY) { + // Do something + } else if (currentStep == STEP_SECURITY_LOCK_CREATE) { + btnClear.visibility = View.VISIBLE + if (pattern.size < 4) { + tvMessage.text = getString(R.string.error__connect_at_least_4_dots) + patternLockView.setViewMode(PatternLockView.PatternViewMode.WRONG) + } else { + tvMessage.text = getString(R.string.text__pattern_recorded) + patternLockView.setViewMode(PatternLockView.PatternViewMode.CORRECT) + patternLockView.isInputEnabled = false + btnNext.isEnabled = true + newPattern = getStringPattern(pattern) + btnNext.setOnClickListener { + currentStep = STEP_SECURITY_LOCK_CONFIRM + setupScreen() + } + } + } else if (currentStep == STEP_SECURITY_LOCK_CONFIRM) { + val patternConfirm = getStringPattern(pattern) + if (patternConfirm != newPattern) { + tvMessage.text = getString(R.string.error__wront_pattern) + btnNext.isEnabled = false + patternLockView.setViewMode(PatternLockView.PatternViewMode.WRONG) + } else { + tvMessage.text = getString(R.string.msg__your_new_unlock_pattern) + patternLockView.isEnabled = false + patternLockView.setViewMode(PatternLockView.PatternViewMode.CORRECT) + btnNext.isEnabled = true + btnNext.setOnClickListener { + context?.let { + val encryptedPattern = CryptoUtils.encrypt(it, patternConfirm).trim() + + // Stores the newly selected Pattern, encrypted + PreferenceManager.getDefaultSharedPreferences(it).edit() + .putString(Constants.KEY_ENCRYPTED_PIN, encryptedPattern) + .putInt(Constants.KEY_SECURITY_LOCK_SELECTED, 1).apply() // 1 -> Pattern + + dismiss() + mCallback?.onPINPatternChanged() + } + } + } } } override fun onCleared() { - context?.toast("Pattern has been cleared") + } } + /** + * Converts the given pattern into a string representation of it. + */ + private fun getStringPattern(pattern: List): String { + val sb = StringBuilder() + for (dot in pattern) + sb.append(dot.id) + + return sb.toString() + } + private fun setupScreen() { when (currentStep) { STEP_SECURITY_LOCK_VERIFY -> { @@ -63,12 +133,22 @@ class PatternSecurityLockDialog : BaseSecurityLockDialog() { STEP_SECURITY_LOCK_CREATE -> { tvTitle.text = getString(R.string.title__set_bitsy_screen_lock) tvSubTitle.text = getString(R.string.msg__set_bitsy_pattern) + tvMessage.text = getString(R.string.text__draw_an_unlock_pattern) + patternLockView.clearPattern() + patternLockView.isInputEnabled = true btnClear.visibility = View.INVISIBLE + btnNext.isEnabled = false } STEP_SECURITY_LOCK_CONFIRM -> { - tvTitle.text = getString(R.string.title__re_enter_your_pin) + tvTitle.text = getString(R.string.title__re_enter_your_pattern) tvSubTitle.text = "" + tvMessage.text = getString(R.string.msg__draw_pattern_confirm) tvSubTitle.visibility = View.GONE + patternLockView.clearPattern() + patternLockView.isInputEnabled = true + btnClear.visibility = View.INVISIBLE + btnNext.isEnabled = false + btnNext.text = getString(R.string.btn__confirm) } } } diff --git a/app/src/main/res/layout/dialog_pattern_security_lock.xml b/app/src/main/res/layout/dialog_pattern_security_lock.xml index 0aa9caf..0a67902 100644 --- a/app/src/main/res/layout/dialog_pattern_security_lock.xml +++ b/app/src/main/res/layout/dialog_pattern_security_lock.xml @@ -44,6 +44,7 @@ android:id="@+id/tvMessage" android:layout_width="match_parent" android:layout_height="wrap_content" + android:layout_marginTop="@dimen/spacing_same_topic" tools:text="Draw an unlock pattern" android:textAlignment="center" app:layout_constraintBottom_toTopOf="@id/patternLockView" @@ -55,7 +56,6 @@ android:id="@+id/patternLockView" android:layout_width="0dp" android:layout_height="0dp" - android:layout_marginBottom="@dimen/spacing_different_topic" app:normalStateColor="?android:textColorSecondary" app:correctStateColor="@color/colorPrimary" app:wrongStateColor="@color/colorSend" diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 9c041e0..f372890 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -157,5 +157,12 @@ Suelta tu dedo al terminar Limpiar Siguiente + Dibuja un patrón de desbloqueo + Dibuja tu patrón de nuevo para confirmar + Confirmar + Tu nuevo patrón de desbloqueo + Patrón incorrecto + Patrón registrado + Conecta al menos 4 puntos. Intenta de nuevo. diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9a1d447..ffbe8f6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -158,5 +158,12 @@ Release finger when done Clear Next + Draw an unlock pattern + Draw pattern again to confirm + Confirm + Your new unlock pattern + Wrong pattern + Pattern recorded + Connect at least 4 dots. Try again.