Add AutoStarter library to help prevent auto kill

https://github.com/judemanutd/AutoStarter
This commit is contained in:
Nathan Kerr 2021-07-13 14:19:15 -07:00
parent b33044c8e5
commit c602a8a101
3 changed files with 607 additions and 631 deletions

View File

@ -94,6 +94,7 @@
target-dir="src/de/appplant/cordova/plugin/background" />
<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-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" />

View File

@ -37,6 +37,8 @@ import android.os.PowerManager;
import android.provider.Settings;
import android.view.View;
import com.judemanutd.autostarter.AutoStartPermissionHelper;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
@ -63,8 +65,8 @@ import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
import static android.view.WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
/**
* Implements extended functions around the main purpose
* of infinite execution in the background.
* Implements extended functions around the main purpose of infinite execution
* in the background.
*/
public class BackgroundModeExt extends CordovaPlugin {
@ -76,19 +78,15 @@ public class BackgroundModeExt extends CordovaPlugin {
*
* @param action The action to execute.
* @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.
*/
@Override
public boolean execute (String action, JSONArray args,
CallbackContext callback)
{
public boolean execute(String action, JSONArray args, CallbackContext callback) {
boolean validAction = true;
switch (action)
{
switch (action) {
case "battery":
disableBatteryOptimizations();
break;
@ -126,6 +124,12 @@ public class BackgroundModeExt extends CordovaPlugin {
wakeup();
unlock();
break;
case "mightAutoKill":
mightAutoKill(callback);
break;
case "showAutoKill":
showAutoKill();
break;
default:
validAction = false;
}
@ -142,8 +146,7 @@ public class BackgroundModeExt extends CordovaPlugin {
/**
* Moves the app to the background.
*/
private void moveToBackground()
{
private void moveToBackground() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
@ -154,15 +157,12 @@ public class BackgroundModeExt extends CordovaPlugin {
/**
* Moves the app to the foreground.
*/
private void moveToForeground()
{
private void moveToForeground() {
Activity app = getApp();
Intent intent = getLaunchIntent();
intent.addFlags(
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |
Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
clearScreenAndKeyguardFlags();
app.startActivity(intent);
@ -172,7 +172,7 @@ public class BackgroundModeExt extends CordovaPlugin {
* Enable GPS position tracking while in background.
*/
private void disableWebViewOptimizations() {
Thread thread = new Thread(){
Thread thread = new Thread() {
public void run() {
try {
Thread.sleep(1000);
@ -180,10 +180,8 @@ public class BackgroundModeExt extends CordovaPlugin {
View view = webView.getView();
try {
Class.forName("org.crosswalk.engine.XWalkCordovaView")
.getMethod("onShow")
.invoke(view);
} catch (Exception e){
Class.forName("org.crosswalk.engine.XWalkCordovaView").getMethod("onShow").invoke(view);
} catch (Exception e) {
view.dispatchWindowVisibilityChanged(View.VISIBLE);
}
});
@ -197,16 +195,15 @@ public class BackgroundModeExt extends CordovaPlugin {
}
/**
* Disables battery optimizations for the app.
* Requires permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS to function.
* Disables battery optimizations for the app. Requires
* permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS to function.
*/
@SuppressLint("BatteryLife")
private void disableBatteryOptimizations()
{
private void disableBatteryOptimizations() {
Activity activity = cordova.getActivity();
Intent intent = new Intent();
String pkgName = activity.getPackageName();
PowerManager pm = (PowerManager)getService(POWER_SERVICE);
PowerManager pm = (PowerManager) getService(POWER_SERVICE);
if (SDK_INT < M)
return;
@ -223,8 +220,7 @@ public class BackgroundModeExt extends CordovaPlugin {
/**
* Opens the Battery Optimization settings screen
*/
private void openBatterySettings()
{
private void openBatterySettings() {
if (SDK_INT < M)
return;
@ -239,14 +235,13 @@ public class BackgroundModeExt extends CordovaPlugin {
*
* @param callback The callback to invoke.
*/
private void isIgnoringBatteryOptimizations(CallbackContext callback)
{
private void isIgnoringBatteryOptimizations(CallbackContext callback) {
if (SDK_INT < M)
return;
Activity activity = cordova.getActivity();
String pkgName = activity.getPackageName();
PowerManager pm = (PowerManager)getService(POWER_SERVICE);
PowerManager pm = (PowerManager) getService(POWER_SERVICE);
boolean isIgnoring = pm.isIgnoringBatteryOptimizations(pkgName);
PluginResult res = new PluginResult(Status.OK, isIgnoring);
@ -273,21 +268,17 @@ public class BackgroundModeExt extends CordovaPlugin {
*
* @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();
PackageManager pm = activity.getPackageManager();
for (Intent intent : getAppStartIntents())
{
if (pm.resolveActivity(intent, MATCH_DEFAULT_ONLY) != null)
{
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))
{
if (arg instanceof Boolean && !((Boolean) arg)) {
activity.startActivity(intent);
break;
}
@ -295,20 +286,17 @@ public class BackgroundModeExt extends CordovaPlugin {
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.setNegativeButton(cancel, (o, d) -> {
});
dialog.setCancelable(true);
if (spec != null && spec.has("title"))
{
if (spec != null && spec.has("title")) {
dialog.setTitle(spec.optString("title"));
}
if (spec != null && spec.has("text"))
{
if (spec != null && spec.has("text")) {
dialog.setMessage(spec.optString("text"));
}
else
{
} else {
dialog.setMessage("missing text");
}
@ -323,8 +311,7 @@ public class BackgroundModeExt extends CordovaPlugin {
* Excludes the app from the recent tasks list.
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void excludeFromTaskList()
{
private void excludeFromTaskList() {
ActivityManager am = (ActivityManager) getService(ACTIVITY_SERVICE);
if (am == null || SDK_INT < 21)
@ -344,8 +331,7 @@ public class BackgroundModeExt extends CordovaPlugin {
* @param callback The callback to invoke.
*/
@SuppressWarnings("deprecation")
private void isDimmed (CallbackContext callback)
{
private void isDimmed(CallbackContext callback) {
boolean status = isDimmed();
PluginResult res = new PluginResult(Status.OK, status);
@ -356,12 +342,10 @@ public class BackgroundModeExt extends CordovaPlugin {
* Returns if the screen is active.
*/
@SuppressWarnings("deprecation")
private boolean isDimmed()
{
private boolean isDimmed() {
PowerManager pm = (PowerManager) getService(POWER_SERVICE);
if (SDK_INT < 20)
{
if (SDK_INT < 20) {
return !pm.isScreenOn();
}
@ -371,8 +355,7 @@ public class BackgroundModeExt extends CordovaPlugin {
/**
* Wakes up the device if the screen isn't still on.
*/
private void wakeup()
{
private void wakeup() {
try {
acquireWakeLock();
} catch (Exception e) {
@ -383,8 +366,7 @@ public class BackgroundModeExt extends CordovaPlugin {
/**
* Unlocks the device even with password protection.
*/
private void unlock()
{
private void unlock() {
addSreenAndKeyguardFlags();
getApp().startActivity(getLaunchIntent());
}
@ -393,8 +375,7 @@ public class BackgroundModeExt extends CordovaPlugin {
* Acquires a wake lock to wake up the device.
*/
@SuppressWarnings("deprecation")
private void acquireWakeLock()
{
private void acquireWakeLock() {
PowerManager pm = (PowerManager) getService(POWER_SERVICE);
releaseWakeLock();
@ -402,8 +383,7 @@ public class BackgroundModeExt extends CordovaPlugin {
if (!isDimmed())
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.setReferenceCounted(false);
@ -413,35 +393,51 @@ public class BackgroundModeExt extends CordovaPlugin {
/**
* Releases the previously acquire wake lock.
*/
private void releaseWakeLock()
{
private void releaseWakeLock() {
if (wakeLock != null && wakeLock.isHeld()) {
wakeLock.release();
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.
*/
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));
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));
}
/**
* Clears required flags to the window to unlock/wakeup the device.
*/
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));
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));
}
/**
* 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));
}
@ -455,8 +451,7 @@ public class BackgroundModeExt extends CordovaPlugin {
/**
* Gets the launch intent for the main activity.
*/
private Intent getLaunchIntent()
{
private Intent getLaunchIntent() {
Context app = getApp().getApplicationContext();
String pkgName = app.getPackageName();
@ -468,36 +463,52 @@ public class BackgroundModeExt extends CordovaPlugin {
*
* @param name The name of the service.
*/
private Object getService(String name)
{
private Object getService(String name) {
return getApp().getSystemService(name);
}
/**
* Returns list of all possible intents to present the app start settings.
*/
private List<Intent> getAppStartIntents()
{
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().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(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"))
);
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")));
}
}

View File

@ -29,12 +29,11 @@ var exec = require('cordova/exec'),
*
* @return [ Void ]
*/
exports.enable = function()
{
exports.enable = function () {
if (this.isEnabled())
return;
var fn = function() {
var fn = function () {
exports._isEnabled = true;
exports.fireEvent('enable');
};
@ -48,12 +47,11 @@ exports.enable = function()
*
* @return [ Void ]
*/
exports.disable = function()
{
exports.disable = function () {
if (!this.isEnabled())
return;
var fn = function() {
var fn = function () {
exports._isEnabled = false;
exports.fireEvent('disable');
};
@ -68,8 +66,7 @@ exports.disable = function()
*
* @return [ Void ]
*/
exports.setEnabled = function (enable)
{
exports.setEnabled = function (enable) {
if (enable) {
this.enable();
} else {
@ -82,8 +79,7 @@ exports.setEnabled = function (enable)
*
* @return [ Object ]
*/
exports.getDefaults = function()
{
exports.getDefaults = function () {
return this._defaults;
};
@ -92,8 +88,7 @@ exports.getDefaults = function()
*
* @return [ Object ]
*/
exports.getSettings = function()
{
exports.getSettings = function () {
return this._settings || {};
};
@ -104,20 +99,16 @@ exports.getSettings = function()
*
* @return [ Void ]
*/
exports.setDefaults = function (overrides)
{
exports.setDefaults = function (overrides) {
var defaults = this.getDefaults();
for (var key in defaults)
{
if (overrides.hasOwnProperty(key))
{
for (var key in defaults) {
if (overrides.hasOwnProperty(key)) {
defaults[key] = overrides[key];
}
}
if (this._isAndroid)
{
if (this._isAndroid) {
cordova.exec(null, null, 'BackgroundMode', 'configure', [defaults, false]);
}
};
@ -130,16 +121,14 @@ exports.setDefaults = function (overrides)
*
* @return [ Void ]
*/
exports.configure = function (options)
{
exports.configure = function (options) {
var settings = this.getSettings(),
defaults = this.getDefaults();
if (!this._isAndroid)
return;
if (!this._isActive)
{
if (!this._isActive) {
console.log('BackgroundMode is not active, skipped...');
return;
}
@ -156,10 +145,8 @@ exports.configure = function (options)
*
* @return [ Void ]
*/
exports.disableWebViewOptimizations = function()
{
if (this._isAndroid)
{
exports.disableWebViewOptimizations = function () {
if (this._isAndroid) {
cordova.exec(null, null, 'BackgroundModeExt', 'webview', []);
}
};
@ -169,10 +156,8 @@ exports.disableWebViewOptimizations = function()
*
* @return [ Void ]
*/
exports.disableBatteryOptimizations = function()
{
if (this._isAndroid)
{
exports.disableBatteryOptimizations = function () {
if (this._isAndroid) {
cordova.exec(null, null, 'BackgroundModeExt', 'battery', []);
}
};
@ -183,10 +168,8 @@ exports.disableBatteryOptimizations = function()
*
* @return [ Void ]
*/
exports.openBatteryOptimizationsSettings = function()
{
if (this._isAndroid)
{
exports.openBatteryOptimizationsSettings = function () {
if (this._isAndroid) {
cordova.exec(null, null, 'BackgroundModeExt', 'batterysettings', []);
}
};
@ -197,18 +180,34 @@ exports.openBatteryOptimizationsSettings = function()
*
* @return [ Void ]
*/
exports.isIgnoringBatteryOptimizations = function(callback)
{
if (this._isAndroid)
{
exports.isIgnoringBatteryOptimizations = function (callback) {
if (this._isAndroid) {
cordova.exec(callback, null, 'BackgroundModeExt', 'optimizationstatus', []);
}
else
{
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
* custom app start settings added by the manufacturer if available.
@ -218,10 +217,8 @@ exports.isIgnoringBatteryOptimizations = function(callback)
*
* @return [ Void ]
*/
exports.openAppStartSettings = function (options)
{
if (this._isAndroid)
{
exports.openAppStartSettings = function (options) {
if (this._isAndroid) {
cordova.exec(null, null, 'BackgroundModeExt', 'appstart', [options]);
}
};
@ -231,10 +228,8 @@ exports.openAppStartSettings = function (options)
*
* @return [ Void ]
*/
exports.moveToBackground = function()
{
if (this._isAndroid)
{
exports.moveToBackground = function () {
if (this._isAndroid) {
cordova.exec(null, null, 'BackgroundModeExt', 'background', []);
}
};
@ -244,10 +239,8 @@ exports.moveToBackground = function()
*
* @return [ Void ]
*/
exports.moveToForeground = function()
{
if (this.isActive() && this._isAndroid)
{
exports.moveToForeground = function () {
if (this.isActive() && this._isAndroid) {
cordova.exec(null, null, 'BackgroundModeExt', 'foreground', []);
}
};
@ -257,7 +250,7 @@ exports.moveToForeground = function()
*
* @return [ Void ]
*/
exports.requestForegroundPermission = function() {
exports.requestForegroundPermission = function () {
if (this._isAndroid) {
cordova.exec(null, null, 'BackgroundModeExt', 'requestTopPermissions', []);
}
@ -268,10 +261,8 @@ exports.requestForegroundPermission = function() {
*
* @return [ Void ]
*/
exports.excludeFromTaskList = function()
{
if (this._isAndroid)
{
exports.excludeFromTaskList = function () {
if (this._isAndroid) {
cordova.exec(null, null, 'BackgroundModeExt', 'tasklist', []);
}
};
@ -282,9 +273,8 @@ exports.excludeFromTaskList = function()
*
* @return [ Void ]
*/
exports.overrideBackButton = function()
{
document.addEventListener('backbutton', function() {
exports.overrideBackButton = function () {
document.addEventListener('backbutton', function () {
exports.moveToBackground();
}, false);
};
@ -296,14 +286,11 @@ exports.overrideBackButton = function()
*
* @return [ Void ]
*/
exports.isScreenOff = function (fn)
{
if (this._isAndroid)
{
exports.isScreenOff = function (fn) {
if (this._isAndroid) {
cordova.exec(fn, null, 'BackgroundModeExt', 'dimmed', []);
}
else
{
else {
fn(undefined);
}
};
@ -313,10 +300,8 @@ exports.isScreenOff = function (fn)
*
* @return [ Void ]
*/
exports.wakeUp = function()
{
if (this._isAndroid)
{
exports.wakeUp = function () {
if (this._isAndroid) {
cordova.exec(null, null, 'BackgroundModeExt', 'wakeup', []);
}
};
@ -326,10 +311,8 @@ exports.wakeUp = function()
*
* @return [ Void ]
*/
exports.unlock = function()
{
if (this._isAndroid)
{
exports.unlock = function () {
if (this._isAndroid) {
cordova.exec(null, null, 'BackgroundModeExt', 'unlock', []);
}
};
@ -339,8 +322,7 @@ exports.unlock = function()
*
* @return [ Boolean ]
*/
exports.isEnabled = function()
{
exports.isEnabled = function () {
return this._isEnabled !== false;
};
@ -349,8 +331,7 @@ exports.isEnabled = function()
*
* @return [ Boolean ]
*/
exports.isActive = function()
{
exports.isActive = function () {
return this._isActive !== false;
};
@ -364,16 +345,14 @@ exports._listener = {};
*
* @return [ Void ]
*/
exports.fireEvent = function (event)
{
exports.fireEvent = function (event) {
var args = Array.apply(null, arguments).slice(1),
listener = this._listener[event];
if (!listener)
return;
for (var i = 0; i < listener.length; i++)
{
for (var i = 0; i < listener.length; i++) {
var fn = listener[i][0],
scope = listener[i][1];
@ -390,13 +369,11 @@ exports.fireEvent = function (event)
*
* @return [ Void ]
*/
exports.on = function (event, callback, scope)
{
exports.on = function (event, callback, scope) {
if (typeof callback !== "function")
return;
if (!this._listener[event])
{
if (!this._listener[event]) {
this._listener[event] = [];
}
@ -413,19 +390,16 @@ exports.on = function (event, callback, scope)
*
* @return [ Void ]
*/
exports.un = function (event, callback)
{
exports.un = function (event, callback) {
var listener = this._listener[event];
if (!listener)
return;
for (var i = 0; i < listener.length; i++)
{
for (var i = 0; i < listener.length; i++) {
var fn = listener[i][0];
if (fn == callback)
{
if (fn == callback) {
listener.splice(i, 1);
break;
}
@ -481,12 +455,9 @@ exports._defaults = {
*
* @return [ Object ] Default values merged with custom values.
*/
exports._mergeObjects = function (options, toMergeIn)
{
for (var key in toMergeIn)
{
if (!options.hasOwnProperty(key))
{
exports._mergeObjects = function (options, toMergeIn) {
for (var key in toMergeIn) {
if (!options.hasOwnProperty(key)) {
options[key] = toMergeIn[key];
continue;
}
@ -505,8 +476,7 @@ exports._mergeObjects = function (options, toMergeIn)
*
* @return [ Void ]
*/
exports._setActive = function(value)
{
exports._setActive = function (value) {
if (this._isActive == value)
return;
@ -524,13 +494,11 @@ exports._setActive = function(value)
*
* @return [ Void ]
*/
exports._pluginInitialize = function()
{
exports._pluginInitialize = function () {
this._isAndroid = device.platform.match(/^android|amazon/i) !== null;
this.setDefaults({});
if (device.platform == 'browser')
{
if (device.platform == 'browser') {
this.enable();
this._isEnabled = true;
}
@ -539,23 +507,19 @@ exports._pluginInitialize = function()
};
// Called before 'deviceready' listener will be called
channel.onCordovaReady.subscribe(function()
{
channel.onCordovaInfoReady.subscribe(function() {
channel.onCordovaReady.subscribe(function () {
channel.onCordovaInfoReady.subscribe(function () {
exports._pluginInitialize();
});
});
// Called after 'deviceready' event
channel.deviceready.subscribe(function()
{
if (exports.isEnabled())
{
channel.deviceready.subscribe(function () {
if (exports.isEnabled()) {
exports.fireEvent('enable');
}
if (exports.isActive())
{
if (exports.isActive()) {
exports.fireEvent('activate');
}
});