diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/banglejs/AppsManagementActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/banglejs/AppsManagementActivity.java index 0b4cbd00b..9b4711147 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/banglejs/AppsManagementActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/banglejs/AppsManagementActivity.java @@ -9,6 +9,7 @@ import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import android.webkit.JavascriptInterface; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.AdapterView; @@ -23,6 +24,9 @@ import androidx.annotation.Nullable; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import org.json.JSONArray; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; @@ -30,11 +34,13 @@ import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBActivity; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; +import nodomain.freeyourgadget.gadgetbridge.service.devices.banglejs.BangleJSDeviceSupport; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.QHybridSupport; import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; import nodomain.freeyourgadget.gadgetbridge.util.GB; public class AppsManagementActivity extends AbstractGBActivity { + private static final Logger LOG = LoggerFactory.getLogger(AppsManagementActivity.class); private WebView webView; private GBDevice mGBDevice; private DeviceCoordinator mCoordinator; @@ -80,12 +86,49 @@ public class AppsManagementActivity extends AbstractGBActivity { } }; + + public class WebViewInterface { + Context mContext; + + WebViewInterface(Context c) { + mContext = c; + } + + @JavascriptInterface + public void bangleTx(String data) { + LOG.info("WebView RX: " + data); + bangleRx("Hello world"); + } + + } + + // Called when data received from Bangle.js + public void bangleRx(String data) { + JSONArray s = new JSONArray(); + s.put(data); + String ss = s.toString(); + final String js = "bangleRx("+ss.substring(1, ss.length()-1)+");"; + LOG.info("WebView TX cmd: " + js); + webView.post(new Runnable() { + @Override + public void run() { + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { + webView.evaluateJavascript(js, null); + } + else { + webView.loadUrl("javascript: "+js); + } + } + }); + } + private void initViews() { //https://stackoverflow.com/questions/4325639/android-calling-javascript-functions-in-webview webView = findViewById(R.id.webview); webView.setWebViewClient(new WebViewClient()); webView.getSettings().setJavaScriptEnabled(true); - webView.loadUrl("https://banglejs.com/apps/"); + webView.addJavascriptInterface(new WebViewInterface(this), "Android"); + webView.loadUrl("https://www.pur3.co.uk/tmp/android.html"); webView.setWebViewClient(new WebViewClient(){ public void onPageFinished(WebView view, String weburl){