From fbf60ac13e1ba1cbdeff03eb704cd6e2bbb231a4 Mon Sep 17 00:00:00 2001 From: William Brawner Date: Tue, 22 Sep 2020 17:57:04 -0700 Subject: [PATCH] Add support for ambient mode on wearable --- .idea/codeStyles/Project.xml | 123 ++++++++++++++++++ .../activetimer/ActiveTimerFragment.kt | 5 +- wear/build.gradle | 1 + .../wbrawner/trainterval/wear/MainActivity.kt | 87 ++++++++----- .../main/res/drawable/background_round.xml | 6 + .../res/drawable/background_round_normal.xml | 8 ++ .../res/drawable/background_round_pressed.xml | 8 ++ wear/src/main/res/drawable/ic_pause_inset.xml | 5 - wear/src/main/res/drawable/ic_play_inset.xml | 5 - wear/src/main/res/layout/activity_main.xml | 17 ++- wear/src/main/res/values/styles.xml | 2 - 11 files changed, 214 insertions(+), 53 deletions(-) create mode 100644 .idea/codeStyles/Project.xml create mode 100644 wear/src/main/res/drawable/background_round.xml create mode 100644 wear/src/main/res/drawable/background_round_normal.xml create mode 100644 wear/src/main/res/drawable/background_round_pressed.xml delete mode 100644 wear/src/main/res/drawable/ic_pause_inset.xml delete mode 100644 wear/src/main/res/drawable/ic_play_inset.xml diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..adef587 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,123 @@ + + + + + + + + + +
+ + + + xmlns:android + + ^$ + + + +
+
+ + + + xmlns:.* + + ^$ + + + BY_NAME + +
+
+ + + + .*:id + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + .*:name + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + name + + ^$ + + + +
+
+ + + + style + + ^$ + + + +
+
+ + + + .* + + ^$ + + + BY_NAME + +
+
+ + + + .* + + http://schemas.android.com/apk/res/android + + + ANDROID_ATTRIBUTE_ORDER + +
+
+ + + + .* + + .* + + + BY_NAME + +
+
+
+
+ + +
+
\ No newline at end of file diff --git a/app/src/main/java/com/wbrawner/trainterval/activetimer/ActiveTimerFragment.kt b/app/src/main/java/com/wbrawner/trainterval/activetimer/ActiveTimerFragment.kt index ac9c8f8..5cc62e3 100644 --- a/app/src/main/java/com/wbrawner/trainterval/activetimer/ActiveTimerFragment.kt +++ b/app/src/main/java/com/wbrawner/trainterval/activetimer/ActiveTimerFragment.kt @@ -18,8 +18,8 @@ import androidx.fragment.app.activityViewModels import androidx.lifecycle.Observer import androidx.lifecycle.viewModelScope import androidx.navigation.fragment.findNavController -import com.robinhood.ticker.TickerUtils import com.google.android.gms.wearable.* +import com.robinhood.ticker.TickerUtils import com.wbrawner.trainterval.Logger import com.wbrawner.trainterval.R import com.wbrawner.trainterval.shared.IntervalTimerDao @@ -92,7 +92,7 @@ class ActiveTimerFragment : Fragment(), MessageClient.OnMessageReceivedListener it.setSupportActionBar(toolbar) it.supportActionBar?.setDisplayHomeAsUpEnabled(true) } - timeRemaining.setCharacterLists(TickerUtils.provideNumberList() + ":") + timeRemaining.setCharacterLists(TickerUtils.provideNumberList()) timerSets.setCharacterLists(TickerUtils.provideNumberList()) timerRounds.setCharacterLists(TickerUtils.provideNumberList()) coroutineScope = CoroutineScope(Dispatchers.Main) @@ -137,7 +137,6 @@ class ActiveTimerFragment : Fragment(), MessageClient.OnMessageReceivedListener } (activity as? AppCompatActivity)?.supportActionBar?.title = state.timerName val backgroundColor = resources.getColor(state.phase.colorRes, context?.theme) - Log.d("ActiveTimerFragment", "State: $state") state.previousPhase?.let { val previousBackgroundColor = resources.getColor(it.colorRes, context?.theme) val colorAnimation = diff --git a/wear/build.gradle b/wear/build.gradle index e37e5bd..7b1cb8e 100644 --- a/wear/build.gradle +++ b/wear/build.gradle @@ -56,6 +56,7 @@ dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation 'androidx.wear:wear:1.0.0' implementation 'com.google.android.support:wearable:2.7.0' + implementation 'androidx.appcompat:appcompat:1.2.0' compileOnly 'com.google.android.wearable:wearable:2.7.0' testImplementation 'junit:junit:4.12' } \ No newline at end of file diff --git a/wear/src/main/java/com/wbrawner/trainterval/wear/MainActivity.kt b/wear/src/main/java/com/wbrawner/trainterval/wear/MainActivity.kt index 61b9c3f..70cee1d 100644 --- a/wear/src/main/java/com/wbrawner/trainterval/wear/MainActivity.kt +++ b/wear/src/main/java/com/wbrawner/trainterval/wear/MainActivity.kt @@ -1,12 +1,15 @@ package com.wbrawner.trainterval.wear import android.content.Context +import android.graphics.Color import android.os.Bundle import android.os.VibrationEffect import android.os.Vibrator -import android.support.wearable.activity.WearableActivity import android.util.Log import android.view.View +import androidx.core.content.ContextCompat +import androidx.fragment.app.FragmentActivity +import androidx.wear.ambient.AmbientModeSupport import com.google.android.gms.wearable.* import com.wbrawner.trainterval.R import com.wbrawner.trainterval.shared.IntervalTimerState @@ -15,12 +18,16 @@ import com.wbrawner.trainterval.shared.IntervalTimerState.Companion.TIMER_STATE import com.wbrawner.trainterval.shared.toIntervalTimerState import kotlinx.android.synthetic.main.activity_main.* -class MainActivity : WearableActivity(), DataClient.OnDataChangedListener { +class MainActivity : FragmentActivity(), + AmbientModeSupport.AmbientCallbackProvider, + DataClient.OnDataChangedListener { private lateinit var dataClient: DataClient private lateinit var messageClient: MessageClient private lateinit var nodeClient: NodeClient private lateinit var vibrator: Vibrator + private lateinit var ambientController: AmbientModeSupport.AmbientController + private var lastState: IntervalTimerState? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -29,7 +36,7 @@ class MainActivity : WearableActivity(), DataClient.OnDataChangedListener { messageClient = Wearable.getMessageClient(this) nodeClient = Wearable.getNodeClient(this) vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator - setAmbientEnabled() + ambientController = AmbientModeSupport.attach(this) } override fun onResume() { @@ -49,30 +56,48 @@ class MainActivity : WearableActivity(), DataClient.OnDataChangedListener { .dataMap .toIntervalTimerState() ?: return@forEach - when (intervalTimerState) { - is IntervalTimerState.LoadingState -> timeRemaining.text = "Loading" - is IntervalTimerState.TimerRunningState -> { - val backgroundColor = - resources.getColor(intervalTimerState.phase.colorRes, theme) - timerRoot.setBackgroundColor(backgroundColor) - timeRemaining.text = intervalTimerState.timeRemaining + lastState = intervalTimerState + renderState() + } + } + + private fun renderState() { + val intervalTimerState = lastState ?: return + when (intervalTimerState) { + is IntervalTimerState.LoadingState -> timeRemaining.text = "Loading" + is IntervalTimerState.TimerRunningState -> { + val backgroundColor = if (ambientController.isAmbient) Color.BLACK + else resources.getColor(intervalTimerState.phase.colorRes, theme) + timerRoot.setBackgroundColor(backgroundColor) + timeRemaining.text = intervalTimerState.timeRemaining + val textColor = if (ambientController.isAmbient) resources.getColor( + intervalTimerState.phase.colorRes, + theme + ) + else Color.BLACK + timeRemaining.setTextColor(textColor) + if (ambientController.isAmbient) { + toggleButton.visibility = View.GONE + } else { + toggleButton.visibility = View.VISIBLE toggleButton.setImageDrawable( - getDrawable( - if (intervalTimerState.isRunning) R.drawable.ic_pause_inset - else R.drawable.ic_play_inset + ContextCompat.getDrawable( + this, + if (intervalTimerState.isRunning) R.drawable.ic_pause + else R.drawable.ic_play_arrow + ) + ) + } + if (intervalTimerState.vibrate) { + vibrator.vibrate( + VibrationEffect.createWaveform( + longArrayOf(0L, 100L, 50L, 100L), + -1 ) ) - if (intervalTimerState.vibrate) { - vibrator.vibrate( - VibrationEffect.createWaveform( - longArrayOf(0L, 100L, 50L, 100L), - -1 - ) - ) - } } - is IntervalTimerState.ExitState -> timeRemaining.text = "Exit" } + is IntervalTimerState.ExitState -> timeRemaining.text = "Exit" } } @@ -91,12 +116,16 @@ class MainActivity : WearableActivity(), DataClient.OnDataChangedListener { } } - override fun onEnterAmbient(ambientDetails: Bundle?) { - super.onEnterAmbient(ambientDetails) - } + override fun getAmbientCallback(): AmbientModeSupport.AmbientCallback = + object : AmbientModeSupport.AmbientCallback() { + override fun onEnterAmbient(ambientDetails: Bundle?) { + super.onEnterAmbient(ambientDetails) + renderState() + } - override fun onExitAmbient() { - super.onExitAmbient() - - } + override fun onExitAmbient() { + super.onExitAmbient() + renderState() + } + } } \ No newline at end of file diff --git a/wear/src/main/res/drawable/background_round.xml b/wear/src/main/res/drawable/background_round.xml new file mode 100644 index 0000000..8dc8f0f --- /dev/null +++ b/wear/src/main/res/drawable/background_round.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/wear/src/main/res/drawable/background_round_normal.xml b/wear/src/main/res/drawable/background_round_normal.xml new file mode 100644 index 0000000..70d443a --- /dev/null +++ b/wear/src/main/res/drawable/background_round_normal.xml @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file diff --git a/wear/src/main/res/drawable/background_round_pressed.xml b/wear/src/main/res/drawable/background_round_pressed.xml new file mode 100644 index 0000000..ebd5597 --- /dev/null +++ b/wear/src/main/res/drawable/background_round_pressed.xml @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file diff --git a/wear/src/main/res/drawable/ic_pause_inset.xml b/wear/src/main/res/drawable/ic_pause_inset.xml deleted file mode 100644 index 185bf9e..0000000 --- a/wear/src/main/res/drawable/ic_pause_inset.xml +++ /dev/null @@ -1,5 +0,0 @@ - - \ No newline at end of file diff --git a/wear/src/main/res/drawable/ic_play_inset.xml b/wear/src/main/res/drawable/ic_play_inset.xml deleted file mode 100644 index 1280cef..0000000 --- a/wear/src/main/res/drawable/ic_play_inset.xml +++ /dev/null @@ -1,5 +0,0 @@ - - \ No newline at end of file diff --git a/wear/src/main/res/layout/activity_main.xml b/wear/src/main/res/layout/activity_main.xml index 41ee873..bcd9174 100644 --- a/wear/src/main/res/layout/activity_main.xml +++ b/wear/src/main/res/layout/activity_main.xml @@ -1,19 +1,19 @@ - + android:background="@color/colorSurface" + android:gravity="center" + android:orientation="vertical"> - \ No newline at end of file + \ No newline at end of file diff --git a/wear/src/main/res/values/styles.xml b/wear/src/main/res/values/styles.xml index af00c6b..9025d59 100644 --- a/wear/src/main/res/values/styles.xml +++ b/wear/src/main/res/values/styles.xml @@ -1,5 +1,3 @@ -