react-native-health-connect 1.2.3 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -4
- package/android/src/main/java/dev/matinzd/healthconnect/HealthConnectManager.kt +7 -42
- package/android/src/main/java/dev/matinzd/healthconnect/permissions/HealthConnectPermissionDelegate.kt +35 -0
- package/android/src/main/java/dev/matinzd/healthconnect/permissions/{HCPermissionManager.kt → PermissionUtils.kt} +2 -25
- package/android/src/main/java/dev/matinzd/healthconnect/records/ReactNutritionRecord.kt +42 -42
- package/android/src/main/java/dev/matinzd/healthconnect/utils/HealthConnectUtils.kt +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,18 +19,52 @@ This library is a wrapper around Health Connect for react native. Health Connect
|
|
|
19
19
|
|
|
20
20
|
## Requirements
|
|
21
21
|
|
|
22
|
-
- [Health Connect](https://play.google.com/store/apps/details?id=com.google.android.apps.healthdata&hl=en&gl=US) needs to be installed on the user's device
|
|
22
|
+
- [Health Connect](https://play.google.com/store/apps/details?id=com.google.android.apps.healthdata&hl=en&gl=US) needs to be installed on the user's device. Starting from Android 14 (Upside Down Cake), Health Connect is part of the Android Framework. Read more [here](https://developer.android.com/health-and-fitness/guides/health-connect/develop/get-started#step-1).
|
|
23
23
|
- Health Connect API requires `minSdkVersion=26` (Android Oreo / 8.0).
|
|
24
|
+
- If you are planning to release your app on Google Play, you will need to submit a [declaration form](https://docs.google.com/forms/d/1LFjbq1MOCZySpP5eIVkoyzXTanpcGTYQH26lKcrQUJo/viewform?edit_requested=true).
|
|
24
25
|
|
|
25
26
|
## Installation
|
|
26
27
|
|
|
27
|
-
|
|
28
|
+
To install react-native-health-connect, use the following command:
|
|
28
29
|
|
|
29
30
|
```bash
|
|
30
|
-
yarn add react-native-health-connect
|
|
31
|
+
yarn add react-native-health-connect
|
|
31
32
|
```
|
|
32
33
|
|
|
33
|
-
|
|
34
|
+
For version 2 onwards, please add the following code into your `MainActivity.kt` within the `onCreate` method:
|
|
35
|
+
|
|
36
|
+
```diff
|
|
37
|
+
package com.healthconnectexample
|
|
38
|
+
|
|
39
|
+
+ import android.os.Bundle
|
|
40
|
+
import com.facebook.react.ReactActivity
|
|
41
|
+
import com.facebook.react.ReactActivityDelegate
|
|
42
|
+
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
|
|
43
|
+
import com.facebook.react.defaults.DefaultReactActivityDelegate
|
|
44
|
+
+ import dev.matinzd.healthconnect.permissions.HealthConnectPermissionDelegate
|
|
45
|
+
|
|
46
|
+
class MainActivity : ReactActivity() {
|
|
47
|
+
/**
|
|
48
|
+
* Returns the name of the main component registered from JavaScript. This is used to schedule
|
|
49
|
+
* rendering of the component.
|
|
50
|
+
*/
|
|
51
|
+
override fun getMainComponentName(): String = "HealthConnectExample"
|
|
52
|
+
|
|
53
|
+
+ override fun onCreate(savedInstanceState: Bundle?) {
|
|
54
|
+
+ super.onCreate(savedInstanceState)
|
|
55
|
+
+ // In order to handle permission contract results, we need to set the permission delegate.
|
|
56
|
+
+ HealthConnectPermissionDelegate.setPermissionDelegate(this)
|
|
57
|
+
+ }
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate]
|
|
61
|
+
* which allows you to enable New Architecture with a single boolean flags [fabricEnabled]
|
|
62
|
+
*/
|
|
63
|
+
override fun createReactActivityDelegate(): ReactActivityDelegate =
|
|
64
|
+
DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
```
|
|
34
68
|
|
|
35
69
|
|
|
36
70
|
## Expo installation
|
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
package dev.matinzd.healthconnect
|
|
2
2
|
|
|
3
|
-
import android.app.Activity
|
|
4
3
|
import android.content.Intent
|
|
5
|
-
import android.os.Bundle
|
|
6
4
|
import androidx.health.connect.client.HealthConnectClient
|
|
7
|
-
import com.facebook.react.bridge.ActivityEventListener
|
|
8
5
|
import com.facebook.react.bridge.Promise
|
|
9
6
|
import com.facebook.react.bridge.ReactApplicationContext
|
|
10
7
|
import com.facebook.react.bridge.ReadableArray
|
|
11
8
|
import com.facebook.react.bridge.ReadableMap
|
|
12
|
-
import dev.matinzd.healthconnect.permissions.
|
|
9
|
+
import dev.matinzd.healthconnect.permissions.HealthConnectPermissionDelegate
|
|
10
|
+
import dev.matinzd.healthconnect.permissions.PermissionUtils
|
|
13
11
|
import dev.matinzd.healthconnect.records.ReactHealthRecord
|
|
14
12
|
import dev.matinzd.healthconnect.utils.ClientNotInitialized
|
|
15
13
|
import dev.matinzd.healthconnect.utils.getTimeRangeFilter
|
|
@@ -19,12 +17,9 @@ import kotlinx.coroutines.CoroutineScope
|
|
|
19
17
|
import kotlinx.coroutines.Dispatchers
|
|
20
18
|
import kotlinx.coroutines.launch
|
|
21
19
|
|
|
22
|
-
class HealthConnectManager(private val applicationContext: ReactApplicationContext)
|
|
23
|
-
ActivityEventListener {
|
|
20
|
+
class HealthConnectManager(private val applicationContext: ReactApplicationContext) {
|
|
24
21
|
private lateinit var healthConnectClient: HealthConnectClient
|
|
25
22
|
private val coroutineScope = CoroutineScope(Dispatchers.IO)
|
|
26
|
-
private var pendingPromise: Promise? = null
|
|
27
|
-
private var latestPermissions: Set<String>? = null
|
|
28
23
|
|
|
29
24
|
private val isInitialized get() = this::healthConnectClient.isInitialized
|
|
30
25
|
|
|
@@ -35,16 +30,6 @@ class HealthConnectManager(private val applicationContext: ReactApplicationConte
|
|
|
35
30
|
block()
|
|
36
31
|
}
|
|
37
32
|
|
|
38
|
-
override fun onActivityResult(
|
|
39
|
-
activity: Activity?, requestCode: Int, resultCode: Int, intent: Intent?
|
|
40
|
-
) {
|
|
41
|
-
if (requestCode == REQUEST_CODE) {
|
|
42
|
-
HCPermissionManager.parseOnActivityResult(resultCode, intent, pendingPromise)
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
override fun onNewIntent(intent: Intent?) {}
|
|
47
|
-
|
|
48
33
|
fun openHealthConnectSettings() {
|
|
49
34
|
val intent = Intent(HealthConnectClient.ACTION_HEALTH_CONNECT_SETTINGS)
|
|
50
35
|
applicationContext.currentActivity?.startActivity(intent)
|
|
@@ -75,22 +60,10 @@ class HealthConnectManager(private val applicationContext: ReactApplicationConte
|
|
|
75
60
|
reactPermissions: ReadableArray, providerPackageName: String, promise: Promise
|
|
76
61
|
) {
|
|
77
62
|
throwUnlessClientIsAvailable(promise) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
val bundle = Bundle().apply {
|
|
82
|
-
putString("providerPackageName", providerPackageName)
|
|
63
|
+
coroutineScope.launch {
|
|
64
|
+
val granted = HealthConnectPermissionDelegate.launch(PermissionUtils.parsePermissions(reactPermissions))
|
|
65
|
+
promise.resolve(PermissionUtils.mapPermissionResult(granted))
|
|
83
66
|
}
|
|
84
|
-
|
|
85
|
-
val intent = HCPermissionManager(providerPackageName).healthPermissionContract.createIntent(
|
|
86
|
-
applicationContext, latestPermissions!!
|
|
87
|
-
)
|
|
88
|
-
|
|
89
|
-
applicationContext.currentActivity?.startActivityForResult(
|
|
90
|
-
intent,
|
|
91
|
-
HealthConnectManager.REQUEST_CODE,
|
|
92
|
-
bundle
|
|
93
|
-
)
|
|
94
67
|
}
|
|
95
68
|
}
|
|
96
69
|
|
|
@@ -105,7 +78,7 @@ class HealthConnectManager(private val applicationContext: ReactApplicationConte
|
|
|
105
78
|
fun getGrantedPermissions(promise: Promise) {
|
|
106
79
|
throwUnlessClientIsAvailable(promise) {
|
|
107
80
|
coroutineScope.launch {
|
|
108
|
-
promise.resolve(
|
|
81
|
+
promise.resolve(PermissionUtils.getGrantedPermissions(healthConnectClient.permissionController))
|
|
109
82
|
}
|
|
110
83
|
}
|
|
111
84
|
}
|
|
@@ -205,13 +178,5 @@ class HealthConnectManager(private val applicationContext: ReactApplicationConte
|
|
|
205
178
|
}
|
|
206
179
|
}
|
|
207
180
|
}
|
|
208
|
-
|
|
209
|
-
companion object {
|
|
210
|
-
const val REQUEST_CODE = 4235
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
init {
|
|
214
|
-
applicationContext.addActivityEventListener(this)
|
|
215
|
-
}
|
|
216
181
|
}
|
|
217
182
|
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
package dev.matinzd.healthconnect.permissions
|
|
2
|
+
|
|
3
|
+
import androidx.activity.result.ActivityResultLauncher
|
|
4
|
+
import androidx.health.connect.client.PermissionController
|
|
5
|
+
import com.facebook.react.ReactActivity
|
|
6
|
+
import kotlinx.coroutines.CoroutineScope
|
|
7
|
+
import kotlinx.coroutines.Dispatchers
|
|
8
|
+
import kotlinx.coroutines.cancel
|
|
9
|
+
import kotlinx.coroutines.channels.Channel
|
|
10
|
+
import kotlinx.coroutines.launch
|
|
11
|
+
|
|
12
|
+
object HealthConnectPermissionDelegate {
|
|
13
|
+
private lateinit var requestPermission: ActivityResultLauncher<Set<String>>
|
|
14
|
+
private val channel = Channel<Set<String>>()
|
|
15
|
+
private val coroutineScope = CoroutineScope(Dispatchers.IO)
|
|
16
|
+
|
|
17
|
+
fun setPermissionDelegate(
|
|
18
|
+
activity: ReactActivity,
|
|
19
|
+
providerPackageName: String = "com.google.android.apps.healthdata"
|
|
20
|
+
) {
|
|
21
|
+
val contract = PermissionController.createRequestPermissionResultContract(providerPackageName)
|
|
22
|
+
|
|
23
|
+
requestPermission = activity.registerForActivityResult(contract) {
|
|
24
|
+
coroutineScope.launch {
|
|
25
|
+
channel.send(it)
|
|
26
|
+
coroutineContext.cancel()
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
suspend fun launch(permissions: Set<String>): Set<String> {
|
|
32
|
+
requestPermission.launch(permissions)
|
|
33
|
+
return channel.receive()
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -1,22 +1,15 @@
|
|
|
1
1
|
package dev.matinzd.healthconnect.permissions
|
|
2
2
|
|
|
3
|
-
import android.content.Intent
|
|
4
3
|
import androidx.health.connect.client.PermissionController
|
|
5
4
|
import androidx.health.connect.client.permission.HealthPermission
|
|
6
|
-
import com.facebook.react.bridge.Promise
|
|
7
5
|
import com.facebook.react.bridge.ReadableArray
|
|
8
6
|
import com.facebook.react.bridge.WritableNativeArray
|
|
9
7
|
import com.facebook.react.bridge.WritableNativeMap
|
|
10
8
|
import dev.matinzd.healthconnect.utils.InvalidRecordType
|
|
11
9
|
import dev.matinzd.healthconnect.utils.reactRecordTypeToClassMap
|
|
12
10
|
|
|
13
|
-
class
|
|
14
|
-
val healthPermissionContract =
|
|
15
|
-
PermissionController.createRequestPermissionResultContract(providerPackageName)
|
|
16
|
-
|
|
11
|
+
class PermissionUtils {
|
|
17
12
|
companion object {
|
|
18
|
-
private const val DEFAULT_PROVIDER_PACKAGE_NAME = "com.google.android.apps.healthdata"
|
|
19
|
-
|
|
20
13
|
fun parsePermissions(reactPermissions: ReadableArray): Set<String> {
|
|
21
14
|
return reactPermissions.toArrayList().mapNotNull {
|
|
22
15
|
it as HashMap<*, *>
|
|
@@ -32,27 +25,11 @@ class HCPermissionManager(providerPackageName: String) {
|
|
|
32
25
|
}.toSet()
|
|
33
26
|
}
|
|
34
27
|
|
|
35
|
-
fun parseOnActivityResult(
|
|
36
|
-
resultCode: Int,
|
|
37
|
-
intent: Intent?,
|
|
38
|
-
pendingPromise: Promise?
|
|
39
|
-
) {
|
|
40
|
-
val providerPackageName =
|
|
41
|
-
intent?.getStringExtra("providerPackageName") ?: DEFAULT_PROVIDER_PACKAGE_NAME
|
|
42
|
-
val contract = HCPermissionManager(providerPackageName).healthPermissionContract
|
|
43
|
-
val result = contract.parseResult(
|
|
44
|
-
resultCode,
|
|
45
|
-
intent
|
|
46
|
-
)
|
|
47
|
-
|
|
48
|
-
pendingPromise?.resolve(mapPermissionResult(result))
|
|
49
|
-
}
|
|
50
|
-
|
|
51
28
|
suspend fun getGrantedPermissions(permissionController: PermissionController): WritableNativeArray {
|
|
52
29
|
return mapPermissionResult(permissionController.getGrantedPermissions())
|
|
53
30
|
}
|
|
54
31
|
|
|
55
|
-
|
|
32
|
+
fun mapPermissionResult(grantedPermissions: Set<String>): WritableNativeArray {
|
|
56
33
|
return WritableNativeArray().apply {
|
|
57
34
|
grantedPermissions.forEach {
|
|
58
35
|
val map = WritableNativeMap()
|
|
@@ -18,48 +18,48 @@ class ReactNutritionRecord : ReactHealthRecordImpl<NutritionRecord> {
|
|
|
18
18
|
endTime = Instant.parse(map.getString("endTime")),
|
|
19
19
|
startZoneOffset = null,
|
|
20
20
|
endZoneOffset = null,
|
|
21
|
-
biotin =
|
|
22
|
-
caffeine =
|
|
23
|
-
calcium =
|
|
24
|
-
energy =
|
|
25
|
-
energyFromFat =
|
|
26
|
-
chloride =
|
|
27
|
-
cholesterol =
|
|
28
|
-
chromium =
|
|
29
|
-
copper =
|
|
30
|
-
dietaryFiber =
|
|
31
|
-
folate =
|
|
32
|
-
folicAcid =
|
|
33
|
-
iodine =
|
|
34
|
-
iron =
|
|
35
|
-
magnesium =
|
|
36
|
-
manganese =
|
|
37
|
-
molybdenum =
|
|
38
|
-
monounsaturatedFat =
|
|
39
|
-
niacin =
|
|
40
|
-
pantothenicAcid =
|
|
41
|
-
phosphorus =
|
|
42
|
-
polyunsaturatedFat =
|
|
43
|
-
potassium =
|
|
44
|
-
protein =
|
|
45
|
-
riboflavin =
|
|
46
|
-
saturatedFat =
|
|
47
|
-
selenium =
|
|
48
|
-
sodium =
|
|
49
|
-
sugar =
|
|
50
|
-
thiamin =
|
|
51
|
-
totalCarbohydrate =
|
|
52
|
-
totalFat =
|
|
53
|
-
transFat =
|
|
54
|
-
unsaturatedFat =
|
|
55
|
-
vitaminA =
|
|
56
|
-
vitaminB12 =
|
|
57
|
-
vitaminB6 =
|
|
58
|
-
vitaminC =
|
|
59
|
-
vitaminD =
|
|
60
|
-
vitaminE =
|
|
61
|
-
vitaminK =
|
|
62
|
-
zinc =
|
|
21
|
+
biotin = map.getMap("biotin")?.let { getMassFromJsMap(it) },
|
|
22
|
+
caffeine = map.getMap("caffeine")?.let { getMassFromJsMap(it) },
|
|
23
|
+
calcium = map.getMap("calcium")?.let { getMassFromJsMap(it) },
|
|
24
|
+
energy = map.getMap("energy")?.let { getEnergyFromJsMap(it) },
|
|
25
|
+
energyFromFat = map.getMap("energyFromFat")?.let { getEnergyFromJsMap(it) },
|
|
26
|
+
chloride = map.getMap("chloride")?.let { getMassFromJsMap(it) },
|
|
27
|
+
cholesterol = map.getMap("cholesterol")?.let { getMassFromJsMap(it) },
|
|
28
|
+
chromium = map.getMap("chromium")?.let { getMassFromJsMap(it) },
|
|
29
|
+
copper = map.getMap("copper")?.let { getMassFromJsMap(it) },
|
|
30
|
+
dietaryFiber = map.getMap("dietaryFiber")?.let { getMassFromJsMap(it) },
|
|
31
|
+
folate = map.getMap("folate")?.let { getMassFromJsMap(it) },
|
|
32
|
+
folicAcid = map.getMap("folicAcid")?.let { getMassFromJsMap(it) },
|
|
33
|
+
iodine = map.getMap("iodine")?.let { getMassFromJsMap(it) },
|
|
34
|
+
iron = map.getMap("iron")?.let { getMassFromJsMap(it) },
|
|
35
|
+
magnesium = map.getMap("magnesium")?.let { getMassFromJsMap(it) },
|
|
36
|
+
manganese = map.getMap("manganese")?.let { getMassFromJsMap(it) },
|
|
37
|
+
molybdenum = map.getMap("molybdenum")?.let { getMassFromJsMap(it) },
|
|
38
|
+
monounsaturatedFat = map.getMap("monounsaturatedFat")?.let { getMassFromJsMap(it) },
|
|
39
|
+
niacin = map.getMap("niacin")?.let { getMassFromJsMap(it) },
|
|
40
|
+
pantothenicAcid = map.getMap("pantothenicAcid")?.let { getMassFromJsMap(it) },
|
|
41
|
+
phosphorus = map.getMap("phosphorus")?.let { getMassFromJsMap(it) },
|
|
42
|
+
polyunsaturatedFat = map.getMap("polyunsaturatedFat")?.let { getMassFromJsMap(it) },
|
|
43
|
+
potassium = map.getMap("potassium")?.let { getMassFromJsMap(it) },
|
|
44
|
+
protein = map.getMap("protein")?.let { getMassFromJsMap(it) },
|
|
45
|
+
riboflavin = map.getMap("riboflavin")?.let { getMassFromJsMap(it) },
|
|
46
|
+
saturatedFat = map.getMap("saturatedFat")?.let { getMassFromJsMap(it) },
|
|
47
|
+
selenium = map.getMap("selenium")?.let { getMassFromJsMap(it) },
|
|
48
|
+
sodium = map.getMap("sodium")?.let { getMassFromJsMap(it) },
|
|
49
|
+
sugar = map.getMap("sugar")?.let { getMassFromJsMap(it) },
|
|
50
|
+
thiamin = map.getMap("thiamin")?.let { getMassFromJsMap(it) },
|
|
51
|
+
totalCarbohydrate = map.getMap("totalCarbohydrate")?.let { getMassFromJsMap(it) },
|
|
52
|
+
totalFat = map.getMap("totalFat")?.let { getMassFromJsMap(it) },
|
|
53
|
+
transFat = map.getMap("transFat")?.let { getMassFromJsMap(it) },
|
|
54
|
+
unsaturatedFat = map.getMap("unsaturatedFat")?.let { getMassFromJsMap(it) },
|
|
55
|
+
vitaminA = map.getMap("vitaminA")?.let { getMassFromJsMap(it) },
|
|
56
|
+
vitaminB12 = map.getMap("vitaminB12")?.let { getMassFromJsMap(it) },
|
|
57
|
+
vitaminB6 = map.getMap("vitaminB6")?.let { getMassFromJsMap(it) },
|
|
58
|
+
vitaminC = map.getMap("vitaminC")?.let { getMassFromJsMap(it) },
|
|
59
|
+
vitaminD = map.getMap("vitaminD")?.let { getMassFromJsMap(it) },
|
|
60
|
+
vitaminE = map.getMap("vitaminE")?.let { getMassFromJsMap(it) },
|
|
61
|
+
vitaminK = map.getMap("vitaminK")?.let { getMassFromJsMap(it) },
|
|
62
|
+
zinc = map.getMap("zinc")?.let { getMassFromJsMap(it) },
|
|
63
63
|
name = map.getString("name"),
|
|
64
64
|
mealType = map.getSafeInt("mealType", MealType.MEAL_TYPE_UNKNOWN),
|
|
65
65
|
)
|
|
@@ -287,8 +287,8 @@ fun getEnergyFromJsMap(energyMap: ReadableMap?): Energy {
|
|
|
287
287
|
|
|
288
288
|
val value = energyMap.getDouble("value")
|
|
289
289
|
return when (energyMap.getString("unit")) {
|
|
290
|
-
"kilojoules" -> Energy.
|
|
291
|
-
"kilocalories" -> Energy.
|
|
290
|
+
"kilojoules" -> Energy.kilojoules(value)
|
|
291
|
+
"kilocalories" -> Energy.kilocalories(value)
|
|
292
292
|
"joules" -> Energy.joules(value)
|
|
293
293
|
"calories" -> Energy.calories(value)
|
|
294
294
|
else -> Energy.calories(value)
|