-
Notifications
You must be signed in to change notification settings - Fork 355
Add Health connect snippets #866
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shounbi
wants to merge
9
commits into
android:main
Choose a base branch
from
shounbi:health-connect-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
02826cb
add health connect folder
shounbi 3f4837d
add health connect snippets
shounbi 0c353ca
Merge branch 'main' into health-connect-integration
shounbi 22ce8aa
fix reviews
shounbi 610e84d
fix reviews
shounbi d38ddcd
fix reviews
shounbi 85cd586
Merge branch 'main' into health-connect-integration
shounbi e06ba50
Merge branch 'main' into health-connect-integration
shounbi 0af5091
Merge branch 'main' into health-connect-integration
shounbi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| plugins { | ||
| alias(libs.plugins.android.application) | ||
| alias(libs.plugins.compose.compiler) | ||
| } | ||
|
|
||
| android { | ||
| namespace = "com.example.healthconnect" | ||
| compileSdkPreview = "CinnamonBun" | ||
|
|
||
| defaultConfig { | ||
| applicationId = "com.example.healthconnect" | ||
| minSdk = 26 | ||
| targetSdk = 36 | ||
| versionCode = 1 | ||
| versionName = "1.0" | ||
|
|
||
| testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" | ||
| } | ||
|
|
||
| buildTypes { | ||
| release { | ||
| isMinifyEnabled = false | ||
| proguardFiles( | ||
| getDefaultProguardFile("proguard-android-optimize.txt"), | ||
| "proguard-rules.pro" | ||
| ) | ||
| } | ||
| } | ||
| compileOptions { | ||
| sourceCompatibility = JavaVersion.VERSION_11 | ||
| targetCompatibility = JavaVersion.VERSION_11 | ||
| } | ||
| buildFeatures { | ||
| compose = true | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation(libs.androidx.core.ktx) | ||
| implementation(libs.androidx.lifecycle.runtime) | ||
| implementation(libs.androidx.activity.compose) | ||
| implementation(platform(libs.androidx.compose.bom)) | ||
| implementation(libs.androidx.compose.ui) | ||
| implementation(libs.androidx.compose.ui.graphics) | ||
| implementation(libs.androidx.compose.ui.tooling.preview) | ||
| implementation(libs.androidx.compose.material3) | ||
| implementation(libs.androidx.health.connect) | ||
| implementation(libs.androidx.work.runtime.ktx) | ||
|
|
||
| debugImplementation(libs.androidx.compose.ui.tooling) | ||
| debugImplementation(libs.androidx.compose.ui.test.manifest) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Add project specific ProGuard rules here. | ||
| # You can control the set of applied configuration files using the | ||
| # proguardFiles setting in build.gradle. | ||
| # | ||
| # For more details, see | ||
| # http://developer.android.com/guide/developing/tools/proguard.html | ||
|
|
||
| # If your project uses WebView with JS, uncomment the following | ||
| # and specify the fully qualified class name to the JavaScript interface | ||
| # class: | ||
| #-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
| # public *; | ||
| #} | ||
|
|
||
| # Uncomment this to preserve the line number information for | ||
| # debugging stack traces. | ||
| #-keepattributes SourceFile,LineNumberTable | ||
|
|
||
| # If you keep the line number information, uncomment this to | ||
| # hide the original source file name. | ||
| #-renamesourcefileattribute SourceFile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
|
||
| <queries> | ||
| <package android:name="com.google.android.apps.healthdata" /> | ||
| </queries> | ||
|
|
||
| <application | ||
| android:allowBackup="true" | ||
| android:icon="@mipmap/ic_launcher" | ||
| android:label="@string/app_name" | ||
| android:roundIcon="@mipmap/ic_launcher_round" | ||
| android:supportsRtl="true" | ||
| android:theme="@style/Theme.Snippets"> | ||
|
|
||
| <activity | ||
| android:name=".HealthConnectActivity" | ||
| android:exported="true"> | ||
| <intent-filter> | ||
| <action android:name="android.intent.action.MAIN" /> | ||
| <category android:name="android.intent.category.LAUNCHER" /> | ||
| </intent-filter> | ||
|
|
||
| <intent-filter> | ||
| <action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE" /> | ||
| </intent-filter> | ||
| </activity> | ||
|
|
||
| </application> | ||
| </manifest> |
37 changes: 37 additions & 0 deletions
37
healthconnect/src/main/java/com/example/healthconnect/ChangesManager.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.example.healthconnect | ||
|
|
||
| import androidx.health.connect.client.HealthConnectClient | ||
| import androidx.health.connect.client.changes.DeletionChange | ||
| import androidx.health.connect.client.changes.UpsertionChange | ||
| import androidx.health.connect.client.request.ChangesTokenRequest | ||
| import androidx.health.connect.client.records.WeightRecord | ||
| import android.content.Context | ||
|
|
||
| class ChangesManager(private val healthConnectClient: HealthConnectClient) { | ||
|
|
||
| suspend fun getChangesToken(): String { | ||
| // [START android_healthconnect_get_changes_token] | ||
| val changesToken = healthConnectClient.getChangesToken( | ||
| ChangesTokenRequest(recordTypes = setOf(WeightRecord::class)) | ||
| ) | ||
| // [END android_healthconnect_get_changes_token] | ||
| return changesToken | ||
| } | ||
|
|
||
| // [START android_healthconnect_process_changes] | ||
| suspend fun processChanges(token: String): String { | ||
| var nextChangesToken = token | ||
| do { | ||
| val response = healthConnectClient.getChanges(nextChangesToken) | ||
| response.changes.forEach { change -> | ||
| when (change) { | ||
| is UpsertionChange -> { /* Process Upsert */ } | ||
| is DeletionChange -> { /* Process Deletion */ } | ||
| } | ||
| } | ||
| nextChangesToken = response.nextChangesToken | ||
| } while (response.hasMore) | ||
| return nextChangesToken | ||
| } | ||
| // [END android_healthconnect_process_changes] | ||
| } |
139 changes: 139 additions & 0 deletions
139
healthconnect/src/main/java/com/example/healthconnect/HealthConnectActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| /* | ||
| * Copyright 2026 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
|
|
||
| package com.example.healthconnect | ||
|
|
||
| import android.os.Bundle | ||
| import androidx.activity.ComponentActivity | ||
| import androidx.activity.compose.setContent | ||
| import androidx.activity.enableEdgeToEdge | ||
| import androidx.compose.foundation.layout.Box | ||
| import androidx.compose.foundation.layout.fillMaxSize | ||
| import androidx.compose.foundation.layout.padding | ||
| import androidx.compose.foundation.lazy.LazyColumn | ||
| import androidx.compose.material3.Button | ||
| import androidx.compose.material3.ExperimentalMaterial3Api | ||
| import androidx.compose.material3.MaterialTheme | ||
| import androidx.compose.material3.Scaffold | ||
| import androidx.compose.material3.SnackbarHost | ||
| import androidx.compose.material3.SnackbarHostState | ||
| import androidx.compose.material3.Text | ||
| import androidx.compose.material3.TopAppBar | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.rememberCoroutineScope | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.platform.LocalContext | ||
| import androidx.compose.ui.text.style.TextAlign | ||
| import androidx.compose.ui.unit.dp | ||
| import androidx.health.connect.client.HealthConnectClient | ||
| import com.example.healthconnect.ui.theme.SnippetsTheme | ||
| import kotlinx.coroutines.launch | ||
| import java.time.Clock | ||
| import java.time.Instant | ||
| import java.time.Duration | ||
|
|
||
| class HealthConnectActivity : ComponentActivity() { | ||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| enableEdgeToEdge() | ||
|
|
||
| setContent { | ||
| SnippetsTheme { | ||
| HealthConnectScreen(Modifier.fillMaxSize()) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @OptIn(ExperimentalMaterial3Api::class) | ||
| @Composable | ||
| fun HealthConnectScreen(modifier: Modifier) { | ||
| val context = LocalContext.current | ||
| val coroutineScope = rememberCoroutineScope() | ||
| val snackbarHostState = remember { SnackbarHostState() } | ||
|
|
||
| // [START android_healthconnect_get_client] | ||
| val availabilityStatus = HealthConnectClient.getSdkStatus(context) | ||
| if (availabilityStatus == HealthConnectClient.SDK_UNAVAILABLE) { | ||
| Box(modifier = modifier.padding(16.dp), contentAlignment = Alignment.Center) { | ||
| Text( | ||
| text = "Health Connect is not available on this device. Please ensure it is installed and updated.", | ||
| style = MaterialTheme.typography.bodyLarge, | ||
| textAlign = TextAlign.Center | ||
| ) | ||
| } | ||
| return | ||
| } | ||
|
shounbi marked this conversation as resolved.
|
||
|
|
||
| val healthConnectClient = remember { | ||
| if (availabilityStatus == HealthConnectClient.SDK_AVAILABLE) { | ||
| HealthConnectClient.getOrCreate(context) | ||
| } else { | ||
| null | ||
| } | ||
| } | ||
| // [END android_healthconnect_get_client] | ||
|
|
||
| // Initialize our snippet manager | ||
| val manager = remember(healthConnectClient) { | ||
| healthConnectClient?.let { HealthConnectManager(it) } | ||
| } | ||
|
|
||
| Scaffold( | ||
| modifier = modifier, | ||
| snackbarHost = { SnackbarHost(snackbarHostState) }, | ||
| topBar = { TopAppBar(title = { Text("Health Connect Snippets") }) }, | ||
| ) { innerPadding -> | ||
| LazyColumn(Modifier.padding(innerPadding).padding(horizontal = 16.dp)) { | ||
| item { | ||
| Text( | ||
| text = if (healthConnectClient != null) | ||
| "Health Connect is available" | ||
| else | ||
| "Health Connect is not available", | ||
| modifier = Modifier.padding(vertical = 16.dp) | ||
| ) | ||
| } | ||
|
|
||
| if (manager != null) { | ||
| item { | ||
| Button(onClick = { | ||
| coroutineScope.launch { | ||
| val startTime = Instant.now().minusSeconds(3600) | ||
| val endTime = Instant.now() | ||
| manager.insertSteps(startTime, endTime) | ||
| snackbarHostState.showSnackbar("Steps inserted!") | ||
| } | ||
| }) { | ||
| Text("Run: Insert Steps") | ||
| } | ||
| } | ||
|
|
||
| item { | ||
| Button( | ||
| modifier = Modifier.padding(top = 8.dp), | ||
| onClick = { | ||
| coroutineScope.launch { | ||
| val startTime = Instant.now().minus(Duration.ofDays(1)) | ||
| val endTime = Instant.now() | ||
|
|
||
| val total = manager.readStepsAggregate(startTime, endTime) | ||
| snackbarHostState.showSnackbar("Total Steps: $total") | ||
| } | ||
| }) { | ||
| Text("Run: Read Steps Aggregate") | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.