cph-cachet / flutter-plugins

A collection of Flutter plugins developed by CACHET
551 stars 681 forks source link

[Health 10.2.0] Health connect permissions don't match what is asked #984

Open louisdeveseleer opened 5 months ago

louisdeveseleer commented 5 months ago

Device / Emulator and OS

Describe the bug

The permissions appearing in Health connect do not match the list of permissions passed to the plugin method requestAuthorization.

Dart:

  final types = [
    HealthDataType.WEIGHT,
    HealthDataType.HEIGHT,
    HealthDataType.WORKOUT,
    HealthDataType.ACTIVE_ENERGY_BURNED,
  ];
  final permissions = [
    HealthDataAccess.WRITE,
    HealthDataAccess.WRITE,
    HealthDataAccess.WRITE,
    HealthDataAccess.WRITE,
  ];
  await health.requestAuthorization(types, permissions: permissions);

Android Manifest:

    <uses-permission android:name="android.permission.health.WRITE_HEIGHT"/>
    <uses-permission android:name="android.permission.health.WRITE_WEIGHT"/>
    <uses-permission android:name="android.permission.health.WRITE_EXERCISE"/>
    <uses-permission android:name="android.permission.health.WRITE_ACTIVE_CALORIES_BURNED"/>

Health connect screen: Screenshot_20240618-105608

The "write" permissions in health connect do not include the ones that were requested, and the "read" permissions include all of them, although none was requested.

As a consequence:

To Reproduce

Ask for Write-only permissions.

Expected behavior

For Health connect to list the exact permissions requested, no more, no less.

Actual behavior

The permissions requested do not appear, and others that were not requested do appear.

Flutter doctor

[✓] Flutter (Channel stable, 3.22.2, on macOS 14.5 23F79 darwin-arm64, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.1)
[✓] VS Code (version 1.90.1)
[✓] Connected device (4 available)
[✓] Network resources

• No issues found!
louisdeveseleer commented 5 months ago

After further investigation, I was able to narrow down the precise issues:

harryandroiddev commented 2 months ago

This is because you are using workout , which under the hood request for total calories burned and distance .

This is the plugin native code for android


   // Workout also needs distance and total energy burned too
            if (typeKey == WORKOUT) {
                if (access == 0) {
                    permList.addAll(
                        listOf(
                            HealthPermission.getReadPermission(
                                DistanceRecord::class
                            ),
                            HealthPermission.getReadPermission(
                                TotalCaloriesBurnedRecord::class
                            ),
                        ),
                    )
                } else {
                    permList.addAll(
                        listOf(
                            HealthPermission.getReadPermission(
                                DistanceRecord::class
                            ),
                            HealthPermission.getReadPermission(
                                TotalCaloriesBurnedRecord::class
                            ),
                            HealthPermission.getWritePermission(
                                DistanceRecord::class
                            ),
                            HealthPermission.getWritePermission(
                                TotalCaloriesBurnedRecord::class
                            ),
                        ),
                    )
                }
            }
louisdeveseleer commented 2 months ago

@harryandroiddev Thanks for sharing your insight! Do you know why "Distance" is automatically added for Workout, considering that many workout types don't involve any distance?