diff --git a/health-connect/HealthConnectSample/app/src/main/AndroidManifest.xml b/health-connect/HealthConnectSample/app/src/main/AndroidManifest.xml
index e7755a63..2f5a1308 100644
--- a/health-connect/HealthConnectSample/app/src/main/AndroidManifest.xml
+++ b/health-connect/HealthConnectSample/app/src/main/AndroidManifest.xml
@@ -28,6 +28,7 @@
+
,
permissionsGranted: Boolean,
+ backgroundReadAvailable: Boolean,
+ backgroundReadGranted: Boolean,
sessionsList: List,
uiState: ExerciseSessionViewModel.UiState,
onInsertClick: () -> Unit = {},
@@ -110,6 +113,26 @@ fun ExerciseSessionScreen(
Text(stringResource(id = R.string.insert_exercise_session))
}
}
+ if (!backgroundReadGranted) {
+ item {
+ Button(
+ modifier = Modifier
+ .fillMaxWidth()
+ .height(48.dp)
+ .padding(4.dp),
+ onClick = {
+ onPermissionsLaunch(setOf(PERMISSION_READ_HEALTH_DATA_IN_BACKGROUND))
+ },
+ enabled = backgroundReadAvailable,
+ ) {
+ if (backgroundReadAvailable){
+ Text("Request Background Read")
+ } else {
+ Text("Background Read Is Not Available")
+ }
+ }
+ }
+ }
items(sessionsList) { session ->
val appInfo = session.sourceAppInfo
@@ -152,6 +175,8 @@ fun ExerciseSessionScreenPreview() {
ExerciseSessionScreen(
permissions = setOf(),
permissionsGranted = true,
+ backgroundReadAvailable = false,
+ backgroundReadGranted = false,
sessionsList = listOf(
ExerciseSession(
title = "Running",
diff --git a/health-connect/HealthConnectSample/app/src/main/java/com/example/healthconnectsample/presentation/screen/exercisesession/ExerciseSessionViewModel.kt b/health-connect/HealthConnectSample/app/src/main/java/com/example/healthconnectsample/presentation/screen/exercisesession/ExerciseSessionViewModel.kt
index 57da7865..2efe92ac 100644
--- a/health-connect/HealthConnectSample/app/src/main/java/com/example/healthconnectsample/presentation/screen/exercisesession/ExerciseSessionViewModel.kt
+++ b/health-connect/HealthConnectSample/app/src/main/java/com/example/healthconnectsample/presentation/screen/exercisesession/ExerciseSessionViewModel.kt
@@ -20,7 +20,10 @@ import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
+import androidx.health.connect.client.HealthConnectFeatures
+import androidx.health.connect.client.feature.ExperimentalFeatureAvailabilityApi
import androidx.health.connect.client.permission.HealthPermission
+import androidx.health.connect.client.permission.HealthPermission.Companion.PERMISSION_READ_HEALTH_DATA_IN_BACKGROUND
import androidx.health.connect.client.records.DistanceRecord
import androidx.health.connect.client.records.ExerciseSessionRecord
import androidx.health.connect.client.records.HeartRateRecord
@@ -59,6 +62,12 @@ class ExerciseSessionViewModel(private val healthConnectManager: HealthConnectMa
var permissionsGranted = mutableStateOf(false)
private set
+ var backgroundReadAvailable = mutableStateOf(false)
+ private set
+
+ var backgroundReadGranted = mutableStateOf(false)
+ private set
+
var sessionsList: MutableState> = mutableStateOf(listOf())
private set
@@ -131,8 +140,15 @@ class ExerciseSessionViewModel(private val healthConnectManager: HealthConnectMa
* Where an error is caught, of the type Health Connect is known to throw, [uiState] is set to
* [UiState.Error], which results in the snackbar being used to show the error message.
*/
+ @OptIn(ExperimentalFeatureAvailabilityApi::class)
private suspend fun tryWithPermissionsCheck(block: suspend () -> Unit) {
permissionsGranted.value = healthConnectManager.hasAllPermissions(permissions)
+ backgroundReadAvailable.value = healthConnectManager.isFeatureAvailable(
+ HealthConnectFeatures.FEATURE_READ_HEALTH_DATA_IN_BACKGROUND
+ )
+ backgroundReadGranted.value = healthConnectManager.hasAllPermissions(
+ setOf(PERMISSION_READ_HEALTH_DATA_IN_BACKGROUND)
+ )
uiState = try {
if (permissionsGranted.value) {
block()