-
-
Notifications
You must be signed in to change notification settings - Fork 926
Use Worker to handle ActivitySensor updates #6468
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,125 @@ | ||||||||||||||||||||
| package io.homeassistant.companion.android.sensors | ||||||||||||||||||||
|
|
||||||||||||||||||||
| import android.content.BroadcastReceiver | ||||||||||||||||||||
| import android.content.Context | ||||||||||||||||||||
| import android.content.Intent | ||||||||||||||||||||
| import com.google.android.gms.location.ActivityRecognitionResult | ||||||||||||||||||||
| import com.google.android.gms.location.DetectedActivity | ||||||||||||||||||||
| import com.google.android.gms.location.SleepClassifyEvent | ||||||||||||||||||||
| import com.google.android.gms.location.SleepSegmentEvent | ||||||||||||||||||||
| import io.homeassistant.companion.android.common.util.STATE_UNKNOWN | ||||||||||||||||||||
| import io.homeassistant.companion.android.sensors.worker.ActivitySensorWorker | ||||||||||||||||||||
| import io.homeassistant.companion.android.sensors.worker.SleepSensorWorker | ||||||||||||||||||||
| import timber.log.Timber | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * Lightweight receiver for GMS activity recognition and sleep intents. | ||||||||||||||||||||
| * | ||||||||||||||||||||
| * Extracts data synchronously from the intent and enqueues an | ||||||||||||||||||||
| * [ActivitySensorWorker] or [SleepSensorWorker] for the actual sensor | ||||||||||||||||||||
| * update work, to keep the BroadcastReceiver lifecycle bellow 10s. | ||||||||||||||||||||
|
||||||||||||||||||||
| * update work, to keep the BroadcastReceiver lifecycle bellow 10s. | |
| * update work, to keep the BroadcastReceiver lifecycle below 10s. |
Copilot
AI
Feb 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getSubActivity assumes probableActivities has at least 2 entries ([1]). ActivityRecognitionResult.probableActivities is not guaranteed to contain 2+ items, so this can throw IndexOutOfBoundsException and crash the receiver. Use getOrNull(1) (or a size check) and fall back to "on_foot" when the second entry is missing.
| if (result.probableActivities[1].type == DetectedActivity.RUNNING) return "running" | |
| if (result.probableActivities[1].type == DetectedActivity.WALKING) return "walking" | |
| return "on_foot" | |
| val subActivity = result.probableActivities.getOrNull(1) ?: return "on_foot" | |
| return when (subActivity.type) { | |
| DetectedActivity.RUNNING -> "running" | |
| DetectedActivity.WALKING -> "walking" | |
| else -> "on_foot" | |
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's necessary to separate the receiver from the SensorManager, it might be best to move it to a more specific package like you've done for workers. Keep the main package for sensors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not necessary but I prefer to have a clear separation between the two, to keep a clear separation of responsibilities.