From 6e1f56f86496a36105e69bd1062fbcabbf8ffb28 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 2 Aug 2017 22:56:26 +0200 Subject: [PATCH] do not allow swiping the protection viewpager when a password is set --- .../gallery/activities/SettingsActivity.kt | 5 +- .../gallery/dialogs/SecurityDialog.kt | 48 +++++++++++-------- .../gallery/extensions/activity.kt | 2 +- .../gallery/helpers/Constants.kt | 10 ++-- .../gallery/views/MyDialogViewPager.kt | 6 ++- 5 files changed, 44 insertions(+), 27 deletions(-) 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 c7da656d8..5b32968ec 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SettingsActivity.kt @@ -155,12 +155,13 @@ class SettingsActivity : SimpleActivity() { private fun setupPasswordProtection() { settings_password_protection.isChecked = config.isPasswordProtectionOn settings_password_protection_holder.setOnClickListener { - SecurityDialog(this, config.passwordHash) { hash, type -> + val tabToShow = if (config.isPasswordProtectionOn) config.protectionType else SHOW_ALL_TABS + SecurityDialog(this, config.passwordHash, tabToShow) { hash, type -> val hasPasswordProtection = config.isPasswordProtectionOn settings_password_protection.isChecked = !hasPasswordProtection config.isPasswordProtectionOn = !hasPasswordProtection config.passwordHash = if (hasPasswordProtection) "" else hash - config.protectionType = PROTECTION_PATTERN + config.protectionType = type } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/SecurityDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/SecurityDialog.kt index f6a6a6b15..c732125ee 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/SecurityDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/SecurityDialog.kt @@ -4,41 +4,24 @@ import android.support.design.widget.TabLayout import android.support.v4.view.ViewPager import android.support.v7.app.AlertDialog import android.view.LayoutInflater +import com.simplemobiletools.commons.extensions.beGone import com.simplemobiletools.commons.extensions.setupDialogStuff import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.activities.SimpleActivity import com.simplemobiletools.gallery.adapters.PasswordTypesAdapter import com.simplemobiletools.gallery.extensions.config +import com.simplemobiletools.gallery.helpers.SHOW_ALL_TABS import com.simplemobiletools.gallery.interfaces.HashListener import com.simplemobiletools.gallery.views.MyDialogViewPager import kotlinx.android.synthetic.main.dialog_security.view.* -class SecurityDialog(val activity: SimpleActivity, val requiredHash: String, val callback: (hash: String, type: Int) -> Unit) : HashListener { +class SecurityDialog(val activity: SimpleActivity, 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) init { view.apply { val viewPager = findViewById(R.id.dialog_tab_view_pager) as MyDialogViewPager - val textColor = context.config.textColor - dialog_tab_layout.setTabTextColors(textColor, textColor) - dialog_tab_layout.setSelectedTabIndicatorColor(context.config.primaryColor) - dialog_tab_layout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener { - override fun onTabReselected(tab: TabLayout.Tab?) { - } - - override fun onTabUnselected(tab: TabLayout.Tab) { - } - - override fun onTabSelected(tab: TabLayout.Tab) { - if (tab.text.toString().equals(resources.getString(R.string.pattern), true)) { - viewPager.currentItem = 0 - } else { - viewPager.currentItem = 1 - } - } - }) - viewPager.adapter = PasswordTypesAdapter(context, requiredHash, this@SecurityDialog) viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener { override fun onPageScrollStateChanged(state: Int) { @@ -51,6 +34,31 @@ class SecurityDialog(val activity: SimpleActivity, val requiredHash: String, val dialog_tab_layout.getTabAt(position)!!.select() } }) + + if (showTabIndex == SHOW_ALL_TABS) { + val textColor = context.config.textColor + dialog_tab_layout.setTabTextColors(textColor, textColor) + dialog_tab_layout.setSelectedTabIndicatorColor(context.config.primaryColor) + dialog_tab_layout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener { + override fun onTabReselected(tab: TabLayout.Tab?) { + } + + override fun onTabUnselected(tab: TabLayout.Tab) { + } + + override fun onTabSelected(tab: TabLayout.Tab) { + if (tab.text.toString().equals(resources.getString(R.string.pattern), true)) { + viewPager.currentItem = 0 + } else { + viewPager.currentItem = 1 + } + } + }) + } else { + dialog_tab_layout.beGone() + viewPager.currentItem = showTabIndex + viewPager.allowSwiping = false + } } dialog = AlertDialog.Builder(activity) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt index cc914d82e..00af3ac71 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt @@ -335,7 +335,7 @@ fun Activity.loadStaticGif(path: String, target: MySquareImageView) { fun SimpleActivity.handleHiddenFolderPasswordProtection(callback: () -> Unit) { if (config.isPasswordProtectionOn) { - SecurityDialog(this, config.passwordHash) { hash, type -> + SecurityDialog(this, config.passwordHash, config.protectionType) { hash, type -> callback() } } else { 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 0dbfbbf1f..5b946fd5d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt @@ -67,6 +67,10 @@ val ORIENT_LANDSCAPE_RIGHT = 2 val ORIENT_UPSIDE_DOWN = 3 // security -val PROTECTION_PATTERN = 1 -val PROTECTION_PIN = 2 -val PROTECTION_FINGERPRINT = 3 +val PROTECTION_PATTERN = 0 +val PROTECTION_PIN = 1 +val PROTECTION_FINGERPRINT = 2 + +val SHOW_ALL_TABS = -1 +val SHOW_PATTERN = 0 +val SHOW_PIN = 1 diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/views/MyDialogViewPager.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/views/MyDialogViewPager.kt index e2f820dfe..5268e3a07 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/views/MyDialogViewPager.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/views/MyDialogViewPager.kt @@ -7,17 +7,21 @@ import android.view.MotionEvent import android.view.View class MyDialogViewPager : ViewPager { + var allowSwiping = true constructor(context: Context) : super(context) constructor(context: Context, attrs: AttributeSet) : super(context, attrs) - // disable manual swiping of viewpager at the dialog + // disable manual swiping of viewpager at the dialog by swiping over the pattern override fun onInterceptTouchEvent(ev: MotionEvent): Boolean { return false } override fun onTouchEvent(ev: MotionEvent): Boolean { + if (!allowSwiping) + return false + try { return super.onTouchEvent(ev) } catch (ignored: Exception) {