diff --git a/plugin.xml b/plugin.xml index 0a6cee4..bc34a71 100644 --- a/plugin.xml +++ b/plugin.xml @@ -94,6 +94,7 @@ target-dir="src/de/appplant/cordova/plugin/background" /> + diff --git a/src/android/BackgroundModeExt.java b/src/android/BackgroundModeExt.java index 06a824e..b757ed7 100644 --- a/src/android/BackgroundModeExt.java +++ b/src/android/BackgroundModeExt.java @@ -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,441 +65,450 @@ 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 { - // To keep the device awake - private PowerManager.WakeLock wakeLock; + // To keep the device awake + private PowerManager.WakeLock wakeLock; - /** - * Executes the request. - * - * @param action The action to execute. - * @param args The exec() arguments. - * @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) - { - boolean validAction = true; + /** + * Executes the request. + * + * @param action The action to execute. + * @param args The exec() arguments. + * @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) { + boolean validAction = true; - switch (action) - { - case "battery": - disableBatteryOptimizations(); - break; - case "batterysettings": - openBatterySettings(); - break; - case "optimizationstatus": - isIgnoringBatteryOptimizations(callback); - break; - case "webview": - disableWebViewOptimizations(); - break; - case "appstart": - openAppStart(args.opt(0)); - break; - case "background": - moveToBackground(); - break; - case "foreground": - moveToForeground(); - break; - case "requestTopPermissions": - requestTopPermissions(); - break; - case "tasklist": - excludeFromTaskList(); - break; - case "dimmed": - isDimmed(callback); - break; - case "wakeup": - wakeup(); - break; - case "unlock": - wakeup(); - unlock(); - break; - default: - validAction = false; - } + switch (action) { + case "battery": + disableBatteryOptimizations(); + break; + case "batterysettings": + openBatterySettings(); + break; + case "optimizationstatus": + isIgnoringBatteryOptimizations(callback); + break; + case "webview": + disableWebViewOptimizations(); + break; + case "appstart": + openAppStart(args.opt(0)); + break; + case "background": + moveToBackground(); + break; + case "foreground": + moveToForeground(); + break; + case "requestTopPermissions": + requestTopPermissions(); + break; + case "tasklist": + excludeFromTaskList(); + break; + case "dimmed": + isDimmed(callback); + break; + case "wakeup": + wakeup(); + break; + case "unlock": + wakeup(); + unlock(); + break; + case "mightAutoKill": + mightAutoKill(callback); + break; + case "showAutoKill": + showAutoKill(); + break; + default: + validAction = false; + } - if (validAction) { - callback.success(); - } else { - callback.error("Invalid action: " + action); - } + if (validAction) { + callback.success(); + } else { + callback.error("Invalid action: " + action); + } - return validAction; - } + return validAction; + } - /** - * Moves the app to the background. - */ - private void moveToBackground() - { - Intent intent = new Intent(Intent.ACTION_MAIN); + /** + * Moves the app to the background. + */ + private void moveToBackground() { + Intent intent = new Intent(Intent.ACTION_MAIN); - intent.addCategory(Intent.CATEGORY_HOME); + intent.addCategory(Intent.CATEGORY_HOME); - getApp().startActivity(intent); - } + getApp().startActivity(intent); + } - /** - * Moves the app to the foreground. - */ - private void moveToForeground() - { - Activity app = getApp(); - Intent intent = getLaunchIntent(); + /** + * Moves the app to the foreground. + */ + 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); - } + clearScreenAndKeyguardFlags(); + app.startActivity(intent); + } - /** - * Enable GPS position tracking while in background. - */ - private void disableWebViewOptimizations() { - Thread thread = new Thread(){ - public void run() { - try { - Thread.sleep(1000); - getApp().runOnUiThread(() -> { - View view = webView.getView(); + /** + * Enable GPS position tracking while in background. + */ + private void disableWebViewOptimizations() { + Thread thread = new Thread() { + public void run() { + try { + Thread.sleep(1000); + getApp().runOnUiThread(() -> { + View view = webView.getView(); - try { - Class.forName("org.crosswalk.engine.XWalkCordovaView") - .getMethod("onShow") - .invoke(view); - } catch (Exception e){ - view.dispatchWindowVisibilityChanged(View.VISIBLE); - } - }); - } catch (InterruptedException e) { - // do nothing - } - } - }; + try { + Class.forName("org.crosswalk.engine.XWalkCordovaView").getMethod("onShow").invoke(view); + } catch (Exception e) { + view.dispatchWindowVisibilityChanged(View.VISIBLE); + } + }); + } catch (InterruptedException e) { + // do nothing + } + } + }; - 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); + /** + * 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 (SDK_INT < M) + return; - if (pm.isIgnoringBatteryOptimizations(pkgName)) - return; + if (pm.isIgnoringBatteryOptimizations(pkgName)) + return; - intent.setAction(ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); - intent.setData(Uri.parse("package:" + pkgName)); + intent.setAction(ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); + intent.setData(Uri.parse("package:" + pkgName)); - cordova.getActivity().startActivity(intent); - } + cordova.getActivity().startActivity(intent); + } - /** - * Opens the Battery Optimization settings screen - */ - private void openBatterySettings() - { - if (SDK_INT < M) - return; + /** + * 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); + Activity activity = cordova.getActivity(); + Intent intent = new Intent(ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); - cordova.getActivity().startActivity(intent); - } + 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; + /** + * 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); + 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); - } + callback.sendPluginResult(res); + } - private void requestTopPermissions() { - if (SDK_INT >= M) { + private void requestTopPermissions() { + if (SDK_INT >= M) { - Activity activity = cordova.getActivity(); - if (Settings.canDrawOverlays(activity.getApplicationContext())) { - return; - } + 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); - } - } + 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 - * 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(); + /** + * 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; + 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); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - if (arg instanceof Boolean && !((Boolean) arg)) - { - activity.startActivity(intent); - break; - } + if (arg instanceof Boolean && !((Boolean) arg)) { + activity.startActivity(intent); + break; + } - 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.setNegativeButton(cancel, (o, d) -> {}); - dialog.setCancelable(true); + 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("title")) { + dialog.setTitle(spec.optString("title")); + } - if (spec != null && spec.has("text")) - { - dialog.setMessage(spec.optString("text")); - } - else - { - dialog.setMessage("missing text"); - } + if (spec != null && spec.has("text")) { + dialog.setMessage(spec.optString("text")); + } else { + dialog.setMessage("missing text"); + } - activity.runOnUiThread(dialog::show); + activity.runOnUiThread(dialog::show); - break; - } - } - } + break; + } + } + } - /** - * Excludes the app from the recent tasks list. - */ - @TargetApi(Build.VERSION_CODES.LOLLIPOP) - private void excludeFromTaskList() - { - ActivityManager am = (ActivityManager) getService(ACTIVITY_SERVICE); + /** + * Excludes the app from the recent tasks list. + */ + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + private void excludeFromTaskList() { + ActivityManager am = (ActivityManager) getService(ACTIVITY_SERVICE); - if (am == null || SDK_INT < 21) - return; + if (am == null || SDK_INT < 21) + return; - List tasks = am.getAppTasks(); + List tasks = am.getAppTasks(); - if (tasks == null || tasks.isEmpty()) - return; + if (tasks == null || tasks.isEmpty()) + return; - tasks.get(0).setExcludeFromRecents(true); - } + tasks.get(0).setExcludeFromRecents(true); + } - /** - * Invokes the callback with information if the screen is on. - * - * @param callback The callback to invoke. - */ - @SuppressWarnings("deprecation") - private void isDimmed (CallbackContext callback) - { - boolean status = isDimmed(); - PluginResult res = new PluginResult(Status.OK, status); + /** + * Invokes the callback with information if the screen is on. + * + * @param callback The callback to invoke. + */ + @SuppressWarnings("deprecation") + private void isDimmed(CallbackContext callback) { + boolean status = isDimmed(); + PluginResult res = new PluginResult(Status.OK, status); - callback.sendPluginResult(res); - } + callback.sendPluginResult(res); + } - /** - * Returns if the screen is active. - */ - @SuppressWarnings("deprecation") - private boolean isDimmed() - { - PowerManager pm = (PowerManager) getService(POWER_SERVICE); + /** + * Returns if the screen is active. + */ + @SuppressWarnings("deprecation") + private boolean isDimmed() { + PowerManager pm = (PowerManager) getService(POWER_SERVICE); - if (SDK_INT < 20) - { - return !pm.isScreenOn(); - } + if (SDK_INT < 20) { + return !pm.isScreenOn(); + } - return !pm.isInteractive(); - } + return !pm.isInteractive(); + } - /** - * Wakes up the device if the screen isn't still on. - */ - private void wakeup() - { - try { - acquireWakeLock(); - } catch (Exception e) { - releaseWakeLock(); - } - } + /** + * Wakes up the device if the screen isn't still on. + */ + private void wakeup() { + try { + acquireWakeLock(); + } catch (Exception e) { + releaseWakeLock(); + } + } - /** - * Unlocks the device even with password protection. - */ - private void unlock() - { - addSreenAndKeyguardFlags(); - getApp().startActivity(getLaunchIntent()); - } + /** + * Unlocks the device even with password protection. + */ + private void unlock() { + addSreenAndKeyguardFlags(); + getApp().startActivity(getLaunchIntent()); + } - /** - * Acquires a wake lock to wake up the device. - */ - @SuppressWarnings("deprecation") - private void acquireWakeLock() - { - PowerManager pm = (PowerManager) getService(POWER_SERVICE); + /** + * Acquires a wake lock to wake up the device. + */ + @SuppressWarnings("deprecation") + private void acquireWakeLock() { + PowerManager pm = (PowerManager) getService(POWER_SERVICE); - releaseWakeLock(); + releaseWakeLock(); - if (!isDimmed()) - return; + 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); - wakeLock.acquire(1000); - } + wakeLock = pm.newWakeLock(level, "backgroundmode:wakelock"); + wakeLock.setReferenceCounted(false); + wakeLock.acquire(1000); + } - /** - * Releases the previously acquire wake lock. - */ - private void releaseWakeLock() - { - if (wakeLock != null && wakeLock.isHeld()) { - wakeLock.release(); - wakeLock = null; - } - } + /** + * Releases the previously acquire wake lock. + */ + private void releaseWakeLock() { + if (wakeLock != null && wakeLock.isHeld()) { + wakeLock.release(); + wakeLock = null; + } + } - /** - * 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 showAutoKill() { + Context context = getApp().getApplicationContext(); + AutoStartPermissionHelper instance = AutoStartPermissionHelper.Companion.getInstance(); + boolean isKilled = instance.isAutoStartPermissionAvailable(context, false); + if (isKilled) { + instance.getAutoStartPermission(context, true, true); + } + } - /** - * 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 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); - /** - * Removes required flags to the window to unlock/wakeup the device. - */ - static void clearKeyguardFlags (Activity app) - { - app.runOnUiThread(() -> app.getWindow().clearFlags(FLAG_DISMISS_KEYGUARD)); - } + callback.sendPluginResult(res); + } - /** - * Returns the activity referenced by cordova. - */ - Activity getApp() { - return cordova.getActivity(); - } + /** + * 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)); + } - /** - * Gets the launch intent for the main activity. - */ - private Intent getLaunchIntent() - { - Context app = getApp().getApplicationContext(); - String pkgName = app.getPackageName(); + /** + * 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)); + } - return app.getPackageManager().getLaunchIntentForPackage(pkgName); - } + /** + * Removes required flags to the window to unlock/wakeup the device. + */ + static void clearKeyguardFlags(Activity app) { + app.runOnUiThread(() -> app.getWindow().clearFlags(FLAG_DISMISS_KEYGUARD)); + } - /** - * Get the requested system service by name. - * - * @param name The name of the service. - */ - private Object getService(String name) - { - return getApp().getSystemService(name); - } + /** + * Returns the activity referenced by cordova. + */ + Activity getApp() { + return cordova.getActivity(); + } - /** - * Returns list of all possible intents to present the app start settings. - */ - private List 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")) - ); - } + /** + * Gets the launch intent for the main activity. + */ + private Intent getLaunchIntent() { + Context app = getApp().getApplicationContext(); + String pkgName = app.getPackageName(); + + return app.getPackageManager().getLaunchIntentForPackage(pkgName); + } + + /** + * Get the requested system service by name. + * + * @param name The name of the service. + */ + private Object getService(String name) { + return getApp().getSystemService(name); + } + + /** + * Returns list of all possible intents to present the app start settings. + */ + private List 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"))); + } } diff --git a/www/background-mode.js b/www/background-mode.js index 4464425..76e8ca6 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -1,26 +1,26 @@ /* - Copyright 2013-2017 appPlant GmbH + Copyright 2013-2017 appPlant GmbH - Licensed to the Apache Software Foundation (ASF) under one - or more contributor license agreements. See the NOTICE file - distributed with this work for additional information - regarding copyright ownership. The ASF licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. You may obtain a copy of the License at + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. */ -var exec = require('cordova/exec'), - channel = require('cordova/channel'); +var exec = require('cordova/exec'), + channel = require('cordova/channel'); /** * Activates the background mode. When activated the application @@ -29,17 +29,16 @@ var exec = require('cordova/exec'), * * @return [ Void ] */ -exports.enable = function() -{ - if (this.isEnabled()) - return; +exports.enable = function () { + if (this.isEnabled()) + return; - var fn = function() { - exports._isEnabled = true; - exports.fireEvent('enable'); - }; + var fn = function () { + exports._isEnabled = true; + exports.fireEvent('enable'); + }; - cordova.exec(fn, null, 'BackgroundMode', 'enable', []); + cordova.exec(fn, null, 'BackgroundMode', 'enable', []); }; /** @@ -48,17 +47,16 @@ exports.enable = function() * * @return [ Void ] */ -exports.disable = function() -{ - if (!this.isEnabled()) - return; +exports.disable = function () { + if (!this.isEnabled()) + return; - var fn = function() { - exports._isEnabled = false; - exports.fireEvent('disable'); - }; + var fn = function () { + exports._isEnabled = false; + exports.fireEvent('disable'); + }; - cordova.exec(fn, null, 'BackgroundMode', 'disable', []); + cordova.exec(fn, null, 'BackgroundMode', 'disable', []); }; /** @@ -68,13 +66,12 @@ exports.disable = function() * * @return [ Void ] */ -exports.setEnabled = function (enable) -{ - if (enable) { - this.enable(); - } else { - this.disable(); - } +exports.setEnabled = function (enable) { + if (enable) { + this.enable(); + } else { + this.disable(); + } }; /** @@ -82,9 +79,8 @@ exports.setEnabled = function (enable) * * @return [ Object ] */ -exports.getDefaults = function() -{ - return this._defaults; +exports.getDefaults = function () { + return this._defaults; }; /** @@ -92,9 +88,8 @@ exports.getDefaults = function() * * @return [ Object ] */ -exports.getSettings = function() -{ - return this._settings || {}; +exports.getSettings = function () { + return this._settings || {}; }; /** @@ -104,22 +99,18 @@ exports.getSettings = function() * * @return [ Void ] */ -exports.setDefaults = function (overrides) -{ - var defaults = this.getDefaults(); +exports.setDefaults = function (overrides) { + var defaults = this.getDefaults(); - for (var key in defaults) - { - if (overrides.hasOwnProperty(key)) - { - defaults[key] = overrides[key]; - } - } + for (var key in defaults) { + if (overrides.hasOwnProperty(key)) { + defaults[key] = overrides[key]; + } + } - if (this._isAndroid) - { - cordova.exec(null, null, 'BackgroundMode', 'configure', [defaults, false]); - } + if (this._isAndroid) { + cordova.exec(null, null, 'BackgroundMode', 'configure', [defaults, false]); + } }; /** @@ -130,25 +121,23 @@ exports.setDefaults = function (overrides) * * @return [ Void ] */ -exports.configure = function (options) -{ - var settings = this.getSettings(), - defaults = this.getDefaults(); +exports.configure = function (options) { + var settings = this.getSettings(), + defaults = this.getDefaults(); - if (!this._isAndroid) - return; + if (!this._isAndroid) + return; - if (!this._isActive) - { - console.log('BackgroundMode is not active, skipped...'); - return; - } + if (!this._isActive) { + console.log('BackgroundMode is not active, skipped...'); + return; + } - this._mergeObjects(options, settings); - this._mergeObjects(options, defaults); - this._settings = options; + this._mergeObjects(options, settings); + this._mergeObjects(options, defaults); + this._settings = options; - cordova.exec(null, null, 'BackgroundMode', 'configure', [options, true]); + cordova.exec(null, null, 'BackgroundMode', 'configure', [options, true]); }; /** @@ -156,12 +145,10 @@ exports.configure = function (options) * * @return [ Void ] */ -exports.disableWebViewOptimizations = function() -{ - if (this._isAndroid) - { - cordova.exec(null, null, 'BackgroundModeExt', 'webview', []); - } +exports.disableWebViewOptimizations = function () { + if (this._isAndroid) { + cordova.exec(null, null, 'BackgroundModeExt', 'webview', []); + } }; /** @@ -169,12 +156,10 @@ exports.disableWebViewOptimizations = function() * * @return [ Void ] */ -exports.disableBatteryOptimizations = function() -{ - if (this._isAndroid) - { - cordova.exec(null, null, 'BackgroundModeExt', 'battery', []); - } +exports.disableBatteryOptimizations = function () { + if (this._isAndroid) { + cordova.exec(null, null, 'BackgroundModeExt', 'battery', []); + } }; /** @@ -183,12 +168,10 @@ exports.disableBatteryOptimizations = function() * * @return [ Void ] */ -exports.openBatteryOptimizationsSettings = function() -{ - if (this._isAndroid) - { - cordova.exec(null, null, 'BackgroundModeExt', 'batterysettings', []); - } +exports.openBatteryOptimizationsSettings = function () { + if (this._isAndroid) { + cordova.exec(null, null, 'BackgroundModeExt', 'batterysettings', []); + } }; /** @@ -197,16 +180,32 @@ exports.openBatteryOptimizationsSettings = function() * * @return [ Void ] */ -exports.isIgnoringBatteryOptimizations = function(callback) -{ - if (this._isAndroid) - { - cordova.exec(callback, null, 'BackgroundModeExt', 'optimizationstatus', []); - } - else - { - callback(true); - } +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', []); + } }; /** @@ -218,12 +217,10 @@ exports.isIgnoringBatteryOptimizations = function(callback) * * @return [ Void ] */ -exports.openAppStartSettings = function (options) -{ - if (this._isAndroid) - { - cordova.exec(null, null, 'BackgroundModeExt', 'appstart', [options]); - } +exports.openAppStartSettings = function (options) { + if (this._isAndroid) { + cordova.exec(null, null, 'BackgroundModeExt', 'appstart', [options]); + } }; /** @@ -231,12 +228,10 @@ exports.openAppStartSettings = function (options) * * @return [ Void ] */ -exports.moveToBackground = function() -{ - if (this._isAndroid) - { - cordova.exec(null, null, 'BackgroundModeExt', 'background', []); - } +exports.moveToBackground = function () { + if (this._isAndroid) { + cordova.exec(null, null, 'BackgroundModeExt', 'background', []); + } }; /** @@ -244,12 +239,10 @@ exports.moveToBackground = function() * * @return [ Void ] */ -exports.moveToForeground = function() -{ - if (this.isActive() && this._isAndroid) - { - cordova.exec(null, null, 'BackgroundModeExt', 'foreground', []); - } +exports.moveToForeground = function () { + if (this.isActive() && this._isAndroid) { + cordova.exec(null, null, 'BackgroundModeExt', 'foreground', []); + } }; /** @@ -257,10 +250,10 @@ exports.moveToForeground = function() * * @return [ Void ] */ -exports.requestForegroundPermission = function() { - if (this._isAndroid) { - cordova.exec(null, null, 'BackgroundModeExt', 'requestTopPermissions', []); - } +exports.requestForegroundPermission = function () { + if (this._isAndroid) { + cordova.exec(null, null, 'BackgroundModeExt', 'requestTopPermissions', []); + } }; /** @@ -268,12 +261,10 @@ exports.requestForegroundPermission = function() { * * @return [ Void ] */ -exports.excludeFromTaskList = function() -{ - if (this._isAndroid) - { - cordova.exec(null, null, 'BackgroundModeExt', 'tasklist', []); - } +exports.excludeFromTaskList = function () { + if (this._isAndroid) { + cordova.exec(null, null, 'BackgroundModeExt', 'tasklist', []); + } }; /** @@ -282,11 +273,10 @@ exports.excludeFromTaskList = function() * * @return [ Void ] */ -exports.overrideBackButton = function() -{ - document.addEventListener('backbutton', function() { - exports.moveToBackground(); - }, false); +exports.overrideBackButton = function () { + document.addEventListener('backbutton', function () { + exports.moveToBackground(); + }, false); }; /** @@ -296,16 +286,13 @@ exports.overrideBackButton = function() * * @return [ Void ] */ -exports.isScreenOff = function (fn) -{ - if (this._isAndroid) - { - cordova.exec(fn, null, 'BackgroundModeExt', 'dimmed', []); - } - else - { - fn(undefined); - } +exports.isScreenOff = function (fn) { + if (this._isAndroid) { + cordova.exec(fn, null, 'BackgroundModeExt', 'dimmed', []); + } + else { + fn(undefined); + } }; /** @@ -313,12 +300,10 @@ exports.isScreenOff = function (fn) * * @return [ Void ] */ -exports.wakeUp = function() -{ - if (this._isAndroid) - { - cordova.exec(null, null, 'BackgroundModeExt', 'wakeup', []); - } +exports.wakeUp = function () { + if (this._isAndroid) { + cordova.exec(null, null, 'BackgroundModeExt', 'wakeup', []); + } }; /** @@ -326,12 +311,10 @@ exports.wakeUp = function() * * @return [ Void ] */ -exports.unlock = function() -{ - if (this._isAndroid) - { - cordova.exec(null, null, 'BackgroundModeExt', 'unlock', []); - } +exports.unlock = function () { + if (this._isAndroid) { + cordova.exec(null, null, 'BackgroundModeExt', 'unlock', []); + } }; /** @@ -339,9 +322,8 @@ exports.unlock = function() * * @return [ Boolean ] */ -exports.isEnabled = function() -{ - return this._isEnabled !== false; +exports.isEnabled = function () { + return this._isEnabled !== false; }; /** @@ -349,9 +331,8 @@ exports.isEnabled = function() * * @return [ Boolean ] */ -exports.isActive = function() -{ - return this._isActive !== false; +exports.isActive = function () { + return this._isActive !== false; }; exports._listener = {}; @@ -364,21 +345,19 @@ exports._listener = {}; * * @return [ Void ] */ -exports.fireEvent = function (event) -{ - var args = Array.apply(null, arguments).slice(1), - listener = this._listener[event]; +exports.fireEvent = function (event) { + var args = Array.apply(null, arguments).slice(1), + listener = this._listener[event]; - if (!listener) - return; + if (!listener) + return; - for (var i = 0; i < listener.length; i++) - { - var fn = listener[i][0], - scope = listener[i][1]; + for (var i = 0; i < listener.length; i++) { + var fn = listener[i][0], + scope = listener[i][1]; - fn.apply(scope, args); - } + fn.apply(scope, args); + } }; /** @@ -390,19 +369,17 @@ exports.fireEvent = function (event) * * @return [ Void ] */ -exports.on = function (event, callback, scope) -{ - if (typeof callback !== "function") - return; +exports.on = function (event, callback, scope) { + if (typeof callback !== "function") + return; - if (!this._listener[event]) - { - this._listener[event] = []; - } + if (!this._listener[event]) { + this._listener[event] = []; + } - var item = [callback, scope || window]; + var item = [callback, scope || window]; - this._listener[event].push(item); + this._listener[event].push(item); }; /** @@ -413,23 +390,20 @@ exports.on = function (event, callback, scope) * * @return [ Void ] */ -exports.un = function (event, callback) -{ - var listener = this._listener[event]; +exports.un = function (event, callback) { + var listener = this._listener[event]; - if (!listener) - return; + if (!listener) + return; - for (var i = 0; i < listener.length; i++) - { - var fn = listener[i][0]; + for (var i = 0; i < listener.length; i++) { + var fn = listener[i][0]; - if (fn == callback) - { - listener.splice(i, 1); - break; - } - } + if (fn == callback) { + listener.splice(i, 1); + break; + } + } }; /** @@ -453,22 +427,22 @@ exports._isActive = false; */ exports._defaults = { - title: 'App is running in background', - text: 'Doing heavy tasks.', - subText: '', - bigText: false, - resume: true, - silent: false, - hidden: true, - color: undefined, - icon: 'icon', - channelName: 'cordova-plugin-background-mode', - channelDescription: 'cordova-plugin-background-moden notification', - allowClose: false, - closeIcon: 'power', - closeTitle: 'Close', - showWhen: true, - visibility: undefined + title: 'App is running in background', + text: 'Doing heavy tasks.', + subText: '', + bigText: false, + resume: true, + silent: false, + hidden: true, + color: undefined, + icon: 'icon', + channelName: 'cordova-plugin-background-mode', + channelDescription: 'cordova-plugin-background-moden notification', + allowClose: false, + closeIcon: 'power', + closeTitle: 'Close', + showWhen: true, + visibility: undefined }; /** @@ -481,18 +455,15 @@ exports._defaults = { * * @return [ Object ] Default values merged with custom values. */ -exports._mergeObjects = function (options, toMergeIn) -{ - for (var key in toMergeIn) - { - if (!options.hasOwnProperty(key)) - { - options[key] = toMergeIn[key]; - continue; - } - } +exports._mergeObjects = function (options, toMergeIn) { + for (var key in toMergeIn) { + if (!options.hasOwnProperty(key)) { + options[key] = toMergeIn[key]; + continue; + } + } - return options; + return options; }; /** @@ -505,13 +476,12 @@ exports._mergeObjects = function (options, toMergeIn) * * @return [ Void ] */ -exports._setActive = function(value) -{ - if (this._isActive == value) - return; +exports._setActive = function (value) { + if (this._isActive == value) + return; - this._isActive = value; - this._settings = value ? this._mergeObjects({}, this._defaults) : {}; + this._isActive = value; + this._settings = value ? this._mergeObjects({}, this._defaults) : {}; }; /** @@ -524,38 +494,32 @@ exports._setActive = function(value) * * @return [ Void ] */ -exports._pluginInitialize = function() -{ - this._isAndroid = device.platform.match(/^android|amazon/i) !== null; - this.setDefaults({}); +exports._pluginInitialize = function () { + this._isAndroid = device.platform.match(/^android|amazon/i) !== null; + this.setDefaults({}); - if (device.platform == 'browser') - { - this.enable(); - this._isEnabled = true; - } + if (device.platform == 'browser') { + this.enable(); + this._isEnabled = true; + } - this._isActive = this._isActive || device.platform == 'browser'; + this._isActive = this._isActive || device.platform == 'browser'; }; // Called before 'deviceready' listener will be called -channel.onCordovaReady.subscribe(function() -{ - channel.onCordovaInfoReady.subscribe(function() { - exports._pluginInitialize(); - }); +channel.onCordovaReady.subscribe(function () { + channel.onCordovaInfoReady.subscribe(function () { + exports._pluginInitialize(); + }); }); // Called after 'deviceready' event -channel.deviceready.subscribe(function() -{ - if (exports.isEnabled()) - { - exports.fireEvent('enable'); - } +channel.deviceready.subscribe(function () { + if (exports.isEnabled()) { + exports.fireEvent('enable'); + } - if (exports.isActive()) - { - exports.fireEvent('activate'); - } + if (exports.isActive()) { + exports.fireEvent('activate'); + } });