cordova.plugins.diagnostic 7.2.10 → 7.3.0

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.
@@ -741,6 +741,46 @@ interface Diagnostic {
741
741
  errorCallback: (error: string) => void
742
742
  ) => void;
743
743
 
744
+ /**
745
+ * Checks if low power mode is currently enabled on the device.
746
+ * @param successCallback
747
+ * @param errorCallback
748
+ */
749
+ isLowPowerModeEnabled?: (
750
+ successCallback: (enabled: boolean) => void,
751
+ errorCallback: (error: string) => void
752
+ ) => void;
753
+
754
+ /**
755
+ * Registers a function to be called when low power mode changes.
756
+ * @param successCallback
757
+ */
758
+ onLowPowerModeChange?: (
759
+ successCallback: (enabled: boolean) => void
760
+ ) => void;
761
+
762
+ /**
763
+ * ANDROID ONLY
764
+ * Checks if the app is currently ignoring battery optimizations.
765
+ * @param successCallback
766
+ * @param errorCallback
767
+ */
768
+ isIgnoringBatteryOptimizations?: (
769
+ successCallback: (enabled: boolean) => void,
770
+ errorCallback: (error: string) => void
771
+ ) => void;
772
+
773
+ /**
774
+ * ANDROID ONLY
775
+ * Prompts the user to allow the app to ignore battery optimizations.
776
+ * @param successCallback
777
+ * @param errorCallback
778
+ */
779
+ requestIgnoreBatteryOptimizations?: (
780
+ successCallback: () => void,
781
+ errorCallback: (error: string) => void
782
+ ) => void;
783
+
744
784
  /**
745
785
  * ANDROID ONLY
746
786
  * Checks if high-accuracy locations are available to the app from GPS hardware.
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "7.2.10",
2
+ "version": "7.3.0",
3
3
  "name": "cordova.plugins.diagnostic",
4
4
  "cordova_name": "Diagnostic",
5
5
  "description": "Cordova/Phonegap plugin to check the state of Location/WiFi/Camera/Bluetooth device settings.",
package/plugin.xml CHANGED
@@ -2,7 +2,7 @@
2
2
  <plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
3
3
  xmlns:android="http://schemas.android.com/apk/res/android"
4
4
  id="cordova.plugins.diagnostic"
5
- version="7.2.10">
5
+ version="7.3.0">
6
6
 
7
7
  <name>Diagnostic</name>
8
8
  <description>Cordova/Phonegap plugin to check the state of Location/WiFi/Camera/Bluetooth device settings.</description>
@@ -46,13 +46,16 @@ import android.Manifest;
46
46
  import android.app.Activity;
47
47
  import android.app.AlarmManager;
48
48
  import android.app.PendingIntent;
49
+ import android.content.BroadcastReceiver;
49
50
  import android.content.SharedPreferences;
51
+ import android.content.IntentFilter;
50
52
  import android.content.pm.ApplicationInfo;
51
53
  import android.content.pm.PackageInfo;
52
54
  import android.net.ConnectivityManager;
53
55
  import android.net.Uri;
54
56
  import android.os.BatteryManager;
55
57
  import android.os.Build;
58
+ import android.os.PowerManager;
56
59
  import android.util.Log;
57
60
 
58
61
  import android.content.Context;
@@ -263,6 +266,7 @@ public class Diagnostic extends CordovaPlugin{
263
266
 
264
267
  protected SharedPreferences sharedPref;
265
268
  protected SharedPreferences.Editor editor;
269
+ protected boolean currentLowPowerModeEnabled = false;
266
270
 
267
271
  /*************
268
272
  * Public API
@@ -291,10 +295,31 @@ public class Diagnostic extends CordovaPlugin{
291
295
  applicationContext = this.cordova.getActivity().getApplicationContext();
292
296
  sharedPref = cordova.getActivity().getSharedPreferences(TAG, Activity.MODE_PRIVATE);
293
297
  editor = sharedPref.edit();
298
+ currentLowPowerModeEnabled = isLowPowerModeEnabled();
299
+
300
+ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
301
+ try {
302
+ applicationContext.registerReceiver(lowPowerModeChangedReceiver, new IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED));
303
+ } catch (Exception e) {
304
+ logWarning("Unable to register low power mode change receiver: " + e.getMessage());
305
+ }
306
+ }
294
307
 
295
308
  super.initialize(cordova, webView);
296
309
  }
297
310
 
311
+ @Override
312
+ public void onDestroy() {
313
+ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
314
+ try {
315
+ applicationContext.unregisterReceiver(lowPowerModeChangedReceiver);
316
+ } catch (Exception e) {
317
+ logWarning("Unable to unregister low power mode change receiver: " + e.getMessage());
318
+ }
319
+ }
320
+ super.onDestroy();
321
+ }
322
+
298
323
  /**
299
324
  * Executes the request and returns PluginResult.
300
325
  *
@@ -351,6 +376,13 @@ public class Diagnostic extends CordovaPlugin{
351
376
  callbackContext.success(getCPUArchitecture());
352
377
  } else if(action.equals("getCurrentBatteryLevel")) {
353
378
  callbackContext.success(getCurrentBatteryLevel());
379
+ } else if(action.equals("isLowPowerModeEnabled")) {
380
+ callbackContext.success(isLowPowerModeEnabled() ? 1 : 0);
381
+ } else if(action.equals("isIgnoringBatteryOptimizations")) {
382
+ callbackContext.success(isIgnoringBatteryOptimizations() ? 1 : 0);
383
+ } else if(action.equals("requestIgnoreBatteryOptimizations")) {
384
+ requestIgnoreBatteryOptimizations();
385
+ callbackContext.success();
354
386
  } else if(action.equals("isAirplaneModeEnabled")) {
355
387
  callbackContext.success(isAirplaneModeEnabled() ? 1 : 0);
356
388
  } else if(action.equals("getDeviceOSVersion")) {
@@ -861,6 +893,17 @@ public class Diagnostic extends CordovaPlugin{
861
893
  executeGlobalJavascript("cordova.plugins.diagnostic." + jsString);
862
894
  }
863
895
 
896
+ protected final BroadcastReceiver lowPowerModeChangedReceiver = new BroadcastReceiver() {
897
+ @Override
898
+ public void onReceive(Context context, Intent intent) {
899
+ final String action = intent.getAction();
900
+ if(instance != null && PowerManager.ACTION_POWER_SAVE_MODE_CHANGED.equals(action)){
901
+ Log.v(TAG, "lowPowerModeChangedReceiver");
902
+ instance.notifyLowPowerModeChange();
903
+ }
904
+ }
905
+ };
906
+
864
907
  /**
865
908
  * Performs a warm app restart - restarts only Cordova main activity
866
909
  */
