forked from mablack/cordova-diagnostic-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 361
/
diagnostic.js
1566 lines (1437 loc) · 78.5 KB
/
diagnostic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Diagnostic plugin for Android
*
* Copyright (c) 2015 Working Edge Ltd.
* Copyright (c) 2012 AVANTIC ESTUDIO DE INGENIEROS
**/
var Diagnostic = (function(){
/***********************
*
* Internal properties
*
*********************/
var Diagnostic = {};
// Indicates if a runtime permissions request is in progress
var requestInProgress = false;
/********************
*
* Public properties
*
********************/
/**
* "Dangerous" permissions that need to be requested at run-time (Android 6.0/API 23 and above)
* See http://developer.android.com/guide/topics/security/permissions.html#perm-groups
* @type {Object}
*/
Diagnostic.permission = {
"ACCEPT_HANDOVER": "ACCEPT_HANDOVER",
"ACCESS_BACKGROUND_LOCATION": "ACCESS_BACKGROUND_LOCATION",
"ACCESS_COARSE_LOCATION": "ACCESS_COARSE_LOCATION",
"ACCESS_FINE_LOCATION": "ACCESS_FINE_LOCATION",
"ACCESS_MEDIA_LOCATION": "ACCESS_MEDIA_LOCATION",
"ACTIVITY_RECOGNITION": "ACTIVITY_RECOGNITION",
"ADD_VOICEMAIL": "ADD_VOICEMAIL",
"ANSWER_PHONE_CALLS": "ANSWER_PHONE_CALLS",
"BLUETOOTH_ADVERTISE": "BLUETOOTH_ADVERTISE",
"BLUETOOTH_CONNECT": "BLUETOOTH_CONNECT",
"BLUETOOTH_SCAN": "BLUETOOTH_SCAN",
"BODY_SENSORS": "BODY_SENSORS",
"BODY_SENSORS_BACKGROUND": "BODY_SENSORS_BACKGROUND",
"CALL_PHONE": "CALL_PHONE",
"CAMERA": "CAMERA",
"GET_ACCOUNTS": "GET_ACCOUNTS",
"NEARBY_WIFI_DEVICES": "NEARBY_WIFI_DEVICES",
"POST_NOTIFICATIONS": "POST_NOTIFICATIONS",
"PROCESS_OUTGOING_CALLS": "PROCESS_OUTGOING_CALLS",
"READ_CALENDAR": "READ_CALENDAR",
"READ_CALL_LOG": "READ_CALL_LOG",
"READ_CONTACTS": "READ_CONTACTS",
"READ_EXTERNAL_STORAGE": "READ_EXTERNAL_STORAGE",
"READ_MEDIA_AUDIO": "READ_MEDIA_AUDIO",
"READ_MEDIA_IMAGES": "READ_MEDIA_IMAGES",
"READ_MEDIA_VIDEO": "READ_MEDIA_VIDEO",
"READ_PHONE_NUMBERS": "READ_PHONE_NUMBERS",
"READ_PHONE_STATE": "READ_PHONE_STATE",
"READ_SMS": "READ_SMS",
"RECEIVE_MMS": "RECEIVE_MMS",
"RECEIVE_SMS": "RECEIVE_SMS",
"RECEIVE_WAP_PUSH": "RECEIVE_WAP_PUSH",
"RECORD_AUDIO": "RECORD_AUDIO",
"SEND_SMS": "SEND_SMS",
"USE_SIP": "USE_SIP",
"UWB_RANGING": "UWB_RANGING",
"WRITE_CALENDAR": "WRITE_CALENDAR",
"WRITE_CALL_LOG": "WRITE_CALL_LOG",
"WRITE_CONTACTS": "WRITE_CONTACTS",
"WRITE_EXTERNAL_STORAGE": "WRITE_EXTERNAL_STORAGE"
};
Diagnostic.permissionStatus = {
// Location permission requested and
// app build SDK/user device is Android >10 and user granted background location ("all the time") permission,
// or app build SDK/user device is Android 6-9 and user granted location permission,
// or non-location permission requested
// and app build SDK/user device is Android >=6 and user granted permission
// or app build SDK/user device is Android <6
"GRANTED": "GRANTED",
// Location permission requested
// and app build SDK/user device is Android >10
// and user granted background foreground location ("while-in-use") permission
"GRANTED_WHEN_IN_USE": "authorized_when_in_use",
// User denied access to this permission
"DENIED_ONCE": "DENIED_ONCE",
// User denied access to this permission and checked "Never Ask Again" box.
"DENIED_ALWAYS": "DENIED_ALWAYS",
// App has not yet requested access to this permission.
"NOT_REQUESTED": "NOT_REQUESTED",
// Limited access to the photo library on Android 14 (API 34) and above
"LIMITED": "LIMITED"
};
Diagnostic.cpuArchitecture = {
UNKNOWN: "unknown",
ARMv6: "ARMv6",
ARMv7: "ARMv7",
ARMv8: "ARMv8",
X86: "X86",
X86_64: "X86_64",
MIPS: "MIPS",
MIPS_64: "MIPS_64"
};
/*****************************
*
* Protected member functions
*
****************************/
// Placeholder listeners
Diagnostic._onNFCStateChange =
Diagnostic._onPermissionRequestComplete = function(){};
Diagnostic._combinePermissionStatuses = function(statuses){
var status = Diagnostic.permissionStatus.NOT_REQUESTED;
if(anyStatusIs(statuses, Diagnostic.permissionStatus.DENIED_ALWAYS)){
status = Diagnostic.permissionStatus.DENIED_ALWAYS;
}else if(anyStatusIs(statuses, Diagnostic.permissionStatus.DENIED_ONCE)){
status = Diagnostic.permissionStatus.DENIED_ONCE;
}else if(anyStatusIs(statuses, Diagnostic.permissionStatus.GRANTED)){
status = Diagnostic.permissionStatus.GRANTED;
}
return status;
};
/********************
*
* Internal functions
*
********************/
function anyStatusIs(statuses, status){
var anyStatus = false;
for(var permission in statuses){
if(statuses[permission] === status){
anyStatus = true;
break;
}
}
return anyStatus;
}
function checkForInvalidPermissions(permissions, errorCallback){
if(typeof(permissions) !== "object") permissions = [permissions];
var valid = true, invalidPermissions = [];
permissions.forEach(function(permission){
if(!Diagnostic.permission[permission]){
invalidPermissions.push(permission);
}
});
if(invalidPermissions.length > 0){
errorCallback("Invalid permissions specified: "+invalidPermissions.join(", "));
valid = false;
}
return valid;
}
/*****************************
*
* Protected member functions
*
****************************/
Diagnostic._ensureBoolean = function (callback){
return function(result){
callback(!!result);
}
};
/**********************
*
* Public API functions
*
**********************/
/***********
* General
***********/
/**
* Enables debug mode, which logs native debug messages to the native and JS consoles.
* Debug mode is initially disabled on plugin initialisation.
*
* @param {Function} successCallback - The callback which will be called when enabling debug is successful.
*/
Diagnostic.enableDebug = function(successCallback) {
return cordova.exec(successCallback,
null,
'Diagnostic',
'enableDebug',
[]);
};
/**
* Opens settings page for this app.
*
* @param {Function} successCallback - The callback which will be called when switch to settings is successful.
* @param {Function} errorCallback - The callback which will be called when switch to settings encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.switchToSettings = function(successCallback, errorCallback) {
return cordova.exec(successCallback,
errorCallback,
'Diagnostic',
'switchToSettings',
[]);
};
/**
* Returns the current authorisation status for a given permission.
* Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will always return GRANTED status as permissions are already granted at installation time.
*
* @param {Function} successCallback - function to call on successful retrieval of status.
* This callback function is passed a single string parameter which defines the current authorisation status as a value in cordova.plugins.diagnostic.permissionStatus.
* @param {Function} errorCallback - function to call on failure to retrieve authorisation status.
* This callback function is passed a single string parameter containing the error message.
* @param {String} permission - permission to request authorisation status for, defined as a value in cordova.plugins.diagnostic.permission
*/
Diagnostic.getPermissionAuthorizationStatus = function(successCallback, errorCallback, permission){
if(!checkForInvalidPermissions(permission, errorCallback)) return;
return cordova.exec(
successCallback,
errorCallback,
'Diagnostic',
'getPermissionAuthorizationStatus',
[permission]);
};
/**
* Returns the current authorisation status for multiple permissions.
* Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will always return GRANTED status as permissions are already granted at installation time.
*
* @param {Function} successCallback - function to call on successful retrieval of status.
* This callback function is passed a single object parameter which defines a key/value map, where the key is the requested permission defined as a value in cordova.plugins.diagnostic.permission, and the value is the current authorisation status of that permission as a value in cordova.plugins.diagnostic.permissionStatus.
* @param {Function} errorCallback - function to call on failure to retrieve authorisation statuses.
* This callback function is passed a single string parameter containing the error message.
* @param {Array} permissions - list of permissions to request authorisation statuses for, defined as values in cordova.plugins.diagnostic.permission
*/
Diagnostic.getPermissionsAuthorizationStatus = function(successCallback, errorCallback, permissions){
if(!checkForInvalidPermissions(permissions, errorCallback)) return;
return cordova.exec(
successCallback,
errorCallback,
'Diagnostic',
'getPermissionsAuthorizationStatus',
[permissions]);
};
/**
* Requests app to be granted authorisation for a runtime permission.
* Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will have no effect as the permissions are already granted at installation time.
*
* @param {Function} successCallback - function to call on successful request for runtime permission.
* This callback function is passed a single string parameter which defines the resulting authorisation status as a value in cordova.plugins.diagnostic.permissionStatus.
* @param {Function} errorCallback - function to call on failure to request authorisation.
* This callback function is passed a single string parameter containing the error message.
* @param {String} permission - permission to request authorisation for, defined as a value in cordova.plugins.diagnostic.permission
*/
Diagnostic.requestRuntimePermission = function(successCallback, errorCallback, permission) {
if(!checkForInvalidPermissions(permission, errorCallback)) return;
if(requestInProgress){
return onError("A runtime permissions request is already in progress");
}
function onSuccess(statuses){
requestInProgress = false;
successCallback(statuses[permission]);
Diagnostic._onPermissionRequestComplete(statuses);
}
function onError(error){
requestInProgress = false;
errorCallback(error);
}
requestInProgress = true;
return cordova.exec(
onSuccess,
onError,
'Diagnostic',
'requestRuntimePermission',
[permission]);
};
/**
* Requests app to be granted authorisation for multiple runtime permissions.
* Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will have no effect as the permissions are already granted at installation time.
*
* @param {Function} successCallback - function to call on successful request for runtime permissions.
* This callback function is passed a single object parameter which defines a key/value map, where the key is the permission to request defined as a value in cordova.plugins.diagnostic.permission, and the value is the resulting authorisation status of that permission as a value in cordova.plugins.diagnostic.permissionStatus.
* @param {Function} errorCallback - function to call on failure to request authorisation.
* This callback function is passed a single string parameter containing the error message.
* @param {Array} permissions - permissions to request authorisation for, defined as values in cordova.plugins.diagnostic.permission
*/
Diagnostic.requestRuntimePermissions = function(successCallback, errorCallback, permissions){
if(!checkForInvalidPermissions(permissions, errorCallback)) return;
if(requestInProgress){
return onError("A runtime permissions request is already in progress");
}
function onSuccess(statuses){
requestInProgress = false;
successCallback(statuses);
Diagnostic._onPermissionRequestComplete(statuses);
}
function onError(error){
requestInProgress = false;
errorCallback(error);
}
requestInProgress = true;
return cordova.exec(
onSuccess,
onError,
'Diagnostic',
'requestRuntimePermissions',
[permissions]);
};
/**
* Indicates if the plugin is currently requesting a runtime permission via the native API.
* Note that only one request can be made concurrently because the native API cannot handle concurrent requests,
* so the plugin will invoke the error callback if attempting to make more than one simultaneous request.
* Multiple permission requests should be grouped into a single call since the native API is setup to handle batch requests of multiple permission groups.
*
* @return {boolean} true if a permission request is currently in progress.
*/
Diagnostic.isRequestingPermission = function(){
return requestInProgress;
};
/**
* Registers a function to be called when a runtime permission request has completed.
* Pass in a falsey value to de-register the currently registered function.
*
* @param {Function} successCallback - The callback which will be called when a runtime permission request has completed.
* This callback function is passed a single object parameter which defines a key/value map, where the key is the permission requested (defined as a value in cordova.plugins.diagnostic.permission) and the value is the resulting authorisation status of that permission as a value in cordova.plugins.diagnostic.permissionStatus.
*/
Diagnostic.registerPermissionRequestCompleteHandler = function(successCallback) {
Diagnostic._onPermissionRequestComplete = successCallback || function(){};
};
/**
* Switches to the wireless settings page in the Settings app.
* Allows configuration of wireless controls such as Wi-Fi, Bluetooth and Mobile networks.
*/
Diagnostic.switchToWirelessSettings = function() {
return cordova.exec(null,
null,
'Diagnostic',
'switchToWirelessSettings',
[]);
};
/**
* Switches to the Mobile Data page in the Settings app
*/
Diagnostic.switchToMobileDataSettings = function() {
return cordova.exec(null,
null,
'Diagnostic',
'switchToMobileDataSettings',
[]);
};
/**
* Checks if ADB mode(debug mode) is switched on.
* Returns true if ADB mode is switched on.
*
* @param {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single boolean parameter which is TRUE if ADB mode(debug mode) is switched on.
* @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.isADBModeEnabled = function(successCallback, errorCallback) {
return cordova.exec(Diagnostic._ensureBoolean(successCallback),
errorCallback,
'Diagnostic',
'isADBModeEnabled',
[]);
};
/**
* Checks if the device is rooted.
* Returns true if the device is rooted.
*
* @param {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single boolean parameter which is TRUE if the device is rooted.
* @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.isDeviceRooted = function(successCallback, errorCallback) {
return cordova.exec(Diagnostic._ensureBoolean(successCallback),
errorCallback,
'Diagnostic',
'isDeviceRooted',
[]);
};
/**
* Restarts the application.
* By default, a "warm" restart will be performed in which the main Cordova activity is immediately restarted, causing the Webview instance to be recreated.
* However, if the `cold` parameter is set to true, then the application will be "cold" restarted, meaning a system exit will be performed, causing the entire application to be restarted.
* This is useful if you want to fully reset the native application state but will cause the application to briefly disappear and re-appear.
*
* Note: There is no successCallback() since if the operation is successful, the application will restart immediately before any success callback can be applied.
*
* @param {Function} errorCallback - function to call on failure to retrieve authorisation status.
* This callback function is passed a single string parameter containing the error message.
* @param {Boolean} cold - if true the application will be cold restarted. Defaults to false.
*/
Diagnostic.restart = function(errorCallback, cold){
return cordova.exec(
null,
errorCallback,
'Diagnostic',
'restart',
[cold]);
};
/**
* Returns CPU architecture of the current device.
*
* @param {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single string parameter defined as a constant in `cordova.plugins.diagnostic.cpuArchitecture`.
* @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.getArchitecture = function(successCallback, errorCallback) {
return cordova.exec(successCallback,
errorCallback,
'Diagnostic',
'getArchitecture',
[]);
};
/**
* Checks if the device data roaming setting is enabled.
* Returns true if data roaming is enabled.
*
* @param {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single boolean parameter which is TRUE if data roaming is enabled.
* @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.isDataRoamingEnabled = function(successCallback, errorCallback) {
return cordova.exec(Diagnostic._ensureBoolean(successCallback),
errorCallback,
'Diagnostic',
'isDataRoamingEnabled',
[]);
};
/**
* Returns the current battery level of the device as a percentage.
*
* @param {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single integer parameter which the current battery level percentage.
* @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.getCurrentBatteryLevel = function(successCallback, errorCallback){
return cordova.exec(successCallback,
errorCallback,
'Diagnostic',
'getCurrentBatteryLevel',
[]);
};
/**
* Checks if airplane mode is enabled on device.
*
* @param {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single boolean parameter which is TRUE if airplane mode is enabled.
* @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.isAirplaneModeEnabled = function(successCallback, errorCallback) {
return cordova.exec(Diagnostic._ensureBoolean(successCallback),
errorCallback,
'Diagnostic',
'isAirplaneModeEnabled',
[]);
};
/**
* Checks if mobile data is enabled on device.
*
* @param {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single boolean parameter which is TRUE if mobile data is enabled.
* @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.isMobileDataEnabled = function(successCallback, errorCallback) {
return cordova.exec(Diagnostic._ensureBoolean(successCallback),
errorCallback,
'Diagnostic',
'isMobileDataEnabled',
[]);
};
/**
* Checks if accessibility mode (talkback) is enabled on device.
*
* @param {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single boolean parameter which is TRUE if accessibility mode is enabled.
* @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.isAccessibilityModeEnabled = function(successCallback, errorCallback) {
return cordova.exec(Diagnostic._ensureBoolean(successCallback),
errorCallback,
'Diagnostic',
'isAccessibilityModeEnabled',
[]);
};
/**
* Checks if touch exploration (in accessibility mode) is enabled on device.
*
* @param {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single boolean parameter which is TRUE if touch exploration (in accessibility mode) is enabled.
* @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.isTouchExplorationEnabled = function(successCallback, errorCallback) {
return cordova.exec(Diagnostic._ensureBoolean(successCallback),
errorCallback,
'Diagnostic',
'isTouchExplorationEnabled',
[]);
};
/**
* Returns details of the OS of the device on which the app is currently running
*
* @param {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single object parameter with the following fields:
* - {string} version - version string of the OS e.g. "11.0"
* - {integer} apiLevel - API level of the OS e.g. 30
* - {string} apiName - code name for API level e.g. "FROYO"
* @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.getDeviceOSVersion = function(successCallback, errorCallback) {
return cordova.exec(successCallback,
errorCallback,
'Diagnostic',
'getDeviceOSVersion',
[]);
};
/**
* Returns details of the SDK levels used to build the app.
*
* @param {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single object parameter with the following fields:
* - {integer} targetApiLevel - API level of the target SDK (used to build the app)
* - {string} targetApiName - code name for API level of the target SDK e.g. "FROYO"
* - {integer} minApiLevel - API level of the minimum SDK (lowest on which the app can be installed)
* - {string} minApiName - code name for API level of the minimum SDK e.g. "FROYO"
* @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.getBuildOSVersion = function(successCallback, errorCallback) {
return cordova.exec(successCallback,
errorCallback,
'Diagnostic',
'getBuildOSVersion',
[]);
};
/************
* Location *
************/
/**
* Checks if location is available for use by the app.
* On Android, this returns true if Location Mode is enabled and any mode is selected (e.g. Battery saving, Device only, High accuracy)
* AND if the app is authorised to use location.
*
* @param {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single boolean parameter which is TRUE if location is available for use.
* @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.isLocationAvailable = function(successCallback, errorCallback) {
if(cordova.plugins.diagnostic.location){
cordova.plugins.diagnostic.location.isLocationAvailable.apply(this, arguments);
}else{
throw "Diagnostic Location module is not installed";
}
};
/**
* Checks if the device location setting is enabled.
* On Android, this returns true if Location Mode is enabled and any mode is selected (e.g. Battery saving, Device only, High accuracy)
*
* @param {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single boolean parameter which is TRUE if location setting is enabled.
* @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.isLocationEnabled = function(successCallback, errorCallback) {
if(cordova.plugins.diagnostic.location){
cordova.plugins.diagnostic.location.isLocationEnabled.apply(this, arguments);
}else{
throw "Diagnostic Location module is not installed";
}
};
/**
* Checks if high-accuracy locations are available to the app from GPS hardware.
* Returns true if Location mode is enabled and is set to "Device only" or "High accuracy"
* AND if the app is authorised to use location.
*
* @param {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single boolean parameter which is TRUE if high-accuracy GPS-based location is available.
* @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.isGpsLocationAvailable = function(successCallback, errorCallback) {
if(cordova.plugins.diagnostic.location){
cordova.plugins.diagnostic.location.isGpsLocationAvailable.apply(this, arguments);
}else{
throw "Diagnostic Location module is not installed";
}
};
/**
* Checks if the device location setting is set to return high-accuracy locations from GPS hardware.
* Returns true if Location mode is enabled and is set to either:
* Device only = GPS hardware only (high accuracy)
* High accuracy = GPS hardware, network triangulation and Wifi network IDs (high and low accuracy)
*
* @param {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single boolean parameter which is TRUE if device setting is set to return high-accuracy GPS-based location.
* @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.isGpsLocationEnabled = function(successCallback, errorCallback) {
if(cordova.plugins.diagnostic.location){
cordova.plugins.diagnostic.location.isGpsLocationEnabled.apply(this, arguments);
}else{
throw "Diagnostic Location module is not installed";
}
};
/**
* Checks if low-accuracy locations are available to the app from network triangulation/WiFi access points.
* Returns true if Location mode is enabled and is set to "Battery saving" or "High accuracy"
* AND if the app is authorised to use location.
*
* @param {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single boolean parameter which is TRUE if low-accuracy network-based location is available.
* @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.isNetworkLocationAvailable = function(successCallback, errorCallback) {
if(cordova.plugins.diagnostic.location){
cordova.plugins.diagnostic.location.isNetworkLocationAvailable.apply(this, arguments);
}else{
throw "Diagnostic Location module is not installed";
}
};
/**
* Checks if the device location setting is set to return low-accuracy locations from network triangulation/WiFi access points.
* Returns true if Location mode is enabled and is set to either:
* Battery saving = network triangulation and Wifi network IDs (low accuracy)
* High accuracy = GPS hardware, network triangulation and Wifi network IDs (high and low accuracy)
*
* @param {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single boolean parameter which is TRUE if device setting is set to return low-accuracy network-based location.
* @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.isNetworkLocationEnabled = function(successCallback, errorCallback) {
if(cordova.plugins.diagnostic.location){
cordova.plugins.diagnostic.location.isNetworkLocationEnabled.apply(this, arguments);
}else{
throw "Diagnostic Location module is not installed";
}
};
/**
* Returns the current location mode setting for the device.
*
* @param {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single string parameter defined as a constant in `cordova.plugins.diagnostic.locationMode`.
* @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.getLocationMode = function(successCallback, errorCallback) {
if(cordova.plugins.diagnostic.location){
cordova.plugins.diagnostic.location.getLocationMode.apply(this, arguments);
}else{
throw "Diagnostic Location module is not installed";
}
};
/**
* Switches to the Location page in the Settings app
*/
Diagnostic.switchToLocationSettings = function() {
if(cordova.plugins.diagnostic.location){
cordova.plugins.diagnostic.location.switchToLocationSettings.apply(this, arguments);
}else{
throw "Diagnostic Location module is not installed";
}
};
/**
* Requests location authorization for the application.
* Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will have no effect as the permissions are already granted at installation time.
* @param {Function} successCallback - function to call on successful request for runtime permissions.
* This callback function is passed a single string parameter which defines the resulting authorisation status as a value in cordova.plugins.diagnostic.permissionStatus.
* @param {Function} errorCallback - function to call on failure to request authorisation.
* @param {String} mode - (optional) location authorization mode as a constant in `cordova.plugins.diagnostic.locationAuthorizationMode`.
* If not specified, defaults to `cordova.plugins.diagnostic.locationAuthorizationMode.WHEN_IN_USE`.
* @param {String} accuracy - (optional) requested location accuracy as a constant in in `cordova.plugins.diagnostic.locationAccuracyAuthorization`.
* If not specified, defaults to `cordova.plugins.diagnostic.locationAccuracyAuthorization.FULL`.
*/
Diagnostic.requestLocationAuthorization = function(successCallback, errorCallback, mode, accuracy){
if(cordova.plugins.diagnostic.location){
cordova.plugins.diagnostic.location.requestLocationAuthorization.apply(this, arguments);
}else{
throw "Diagnostic Location module is not installed";
}
};
/**
* Returns the location authorization status for the application.
* Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will always return GRANTED status as permissions are already granted at installation time.
* @param {Function} successCallback - function to call on successful request for runtime permissions status.
* This callback function is passed a single string parameter which defines the current authorisation status as a value in cordova.plugins.diagnostic.permissionStatus.
* @param {Function} errorCallback - function to call on failure to request authorisation status.
*/
Diagnostic.getLocationAuthorizationStatus = function(successCallback, errorCallback){
if(cordova.plugins.diagnostic.location){
cordova.plugins.diagnostic.location.getLocationAuthorizationStatus.apply(this, arguments);
}else{
throw "Diagnostic Location module is not installed";
}
};
/**
* Returns the individual location authorization status for each type of location access (FINE, COARSE and BACKGROUND)
* Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will always return GRANTED status as permissions are already granted at installation time.
* @param {Function} successCallback - function to call on successful request for runtime permissions statuses.
* This callback function is passed a single string parameter which defines the current authorisation status as a value in cordova.plugins.diagnostic.permissionStatus.
* @param {Function} errorCallback - function to call on failure to request authorisation status.
*/
Diagnostic.getLocationAuthorizationStatuses = function(successCallback, errorCallback){
if(cordova.plugins.diagnostic.location){
cordova.plugins.diagnostic.location.getLocationAuthorizationStatuses.apply(this, arguments);
}else{
throw "Diagnostic Location module is not installed";
}
};
/**
* Checks if the application is authorized to use location.
* Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will always return TRUE as permissions are already granted at installation time.
* @param {Function} successCallback - function to call on successful request for runtime permissions status.
* This callback function is passed a single boolean parameter which is TRUE if the app currently has runtime authorisation to use location.
* @param {Function} errorCallback - function to call on failure to request authorisation status.
*/
Diagnostic.isLocationAuthorized = function(successCallback, errorCallback){
if(cordova.plugins.diagnostic.location){
cordova.plugins.diagnostic.location.isLocationAuthorized.apply(this, arguments);
}else{
throw "Diagnostic Location module is not installed";
}
};
/**
* Registers a function to be called when a change in Location state occurs.
* On Android, this occurs when the Location Mode is changed.
* Pass in a falsey value to de-register the currently registered function.
*
* @param {Function} successCallback - The callback which will be called when the Location state changes.
* This callback function is passed a single string parameter defined as a constant in `cordova.plugins.diagnostic.locationMode`.
*/
Diagnostic.registerLocationStateChangeHandler = function(successCallback) {
if(cordova.plugins.diagnostic.location){
cordova.plugins.diagnostic.location.registerLocationStateChangeHandler.apply(this, arguments);
}else{
throw "Diagnostic Location module is not installed";
}
};
/**
* Returns the location accuracy authorization for the application.
* Will invoke the error callback if location permission is not yet granted.
*
* @param {Function} successCallback - The callback which will be called when operation is successful.
* This callback function is passed a single string parameter which indicates the location accuracy authorization as a constant in `cordova.plugins.diagnostic.locationAccuracyAuthorization`.
* Possible values are:
* `cordova.plugins.diagnostic.locationAccuracyAuthorization.FULL`
* `cordova.plugins.diagnostic.locationAccuracyAuthorization.REDUCED`
* @param {Function} errorCallback - The callback which will be called when operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.getLocationAccuracyAuthorization = function(successCallback, errorCallback){
if(cordova.plugins.diagnostic.location){
cordova.plugins.diagnostic.location.getLocationAccuracyAuthorization.apply(this, arguments);
}else{
throw "Diagnostic Location module is not installed";
}
};
/************
* WiFi *
************/
/**
* Checks if Wifi is enabled.
* On Android this returns true if the WiFi setting is set to enabled.
*
* @param {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single boolean parameter which is TRUE if WiFi is enabled.
* @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.isWifiAvailable = Diagnostic.isWifiEnabled = function(successCallback, errorCallback) {
if(cordova.plugins.diagnostic.wifi){
cordova.plugins.diagnostic.wifi.isWifiAvailable.apply(this, arguments);
}else{
throw "Diagnostic Wifi module is not installed";
}
};
/**
* Switches to the WiFi page in the Settings app
*/
Diagnostic.switchToWifiSettings = function() {
if(cordova.plugins.diagnostic.wifi){
cordova.plugins.diagnostic.wifi.switchToWifiSettings.apply(this, arguments);
}else{
throw "Diagnostic Wifi module is not installed";
}
};
/**
* Enables/disables WiFi on the device.
*
* @param {Function} successCallback - function to call on successful setting of WiFi state
* @param {Function} errorCallback - function to call on failure to set WiFi state.
* This callback function is passed a single string parameter containing the error message.
* @param {Boolean} state - WiFi state to set: TRUE for enabled, FALSE for disabled.
*/
Diagnostic.setWifiState = function(successCallback, errorCallback, state) {
if(cordova.plugins.diagnostic.wifi){
cordova.plugins.diagnostic.wifi.setWifiState.apply(this, arguments);
}else{
throw "Diagnostic Wifi module is not installed";
}
};
/************
* Camera *
************/
/**
* Checks if camera is usable: both present and authorised for use.
*
* @param {Object} params - (optional) parameters:
* - {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single boolean parameter which is TRUE if camera is present and authorized for use.
* - {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
* - {Boolean} externalStorage - (Android only) If true, checks permission for READ_EXTERNAL_STORAGE in addition to CAMERA run-time permission.
* [email protected]+ requires both of these permissions. Defaults to true.
*/
Diagnostic.isCameraAvailable = function(params) {
if(cordova.plugins.diagnostic.camera){
cordova.plugins.diagnostic.camera.isCameraAvailable.apply(this, arguments);
}else{
throw "Diagnostic Camera module is not installed";
}
};
/**
* Checks if camera hardware is present on device.
*
* @param {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single boolean parameter which is TRUE if camera is present
* @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.isCameraPresent = function(successCallback, errorCallback) {
if(cordova.plugins.diagnostic.camera){
cordova.plugins.diagnostic.camera.isCameraPresent.apply(this, arguments);
}else{
throw "Diagnostic Camera module is not installed";
}
};
/**
* Requests authorisation for runtime permissions to use the camera.
* Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will have no effect as the permissions are already granted at installation time.
* @param {Object} params - (optional) parameters:
* - {Function} successCallback - function to call on successful request for runtime permissions.
* This callback function is passed a single string parameter which defines the resulting authorisation status as a value in cordova.plugins.diagnostic.permissionStatus.
* - {Function} errorCallback - function to call on failure to request authorisation.
* - {Boolean} externalStorage - (Android only) If true, requests permission for READ_EXTERNAL_STORAGE in addition to CAMERA run-time permission.
* [email protected]+ requires both of these permissions. Defaults to true.
*/
Diagnostic.requestCameraAuthorization = function(params){
if(cordova.plugins.diagnostic.camera){
cordova.plugins.diagnostic.camera.requestCameraAuthorization.apply(this, arguments);
}else{
throw "Diagnostic Camera module is not installed";
}
};
/**
* Returns the authorisation status for runtime permissions to use the camera.
* Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will always return GRANTED status as permissions are already granted at installation time.
* @param {Object} params - (optional) parameters:
* - {Function} successCallback - function to call on successful request for runtime permissions status.
* This callback function is passed a single string parameter which defines the current authorisation status as a value in cordova.plugins.diagnostic.permissionStatus.
* - {Function} errorCallback - function to call on failure to request authorisation status.
* - {Boolean} externalStorage - (Android only) If true, checks permission for READ_EXTERNAL_STORAGE in addition to CAMERA run-time permission.
* [email protected]+ requires both of these permissions. Defaults to true.
*/
Diagnostic.getCameraAuthorizationStatus = function(params){
if(cordova.plugins.diagnostic.camera){
cordova.plugins.diagnostic.camera.getCameraAuthorizationStatus.apply(this, arguments);
}else{
throw "Diagnostic Camera module is not installed";
}
};
/**
* Returns the individual authorisation statuses for runtime permissions to use the camera.
* Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will always return GRANTED status as permissions are already granted at installation time.
* @param {Object} params - (optional) parameters:
* - {Function} successCallback - function to call on successful request for runtime permission status.
* This callback function is passed a single object parameter where each key indicates the permission name and the value defines the current authorisation status as a value in cordova.plugins.diagnostic.permissionStatus.
* - {Function} errorCallback - function to call on failure to request authorisation status.
* - {Boolean} storage - (Android only) If true, queries storage permissions in addition to CAMERA run-time permission.
* On Android 13+, storage permissions are READ_MEDIA_IMAGES and READ_MEDIA_VIDEO. On Android 9-12, storage permission is READ_EXTERNAL_STORAGE.
* cordova-plugin-camera requires both storage and camera permissions.
* Defaults to true.
*/
Diagnostic.getCameraAuthorizationStatuses = function(params){
if(cordova.plugins.diagnostic.camera){
cordova.plugins.diagnostic.camera.getCameraAuthorizationStatuses.apply(this, arguments);
}else{
throw "Diagnostic Camera module is not installed";
}
};
/**
* Checks if the application is authorized to use the camera.
* Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will always return TRUE as permissions are already granted at installation time.
* @param {Object} params - (optional) parameters:
* - {Function} successCallback - function to call on successful request for runtime permissions status.
* This callback function is passed a single boolean parameter which is TRUE if the app currently has runtime authorisation to use location.
* - {Function} errorCallback - function to call on failure to request authorisation status.
* - {Boolean} externalStorage - (Android only) If true, checks permission for READ_EXTERNAL_STORAGE in addition to CAMERA run-time permission.
* [email protected]+ requires both of these permissions. Defaults to true.
*/
Diagnostic.isCameraAuthorized = function(params){
if(cordova.plugins.diagnostic.camera){
cordova.plugins.diagnostic.camera.isCameraAuthorized.apply(this, arguments);
}else{
throw "Diagnostic Camera module is not installed";
}
};
/**********************
* External storage *
**********************/
/**
* Requests authorisation for runtime permission to use the external storage.
* Note: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will have no effect as the permission is already granted at installation time.
* @param {Function} successCallback - function to call on successful request for runtime permission.
* This callback function is passed a single string parameter which defines the resulting authorisation status as a value in cordova.plugins.diagnostic.permissionStatus.
* @param {Function} errorCallback - function to call on failure to request authorisation.
*/
Diagnostic.requestExternalStorageAuthorization = function(successCallback, errorCallback){
if(cordova.plugins.diagnostic.external_storage){