From 711800f3d0a3028c65108218de190776950a295c Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Sat, 25 Feb 2017 18:01:08 +0100 Subject: [PATCH] Collaborative attempt to initialize the background webview on startup --- app/src/main/AndroidManifest.xml | 4 + .../gadgetbridge/GBApplication.java | 7 ++ .../activities/BackgroundWebViewActivity.java | 16 ++++ .../activities/ExternalPebbleJSActivity.java | 4 +- .../AbstractAppManagerFragment.java | 2 - .../devices/pebble/PebbleIoThread.java | 5 +- .../gadgetbridge/util/WebViewSingleton.java | 82 ++++++++----------- app/src/main/res/values/strings.xml | 1 + 8 files changed, 67 insertions(+), 54 deletions(-) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/BackgroundWebViewActivity.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b4132de8f..97c8d09d5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -281,6 +281,10 @@ android:name=".activities.DiscoveryActivity" android:label="@string/title_activity_discovery" android:parentActivityName=".activities.ControlCenter" /> + diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index 1973e4e41..599b53c13 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -27,6 +27,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; +import nodomain.freeyourgadget.gadgetbridge.activities.BackgroundWebViewActivity; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHelper; import nodomain.freeyourgadget.gadgetbridge.database.DBOpenHelper; @@ -124,6 +125,8 @@ public class GBApplication extends Application { deviceManager = new DeviceManager(this); + createWebViewActivity(); + deviceService = createDeviceService(); loadBlackList(); @@ -132,6 +135,10 @@ public class GBApplication extends Application { } } + private void createWebViewActivity() { + startActivity(new Intent(getContext(), BackgroundWebViewActivity.class)); + } + @Override public void onTrimMemory(int level) { super.onTrimMemory(level); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/BackgroundWebViewActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/BackgroundWebViewActivity.java new file mode 100644 index 000000000..9b3fda2d3 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/BackgroundWebViewActivity.java @@ -0,0 +1,16 @@ +package nodomain.freeyourgadget.gadgetbridge.activities; + +import android.app.Activity; +import android.os.Bundle; +import android.os.PersistableBundle; + +import nodomain.freeyourgadget.gadgetbridge.util.WebViewSingleton; + +public class BackgroundWebViewActivity extends Activity { + @Override + public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) { + super.onCreate(savedInstanceState, persistentState); + WebViewSingleton.createWebView(this); + setVisible(false); + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ExternalPebbleJSActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ExternalPebbleJSActivity.java index b103d1f95..a7985dbb9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ExternalPebbleJSActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ExternalPebbleJSActivity.java @@ -48,8 +48,8 @@ public class ExternalPebbleJSActivity extends GBActivity { setContentView(R.layout.activity_external_pebble_js); - myWebView = WebViewSingleton.getorInitWebView(this, mGBDevice, appUuid); - myWebView.setWillNotDraw(false); + WebViewSingleton.updateActivityContext(this); + myWebView = WebViewSingleton.getWebView(); myWebView.addJavascriptInterface(new ActivityJSInterface(ExternalPebbleJSActivity.this), "GBActivity"); FrameLayout fl = (FrameLayout) findViewById(R.id.webview_placeholder); fl.addView(myWebView); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/appmanager/AbstractAppManagerFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/appmanager/AbstractAppManagerFragment.java index 090560e7a..023a36848 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/appmanager/AbstractAppManagerFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/appmanager/AbstractAppManagerFragment.java @@ -338,8 +338,6 @@ public abstract class AbstractAppManagerFragment extends Fragment { view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); popupMenu.show(); - //TODO: replace with local broadcast on app start - WebViewSingleton.getorInitWebView(getActivity(), mGBDevice, selectedApp.getUUID()); return true; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java index 2a116a451..6f3348d64 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java @@ -82,7 +82,7 @@ class PebbleIoThread extends GBDeviceIoThread { private int mBytesWritten = -1; private void sendAppMessageJS(GBDeviceEventAppMessage appMessage) { - WebViewSingleton.getorInitWebView(getContext(), gbDevice, appMessage.appUUID); +// WebViewSingleton.runJavascriptInterface(gbDevice, appMessage.appUUID); WebViewSingleton.appMessage(appMessage.message); } @@ -487,8 +487,7 @@ class PebbleIoThread extends GBDeviceIoThread { break; case START: LOG.info("got GBDeviceEventAppManagement START event for uuid: " + appMgmt.uuid); - WebViewSingleton.getorInitWebView(getContext(), gbDevice, appMgmt.uuid); - //TODO: the method call above will not work the first time as we need an activity. Either we find a way to have one here, or replace it with a local broadcast + WebViewSingleton.runJavascriptInterface(gbDevice, appMgmt.uuid); break; default: break; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/WebViewSingleton.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/WebViewSingleton.java index 89c5f9aa8..ce453821b 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/WebViewSingleton.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/WebViewSingleton.java @@ -13,6 +13,7 @@ import android.net.Uri; import android.os.Build; import android.os.Handler; import android.os.Looper; +import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.webkit.ConsoleMessage; import android.webkit.JavascriptInterface; @@ -58,63 +59,50 @@ public class WebViewSingleton extends Activity { private WebViewSingleton() { } - public static WebView getorInitWebView(Context context, final GBDevice device, final UUID uuid) { + public static synchronized WebView createWebView(Context context) { + if (instance == null) { + contextWrapper = new MutableContextWrapper(context); + instance = new WebView(contextWrapper); + instance.setWillNotDraw(true); + instance.clearCache(true); + instance.setWebViewClient(new GBWebClient()); + instance.setWebChromeClient(new GBChromeClient()); + instance.setWebContentsDebuggingEnabled(true); + WebSettings webSettings = instance.getSettings(); + webSettings.setJavaScriptEnabled(true); + //needed to access the DOM + webSettings.setDomStorageEnabled(true); + //needed for localstorage + webSettings.setDatabaseEnabled(true); + } + return instance; + } - if (jsInterface == null || (jsInterface != null && (!device.equals(jsInterface.device) || !uuid.equals(jsInterface.mUuid)))) { + public static void updateActivityContext(Context context) { + if (context instanceof Activity) { + contextWrapper.setBaseContext(context); + } + } + + @NonNull + public static WebView getWebView() { + return instance; + } + + public static void runJavascriptInterface(final GBDevice device, final UUID uuid) { + if (jsInterface == null || !device.equals(jsInterface.device) || !uuid.equals(jsInterface.mUuid)) { jsInterface = new JSInterface(device, uuid); new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { - if (instance != null) { - instance.removeJavascriptInterface("GBjs"); - instance.addJavascriptInterface(jsInterface, "GBjs"); - instance.loadUrl("file:///android_asset/app_config/configure.html"); - } + instance.removeJavascriptInterface("GBjs"); + instance.addJavascriptInterface(jsInterface, "GBjs"); + instance.loadUrl("file:///android_asset/app_config/configure.html"); } }); } else { LOG.debug("Not replacing the JS in the webview. JS uuid " + jsInterface.mUuid.toString()); } - - if (context instanceof Activity) { - if (contextWrapper != null) { - contextWrapper.setBaseContext(context); - } else { - contextWrapper = new MutableContextWrapper(context); - } - - if (instance == null) { - LOG.debug("WEBV: creating webview"); - - new Handler(Looper.getMainLooper()).post(new Runnable() { - @Override - public void run() { - instance = new WebView(contextWrapper); - instance.setWillNotDraw(true); - instance.clearCache(true); - instance.setWebViewClient(new GBWebClient()); - instance.setWebChromeClient(new GBChromeClient()); - instance.setWebContentsDebuggingEnabled(true); - WebSettings webSettings = instance.getSettings(); - webSettings.setJavaScriptEnabled(true); - //needed to access the DOM - webSettings.setDomStorageEnabled(true); - //needed for localstorage - webSettings.setDatabaseEnabled(true); - if (jsInterface != null) { - LOG.debug("Attaching the existing jsInterface to the new webview instance"); - instance.addJavascriptInterface(jsInterface, "GBjs"); - instance.loadUrl("file:///android_asset/app_config/configure.html"); - } - } - }); - } - - } else { - LOG.debug("WEBV: not using the passed context, as it is not an activity"); - } - - return instance; } public static void appMessage(final String message) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7e3d0d247..fbbe82e27 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -393,4 +393,5 @@ 24H AM/PM Alarm Clock + Web View Activity