Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HR and Step with third party watchfaces #3398

Merged
merged 3 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ import app.aaps.core.interfaces.rx.bus.RxBus
import app.aaps.core.interfaces.rx.events.EventWearDataToMobile
import app.aaps.core.interfaces.rx.events.EventWearToMobile
import app.aaps.core.interfaces.rx.weardata.EventData
import app.aaps.core.interfaces.rx.weardata.EventData.ActionResendData
import app.aaps.core.interfaces.sharedPreferences.SP
import app.aaps.shared.impl.weardata.ZipWatchfaceFormat
import app.aaps.wear.R
import app.aaps.wear.events.EventWearPreferenceChange
import app.aaps.wear.heartrate.HeartRateListener
import app.aaps.wear.interaction.ConfigurationActivity
import app.aaps.wear.interaction.utils.Persistence
import app.aaps.wear.wearStepCount.StepCountListener
import com.google.android.gms.tasks.Tasks
import com.google.android.gms.wearable.CapabilityClient
import com.google.android.gms.wearable.CapabilityInfo
Expand Down Expand Up @@ -61,6 +65,8 @@ class DataLayerListenerServiceWear : WearableListenerService() {
private var handler = Handler(HandlerThread(this::class.simpleName + "Handler").also { it.start() }.looper)

private val disposable = CompositeDisposable()
private var heartRateListener: HeartRateListener? = null
private var stepCountListener: StepCountListener? = null

private val rxPath get() = getString(app.aaps.core.interfaces.R.string.path_rx_bridge)
private val rxDataPath get() = getString(app.aaps.core.interfaces.R.string.path_rx_data_bridge)
Expand All @@ -83,6 +89,16 @@ class DataLayerListenerServiceWear : WearableListenerService() {
.subscribe {
sendMessage(rxDataPath, it.payload.serializeByte())
}
disposable += rxBus
.toObservable(EventWearPreferenceChange::class.java)
.observeOn(aapsSchedulers.main)
.subscribe { event: EventWearPreferenceChange ->
if (event.changedKey == getString(R.string.key_heart_rate_sampling)) updateHeartRateListener()
if (event.changedKey == getString(R.string.key_steps_sampling)) updatestepsCountListener()
}

updateHeartRateListener()
updatestepsCountListener()
}

override fun onCapabilityChanged(p0: CapabilityInfo) {
Expand Down Expand Up @@ -174,6 +190,37 @@ class DataLayerListenerServiceWear : WearableListenerService() {
startForeground(FOREGROUND_NOTIF_ID, notification)
}


private fun updateHeartRateListener() {
if (sp.getBoolean(R.string.key_heart_rate_sampling, false)) {
if (heartRateListener == null) {
heartRateListener = HeartRateListener(
this, aapsLogger, aapsSchedulers
).also { hrl -> disposable += hrl }
}
} else {
heartRateListener?.let { hrl ->
disposable.remove(hrl)
heartRateListener = null
}
}
}

private fun updatestepsCountListener() {
if (sp.getBoolean(R.string.key_steps_sampling, false)) {
if (stepCountListener == null) {
stepCountListener = StepCountListener(
this, aapsLogger, aapsSchedulers
).also { scl -> disposable += scl }
}
} else {
stepCountListener?.let { scl ->
disposable.remove(scl)
stepCountListener = null
}
}
}

@Suppress("PrivatePropertyName")
private val CHANNEL_ID: String = "DataLayerForegroundServiceChannel"
private fun createNotificationChannel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ abstract class BaseWatchFace : WatchFace() {

private var mLastSvg = ""
private var mLastDirection = ""
private var heartRateListener: HeartRateListener? = null
private var stepCountListener: StepCountListener? = null

override fun onCreate() {
// Not derived from DaggerService, do injection here
Expand All @@ -127,8 +125,6 @@ abstract class BaseWatchFace : WatchFace() {
.subscribe { event: EventWearPreferenceChange ->
simpleUi.updatePreferences()
if (event.changedKey != null && event.changedKey == "delta_granularity") rxBus.send(EventWearToMobile(ActionResendData("BaseWatchFace:onSharedPreferenceChanged")))
if (event.changedKey == getString(R.string.key_heart_rate_sampling)) updateHeartRateListener()
if (event.changedKey == getString(R.string.key_steps_sampling)) updatestepsCountListener()
if (layoutSet) setDataFields()
invalidate()
}
Expand All @@ -153,45 +149,13 @@ abstract class BaseWatchFace : WatchFace() {
layoutView = binding.root
performViewSetup()
rxBus.send(EventWearToMobile(ActionResendData("BaseWatchFace::onCreate")))
updateHeartRateListener()
updatestepsCountListener()
}

private fun forceUpdate() {
setDataFields()
invalidate()
}

private fun updateHeartRateListener() {
if (sp.getBoolean(R.string.key_heart_rate_sampling, false)) {
if (heartRateListener == null) {
heartRateListener = HeartRateListener(
this, aapsLogger, aapsSchedulers
).also { hrl -> disposable += hrl }
}
} else {
heartRateListener?.let { hrl ->
disposable.remove(hrl)
heartRateListener = null
}
}
}

private fun updatestepsCountListener() {
if (sp.getBoolean(R.string.key_steps_sampling, false)) {
if (stepCountListener == null) {
stepCountListener = StepCountListener(
this, aapsLogger, aapsSchedulers
).also { scl -> disposable += scl }
}
} else {
stepCountListener?.let { scl ->
disposable.remove(scl)
stepCountListener = null
}
}
}

override fun onTapCommand(tapType: Int, x: Int, y: Int, eventTime: Long) {
binding.chart?.let { chart ->
if (tapType == TAP_TYPE_TAP && x >= chart.left && x <= chart.right && y >= chart.top && y <= chart.bottom) {
Expand Down