├── www └── DozeOptimize.js ├── package.json ├── plugin.xml ├── README.md └── src └── android └── DozeOptimize.java /www/DozeOptimize.js: -------------------------------------------------------------------------------- 1 | var exec = require('cordova/exec'); 2 | 3 | exports.RequestOptimizations = function(success, error) { 4 | exec(success, error, "DozeOptimize", "RequestOptimizations", []); 5 | }; 6 | 7 | exports.IsIgnoringBatteryOptimizations = function(success, error) { 8 | exec(success, error, "DozeOptimize", "IsIgnoringBatteryOptimizations", []); 9 | }; 10 | 11 | exports.IsIgnoringDataSaver = function(success, error) { 12 | exec(success, error, "DozeOptimize", "IsIgnoringDataSaver", []); 13 | }; 14 | 15 | exports.RequestOptimizationsMenu = function(success, error) { 16 | exec(success, error, "DozeOptimize", "RequestOptimizationsMenu", []); 17 | }; 18 | 19 | exports.RequestDataSaverMenu = function(success, error) { 20 | exec(success, error, "DozeOptimize", "RequestDataSaverMenu", []); 21 | }; 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cordova-plugin-doze-optimize", 3 | "version": "0.1.0", 4 | "description": "If you want to run your application in background mode and it shoud standby even battery optimization enabled, then this Cordova plugin is used to check the doze or battery optimization status and also it help to request whitlest popup for battery optimization", 5 | "cordova": { 6 | "id": "cordova-plugin-doze-optimize", 7 | "platforms": [ 8 | "android" 9 | ] 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/thomas550i/cordova-plugin-doze-Optimize.git" 14 | }, 15 | "keywords": [ 16 | "ecosystem:cordova", 17 | "cordova-android" 18 | ], 19 | "author": "thomas550i", 20 | "license": "ISC", 21 | "bugs": { 22 | "url": "https://github.com/thomas550i/cordova-plugin-doze-Optimize/issues" 23 | }, 24 | "homepage": "https://github.com/thomas550i/cordova-plugin-doze-Optimize#readme" 25 | } 26 | -------------------------------------------------------------------------------- /plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | DozeOptimize 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cordova-plugin-doze-Optimize 2 | 3 | # Whitelisting an Android application programmatically 4 | 5 | If you want to run your application in background mode and it should standby even battery optimization enabled, then this Cordova plugin is used to check the doze or battery optimization status and also it help to request whitelist popup for battery optimization. 6 | 7 | This will also help check if your app is whitelisted from the Data Saver options in Android 7+ 8 | 9 | 10 | 11 | ## Getting Started 12 | 13 | You have to install this plugin in to your cordova project 14 | 15 | ### Installing 16 | 17 | What things you need to install the software and how to install them 18 | ``` 19 | cordova plugin add cordova-plugin-doze-optimize 20 | ``` 21 | 22 | OR 23 | 24 | ``` 25 | npm i cordova-plugin-doze-optimize 26 | ``` 27 | 28 | OR 29 | 30 | ``` 31 | cordova plugin add https://github.com/thomas550i/cordova-plugin-doze-Optimize 32 | ``` 33 | 34 | ### Usage 35 | 36 | Sample Code to get the status of your app 37 | 38 | ``` 39 | cordova.plugins.DozeOptimize.IsIgnoringBatteryOptimizations(function (responce){ 40 | console.log("IsIgnoringBatteryOptimizations: "+responce); 41 | if(responce=="false") 42 | { 43 | console.log("Application not Ignoring Battery Optimizations"); 44 | } 45 | else 46 | { 47 | console.log("Application already Ignoring Battery Optimizations"); 48 | } 49 | }, function (error){ 50 | console.error("IsIgnoringBatteryOptimizations Error"+error); 51 | 52 | }); 53 | ``` 54 | 55 | ``` 56 | cordova.plugins.DozeOptimize.IsIgnoringDataSaver(function (response){ 57 | if(responce=="false") 58 | { 59 | console.log("Application not Ignoring data saver"); 60 | } 61 | else 62 | { 63 | console.log("Application already Ignoring data saver and is probably whitelisted."); 64 | } 65 | }, function (error){ 66 | console.log(error); 67 | }); 68 | ``` 69 | 70 | Sample Code to Popup ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS to whitelist your app. 71 | 72 | ``` 73 | cordova.plugins.DozeOptimize.RequestOptimizations(function (responce){ 74 | console.log(responce); // Will give "Optimizations Requested Successfully" 75 | }, function (error){ 76 | console.error("BatteryOptimizations Request Error"+error); 77 | }); 78 | ``` 79 | 80 | Full Code of usage with step one and two 81 | 82 | ``` 83 | cordova.plugins.DozeOptimize.IsIgnoringBatteryOptimizations(function (responce){ 84 | console.log("IsIgnoringBatteryOptimizations: "+responce); 85 | if(responce=="false") 86 | { 87 | cordova.plugins.DozeOptimize.RequestOptimizations(function (responce){ 88 | console.log(responce); 89 | }, function (error){ 90 | console.error("BatteryOptimizations Request Error"+error); 91 | }); 92 | } 93 | else 94 | { 95 | console.log("Application already Ignoring Battery Optimizations"); 96 | } 97 | }, function (error){ 98 | console.error("IsIgnoringBatteryOptimizations Error"+error); 99 | }); 100 | ``` 101 | ### Important notes 102 | 103 | Battery Optimization will work only Android 6.0 and higher. if you using this plugin below 6.0 you will get "BATTERY_OPTIMIZATIONS Not available" as a result. 104 | 105 | # Contributors are welcome! send Request to thomas550i@gmail.com 106 | -------------------------------------------------------------------------------- /src/android/DozeOptimize.java: -------------------------------------------------------------------------------- 1 | package cordova.plugins.DozeOptimize; 2 | 3 | import org.apache.cordova.CordovaPlugin; 4 | import org.apache.cordova.CallbackContext; 5 | 6 | import org.json.JSONArray; 7 | import org.json.JSONException; 8 | import org.json.JSONObject; 9 | 10 | import android.content.Context; 11 | import org.apache.cordova.CordovaWebView; 12 | import org.apache.cordova.CordovaInterface; 13 | import org.apache.cordova.PluginResult; 14 | import org.apache.cordova.PluginResult.Status; 15 | 16 | 17 | 18 | 19 | import android.os.Build; 20 | import android.os.Environment; 21 | import android.util.Base64; 22 | import android.net.Uri; 23 | import android.net.ConnectivityManager; 24 | import android.content.Context; 25 | import android.content.Intent; 26 | import android.os.PowerManager; 27 | import android.provider.Settings; 28 | import android.provider.Settings.SettingNotFoundException; 29 | 30 | import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_DISABLED; 31 | import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED; 32 | import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_WHITELISTED; 33 | 34 | /** 35 | * This class echoes a string called from JavaScript. 36 | */ 37 | public class DozeOptimize extends CordovaPlugin { 38 | 39 | @Override 40 | public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { 41 | 42 | Context context = cordova.getActivity().getApplicationContext(); 43 | String packageName = context.getPackageName(); 44 | 45 | if (action.equals("IsIgnoringBatteryOptimizations")) { 46 | try { 47 | 48 | if (Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1) { 49 | 50 | String message =""; 51 | PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 52 | if (pm.isIgnoringBatteryOptimizations(packageName)) { 53 | message ="true"; 54 | } 55 | else 56 | { 57 | message ="false"; 58 | } 59 | callbackContext.success(message); 60 | return true; 61 | } 62 | else 63 | { 64 | callbackContext.error("BATTERY_OPTIMIZATIONS Not available."); 65 | return false; 66 | } 67 | } catch (Exception e) { 68 | callbackContext.error("IsIgnoringBatteryOptimizations: failed N/A"); 69 | return false; 70 | } 71 | }else if (action.equals("RequestOptimizations")) { 72 | try { 73 | 74 | if (Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1) { 75 | 76 | String message ="Optimizations Requested Successfully"; 77 | 78 | Intent intent = new Intent(); 79 | intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); 80 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 81 | intent.setData(Uri.parse("package:" + packageName)); 82 | context.startActivity(intent); 83 | 84 | callbackContext.success(message); 85 | return true; 86 | } 87 | else 88 | { 89 | callbackContext.error("BATTERY_OPTIMIZATIONS Not available."); 90 | return false; 91 | } 92 | } catch (Exception e) { 93 | callbackContext.error("N/A"); 94 | return false; 95 | } 96 | } else if (action.equals("RequestOptimizationsMenu")) { 97 | try { 98 | 99 | if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) { 100 | 101 | Intent intent = new Intent(); 102 | PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 103 | if (pm.isIgnoringBatteryOptimizations(packageName)){ 104 | intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); 105 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 106 | context.startActivity(intent); 107 | } 108 | 109 | callbackContext.success("requested"); 110 | return true; 111 | } 112 | else 113 | { 114 | callbackContext.error("BATTERY_OPTIMIZATIONS Not available."); 115 | return false; 116 | } 117 | } catch (Exception e) { 118 | callbackContext.error("RequestOptimizationsMenu: failed N/A"); 119 | return false; 120 | } 121 | }else if(action.equals("IsIgnoringDataSaver")) { 122 | try { 123 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 124 | 125 | String message =""; 126 | ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 127 | 128 | switch (connMgr.getRestrictBackgroundStatus()) 129 | { 130 | case RESTRICT_BACKGROUND_STATUS_ENABLED: 131 | // The app is whitelisted. Wherever possible, 132 | // the app should use less data in the foreground and background. 133 | message = "false"; 134 | break; 135 | 136 | case RESTRICT_BACKGROUND_STATUS_WHITELISTED: 137 | // Background data usage is blocked for this app. Wherever possible, 138 | // the app should also use less data in the foreground. 139 | case RESTRICT_BACKGROUND_STATUS_DISABLED: 140 | // Data Saver is disabled. Since the device is connected to a 141 | // metered network, the app should use less data wherever possible. 142 | message = "true"; 143 | break; 144 | } 145 | callbackContext.success(message); 146 | return true; 147 | 148 | } 149 | else 150 | { 151 | callbackContext.error("DATA_SAVER Not available."); 152 | return false; 153 | } 154 | } catch (Exception e) { 155 | callbackContext.error("IsIgnoringDataSaver: failed N/A"); 156 | return false; 157 | } 158 | }else if (action.equals("RequestDataSaverMenu")) { 159 | try { 160 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 161 | Intent intent = new Intent(); 162 | ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 163 | 164 | intent.setAction(Settings.ACTION_IGNORE_BACKGROUND_DATA_RESTRICTIONS_SETTINGS); 165 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 166 | intent.setData(Uri.parse("package:" + packageName)); 167 | context.startActivity(intent); 168 | 169 | callbackContext.success("requested"); 170 | return true; 171 | }else{ 172 | callbackContext.error("DATA_SAVER Not available."); 173 | return false; 174 | } 175 | } catch (Exception e) { 176 | callbackContext.error("RequestDataSaverMenu failed: N/A"); 177 | return false; 178 | } 179 | } 180 | return false; 181 | } 182 | } 183 | --------------------------------------------------------------------------------