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

Attempt to fix Health Connect on Android 14 #834

Merged

Conversation

eliasteeny
Copy link
Contributor

This pull request addresses the "Activity not found" error encountered when attempting to open the Health Connect permissions screen on Android 14. The issue was resolved by migrating from the old startActivityForResult method to the new API, registerForActivityResult.

Introduced a limitation for the Main app's activity: it should extend FlutterFragmentActivity instead of FlutterActivity. This adjustment allows casting from Activity to ComponentActivity for accessing registerForActivityResult.

Note: As a non-experienced Android/Kotlin developer, any suggestions for improvements or alternative approaches are welcome especially regarding FlutterFragmentActivity.

@chadpav
Copy link
Contributor

chadpav commented Nov 18, 2023

I was able to pull down this PR branch and verify the functionality work. The code looks reasonable to me.

This PR resolves Issue #800

Thank you @eliasteeny

Copy link
Contributor

@chadpav chadpav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solution works and code LGTM. I think we should add some documentation to the readme though. (I submitted a PR to your branch with proposed ReadMe updates)

NOTE: I'm not a maintainer so my approval means nothing. :)

0.0, HealthDataType.SLEEP_AWAKE, earlier, now);
success &= await health.writeHealthData(
0.0, HealthDataType.SLEEP_DEEP, earlier, now);
// success &= await health.writeHealthData(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these changes relevant to the PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah for some reason adding the sleep records is failing stating that it is an unsupported type. Although it is working on Android 13 and below.

HealthDataType.SLEEP_DEEP,
HealthDataType.SLEEP_REM,
HealthDataType.SLEEP_SESSION,
// HealthDataType.SLEEP_AWAKE,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these changes relevant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fetching the SLEEP data is crashing the app with the same error as writing.

Copy link
Contributor

@eric-nextsense eric-nextsense Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked out this PR, uncommented the SLEEP lines, and i can read sleep data without a crash. I am running Android 14.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eliasteeny could you please double check if this error still occurs on your device and if so, tell us your OS version and setup?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got a closer look. I did not have a wearable so was reading only sleep session data (time asleep, one data point) and that worked fine. I get the crash now that i have a wearable and i try to read the stages. The issue seems to be that SleepStageRecord object no longer exists in Android 14. Instead there is a list in the SleepSessionRecord. You can see this sample code in https://developer.android.com/health-and-fitness/guides/health-connect/develop/sessions

suspend fun readSleepSessions(
healthConnectClient: HealthConnectClient,
startTime: Instant,
endTime: Instant
) {
val response =
healthConnectClient.readRecords(
ReadRecordsRequest(
SleepSessionRecord::class,
timeRangeFilter = TimeRangeFilter.between(startTime, endTime)
)
)
for (sleepRecord in response.records) {
// Retrieve relevant sleep stages from each sleep record
val sleepStages = sleepRecord.stages
}
}

The convertRecord function returns a single record and now it should return many, not sure how to handle this with the way it works now?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a pull request on your fork @eliasteeny. I tested it on Android 13 and Android 14, both were able to pull both SLEEP_SESSION (total sleep time only) and individual sleep stages.

I did not test writing though, i don't have an app that would do that.

@Letalus
Copy link

Letalus commented Dec 15, 2023

any updates on this PR? This is a critical bug for all android 34 devices

@hoffmatteo
Copy link
Contributor

Hi, due to other projects and Christmas I was not able to properly evaluate it, I will do it this week though since it is an urgent issue.

@Letalus
Copy link

Letalus commented Jan 8, 2024

@hoffmatteo I hope you had a great start in the new year. Just for an update, where you able to review @eliasteeny PR and have any comments on it?

@madhav-sunil
Copy link

madhav-sunil commented Jan 9, 2024

Hi, is there any way to keep FlutterActivity instead of FlutterFragmentActivity?
I'm currently using a custom implementation of the package so any change is acceptable.
PS: I was able to open the Health connect settings to by using the health connects app id. Any thoughts on that..

