From 9fb7257297a7989eef244e0ba22d332396c4a9b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Fri, 8 Feb 2019 18:33:54 +0100 Subject: [PATCH 1/8] Change some formats --- www/background-mode.js | 194 +++++++++++++++++++++-------------------- 1 file changed, 101 insertions(+), 93 deletions(-) diff --git a/www/background-mode.js b/www/background-mode.js index 440edc3..85f9b50 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -22,11 +22,6 @@ var exec = require('cordova/exec'), channel = require('cordova/channel'); - -/************* - * INTERFACE * - *************/ - /** * Activates the background mode. When activated the application * will be prevented from going to sleep while in background @@ -34,14 +29,15 @@ var exec = require('cordova/exec'), * * @return [ Void ] */ -exports.enable = function() { +exports.enable = function() +{ if (this.isEnabled()) return; var fn = function() { - exports._isEnabled = true; - exports.fireEvent('enable'); - }; + exports._isEnabled = true; + exports.fireEvent('enable'); + }; cordova.exec(fn, null, 'BackgroundMode', 'enable', []); }; @@ -52,14 +48,15 @@ exports.enable = function() { * * @return [ Void ] */ -exports.disable = function() { +exports.disable = function() +{ if (!this.isEnabled()) return; var fn = function() { - exports._isEnabled = false; - exports.fireEvent('disable'); - }; + exports._isEnabled = false; + exports.fireEvent('disable'); + }; cordova.exec(fn, null, 'BackgroundMode', 'disable', []); }; @@ -71,7 +68,8 @@ exports.disable = function() { * * @return [ Void ] */ -exports.setEnabled = function (enable) { +exports.setEnabled = function (enable) +{ if (enable) { this.enable(); } else { @@ -84,7 +82,8 @@ exports.setEnabled = function (enable) { * * @return [ Object ] */ -exports.getDefaults = function() { +exports.getDefaults = function() +{ return this._defaults; }; @@ -93,7 +92,8 @@ exports.getDefaults = function() { * * @return [ Object ] */ -exports.getSettings = function() { +exports.getSettings = function() +{ return this._settings || {}; }; @@ -104,16 +104,20 @@ 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]); } }; @@ -126,14 +130,16 @@ 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; } @@ -150,8 +156,10 @@ exports.configure = function (options) { * * @return [ Void ] */ -exports.disableWebViewOptimizations = function() { - if (this._isAndroid) { +exports.disableWebViewOptimizations = function() +{ + if (this._isAndroid) + { cordova.exec(null, null, 'BackgroundMode', 'optimizations', []); } }; @@ -161,8 +169,10 @@ exports.disableWebViewOptimizations = function() { * * @return [ Void ] */ -exports.moveToBackground = function() { - if (this._isAndroid) { +exports.moveToBackground = function() +{ + if (this._isAndroid) + { cordova.exec(null, null, 'BackgroundMode', 'background', []); } }; @@ -172,8 +182,10 @@ 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, 'BackgroundMode', 'foreground', []); } }; @@ -183,8 +195,10 @@ exports.moveToForeground = function() { * * @return [ Void ] */ -exports.excludeFromTaskList = function() { - if (this._isAndroid) { +exports.excludeFromTaskList = function() +{ + if (this._isAndroid) + { cordova.exec(null, null, 'BackgroundMode', 'tasklist', []); } }; @@ -195,7 +209,8 @@ exports.excludeFromTaskList = function() { * * @return [ Void ] */ -exports.overrideBackButton = function() { +exports.overrideBackButton = function() +{ document.addEventListener('backbutton', function() { exports.moveToBackground(); }, false); @@ -208,10 +223,14 @@ exports.overrideBackButton = function() { * * @return [ Void ] */ -exports.isScreenOff = function (fn) { - if (this._isAndroid) { +exports.isScreenOff = function (fn) +{ + if (this._isAndroid) + { cordova.exec(fn, null, 'BackgroundMode', 'dimmed', []); - } else { + } + else + { fn(undefined); } }; @@ -221,8 +240,10 @@ exports.isScreenOff = function (fn) { * * @return [ Void ] */ -exports.wakeUp = function() { - if (this._isAndroid) { +exports.wakeUp = function() +{ + if (this._isAndroid) + { cordova.exec(null, null, 'BackgroundMode', 'wakeup', []); } }; @@ -232,8 +253,10 @@ exports.wakeUp = function() { * * @return [ Void ] */ -exports.unlock = function() { - if (this._isAndroid) { +exports.unlock = function() +{ + if (this._isAndroid) + { cordova.exec(null, null, 'BackgroundMode', 'unlock', []); } }; @@ -243,7 +266,8 @@ exports.unlock = function() { * * @return [ Boolean ] */ -exports.isEnabled = function() { +exports.isEnabled = function() +{ return this._isEnabled !== false; }; @@ -252,15 +276,11 @@ exports.isEnabled = function() { * * @return [ Boolean ] */ -exports.isActive = function() { +exports.isActive = function() +{ return this._isActive !== false; }; - -/********** - * EVENTS * - **********/ - exports._listener = {}; /** @@ -271,14 +291,16 @@ 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]; @@ -295,12 +317,13 @@ 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] = []; } @@ -317,51 +340,25 @@ 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; } } }; -/** - * @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 * @@ -381,7 +378,8 @@ exports._isActive = false; * * Default values of all available options. */ -exports._defaults = { +exports._defaults = +{ title: 'App is running in background', text: 'Doing heavy tasks.', bigText: false, @@ -402,9 +400,12 @@ 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; } @@ -423,7 +424,8 @@ exports._mergeObjects = function (options, toMergeIn) { * * @return [ Void ] */ -exports._setActive = function(value) { +exports._setActive = function(value) +{ if (this._isActive == value) return; @@ -441,11 +443,13 @@ 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; } @@ -454,19 +458,23 @@ exports._pluginInitialize = function() { }; // Called before 'deviceready' listener will be called -channel.onCordovaReady.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'); } }); From 537f7a859f6ae7aacadcc42eb6db28571a20eea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Fri, 8 Feb 2019 18:45:03 +0100 Subject: [PATCH 2/8] New "disableBatteryOptimizations" function for Android --- plugin.xml | 1 + src/android/BackgroundExt.java | 29 ++++++++++++++++++++++++++++- src/android/BackgroundMode.java | 4 ++-- src/android/ForegroundService.java | 1 - www/background-mode.js | 15 ++++++++++++++- 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/plugin.xml b/plugin.xml index 52bd90e..004c203 100644 --- a/plugin.xml +++ b/plugin.xml @@ -75,6 +75,7 @@ + webView.loadUrl("javascript:" + js)); } - } diff --git a/src/android/ForegroundService.java b/src/android/ForegroundService.java index 0544192..4e4aa8f 100644 --- a/src/android/ForegroundService.java +++ b/src/android/ForegroundService.java @@ -320,5 +320,4 @@ public class ForegroundService extends Service { { return (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } - } diff --git a/www/background-mode.js b/www/background-mode.js index 85f9b50..37e6a18 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -160,7 +160,20 @@ exports.disableWebViewOptimizations = function() { if (this._isAndroid) { - cordova.exec(null, null, 'BackgroundMode', 'optimizations', []); + cordova.exec(null, null, 'BackgroundMode', 'webviewoptimizations', []); + } +}; + +/** + * Disables battery optimazation mode for the app. + * + * @return [ Void ] + */ +exports.disableBatteryOptimizations = function() +{ + if (this._isAndroid) + { + cordova.exec(null, null, 'BackgroundMode', 'batteryoptimizations', []); } }; From a9669052c0e927eedaa03d896217a011b87eb43c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Sun, 10 Feb 2019 11:22:11 +0100 Subject: [PATCH 3/8] REQUEST_IGNORE_BATTERY_OPTIMIZATIONS needs to be added manually --- plugin.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/plugin.xml b/plugin.xml index 004c203..52bd90e 100644 --- a/plugin.xml +++ b/plugin.xml @@ -75,7 +75,6 @@ - Date: Sun, 10 Feb 2019 13:09:57 +0100 Subject: [PATCH 4/8] Initial implementation for openAppStartSettings --- src/android/BackgroundExt.java | 104 ++++++++++++++++++++++++++++++-- src/android/BackgroundMode.java | 3 +- www/background-mode.js | 17 ++++++ 3 files changed, 119 insertions(+), 5 deletions(-) diff --git a/src/android/BackgroundExt.java b/src/android/BackgroundExt.java index 3fe0657..07061d9 100644 --- a/src/android/BackgroundExt.java +++ b/src/android/BackgroundExt.java @@ -26,8 +26,11 @@ import android.annotation.TargetApi; import android.app.Activity; import android.app.ActivityManager; import android.app.ActivityManager.AppTask; +import android.app.AlertDialog; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; import android.net.Uri; import android.os.Build; import android.os.PowerManager; @@ -39,11 +42,18 @@ import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CordovaWebView; import org.apache.cordova.PluginResult; import org.apache.cordova.PluginResult.Status; +import org.json.JSONArray; +import org.json.JSONObject; +import java.util.Arrays; 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.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_CODES.M; import static android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS; @@ -58,6 +68,29 @@ import static android.view.WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON; */ class BackgroundExt { + // List of intents for various manufactures to adjust the power saver mode. + private static final List APPSTART_INTENTS = 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")) + ); + // Reference to the cordova interface passed by the plugin private final CordovaInterface cordova; @@ -72,7 +105,7 @@ class BackgroundExt { * * @param plugin The cordova plugin. */ - BackgroundExt(CordovaPlugin plugin) + private BackgroundExt (CordovaPlugin plugin) { this.cordova = plugin.cordova; this.webView = plugin.webView; @@ -82,22 +115,26 @@ class BackgroundExt { * Executes the request within a thread. * * @param action The action to execute. + * @param args The exec() arguments. * @param callback The callback context used when * calling back into JavaScript. */ - void executeAsync (String action, CallbackContext callback) + static void execute (CordovaPlugin plugin, String action, JSONArray args, + CallbackContext callback) { - cordova.getThreadPool().execute(() -> execute(action, callback)); + plugin.cordova.getThreadPool().execute(() -> new BackgroundExt(plugin).execute(action, args, callback)); } /** * 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. */ - private void execute (String action, CallbackContext callback) + private void execute (String action, JSONArray args, + CallbackContext callback) { switch (action) { @@ -107,6 +144,9 @@ class BackgroundExt { case "webviewoptimizations": disableWebViewOptimizations(); break; + case "appstart": + openAppStart(args.opt(0)); + break; case "background": moveToBackground(); break; @@ -186,6 +226,10 @@ class BackgroundExt { thread.start(); } + /** + * Disables battery optimizations for the app. + * Requires permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS to function. + */ @SuppressLint("BatteryLife") private void disableBatteryOptimizations() { @@ -206,6 +250,58 @@ class BackgroundExt { 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 : APPSTART_INTENTS) + { + 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. */ diff --git a/src/android/BackgroundMode.java b/src/android/BackgroundMode.java index 8278901..d7fc2e1 100644 --- a/src/android/BackgroundMode.java +++ b/src/android/BackgroundMode.java @@ -106,13 +106,14 @@ public class BackgroundMode extends CordovaPlugin { break; case "webviewoptimizations": case "batteryoptimizations": + case "appstart": case "background": case "foreground": case "tasklist": case "dimmed": case "wakeup": case "unlock": - new BackgroundExt(this).executeAsync(action, callback); + BackgroundExt.execute(this, action, args, callback); break; default: validAction = false; diff --git a/www/background-mode.js b/www/background-mode.js index 37e6a18..9fd0576 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -177,6 +177,23 @@ exports.disableBatteryOptimizations = function() } }; +/** + * 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, 'BackgroundMode', 'appstart', [options]); + } +}; + /** * Move app to background (Android only). * From 2c4df332d33b37e9776641004dd9347c902480f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Mon, 11 Feb 2019 15:21:06 +0100 Subject: [PATCH 5/8] Restructure BackgroundModeExt as an internal cordova plugin --- plugin.xml | 10 ++- src/android/BackgroundMode.java | 43 ++++----- ...kgroundExt.java => BackgroundModeExt.java} | 88 ++++++++----------- src/android/ForegroundService.java | 30 +++---- src/browser/BackgroundModeProxy.js | 30 +++---- src/windows/BackgroundModeProxy.js | 30 +++---- www/background-mode.js | 18 ++-- 7 files changed, 111 insertions(+), 138 deletions(-) rename src/android/{BackgroundExt.java => BackgroundModeExt.java} (86%) diff --git a/plugin.xml b/plugin.xml index 52bd90e..2b77440 100644 --- a/plugin.xml +++ b/plugin.xml @@ -59,9 +59,11 @@ - - + + + + + @@ -82,7 +84,7 @@ target-dir="src/de/appplant/cordova/plugin/background" /> APPSTART_INTENTS = Arrays.asList( @@ -91,40 +89,9 @@ class BackgroundExt { new Intent().setComponent(new ComponentName("com.zui.safecenter", "com.lenovo.safecenter.MainTab.LeSafeMainActivity")) ); - // 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 private PowerManager.WakeLock wakeLock; - /** - * Initialize the extension to perform non-background related tasks. - * - * @param plugin The cordova plugin. - */ - private BackgroundExt (CordovaPlugin plugin) - { - this.cordova = plugin.cordova; - this.webView = plugin.webView; - } - - /** - * Executes the request within a thread. - * - * @param action The action to execute. - * @param args The exec() arguments. - * @param callback The callback context used when - * calling back into JavaScript. - */ - static void execute (CordovaPlugin plugin, String action, JSONArray args, - CallbackContext callback) - { - plugin.cordova.getThreadPool().execute(() -> new BackgroundExt(plugin).execute(action, args, callback)); - } - /** * Executes the request. * @@ -132,16 +99,21 @@ class BackgroundExt { * @param args The exec() arguments. * @param callback The callback context used when * calling back into JavaScript. + * + * @return Returning false results in a "MethodNotFound" error. */ - private void execute (String action, JSONArray args, - CallbackContext callback) + @Override + public boolean execute (String action, JSONArray args, + CallbackContext callback) { + boolean validAction = true; + switch (action) { - case "batteryoptimizations": + case "battery": disableBatteryOptimizations(); break; - case "webviewoptimizations": + case "webview": disableWebViewOptimizations(); break; case "appstart": @@ -166,7 +138,17 @@ class BackgroundExt { wakeup(); unlock(); break; + default: + validAction = false; } + + if (validAction) { + callback.success(); + } else { + callback.error("Invalid action: " + action); + } + + return validAction; } /** diff --git a/src/android/ForegroundService.java b/src/android/ForegroundService.java index 4e4aa8f..806c956 100644 --- a/src/android/ForegroundService.java +++ b/src/android/ForegroundService.java @@ -1,22 +1,22 @@ /* - Copyright 2013-2017 appPlant GmbH + Copyright 2013 Sebastián Katzer - 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. */ package de.appplant.cordova.plugin.background; diff --git a/src/browser/BackgroundModeProxy.js b/src/browser/BackgroundModeProxy.js index 5ba7880..4a880cb 100644 --- a/src/browser/BackgroundModeProxy.js +++ b/src/browser/BackgroundModeProxy.js @@ -1,22 +1,22 @@ /* - Copyright 2013-2017 appPlant GmbH + Copyright 2013 Sebastián Katzer - 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. */ /** diff --git a/src/windows/BackgroundModeProxy.js b/src/windows/BackgroundModeProxy.js index 5542e3f..b151ae1 100644 --- a/src/windows/BackgroundModeProxy.js +++ b/src/windows/BackgroundModeProxy.js @@ -1,22 +1,22 @@ /* - Copyright 2013-2017 appPlant GmbH + Copyright 2013 Sebastián Katzer - 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 plugin = cordova.plugins.backgroundMode; diff --git a/www/background-mode.js b/www/background-mode.js index 9fd0576..fff3f8d 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -160,7 +160,7 @@ exports.disableWebViewOptimizations = function() { if (this._isAndroid) { - cordova.exec(null, null, 'BackgroundMode', 'webviewoptimizations', []); + cordova.exec(null, null, 'BackgroundModeExt', 'webview', []); } }; @@ -173,7 +173,7 @@ exports.disableBatteryOptimizations = function() { if (this._isAndroid) { - cordova.exec(null, null, 'BackgroundMode', 'batteryoptimizations', []); + cordova.exec(null, null, 'BackgroundModeExt', 'battery', []); } }; @@ -190,7 +190,7 @@ exports.openAppStartSettings = function (options) { if (this._isAndroid) { - cordova.exec(null, null, 'BackgroundMode', 'appstart', [options]); + cordova.exec(null, null, 'BackgroundModeExt', 'appstart', [options]); } }; @@ -203,7 +203,7 @@ exports.moveToBackground = function() { if (this._isAndroid) { - cordova.exec(null, null, 'BackgroundMode', 'background', []); + cordova.exec(null, null, 'BackgroundModeExt', 'background', []); } }; @@ -216,7 +216,7 @@ exports.moveToForeground = function() { if (this.isActive() && this._isAndroid) { - cordova.exec(null, null, 'BackgroundMode', 'foreground', []); + cordova.exec(null, null, 'BackgroundModeExt', 'foreground', []); } }; @@ -229,7 +229,7 @@ exports.excludeFromTaskList = function() { if (this._isAndroid) { - cordova.exec(null, null, 'BackgroundMode', 'tasklist', []); + cordova.exec(null, null, 'BackgroundModeExt', 'tasklist', []); } }; @@ -257,7 +257,7 @@ exports.isScreenOff = function (fn) { if (this._isAndroid) { - cordova.exec(fn, null, 'BackgroundMode', 'dimmed', []); + cordova.exec(fn, null, 'BackgroundModeExt', 'dimmed', []); } else { @@ -274,7 +274,7 @@ exports.wakeUp = function() { if (this._isAndroid) { - cordova.exec(null, null, 'BackgroundMode', 'wakeup', []); + cordova.exec(null, null, 'BackgroundModeExt', 'wakeup', []); } }; @@ -287,7 +287,7 @@ exports.unlock = function() { if (this._isAndroid) { - cordova.exec(null, null, 'BackgroundMode', 'unlock', []); + cordova.exec(null, null, 'BackgroundModeExt', 'unlock', []); } }; From 5f6072e738e2d171d657715162ab58d9bd15ab9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Mon, 11 Feb 2019 15:23:27 +0100 Subject: [PATCH 6/8] Fix type in clearScreenAndKeyguardFlags --- src/android/BackgroundMode.java | 30 +++++++++++++------------- src/android/BackgroundModeExt.java | 34 +++++++++++++++--------------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/android/BackgroundMode.java b/src/android/BackgroundMode.java index cac418a..811bcf1 100644 --- a/src/android/BackgroundMode.java +++ b/src/android/BackgroundMode.java @@ -1,22 +1,22 @@ /* - Copyright 2013 Sebastián Katzer + 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. */ package de.appplant.cordova.plugin.background; diff --git a/src/android/BackgroundModeExt.java b/src/android/BackgroundModeExt.java index 0eef67b..8d9fe6c 100644 --- a/src/android/BackgroundModeExt.java +++ b/src/android/BackgroundModeExt.java @@ -1,22 +1,22 @@ /* - Copyright 2013 Sebastián Katzer + 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. */ package de.appplant.cordova.plugin.background; @@ -176,7 +176,7 @@ public class BackgroundModeExt extends CordovaPlugin { Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); - clearSreenAndKeyguardFlags(); + clearScreenAndKeyguardFlags(); app.startActivity(intent); } @@ -397,7 +397,7 @@ public class BackgroundModeExt extends CordovaPlugin { /** * 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)); } From 157b34f867d14fd02109906609e42bdfae07fc5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Mon, 11 Feb 2019 15:26:42 +0100 Subject: [PATCH 7/8] Create app start intents at runtime --- src/android/BackgroundMode.java | 30 +++++------ src/android/BackgroundModeExt.java | 83 ++++++++++++++++-------------- 2 files changed, 59 insertions(+), 54 deletions(-) diff --git a/src/android/BackgroundMode.java b/src/android/BackgroundMode.java index 811bcf1..cac418a 100644 --- a/src/android/BackgroundMode.java +++ b/src/android/BackgroundMode.java @@ -1,22 +1,22 @@ /* - Copyright 2013-2017 appPlant GmbH + Copyright 2013 Sebastián Katzer - 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. */ package de.appplant.cordova.plugin.background; diff --git a/src/android/BackgroundModeExt.java b/src/android/BackgroundModeExt.java index 8d9fe6c..380d4a8 100644 --- a/src/android/BackgroundModeExt.java +++ b/src/android/BackgroundModeExt.java @@ -1,22 +1,22 @@ /* - Copyright 2013-2017 appPlant GmbH + Copyright 2013 Sebastián Katzer - 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. */ package de.appplant.cordova.plugin.background; @@ -66,29 +66,6 @@ import static android.view.WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON; */ public class BackgroundModeExt extends CordovaPlugin { - // List of intents for various manufactures to adjust the power saver mode. - private static final List APPSTART_INTENTS = 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")) - ); - // To keep the device awake private PowerManager.WakeLock wakeLock; @@ -243,7 +220,7 @@ public class BackgroundModeExt extends CordovaPlugin { Activity activity = cordova.getActivity(); PackageManager pm = activity.getPackageManager(); - for (Intent intent : APPSTART_INTENTS) + for (Intent intent : getAppstartIntents()) { if (pm.resolveActivity(intent, MATCH_DEFAULT_ONLY) != null) { @@ -437,4 +414,32 @@ public class BackgroundModeExt extends CordovaPlugin { { 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")) + ); + } } From 0e95173de0faec26e0f131a4701bfd212418d1c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Mon, 11 Feb 2019 15:27:15 +0100 Subject: [PATCH 8/8] Fix typo in getAppStartIntents --- src/android/BackgroundModeExt.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/android/BackgroundModeExt.java b/src/android/BackgroundModeExt.java index 380d4a8..ed76598 100644 --- a/src/android/BackgroundModeExt.java +++ b/src/android/BackgroundModeExt.java @@ -220,7 +220,7 @@ public class BackgroundModeExt extends CordovaPlugin { Activity activity = cordova.getActivity(); PackageManager pm = activity.getPackageManager(); - for (Intent intent : getAppstartIntents()) + for (Intent intent : getAppStartIntents()) { if (pm.resolveActivity(intent, MATCH_DEFAULT_ONLY) != null) { @@ -418,7 +418,7 @@ public class BackgroundModeExt extends CordovaPlugin { /** * Returns list of all possible intents to present the app start settings. */ - private List getAppstartIntents() + private List getAppStartIntents() { return Arrays.asList( new Intent().setComponent(new ComponentName("com.miui.securitycenter","com.miui.permcenter.autostart.AutoStartManagementActivity")),