PermissionController


interface PermissionController

Known direct subclasses
FakePermissionController

A fake PermissionController that enables full control of permissions in tests for a HealthConnectClient.


Interface for operations related to permissions.

Summary

Public companion functions

ActivityResultContract<Set<String>, Set<String>>

Creates an ActivityResultContract to request Health permissions.

Public functions

suspend Set<String>

Returns a set of all health permissions granted by the user to the calling app.

suspend Unit

Revokes all previously granted HealthPermission by the user to the calling app.

Public companion functions

createRequestPermissionResultContract

Added in 1.1.0-alpha10
fun createRequestPermissionResultContract(
    providerPackageName: String = DEFAULT_PROVIDER_PACKAGE_NAME
): ActivityResultContract<Set<String>, Set<String>>

Creates an ActivityResultContract to request Health permissions.

import androidx.health.connect.client.PermissionController
import androidx.health.connect.client.permission.HealthPermission
import androidx.health.connect.client.records.StepsRecord

val requestPermission =
    activity.registerForActivityResult(
        PermissionController.createRequestPermissionResultContract()
    ) { grantedPermissions: Set<String> ->
        if (
            grantedPermissions.contains(HealthPermission.getReadPermission(StepsRecord::class))
        ) {
            // Read or process steps related health records.
        } else {
            // user denied permission
        }
    }
requestPermission.launch(setOf(HealthPermission.getReadPermission(StepsRecord::class)))
Parameters
providerPackageName: String = DEFAULT_PROVIDER_PACKAGE_NAME

Optional provider package name to request health permissions from.

Public functions

getGrantedPermissions

suspend fun getGrantedPermissions(): Set<String>

Returns a set of all health permissions granted by the user to the calling app.

import androidx.health.connect.client.permission.HealthPermission
import androidx.health.connect.client.records.StepsRecord

val grantedPermissions = permissionController.getGrantedPermissions()

if (grantedPermissions.contains(HealthPermission.getReadPermission(StepsRecord::class))) {
    // Read or process steps related health records.
} else {
    // user denied permission
}
Returns
Set<String>

set of granted permissions.

Throws
android.os.RemoteException

For any IPC transportation failures.

java.io.IOException

For any disk I/O issues.

kotlin.IllegalStateException

If service is not available.

revokeAllPermissions

Added in 1.1.0-alpha10
suspend fun revokeAllPermissions(): Unit

Revokes all previously granted HealthPermission by the user to the calling app.

Throws
android.os.RemoteException

For any IPC transportation failures.

java.io.IOException

For any disk I/O issues.

kotlin.IllegalStateException

If service is not available.