Merge branch 'master' into master
This commit is contained in:
commit
949a9e721c
@ -60,8 +60,10 @@
|
|||||||
<platform name="android">
|
<platform name="android">
|
||||||
<config-file target="res/xml/config.xml" parent="/*">
|
<config-file target="res/xml/config.xml" parent="/*">
|
||||||
<feature name="BackgroundMode">
|
<feature name="BackgroundMode">
|
||||||
<param name="android-package"
|
<param name="android-package" value="de.appplant.cordova.plugin.background.BackgroundMode"/>
|
||||||
value="de.appplant.cordova.plugin.background.BackgroundMode"/>
|
</feature>
|
||||||
|
<feature name="BackgroundModeExt">
|
||||||
|
<param name="android-package" value="de.appplant.cordova.plugin.background.BackgroundModeExt"/>
|
||||||
</feature>
|
</feature>
|
||||||
</config-file>
|
</config-file>
|
||||||
|
|
||||||
@ -82,7 +84,7 @@
|
|||||||
target-dir="src/de/appplant/cordova/plugin/background" />
|
target-dir="src/de/appplant/cordova/plugin/background" />
|
||||||
|
|
||||||
<source-file
|
<source-file
|
||||||
src="src/android/BackgroundExt.java"
|
src="src/android/BackgroundModeExt.java"
|
||||||
target-dir="src/de/appplant/cordova/plugin/background" />
|
target-dir="src/de/appplant/cordova/plugin/background" />
|
||||||
|
|
||||||
<source-file
|
<source-file
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
Copyright 2013-2017 appPlant GmbH
|
Copyright 2013 Sebastián Katzer
|
||||||
|
|
||||||
Licensed to the Apache Software Foundation (ASF) under one
|
Licensed to the Apache Software Foundation (ASF) under one
|
||||||
or more contributor license agreements. See the NOTICE file
|
or more contributor license agreements. See the NOTICE file
|
||||||
@ -36,7 +36,7 @@ import org.json.JSONObject;
|
|||||||
import de.appplant.cordova.plugin.background.ForegroundService.ForegroundBinder;
|
import de.appplant.cordova.plugin.background.ForegroundService.ForegroundBinder;
|
||||||
|
|
||||||
import static android.content.Context.BIND_AUTO_CREATE;
|
import static android.content.Context.BIND_AUTO_CREATE;
|
||||||
import static de.appplant.cordova.plugin.background.BackgroundExt.clearKeyguardFlags;
|
import static de.appplant.cordova.plugin.background.BackgroundModeExt.clearKeyguardFlags;
|
||||||
|
|
||||||
public class BackgroundMode extends CordovaPlugin {
|
public class BackgroundMode extends CordovaPlugin {
|
||||||
|
|
||||||
@ -123,15 +123,6 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
case "disable":
|
case "disable":
|
||||||
disableMode();
|
disableMode();
|
||||||
break;
|
break;
|
||||||
case "optimizations":
|
|
||||||
case "background":
|
|
||||||
case "foreground":
|
|
||||||
case "tasklist":
|
|
||||||
case "dimmed":
|
|
||||||
case "wakeup":
|
|
||||||
case "unlock":
|
|
||||||
new BackgroundExt(this).executeAsync(action, callback);
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
validAction = false;
|
validAction = false;
|
||||||
}
|
}
|
||||||
@ -322,5 +313,4 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
|
|
||||||
cordova.getActivity().runOnUiThread(() -> webView.loadUrl("javascript:" + js));
|
cordova.getActivity().runOnUiThread(() -> webView.loadUrl("javascript:" + js));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
Copyright 2013-2017 appPlant GmbH
|
Copyright 2013 Sebastián Katzer
|
||||||
|
|
||||||
Licensed to the Apache Software Foundation (ASF) under one
|
Licensed to the Apache Software Foundation (ASF) under one
|
||||||
or more contributor license agreements. See the NOTICE file
|
or more contributor license agreements. See the NOTICE file
|
||||||
@ -21,28 +21,40 @@
|
|||||||
|
|
||||||
package de.appplant.cordova.plugin.background;
|
package de.appplant.cordova.plugin.background;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.ActivityManager;
|
import android.app.ActivityManager;
|
||||||
import android.app.ActivityManager.AppTask;
|
import android.app.ActivityManager.AppTask;
|
||||||
|
import android.app.AlertDialog;
|
||||||
|
import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.PowerManager;
|
import android.os.PowerManager;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
import org.apache.cordova.CallbackContext;
|
import org.apache.cordova.CallbackContext;
|
||||||
import org.apache.cordova.CordovaInterface;
|
|
||||||
import org.apache.cordova.CordovaPlugin;
|
import org.apache.cordova.CordovaPlugin;
|
||||||
import org.apache.cordova.CordovaWebView;
|
|
||||||
import org.apache.cordova.PluginResult;
|
import org.apache.cordova.PluginResult;
|
||||||
import org.apache.cordova.PluginResult.Status;
|
import org.apache.cordova.PluginResult.Status;
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static android.R.string.cancel;
|
||||||
|
import static android.R.string.ok;
|
||||||
|
import static android.R.style.Theme_DeviceDefault_Light_Dialog;
|
||||||
import static android.content.Context.ACTIVITY_SERVICE;
|
import static android.content.Context.ACTIVITY_SERVICE;
|
||||||
import static android.content.Context.POWER_SERVICE;
|
import static android.content.Context.POWER_SERVICE;
|
||||||
|
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.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS;
|
||||||
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;
|
||||||
@ -52,54 +64,38 @@ import static android.view.WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
|
|||||||
* Implements extended functions around the main purpose
|
* Implements extended functions around the main purpose
|
||||||
* of infinite execution in the background.
|
* of infinite execution in the background.
|
||||||
*/
|
*/
|
||||||
class BackgroundExt {
|
public class BackgroundModeExt extends CordovaPlugin {
|
||||||
|
|
||||||
// Reference to the cordova interface passed by the plugin
|
|
||||||
private final CordovaInterface cordova;
|
|
||||||
|
|
||||||
// Reference to the cordova web view passed by the plugin
|
|
||||||
private final CordovaWebView webView;
|
|
||||||
|
|
||||||
// To keep the device awake
|
// To keep the device awake
|
||||||
private PowerManager.WakeLock wakeLock;
|
private PowerManager.WakeLock wakeLock;
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialize the extension to perform non-background related tasks.
|
|
||||||
*
|
|
||||||
* @param plugin The cordova plugin.
|
|
||||||
*/
|
|
||||||
BackgroundExt(CordovaPlugin plugin)
|
|
||||||
{
|
|
||||||
this.cordova = plugin.cordova;
|
|
||||||
this.webView = plugin.webView;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Executes the request within a thread.
|
|
||||||
*
|
|
||||||
* @param action The action to execute.
|
|
||||||
* @param callback The callback context used when
|
|
||||||
* calling back into JavaScript.
|
|
||||||
*/
|
|
||||||
void executeAsync (String action, CallbackContext callback)
|
|
||||||
{
|
|
||||||
cordova.getThreadPool().execute(() -> execute(action, callback));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes the request.
|
* Executes the request.
|
||||||
*
|
*
|
||||||
* @param action The action to execute.
|
* @param action The action to execute.
|
||||||
|
* @param args The exec() arguments.
|
||||||
* @param callback The callback context used when
|
* @param callback The callback context used when
|
||||||
* calling back into JavaScript.
|
* calling back into JavaScript.
|
||||||
|
*
|
||||||
|
* @return Returning false results in a "MethodNotFound" error.
|
||||||
*/
|
*/
|
||||||
private void execute (String action, CallbackContext callback)
|
@Override
|
||||||
|
public boolean execute (String action, JSONArray args,
|
||||||
|
CallbackContext callback)
|
||||||
{
|
{
|
||||||
|
boolean validAction = true;
|
||||||
|
|
||||||
switch (action)
|
switch (action)
|
||||||
{
|
{
|
||||||
case "optimizations":
|
case "battery":
|
||||||
|
disableBatteryOptimizations();
|
||||||
|
break;
|
||||||
|
case "webview":
|
||||||
disableWebViewOptimizations();
|
disableWebViewOptimizations();
|
||||||
break;
|
break;
|
||||||
|
case "appstart":
|
||||||
|
openAppStart(args.opt(0));
|
||||||
|
break;
|
||||||
case "background":
|
case "background":
|
||||||
moveToBackground();
|
moveToBackground();
|
||||||
break;
|
break;
|
||||||
@ -119,7 +115,17 @@ class BackgroundExt {
|
|||||||
wakeup();
|
wakeup();
|
||||||
unlock();
|
unlock();
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
validAction = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (validAction) {
|
||||||
|
callback.success();
|
||||||
|
} else {
|
||||||
|
callback.error("Invalid action: " + action);
|
||||||
|
}
|
||||||
|
|
||||||
|
return validAction;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -147,7 +153,7 @@ class BackgroundExt {
|
|||||||
Intent.FLAG_ACTIVITY_SINGLE_TOP |
|
Intent.FLAG_ACTIVITY_SINGLE_TOP |
|
||||||
Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||||
|
|
||||||
clearSreenAndKeyguardFlags();
|
clearScreenAndKeyguardFlags();
|
||||||
app.startActivity(intent);
|
app.startActivity(intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,6 +185,82 @@ class BackgroundExt {
|
|||||||
thread.start();
|
thread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disables battery optimizations for the app.
|
||||||
|
* Requires permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS to function.
|
||||||
|
*/
|
||||||
|
@SuppressLint("BatteryLife")
|
||||||
|
private void disableBatteryOptimizations()
|
||||||
|
{
|
||||||
|
Activity activity = cordova.getActivity();
|
||||||
|
Intent intent = new Intent();
|
||||||
|
String pkgName = activity.getPackageName();
|
||||||
|
PowerManager pm = (PowerManager)getService(POWER_SERVICE);
|
||||||
|
|
||||||
|
if (SDK_INT < M)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (pm.isIgnoringBatteryOptimizations(pkgName))
|
||||||
|
return;
|
||||||
|
|
||||||
|
intent.setAction(ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
|
||||||
|
intent.setData(Uri.parse("package:" + pkgName));
|
||||||
|
|
||||||
|
cordova.getActivity().startActivity(intent);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Opens the system settings dialog where the user can tweak or turn off any
|
||||||
|
* custom app start settings added by the manufacturer if available.
|
||||||
|
*
|
||||||
|
* @param arg Text and title for the dialog or false to skip the dialog.
|
||||||
|
*/
|
||||||
|
private void openAppStart (Object arg)
|
||||||
|
{
|
||||||
|
Activity activity = cordova.getActivity();
|
||||||
|
PackageManager pm = activity.getPackageManager();
|
||||||
|
|
||||||
|
for (Intent intent : getAppStartIntents())
|
||||||
|
{
|
||||||
|
if (pm.resolveActivity(intent, MATCH_DEFAULT_ONLY) != null)
|
||||||
|
{
|
||||||
|
JSONObject spec = (arg instanceof JSONObject) ? (JSONObject) arg : null;
|
||||||
|
|
||||||
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
|
|
||||||
|
if (arg instanceof Boolean && !((Boolean) arg))
|
||||||
|
{
|
||||||
|
activity.startActivity(intent);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
AlertDialog.Builder dialog = new AlertDialog.Builder(activity, Theme_DeviceDefault_Light_Dialog);
|
||||||
|
|
||||||
|
dialog.setPositiveButton(ok, (o, d) -> activity.startActivity(intent));
|
||||||
|
dialog.setNegativeButton(cancel, (o, d) -> {});
|
||||||
|
dialog.setCancelable(true);
|
||||||
|
|
||||||
|
if (spec != null && spec.has("title"))
|
||||||
|
{
|
||||||
|
dialog.setTitle(spec.optString("title"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (spec != null && spec.has("text"))
|
||||||
|
{
|
||||||
|
dialog.setMessage(spec.optString("text"));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
dialog.setMessage("missing text");
|
||||||
|
}
|
||||||
|
|
||||||
|
activity.runOnUiThread(dialog::show);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Excludes the app from the recent tasks list.
|
* Excludes the app from the recent tasks list.
|
||||||
*/
|
*/
|
||||||
@ -292,7 +374,7 @@ class BackgroundExt {
|
|||||||
/**
|
/**
|
||||||
* Clears required flags to the window to unlock/wakeup the device.
|
* Clears required flags to the window to unlock/wakeup the device.
|
||||||
*/
|
*/
|
||||||
private void clearSreenAndKeyguardFlags()
|
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));
|
||||||
}
|
}
|
||||||
@ -332,4 +414,32 @@ class BackgroundExt {
|
|||||||
{
|
{
|
||||||
return getApp().getSystemService(name);
|
return getApp().getSystemService(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns list of all possible intents to present the app start settings.
|
||||||
|
*/
|
||||||
|
private List<Intent> getAppStartIntents()
|
||||||
|
{
|
||||||
|
return Arrays.asList(
|
||||||
|
new Intent().setComponent(new ComponentName("com.miui.securitycenter","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.huawei.systemmanager", "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity")),
|
||||||
|
new Intent().setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity")),
|
||||||
|
new Intent().setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity")),
|
||||||
|
new Intent().setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.startupapp.StartupAppListActivity")),
|
||||||
|
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().setComponent(new ComponentName("com.samsung.android.sm_cn", "com.samsung.android.sm.ui.ram.AutoRunActivity")),
|
||||||
|
new Intent().setComponent(ComponentName.unflattenFromString("com.iqoo.secure/.MainActivity")),
|
||||||
|
new Intent().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 ComponentName("cn.nubia.security2", "cn.nubia.security.appmanage.selfstart.ui.SelfStartActivity")),
|
||||||
|
new Intent().setComponent(new ComponentName("com.zui.safecenter", "com.lenovo.safecenter.MainTab.LeSafeMainActivity"))
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
Copyright 2013-2017 appPlant GmbH
|
Copyright 2013 Sebastián Katzer
|
||||||
|
|
||||||
Licensed to the Apache Software Foundation (ASF) under one
|
Licensed to the Apache Software Foundation (ASF) under one
|
||||||
or more contributor license agreements. See the NOTICE file
|
or more contributor license agreements. See the NOTICE file
|
||||||
@ -339,5 +339,4 @@ public class ForegroundService extends Service {
|
|||||||
{
|
{
|
||||||
return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
Copyright 2013-2017 appPlant GmbH
|
Copyright 2013 Sebastián Katzer
|
||||||
|
|
||||||
Licensed to the Apache Software Foundation (ASF) under one
|
Licensed to the Apache Software Foundation (ASF) under one
|
||||||
or more contributor license agreements. See the NOTICE file
|
or more contributor license agreements. See the NOTICE file
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
Copyright 2013-2017 appPlant GmbH
|
Copyright 2013 Sebastián Katzer
|
||||||
|
|
||||||
Licensed to the Apache Software Foundation (ASF) under one
|
Licensed to the Apache Software Foundation (ASF) under one
|
||||||
or more contributor license agreements. See the NOTICE file
|
or more contributor license agreements. See the NOTICE file
|
||||||
|
@ -22,11 +22,6 @@
|
|||||||
var exec = require('cordova/exec'),
|
var exec = require('cordova/exec'),
|
||||||
channel = require('cordova/channel');
|
channel = require('cordova/channel');
|
||||||
|
|
||||||
|
|
||||||
/*************
|
|
||||||
* INTERFACE *
|
|
||||||
*************/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Activates the background mode. When activated the application
|
* Activates the background mode. When activated the application
|
||||||
* will be prevented from going to sleep while in background
|
* will be prevented from going to sleep while in background
|
||||||
@ -34,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;
|
||||||
|
|
||||||
@ -52,7 +48,8 @@ exports.enable = function() {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.disable = function() {
|
exports.disable = function()
|
||||||
|
{
|
||||||
if (!this.isEnabled())
|
if (!this.isEnabled())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -71,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 {
|
||||||
@ -84,7 +82,8 @@ exports.setEnabled = function (enable) {
|
|||||||
*
|
*
|
||||||
* @return [ Object ]
|
* @return [ Object ]
|
||||||
*/
|
*/
|
||||||
exports.getDefaults = function() {
|
exports.getDefaults = function()
|
||||||
|
{
|
||||||
return this._defaults;
|
return this._defaults;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -93,7 +92,8 @@ exports.getDefaults = function() {
|
|||||||
*
|
*
|
||||||
* @return [ Object ]
|
* @return [ Object ]
|
||||||
*/
|
*/
|
||||||
exports.getSettings = function() {
|
exports.getSettings = function()
|
||||||
|
{
|
||||||
return this._settings || {};
|
return this._settings || {};
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -104,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]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -126,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;
|
||||||
}
|
}
|
||||||
@ -150,9 +156,41 @@ exports.configure = function (options) {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.disableWebViewOptimizations = function() {
|
exports.disableWebViewOptimizations = function()
|
||||||
if (this._isAndroid) {
|
{
|
||||||
cordova.exec(null, null, 'BackgroundMode', 'optimizations', []);
|
if (this._isAndroid)
|
||||||
|
{
|
||||||
|
cordova.exec(null, null, 'BackgroundModeExt', 'webview', []);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disables battery optimazation mode for the app.
|
||||||
|
*
|
||||||
|
* @return [ Void ]
|
||||||
|
*/
|
||||||
|
exports.disableBatteryOptimizations = function()
|
||||||
|
{
|
||||||
|
if (this._isAndroid)
|
||||||
|
{
|
||||||
|
cordova.exec(null, null, 'BackgroundModeExt', 'battery', []);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Opens the system settings dialog where the user can tweak or turn off any
|
||||||
|
* custom app start settings added by the manufacturer if available.
|
||||||
|
*
|
||||||
|
* @param [ Object|Bool ] options Set to false if you dont want to display an
|
||||||
|
* alert dialog first.
|
||||||
|
*
|
||||||
|
* @return [ Void ]
|
||||||
|
*/
|
||||||
|
exports.openAppStartSettings = function (options)
|
||||||
|
{
|
||||||
|
if (this._isAndroid)
|
||||||
|
{
|
||||||
|
cordova.exec(null, null, 'BackgroundModeExt', 'appstart', [options]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -161,9 +199,11 @@ exports.disableWebViewOptimizations = function() {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.moveToBackground = function() {
|
exports.moveToBackground = function()
|
||||||
if (this._isAndroid) {
|
{
|
||||||
cordova.exec(null, null, 'BackgroundMode', 'background', []);
|
if (this._isAndroid)
|
||||||
|
{
|
||||||
|
cordova.exec(null, null, 'BackgroundModeExt', 'background', []);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -172,9 +212,11 @@ exports.moveToBackground = function() {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.moveToForeground = function() {
|
exports.moveToForeground = function()
|
||||||
if (this.isActive() && this._isAndroid) {
|
{
|
||||||
cordova.exec(null, null, 'BackgroundMode', 'foreground', []);
|
if (this.isActive() && this._isAndroid)
|
||||||
|
{
|
||||||
|
cordova.exec(null, null, 'BackgroundModeExt', 'foreground', []);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -183,9 +225,11 @@ exports.moveToForeground = function() {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.excludeFromTaskList = function() {
|
exports.excludeFromTaskList = function()
|
||||||
if (this._isAndroid) {
|
{
|
||||||
cordova.exec(null, null, 'BackgroundMode', 'tasklist', []);
|
if (this._isAndroid)
|
||||||
|
{
|
||||||
|
cordova.exec(null, null, 'BackgroundModeExt', 'tasklist', []);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -195,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);
|
||||||
@ -208,10 +253,14 @@ exports.overrideBackButton = function() {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.isScreenOff = function (fn) {
|
exports.isScreenOff = function (fn)
|
||||||
if (this._isAndroid) {
|
{
|
||||||
cordova.exec(fn, null, 'BackgroundMode', 'dimmed', []);
|
if (this._isAndroid)
|
||||||
} else {
|
{
|
||||||
|
cordova.exec(fn, null, 'BackgroundModeExt', 'dimmed', []);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
fn(undefined);
|
fn(undefined);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -221,9 +270,11 @@ exports.isScreenOff = function (fn) {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.wakeUp = function() {
|
exports.wakeUp = function()
|
||||||
if (this._isAndroid) {
|
{
|
||||||
cordova.exec(null, null, 'BackgroundMode', 'wakeup', []);
|
if (this._isAndroid)
|
||||||
|
{
|
||||||
|
cordova.exec(null, null, 'BackgroundModeExt', 'wakeup', []);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -232,9 +283,11 @@ exports.wakeUp = function() {
|
|||||||
*
|
*
|
||||||
* @return [ Void ]
|
* @return [ Void ]
|
||||||
*/
|
*/
|
||||||
exports.unlock = function() {
|
exports.unlock = function()
|
||||||
if (this._isAndroid) {
|
{
|
||||||
cordova.exec(null, null, 'BackgroundMode', 'unlock', []);
|
if (this._isAndroid)
|
||||||
|
{
|
||||||
|
cordova.exec(null, null, 'BackgroundModeExt', 'unlock', []);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -243,7 +296,8 @@ exports.unlock = function() {
|
|||||||
*
|
*
|
||||||
* @return [ Boolean ]
|
* @return [ Boolean ]
|
||||||
*/
|
*/
|
||||||
exports.isEnabled = function() {
|
exports.isEnabled = function()
|
||||||
|
{
|
||||||
return this._isEnabled !== false;
|
return this._isEnabled !== false;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -252,15 +306,11 @@ exports.isEnabled = function() {
|
|||||||
*
|
*
|
||||||
* @return [ Boolean ]
|
* @return [ Boolean ]
|
||||||
*/
|
*/
|
||||||
exports.isActive = function() {
|
exports.isActive = function()
|
||||||
|
{
|
||||||
return this._isActive !== false;
|
return this._isActive !== false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**********
|
|
||||||
* EVENTS *
|
|
||||||
**********/
|
|
||||||
|
|
||||||
exports._listener = {};
|
exports._listener = {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -271,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];
|
||||||
|
|
||||||
@ -295,12 +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] = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -317,51 +370,25 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* @deprecated
|
|
||||||
*
|
|
||||||
* Called when the background mode has been activated.
|
|
||||||
*/
|
|
||||||
exports.onactivate = function() {};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @deprecated
|
|
||||||
*
|
|
||||||
* Called when the background mode has been deaktivated.
|
|
||||||
*/
|
|
||||||
exports.ondeactivate = function() {};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @deprecated
|
|
||||||
*
|
|
||||||
* Called when the background mode could not been activated.
|
|
||||||
*
|
|
||||||
* @param {Integer} errorCode
|
|
||||||
* Error code which describes the error
|
|
||||||
*/
|
|
||||||
exports.onfailure = function() {};
|
|
||||||
|
|
||||||
|
|
||||||
/***********
|
|
||||||
* PRIVATE *
|
|
||||||
***********/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
*
|
*
|
||||||
@ -381,6 +408,7 @@ exports._isActive = false;
|
|||||||
*
|
*
|
||||||
* Default values of all available options.
|
* Default values of all available options.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
exports._defaults = {
|
exports._defaults = {
|
||||||
title: 'App is running in background',
|
title: 'App is running in background',
|
||||||
text: 'Doing heavy tasks.',
|
text: 'Doing heavy tasks.',
|
||||||
@ -409,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;
|
||||||
}
|
}
|
||||||
@ -430,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;
|
||||||
|
|
||||||
@ -448,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;
|
||||||
}
|
}
|
||||||
@ -461,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');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user