From ec3b54ef47051bf94b9059459ed905cb94d93a9c Mon Sep 17 00:00:00 2001 From: Gordon Williams Date: Wed, 18 May 2022 16:31:22 +0100 Subject: [PATCH] Bangle.js: Add option for enabling/disabling internet access, and add ability to send Intents direct from Bangle.js # Conflicts: # app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSDeviceSupport.java --- .../DeviceSettingsPreferenceConst.java | 3 + .../devices/banglejs/BangleJSCoordinator.java | 15 +- .../banglejs/BangleJSDeviceSupport.java | 133 +++++++++++++----- app/src/main/res/values/strings.xml | 4 + .../res/xml/devicesettings_device_intents.xml | 9 ++ .../devicesettings_device_internet_access.xml | 9 ++ 6 files changed, 138 insertions(+), 35 deletions(-) create mode 100644 app/src/main/res/xml/devicesettings_device_intents.xml create mode 100644 app/src/main/res/xml/devicesettings_device_internet_access.xml diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java index a083c1724..c50de8312 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java @@ -41,6 +41,9 @@ public class DeviceSettingsPreferenceConst { public static final String PREF_VIBRATION_STRENGH_PERCENTAGE = "vibration_strength"; public static final String PREF_RELAX_FIRMWARE_CHECKS = "relax_firmware_checks"; + public static final String PREF_DEVICE_INTERNET_ACCESS = "device_internet_access"; + public static final String PREF_DEVICE_INTENTS = "device_intents"; + public static final String PREF_DISCONNECT_NOTIFICATION = "disconnect_notification"; public static final String PREF_DISCONNECT_NOTIFICATION_START = "disconnect_notification_start"; public static final String PREF_DISCONNECT_NOTIFICATION_END = "disconnect_notification_end"; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/banglejs/BangleJSCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/banglejs/BangleJSCoordinator.java index ad052bf1e..50675ba29 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/banglejs/BangleJSCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/banglejs/BangleJSCoordinator.java @@ -29,7 +29,9 @@ import androidx.annotation.NonNull; import java.util.Collection; import java.util.Collections; +import java.util.Vector; +import nodomain.freeyourgadget.gadgetbridge.BuildConfig; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.devices.AbstractDeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler; @@ -168,9 +170,16 @@ public class BangleJSCoordinator extends AbstractDeviceCoordinator { } public int[] getSupportedDeviceSpecificSettings(GBDevice device) { - return new int[]{ - R.xml.devicesettings_transliteration - }; + Vector settings = new Vector(); + settings.add(R.xml.devicesettings_transliteration); + settings.add(R.xml.devicesettings_high_mtu); + if (BuildConfig.INTERNET_ACCESS) + settings.add(R.xml.devicesettings_device_internet_access); + settings.add(R.xml.devicesettings_device_intents); + // must be a better way of doing this? + int[] settingsInt = new int[settings.size()]; + for (int i=0; i() { - @Override - public void onResponse(String response) { - JSONObject o = new JSONObject(); - try { - o.put("t", "http"); - o.put("resp", response); - } catch (JSONException e) { - GB.toast(getContext(), "HTTP: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR); - } - uartTxJSON("http", o); - } - }, new Response.ErrorListener() { - @Override - public void onErrorResponse(VolleyError error) { - JSONObject o = new JSONObject(); - try { - o.put("t", "http"); - o.put("err", error.toString()); - } catch (JSONException e) { - GB.toast(getContext(), "HTTP: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR); - } - uartTxJSON("http", o); + Prefs devicePrefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress())); + if (BuildConfig.INTERNET_ACCESS && devicePrefs.getBoolean(PREF_DEVICE_INTERNET_ACCESS, false)) { + RequestQueue queue = Volley.newRequestQueue(getContext()); + String url = json.getString("url"); + String _xmlPath = ""; + try { + _xmlPath = json.getString("xpath"); + } catch (JSONException e) { } - }); - queue.add(stringRequest); + final String xmlPath = _xmlPath; + // Request a string response from the provided URL. + StringRequest stringRequest = new StringRequest(Request.Method.GET, url, + new Response.Listener() { + @Override + public void onResponse(String response) { + JSONObject o = new JSONObject(); + if (xmlPath.length() != 0) { + try { + InputSource inputXML = new InputSource(new StringReader(response)); + XPath xPath = XPathFactory.newInstance().newXPath(); + response = xPath.evaluate(xmlPath, inputXML); + } catch (Exception error) { + uartTxJSONError("http", error.toString()); + return; + } + } + try { + o.put("t", "http"); + o.put("resp", response); + } catch (JSONException e) { + GB.toast(getContext(), "HTTP: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR); + } + uartTxJSON("http", o); + } + }, new Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError error) { + JSONObject o = new JSONObject(); + uartTxJSONError("http", error.toString()); + } + }); + queue.add(stringRequest); + } else { + if (BuildConfig.INTERNET_ACCESS) + uartTxJSONError("http", "Internet access not enabled, check Gadgetbridge Device Settings"); + else + uartTxJSONError("http", "Internet access not enabled in this Gadgetbridge build"); + } } break; + case "intent": { + Prefs devicePrefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress())); + if (devicePrefs.getBoolean(PREF_DEVICE_INTENTS, false)) { + String action = json.getString("action"); + JSONObject extra = json.getJSONObject("extra"); + Intent in = new Intent(); + in.setAction(action); + if (extra != null) { + Iterator iter = extra.keys(); + while (iter.hasNext()) { + String key = iter.next(); + in.putExtra(key, extra.getString(key)); + } + } + LOG.info("Sending intent " + action); + this.getContext().getApplicationContext().sendBroadcast(in); + } else { + uartTxJSONError("intent", "Android Intents not enabled, check Gadgetbridge Device Settings"); + } + } + default : { + LOG.info("UART RX JSON packet type '"+packetType+"' not understood."); + } } } @@ -322,7 +391,7 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport { if (BangleJSConstants.UUID_CHARACTERISTIC_NORDIC_UART_RX.equals(characteristic.getUuid())) { byte[] chars = characteristic.getValue(); // check to see if we get more data - if so, increase out MTU for sending - if (chars.length > mtuSize) + if (allowHighMTU && chars.length > mtuSize) mtuSize = chars.length; String packetStr = new String(chars); LOG.info("RX: " + packetStr); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 895419d9c..b58e6c1e2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -264,6 +264,10 @@ Enable this if your device has a custom font firmware for emoji support Allow high MTU Increases transfer speed, but might not work on some Android devices. + Allow Internet Access + Allow apps on this device to access the internet + Allow Intents + Allow apps on this device to send Android Intents Enables calendar alerts, even when disconnected Sync calendar events Relax firmware checks diff --git a/app/src/main/res/xml/devicesettings_device_intents.xml b/app/src/main/res/xml/devicesettings_device_intents.xml new file mode 100644 index 000000000..d30169584 --- /dev/null +++ b/app/src/main/res/xml/devicesettings_device_intents.xml @@ -0,0 +1,9 @@ + + + + diff --git a/app/src/main/res/xml/devicesettings_device_internet_access.xml b/app/src/main/res/xml/devicesettings_device_internet_access.xml new file mode 100644 index 000000000..a84b985b8 --- /dev/null +++ b/app/src/main/res/xml/devicesettings_device_internet_access.xml @@ -0,0 +1,9 @@ + + + +