@capgo/capacitor-health 7.2.13 → 7.2.15
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.
|
@@ -2,14 +2,16 @@ package app.capgo.plugin.health
|
|
|
2
2
|
|
|
3
3
|
import android.app.Activity
|
|
4
4
|
import android.content.Intent
|
|
5
|
+
import androidx.activity.result.ActivityResult
|
|
5
6
|
import com.getcapacitor.JSArray
|
|
6
7
|
import com.getcapacitor.JSObject
|
|
7
8
|
import com.getcapacitor.Plugin
|
|
8
9
|
import com.getcapacitor.PluginCall
|
|
9
10
|
import com.getcapacitor.PluginMethod
|
|
11
|
+
import com.getcapacitor.annotation.ActivityCallback
|
|
10
12
|
import com.getcapacitor.annotation.CapacitorPlugin
|
|
11
13
|
import androidx.health.connect.client.HealthConnectClient
|
|
12
|
-
import androidx.health.connect.client.
|
|
14
|
+
import androidx.health.connect.client.PermissionController
|
|
13
15
|
import java.time.Instant
|
|
14
16
|
import java.time.Duration
|
|
15
17
|
import java.time.format.DateTimeParseException
|
|
@@ -18,15 +20,17 @@ import kotlinx.coroutines.Dispatchers
|
|
|
18
20
|
import kotlinx.coroutines.SupervisorJob
|
|
19
21
|
import kotlinx.coroutines.cancel
|
|
20
22
|
import kotlinx.coroutines.launch
|
|
21
|
-
import kotlinx.coroutines.withContext
|
|
22
23
|
|
|
23
24
|
@CapacitorPlugin(name = "Health")
|
|
24
25
|
class HealthPlugin : Plugin() {
|
|
25
|
-
private val pluginVersion = "7.2.
|
|
26
|
+
private val pluginVersion = "7.2.14"
|
|
26
27
|
private val manager = HealthManager()
|
|
27
28
|
private val pluginScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
|
|
28
|
-
private
|
|
29
|
-
|
|
29
|
+
private val permissionContract = PermissionController.createRequestPermissionResultContract()
|
|
30
|
+
|
|
31
|
+
// Store pending request data for callback
|
|
32
|
+
private var pendingReadTypes: List<HealthDataType> = emptyList()
|
|
33
|
+
private var pendingWriteTypes: List<HealthDataType> = emptyList()
|
|
30
34
|
|
|
31
35
|
override fun handleOnDestroy() {
|
|
32
36
|
super.handleOnDestroy()
|
|
@@ -72,35 +76,41 @@ class HealthPlugin : Plugin() {
|
|
|
72
76
|
return@launch
|
|
73
77
|
}
|
|
74
78
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return@launch
|
|
79
|
-
}
|
|
79
|
+
// Store types for callback
|
|
80
|
+
pendingReadTypes = readTypes
|
|
81
|
+
pendingWriteTypes = writeTypes
|
|
80
82
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
return@launch
|
|
84
|
-
}
|
|
83
|
+
// Create intent using the Health Connect permission contract
|
|
84
|
+
val intent = permissionContract.createIntent(context, permissions)
|
|
85
85
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
withContext(Dispatchers.Main) {
|
|
93
|
-
try {
|
|
94
|
-
activity.startActivityForResult(intent, REQUEST_AUTHORIZATION)
|
|
95
|
-
} catch (e: Exception) {
|
|
96
|
-
pendingAuthorization = null
|
|
97
|
-
call.setKeepAlive(false)
|
|
98
|
-
call.reject("Failed to launch Health Connect permission request.", null, e)
|
|
99
|
-
}
|
|
86
|
+
try {
|
|
87
|
+
startActivityForResult(call, intent, "handlePermissionResult")
|
|
88
|
+
} catch (e: Exception) {
|
|
89
|
+
pendingReadTypes = emptyList()
|
|
90
|
+
pendingWriteTypes = emptyList()
|
|
91
|
+
call.reject("Failed to launch Health Connect permission request.", null, e)
|
|
100
92
|
}
|
|
101
93
|
}
|
|
102
94
|
}
|
|
103
95
|
|
|
96
|
+
@ActivityCallback
|
|
97
|
+
private fun handlePermissionResult(call: PluginCall?, result: ActivityResult) {
|
|
98
|
+
if (call == null) {
|
|
99
|
+
return
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
val readTypes = pendingReadTypes
|
|
103
|
+
val writeTypes = pendingWriteTypes
|
|
104
|
+
pendingReadTypes = emptyList()
|
|
105
|
+
pendingWriteTypes = emptyList()
|
|
106
|
+
|
|
107
|
+
pluginScope.launch {
|
|
108
|
+
val client = getClientOrReject(call) ?: return@launch
|
|
109
|
+
val status = manager.authorizationStatus(client, readTypes, writeTypes)
|
|
110
|
+
call.resolve(status)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
104
114
|
@PluginMethod
|
|
105
115
|
fun checkAuthorization(call: PluginCall) {
|
|
106
116
|
val readTypes = try {
|
|
@@ -242,28 +252,6 @@ class HealthPlugin : Plugin() {
|
|
|
242
252
|
}
|
|
243
253
|
}
|
|
244
254
|
|
|
245
|
-
override fun handleOnActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
246
|
-
super.handleOnActivityResult(requestCode, resultCode, data)
|
|
247
|
-
if (requestCode != REQUEST_AUTHORIZATION) {
|
|
248
|
-
return
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
val pending = pendingAuthorization ?: return
|
|
252
|
-
pendingAuthorization = null
|
|
253
|
-
pending.call.setKeepAlive(false)
|
|
254
|
-
|
|
255
|
-
pluginScope.launch {
|
|
256
|
-
val client = getClientOrReject(pending.call) ?: return@launch
|
|
257
|
-
if (resultCode != Activity.RESULT_OK) {
|
|
258
|
-
pending.call.reject("Authorization request was cancelled or denied.")
|
|
259
|
-
return@launch
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
val status = manager.authorizationStatus(client, pending.readTypes, pending.writeTypes)
|
|
263
|
-
pending.call.resolve(status)
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
255
|
private fun parseTypeList(call: PluginCall, key: String): List<HealthDataType> {
|
|
268
256
|
val array = call.getArray(key) ?: JSArray()
|
|
269
257
|
val result = mutableListOf<HealthDataType>()
|
|
@@ -303,12 +291,6 @@ class HealthPlugin : Plugin() {
|
|
|
303
291
|
}
|
|
304
292
|
}
|
|
305
293
|
|
|
306
|
-
private data class PendingAuthorization(
|
|
307
|
-
val call: PluginCall,
|
|
308
|
-
val readTypes: List<HealthDataType>,
|
|
309
|
-
val writeTypes: List<HealthDataType>
|
|
310
|
-
)
|
|
311
|
-
|
|
312
294
|
@PluginMethod
|
|
313
295
|
fun getPluginVersion(call: PluginCall) {
|
|
314
296
|
try {
|
|
@@ -321,7 +303,6 @@ class HealthPlugin : Plugin() {
|
|
|
321
303
|
}
|
|
322
304
|
|
|
323
305
|
companion object {
|
|
324
|
-
private const val REQUEST_AUTHORIZATION = 9501
|
|
325
306
|
private const val DEFAULT_LIMIT = 100
|
|
326
307
|
private val DEFAULT_PAST_DURATION: Duration = Duration.ofDays(1)
|
|
327
308
|
}
|
|
@@ -3,7 +3,7 @@ import Capacitor
|
|
|
3
3
|
|
|
4
4
|
@objc(HealthPlugin)
|
|
5
5
|
public class HealthPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
6
|
-
private let pluginVersion: String = "7.2.
|
|
6
|
+
private let pluginVersion: String = "7.2.15"
|
|
7
7
|
public let identifier = "HealthPlugin"
|
|
8
8
|
public let jsName = "Health"
|
|
9
9
|
public let pluginMethods: [CAPPluginMethod] = [
|
package/package.json
CHANGED