class HealthPermission


A Permission either to read or write data associated with a Record type.

Summary

Constants

const String

A permission that allows to read the entire history of health data (of any type).

const String

A permission to read data in background.

const String

A permission to write exercise routes.

Public companion functions

String

Returns a permission defined in HealthPermission to read provided recordType, such as Steps::class.

String

Returns a permission defined in HealthPermission to read provided recordType, such as Steps::class.

Public functions

open operator Boolean
equals(other: Any?)
open Int

Constants

PERMISSION_READ_HEALTH_DATA_HISTORY

const val PERMISSION_READ_HEALTH_DATA_HISTORYString

A permission that allows to read the entire history of health data (of any type).

An attempt to read data older than 30 days without this permission will result in an error. This applies for the following api methods: HealthConnectClient.readRecord, HealthConnectClient.readRecords, HealthConnectClient.aggregate, HealthConnectClient.aggregateGroupByPeriod, HealthConnectClient.aggregateGroupByDuration and HealthConnectClient.getChanges.

This feature is dependent on the version of HealthConnect installed on the device. To check if it's available call HealthConnectFeatures.getFeatureStatus and pass HealthConnectFeatures.FEATURE_READ_HEALTH_DATA_HISTORY as an argument.

import androidx.health.connect.client.HealthConnectFeatures
import androidx.health.connect.client.PermissionController
import androidx.health.connect.client.permission.HealthPermission.Companion.PERMISSION_READ_HEALTH_DATA_HISTORY

val requestPermission =
    activity.registerForActivityResult(
        PermissionController.createRequestPermissionResultContract()
    ) { grantedPermissions: Set<String> ->
        if (PERMISSION_READ_HEALTH_DATA_HISTORY in grantedPermissions) {
            // It will be possible to read data older than 30 days from now on
        } else {
            // Permission denied, it won't be possible to read data older than 30 days
        }
    }

if (
    features.getFeatureStatus(HealthConnectFeatures.FEATURE_READ_HEALTH_DATA_HISTORY) ==
        HealthConnectFeatures.FEATURE_STATUS_AVAILABLE
) {
    // The feature is available, request history read permissions
    requestPermission.launch(setOf(PERMISSION_READ_HEALTH_DATA_HISTORY))
}

PERMISSION_READ_HEALTH_DATA_IN_BACKGROUND

const val PERMISSION_READ_HEALTH_DATA_IN_BACKGROUNDString

A permission to read data in background.

An attempt to read data in background without this permission may result in an error.

This feature is dependent on the version of HealthConnect installed on the device. To check if it's available call HealthConnectFeatures.getFeatureStatus and pass HealthConnectFeatures.FEATURE_READ_HEALTH_DATA_IN_BACKGROUND as an argument.

import androidx.health.connect.client.HealthConnectFeatures
import androidx.health.connect.client.PermissionController
import androidx.health.connect.client.permission.HealthPermission.Companion.PERMISSION_READ_HEALTH_DATA_IN_BACKGROUND

val requestPermission =
    activity.registerForActivityResult(
        PermissionController.createRequestPermissionResultContract()
    ) { grantedPermissions: Set<String> ->
        if (PERMISSION_READ_HEALTH_DATA_IN_BACKGROUND in grantedPermissions) {
            // It will be possible to read data in background from now on
        } else {
            // Permission denied, it won't be possible to read data in background
        }
    }

if (
    features.getFeatureStatus(HealthConnectFeatures.FEATURE_READ_HEALTH_DATA_IN_BACKGROUND) ==
        HealthConnectFeatures.FEATURE_STATUS_AVAILABLE
) {
    // The feature is available, request background reads permission
    requestPermission.launch(setOf(PERMISSION_READ_HEALTH_DATA_IN_BACKGROUND))
}
import androidx.health.connect.client.permission.HealthPermission.Companion.PERMISSION_READ_HEALTH_DATA_IN_BACKGROUND
import androidx.health.connect.client.readRecord
import androidx.health.connect.client.records.StepsRecord
import androidx.health.connect.client.request.ReadRecordsRequest
import androidx.health.connect.client.time.TimeRangeFilter

