From bc60b66abf3b84a449c27844531f1df7961f7e7c Mon Sep 17 00:00:00 2001 From: Daniel Dakhno Date: Tue, 9 Nov 2021 22:48:30 +0100 Subject: [PATCH] added capability to push config via intents --- .../devices/qhybrid/QHybridSupport.java | 21 ++++++- .../devices/qhybrid/adapter/WatchAdapter.java | 4 ++ .../fossil_hr/FossilHRWatchAdapter.java | 58 +++++++++---------- .../fossil_hr/json/JsonPutRequest.java | 6 +- 4 files changed, 54 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/QHybridSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/QHybridSupport.java index 1fb03d3cc..24fac2be6 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/QHybridSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/QHybridSupport.java @@ -97,6 +97,7 @@ public class QHybridSupport extends QHybridBaseSupport { public static final String QHYBRID_COMMAND_SET_WIDGET_CONTENT = "nodomain.freeyourgadget.gadgetbridge.Q_SET_WIDGET_CONTENT"; public static final String QHYBRID_COMMAND_SET_BACKGROUND_IMAGE = "nodomain.freeyourgadget.gadgetbridge.Q_SET_BACKGROUND_IMAGE"; public static final String QHYBRID_COMMAND_UNINSTALL_APP = "nodomain.freeyourgadget.gadgetbridge.Q_UNINSTALL_APP"; + public static final String QHYBRID_COMMAND_PUSH_CONFIG = "nodomain.freeyourgadget.gadgetbridge.Q_PUSH_CONFIG"; public static final String QHYBRID_COMMAND_DOWNLOAD_FILE = "nodomain.freeyourgadget.gadgetbridge.Q_DOWNLOAD_FILE"; public static final String QHYBRID_COMMAND_UPLOAD_FILE = "nodomain.freeyourgadget.gadgetbridge.Q_UPLOAD_FILE"; @@ -304,6 +305,7 @@ public class QHybridSupport extends QHybridBaseSupport { globalFilter.addAction(QHYBRID_COMMAND_SET_MENU_MESSAGE); globalFilter.addAction(QHYBRID_COMMAND_SET_WIDGET_CONTENT); globalFilter.addAction(QHYBRID_COMMAND_UPLOAD_FILE); + globalFilter.addAction(QHYBRID_COMMAND_PUSH_CONFIG); globalCommandReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { @@ -367,9 +369,13 @@ public class QHybridSupport extends QHybridBaseSupport { break; } case QHYBRID_COMMAND_UPLOAD_FILE:{ - if (GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress()).getBoolean(DeviceSettingsPreferenceConst.PREF_HYBRID_HR_DANGEROUS_EXTERNAL_INTENTS, true)) { - handleFileUploadIntent(intent); - } + if(!externalAccessAllowed()) break; + handleFileUploadIntent(intent); + break; + } + case QHYBRID_COMMAND_PUSH_CONFIG:{ + if(!externalAccessAllowed()) break; + handleConfigSetIntent(intent); break; } } @@ -378,6 +384,15 @@ public class QHybridSupport extends QHybridBaseSupport { GBApplication.getContext().registerReceiver(globalCommandReceiver, globalFilter); } + private void handleConfigSetIntent(Intent intent) { + String configJson = intent.getExtras().getString("EXTRA_CONFIG_JSON", "{}"); + watchAdapter.pushConfigJson(configJson); + } + + private boolean externalAccessAllowed(){ + return GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress()).getBoolean(DeviceSettingsPreferenceConst.PREF_HYBRID_HR_DANGEROUS_EXTERNAL_INTENTS, true); + } + private void handleFileUploadIntent(Intent intent){ boolean generateHeader = intent.getBooleanExtra("EXTRA_GENERATE_FILE_HEADER", false); String filePath = intent.getStringExtra("EXTRA_PATH"); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/adapter/WatchAdapter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/adapter/WatchAdapter.java index 5d062a635..143bf258e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/adapter/WatchAdapter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/adapter/WatchAdapter.java @@ -156,4 +156,8 @@ public abstract class WatchAdapter { public void uninstallApp(String appName) { } + + public void pushConfigJson(String configJson){ + + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/adapter/fossil_hr/FossilHRWatchAdapter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/adapter/fossil_hr/FossilHRWatchAdapter.java index c9dd2464d..993b4b893 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/adapter/fossil_hr/FossilHRWatchAdapter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/adapter/fossil_hr/FossilHRWatchAdapter.java @@ -1224,39 +1224,29 @@ public class FossilHRWatchAdapter extends FossilWatchAdapter { @Override public void onTestNewFunction() { - /*queueWrite(new ButtonConfigurationPutRequest( - new String[]{"test"}, - new ButtonConfiguration[]{ - new ButtonConfiguration( - "short_press_release", "stopwatchApp" - ) - }, - this - ));*/ - /*queueWrite(new FileLookupAndGetRequest(FileHandle.APP_CODE, this) { - @Override - public void handleFileData(byte[] fileData) { - log("test"); - } - @Override - public void handleFileLookupError(FILE_LOOKUP_ERROR error) { + try{ + JSONObject data = new JSONObject() + .put("push", new JSONObject() + .put("set", new JSONObject() + .put("widgetCustom0._.config.upper_text", "0 up") + .put("widgetCustom0._.config.lower_text", "0 low") - } - });*/ - /*queueWrite(new TranslationsGetRequest(this){ - @Override - public void handleTranslations(TranslationData translationData) { - translationData.replaceByOriginal("ON", "oi m8"); - translationData.replaceByOriginal("OFF", "nah go away"); - translationData.replaceByOriginal("Device is about to reset.", "oh frick no no no"); - translationData.replaceByOriginal("Release button to stop reset.", "please don't let me die like that"); - translationData.replaceByOriginal("Serial number", "Gadgetbridge :)"); - translationData.replaceByOriginal("Dial Info", "Widgets"); - TranslationsPutRequest request = new TranslationsPutRequest(translationData, FossilHRWatchAdapter.this); - queueWrite(request); - } - });*/ + .put("widgetCustom1._.config.upper_text", "1 up") + .put("widgetCustom1._.config.lower_text", "1 low") + + .put("widgetCustom2._.config.upper_text", "2 up") + .put("widgetCustom2._.config.lower_text", "2 low") + + .put("widgetCustom3._.config.upper_text", "3 up") + .put("widgetCustom3._.config.lower_text", "3 low") + + ) + ); + queueWrite(new JsonPutRequest(data, this)); + }catch (Exception e) { + e.printStackTrace(); + } } public byte[] getSecretKey() throws IllegalAccessException { @@ -1279,6 +1269,12 @@ public class FossilHRWatchAdapter extends FossilWatchAdapter { return authKeyBytes; } + @Override + public void pushConfigJson(String configJson){ + configJson = configJson.replace("\n", ""); + queueWrite(new JsonPutRequest(configJson, this)); + } + public void setPhoneRandomNumber(byte[] phoneRandomNumber) { this.phoneRandomNumber = phoneRandomNumber; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/json/JsonPutRequest.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/json/JsonPutRequest.java index 1a1338a04..ca24320df 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/json/JsonPutRequest.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/json/JsonPutRequest.java @@ -23,6 +23,10 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fos public class JsonPutRequest extends FilePutRawRequest { public JsonPutRequest(JSONObject object, FossilHRWatchAdapter adapter) { - super((short)(0x0500 | (adapter.getJsonIndex() & 0xFF)), object.toString().getBytes(), adapter); + this(object.toString(), adapter); + } + + public JsonPutRequest(String json, FossilHRWatchAdapter adapter) { + super((short)(0x0500 | (adapter.getJsonIndex() & 0xFF)), json.getBytes(), adapter); } }