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] 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', []); } };