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.util.AttributeSet
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.widget.ImageButton
import android.widget.LinearLayout import android.widget.LinearLayout
import kotlinx.android.synthetic.main.view_flicker_code.view.* import kotlinx.android.synthetic.main.view_flicker_code.view.*
import kotlinx.android.synthetic.main.view_tan_image_size_controls.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 stripe4: ChipTanFlickerCodeStripeView
protected lateinit var stripe5: ChipTanFlickerCodeStripeView protected lateinit var stripe5: ChipTanFlickerCodeStripeView
protected lateinit var allStripes: List<ChipTanFlickerCodeStripeView>
protected lateinit var tanGeneratorLeftMarker: View protected lateinit var tanGeneratorLeftMarker: View
protected lateinit var tanGeneratorRightMarker: View protected lateinit var tanGeneratorRightMarker: View
protected lateinit var allStripes: List<ChipTanFlickerCodeStripeView> protected lateinit var btnPauseFlickerCode: ImageButton
protected val animator = FlickerCodeAnimator() protected val animator = FlickerCodeAnimator()
@ -49,6 +52,8 @@ open class ChipTanFlickerCodeView @JvmOverloads constructor(
protected var currentFrequency = 30 protected var currentFrequency = 30
protected var isFlickerCodePaused = false
init { init {
setupUi(context) setupUi(context)
@ -64,6 +69,9 @@ open class ChipTanFlickerCodeView @JvmOverloads constructor(
rootView.btnIncreaseSpeed.setOnClickListener { increaseFrequency() } rootView.btnIncreaseSpeed.setOnClickListener { increaseFrequency() }
rootView.btnDecreaseSpeed.setOnClickListener { decreaseFrequency() } rootView.btnDecreaseSpeed.setOnClickListener { decreaseFrequency() }
btnPauseFlickerCode = rootView.btnPauseFlickerCode
btnPauseFlickerCode.setOnClickListener { togglePauseFlickerCode() }
stripe1 = rootView.findViewById(R.id.flickerCodeStripe1) stripe1 = rootView.findViewById(R.id.flickerCodeStripe1)
stripe2 = rootView.findViewById(R.id.flickerCodeStripe2) stripe2 = rootView.findViewById(R.id.flickerCodeStripe2)
stripe3 = rootView.findViewById(R.id.flickerCodeStripe3) stripe3 = rootView.findViewById(R.id.flickerCodeStripe3)
@ -83,7 +91,6 @@ open class ChipTanFlickerCodeView @JvmOverloads constructor(
} }
override fun onDetachedFromWindow() { override fun onDetachedFromWindow() {
animator.stop() 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) { open fun setCode(flickerCode: FlickerCode) {
animator.stop() animator.stop()

View File

@ -22,6 +22,9 @@ open class FlickerCodeAnimator { // TODO: move to fints4javaLib
protected var currentStepIndex = 0 protected var currentStepIndex = 0
@Volatile
protected var isPaused = false
protected var calculateAnimationThread: Thread? = null protected var calculateAnimationThread: Thread? = null
@ -41,6 +44,7 @@ open class FlickerCodeAnimator { // TODO: move to fints4javaLib
protected open fun calculateAnimation(steps: List<Array<Bit>>, showStep: (Array<Bit>) -> Unit) { protected open fun calculateAnimation(steps: List<Array<Bit>>, showStep: (Array<Bit>) -> Unit) {
while (Thread.currentThread().isInterrupted == false) { while (Thread.currentThread().isInterrupted == false) {
if (isPaused == false) {
val nextStep = steps[currentStepIndex] val nextStep = steps[currentStepIndex]
showStep(nextStep) showStep(nextStep)
@ -49,6 +53,7 @@ open class FlickerCodeAnimator { // TODO: move to fints4javaLib
if (currentStepIndex >= steps.size) { if (currentStepIndex >= steps.size) {
currentStepIndex = 0 // all steps shown, start again from beginning currentStepIndex = 0 // all steps shown, start again from beginning
} }
}
try { try {
TimeUnit.MILLISECONDS.sleep(1000L / currentFrequency) TimeUnit.MILLISECONDS.sleep(1000L / currentFrequency)
@ -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() { open fun stop() {
try { try {
if (calculateAnimationThread?.isInterrupted == false) { if (calculateAnimationThread?.isInterrupted == false) {

View File

@ -19,7 +19,8 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:gravity="center_vertical" android:gravity="center_vertical"
android:layout_marginRight="@dimen/view_flicker_code_controls_sections_space" 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 <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -42,6 +43,15 @@
android:text="@string/view_flicker_code_decrease_frequency" 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> </LinearLayout>
<LinearLayout <LinearLayout