mirror of
https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background
synced 2025-12-16 19:33:23 +00:00
Compare commits
1 Commits
kerr/preve
...
issue/#5-f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
48a06fc3eb |
28
README.md
28
README.md
@@ -81,8 +81,6 @@ cordova.plugins.backgroundMode.un('EVENT', function);
|
|||||||
### Transit between application states
|
### Transit between application states
|
||||||
Android allows to programmatically move from foreground to background or vice versa.
|
Android allows to programmatically move from foreground to background or vice versa.
|
||||||
|
|
||||||
Note: starting with Android 10, you must request the "Draw on Top" permission from the user or the call to `moveToForeground` will silently fail. You can request it with `cordova.plugins.backgroundMode.requestForegroundPermission();`. This permission isn't necessary for `moveToBackground`
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
cordova.plugins.backgroundMode.moveToBackground();
|
cordova.plugins.backgroundMode.moveToBackground();
|
||||||
// or
|
// or
|
||||||
@@ -124,33 +122,10 @@ cordova.plugins.backgroundMode.unlock();
|
|||||||
|
|
||||||
### Request to disable battery optimizations
|
### Request to disable battery optimizations
|
||||||
Starting in Android 8, apps can be put to sleep to conserve battery. When this happens (usually after 5 minutes or so), the background task is killed. This will cause things like MQTT connections to break.
|
Starting in Android 8, apps can be put to sleep to conserve battery. When this happens (usually after 5 minutes or so), the background task is killed. This will cause things like MQTT connections to break.
|
||||||
|
|
||||||
This method will show a permission prompt for the user (only if the app hasn't been granted permission) to ignore the optimization.
|
This method will show a permission prompt for the user (only if the app hasn't been granted permission) to ignore the optimization.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
cordova.plugins.backgroundMode.disableBatteryOptimizations();
|
|
||||||
```
|
|
||||||
|
|
||||||
You can also open the battery optimization settings menu directly, and get the user to set it manually. This may be a better option for devices which may ignore the prompt above.
|
|
||||||
|
|
||||||
```js
|
|
||||||
cordova.plugins.backgroundMode.openBatteryOptimizationsSettings();
|
|
||||||
```
|
|
||||||
|
|
||||||
To check if battery optimizations are disabled for the app:
|
|
||||||
|
|
||||||
```js
|
|
||||||
cordova.plugins.backgroundMode.isIgnoringBatteryOptimizations(function(isIgnoring) {
|
|
||||||
...
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
Additionally, you may find that your JS code begins to run less frequently, or not at all while in the background. This can be due to the webview slowing down its execution due to being in the background. The `disableWebViewOptimizations` function can prevent that, but it's important that it is run _after_ the app goes to the background.
|
|
||||||
|
|
||||||
```js
|
|
||||||
cordova.plugins.backgroundMode.on('activate', function() {
|
|
||||||
cordova.plugins.backgroundMode.disableWebViewOptimizations();
|
cordova.plugins.backgroundMode.disableWebViewOptimizations();
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Notification
|
### Notification
|
||||||
@@ -174,8 +149,7 @@ cordova.plugins.backgroundMode.setDefaults({
|
|||||||
allowClose: Boolean, // add a "Close" action to the notification
|
allowClose: Boolean, // add a "Close" action to the notification
|
||||||
closeIcon: 'power', // An icon shown for the close action
|
closeIcon: 'power', // An icon shown for the close action
|
||||||
closeTitle: 'Close', // The text for the close action
|
closeTitle: 'Close', // The text for the close action
|
||||||
showWhen: Boolean, //(Default: true) Show the time since the notification was created
|
showWhen: Boolean //(Default: true) Show the time since the notification was created
|
||||||
visibility: String, // Android only: one of 'private' (default), 'public' or 'secret' (see https://developer.android.com/reference/android/app/Notification.Builder.html#setVisibility(int))
|
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "cordova-plugin-background-mode",
|
"name": "cordova-plugin-background-mode",
|
||||||
"version": "0.7.3",
|
"version": "0.7.2",
|
||||||
"description": "Prevent apps from going to sleep in background.",
|
"description": "Prevent apps from going to sleep in background.",
|
||||||
"cordova": {
|
"cordova": {
|
||||||
"id": "cordova-plugin-background-mode",
|
"id": "cordova-plugin-background-mode",
|
||||||
|
|||||||
@@ -3,13 +3,13 @@
|
|||||||
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
|
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
id="cordova-plugin-background-mode"
|
id="cordova-plugin-background-mode"
|
||||||
version="0.7.3">
|
version="0.7.2">
|
||||||
|
|
||||||
<name>BackgroundMode</name>
|
<name>BackgroundMode</name>
|
||||||
|
|
||||||
<description>Prevent apps from going to sleep in background.</description>
|
<description>Prevent apps from going to sleep in background.</description>
|
||||||
|
|
||||||
<repo>https://bitbucket.org:TheBosZ/cordova-plugin-run-in-background.git</repo>
|
<repo>https://github.com/katzer/cordova-plugin-background-mode.git</repo>
|
||||||
|
|
||||||
<keywords>appplant, background</keywords>
|
<keywords>appplant, background</keywords>
|
||||||
|
|
||||||
@@ -78,7 +78,6 @@
|
|||||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||||
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
|
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
|
||||||
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
|
|
||||||
</config-file>
|
</config-file>
|
||||||
|
|
||||||
<source-file
|
<source-file
|
||||||
@@ -94,7 +93,6 @@
|
|||||||
target-dir="src/de/appplant/cordova/plugin/background" />
|
target-dir="src/de/appplant/cordova/plugin/background" />
|
||||||
|
|
||||||
<framework src="com.android.support:support-compat:27.1.1" />
|
<framework src="com.android.support:support-compat:27.1.1" />
|
||||||
<framework src="com.github.judemanutd:autostarter:1.1.0" />
|
|
||||||
<resource-file src="src/android/res/drawable/power.xml" target="res/drawable/power.xml" />
|
<resource-file src="src/android/res/drawable/power.xml" target="res/drawable/power.xml" />
|
||||||
<resource-file src="src/android/res/drawable-hdpi/power.png" target="res/drawable-hdpi/power.png" />
|
<resource-file src="src/android/res/drawable-hdpi/power.png" target="res/drawable-hdpi/power.png" />
|
||||||
<resource-file src="src/android/res/drawable-mdpi/power.png" target="res/drawable-mdpi/power.png" />
|
<resource-file src="src/android/res/drawable-mdpi/power.png" target="res/drawable-mdpi/power.png" />
|
||||||
|
|||||||
@@ -34,11 +34,8 @@ import android.content.pm.PackageManager;
|
|||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.PowerManager;
|
import android.os.PowerManager;
|
||||||
import android.provider.Settings;
|
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
import com.judemanutd.autostarter.AutoStartPermissionHelper;
|
|
||||||
|
|
||||||
import org.apache.cordova.CallbackContext;
|
import org.apache.cordova.CallbackContext;
|
||||||
import org.apache.cordova.CordovaPlugin;
|
import org.apache.cordova.CordovaPlugin;
|
||||||
import org.apache.cordova.PluginResult;
|
import org.apache.cordova.PluginResult;
|
||||||
@@ -58,15 +55,14 @@ import static android.content.pm.PackageManager.MATCH_DEFAULT_ONLY;
|
|||||||
import static android.os.Build.VERSION.SDK_INT;
|
import static android.os.Build.VERSION.SDK_INT;
|
||||||
import static android.os.Build.VERSION_CODES.M;
|
import static android.os.Build.VERSION_CODES.M;
|
||||||
import static android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS;
|
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_ALLOW_LOCK_WHILE_SCREEN_ON;
|
||||||
import static android.view.WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
|
import static android.view.WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
|
||||||
import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
|
import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
|
||||||
import static android.view.WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
|
import static android.view.WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements extended functions around the main purpose of infinite execution
|
* Implements extended functions around the main purpose
|
||||||
* in the background.
|
* of infinite execution in the background.
|
||||||
*/
|
*/
|
||||||
public class BackgroundModeExt extends CordovaPlugin {
|
public class BackgroundModeExt extends CordovaPlugin {
|
||||||
|
|
||||||
@@ -78,24 +74,22 @@ public class BackgroundModeExt extends CordovaPlugin {
|
|||||||
*
|
*
|
||||||
* @param action The action to execute.
|
* @param action The action to execute.
|
||||||
* @param args The exec() arguments.
|
* @param args The exec() arguments.
|
||||||
* @param callback The callback context used when calling back into JavaScript.
|
* @param callback The callback context used when
|
||||||
|
* calling back into JavaScript.
|
||||||
*
|
*
|
||||||
* @return Returning false results in a "MethodNotFound" error.
|
* @return Returning false results in a "MethodNotFound" error.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean execute(String action, JSONArray args, CallbackContext callback) {
|
public boolean execute (String action, JSONArray args,
|
||||||
|
CallbackContext callback)
|
||||||
|
{
|
||||||
boolean validAction = true;
|
boolean validAction = true;
|
||||||
|
|
||||||
switch (action) {
|
switch (action)
|
||||||
|
{
|
||||||
case "battery":
|
case "battery":
|
||||||
disableBatteryOptimizations();
|
disableBatteryOptimizations();
|
||||||
break;
|
break;
|
||||||
case "batterysettings":
|
|
||||||
openBatterySettings();
|
|
||||||
break;
|
|
||||||
case "optimizationstatus":
|
|
||||||
isIgnoringBatteryOptimizations(callback);
|
|
||||||
break;
|
|
||||||
case "webview":
|
case "webview":
|
||||||
disableWebViewOptimizations();
|
disableWebViewOptimizations();
|
||||||
break;
|
break;
|
||||||
@@ -108,9 +102,6 @@ public class BackgroundModeExt extends CordovaPlugin {
|
|||||||
case "foreground":
|
case "foreground":
|
||||||
moveToForeground();
|
moveToForeground();
|
||||||
break;
|
break;
|
||||||
case "requestTopPermissions":
|
|
||||||
requestTopPermissions();
|
|
||||||
break;
|
|
||||||
case "tasklist":
|
case "tasklist":
|
||||||
excludeFromTaskList();
|
excludeFromTaskList();
|
||||||
break;
|
break;
|
||||||
@@ -124,12 +115,6 @@ public class BackgroundModeExt extends CordovaPlugin {
|
|||||||
wakeup();
|
wakeup();
|
||||||
unlock();
|
unlock();
|
||||||
break;
|
break;
|
||||||
case "mightAutoKill":
|
|
||||||
mightAutoKill(callback);
|
|
||||||
break;
|
|
||||||
case "showAutoKill":
|
|
||||||
showAutoKill();
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
validAction = false;
|
validAction = false;
|
||||||
}
|
}
|
||||||
@@ -146,7 +131,8 @@ public class BackgroundModeExt extends CordovaPlugin {
|
|||||||
/**
|
/**
|
||||||
* Moves the app to the background.
|
* Moves the app to the background.
|
||||||
*/
|
*/
|
||||||
private void moveToBackground() {
|
private void moveToBackground()
|
||||||
|
{
|
||||||
Intent intent = new Intent(Intent.ACTION_MAIN);
|
Intent intent = new Intent(Intent.ACTION_MAIN);
|
||||||
|
|
||||||
intent.addCategory(Intent.CATEGORY_HOME);
|
intent.addCategory(Intent.CATEGORY_HOME);
|
||||||
@@ -157,12 +143,15 @@ public class BackgroundModeExt extends CordovaPlugin {
|
|||||||
/**
|
/**
|
||||||
* Moves the app to the foreground.
|
* Moves the app to the foreground.
|
||||||
*/
|
*/
|
||||||
private void moveToForeground() {
|
private void moveToForeground()
|
||||||
|
{
|
||||||
Activity app = getApp();
|
Activity app = getApp();
|
||||||
Intent intent = getLaunchIntent();
|
Intent intent = getLaunchIntent();
|
||||||
|
|
||||||
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP
|
intent.addFlags(
|
||||||
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |
|
||||||
|
Intent.FLAG_ACTIVITY_SINGLE_TOP |
|
||||||
|
Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||||
|
|
||||||
clearScreenAndKeyguardFlags();
|
clearScreenAndKeyguardFlags();
|
||||||
app.startActivity(intent);
|
app.startActivity(intent);
|
||||||
@@ -177,10 +166,12 @@ public class BackgroundModeExt extends CordovaPlugin {
|
|||||||
try {
|
try {
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
getApp().runOnUiThread(() -> {
|
getApp().runOnUiThread(() -> {
|
||||||
View view = webView.getView();
|
View view = webView.getEngine().getView();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Class.forName("org.crosswalk.engine.XWalkCordovaView").getMethod("onShow").invoke(view);
|
Class.forName("org.crosswalk.engine.XWalkCordovaView")
|
||||||
|
.getMethod("onShow")
|
||||||
|
.invoke(view);
|
||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
view.dispatchWindowVisibilityChanged(View.VISIBLE);
|
view.dispatchWindowVisibilityChanged(View.VISIBLE);
|
||||||
}
|
}
|
||||||
@@ -195,11 +186,12 @@ public class BackgroundModeExt extends CordovaPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disables battery optimizations for the app. Requires
|
* Disables battery optimizations for the app.
|
||||||
* permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS to function.
|
* Requires permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS to function.
|
||||||
*/
|
*/
|
||||||
@SuppressLint("BatteryLife")
|
@SuppressLint("BatteryLife")
|
||||||
private void disableBatteryOptimizations() {
|
private void disableBatteryOptimizations()
|
||||||
|
{
|
||||||
Activity activity = cordova.getActivity();
|
Activity activity = cordova.getActivity();
|
||||||
Intent intent = new Intent();
|
Intent intent = new Intent();
|
||||||
String pkgName = activity.getPackageName();
|
String pkgName = activity.getPackageName();
|
||||||
@@ -217,68 +209,27 @@ public class BackgroundModeExt extends CordovaPlugin {
|
|||||||
cordova.getActivity().startActivity(intent);
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void requestTopPermissions() {
|
|
||||||
if (SDK_INT >= M) {
|
|
||||||
|
|
||||||
Activity activity = cordova.getActivity();
|
|
||||||
if (Settings.canDrawOverlays(activity.getApplicationContext())) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String pkgName = activity.getPackageName();
|
|
||||||
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + pkgName));
|
|
||||||
activity.startActivity(intent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Opens the system settings dialog where the user can tweak or turn off any
|
* Opens the system settings dialog where the user can tweak or turn off any
|
||||||
* custom app start settings added by the manufacturer if available.
|
* custom app start settings added by the manufacturer if available.
|
||||||
*
|
*
|
||||||
* @param arg Text and title for the dialog or false to skip the dialog.
|
* @param arg Text and title for the dialog or false to skip the dialog.
|
||||||
*/
|
*/
|
||||||
private void openAppStart(Object arg) {
|
private void openAppStart (Object arg)
|
||||||
|
{
|
||||||
Activity activity = cordova.getActivity();
|
Activity activity = cordova.getActivity();
|
||||||
PackageManager pm = activity.getPackageManager();
|
PackageManager pm = activity.getPackageManager();
|
||||||
|
|
||||||
for (Intent intent : getAppStartIntents()) {
|
for (Intent intent : getAppStartIntents())
|
||||||
if (pm.resolveActivity(intent, MATCH_DEFAULT_ONLY) != null) {
|
{
|
||||||
|
if (pm.resolveActivity(intent, MATCH_DEFAULT_ONLY) != null)
|
||||||
|
{
|
||||||
JSONObject spec = (arg instanceof JSONObject) ? (JSONObject) arg : null;
|
JSONObject spec = (arg instanceof JSONObject) ? (JSONObject) arg : null;
|
||||||
|
|
||||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
|
|
||||||
if (arg instanceof Boolean && !((Boolean) arg)) {
|
if (arg instanceof Boolean && !((Boolean) arg))
|
||||||
|
{
|
||||||
activity.startActivity(intent);
|
activity.startActivity(intent);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -286,17 +237,20 @@ public class BackgroundModeExt extends CordovaPlugin {
|
|||||||
AlertDialog.Builder dialog = new AlertDialog.Builder(activity, Theme_DeviceDefault_Light_Dialog);
|
AlertDialog.Builder dialog = new AlertDialog.Builder(activity, Theme_DeviceDefault_Light_Dialog);
|
||||||
|
|
||||||
dialog.setPositiveButton(ok, (o, d) -> activity.startActivity(intent));
|
dialog.setPositiveButton(ok, (o, d) -> activity.startActivity(intent));
|
||||||
dialog.setNegativeButton(cancel, (o, d) -> {
|
dialog.setNegativeButton(cancel, (o, d) -> {});
|
||||||
});
|
|
||||||
dialog.setCancelable(true);
|
dialog.setCancelable(true);
|
||||||
|
|
||||||
if (spec != null && spec.has("title")) {
|
if (spec != null && spec.has("title"))
|
||||||
|
{
|
||||||
dialog.setTitle(spec.optString("title"));
|
dialog.setTitle(spec.optString("title"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (spec != null && spec.has("text")) {
|
if (spec != null && spec.has("text"))
|
||||||
|
{
|
||||||
dialog.setMessage(spec.optString("text"));
|
dialog.setMessage(spec.optString("text"));
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
dialog.setMessage("missing text");
|
dialog.setMessage("missing text");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -311,7 +265,8 @@ public class BackgroundModeExt extends CordovaPlugin {
|
|||||||
* Excludes the app from the recent tasks list.
|
* Excludes the app from the recent tasks list.
|
||||||
*/
|
*/
|
||||||
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
||||||
private void excludeFromTaskList() {
|
private void excludeFromTaskList()
|
||||||
|
{
|
||||||
ActivityManager am = (ActivityManager) getService(ACTIVITY_SERVICE);
|
ActivityManager am = (ActivityManager) getService(ACTIVITY_SERVICE);
|
||||||
|
|
||||||
if (am == null || SDK_INT < 21)
|
if (am == null || SDK_INT < 21)
|
||||||
@@ -331,7 +286,8 @@ public class BackgroundModeExt extends CordovaPlugin {
|
|||||||
* @param callback The callback to invoke.
|
* @param callback The callback to invoke.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
private void isDimmed(CallbackContext callback) {
|
private void isDimmed (CallbackContext callback)
|
||||||
|
{
|
||||||
boolean status = isDimmed();
|
boolean status = isDimmed();
|
||||||
PluginResult res = new PluginResult(Status.OK, status);
|
PluginResult res = new PluginResult(Status.OK, status);
|
||||||
|
|
||||||
@@ -342,10 +298,12 @@ public class BackgroundModeExt extends CordovaPlugin {
|
|||||||
* Returns if the screen is active.
|
* Returns if the screen is active.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
private boolean isDimmed() {
|
private boolean isDimmed()
|
||||||
|
{
|
||||||
PowerManager pm = (PowerManager) getService(POWER_SERVICE);
|
PowerManager pm = (PowerManager) getService(POWER_SERVICE);
|
||||||
|
|
||||||
if (SDK_INT < 20) {
|
if (SDK_INT < 20)
|
||||||
|
{
|
||||||
return !pm.isScreenOn();
|
return !pm.isScreenOn();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -355,7 +313,8 @@ public class BackgroundModeExt extends CordovaPlugin {
|
|||||||
/**
|
/**
|
||||||
* Wakes up the device if the screen isn't still on.
|
* Wakes up the device if the screen isn't still on.
|
||||||
*/
|
*/
|
||||||
private void wakeup() {
|
private void wakeup()
|
||||||
|
{
|
||||||
try {
|
try {
|
||||||
acquireWakeLock();
|
acquireWakeLock();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -366,7 +325,8 @@ public class BackgroundModeExt extends CordovaPlugin {
|
|||||||
/**
|
/**
|
||||||
* Unlocks the device even with password protection.
|
* Unlocks the device even with password protection.
|
||||||
*/
|
*/
|
||||||
private void unlock() {
|
private void unlock()
|
||||||
|
{
|
||||||
addSreenAndKeyguardFlags();
|
addSreenAndKeyguardFlags();
|
||||||
getApp().startActivity(getLaunchIntent());
|
getApp().startActivity(getLaunchIntent());
|
||||||
}
|
}
|
||||||
@@ -375,7 +335,8 @@ public class BackgroundModeExt extends CordovaPlugin {
|
|||||||
* Acquires a wake lock to wake up the device.
|
* Acquires a wake lock to wake up the device.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
private void acquireWakeLock() {
|
private void acquireWakeLock()
|
||||||
|
{
|
||||||
PowerManager pm = (PowerManager) getService(POWER_SERVICE);
|
PowerManager pm = (PowerManager) getService(POWER_SERVICE);
|
||||||
|
|
||||||
releaseWakeLock();
|
releaseWakeLock();
|
||||||
@@ -383,7 +344,8 @@ public class BackgroundModeExt extends CordovaPlugin {
|
|||||||
if (!isDimmed())
|
if (!isDimmed())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
int level = PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP;
|
int level = PowerManager.SCREEN_DIM_WAKE_LOCK |
|
||||||
|
PowerManager.ACQUIRE_CAUSES_WAKEUP;
|
||||||
|
|
||||||
wakeLock = pm.newWakeLock(level, "backgroundmode:wakelock");
|
wakeLock = pm.newWakeLock(level, "backgroundmode:wakelock");
|
||||||
wakeLock.setReferenceCounted(false);
|
wakeLock.setReferenceCounted(false);
|
||||||
@@ -393,51 +355,35 @@ public class BackgroundModeExt extends CordovaPlugin {
|
|||||||
/**
|
/**
|
||||||
* Releases the previously acquire wake lock.
|
* Releases the previously acquire wake lock.
|
||||||
*/
|
*/
|
||||||
private void releaseWakeLock() {
|
private void releaseWakeLock()
|
||||||
|
{
|
||||||
if (wakeLock != null && wakeLock.isHeld()) {
|
if (wakeLock != null && wakeLock.isHeld()) {
|
||||||
wakeLock.release();
|
wakeLock.release();
|
||||||
wakeLock = null;
|
wakeLock = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showAutoKill() {
|
|
||||||
Context context = getApp().getApplicationContext();
|
|
||||||
AutoStartPermissionHelper instance = AutoStartPermissionHelper.Companion.getInstance();
|
|
||||||
boolean isKilled = instance.isAutoStartPermissionAvailable(context, false);
|
|
||||||
if (isKilled) {
|
|
||||||
instance.getAutoStartPermission(context, true, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void mightAutoKill(CallbackContext callback) {
|
|
||||||
Context context = getApp().getApplicationContext();
|
|
||||||
AutoStartPermissionHelper instance = AutoStartPermissionHelper.Companion.getInstance();
|
|
||||||
boolean isKilled = instance.isAutoStartPermissionAvailable(context, false);
|
|
||||||
PluginResult res = new PluginResult(Status.OK, isKilled);
|
|
||||||
|
|
||||||
callback.sendPluginResult(res);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds required flags to the window to unlock/wakeup the device.
|
* Adds required flags to the window to unlock/wakeup the device.
|
||||||
*/
|
*/
|
||||||
private void addSreenAndKeyguardFlags() {
|
private void addSreenAndKeyguardFlags()
|
||||||
getApp().runOnUiThread(() -> getApp().getWindow().addFlags(
|
{
|
||||||
FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | FLAG_SHOW_WHEN_LOCKED | FLAG_TURN_SCREEN_ON | FLAG_DISMISS_KEYGUARD));
|
getApp().runOnUiThread(() -> getApp().getWindow().addFlags(FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | FLAG_SHOW_WHEN_LOCKED | FLAG_TURN_SCREEN_ON | FLAG_DISMISS_KEYGUARD));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clears required flags to the window to unlock/wakeup the device.
|
* Clears required flags to the window to unlock/wakeup the device.
|
||||||
*/
|
*/
|
||||||
private void clearScreenAndKeyguardFlags() {
|
private void clearScreenAndKeyguardFlags()
|
||||||
getApp().runOnUiThread(() -> getApp().getWindow().clearFlags(
|
{
|
||||||
FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | FLAG_SHOW_WHEN_LOCKED | FLAG_TURN_SCREEN_ON | FLAG_DISMISS_KEYGUARD));
|
getApp().runOnUiThread(() -> getApp().getWindow().clearFlags(FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | FLAG_SHOW_WHEN_LOCKED | FLAG_TURN_SCREEN_ON | FLAG_DISMISS_KEYGUARD));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes required flags to the window to unlock/wakeup the device.
|
* Removes required flags to the window to unlock/wakeup the device.
|
||||||
*/
|
*/
|
||||||
static void clearKeyguardFlags(Activity app) {
|
static void clearKeyguardFlags (Activity app)
|
||||||
|
{
|
||||||
app.runOnUiThread(() -> app.getWindow().clearFlags(FLAG_DISMISS_KEYGUARD));
|
app.runOnUiThread(() -> app.getWindow().clearFlags(FLAG_DISMISS_KEYGUARD));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -451,7 +397,8 @@ public class BackgroundModeExt extends CordovaPlugin {
|
|||||||
/**
|
/**
|
||||||
* Gets the launch intent for the main activity.
|
* Gets the launch intent for the main activity.
|
||||||
*/
|
*/
|
||||||
private Intent getLaunchIntent() {
|
private Intent getLaunchIntent()
|
||||||
|
{
|
||||||
Context app = getApp().getApplicationContext();
|
Context app = getApp().getApplicationContext();
|
||||||
String pkgName = app.getPackageName();
|
String pkgName = app.getPackageName();
|
||||||
|
|
||||||
@@ -463,52 +410,36 @@ public class BackgroundModeExt extends CordovaPlugin {
|
|||||||
*
|
*
|
||||||
* @param name The name of the service.
|
* @param name The name of the service.
|
||||||
*/
|
*/
|
||||||
private Object getService(String name) {
|
private Object getService(String name)
|
||||||
|
{
|
||||||
return getApp().getSystemService(name);
|
return getApp().getSystemService(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns list of all possible intents to present the app start settings.
|
* Returns list of all possible intents to present the app start settings.
|
||||||
*/
|
*/
|
||||||
private List<Intent> getAppStartIntents() {
|
private List<Intent> getAppStartIntents()
|
||||||
|
{
|
||||||
return Arrays.asList(
|
return Arrays.asList(
|
||||||
new Intent().setComponent(new ComponentName("com.miui.securitycenter",
|
new Intent().setComponent(new ComponentName("com.miui.securitycenter","com.miui.permcenter.autostart.AutoStartManagementActivity")),
|
||||||
"com.miui.permcenter.autostart.AutoStartManagementActivity")),
|
new Intent().setComponent(new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity")),
|
||||||
new Intent().setComponent(new ComponentName("com.letv.android.letvsafe",
|
new Intent().setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity")),
|
||||||
"com.letv.android.letvsafe.AutobootManageActivity")),
|
new Intent().setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity")),
|
||||||
new Intent().setComponent(new ComponentName("com.huawei.systemmanager",
|
new Intent().setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity")),
|
||||||
"com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity")),
|
new Intent().setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.startupapp.StartupAppListActivity")),
|
||||||
new Intent().setComponent(new ComponentName("com.huawei.systemmanager",
|
new Intent().setComponent(new ComponentName("com.oppo.safe", "com.oppo.safe.permission.startup.StartupAppListActivity")),
|
||||||
"com.huawei.systemmanager.optimize.process.ProtectActivity")),
|
new Intent().setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity")),
|
||||||
new Intent().setComponent(new ComponentName("com.coloros.safecenter",
|
new Intent().setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.BgStartUpManager")),
|
||||||
"com.coloros.safecenter.permission.startup.StartupAppListActivity")),
|
new Intent().setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity")),
|
||||||
new Intent().setComponent(new ComponentName("com.coloros.safecenter",
|
new Intent().setComponent(new ComponentName("com.asus.mobilemanager", "com.asus.mobilemanager.autostart.AutoStartActivity")),
|
||||||
"com.coloros.safecenter.startupapp.StartupAppListActivity")),
|
new Intent().setComponent(new ComponentName("com.asus.mobilemanager", "com.asus.mobilemanager.entry.FunctionActivity")).setData(android.net.Uri.parse("mobilemanager://function/entry/AutoStart")),
|
||||||
new Intent().setComponent(
|
|
||||||
new ComponentName("com.oppo.safe", "com.oppo.safe.permission.startup.StartupAppListActivity")),
|
|
||||||
new Intent().setComponent(
|
|
||||||
new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity")),
|
|
||||||
new Intent().setComponent(
|
|
||||||
new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.BgStartUpManager")),
|
|
||||||
new Intent().setComponent(new ComponentName("com.vivo.permissionmanager",
|
|
||||||
"com.vivo.permissionmanager.activity.BgStartUpManagerActivity")),
|
|
||||||
new Intent().setComponent(new ComponentName("com.asus.mobilemanager",
|
|
||||||
"com.asus.mobilemanager.autostart.AutoStartActivity")),
|
|
||||||
new Intent()
|
|
||||||
.setComponent(new ComponentName("com.asus.mobilemanager",
|
|
||||||
"com.asus.mobilemanager.entry.FunctionActivity"))
|
|
||||||
.setData(android.net.Uri.parse("mobilemanager://function/entry/AutoStart")),
|
|
||||||
new Intent().setAction("com.letv.android.permissionautoboot"),
|
new Intent().setAction("com.letv.android.permissionautoboot"),
|
||||||
new Intent().setComponent(new ComponentName("com.samsung.android.sm_cn",
|
new Intent().setComponent(new ComponentName("com.samsung.android.sm_cn", "com.samsung.android.sm.ui.ram.AutoRunActivity")),
|
||||||
"com.samsung.android.sm.ui.ram.AutoRunActivity")),
|
|
||||||
new Intent().setComponent(ComponentName.unflattenFromString("com.iqoo.secure/.MainActivity")),
|
new Intent().setComponent(ComponentName.unflattenFromString("com.iqoo.secure/.MainActivity")),
|
||||||
new Intent()
|
new Intent().setComponent(ComponentName.unflattenFromString("com.meizu.safe/.permission.SmartBGActivity")),
|
||||||
.setComponent(ComponentName.unflattenFromString("com.meizu.safe/.permission.SmartBGActivity")),
|
new Intent().setComponent(new ComponentName("com.yulong.android.coolsafe", ".ui.activity.autorun.AutoRunListActivity")),
|
||||||
new Intent().setComponent(
|
new Intent().setComponent(new ComponentName("cn.nubia.security2", "cn.nubia.security.appmanage.selfstart.ui.SelfStartActivity")),
|
||||||
new ComponentName("com.yulong.android.coolsafe", ".ui.activity.autorun.AutoRunListActivity")),
|
new Intent().setComponent(new ComponentName("com.zui.safecenter", "com.lenovo.safecenter.MainTab.LeSafeMainActivity"))
|
||||||
new Intent().setComponent(new ComponentName("cn.nubia.security2",
|
);
|
||||||
"cn.nubia.security.appmanage.selfstart.ui.SelfStartActivity")),
|
|
||||||
new Intent().setComponent(
|
|
||||||
new ComponentName("com.zui.safecenter", "com.lenovo.safecenter.MainTab.LeSafeMainActivity")));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -189,23 +189,17 @@ public class ForegroundService extends Service {
|
|||||||
String text = settings.optString("text", NOTIFICATION_TEXT);
|
String text = settings.optString("text", NOTIFICATION_TEXT);
|
||||||
boolean bigText = settings.optBoolean("bigText", false);
|
boolean bigText = settings.optBoolean("bigText", false);
|
||||||
String subText = settings.optString("subText", "");
|
String subText = settings.optString("subText", "");
|
||||||
String visibility = settings.optString("visibility", "");
|
|
||||||
|
|
||||||
Context context = getApplicationContext();
|
Context context = getApplicationContext();
|
||||||
String pkgName = context.getPackageName();
|
String pkgName = context.getPackageName();
|
||||||
Intent intent = context.getPackageManager()
|
Intent intent = context.getPackageManager()
|
||||||
.getLaunchIntentForPackage(pkgName);
|
.getLaunchIntentForPackage(pkgName);
|
||||||
|
|
||||||
int smallIcon = getIconResId(settings);
|
|
||||||
if (smallIcon == 0) { //If no icon at all was found, fall back to the app's icon
|
|
||||||
smallIcon = context.getApplicationInfo().icon;
|
|
||||||
}
|
|
||||||
|
|
||||||
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, CHANNEL_ID)
|
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, CHANNEL_ID)
|
||||||
.setContentTitle(title)
|
.setContentTitle(title)
|
||||||
.setContentText(text)
|
.setContentText(text)
|
||||||
.setOngoing(true)
|
.setOngoing(true)
|
||||||
.setSmallIcon(smallIcon)
|
.setSmallIcon(getIconResId(settings))
|
||||||
.setShowWhen(settings.optBoolean("showWhen", true));
|
.setShowWhen(settings.optBoolean("showWhen", true));
|
||||||
|
|
||||||
if (!subText.equals("")) {
|
if (!subText.equals("")) {
|
||||||
@@ -230,10 +224,6 @@ public class ForegroundService extends Service {
|
|||||||
new NotificationCompat.BigTextStyle().bigText(text));
|
new NotificationCompat.BigTextStyle().bigText(text));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!visibility.equals("")) {
|
|
||||||
notification.setVisibility(getVisibility(visibility));
|
|
||||||
}
|
|
||||||
|
|
||||||
setColor(notification, settings);
|
setColor(notification, settings);
|
||||||
|
|
||||||
if (intent != null && settings.optBoolean("resume")) {
|
if (intent != null && settings.optBoolean("resume")) {
|
||||||
@@ -319,23 +309,6 @@ public class ForegroundService extends Service {
|
|||||||
return res.getIdentifier(icon, type, pkgName);
|
return res.getIdentifier(icon, type, pkgName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the visibility constant from a string.
|
|
||||||
*
|
|
||||||
* @param visibility one of 'public', 'private', 'secret'
|
|
||||||
*
|
|
||||||
* @return The visibility constant if a match is found, 'private' otherwise
|
|
||||||
*/
|
|
||||||
private int getVisibility (String visibility)
|
|
||||||
{
|
|
||||||
if (visibility.equals("public")) {
|
|
||||||
return Notification.VISIBILITY_PUBLIC;
|
|
||||||
} else if (visibility.equals("secret")) {
|
|
||||||
return Notification.VISIBILITY_SECRET;
|
|
||||||
} else {
|
|
||||||
return Notification.VISIBILITY_PRIVATE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* Set notification color if its supported by the SDK.
|
* Set notification color if its supported by the SDK.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -174,9 +174,6 @@ NSString* const kAPPBackgroundEventDeactivate = @"deactivate";
|
|||||||
// even another app starts playing sound
|
// even another app starts playing sound
|
||||||
[session setCategory:AVAudioSessionCategoryPlayback
|
[session setCategory:AVAudioSessionCategoryPlayback
|
||||||
error:NULL];
|
error:NULL];
|
||||||
// Prevent sound/music from stopping when opening the app. Also prevents embedded videos from pausing when unmuted.
|
|
||||||
[session setCategory:AVAudioSessionCategoryAmbient
|
|
||||||
error:NULL];
|
|
||||||
|
|
||||||
// Active the audio session
|
// Active the audio session
|
||||||
[session setActive:YES error:NULL];
|
[session setActive:YES error:NULL];
|
||||||
@@ -244,16 +241,7 @@ NSString* const kAPPBackgroundEventDeactivate = @"deactivate";
|
|||||||
*/
|
*/
|
||||||
+ (NSString*) wkProperty
|
+ (NSString*) wkProperty
|
||||||
{
|
{
|
||||||
NSString * str = @"";
|
NSString* str = @"YWx3YXlzUnVuc0F0Rm9yZWdyb3VuZFByaW9yaXR5";
|
||||||
if (@available(iOS 12.2, *)) {
|
|
||||||
// do stuff for iOS 12.2 and newer
|
|
||||||
NSLog(@"iOS 12.2+ detected");
|
|
||||||
str = @"YWx3YXlzUnVuc0F0Rm9yZWdyb3VuZFByaW9yaXR5";
|
|
||||||
} else {
|
|
||||||
// do stuff for iOS 12.1 and older
|
|
||||||
NSLog(@"iOS Below 12.2 detected");
|
|
||||||
str = @"X2Fsd2F5c1J1bnNBdEZvcmVncm91bmRQcmlvcml0eQ==";
|
|
||||||
}
|
|
||||||
NSData* data = [[NSData alloc] initWithBase64EncodedString:str options:0];
|
NSData* data = [[NSData alloc] initWithBase64EncodedString:str options:0];
|
||||||
|
|
||||||
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
||||||
|
|||||||
@@ -29,7 +29,8 @@ var exec = require('cordova/exec'),
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.enable = function () {
|
exports.enable = function()
|
||||||
|
{
|
||||||
if (this.isEnabled())
|
if (this.isEnabled())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -47,7 +48,8 @@ exports.enable = function () {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.disable = function () {
|
exports.disable = function()
|
||||||
|
{
|
||||||
if (!this.isEnabled())
|
if (!this.isEnabled())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -66,7 +68,8 @@ exports.disable = function () {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.setEnabled = function (enable) {
|
exports.setEnabled = function (enable)
|
||||||
|
{
|
||||||
if (enable) {
|
if (enable) {
|
||||||
this.enable();
|
this.enable();
|
||||||
} else {
|
} else {
|
||||||
@@ -79,7 +82,8 @@ exports.setEnabled = function (enable) {
|
|||||||
*
|
*
|
||||||
* @return [ Object ]
|
* @return [ Object ]
|
||||||
*/
|
*/
|
||||||
exports.getDefaults = function () {
|
exports.getDefaults = function()
|
||||||
|
{
|
||||||
return this._defaults;
|
return this._defaults;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -88,7 +92,8 @@ exports.getDefaults = function () {
|
|||||||
*
|
*
|
||||||
* @return [ Object ]
|
* @return [ Object ]
|
||||||
*/
|
*/
|
||||||
exports.getSettings = function () {
|
exports.getSettings = function()
|
||||||
|
{
|
||||||
return this._settings || {};
|
return this._settings || {};
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -99,16 +104,20 @@ exports.getSettings = function () {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.setDefaults = function (overrides) {
|
exports.setDefaults = function (overrides)
|
||||||
|
{
|
||||||
var defaults = this.getDefaults();
|
var defaults = this.getDefaults();
|
||||||
|
|
||||||
for (var key in defaults) {
|
for (var key in defaults)
|
||||||
if (overrides.hasOwnProperty(key)) {
|
{
|
||||||
|
if (overrides.hasOwnProperty(key))
|
||||||
|
{
|
||||||
defaults[key] = overrides[key];
|
defaults[key] = overrides[key];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this._isAndroid) {
|
if (this._isAndroid)
|
||||||
|
{
|
||||||
cordova.exec(null, null, 'BackgroundMode', 'configure', [defaults, false]);
|
cordova.exec(null, null, 'BackgroundMode', 'configure', [defaults, false]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -121,14 +130,16 @@ exports.setDefaults = function (overrides) {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.configure = function (options) {
|
exports.configure = function (options)
|
||||||
|
{
|
||||||
var settings = this.getSettings(),
|
var settings = this.getSettings(),
|
||||||
defaults = this.getDefaults();
|
defaults = this.getDefaults();
|
||||||
|
|
||||||
if (!this._isAndroid)
|
if (!this._isAndroid)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!this._isActive) {
|
if (!this._isActive)
|
||||||
|
{
|
||||||
console.log('BackgroundMode is not active, skipped...');
|
console.log('BackgroundMode is not active, skipped...');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -145,8 +156,10 @@ exports.configure = function (options) {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.disableWebViewOptimizations = function () {
|
exports.disableWebViewOptimizations = function()
|
||||||
if (this._isAndroid) {
|
{
|
||||||
|
if (this._isAndroid)
|
||||||
|
{
|
||||||
cordova.exec(null, null, 'BackgroundModeExt', 'webview', []);
|
cordova.exec(null, null, 'BackgroundModeExt', 'webview', []);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -156,58 +169,14 @@ exports.disableWebViewOptimizations = function () {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.disableBatteryOptimizations = function () {
|
exports.disableBatteryOptimizations = function()
|
||||||
if (this._isAndroid) {
|
{
|
||||||
|
if (this._isAndroid)
|
||||||
|
{
|
||||||
cordova.exec(null, null, 'BackgroundModeExt', 'battery', []);
|
cordova.exec(null, null, 'BackgroundModeExt', 'battery', []);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Opens the system settings screen for battery optimization, allowing the user to
|
|
||||||
* manually change the optimization settings.
|
|
||||||
*
|
|
||||||
* @return [ Void ]
|
|
||||||
*/
|
|
||||||
exports.openBatteryOptimizationsSettings = function () {
|
|
||||||
if (this._isAndroid) {
|
|
||||||
cordova.exec(null, null, 'BackgroundModeExt', 'batterysettings', []);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Opens the system settings screen for battery optimization, allowing the user to
|
|
||||||
* manually change the optimization settings.
|
|
||||||
*
|
|
||||||
* @return [ Void ]
|
|
||||||
*/
|
|
||||||
exports.isIgnoringBatteryOptimizations = function (callback) {
|
|
||||||
if (this._isAndroid) {
|
|
||||||
cordova.exec(callback, null, 'BackgroundModeExt', 'optimizationstatus', []);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
callback(true);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reports potential "auto kill".
|
|
||||||
* @see https://github.com/judemanutd/AutoStarter
|
|
||||||
* @return [ Boolean ]
|
|
||||||
*/
|
|
||||||
exports.mightAutoKill = function (callback) {
|
|
||||||
if (this._isAndroid) {
|
|
||||||
cordova.exec(callback, null, 'BackgroundModeExt', 'mightAutoKill', []);
|
|
||||||
} else {
|
|
||||||
callback(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.showAutoKill = function () {
|
|
||||||
if (this._isAndroid) {
|
|
||||||
cordova.exec(null, null, 'BackgroundModeExt', 'showAutoKill', []);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Opens the system settings dialog where the user can tweak or turn off any
|
* Opens the system settings dialog where the user can tweak or turn off any
|
||||||
* custom app start settings added by the manufacturer if available.
|
* custom app start settings added by the manufacturer if available.
|
||||||
@@ -217,8 +186,10 @@ exports.showAutoKill = function () {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.openAppStartSettings = function (options) {
|
exports.openAppStartSettings = function (options)
|
||||||
if (this._isAndroid) {
|
{
|
||||||
|
if (this._isAndroid)
|
||||||
|
{
|
||||||
cordova.exec(null, null, 'BackgroundModeExt', 'appstart', [options]);
|
cordova.exec(null, null, 'BackgroundModeExt', 'appstart', [options]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -228,8 +199,10 @@ exports.openAppStartSettings = function (options) {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.moveToBackground = function () {
|
exports.moveToBackground = function()
|
||||||
if (this._isAndroid) {
|
{
|
||||||
|
if (this._isAndroid)
|
||||||
|
{
|
||||||
cordova.exec(null, null, 'BackgroundModeExt', 'background', []);
|
cordova.exec(null, null, 'BackgroundModeExt', 'background', []);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -239,30 +212,23 @@ exports.moveToBackground = function () {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.moveToForeground = function () {
|
exports.moveToForeground = function()
|
||||||
if (this.isActive() && this._isAndroid) {
|
{
|
||||||
|
if (this.isActive() && this._isAndroid)
|
||||||
|
{
|
||||||
cordova.exec(null, null, 'BackgroundModeExt', 'foreground', []);
|
cordova.exec(null, null, 'BackgroundModeExt', 'foreground', []);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Requests permission to "draw on top" which is necessary for the "moveToForeground" method in Android 10+
|
|
||||||
*
|
|
||||||
* @return [ Void ]
|
|
||||||
*/
|
|
||||||
exports.requestForegroundPermission = function () {
|
|
||||||
if (this._isAndroid) {
|
|
||||||
cordova.exec(null, null, 'BackgroundModeExt', 'requestTopPermissions', []);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exclude the app from the recent tasks list (Android only).
|
* Exclude the app from the recent tasks list (Android only).
|
||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.excludeFromTaskList = function () {
|
exports.excludeFromTaskList = function()
|
||||||
if (this._isAndroid) {
|
{
|
||||||
|
if (this._isAndroid)
|
||||||
|
{
|
||||||
cordova.exec(null, null, 'BackgroundModeExt', 'tasklist', []);
|
cordova.exec(null, null, 'BackgroundModeExt', 'tasklist', []);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -273,7 +239,8 @@ exports.excludeFromTaskList = function () {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.overrideBackButton = function () {
|
exports.overrideBackButton = function()
|
||||||
|
{
|
||||||
document.addEventListener('backbutton', function() {
|
document.addEventListener('backbutton', function() {
|
||||||
exports.moveToBackground();
|
exports.moveToBackground();
|
||||||
}, false);
|
}, false);
|
||||||
@@ -286,11 +253,14 @@ exports.overrideBackButton = function () {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.isScreenOff = function (fn) {
|
exports.isScreenOff = function (fn)
|
||||||
if (this._isAndroid) {
|
{
|
||||||
|
if (this._isAndroid)
|
||||||
|
{
|
||||||
cordova.exec(fn, null, 'BackgroundModeExt', 'dimmed', []);
|
cordova.exec(fn, null, 'BackgroundModeExt', 'dimmed', []);
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
fn(undefined);
|
fn(undefined);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -300,8 +270,10 @@ exports.isScreenOff = function (fn) {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.wakeUp = function () {
|
exports.wakeUp = function()
|
||||||
if (this._isAndroid) {
|
{
|
||||||
|
if (this._isAndroid)
|
||||||
|
{
|
||||||
cordova.exec(null, null, 'BackgroundModeExt', 'wakeup', []);
|
cordova.exec(null, null, 'BackgroundModeExt', 'wakeup', []);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -311,8 +283,10 @@ exports.wakeUp = function () {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.unlock = function () {
|
exports.unlock = function()
|
||||||
if (this._isAndroid) {
|
{
|
||||||
|
if (this._isAndroid)
|
||||||
|
{
|
||||||
cordova.exec(null, null, 'BackgroundModeExt', 'unlock', []);
|
cordova.exec(null, null, 'BackgroundModeExt', 'unlock', []);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -322,7 +296,8 @@ exports.unlock = function () {
|
|||||||
*
|
*
|
||||||
* @return [ Boolean ]
|
* @return [ Boolean ]
|
||||||
*/
|
*/
|
||||||
exports.isEnabled = function () {
|
exports.isEnabled = function()
|
||||||
|
{
|
||||||
return this._isEnabled !== false;
|
return this._isEnabled !== false;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -331,7 +306,8 @@ exports.isEnabled = function () {
|
|||||||
*
|
*
|
||||||
* @return [ Boolean ]
|
* @return [ Boolean ]
|
||||||
*/
|
*/
|
||||||
exports.isActive = function () {
|
exports.isActive = function()
|
||||||
|
{
|
||||||
return this._isActive !== false;
|
return this._isActive !== false;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -345,14 +321,16 @@ exports._listener = {};
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.fireEvent = function (event) {
|
exports.fireEvent = function (event)
|
||||||
|
{
|
||||||
var args = Array.apply(null, arguments).slice(1),
|
var args = Array.apply(null, arguments).slice(1),
|
||||||
listener = this._listener[event];
|
listener = this._listener[event];
|
||||||
|
|
||||||
if (!listener)
|
if (!listener)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (var i = 0; i < listener.length; i++) {
|
for (var i = 0; i < listener.length; i++)
|
||||||
|
{
|
||||||
var fn = listener[i][0],
|
var fn = listener[i][0],
|
||||||
scope = listener[i][1];
|
scope = listener[i][1];
|
||||||
|
|
||||||
@@ -369,11 +347,13 @@ exports.fireEvent = function (event) {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.on = function (event, callback, scope) {
|
exports.on = function (event, callback, scope)
|
||||||
|
{
|
||||||
if (typeof callback !== "function")
|
if (typeof callback !== "function")
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!this._listener[event]) {
|
if (!this._listener[event])
|
||||||
|
{
|
||||||
this._listener[event] = [];
|
this._listener[event] = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -390,16 +370,19 @@ exports.on = function (event, callback, scope) {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.un = function (event, callback) {
|
exports.un = function (event, callback)
|
||||||
|
{
|
||||||
var listener = this._listener[event];
|
var listener = this._listener[event];
|
||||||
|
|
||||||
if (!listener)
|
if (!listener)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (var i = 0; i < listener.length; i++) {
|
for (var i = 0; i < listener.length; i++)
|
||||||
|
{
|
||||||
var fn = listener[i][0];
|
var fn = listener[i][0];
|
||||||
|
|
||||||
if (fn == callback) {
|
if (fn == callback)
|
||||||
|
{
|
||||||
listener.splice(i, 1);
|
listener.splice(i, 1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -441,8 +424,7 @@ exports._defaults = {
|
|||||||
allowClose: false,
|
allowClose: false,
|
||||||
closeIcon: 'power',
|
closeIcon: 'power',
|
||||||
closeTitle: 'Close',
|
closeTitle: 'Close',
|
||||||
showWhen: true,
|
showWhen: true
|
||||||
visibility: undefined
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -455,9 +437,12 @@ exports._defaults = {
|
|||||||
*
|
*
|
||||||
* @return [ Object ] Default values merged with custom values.
|
* @return [ Object ] Default values merged with custom values.
|
||||||
*/
|
*/
|
||||||
exports._mergeObjects = function (options, toMergeIn) {
|
exports._mergeObjects = function (options, toMergeIn)
|
||||||
for (var key in toMergeIn) {
|
{
|
||||||
if (!options.hasOwnProperty(key)) {
|
for (var key in toMergeIn)
|
||||||
|
{
|
||||||
|
if (!options.hasOwnProperty(key))
|
||||||
|
{
|
||||||
options[key] = toMergeIn[key];
|
options[key] = toMergeIn[key];
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -476,7 +461,8 @@ exports._mergeObjects = function (options, toMergeIn) {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports._setActive = function (value) {
|
exports._setActive = function(value)
|
||||||
|
{
|
||||||
if (this._isActive == value)
|
if (this._isActive == value)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -494,11 +480,13 @@ exports._setActive = function (value) {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports._pluginInitialize = function () {
|
exports._pluginInitialize = function()
|
||||||
|
{
|
||||||
this._isAndroid = device.platform.match(/^android|amazon/i) !== null;
|
this._isAndroid = device.platform.match(/^android|amazon/i) !== null;
|
||||||
this.setDefaults({});
|
this.setDefaults({});
|
||||||
|
|
||||||
if (device.platform == 'browser') {
|
if (device.platform == 'browser')
|
||||||
|
{
|
||||||
this.enable();
|
this.enable();
|
||||||
this._isEnabled = true;
|
this._isEnabled = true;
|
||||||
}
|
}
|
||||||
@@ -507,19 +495,23 @@ exports._pluginInitialize = function () {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Called before 'deviceready' listener will be called
|
// Called before 'deviceready' listener will be called
|
||||||
channel.onCordovaReady.subscribe(function () {
|
channel.onCordovaReady.subscribe(function()
|
||||||
|
{
|
||||||
channel.onCordovaInfoReady.subscribe(function() {
|
channel.onCordovaInfoReady.subscribe(function() {
|
||||||
exports._pluginInitialize();
|
exports._pluginInitialize();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Called after 'deviceready' event
|
// Called after 'deviceready' event
|
||||||
channel.deviceready.subscribe(function () {
|
channel.deviceready.subscribe(function()
|
||||||
if (exports.isEnabled()) {
|
{
|
||||||
|
if (exports.isEnabled())
|
||||||
|
{
|
||||||
exports.fireEvent('enable');
|
exports.fireEvent('enable');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (exports.isActive()) {
|
if (exports.isActive())
|
||||||
|
{
|
||||||
exports.fireEvent('activate');
|
exports.fireEvent('activate');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user