@@ -953,6 +996,41 @@ public class Diagnostic extends CordovaPlugin{
953
996
  return bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
954
997
  }
955
998
 
999
+ protected boolean isLowPowerModeEnabled(){
1000
+ if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
1001
+ return false;
1002
+ }
1003
+ PowerManager powerManager = (PowerManager) cordova.getContext().getApplicationContext().getSystemService(Context.POWER_SERVICE);
1004
+ return powerManager != null && powerManager.isPowerSaveMode();
1005
+ }
1006
+
1007
+ protected boolean isIgnoringBatteryOptimizations(){
1008
+ if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
1009
+ return true;
1010
+ }
1011
+ PowerManager powerManager = (PowerManager) cordova.getContext().getApplicationContext().getSystemService(Context.POWER_SERVICE);
1012
+ return powerManager != null && powerManager.isIgnoringBatteryOptimizations(cordova.getActivity().getPackageName());
1013
+ }
1014
+
1015
+ protected void requestIgnoreBatteryOptimizations(){
1016
+ if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M || isIgnoringBatteryOptimizations()){
1017
+ return;
1018
+ }
1019
+
1020
+ Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
1021
+ intent.setData(Uri.parse("package:" + cordova.getActivity().getPackageName()));
1022
+ cordova.getActivity().startActivity(intent);
1023
+ }
1024
+
1025
+ protected void notifyLowPowerModeChange(){
1026
+ boolean lowPowerModeEnabled = isLowPowerModeEnabled();
1027
+ if(lowPowerModeEnabled != currentLowPowerModeEnabled){
1028
+ currentLowPowerModeEnabled = lowPowerModeEnabled;
1029
+ logDebug("Low power mode changed to: " + lowPowerModeEnabled);
1030
+ executePluginJavascript("_onLowPowerModeChange(" + (lowPowerModeEnabled ? "true" : "false") + ");");
1031
+ }
1032
+ }
1033
+
956
1034
  // https://stackoverflow.com/a/18237962/777265
957
1035
  protected boolean hasBuildPermission(String permission)
