Skip to content

Commit

Permalink
Update Health Connect for Android 14
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Nov 14, 2023
1 parent 4399ae2 commit 3e86af7
Show file tree
Hide file tree
Showing 11 changed files with 273 additions and 96 deletions.
7 changes: 3 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,11 @@ dependencies {
exclude group: 'androidx.core', module: 'core' // fix INotificationSideChannel // android.support.v4.app
}

api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1"
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1"
implementation(name: 'ns-sdk-full-release', ext: 'aar')

//implementation 'androidx.core:core-ktx:1.9.0'
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.7.21'
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.8.22'

implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'
Expand Down Expand Up @@ -302,7 +302,7 @@ dependencies {
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
implementation 'com.getpebble:pebblekit:3.1.0'
implementation ("androidx.health.connect:connect-client:1.0.0-alpha07") {
implementation ("androidx.health.connect:connect-client:1.1.0-alpha06") {
exclude group: 'androidx.core', module: 'core'
}
implementation 'com.github.jamorham:amazfitcommunication:master-SNAPSHOT'
Expand Down Expand Up @@ -332,7 +332,6 @@ dependencies {
implementation(name: 'colorpicker', ext: 'aar')
implementation(name: 'hellocharts-library-release', ext: 'aar')
implementation(name: 'search-preference', ext: 'aar')
implementation(name: 'javakotlininterop', ext: 'aar')
implementation 'org.apache.commons:commons-text:1.3'
implementation 'com.google.dagger:dagger:2.25.4'
implementation "com.evernote:android-job:1.2.6"
Expand Down
Binary file removed app/libs/javakotlininterop.aar
Binary file not shown.
43 changes: 40 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,32 @@
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />

<uses-permission android:name="android.permission.MANAGE_HEALTH_DATA"/>
<uses-permission android:name="android.permission.MANAGE_HEALTH_PERMISSIONS"/>

<uses-permission android:name="android.permission.health.READ_HEART_RATE"/>
<uses-permission android:name="android.permission.health.READ_BLOOD_GLUCOSE"/>
<uses-permission android:name="android.permission.health.WRITE_BLOOD_GLUCOSE"/>
<uses-permission android:name="android.permission.health.READ_STEPS"/>
<uses-permission android:name="android.permission.health.READ_EXERCISE"/>
<uses-permission android:name="android.permission.health.READ_TOTAL_CALORIES_BURNED"/>
<uses-permission android:name="android.permission.health.READ_WEIGHT"/>
<uses-permission android:name="android.permission.health.READ_DISTANCE"/>
<uses-permission android:name="android.permission.health.READ_ELEVATION_GAINED"/>
<uses-permission android:name="android.permission.health.READ_FLOORS_CLIMBED"/>
<uses-permission android:name="android.permission.health.READ_HEART_RATE_VARIABILITY"/>
<uses-permission android:name="android.permission.health.READ_HEIGHT"/>
<uses-permission android:name="android.permission.health.READ_HYDRATION"/>
<uses-permission android:name="android.permission.health.READ_NUTRITION"/>
<uses-permission android:name="android.permission.health.WRITE_NUTRITION"/>
<uses-permission android:name="android.permission.health.READ_POWER"/>
<uses-permission android:name="android.permission.health.READ_RESTING_HEART_RATE"/>
<uses-permission android:name="android.permission.health.READ_SLEEP"/>
<uses-permission android:name="android.permission.health.READ_SPEED"/>
<uses-permission android:name="android.permission.health.READ_WHEELCHAIR_PUSHES"/>

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>

<application
android:name=".xdrip"
android:allowBackup="false"
Expand All @@ -77,10 +103,21 @@
<intent-filter>
<action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE" />
</intent-filter>
<meta-data
android:name="health_permissions"
android:resource="@array/health_permissions" />
</activity>
<activity-alias
android:name="AndroidURationaleActivity"
android:exported="true"
android:targetActivity=".HealthPrivacy">
<intent-filter>
<action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE" />
</intent-filter>
<!-- Permission handling for Android 14 -->
<intent-filter>
<action android:name="android.intent.action.VIEW_PERMISSION_USAGE"/>
<category android:name="android.intent.category.HEALTH_PERMISSIONS"/>
</intent-filter>
</activity-alias>

<activity
android:name=".cloud.backup.BackupActivity"
android:exported="false"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.eveningoutpost.dexdrip.healthconnect

import android.annotation.TargetApi

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.health.connect.client.HealthConnectClient
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.lang.RuntimeException
import java.util.function.BiConsumer
import kotlin.coroutines.Continuation
import kotlin.coroutines.CoroutineContext

// jamorham

object Coroutines {
@JvmOverloads
fun <R> getContinuation(
onFinished: BiConsumer<R?, Throwable?>,
dispatcher: CoroutineDispatcher = Dispatchers.IO
): Continuation<R> {
return object : Continuation<R> {
override val context: CoroutineContext
get() = dispatcher

@TargetApi(Build.VERSION_CODES.N)
override fun resumeWith(result: Result<R>) {
onFinished.accept(result.getOrNull(), result.exceptionOrNull())
}
}
}

fun interface FunctionWrapperWithBiConsumer<T, R> {
fun apply(t: T, resultHandler: BiConsumer<R?, Throwable?>?)
}

@OptIn(DelicateCoroutinesApi::class)
@RequiresApi(Build.VERSION_CODES.N)
@JvmStatic
// Note: the fact that this anti-pattern is needed shows the imprecision of kotlin and lack of interoperability
fun <T, R> suspendFunction(fn: (T) -> R): FunctionWrapperWithBiConsumer<T, R> {
return FunctionWrapperWithBiConsumer { t, resultHandler ->
if (resultHandler == null) throw RuntimeException("result handler was null!")
GlobalScope.launch(Dispatchers.IO) {
try {
val result = fn(t)
resultHandler.accept(result, null)
} catch (e: Throwable) {
resultHandler.accept(null, e)
}
}
}
}

@JvmStatic
fun getGrantedPermissions(healthConnectClient: HealthConnectClient): Set<String> {
return runBlocking {
return@runBlocking healthConnectClient.permissionController.getGrantedPermissions()
}
}
}

Loading

0 comments on commit 3e86af7

Please sign in to comment.