From 15d282a1757d44ef71d914c37946941e226ebb38 Mon Sep 17 00:00:00 2001 From: dankl Date: Fri, 3 Jan 2020 18:05:09 +0100 Subject: [PATCH] Implemented pausing flicker code --- .../ui/views/ChipTanFlickerCodeView.kt | 25 +++++++++++++++++-- .../android/util/FlickerCodeAnimator.kt | 23 +++++++++++++---- .../src/main/res/layout/view_flicker_code.xml | 20 +++++++++++---- 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/views/ChipTanFlickerCodeView.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/views/ChipTanFlickerCodeView.kt index 17265df9..be2bd3df 100644 --- a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/views/ChipTanFlickerCodeView.kt +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/views/ChipTanFlickerCodeView.kt @@ -4,6 +4,7 @@ import android.content.Context import android.util.AttributeSet import android.view.LayoutInflater import android.view.View +import android.widget.ImageButton import android.widget.LinearLayout import kotlinx.android.synthetic.main.view_flicker_code.view.* import kotlinx.android.synthetic.main.view_tan_image_size_controls.view.* @@ -35,10 +36,12 @@ open class ChipTanFlickerCodeView @JvmOverloads constructor( protected lateinit var stripe4: ChipTanFlickerCodeStripeView protected lateinit var stripe5: ChipTanFlickerCodeStripeView + protected lateinit var allStripes: List + protected lateinit var tanGeneratorLeftMarker: View protected lateinit var tanGeneratorRightMarker: View - protected lateinit var allStripes: List + protected lateinit var btnPauseFlickerCode: ImageButton protected val animator = FlickerCodeAnimator() @@ -49,6 +52,8 @@ open class ChipTanFlickerCodeView @JvmOverloads constructor( protected var currentFrequency = 30 + protected var isFlickerCodePaused = false + init { setupUi(context) @@ -64,6 +69,9 @@ open class ChipTanFlickerCodeView @JvmOverloads constructor( rootView.btnIncreaseSpeed.setOnClickListener { increaseFrequency() } rootView.btnDecreaseSpeed.setOnClickListener { decreaseFrequency() } + btnPauseFlickerCode = rootView.btnPauseFlickerCode + btnPauseFlickerCode.setOnClickListener { togglePauseFlickerCode() } + stripe1 = rootView.findViewById(R.id.flickerCodeStripe1) stripe2 = rootView.findViewById(R.id.flickerCodeStripe2) stripe3 = rootView.findViewById(R.id.flickerCodeStripe3) @@ -83,7 +91,6 @@ open class ChipTanFlickerCodeView @JvmOverloads constructor( } - override fun onDetachedFromWindow() { animator.stop() @@ -160,6 +167,20 @@ open class ChipTanFlickerCodeView @JvmOverloads constructor( } + open fun togglePauseFlickerCode() { + if (isFlickerCodePaused == false) { + animator.pause() + btnPauseFlickerCode.setImageResource(android.R.drawable.ic_media_play) + } + else { + animator.resume() + btnPauseFlickerCode.setImageResource(android.R.drawable.ic_media_pause) + } + + isFlickerCodePaused = !!! isFlickerCodePaused + } + + open fun setCode(flickerCode: FlickerCode) { animator.stop() diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/util/FlickerCodeAnimator.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/util/FlickerCodeAnimator.kt index 03980dba..489a4313 100644 --- a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/util/FlickerCodeAnimator.kt +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/util/FlickerCodeAnimator.kt @@ -22,6 +22,9 @@ open class FlickerCodeAnimator { // TODO: move to fints4javaLib protected var currentStepIndex = 0 + @Volatile + protected var isPaused = false + protected var calculateAnimationThread: Thread? = null @@ -41,13 +44,15 @@ open class FlickerCodeAnimator { // TODO: move to fints4javaLib protected open fun calculateAnimation(steps: List>, showStep: (Array) -> Unit) { while (Thread.currentThread().isInterrupted == false) { - val nextStep = steps[currentStepIndex] + if (isPaused == false) { + val nextStep = steps[currentStepIndex] - showStep(nextStep) + showStep(nextStep) - currentStepIndex++ - if (currentStepIndex >= steps.size) { - currentStepIndex = 0 // all steps shown, start again from beginning + currentStepIndex++ + if (currentStepIndex >= steps.size) { + currentStepIndex = 0 // all steps shown, start again from beginning + } } try { @@ -58,6 +63,14 @@ open class FlickerCodeAnimator { // TODO: move to fints4javaLib } } + open fun pause() { + this.isPaused = true + } + + open fun resume() { + this.isPaused = false + } + open fun stop() { try { if (calculateAnimationThread?.isInterrupted == false) { diff --git a/fints4javaAndroidApp/src/main/res/layout/view_flicker_code.xml b/fints4javaAndroidApp/src/main/res/layout/view_flicker_code.xml index 2e293615..6b77b2d8 100644 --- a/fints4javaAndroidApp/src/main/res/layout/view_flicker_code.xml +++ b/fints4javaAndroidApp/src/main/res/layout/view_flicker_code.xml @@ -12,35 +12,45 @@ android:layout_height="@dimen/view_tan_image_controls_buttons_height" android:layout_gravity="center_horizontal" android:gravity="center_horizontal" - > + > + android:layout_marginEnd="@dimen/view_flicker_code_controls_sections_space" + /> + />