958
1036
  {
@@ -28,6 +28,7 @@ extern NSString*const AUTHORIZATION_LIMITED;
28
28
 
29
29
  @property (nonatomic) float osVersion;
30
30
  @property (nonatomic) BOOL debugEnabled;
31
+ @property (nonatomic, strong) id lowPowerModeChangeObserver;
31
32
 
32
33
  // Plugin API
33
34
  - (void) enableDebug: (CDVInvokedUrlCommand*)command;
@@ -35,6 +36,7 @@ extern NSString*const AUTHORIZATION_LIMITED;
35
36
  - (void) getBackgroundRefreshStatus: (CDVInvokedUrlCommand*)command;
36
37
  - (void) getArchitecture: (CDVInvokedUrlCommand*)command;
37
38
  - (void) getCurrentBatteryLevel: (CDVInvokedUrlCommand*)command;
39
+ - (void) isLowPowerModeEnabled: (CDVInvokedUrlCommand*)command;
38
40
  - (void) getDeviceOSVersion: (CDVInvokedUrlCommand*)command;
39
41
  - (void) getBuildOSVersion: (CDVInvokedUrlCommand*)command;
40
42
  - (void) isMobileDataAuthorized: (CDVInvokedUrlCommand*)command;
@@ -41,6 +41,7 @@ static Diagnostic* diagnostic = nil;
41
41
  static CTCellularData* cellularData;
42
42
  #endif
43
43
 
44
+
44
45
  /********************************/
45
46
  #pragma mark - Public static functions
46
47
  /********************************/
@@ -126,6 +127,12 @@ static CTCellularData* cellularData;
126
127
  #if !TARGET_OS_MACCATALYST
127
128
  cellularData = [[CTCellularData alloc] init];
128
129
  #endif
130
+
131
+ self.lowPowerModeChangeObserver = [[NSNotificationCenter defaultCenter] addObserverForName:NSProcessInfoPowerStateDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
132
+ BOOL isLowPowerModeEnabled = [[NSProcessInfo processInfo] isLowPowerModeEnabled];
133
+ [self logDebug:[NSString stringWithFormat:@"Low power mode changed to: %@", isLowPowerModeEnabled ? @"true" : @"false"]];
134
+ [self executeGlobalJavascript:[NSString stringWithFormat:@"cordova.plugins.diagnostic._onLowPowerModeChange(%@);", isLowPowerModeEnabled ? @"true" : @"false"]];
135
+ }];
129
136
  }
130
137
 
131
138
  // https://stackoverflow.com/a/38441011/777265
@@ -186,6 +193,18 @@ static CTCellularData* cellularData;
186
193
  }];
187
194
  }
188
195
 
