1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-10-19 01:39:39 +02:00

Pebble: webview. Use the main thread explicitly again and log XHR requests at least on Lollipop and up

This commit is contained in:
Daniele Gobbetti 2017-01-02 12:07:57 +01:00
parent 4ef0415da2
commit 7ab74fb11b

View File

@ -12,6 +12,8 @@ import android.webkit.ConsoleMessage;
import android.webkit.JavascriptInterface; import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback; import android.webkit.ValueCallback;
import android.webkit.WebChromeClient; import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings; import android.webkit.WebSettings;
import android.webkit.WebView; import android.webkit.WebView;
import android.webkit.WebViewClient; import android.webkit.WebViewClient;
@ -55,35 +57,47 @@ public class WebViewSingleton extends Activity {
} else { } else {
contextWrapper = new MutableContextWrapper(context); contextWrapper = new MutableContextWrapper(context);
} }
}
//here we are sure that contextWrapper is either null or an activity, hence we run on the main thread
if (contextWrapper != null) {
if (instance == null) { if (instance == null) {
instance = new WebView(contextWrapper); instance = new WebView(contextWrapper);
instance.setWillNotDraw(true); new Handler(Looper.getMainLooper()).post(new Runnable() {
instance.clearCache(true); @Override
instance.setWebViewClient(new GBWebClient()); public void run() {
instance.setWebChromeClient(new GBChromeClient()); instance.setWillNotDraw(true);
instance.setWebContentsDebuggingEnabled(true); instance.clearCache(true);
WebSettings webSettings = instance.getSettings(); instance.setWebViewClient(new GBWebClient());
webSettings.setJavaScriptEnabled(true); instance.setWebChromeClient(new GBChromeClient());
//needed to access the DOM instance.setWebContentsDebuggingEnabled(true);
webSettings.setDomStorageEnabled(true); WebSettings webSettings = instance.getSettings();
//needed for localstorage webSettings.setJavaScriptEnabled(true);
webSettings.setDatabaseEnabled(true); //needed to access the DOM
webSettings.setDomStorageEnabled(true);
//needed for localstorage
webSettings.setDatabaseEnabled(true);
}
});
} }
if (jsInterface == null || (jsInterface != null && (!device.equals(jsInterface.device) || !uuid.equals(jsInterface.mUuid)))) {
instance.removeJavascriptInterface("GBjs");
jsInterface = new JSInterface(device, uuid);
instance.addJavascriptInterface(jsInterface, "GBjs");
instance.loadUrl("file:///android_asset/app_config/configure.html");
} else {
LOG.debug("Not reloading the webview " + jsInterface.mUuid.toString());
}
} else { } else {
LOG.debug("WEBV: not using the passed context, as it is not an activity"); LOG.debug("WEBV: not using the passed context, as it is not an activity");
} }
if (jsInterface == null || (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");
}
}
});
} else {
LOG.debug("Not reloading the webview " + jsInterface.mUuid.toString());
}
return instance; return instance;
} }
@ -132,6 +146,15 @@ public class WebViewSingleton extends Activity {
} }
private static class GBWebClient extends WebViewClient { private static class GBWebClient extends WebViewClient {
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
LOG.debug("WEBVIEW shouldInterceptRequest URL" + request.getUrl());
}
LOG.debug("WEBVIEW request:" + request.toString());
return super.shouldInterceptRequest(view, request);
}
@Override @Override
public boolean shouldOverrideUrlLoading(WebView view, String url) { public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("http://") || url.startsWith("https://")) { if (url.startsWith("http://") || url.startsWith("https://")) {