From 1981998fc730231adc8700bb22432abfc7cb0895 Mon Sep 17 00:00:00 2001 From: Daniel Dakhno Date: Wed, 3 Feb 2021 22:25:11 +0100 Subject: [PATCH] Fossil HR: splitted button config and menu config --- .../fossil_hr/FossilHRWatchAdapter.java | 21 +++++--- .../ButtonConfigurationPutRequest.java | 7 ++- .../commute/CommuteConfigPutRequest.java | 49 +++++++++++++++++++ 3 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/commute/CommuteConfigPutRequest.java 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 ecd1296fb..2730e9760 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 @@ -95,6 +95,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fos import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.authentication.VerifyPrivateKeyRequest; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.buttons.ButtonConfiguration; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.buttons.ButtonConfigurationPutRequest; +import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.commute.CommuteConfigPutRequest; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.configuration.ConfigurationGetRequest; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.configuration.ConfigurationPutRequest; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.file.AssetFilePutRequest; @@ -1147,12 +1148,6 @@ public class FossilHRWatchAdapter extends FossilWatchAdapter { @Override public void overwriteButtons(String jsonConfigString) { try { - JSONArray jsonArray = new JSONArray( - GBApplication.getPrefs().getString(HRConfigActivity.CONFIG_KEY_Q_ACTIONS, "[]") - ); - String[] menuItems = new String[jsonArray.length()]; - for (int i = 0; i < jsonArray.length(); i++) menuItems[i] = jsonArray.getString(i); - SharedPreferences prefs = getDeviceSpecificPreferences(); String singlePressEvent = "short_press_release"; @@ -1187,12 +1182,22 @@ public class FossilHRWatchAdapter extends FossilWatchAdapter { } } - queueWrite(new ButtonConfigurationPutRequest( - menuItems, availableConfigs.toArray(new ButtonConfiguration[0]), this )); + + for(ApplicationInformation info : installedApplications){ + if(info.getAppName().equals("commuteApp")){ + JSONArray jsonArray = new JSONArray( + GBApplication.getPrefs().getString(HRConfigActivity.CONFIG_KEY_Q_ACTIONS, "[]") + ); + String[] menuItems = new String[jsonArray.length()]; + for (int i = 0; i < jsonArray.length(); i++) menuItems[i] = jsonArray.getString(i); + queueWrite(new CommuteConfigPutRequest(menuItems, this)); + break; + } + } } catch (JSONException e) { e.printStackTrace(); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/buttons/ButtonConfigurationPutRequest.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/buttons/ButtonConfigurationPutRequest.java index 65ef892a8..650ace9f9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/buttons/ButtonConfigurationPutRequest.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/buttons/ButtonConfigurationPutRequest.java @@ -28,11 +28,11 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fos import nodomain.freeyourgadget.gadgetbridge.util.GB; public class ButtonConfigurationPutRequest extends JsonPutRequest { - public ButtonConfigurationPutRequest(String[] menuItems, ButtonConfiguration[] buttonConfigurations, FossilHRWatchAdapter adapter) { - super(createObject(menuItems, buttonConfigurations), adapter); + public ButtonConfigurationPutRequest(ButtonConfiguration[] buttonConfigurations, FossilHRWatchAdapter adapter) { + super(createObject(buttonConfigurations), adapter); } - private static JSONObject createObject(String[] menuItems, ButtonConfiguration[] buttonConfigurations) { + private static JSONObject createObject(ButtonConfiguration[] buttonConfigurations) { try { JSONArray configuration = new JSONArray(); for(ButtonConfiguration buttonConfiguration : buttonConfigurations){ @@ -41,7 +41,6 @@ public class ButtonConfigurationPutRequest extends JsonPutRequest { return new JSONObject() .put("push", new JSONObject() .put("set", new JSONObject() - .put("commuteApp._.config.destinations", new JSONArray(menuItems)) .put("master._.config.buttons", configuration) ) ); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/commute/CommuteConfigPutRequest.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/commute/CommuteConfigPutRequest.java new file mode 100644 index 000000000..65d349184 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/commute/CommuteConfigPutRequest.java @@ -0,0 +1,49 @@ +/* Copyright (C) 2019-2021 Andreas Shimokawa, Daniel Dakhno + + This file is part of Gadgetbridge. + + Gadgetbridge is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Gadgetbridge is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . */ +package nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.commute; + +import android.widget.Toast; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fossil_hr.FossilHRWatchAdapter; +import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.buttons.ButtonConfiguration; +import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.json.JsonPutRequest; +import nodomain.freeyourgadget.gadgetbridge.util.GB; + +public class CommuteConfigPutRequest extends JsonPutRequest { + public CommuteConfigPutRequest(String[] menuItems, FossilHRWatchAdapter adapter) { + super(createObject(menuItems), adapter); + } + + private static JSONObject createObject(String[] menuItems) { + try { + return new JSONObject() + .put("push", new JSONObject() + .put("set", new JSONObject() + .put("commuteApp._.config.destinations", new JSONArray(menuItems)) + ) + ); + } catch (JSONException e) { + GB.toast("error creating json", Toast.LENGTH_LONG, GB.ERROR, e); + } + + return null; + } +}