Implemented pausing flicker code

This commit is contained in:
dankl 2020-01-03 18:05:09 +01:00 committed by dankito
parent 87985469bf
commit 15d282a175
3 changed files with 56 additions and 12 deletions

View File

@ -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<ChipTanFlickerCodeStripeView>
protected lateinit var tanGeneratorLeftMarker: View
protected lateinit var tanGeneratorRightMarker: View
protected lateinit var allStripes: List<ChipTanFlickerCodeStripeView>
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()

View File

@ -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<Array<Bit>>, showStep: (Array<Bit>) -> 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) {

View File

@ -12,35 +12,45 @@
android:layout_height="@dimen/view_tan_image_controls_buttons_height"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
>
>
<net.dankito.banking.fints4java.android.ui.views.TanImageSizeControlsView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_marginRight="@dimen/view_flicker_code_controls_sections_space"
android:layout_marginEnd="@dimen/view_flicker_code_controls_sections_space"/>
android:layout_marginEnd="@dimen/view_flicker_code_controls_sections_space"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="@string/view_flicker_code_frequency"
/>
/>
<Button
android:id="@+id/btnIncreaseSpeed"
android:layout_width="@dimen/view_tan_image_controls_buttons_width"
android:layout_height="@dimen/view_tan_image_controls_buttons_height"
android:text="@string/view_flicker_code_increase_frequency"
/>
/>
<Button
android:id="@+id/btnDecreaseSpeed"
android:layout_width="@dimen/view_tan_image_controls_buttons_width"
android:layout_height="@dimen/view_tan_image_controls_buttons_height"
android:text="@string/view_flicker_code_decrease_frequency"
/>
/>
<ImageButton
android:id="@+id/btnPauseFlickerCode"
android:layout_width="@dimen/view_tan_image_controls_buttons_width"
android:layout_height="@dimen/view_tan_image_controls_buttons_height"
android:layout_marginLeft="@dimen/view_flicker_code_controls_sections_space"
android:layout_marginStart="@dimen/view_flicker_code_controls_sections_space"
android:src="@android:drawable/ic_media_pause"
/>
</LinearLayout>