From fff519b5214e799d8ef58a03180028cb6f6136ae Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 29 Jul 2017 20:13:19 +0200 Subject: [PATCH] adding a pattern protection to the toggle button --- app/build.gradle | 1 + .../gallery/activities/SettingsActivity.kt | 16 ++++- .../gallery/dialogs/PatternDialog.kt | 68 +++++++++++++++++++ .../gallery/helpers/Config.kt | 10 ++- .../gallery/helpers/Constants.kt | 3 +- app/src/main/res/layout/dialog_pattern.xml | 22 ++++++ app/src/main/res/values-cs/strings.xml | 2 + app/src/main/res/values-de/strings.xml | 2 + app/src/main/res/values-es/strings.xml | 2 + app/src/main/res/values-fr/strings.xml | 2 + app/src/main/res/values-hu/strings.xml | 2 + app/src/main/res/values-it/strings.xml | 2 + app/src/main/res/values-ja/strings.xml | 2 + app/src/main/res/values-pl/strings.xml | 2 + app/src/main/res/values-pt-rBR/strings.xml | 2 + app/src/main/res/values-pt/strings.xml | 2 + app/src/main/res/values-ru/strings.xml | 2 + app/src/main/res/values-sk/strings.xml | 2 + app/src/main/res/values-sv/strings.xml | 2 + app/src/main/res/values-tr/strings.xml | 2 + app/src/main/res/values-zh-rCN/strings.xml | 2 + app/src/main/res/values-zh-rTW/strings.xml | 2 + app/src/main/res/values/strings.xml | 2 + 23 files changed, 147 insertions(+), 7 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PatternDialog.kt create mode 100644 app/src/main/res/layout/dialog_pattern.xml diff --git a/app/build.gradle b/app/build.gradle index 8c287ec87..bcd35c2e4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -39,6 +39,7 @@ dependencies { compile 'com.google.code.gson:gson:2.8.0' compile 'com.github.chrisbanes:PhotoView:2.1.2' compile 'it.sephiroth.android.exif:library:1.0.1' + compile 'com.andrognito.patternlockview:patternlockview:1.0.0' compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.1' diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SettingsActivity.kt index 5451dc6cf..8493c1dab 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SettingsActivity.kt @@ -7,6 +7,7 @@ import com.simplemobiletools.commons.dialogs.RadioGroupDialog import com.simplemobiletools.commons.extensions.updateTextColors import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.gallery.R +import com.simplemobiletools.gallery.dialogs.PatternDialog import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.helpers.* import kotlinx.android.synthetic.main.activity_settings.* @@ -141,10 +142,19 @@ class SettingsActivity : SimpleActivity() { } private fun setupPasswordProtection() { - settings_password_protection.isChecked = config.passwordProtection + settings_password_protection.isChecked = config.isPasswordProtectionOn settings_password_protection_holder.setOnClickListener { - settings_password_protection.toggle() - config.passwordProtection = settings_password_protection.isChecked + PatternDialog(this) { + if (config.isPasswordProtectionOn) { + settings_password_protection.isChecked = false + config.isPasswordProtectionOn = false + config.passwordHash = "" + } else { + settings_password_protection.isChecked = true + config.isPasswordProtectionOn = true + config.passwordHash = it + } + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PatternDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PatternDialog.kt new file mode 100644 index 000000000..e0f27ab7f --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PatternDialog.kt @@ -0,0 +1,68 @@ +package com.simplemobiletools.gallery.dialogs + +import android.os.Handler +import android.support.v7.app.AlertDialog +import android.view.LayoutInflater +import com.andrognito.patternlockview.PatternLockView +import com.andrognito.patternlockview.listener.PatternLockViewListener +import com.andrognito.patternlockview.utils.PatternLockUtils +import com.simplemobiletools.commons.extensions.setupDialogStuff +import com.simplemobiletools.commons.extensions.toast +import com.simplemobiletools.gallery.R +import com.simplemobiletools.gallery.activities.SimpleActivity +import com.simplemobiletools.gallery.extensions.config +import kotlinx.android.synthetic.main.dialog_pattern.view.* + +class PatternDialog(val activity: SimpleActivity, val callback: (hash: String) -> Unit) { + var dialog: AlertDialog? = null + + init { + var hash = "" + val view = LayoutInflater.from(activity).inflate(R.layout.dialog_pattern, null) + view.apply { + pattern_lock_view.correctStateColor = activity.config.primaryColor + pattern_lock_view.normalStateColor = activity.config.textColor + pattern_lock_view.addPatternLockListener(object : PatternLockViewListener { + override fun onComplete(pattern: MutableList?) { + if (hash.isEmpty()) { + hash = PatternLockUtils.patternToSha1(pattern_lock_view, pattern) + pattern_lock_view.clearPattern() + pattern_dialog_title.setText(R.string.repeat_pattern) + } else { + val newHash = PatternLockUtils.patternToSha1(pattern_lock_view, pattern) + if (hash == newHash) { + pattern_lock_view.setViewMode(PatternLockView.PatternViewMode.CORRECT) + Handler().postDelayed({ + callback(hash) + dialog!!.dismiss() + }, 300) + } else { + pattern_lock_view.setViewMode(PatternLockView.PatternViewMode.WRONG) + activity.toast(R.string.wrong_pattern) + Handler().postDelayed({ + hash = "" + pattern_lock_view.clearPattern() + pattern_dialog_title.setText(R.string.select_pattern) + }, 1000) + } + } + } + + override fun onCleared() { + } + + override fun onStarted() { + } + + override fun onProgress(progressPattern: MutableList?) { + } + }) + } + + dialog = AlertDialog.Builder(activity) + .setNegativeButton(R.string.cancel, null) + .create().apply { + activity.setupDialogStuff(view, this) + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt index b1d5b3918..d02c78187 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt @@ -233,7 +233,11 @@ class Config(context: Context) : BaseConfig(context) { get() = prefs.getBoolean(REPLACE_SHARE_WITH_ROTATE, false) set(replaceShare) = prefs.edit().putBoolean(REPLACE_SHARE_WITH_ROTATE, replaceShare).apply() - var passwordProtection: Boolean - get() = prefs.getBoolean(PASSWORD_PROTECTION, false) - set(passwordProtection) = prefs.edit().putBoolean(PASSWORD_PROTECTION, passwordProtection).apply() + var isPasswordProtectionOn: Boolean + get() = prefs.getBoolean(IS_PASSWORD_PROTECTION, false) + set(passwordProtection) = prefs.edit().putBoolean(IS_PASSWORD_PROTECTION, passwordProtection).apply() + + var passwordHash: String + get() = prefs.getString(PASSWORD_HASH, "") + set(passwordHash) = prefs.edit().putString(PASSWORD_HASH, passwordHash).apply() } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt index a10f57105..01d829cc2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt @@ -33,7 +33,8 @@ val ALBUM_COVERS = "album_covers" val SCROLL_HORIZONTALLY = "scroll_horizontally" val HIDE_SYSTEM_UI = "hide_system_ui" val REPLACE_SHARE_WITH_ROTATE = "replace_share_with_rotate" -val PASSWORD_PROTECTION = "password_protection" +val IS_PASSWORD_PROTECTION = "password_protection" +val PASSWORD_HASH = "password_hash" val NOMEDIA = ".nomedia" diff --git a/app/src/main/res/layout/dialog_pattern.xml b/app/src/main/res/layout/dialog_pattern.xml new file mode 100644 index 000000000..78eeb54ce --- /dev/null +++ b/app/src/main/res/layout/dialog_pattern.xml @@ -0,0 +1,22 @@ + + + + + + + + diff --git a/app/src/main/res/values-cs/strings.xml b/app/src/main/res/values-cs/strings.xml index 7d591baf7..6f0be4434 100644 --- a/app/src/main/res/values-cs/strings.xml +++ b/app/src/main/res/values-cs/strings.xml @@ -81,9 +81,11 @@ PIN + Enter PIN Wrong PIN Repeat PIN Pattern + Select pattern Wrong pattern Repeat pattern diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 5c811111c..a289cc3a5 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -81,9 +81,11 @@ PIN + Enter PIN Wrong PIN Repeat PIN Pattern + Select pattern Wrong pattern Repeat pattern diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 018aace58..806e9932d 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -81,9 +81,11 @@ PIN + Enter PIN Wrong PIN Repeat PIN Pattern + Select pattern Wrong pattern Repeat pattern diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index a17ddc010..f54127ab1 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -81,9 +81,11 @@ PIN + Enter PIN Wrong PIN Repeat PIN Pattern + Select pattern Wrong pattern Repeat pattern diff --git a/app/src/main/res/values-hu/strings.xml b/app/src/main/res/values-hu/strings.xml index 6a0254652..495defe16 100644 --- a/app/src/main/res/values-hu/strings.xml +++ b/app/src/main/res/values-hu/strings.xml @@ -81,9 +81,11 @@ PIN + Enter PIN Wrong PIN Repeat PIN Pattern + Select pattern Wrong pattern Repeat pattern diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index e12dea886..8dbd7f1dd 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -81,9 +81,11 @@ PIN + Enter PIN Wrong PIN Repeat PIN Pattern + Select pattern Wrong pattern Repeat pattern diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index cebdc055b..043c46c0f 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -81,9 +81,11 @@ PIN + Enter PIN Wrong PIN Repeat PIN Pattern + Select pattern Wrong pattern Repeat pattern diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index 7875f20bc..bca2227cf 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -81,9 +81,11 @@ PIN + Enter PIN Wrong PIN Repeat PIN Pattern + Select pattern Wrong pattern Repeat pattern diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index ee583f55e..f9aa512a7 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -81,9 +81,11 @@ PIN + Enter PIN Wrong PIN Repeat PIN Pattern + Select pattern Wrong pattern Repeat pattern diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xml index 95995f1cf..78e929e07 100644 --- a/app/src/main/res/values-pt/strings.xml +++ b/app/src/main/res/values-pt/strings.xml @@ -81,9 +81,11 @@ PIN + Enter PIN Wrong PIN Repeat PIN Pattern + Select pattern Wrong pattern Repeat pattern diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index bf5131d04..60242d8a7 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -81,9 +81,11 @@ PIN + Enter PIN Wrong PIN Repeat PIN Pattern + Select pattern Wrong pattern Repeat pattern diff --git a/app/src/main/res/values-sk/strings.xml b/app/src/main/res/values-sk/strings.xml index c8c76fa94..a1311d47d 100644 --- a/app/src/main/res/values-sk/strings.xml +++ b/app/src/main/res/values-sk/strings.xml @@ -81,9 +81,11 @@ PIN + Zadajte PIN Nesprávny PIN Zopakujte PIN Vzor + Zvoľte vzor Nesprávny vzor Zopakujte vzor diff --git a/app/src/main/res/values-sv/strings.xml b/app/src/main/res/values-sv/strings.xml index f24744d90..2dfadcbeb 100644 --- a/app/src/main/res/values-sv/strings.xml +++ b/app/src/main/res/values-sv/strings.xml @@ -81,9 +81,11 @@ PIN + Enter PIN Wrong PIN Repeat PIN Pattern + Select pattern Wrong pattern Repeat pattern diff --git a/app/src/main/res/values-tr/strings.xml b/app/src/main/res/values-tr/strings.xml index fb11ac7d8..93638100d 100644 --- a/app/src/main/res/values-tr/strings.xml +++ b/app/src/main/res/values-tr/strings.xml @@ -81,9 +81,11 @@ PIN + Enter PIN Wrong PIN Repeat PIN Pattern + Select pattern Wrong pattern Repeat pattern diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index fffe77891..9bd074a5f 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -81,9 +81,11 @@ PIN + Enter PIN Wrong PIN Repeat PIN Pattern + Select pattern Wrong pattern Repeat pattern diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index ab2a0f4a5..ba3bf331e 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -81,9 +81,11 @@ PIN + Enter PIN Wrong PIN Repeat PIN Pattern + Select pattern Wrong pattern Repeat pattern diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 92dbcef9b..956c73e77 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -81,9 +81,11 @@ PIN + Enter PIN Wrong PIN Repeat PIN Pattern + Select pattern Wrong pattern Repeat pattern