@@ -14,7 +14,14 @@ import getApplicationContext = ad.getApplicationContext;
14
14
15
15
const GOOGLE_FIT_PERMISSIONS_REQUEST_CODE = 2 ;
16
16
17
- declare const com : any ;
17
+ declare const android , com , global : any ;
18
+
19
+ const AppPackageName = useAndroidX ( ) ? global . androidx . core . app : android . support . v4 . app ;
20
+ const ContentPackageName = useAndroidX ( ) ? global . androidx . core . content : android . support . v4 . content ;
21
+
22
+ function useAndroidX ( ) {
23
+ return global . androidx && global . androidx . appcompat ;
24
+ }
18
25
19
26
// android imports
20
27
const DataReadRequest = com . google . android . gms . fitness . request . DataReadRequest ;
@@ -57,7 +64,13 @@ export class HealthData extends Common implements HealthDataApi {
57
64
}
58
65
59
66
requestAuthorization ( types : Array < HealthDataType > ) : Promise < boolean > {
60
- return new Promise < boolean > ( ( resolve , reject ) => {
67
+ return Promise . all ( [
68
+ this . requestHardwarePermissions ( ) ,
69
+ this . requestAuthorizationInternal ( types )
70
+ ] ) . then ( results => Promise . resolve ( results [ 0 ] && results [ 1 ] ) ) ;
71
+ }
72
+
73
+ requestAuthorizationInternal ( types : Array < HealthDataType > ) : Promise < boolean > { return new Promise < boolean > ( ( resolve , reject ) => {
61
74
const fitnessOptionsBuilder = FitnessOptions . builder ( ) ;
62
75
63
76
types . filter ( t => t . accessType === "read" || t . accessType === "readAndWrite" )
@@ -90,7 +103,7 @@ export class HealthData extends Common implements HealthDataApi {
90
103
return new Promise ( ( resolve , reject ) => {
91
104
try {
92
105
// make sure the user is authorized
93
- this . requestAuthorization ( [ { name : opts . dataType , accessType : "read" } ] ) . then ( authorized => {
106
+ this . requestAuthorizationInternal ( [ { name : opts . dataType , accessType : "read" } ] ) . then ( authorized => {
94
107
if ( ! authorized ) {
95
108
reject ( "Not authorized" ) ;
96
109
return ;
@@ -221,6 +234,60 @@ export class HealthData extends Common implements HealthDataApi {
221
234
const typeOfData = acceptableDataTypesForCommonity [ pluginType ] ;
222
235
return aggregatedDataTypes [ typeOfData ] ;
223
236
}
237
+
238
+ private requestHardwarePermissions ( ) : Promise < boolean > {
239
+ return this . requestPermissionFor ( this . permissionsNeeded ( )
240
+ . filter ( permission => ! this . wasPermissionGranted ( permission ) ) ) ;
241
+ }
242
+
243
+ private wasPermissionGranted ( permission : any ) {
244
+ let hasPermission = android . os . Build . VERSION . SDK_INT < 23 ; // Android M. (6.0)
245
+ if ( ! hasPermission ) {
246
+ hasPermission = android . content . pm . PackageManager . PERMISSION_GRANTED ===
247
+ ContentPackageName . ContextCompat . checkSelfPermission (
248
+ utils . ad . getApplicationContext ( ) ,
249
+ permission ) ;
250
+ }
251
+ return hasPermission ;
252
+ }
253
+
254
+ private wasPermissionsGrantedForAll ( ) : boolean {
255
+ return this . permissionsNeeded ( ) . every ( permission => this . wasPermissionGranted ( permission ) ) ;
256
+ }
257
+
258
+ private requestPermissionFor ( permissions : any [ ] ) : Promise < boolean > {
259
+ return new Promise < boolean > ( ( resolve , reject ) => {
260
+ if ( ! this . wasPermissionsGrantedForAll ( ) ) {
261
+ const activityRequestPermissionsHandler = args => {
262
+ application . android . off ( application . AndroidApplication . activityRequestPermissionsEvent , activityRequestPermissionsHandler ) ;
263
+ resolve ( true ) ;
264
+ } ;
265
+
266
+ application . android . on ( application . AndroidApplication . activityRequestPermissionsEvent , activityRequestPermissionsHandler ) ;
267
+
268
+ AppPackageName . ActivityCompat . requestPermissions (
269
+ application . android . foregroundActivity ,
270
+ permissions ,
271
+ 235 // irrelevant since we simply invoke onPermissionGranted
272
+ ) ;
273
+ } else {
274
+ resolve ( true ) ;
275
+ }
276
+ } ) ;
277
+ }
278
+
279
+ private permissionsNeeded ( ) : any [ ] {
280
+ const permissions = [
281
+ android . Manifest . permission . ACCESS_FINE_LOCATION ,
282
+ android . Manifest . permission . ACCESS_NETWORK_STATE ,
283
+ android . Manifest . permission . GET_ACCOUNTS ] ;
284
+
285
+ if ( android . os . Build . VERSION . SDK_INT > 19 ) {
286
+ permissions . push ( android . Manifest . permission . BODY_SENSORS ) ;
287
+ }
288
+
289
+ return permissions ;
290
+ }
224
291
}
225
292
226
293
const aggregatedDataTypes = {
0 commit comments