Skip to content

Commit 3a07635

Browse files
alexandre-rouxAlexandre Roux
and
Alexandre Roux
authored
1.0.1 (#2)
* Bug fix when stopping a SolitariesLoop that hasn't been started * Bug fix: don't launch another animation if the current one is canceled * Add logs Co-authored-by: Alexandre Roux <aroux@softbankrobotics.com>
1 parent efa2921 commit 3a07635

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

animated-solitaries-root/solitaries-loop/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ android {
99
defaultConfig {
1010
minSdkVersion 23
1111
targetSdkVersion 29
12-
versionCode 1
13-
versionName "1.0.0"
12+
versionCode 2
13+
versionName "1.0.1"
1414

1515
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1616
consumerProguardFiles 'consumer-rules.pro'

animated-solitaries-root/solitaries-loop/src/main/java/com/softbankrobotics/dx/solitariesloop/SolitariesLoop.kt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.softbankrobotics.dx.solitariesloop
22

3+
import android.util.Log
34
import com.aldebaran.qi.Future
45
import com.aldebaran.qi.sdk.QiContext
56
import com.aldebaran.qi.sdk.builder.AnimateBuilder
@@ -14,6 +15,10 @@ import java.util.concurrent.TimeUnit
1415
*/
1516
class SolitariesLoop(private val qiContext: QiContext, private val delayInSeconds: Int = 60) {
1617

18+
companion object {
19+
private const val TAG = "SolitariesLoop"
20+
}
21+
1722
private val animationNames = arrayOf(
1823
"CheckLeft_01.qianim",
1924
"CheckRight_01.qianim",
@@ -39,7 +44,7 @@ class SolitariesLoop(private val qiContext: QiContext, private val delayInSecond
3944
private var animationNamesQueue = mutableListOf<String>()
4045
private var lastAnimationName = ""
4146

42-
private lateinit var animationFuture: Future<Void>
47+
private var animationFuture: Future<Void>? = null
4348

4449
private fun buildAndRunAnimate(): Future<Void> {
4550
return FutureUtils.wait(delayInSeconds.toLong(), TimeUnit.SECONDS)
@@ -58,7 +63,7 @@ class SolitariesLoop(private val qiContext: QiContext, private val delayInSecond
5863
animate.async().run()
5964
}
6065
.thenCompose {
61-
if (it.isCancelled) {
66+
if (!it.isCancelled) {
6267
buildAndRunAnimate()
6368
} else {
6469
FutureUtils.wait(0, TimeUnit.NANOSECONDS)
@@ -87,15 +92,21 @@ class SolitariesLoop(private val qiContext: QiContext, private val delayInSecond
8792
* Starts periodically playing random animations, until stopped.
8893
*/
8994
fun start() {
95+
Log.i(TAG, "SolitariesLoop starting")
9096
animationFuture = buildAndRunAnimate()
9197
}
9298

9399
/**
94100
* Stops the loop and cancels the current animation (if any); returns a future (that will finish
95101
* when the animation has effectively been stopped.
96102
*/
97-
fun stop(): Future<Void> {
98-
animationFuture.requestCancellation()
103+
fun stop(): Future<Void>? {
104+
Log.i(TAG, "SolitariesLoop stopping")
105+
if (animationFuture == null || animationFuture!!.isDone) {
106+
Log.e(TAG, "Error: trying to stop a SolitariesLoop that hasn't been started")
107+
} else {
108+
animationFuture!!.requestCancellation()
109+
}
99110
return animationFuture
100111
}
101112
}

0 commit comments

Comments
 (0)