diff --git a/packages/health/android/build.gradle b/packages/health/android/build.gradle index 1f96a1fb1..bed498fd2 100644 --- a/packages/health/android/build.gradle +++ b/packages/health/android/build.gradle @@ -55,7 +55,7 @@ dependencies { implementation(composeBom) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - implementation("androidx.health.connect:connect-client:1.1.0-alpha07") + implementation("androidx.health.connect:connect-client:1.1.0-alpha11") def fragment_version = "1.6.2" implementation "androidx.fragment:fragment-ktx:$fragment_version" diff --git a/packages/health/android/src/main/kotlin/cachet/plugins/health/HealthPlugin.kt b/packages/health/android/src/main/kotlin/cachet/plugins/health/HealthPlugin.kt index 1d6ee83dc..4bba159e1 100644 --- a/packages/health/android/src/main/kotlin/cachet/plugins/health/HealthPlugin.kt +++ b/packages/health/android/src/main/kotlin/cachet/plugins/health/HealthPlugin.kt @@ -12,6 +12,7 @@ import androidx.annotation.NonNull import androidx.health.connect.client.HealthConnectClient import androidx.health.connect.client.PermissionController import androidx.health.connect.client.permission.HealthPermission +import androidx.health.connect.client.permission.HealthPermission.Companion.PERMISSION_READ_HEALTH_DATA_HISTORY import androidx.health.connect.client.records.* import androidx.health.connect.client.records.MealType.MEAL_TYPE_BREAKFAST import androidx.health.connect.client.records.MealType.MEAL_TYPE_DINNER @@ -147,6 +148,8 @@ class HealthPlugin(private var channel: MethodChannel? = null) : when (call.method) { "installHealthConnect" -> installHealthConnect(call, result) "getHealthConnectSdkStatus" -> getHealthConnectSdkStatus(call, result) + "isHealthDataHistoryAuthorized" -> isHealthDataHistoryAuthorized(call, result) + "requestHealthDataHistoryAuthorization" -> requestHealthDataHistoryAuthorization(call, result) "hasPermissions" -> hasPermissions(call, result) "requestAuthorization" -> requestAuthorization(call, result) "revokePermissions" -> revokePermissions(call, result) @@ -512,6 +515,41 @@ class HealthPlugin(private var channel: MethodChannel? = null) : } } + /** + * Checks if PERMISSION_READ_HEALTH_DATA_HISTORY has been granted + */ + private fun isHealthDataHistoryAuthorized(call: MethodCall, result: Result) { + scope.launch { + result.success( + healthConnectClient + .permissionController + .getGrantedPermissions() + .containsAll(listOf(PERMISSION_READ_HEALTH_DATA_HISTORY)), + ) + } + } + + /** + * Requests authorization for PERMISSION_READ_HEALTH_DATA_HISTORY + */ + private fun requestHealthDataHistoryAuthorization(call: MethodCall, result: Result) { + if (context == null) { + result.success(false) + return + } + + if (healthConnectRequestPermissionsLauncher == null) { + result.success(false) + Log.i("FLUTTER_HEALTH", "Permission launcher not found") + return + } + + // Store the result to be called in [onHealthConnectPermissionCallback] + mResult = result + isReplySubmitted = false + healthConnectRequestPermissionsLauncher!!.launch(setOf(PERMISSION_READ_HEALTH_DATA_HISTORY)) + } + private fun hasPermissions(call: MethodCall, result: Result) { val args = call.arguments as HashMap<*, *> val types = (args["types"] as? ArrayList<*>)?.filterIsInstance()!! diff --git a/packages/health/lib/src/health_plugin.dart b/packages/health/lib/src/health_plugin.dart index cc62e3ac7..7ed39cb7b 100644 --- a/packages/health/lib/src/health_plugin.dart +++ b/packages/health/lib/src/health_plugin.dart @@ -200,6 +200,48 @@ class Health { } } + /// Checks the current status of the Health Data History permission. + /// + /// See this for more info: https://developer.android.com/reference/androidx/health/connect/client/permission/HealthPermission#PERMISSION_READ_HEALTH_DATA_HISTORY() + /// + /// + /// Android only. Returns true on iOS or false if an error occurs. + Future isHealthDataHistoryAuthorized() async { + if (Platform.isIOS) return true; + + try { + final status = + await _channel.invokeMethod('isHealthDataHistoryAuthorized'); + return status ?? false; + } catch (e) { + debugPrint('$runtimeType - Exception in getHealthConnectSdkStatus(): $e'); + return false; + } + } + + /// Requests the Health Data History permission. + /// + /// Returns true if successful, false otherwise. + /// + /// See this for more info: https://developer.android.com/reference/androidx/health/connect/client/permission/HealthPermission#PERMISSION_READ_HEALTH_DATA_HISTORY() + /// + /// + /// Android only. Returns true on iOS or false if an error occurs. + Future requestHealthDataHistoryAuthorization() async { + if (Platform.isIOS) return true; + + await _checkIfHealthConnectAvailableOnAndroid(); + try { + final bool? isAuthorized = + await _channel.invokeMethod('requestHealthDataHistoryAuthorization'); + return isAuthorized ?? false; + } catch (e) { + debugPrint( + '$runtimeType - Exception in requestHealthDataHistoryAuthorization(): $e'); + return false; + } + } + /// Requests permissions to access health data [types]. /// /// Returns true if successful, false otherwise.