From 02ee4dcc037e9ffda04c7b20ab8222b8372fdb1a Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 30 Jul 2017 23:07:53 +0200 Subject: [PATCH] create a viewpager for switching between pattern/pin protection --- .../gallery/adapters/PasswordTypesAdapter.kt | 33 ++++++++ .../gallery/dialogs/PatternDialog.kt | 60 +++------------ .../gallery/views/MyDialogViewPager.kt | 42 ++++++++++ .../gallery/views/PatternTab.kt | 76 +++++++++++++++++++ .../simplemobiletools/gallery/views/PinTab.kt | 11 +++ app/src/main/res/layout/dialog_pattern.xml | 39 ++++++---- app/src/main/res/layout/tab_pattern.xml | 22 ++++++ app/src/main/res/layout/tab_pin.xml | 8 ++ 8 files changed, 227 insertions(+), 64 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/gallery/adapters/PasswordTypesAdapter.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/gallery/views/MyDialogViewPager.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/gallery/views/PatternTab.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/gallery/views/PinTab.kt create mode 100644 app/src/main/res/layout/tab_pattern.xml create mode 100644 app/src/main/res/layout/tab_pin.xml diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/PasswordTypesAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/PasswordTypesAdapter.kt new file mode 100644 index 000000000..311c14815 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/PasswordTypesAdapter.kt @@ -0,0 +1,33 @@ +package com.simplemobiletools.gallery.adapters + +import android.content.Context +import android.support.v4.view.PagerAdapter +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import com.simplemobiletools.gallery.R +import com.simplemobiletools.gallery.views.PatternTab + +class PasswordTypesAdapter(val context: Context, val requiredHash: String, val hashListener: PatternTab.HashListener) : PagerAdapter() { + + override fun instantiateItem(container: ViewGroup, position: Int): Any { + val view = LayoutInflater.from(context).inflate(layoutSelection(position), container, false) + container.addView(view) + if (position == 0) + (view as PatternTab).initTab(requiredHash, hashListener) + return view + } + + override fun destroyItem(container: ViewGroup, position: Int, item: Any) { + container.removeView(item as View) + } + + override fun getCount() = 2 + override fun isViewFromObject(view: View, item: Any) = view == item + + private fun layoutSelection(position: Int): Int = when (position) { + 0 -> R.layout.tab_pattern + 1 -> R.layout.tab_pin + else -> throw RuntimeException("Only 2 tabs allowed") + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PatternDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PatternDialog.kt index 8a0e45296..8a909c945 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PatternDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PatternDialog.kt @@ -1,42 +1,27 @@ 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.adapters.PasswordTypesAdapter import com.simplemobiletools.gallery.extensions.config +import com.simplemobiletools.gallery.views.MyDialogViewPager +import com.simplemobiletools.gallery.views.PatternTab import kotlinx.android.synthetic.main.dialog_pattern.view.* -class PatternDialog(val activity: SimpleActivity, val requiredHash: String, val callback: (hash: String) -> Unit) { +class PatternDialog(val activity: SimpleActivity, val requiredHash: String, val callback: (hash: String) -> Unit) : PatternTab.HashListener { var dialog: AlertDialog? = null val view = LayoutInflater.from(activity).inflate(R.layout.dialog_pattern, null) - var hash = requiredHash - init { 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?) { - receivedHash(PatternLockUtils.patternToSha1(pattern_lock_view, pattern)) - } + val textColor = context.config.textColor + dialog_tab_layout.setTabTextColors(textColor, textColor) - override fun onCleared() { - } - - override fun onStarted() { - } - - override fun onProgress(progressPattern: MutableList?) { - } - }) + val viewPager = findViewById(R.id.dialog_tab_view_pager) as MyDialogViewPager + viewPager.adapter = PasswordTypesAdapter(context, requiredHash, this@PatternDialog) } dialog = AlertDialog.Builder(activity) @@ -46,31 +31,8 @@ class PatternDialog(val activity: SimpleActivity, val requiredHash: String, val } } - private fun receivedHash(newHash: String) { - view.apply { - if (hash.isEmpty()) { - hash = newHash - pattern_lock_view.clearPattern() - pattern_dialog_title.setText(R.string.repeat_pattern) - } else { - 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({ - pattern_lock_view.clearPattern() - if (requiredHash.isEmpty()) { - hash = "" - pattern_dialog_title.setText(R.string.insert_pattern) - } - }, 1000) - } - } - } + override fun receivedHash(hash: String) { + callback(hash) + dialog!!.dismiss() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/views/MyDialogViewPager.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/views/MyDialogViewPager.kt new file mode 100644 index 000000000..e2f820dfe --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/views/MyDialogViewPager.kt @@ -0,0 +1,42 @@ +package com.simplemobiletools.gallery.views + +import android.content.Context +import android.support.v4.view.ViewPager +import android.util.AttributeSet +import android.view.MotionEvent +import android.view.View + +class MyDialogViewPager : ViewPager { + + constructor(context: Context) : super(context) + + constructor(context: Context, attrs: AttributeSet) : super(context, attrs) + + // disable manual swiping of viewpager at the dialog + override fun onInterceptTouchEvent(ev: MotionEvent): Boolean { + return false + } + + override fun onTouchEvent(ev: MotionEvent): Boolean { + try { + return super.onTouchEvent(ev) + } catch (ignored: Exception) { + } + + return false + } + + // https://stackoverflow.com/a/20784791/1967672 + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { + var height = 0 + for (i in 0..childCount - 1) { + val child = getChildAt(i) + child.measure(widthMeasureSpec, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)) + val h = child.measuredHeight + if (h > height) height = h + } + + val newHeightMeasureSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY) + super.onMeasure(widthMeasureSpec, newHeightMeasureSpec) + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/views/PatternTab.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/views/PatternTab.kt new file mode 100644 index 000000000..5aada4281 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/views/PatternTab.kt @@ -0,0 +1,76 @@ +package com.simplemobiletools.gallery.views + +import android.content.Context +import android.os.Handler +import android.util.AttributeSet +import android.widget.RelativeLayout +import com.andrognito.patternlockview.PatternLockView +import com.andrognito.patternlockview.listener.PatternLockViewListener +import com.andrognito.patternlockview.utils.PatternLockUtils +import com.simplemobiletools.commons.extensions.toast +import com.simplemobiletools.gallery.R +import com.simplemobiletools.gallery.extensions.config +import kotlinx.android.synthetic.main.tab_pattern.view.* + +class PatternTab(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs) { + var hash = "" + var requiredHash = "" + lateinit var hashListener: HashListener + + override fun onFinishInflate() { + super.onFinishInflate() + val textColor = context.config.textColor + pattern_lock_title.setTextColor(textColor) + pattern_lock_view.correctStateColor = context.config.primaryColor + pattern_lock_view.normalStateColor = textColor + pattern_lock_view.addPatternLockListener(object : PatternLockViewListener { + override fun onComplete(pattern: MutableList?) { + receivedHash(PatternLockUtils.patternToSha1(pattern_lock_view, pattern)) + } + + override fun onCleared() { + } + + override fun onStarted() { + } + + override fun onProgress(progressPattern: MutableList?) { + } + }) + } + + fun initTab(requiredHash: String, listener: HashListener) { + this.requiredHash = requiredHash + hash = requiredHash + hashListener = listener + } + + 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) + }, 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) + } + } + } + + interface HashListener { + fun receivedHash(hash: String) + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/views/PinTab.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/views/PinTab.kt new file mode 100644 index 000000000..0af058ebc --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/views/PinTab.kt @@ -0,0 +1,11 @@ +package com.simplemobiletools.gallery.views + +import android.content.Context +import android.util.AttributeSet +import android.widget.RelativeLayout + +class PinTab(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs) { + override fun onFinishInflate() { + super.onFinishInflate() + } +} diff --git a/app/src/main/res/layout/dialog_pattern.xml b/app/src/main/res/layout/dialog_pattern.xml index e900eb2f9..5cebd31ea 100644 --- a/app/src/main/res/layout/dialog_pattern.xml +++ b/app/src/main/res/layout/dialog_pattern.xml @@ -1,22 +1,31 @@ + android:id="@+id/dialog_holder" + android:layout_width="wrap_content" + android:layout_height="wrap_content"> - + + + + + + + + - - + android:layout_below="@+id/dialog_tab_layout"/> diff --git a/app/src/main/res/layout/tab_pattern.xml b/app/src/main/res/layout/tab_pattern.xml new file mode 100644 index 000000000..13cae2fcb --- /dev/null +++ b/app/src/main/res/layout/tab_pattern.xml @@ -0,0 +1,22 @@ + + + + + + + + diff --git a/app/src/main/res/layout/tab_pin.xml b/app/src/main/res/layout/tab_pin.xml new file mode 100644 index 000000000..33f0f93a1 --- /dev/null +++ b/app/src/main/res/layout/tab_pin.xml @@ -0,0 +1,8 @@ + + + +