Merged in Menardi/cordova-plugin-run-in-background/android_optimization (pull request #2)

Add more Android battery optimisation functions

- Go to Battery Optimization settings screen
- Add function to check if battery optimization is enabled or not
This commit is contained in:
Menardi
2019-08-21 20:18:23 +00:00
committed by Nathan Kerr
parent 0da2006f7c
commit 53f3278722
3 changed files with 96 additions and 1 deletions

View File

@@ -55,6 +55,7 @@ import static android.content.pm.PackageManager.MATCH_DEFAULT_ONLY;
import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.M;
import static android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS;
import static android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS;
import static android.view.WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON;
import static android.view.WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
@@ -90,6 +91,12 @@ public class BackgroundModeExt extends CordovaPlugin {
case "battery":
disableBatteryOptimizations();
break;
case "batterysettings":
openBatterySettings();
break;
case "optimizationstatus":
isIgnoringBatteryOptimizations(callback);
break;
case "webview":
disableWebViewOptimizations();
break;
@@ -209,6 +216,39 @@ public class BackgroundModeExt extends CordovaPlugin {
cordova.getActivity().startActivity(intent);
}
/**
* Opens the Battery Optimization settings screen
*/
private void openBatterySettings()
{
if (SDK_INT < M)
return;
Activity activity = cordova.getActivity();
Intent intent = new Intent(ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
cordova.getActivity().startActivity(intent);
}
/**
* Opens the Battery Optimization settings screen
*
* @param callback The callback to invoke.
*/
private void isIgnoringBatteryOptimizations(CallbackContext callback)
{
if (SDK_INT < M)
return;
Activity activity = cordova.getActivity();
String pkgName = activity.getPackageName();
PowerManager pm = (PowerManager)getService(POWER_SERVICE);
boolean isIgnoring = pm.isIgnoringBatteryOptimizations(pkgName);
PluginResult res = new PluginResult(Status.OK, isIgnoring);
callback.sendPluginResult(res);
}
/**
* Opens the system settings dialog where the user can tweak or turn off any
* custom app start settings added by the manufacturer if available.