android / android/health-samples
Collecting UserActivityInfo not working
- Dominant language
- Kotlin
- Stars
- 346
- Forks
- 183
- PR merge metrics
- No merged PRs in 30d
Description
Hello, I have encountered an issue: I am not getting new user activity info in fun `onUserActivityInfoReceived(info: UserActivityInfo)`. To say that I do not receive at all is not entirely correct, I receive, but I receive this data immediately after I registered the service with `setPassiveListenerServiceAsync` and this data is outdated. Here's an log of UserActivityInfo I collected for a day:
| № | write time to DB | userActivityInfo.stateChangeTime.toMillis | activity state |
| ------------- | ------------- | ------------- | ------------- |
| 1 | 1678282350577 - 08 2023 13:32:30 | 1678177045675 - 07 2023 08:17:25 | passive |
| **2** | **1678263022160 - 08 2023 08:10:22** - end of sleep| **1678177045669 - 07 2023 08:17:25** | **passive** |
| **3** | **1678223467842 - 07 2023 21:11:07** - start of sleep | **1678177045676 - 07 2023 08:17:25** | **passive** |
| 4 | 1678223370897 - 07 2023 21:09:30 | 1678177045670 - 07 2023 08:17:25 | passive |
| 5 | 1678209916454 - 07 2023 17:25:16 | 1678177045676 - 07 2023 08:17:25 | passive |
| 6 | 1678208627154 - 07 2023 17:03:47 | 1678177045676 - 07 2023 08:17:25 | passive |
| 7 | 1678206676803 - 07 2023 16:31:16 | 1678177045676 - 07 2023 08:17:25 | passive |
| 8 | 1678205537674 - 07 2023 16:12:17 | 1678177045676 - 07 2023 08:17:25 | passive |
| 9 | 1678204862883 - 07 2023 16:01:02 | 1678177045676 - 07 2023 08:17:25 | passive |
| 10 | 1678204832426 - 07 2023 16:00:32 | 1678177045676 - 07 2023 08:17:25 | passive |
| 11 | 1678204827020 - 07 2023 16:00:27 | 1678177045676 - 07 2023 08:17:25 | passive |
| 12 | 1678204820542 - 07 2023 16:00:20 | 1678177045676 - 07 2023 08:17:25 | passive |
| 13 | 1678184515495 - 07 2023 10:21:55 | 1678177045676 - 07 2023 08:17:25 | passive |
| 14 | 1678179278383 - 07 2023 08:54:38 | 1678177045676 - 07 2023 08:17:25 | passive |
| 15 | 1678178784282 - 07 2023 08:46:24 | 1678177045676 - 07 2023 08:17:25 | passive |
| 16 | 1678177767542 - 07 2023 08:29:27 | 1678177045676 - 07 2023 08:17:25 | passive |
| 17 | 1678177675343 - 07 2023 08:27:55 | 1678177045676 - 07 2023 08:17:25 | passive |
All the data here, these are the ones that I received immediately after registering the service. If you say that you shouldn’t register so often, then it’s fine, I have an interval in 11 hours when I didn't registered service, it's an interval 2 and 3, there is a period of time when I slept with the watch, and in theory, between these records, there should be other activities that say that I’m asleep, but as you can see there's nothing. And that's the problem, I want to track my sleep duration but I can't because I'm not getting activity changes :(
### Code
Manifest.sml
```
```
Registered service here
```
suspend fun registerListenerForFitData(dataTypes: Set>) {
val passiveListenerConfig = PassiveListenerConfig.builder()
.setShouldUserActivityInfoBeRequested(shouldUserActivityInfoBeRequested = true)
.setDataTypes(dataTypes)
.build()
passiveMonitoringClient.setPassiveListenerServiceAsync(
PassiveDataService::class.java,
passiveListenerConfig
).await()
}
```
PassiveDataService.kt
```
class PassiveDataService : PassiveListenerService() {
private val activityStateManager: ActivityStateManager by inject()
private val job = SupervisorJob()
private val scope = CoroutineScope(Dispatchers.IO + job)
override fun onUserActivityInfoReceived(info: UserActivityInfo) {
scope.launch {
activityStateManager.addActivityState(
activityName = info.userActivityState.toActivityName(),
timeInMillis = info.stateChangeTime.toEpochMilli()
)
Log.d("TAG!", "userActivityState: ${info.userActivityState.name}")
}
}
override fun onNewDataPointsReceived(dataPoints: DataPointContainer) {
...
}
}
```
### Environments:
**Device:** Fossil Gen 6
**OS:** Wear OS 3
**Android SDK:** 30
**Lib version:** 1.0.0-beta02
### Notes:
- Steps, calories and heart rate tracking work
- I've been tracking for 4 days now and the logs for those days were the same
Thank you in advance!
Contributor guide
Research direction
Start with registerListenerForFitData and PassiveDataService.kt, then review the manifest declarations for PassiveDataService and the related receivers. Reproduce on the Fossil Gen 6 with Wear OS 3 and inspect when onUserActivityInfoReceived is invoked and what stateChangeTime contains. Done means identifying whether the stale callbacks are caused by registration, the passive listener service, or the platform, and documenting or fixing the confirmed cause.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, kotlin
- Domain
- mobile
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100