From 6000adfa15e06a6cfdfd435a72f6015e34c4c542 Mon Sep 17 00:00:00 2001 From: Nathan Kerr Date: Fri, 14 Aug 2020 10:34:19 -0700 Subject: [PATCH] Add ability to re-enable webview optimizations --- src/android/BackgroundModeExt.java | 28 ++++++++++++++++++++++++++++ www/background-mode.js | 8 ++++++++ 2 files changed, 36 insertions(+) diff --git a/src/android/BackgroundModeExt.java b/src/android/BackgroundModeExt.java index 06a824e..0c0f079 100644 --- a/src/android/BackgroundModeExt.java +++ b/src/android/BackgroundModeExt.java @@ -101,6 +101,9 @@ public class BackgroundModeExt extends CordovaPlugin { case "webview": disableWebViewOptimizations(); break; + case "enableWebview": + enableWebViewOptimizations(); + break; case "appstart": openAppStart(args.opt(0)); break; @@ -196,6 +199,31 @@ public class BackgroundModeExt extends CordovaPlugin { thread.start(); } + private void enableWebViewOptimizations() { + 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("onHide") + .invoke(view); + } catch (Exception e){ + view.dispatchWindowVisibilityChanged(View.GONE); + } + }); + } catch (InterruptedException e) { + // do nothing + } + } + }; + + thread.start(); + } + /** * Disables battery optimizations for the app. * Requires permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS to function. diff --git a/www/background-mode.js b/www/background-mode.js index 4464425..3d24622 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -164,6 +164,14 @@ exports.disableWebViewOptimizations = function() } }; +exports.enableWebViewOptimizations = function() +{ + if (this._isAndroid) + { + cordova.exec(null, null, 'BackgroundModeExt', 'enableWebview', []); + } +}; + /** * Disables battery optimazation mode for the app. *