private fun manageHealthAppPermission() {
val intent =
context!!.packageManager.getLaunchIntentForPackage("com.google.android.apps.healthdata")
checkAvailability()
if (healthConnectAvailable && intent !== null) {
context!!.startActivity(intent)
}
}

if ( healthConnectAvailable) {
val requestPermissionActivityContract = PermissionController.createRequestPermissionResultContract()

healthConnectRequestPermissionsLauncher =(activity as ComponentActivity).registerForActivityResult(requestPermissionActivityContract) { granted ->

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we use FlutterFragmentActivity instead of ComponentActivity

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need the ComponentActivity in order to use the new api function registerForActivityResult, FlutterFragmentActivity is built upon the new Android apis which can be casted to a ComponentActivity. If you try to cast FlutterActivity to a ComponentActivity it will throw an error.

@Alejandroem
Copy link

I have downloaded this fork and used the dependency as a reference from the download. And I can confirm this fix works. It will send me to the Health Connect Dialog to confirm the permissions and I can go from there.

I would advise anyone to do this until the package is properly updated. Thank you @eliasteeny you saved my app 💪🏻

@tomassasovsky
Copy link

Getting an error while testing on a Samsung Galaxy S23 Ultra, Android 14:

I/FLUTTER_HEALTH(27526): Permission launcher not found

@eric-nextsense
Copy link
Contributor

I created a fork of this pull request (and created a pull request with it to update this one) which fixes sleep read issues.

https://github.com/eric-nextsense/flutter-plugins/tree/health-connect-android-14-fix

@tomassasovsky
Copy link

Works perfectly, thanks @eric-nextsense!

@limewareio
Copy link

limewareio commented Jan 18, 2024

I'm getting the same error as @tomassasovsky - Permission launcher not found. I've tried referencing the https://github.com/eric-nextsense/flutter-plugins/tree/health-connect-android-14-fix repository in the pubspec.yaml file like this:

health:
    git:
      url: https://github.com/eric-nextsense/flutter-plugins
      path: packages/health
      ref: health-connect-android-14-fix

Any clues what might be wrong?

@eliasteeny
Copy link
Contributor Author

Getting an error while testing on a Samsung Galaxy S23 Ultra, Android 14:

I/FLUTTER_HEALTH(27526): Permission launcher not found

I'm getting the same error as @tomassasovsky - Permission launcher not found. I've tried referencing the https://github.com/eric-nextsense/flutter-plugins/tree/health-connect-android-14-fix repository in the pubspec.yaml file like this:

health:
    git:
      url: https://github.com/eric-nextsense/flutter-plugins
      path: packages/health
      ref: health-connect-android-14-fix

Any clues what might be wrong?

Did you use FlutterFragmentActivity instead of FlutterActivity in the MainActivity file ?

@hoffmatteo
Copy link
Contributor

Hi, I was finally able to test the PR and will include it in the upcoming release that will be coming late this week/early next week!

@hoffmatteo hoffmatteo changed the base branch from master to health-dev January 22, 2024 13:28
@hoffmatteo hoffmatteo merged commit 80ec67c into cph-cachet:health-dev Jan 22, 2024
1 check passed
@eric-nextsense
Copy link
Contributor

Did you include the additional changes to fix sleep that are in my branch? They were not merged in this fork by the author.

https://github.com/eric-nextsense/flutter-plugins/tree/health-connect-android-14-fix

@eliasteeny
Copy link
Contributor Author

@eric-nextsense I didn't merge it because I was waiting to have some free time to also fix the writing of sleep data.

@hoffmatteo
Copy link
Contributor

hoffmatteo commented Jan 23, 2024

Did you include the additional changes to fix sleep that are in my branch? They were not merged in this fork by the author.

https://github.com/eric-nextsense/flutter-plugins/tree/health-connect-android-14-fix

Hi, I was just looking into the sleep issue. Your PR looks great, I added it to target health-dev and am looking into adding write functionality right now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants