From 4ef0415da260d9923063b6b0dba73f9560109812 Mon Sep 17 00:00:00 2001 From: Daniele Gobbetti Date: Sun, 1 Jan 2017 21:01:58 +0100 Subject: [PATCH] Pebble: webview. Ensure we are on the main thread for disposing the webview and implement sending messages to the webview. --- .../gadgetbridge/util/WebViewSingleton.java | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) 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 b19586a3f..e8a0f1c3c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/WebViewSingleton.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/WebViewSingleton.java @@ -5,8 +5,12 @@ import android.content.Context; import android.content.Intent; import android.content.MutableContextWrapper; import android.net.Uri; +import android.os.Build; +import android.os.Handler; +import android.os.Looper; import android.webkit.ConsoleMessage; import android.webkit.JavascriptInterface; +import android.webkit.ValueCallback; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; @@ -83,12 +87,36 @@ public class WebViewSingleton extends Activity { return instance; } + public static void appMessage(final String message) { + new Handler(Looper.getMainLooper()).post(new Runnable() { + @Override + public void run() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + instance.evaluateJavascript("Pebble.evaluate('appmessage',[{'payload':" + message + "}]);", new ValueCallback() { + @Override + public void onReceiveValue(String s) { + LOG.debug("Callback from showConfiguration", s); + } + }); + } else { + instance.loadUrl("javascript:Pebble.evaluate('appmessage',[{'payload':" + message + "}]);"); + } + } + }); + } public static void disposeWebView() { - if (instance != null) { - instance.destroy(); - instance = null; - } + new Handler(Looper.getMainLooper()).post(new Runnable() { + @Override + public void run() { + if (instance != null) { + instance.destroy(); + instance = null; + contextWrapper = null; + jsInterface = null; + } + } + }); } private static class GBChromeClient extends WebChromeClient {