val grantedPermissions = healthConnectClient.permissionController.getGrantedPermissions()

// The permission should be requested and granted beforehand when the app is in foreground
if (PERMISSION_READ_HEALTH_DATA_IN_BACKGROUND !in grantedPermissions) {
    return
}

val response =
    healthConnectClient.readRecords(
        ReadRecordsRequest<StepsRecord>(
            timeRangeFilter = TimeRangeFilter.between(startTime, endTime),
        )
    )

for (stepsRecord in response.records) {
    // Process each record
}

PERMISSION_WRITE_EXERCISE_ROUTE

const val PERMISSION_WRITE_EXERCISE_ROUTEString

A permission to write exercise routes.

This permission must be granted to successfully insert a route as a field of the corresponding androidx.health.connect.client.records.ExerciseSessionRecord. An attempt to insert/update a session with a set route without the permission granted will result in a failed call and the session insertion/update will be rejected.

If the permission is not granted the previously written route will not be deleted if the session gets updated with no route set.

import androidx.health.connect.client.permission.HealthPermission.Companion.PERMISSION_WRITE_EXERCISE_ROUTE
import androidx.health.connect.client.permission.HealthPermission.Companion.getWritePermission
import androidx.health.connect.client.records.ExerciseRoute
import androidx.health.connect.client.records.ExerciseSessionRecord
import androidx.health.connect.client.units.Length

val grantedPermissions = healthConnectClient.permissionController.getGrantedPermissions()

if (!grantedPermissions.contains(getWritePermission(ExerciseSessionRecord::class))) {
    return
}

val sessionStartTime = Instant.parse("2023-07-11T10:00:00.00Z")
val sessionDuration = Duration.ofMinutes(10)

val startLatitude = 51.511831
val endLatitude = 51.506007
val startLongitude = -0.165785
val endLongitude = -0.164888
val latitudeDeltaPerSecond = (endLatitude - startLatitude) / sessionDuration.seconds
val longitudeDeltaPerSecond = (endLongitude - startLongitude) / sessionDuration.seconds

val exerciseRoute =
    if (grantedPermissions.contains(PERMISSION_WRITE_EXERCISE_ROUTE)) {
        ExerciseRoute(
            List(sessionDuration.seconds.toInt()) { timeSeconds ->
                ExerciseRoute.Location(
                    time = sessionStartTime.plusSeconds(timeSeconds.toLong()),
                    latitude = startLatitude + latitudeDeltaPerSecond * timeSeconds,
                    longitude = startLongitude + longitudeDeltaPerSecond * timeSeconds,
                    horizontalAccuracy = Length.meters(2.0),
                    verticalAccuracy = Length.meters(2.0),
                    altitude = Length.meters(19.0)
                )
            }
        )
    } else {
        null
    }

val exerciseSessionRecord =
    ExerciseSessionRecord(
        startTime = sessionStartTime,
        startZoneOffset = ZoneOffset.UTC,
        endTime = sessionStartTime.plus(sessionDuration),
        endZoneOffset = ZoneOffset.UTC,
        exerciseType = ExerciseSessionRecord.EXERCISE_TYPE_RUNNING,
        title = "Morning Run",
        notes = "A nice run in a park",
        exerciseRoute = exerciseRoute
    )

healthConnectClient.insertRecords(listOf(exerciseSessionRecord))

Public companion functions

getReadPermission

Added in 1.1.0-alpha10
fun getReadPermission(recordType: KClass<Record>): String

Returns a permission defined in HealthPermission to read provided recordType, such as Steps::class.

Throws
kotlin.IllegalArgumentException

if the given record type is invalid.

getWritePermission

Added in 1.1.0-alpha10
fun getWritePermission(recordType: KClass<Record>): String

Returns a permission defined in HealthPermission to read provided recordType, such as Steps::class.

Returns
String

Permission object to use with androidx.health.connect.client.PermissionController.

Throws
kotlin.IllegalArgumentException

if the given record type is invalid.

Public functions

equals

open operator fun equals(other: Any?): Boolean

hashCode

open fun hashCode(): Int