@solutionforest/android-health-connect 0.0.0-stage → 0.3.14
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/AndroidHealthConnect.podspec +20 -0
- package/CHANGELOG.md +211 -0
- package/LICENSE +20 -0
- package/README.md +849 -2
- package/SECURITY.md +99 -0
- package/android/build.gradle +104 -0
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/java/com/androidhealthconnect/AggregateMapper.kt +521 -0
- package/android/src/main/java/com/androidhealthconnect/AndroidHealthConnectModule.kt +637 -0
- package/android/src/main/java/com/androidhealthconnect/AndroidHealthConnectPackage.kt +31 -0
- package/android/src/main/java/com/androidhealthconnect/AvailabilitySupport.kt +149 -0
- package/android/src/main/java/com/androidhealthconnect/ChangesMapper.kt +105 -0
- package/android/src/main/java/com/androidhealthconnect/ExtendedRecordMapper.kt +665 -0
- package/android/src/main/java/com/androidhealthconnect/FeatureSupport.kt +88 -0
- package/android/src/main/java/com/androidhealthconnect/PhrMapper.kt +187 -0
- package/android/src/main/java/com/androidhealthconnect/PlannedExerciseMapper.kt +242 -0
- package/android/src/main/java/com/androidhealthconnect/RecordMapper.kt +1222 -0
- package/app.plugin.js +1 -0
- package/docs/assets/logo.jpg +0 -0
- package/ios/AndroidHealthConnect.h +5 -0
- package/ios/AndroidHealthConnect.mm +21 -0
- package/lib/module/NativeAndroidHealthConnect.js +5 -0
- package/lib/module/NativeAndroidHealthConnect.js.map +1 -0
- package/lib/module/foreground.js +117 -0
- package/lib/module/foreground.js.map +1 -0
- package/lib/module/helpers.js +280 -0
- package/lib/module/helpers.js.map +1 -0
- package/lib/module/index.js +354 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/types.js +392 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/upgrade.js +101 -0
- package/lib/module/upgrade.js.map +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/plugin/src/index.d.ts +14 -0
- package/lib/typescript/plugin/src/index.d.ts.map +1 -0
- package/lib/typescript/src/NativeAndroidHealthConnect.d.ts +95 -0
- package/lib/typescript/src/NativeAndroidHealthConnect.d.ts.map +1 -0
- package/lib/typescript/src/foreground.d.ts +43 -0
- package/lib/typescript/src/foreground.d.ts.map +1 -0
- package/lib/typescript/src/helpers.d.ts +108 -0
- package/lib/typescript/src/helpers.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +208 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/types.d.ts +1209 -0
- package/lib/typescript/src/types.d.ts.map +1 -0
- package/lib/typescript/src/upgrade.d.ts +26 -0
- package/lib/typescript/src/upgrade.d.ts.map +1 -0
- package/package.json +255 -4
- package/plugin/build/index.d.ts +13 -0
- package/plugin/build/index.js +57 -0
- package/plugin/src/index.ts +105 -0
- package/src/NativeAndroidHealthConnect.ts +133 -0
- package/src/foreground.ts +167 -0
- package/src/helpers.ts +373 -0
- package/src/index.tsx +623 -0
- package/src/types.ts +1525 -0
- package/src/upgrade.ts +155 -0
|
@@ -0,0 +1,637 @@
|
|
|
1
|
+
package com.androidhealthconnect
|
|
2
|
+
|
|
3
|
+
import android.app.Activity
|
|
4
|
+
import android.content.Intent
|
|
5
|
+
import android.net.Uri
|
|
6
|
+
import android.os.Build
|
|
7
|
+
import android.util.Log
|
|
8
|
+
import androidx.health.connect.client.ExperimentalMatchmakingApi
|
|
9
|
+
import androidx.health.connect.client.HealthConnectClient
|
|
10
|
+
import androidx.health.connect.client.PermissionController
|
|
11
|
+
import androidx.health.connect.client.contracts.ExerciseRouteRequestContract
|
|
12
|
+
import androidx.health.connect.client.feature.ExperimentalPersonalHealthRecordApi
|
|
13
|
+
import androidx.health.connect.client.matchmaking.MatchmakingRequest
|
|
14
|
+
import androidx.health.connect.client.records.ExerciseRoute
|
|
15
|
+
import androidx.health.connect.client.records.metadata.DataOrigin
|
|
16
|
+
import com.facebook.react.bridge.ActivityEventListener
|
|
17
|
+
import com.facebook.react.bridge.Arguments
|
|
18
|
+
import com.facebook.react.bridge.Promise
|
|
19
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
20
|
+
import com.facebook.react.bridge.ReadableArray
|
|
21
|
+
import kotlinx.coroutines.CoroutineScope
|
|
22
|
+
import kotlinx.coroutines.Dispatchers
|
|
23
|
+
import kotlinx.coroutines.SupervisorJob
|
|
24
|
+
import kotlinx.coroutines.cancel
|
|
25
|
+
import kotlinx.coroutines.launch
|
|
26
|
+
import org.json.JSONArray
|
|
27
|
+
import org.json.JSONObject
|
|
28
|
+
import java.time.Instant
|
|
29
|
+
|
|
30
|
+
@OptIn(ExperimentalMatchmakingApi::class, ExperimentalPersonalHealthRecordApi::class)
|
|
31
|
+
class AndroidHealthConnectModule(reactContext: ReactApplicationContext) :
|
|
32
|
+
NativeAndroidHealthConnectSpec(reactContext), ActivityEventListener {
|
|
33
|
+
|
|
34
|
+
companion object {
|
|
35
|
+
const val NAME = NativeAndroidHealthConnectSpec.NAME
|
|
36
|
+
private const val PERMISSION_REQUEST_CODE = 21432
|
|
37
|
+
private const val MATCHMAKING_REQUEST_CODE = 21433
|
|
38
|
+
private const val EXERCISE_ROUTE_REQUEST_CODE = 21434
|
|
39
|
+
private const val TAG = "AndroidHealthConnect"
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
private val moduleScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
|
43
|
+
|
|
44
|
+
/** Lazily obtained once we know HC is available. */
|
|
45
|
+
private val healthConnectClient by lazy {
|
|
46
|
+
HealthConnectClient.getOrCreate(reactApplicationContext)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Contract used to build the permissions intent and parse its result. */
|
|
50
|
+
private val requestPermissionsContract =
|
|
51
|
+
PermissionController.createRequestPermissionResultContract()
|
|
52
|
+
|
|
53
|
+
private val exerciseRouteRequestContract = ExerciseRouteRequestContract()
|
|
54
|
+
|
|
55
|
+
/** Pending promise for an in-flight permission request. */
|
|
56
|
+
@Volatile
|
|
57
|
+
private var pendingPermissionPromise: Promise? = null
|
|
58
|
+
|
|
59
|
+
/** Pending promise for an in-flight matchmaking flow. */
|
|
60
|
+
@Volatile
|
|
61
|
+
private var pendingMatchmakingPromise: Promise? = null
|
|
62
|
+
|
|
63
|
+
/** Pending promise for an in-flight exercise-route consent flow. */
|
|
64
|
+
@Volatile
|
|
65
|
+
private var pendingExerciseRoutePromise: Promise? = null
|
|
66
|
+
|
|
67
|
+
init {
|
|
68
|
+
reactContext.addActivityEventListener(this)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// ─── Availability ──────────────────────────────────────────────────────────
|
|
72
|
+
|
|
73
|
+
override fun isHealthConnectAvailable(promise: Promise) {
|
|
74
|
+
val status = HealthConnectClient.getSdkStatus(reactApplicationContext)
|
|
75
|
+
promise.resolve(AvailabilitySupport.mapStatus(status))
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
override fun getHealthConnectAvailability(promise: Promise) {
|
|
79
|
+
val ctx = reactApplicationContext
|
|
80
|
+
val sdkStatus = HealthConnectClient.getSdkStatus(ctx)
|
|
81
|
+
val json =
|
|
82
|
+
AvailabilitySupport.toJson(
|
|
83
|
+
sdkStatus = sdkStatus,
|
|
84
|
+
provider = AvailabilitySupport.providerKind(),
|
|
85
|
+
googlePlayServicesAvailable =
|
|
86
|
+
AvailabilitySupport.isPackageInstalled(ctx, AvailabilitySupport.GMS_PACKAGE),
|
|
87
|
+
playStoreAvailable =
|
|
88
|
+
AvailabilitySupport.isPackageInstalled(ctx, AvailabilitySupport.PLAY_STORE_PACKAGE),
|
|
89
|
+
androidSdkInt = Build.VERSION.SDK_INT,
|
|
90
|
+
)
|
|
91
|
+
promise.resolve(json.toString())
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// ─── Permissions ───────────────────────────────────────────────────────────
|
|
95
|
+
|
|
96
|
+
override fun requestPermissions(permissions: ReadableArray, promise: Promise) {
|
|
97
|
+
val activity = reactApplicationContext.currentActivity
|
|
98
|
+
if (activity == null) {
|
|
99
|
+
promise.reject("NO_ACTIVITY", "No foreground Activity available")
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
if (!isAvailable(promise)) return
|
|
103
|
+
|
|
104
|
+
pendingPermissionPromise = promise
|
|
105
|
+
val permSet = permissions.toArrayList().map { it.toString() }.toSet()
|
|
106
|
+
val intent = requestPermissionsContract.createIntent(activity, permSet)
|
|
107
|
+
activity.startActivityForResult(intent, PERMISSION_REQUEST_CODE)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
override fun checkPermissions(permissions: ReadableArray, promise: Promise) {
|
|
111
|
+
if (!isAvailable(promise)) return
|
|
112
|
+
moduleScope.launch {
|
|
113
|
+
try {
|
|
114
|
+
val granted = healthConnectClient.permissionController.getGrantedPermissions()
|
|
115
|
+
val requested = permissions.toArrayList().map { it.toString() }
|
|
116
|
+
val result = Arguments.createArray()
|
|
117
|
+
requested.filter { granted.contains(it) }.forEach { result.pushString(it) }
|
|
118
|
+
promise.resolve(result)
|
|
119
|
+
} catch (e: Exception) {
|
|
120
|
+
promise.reject("CHECK_PERMISSIONS_ERROR", e.message, e)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
override fun revokeAllPermissions(promise: Promise) {
|
|
126
|
+
if (!isAvailable(promise)) return
|
|
127
|
+
moduleScope.launch {
|
|
128
|
+
try {
|
|
129
|
+
healthConnectClient.permissionController.revokeAllPermissions()
|
|
130
|
+
promise.resolve(true)
|
|
131
|
+
} catch (e: Exception) {
|
|
132
|
+
promise.reject("REVOKE_PERMISSIONS_ERROR", e.message, e)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// ─── Navigation ────────────────────────────────────────────────────────────
|
|
138
|
+
|
|
139
|
+
override fun openHealthConnectSettings() {
|
|
140
|
+
val intent = Intent("androidx.health.ACTION_HEALTH_CONNECT_SETTINGS").apply {
|
|
141
|
+
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
142
|
+
}
|
|
143
|
+
startSafeActivity(intent)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
override fun openHealthConnectDataManagement(packageName: String) {
|
|
147
|
+
val intent = Intent("androidx.health.ACTION_MANAGE_HEALTH_PERMISSIONS").apply {
|
|
148
|
+
if (packageName.isNotEmpty()) {
|
|
149
|
+
putExtra(Intent.EXTRA_PACKAGE_NAME, packageName)
|
|
150
|
+
}
|
|
151
|
+
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
152
|
+
}
|
|
153
|
+
startSafeActivity(intent)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
override fun openHealthConnectApp() {
|
|
157
|
+
val ctx = reactApplicationContext
|
|
158
|
+
val status = HealthConnectClient.getSdkStatus(ctx)
|
|
159
|
+
if (status == HealthConnectClient.SDK_UNAVAILABLE_PROVIDER_UPDATE_REQUIRED) {
|
|
160
|
+
val hasPlayStore =
|
|
161
|
+
AvailabilitySupport.isPackageInstalled(ctx, AvailabilitySupport.PLAY_STORE_PACKAGE)
|
|
162
|
+
if (hasPlayStore) {
|
|
163
|
+
// Official sample: market:// via Play Store package, with HTTPS fallback.
|
|
164
|
+
val market =
|
|
165
|
+
Intent(Intent.ACTION_VIEW, Uri.parse(AvailabilitySupport.HC_PLAY_MARKET)).apply {
|
|
166
|
+
setPackage(AvailabilitySupport.PLAY_STORE_PACKAGE)
|
|
167
|
+
putExtra("overlay", true)
|
|
168
|
+
putExtra("callerId", ctx.packageName)
|
|
169
|
+
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
170
|
+
}
|
|
171
|
+
if (startSafeActivity(market)) {
|
|
172
|
+
return
|
|
173
|
+
}
|
|
174
|
+
startSafeActivity(
|
|
175
|
+
Intent(Intent.ACTION_VIEW, Uri.parse(AvailabilitySupport.HC_PLAY_HTTPS)).apply {
|
|
176
|
+
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
177
|
+
}
|
|
178
|
+
)
|
|
179
|
+
return
|
|
180
|
+
}
|
|
181
|
+
// No Play Store (common on mainland China / some HMS devices).
|
|
182
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
|
|
183
|
+
startSafeActivity(healthConnectSettingsIntent())
|
|
184
|
+
} else {
|
|
185
|
+
Log.w(
|
|
186
|
+
TAG,
|
|
187
|
+
"Health Connect install required but Play Store is missing (no GMS / China ROM)."
|
|
188
|
+
)
|
|
189
|
+
}
|
|
190
|
+
return
|
|
191
|
+
}
|
|
192
|
+
startSafeActivity(healthConnectSettingsIntent())
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// ─── Records ───────────────────────────────────────────────────────────────
|
|
196
|
+
|
|
197
|
+
override fun readRecords(recordType: String, options: String, promise: Promise) {
|
|
198
|
+
if (!isAvailable(promise)) return
|
|
199
|
+
moduleScope.launch {
|
|
200
|
+
try {
|
|
201
|
+
val result = RecordMapper.readRecords(
|
|
202
|
+
healthConnectClient, recordType, JSONObject(options)
|
|
203
|
+
)
|
|
204
|
+
promise.resolve(result.toString())
|
|
205
|
+
} catch (e: Exception) {
|
|
206
|
+
Log.e(TAG, "readRecords error", e)
|
|
207
|
+
promise.reject("READ_RECORDS_ERROR", e.message, e)
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
override fun writeRecords(records: String, promise: Promise) {
|
|
213
|
+
if (!isAvailable(promise)) return
|
|
214
|
+
moduleScope.launch {
|
|
215
|
+
try {
|
|
216
|
+
val arr = JSONArray(records)
|
|
217
|
+
val rnRecords = (0 until arr.length()).map { i ->
|
|
218
|
+
RecordMapper.jsonToRecord(arr.getJSONObject(i))
|
|
219
|
+
}
|
|
220
|
+
val response = healthConnectClient.insertRecords(rnRecords)
|
|
221
|
+
val result = JSONObject().apply {
|
|
222
|
+
put("recordIdsList", org.json.JSONArray(response.recordIdsList))
|
|
223
|
+
}
|
|
224
|
+
promise.resolve(result.toString())
|
|
225
|
+
} catch (e: Exception) {
|
|
226
|
+
Log.e(TAG, "writeRecords error", e)
|
|
227
|
+
promise.reject("WRITE_RECORDS_ERROR", e.message, e)
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
override fun updateRecords(records: String, promise: Promise) {
|
|
233
|
+
if (!isAvailable(promise)) return
|
|
234
|
+
moduleScope.launch {
|
|
235
|
+
try {
|
|
236
|
+
val arr = JSONArray(records)
|
|
237
|
+
val rnRecords =
|
|
238
|
+
(0 until arr.length()).map { i ->
|
|
239
|
+
val json = arr.getJSONObject(i)
|
|
240
|
+
RecordMapper.requireMetadataIdForUpdate(json)
|
|
241
|
+
RecordMapper.jsonToRecord(json)
|
|
242
|
+
}
|
|
243
|
+
healthConnectClient.updateRecords(rnRecords)
|
|
244
|
+
promise.resolve(true)
|
|
245
|
+
} catch (e: Exception) {
|
|
246
|
+
Log.e(TAG, "updateRecords error", e)
|
|
247
|
+
promise.reject("UPDATE_RECORDS_ERROR", e.message, e)
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
override fun deleteRecordsByTime(
|
|
253
|
+
recordType: String,
|
|
254
|
+
startTime: String,
|
|
255
|
+
endTime: String,
|
|
256
|
+
promise: Promise,
|
|
257
|
+
) {
|
|
258
|
+
if (!isAvailable(promise)) return
|
|
259
|
+
moduleScope.launch {
|
|
260
|
+
try {
|
|
261
|
+
val filter = androidx.health.connect.client.time.TimeRangeFilter.between(
|
|
262
|
+
Instant.parse(startTime), Instant.parse(endTime)
|
|
263
|
+
)
|
|
264
|
+
RecordMapper.deleteByTimeRange(healthConnectClient, recordType, filter)
|
|
265
|
+
promise.resolve(true)
|
|
266
|
+
} catch (e: Exception) {
|
|
267
|
+
Log.e(TAG, "deleteRecordsByTime error", e)
|
|
268
|
+
promise.reject("DELETE_RECORDS_ERROR", e.message, e)
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
override fun deleteRecordsById(
|
|
274
|
+
recordType: String,
|
|
275
|
+
recordIds: ReadableArray,
|
|
276
|
+
promise: Promise,
|
|
277
|
+
) {
|
|
278
|
+
if (!isAvailable(promise)) return
|
|
279
|
+
moduleScope.launch {
|
|
280
|
+
try {
|
|
281
|
+
val ids = recordIds.toArrayList().map { it.toString() }
|
|
282
|
+
RecordMapper.deleteByIds(healthConnectClient, recordType, ids)
|
|
283
|
+
promise.resolve(true)
|
|
284
|
+
} catch (e: Exception) {
|
|
285
|
+
Log.e(TAG, "deleteRecordsById error", e)
|
|
286
|
+
promise.reject("DELETE_RECORDS_ERROR", e.message, e)
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// ─── Aggregates ────────────────────────────────────────────────────────────
|
|
292
|
+
|
|
293
|
+
override fun aggregateRecord(options: String, promise: Promise) {
|
|
294
|
+
if (!isAvailable(promise)) return
|
|
295
|
+
moduleScope.launch {
|
|
296
|
+
try {
|
|
297
|
+
val result = AggregateMapper.aggregate(healthConnectClient, JSONObject(options))
|
|
298
|
+
promise.resolve(result.toString())
|
|
299
|
+
} catch (e: Exception) {
|
|
300
|
+
Log.e(TAG, "aggregateRecord error", e)
|
|
301
|
+
promise.reject("AGGREGATE_ERROR", e.message, e)
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
override fun aggregateGroupByDuration(options: String, promise: Promise) {
|
|
307
|
+
if (!isAvailable(promise)) return
|
|
308
|
+
moduleScope.launch {
|
|
309
|
+
try {
|
|
310
|
+
val result =
|
|
311
|
+
AggregateMapper.aggregateGroupByDuration(healthConnectClient, JSONObject(options))
|
|
312
|
+
promise.resolve(result.toString())
|
|
313
|
+
} catch (e: Exception) {
|
|
314
|
+
Log.e(TAG, "aggregateGroupByDuration error", e)
|
|
315
|
+
promise.reject("AGGREGATE_DURATION_ERROR", e.message, e)
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
override fun aggregateGroupByPeriod(options: String, promise: Promise) {
|
|
321
|
+
if (!isAvailable(promise)) return
|
|
322
|
+
moduleScope.launch {
|
|
323
|
+
try {
|
|
324
|
+
val result =
|
|
325
|
+
AggregateMapper.aggregateGroupByPeriod(healthConnectClient, JSONObject(options))
|
|
326
|
+
promise.resolve(result.toString())
|
|
327
|
+
} catch (e: Exception) {
|
|
328
|
+
Log.e(TAG, "aggregateGroupByPeriod error", e)
|
|
329
|
+
promise.reject("AGGREGATE_PERIOD_ERROR", e.message, e)
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// ─── Changelog / changes token ────────────────────────────────────────────
|
|
335
|
+
|
|
336
|
+
override fun getChangesToken(options: String, promise: Promise) {
|
|
337
|
+
if (!isAvailable(promise)) return
|
|
338
|
+
moduleScope.launch {
|
|
339
|
+
try {
|
|
340
|
+
val token = ChangesMapper.getChangesToken(healthConnectClient, JSONObject(options))
|
|
341
|
+
promise.resolve(token)
|
|
342
|
+
} catch (e: Exception) {
|
|
343
|
+
Log.e(TAG, "getChangesToken error", e)
|
|
344
|
+
promise.reject("CHANGES_TOKEN_ERROR", e.message, e)
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
override fun getChanges(changesToken: String, options: String, promise: Promise) {
|
|
350
|
+
if (!isAvailable(promise)) return
|
|
351
|
+
moduleScope.launch {
|
|
352
|
+
try {
|
|
353
|
+
val opts = if (options.isBlank()) JSONObject() else JSONObject(options)
|
|
354
|
+
val result = ChangesMapper.getChanges(healthConnectClient, changesToken, opts)
|
|
355
|
+
promise.resolve(result.toString())
|
|
356
|
+
} catch (e: Exception) {
|
|
357
|
+
Log.e(TAG, "getChanges error", e)
|
|
358
|
+
promise.reject("CHANGES_ERROR", e.message, e)
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// ─── Exercise route consent ───────────────────────────────────────────────
|
|
364
|
+
|
|
365
|
+
override fun requestExerciseRoute(recordId: String, promise: Promise) {
|
|
366
|
+
val activity = reactApplicationContext.currentActivity
|
|
367
|
+
if (activity == null) {
|
|
368
|
+
promise.reject("NO_ACTIVITY", "No foreground Activity available")
|
|
369
|
+
return
|
|
370
|
+
}
|
|
371
|
+
if (!isAvailable(promise)) return
|
|
372
|
+
if (recordId.isBlank()) {
|
|
373
|
+
promise.reject("INVALID_RECORD_ID", "Exercise session recordId must not be empty")
|
|
374
|
+
return
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
pendingExerciseRoutePromise = promise
|
|
378
|
+
try {
|
|
379
|
+
val intent = exerciseRouteRequestContract.createIntent(activity, recordId)
|
|
380
|
+
activity.startActivityForResult(intent, EXERCISE_ROUTE_REQUEST_CODE)
|
|
381
|
+
} catch (e: Exception) {
|
|
382
|
+
pendingExerciseRoutePromise = null
|
|
383
|
+
Log.e(TAG, "requestExerciseRoute error", e)
|
|
384
|
+
promise.reject("EXERCISE_ROUTE_ERROR", e.message, e)
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// ─── Feature availability ─────────────────────────────────────────────────
|
|
389
|
+
|
|
390
|
+
override fun getFeatureStatus(feature: String, promise: Promise) {
|
|
391
|
+
if (!isAvailable(promise)) return
|
|
392
|
+
moduleScope.launch {
|
|
393
|
+
try {
|
|
394
|
+
promise.resolve(FeatureSupport.getStatus(healthConnectClient, feature))
|
|
395
|
+
} catch (e: Exception) {
|
|
396
|
+
promise.reject("FEATURE_STATUS_ERROR", e.message, e)
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// ─── Matchmaking ──────────────────────────────────────────────────────────
|
|
402
|
+
|
|
403
|
+
override fun checkIfMatchmakingIsPossible(options: String, promise: Promise) {
|
|
404
|
+
if (!isAvailable(promise)) return
|
|
405
|
+
moduleScope.launch {
|
|
406
|
+
try {
|
|
407
|
+
val request = parseMatchmakingRequest(JSONObject(options))
|
|
408
|
+
val response = healthConnectClient.checkIfMatchmakingIsPossible(request)
|
|
409
|
+
promise.resolve(
|
|
410
|
+
JSONObject().put("isMatchmakingPossible", response.isMatchmakingPossible).toString()
|
|
411
|
+
)
|
|
412
|
+
} catch (e: Exception) {
|
|
413
|
+
Log.e(TAG, "checkIfMatchmakingIsPossible error", e)
|
|
414
|
+
promise.reject("MATCHMAKING_ERROR", e.message, e)
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
override fun launchMatchmaking(options: String, promise: Promise) {
|
|
420
|
+
val activity = reactApplicationContext.currentActivity
|
|
421
|
+
if (activity == null) {
|
|
422
|
+
promise.reject("NO_ACTIVITY", "No foreground Activity available")
|
|
423
|
+
return
|
|
424
|
+
}
|
|
425
|
+
if (!isAvailable(promise)) return
|
|
426
|
+
moduleScope.launch {
|
|
427
|
+
try {
|
|
428
|
+
val request = parseMatchmakingRequest(JSONObject(options))
|
|
429
|
+
val intent = healthConnectClient.createMatchmakingIntent(request)
|
|
430
|
+
pendingMatchmakingPromise = promise
|
|
431
|
+
activity.runOnUiThread {
|
|
432
|
+
activity.startActivityForResult(intent, MATCHMAKING_REQUEST_CODE)
|
|
433
|
+
}
|
|
434
|
+
} catch (e: Exception) {
|
|
435
|
+
Log.e(TAG, "launchMatchmaking error", e)
|
|
436
|
+
promise.reject("MATCHMAKING_ERROR", e.message, e)
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
// ─── Personal Health Record ───────────────────────────────────────────────
|
|
442
|
+
|
|
443
|
+
override fun createMedicalDataSource(request: String, promise: Promise) {
|
|
444
|
+
if (!isAvailable(promise)) return
|
|
445
|
+
moduleScope.launch {
|
|
446
|
+
try {
|
|
447
|
+
val source = healthConnectClient.createMedicalDataSource(
|
|
448
|
+
PhrMapper.createRequestFromJson(JSONObject(request))
|
|
449
|
+
)
|
|
450
|
+
promise.resolve(PhrMapper.medicalDataSourceToJson(source).toString())
|
|
451
|
+
} catch (e: Exception) {
|
|
452
|
+
Log.e(TAG, "createMedicalDataSource error", e)
|
|
453
|
+
promise.reject("PHR_ERROR", e.message, e)
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
override fun deleteMedicalDataSourceWithData(id: String, promise: Promise) {
|
|
459
|
+
if (!isAvailable(promise)) return
|
|
460
|
+
moduleScope.launch {
|
|
461
|
+
try {
|
|
462
|
+
healthConnectClient.deleteMedicalDataSourceWithData(id)
|
|
463
|
+
promise.resolve(true)
|
|
464
|
+
} catch (e: Exception) {
|
|
465
|
+
Log.e(TAG, "deleteMedicalDataSourceWithData error", e)
|
|
466
|
+
promise.reject("PHR_ERROR", e.message, e)
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
override fun getMedicalDataSources(request: String, promise: Promise) {
|
|
472
|
+
if (!isAvailable(promise)) return
|
|
473
|
+
moduleScope.launch {
|
|
474
|
+
try {
|
|
475
|
+
val result = PhrMapper.getMedicalDataSources(
|
|
476
|
+
healthConnectClient, JSONObject(request.ifEmpty { "{}" })
|
|
477
|
+
)
|
|
478
|
+
promise.resolve(result.toString())
|
|
479
|
+
} catch (e: Exception) {
|
|
480
|
+
Log.e(TAG, "getMedicalDataSources error", e)
|
|
481
|
+
promise.reject("PHR_ERROR", e.message, e)
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
override fun upsertMedicalResources(requests: String, promise: Promise) {
|
|
487
|
+
if (!isAvailable(promise)) return
|
|
488
|
+
moduleScope.launch {
|
|
489
|
+
try {
|
|
490
|
+
val upserted = healthConnectClient.upsertMedicalResources(
|
|
491
|
+
PhrMapper.upsertRequestsFromJson(JSONArray(requests))
|
|
492
|
+
)
|
|
493
|
+
val arr = JSONArray().apply {
|
|
494
|
+
upserted.forEach { put(PhrMapper.medicalResourceToJson(it)) }
|
|
495
|
+
}
|
|
496
|
+
promise.resolve(arr.toString())
|
|
497
|
+
} catch (e: Exception) {
|
|
498
|
+
Log.e(TAG, "upsertMedicalResources error", e)
|
|
499
|
+
promise.reject("PHR_ERROR", e.message, e)
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
override fun readMedicalResources(request: String, promise: Promise) {
|
|
505
|
+
if (!isAvailable(promise)) return
|
|
506
|
+
moduleScope.launch {
|
|
507
|
+
try {
|
|
508
|
+
val result = PhrMapper.readMedicalResources(
|
|
509
|
+
healthConnectClient, JSONObject(request)
|
|
510
|
+
)
|
|
511
|
+
promise.resolve(result.toString())
|
|
512
|
+
} catch (e: Exception) {
|
|
513
|
+
Log.e(TAG, "readMedicalResources error", e)
|
|
514
|
+
promise.reject("PHR_ERROR", e.message, e)
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
override fun deleteMedicalResources(request: String, promise: Promise) {
|
|
520
|
+
if (!isAvailable(promise)) return
|
|
521
|
+
moduleScope.launch {
|
|
522
|
+
try {
|
|
523
|
+
PhrMapper.deleteMedicalResources(healthConnectClient, JSONObject(request))
|
|
524
|
+
promise.resolve(true)
|
|
525
|
+
} catch (e: Exception) {
|
|
526
|
+
Log.e(TAG, "deleteMedicalResources error", e)
|
|
527
|
+
promise.reject("PHR_ERROR", e.message, e)
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
// ─── ActivityEventListener ────────────────────────────────────────────────
|
|
533
|
+
|
|
534
|
+
override fun onActivityResult(
|
|
535
|
+
activity: Activity,
|
|
536
|
+
requestCode: Int,
|
|
537
|
+
resultCode: Int,
|
|
538
|
+
data: Intent?,
|
|
539
|
+
) {
|
|
540
|
+
when (requestCode) {
|
|
541
|
+
PERMISSION_REQUEST_CODE -> {
|
|
542
|
+
val granted = requestPermissionsContract.parseResult(resultCode, data)
|
|
543
|
+
val array = Arguments.createArray()
|
|
544
|
+
granted.forEach { array.pushString(it) }
|
|
545
|
+
pendingPermissionPromise?.resolve(array)
|
|
546
|
+
pendingPermissionPromise = null
|
|
547
|
+
}
|
|
548
|
+
MATCHMAKING_REQUEST_CODE -> {
|
|
549
|
+
pendingMatchmakingPromise?.resolve(resultCode == Activity.RESULT_OK)
|
|
550
|
+
pendingMatchmakingPromise = null
|
|
551
|
+
}
|
|
552
|
+
EXERCISE_ROUTE_REQUEST_CODE -> {
|
|
553
|
+
val route: ExerciseRoute? =
|
|
554
|
+
exerciseRouteRequestContract.parseResult(resultCode, data)
|
|
555
|
+
if (route != null) {
|
|
556
|
+
pendingExerciseRoutePromise?.resolve(
|
|
557
|
+
RecordMapper.exerciseRouteDataToJson(route).toString()
|
|
558
|
+
)
|
|
559
|
+
} else {
|
|
560
|
+
// Denied or no route — empty string so JS maps to null.
|
|
561
|
+
pendingExerciseRoutePromise?.resolve("")
|
|
562
|
+
}
|
|
563
|
+
pendingExerciseRoutePromise = null
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
override fun onNewIntent(intent: Intent) {}
|
|
569
|
+
|
|
570
|
+
// ─── Lifecycle ────────────────────────────────────────────────────────────
|
|
571
|
+
|
|
572
|
+
override fun invalidate() {
|
|
573
|
+
reactApplicationContext.removeActivityEventListener(this)
|
|
574
|
+
moduleScope.cancel()
|
|
575
|
+
super.invalidate()
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
// ─── Private helpers ──────────────────────────────────────────────────────
|
|
579
|
+
|
|
580
|
+
private fun isAvailable(promise: Promise): Boolean {
|
|
581
|
+
val ctx = reactApplicationContext
|
|
582
|
+
val status = HealthConnectClient.getSdkStatus(ctx)
|
|
583
|
+
if (status != HealthConnectClient.SDK_AVAILABLE) {
|
|
584
|
+
val message =
|
|
585
|
+
AvailabilitySupport.unavailableRejectMessage(
|
|
586
|
+
sdkStatus = status,
|
|
587
|
+
googlePlayServicesAvailable =
|
|
588
|
+
AvailabilitySupport.isPackageInstalled(ctx, AvailabilitySupport.GMS_PACKAGE),
|
|
589
|
+
playStoreAvailable =
|
|
590
|
+
AvailabilitySupport.isPackageInstalled(ctx, AvailabilitySupport.PLAY_STORE_PACKAGE),
|
|
591
|
+
)
|
|
592
|
+
promise.reject("HEALTH_CONNECT_UNAVAILABLE", message)
|
|
593
|
+
return false
|
|
594
|
+
}
|
|
595
|
+
return true
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
private fun healthConnectSettingsIntent(): Intent =
|
|
599
|
+
Intent("androidx.health.ACTION_HEALTH_CONNECT_SETTINGS").apply {
|
|
600
|
+
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/** @return true when an activity was started. */
|
|
604
|
+
private fun startSafeActivity(intent: Intent): Boolean {
|
|
605
|
+
return try {
|
|
606
|
+
reactApplicationContext.startActivity(intent)
|
|
607
|
+
true
|
|
608
|
+
} catch (e: Exception) {
|
|
609
|
+
Log.e(TAG, "Could not start activity: ${e.message}")
|
|
610
|
+
false
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
private fun parseMatchmakingRequest(options: JSONObject): MatchmakingRequest {
|
|
615
|
+
val recordTypesArr = options.optJSONArray("recordTypes")
|
|
616
|
+
val recordTypes =
|
|
617
|
+
if (recordTypesArr != null) {
|
|
618
|
+
(0 until recordTypesArr.length())
|
|
619
|
+
.map { FeatureSupport.recordClassForType(recordTypesArr.getString(it)) }
|
|
620
|
+
.toSet()
|
|
621
|
+
} else {
|
|
622
|
+
emptySet()
|
|
623
|
+
}
|
|
624
|
+
val included = parseDataOrigins(options.optJSONArray("includedDataSources"))
|
|
625
|
+
val excluded = parseDataOrigins(options.optJSONArray("excludedDataSources"))
|
|
626
|
+
return MatchmakingRequest(
|
|
627
|
+
recordTypes = recordTypes,
|
|
628
|
+
includedDataSources = included,
|
|
629
|
+
excludedDataSources = excluded,
|
|
630
|
+
)
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
private fun parseDataOrigins(arr: JSONArray?): Set<DataOrigin> {
|
|
634
|
+
if (arr == null) return emptySet()
|
|
635
|
+
return (0 until arr.length()).map { DataOrigin(arr.getString(it)) }.toSet()
|
|
636
|
+
}
|
|
637
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
package com.androidhealthconnect
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.BaseReactPackage
|
|
4
|
+
import com.facebook.react.bridge.NativeModule
|
|
5
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.module.model.ReactModuleInfo
|
|
7
|
+
import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
8
|
+
import java.util.HashMap
|
|
9
|
+
|
|
10
|
+
class AndroidHealthConnectPackage : BaseReactPackage() {
|
|
11
|
+
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
|
|
12
|
+
return if (name == AndroidHealthConnectModule.NAME) {
|
|
13
|
+
AndroidHealthConnectModule(reactContext)
|
|
14
|
+
} else {
|
|
15
|
+
null
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
override fun getReactModuleInfoProvider() = ReactModuleInfoProvider {
|
|
20
|
+
mapOf(
|
|
21
|
+
AndroidHealthConnectModule.NAME to ReactModuleInfo(
|
|
22
|
+
name = AndroidHealthConnectModule.NAME,
|
|
23
|
+
className = AndroidHealthConnectModule.NAME,
|
|
24
|
+
canOverrideExistingModule = false,
|
|
25
|
+
needsEagerInit = false,
|
|
26
|
+
isCxxModule = false,
|
|
27
|
+
isTurboModule = true
|
|
28
|
+
)
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
}
|