196
+ - (void) isLowPowerModeEnabled: (CDVInvokedUrlCommand*)command {
197
+ [self.commandDelegate runInBackground:^{
198
+ @try {
199
+ BOOL isLowPowerModeEnabled = [[NSProcessInfo processInfo] isLowPowerModeEnabled];
200
+ [self logDebug:[NSString stringWithFormat:@"Low power mode enabled: %@", isLowPowerModeEnabled ? @"true" : @"false"]];
201
+ [self sendPluginResultBool:isLowPowerModeEnabled :command];
202
+ }@catch (NSException *exception) {
203
+ [self handlePluginException:exception :command];
204
+ }
205
+ }];
206
+ }
207
+
189
208
  - (void) getDeviceOSVersion: (CDVInvokedUrlCommand*)command {
190
209
  [self.commandDelegate runInBackground:^{
191
210
  @try {
@@ -419,6 +438,13 @@ static CTCellularData* cellularData;
419
438
  return [[NSUserDefaults standardUserDefaults] objectForKey:key];
420
439
  }
421
440
 
441
+ - (void)dealloc {
442
+ if(self.lowPowerModeChangeObserver != nil){
443
+ [[NSNotificationCenter defaultCenter] removeObserver:self.lowPowerModeChangeObserver];
444
+ self.lowPowerModeChangeObserver = nil;
445
+ }
446
+ }
447
+
422
448
  @end
423
449
 
424
450
 
@@ -156,12 +156,14 @@ static NSString*const LOG_TAG = @"Diagnostic_Bluetooth[native]";
156
156
  }
157
157
 
158
158
  - (void) ensureBluetoothManager {
159
- if(![self.bluetoothManager isKindOfClass:[CBCentralManager class]]){
159
+ @synchronized(self) {
160
+ if(![self.bluetoothManager isKindOfClass:[CBCentralManager class]]){
160
161
  self.bluetoothManager = [[CBCentralManager alloc]
161
162
  initWithDelegate:self
162
163
  queue:dispatch_get_main_queue()
163
164
  options:@{CBCentralManagerOptionShowPowerAlertKey: @(NO)}];
164
165
  [self centralManagerDidUpdateState:self.bluetoothManager]; // Send initial state
166
+ }
165
167
  }
166
168
  }
167
169
 
@@ -112,7 +112,8 @@ var Diagnostic = (function(){
112
112
  *
113
113
  ****************************/
114
114
  // Placeholder listeners
115
- Diagnostic._onNFCStateChange =
115
+ Diagnostic._onLowPowerModeChange =
116
+ Diagnostic._onNFCStateChange =
116
117
  Diagnostic._onPermissionRequestComplete = function(){};
117
118
 
118
119
  Diagnostic._combinePermissionStatuses = function(statuses){
@@ -484,6 +485,63 @@ var Diagnostic = (function(){
484
485
  []);
485
486
  };
486
487
 
488
+ /**
489
+ * Checks if low power mode is currently enabled on device.
490
+ *
491
+ * @param {Function} successCallback - The callback which will be called when the operation is successful.
492
+ * This callback function is passed a single boolean parameter which is TRUE if low power mode is enabled.
493
+ * @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
494
+ * This callback function is passed a single string parameter containing the error message.
495
+ */
496
+ Diagnostic.isLowPowerModeEnabled = function(successCallback, errorCallback){
497
+ return cordova.exec(Diagnostic._ensureBoolean(successCallback),
498
+ errorCallback,
499
+ 'Diagnostic',
500
+ 'isLowPowerModeEnabled',
501
+ []);
502
+ };
503
+
504
+ /**
505
+ * Checks if the app is currently ignoring battery optimizations.
506
+ *
507
+ * @param {Function} successCallback - The callback which will be called when the operation is successful.
508
+ * This callback function is passed a single boolean parameter which is TRUE if the app is ignoring battery optimizations.
509
+ * @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
510
+ * This callback function is passed a single string parameter containing the error message.
511
+ */
512
+ Diagnostic.isIgnoringBatteryOptimizations = function(successCallback, errorCallback){
513
+ return cordova.exec(Diagnostic._ensureBoolean(successCallback),
514
+ errorCallback,
515
+ 'Diagnostic',
516
+ 'isIgnoringBatteryOptimizations',
517
+ []);
518
+ };
519
+
520
+ /**
521
+ * Prompts the user to allow the app to ignore battery optimizations.
522
+ *
523
+ * @param {Function} successCallback - The callback which will be called when the request intent is opened.
524
+ * @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
525
+ * This callback function is passed a single string parameter containing the error message.
526
+ */
527
+ Diagnostic.requestIgnoreBatteryOptimizations = function(successCallback, errorCallback){
528
+ return cordova.exec(successCallback,
529
+ errorCallback,
530
+ 'Diagnostic',
531
+ 'requestIgnoreBatteryOptimizations',
532
+ []);
533
+ };
534
+
535
+ /**
536
+ * Registers a function to be called when the device low power mode changes.
537
+ *
538
+ * @param {Function} successCallback - The callback which will be called when low power mode changes.
539
+ * This callback function is passed a single boolean parameter which is TRUE if low power mode is enabled.
540
+ */
541
+ Diagnostic.onLowPowerModeChange = function(successCallback) {
542
+ Diagnostic._onLowPowerModeChange = successCallback || function(){};
543
+ };
544
+
487
545
  /**
488
546
  * Checks if airplane mode is enabled on device.
489
547
  *
@@ -154,6 +154,32 @@ var Diagnostic = (function(){
154
154
  []);
155
155
  };
156
156
 
157
+ /**
158
+ * Checks if low power mode is currently enabled on device.
159
+ *
160
+ * @param {Function} successCallback - The callback which will be called when the operation is successful.
161
+ * This callback function is passed a single boolean parameter which is TRUE if low power mode is enabled.
162
+ * @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
163
+ * This callback function is passed a single string parameter containing the error message.
164
+ */
165
+ Diagnostic.isLowPowerModeEnabled = function(successCallback, errorCallback){
166
+ return cordova.exec(Diagnostic._ensureBoolean(successCallback),
167
+ errorCallback,
168
+ 'Diagnostic',
169
+ 'isLowPowerModeEnabled',
170
+ []);
171
+ };
172
+
173
+ /**
174
+ * Registers a function to be called when the device low power mode changes.
175
+ *
176
+ * @param {Function} successCallback - The callback which will be called when low power mode changes.
177
+ * This callback function is passed a single boolean parameter which is TRUE if low power mode is enabled.
178
+ */
179
+ Diagnostic.onLowPowerModeChange = function(successCallback) {
180
+ Diagnostic._onLowPowerModeChange = successCallback || function(){};
181
+ };
182
+
157
183
  /**
158
184
  * Checks if mobile data is authorized for this app.
159
185
  * Returns true if the per-app Mobile Data setting is set to enabled (regardless of whether the device is currently connected to a cellular network)
@@ -1228,7 +1254,7 @@ var Diagnostic = (function(){
1228
1254
  }
1229
1255
  };
1230
1256
 
1231
-
1257
+ Diagnostic._onLowPowerModeChange = function(){};
1232
1258
 
1233
1259
  return Diagnostic;
1234
1260
  })();
@@ -1,8 +0,0 @@
1
- {
2
- "workspace_id": "3d27-7072-f841-12e1",
3
- "workspace_id_at": "2026-02-24T14:03:38.792Z",
4
- "project_name": "cordova-diagnostic-plugin",
5
- "cloud_sync": false,
6
- "git_id": "303b-c6e9-a865-86a3",
7
- "git_id_at": "2026-02-24T14:03:38.810Z"
8
- }