Search in sources :

Example 1 with CustomTextWidgetElement

use of nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomTextWidgetElement in project Gadgetbridge by Freeyourgadget.

the class HRConfigActivity method loadCustomWidgetList.

private void loadCustomWidgetList() {
    String customWidgetJson = sharedPreferences.getString("QHYBRID_CUSTOM_WIDGETS", "[]");
    try {
        JSONArray customWidgets = new JSONArray(customWidgetJson);
        this.customWidgets.clear();
        for (int i = 0; i < customWidgets.length(); i++) {
            JSONObject customWidgetObject = customWidgets.getJSONObject(i);
            CustomWidget widget = new CustomWidget(// FIXME: handle force white background
            customWidgetObject.getString("name"), // FIXME: handle force white background
            0, // FIXME: handle force white background
            0, // FIXME: handle force white background
            "default");
            JSONArray elements = customWidgetObject.getJSONArray("elements");
            for (int i2 = 0; i2 < elements.length(); i2++) {
                JSONObject element = elements.getJSONObject(i2);
                if (element.getString("type").equals("text")) {
                    widget.addElement(new CustomTextWidgetElement(element.getString("id"), element.getString("value"), element.getInt("x"), element.getInt("y")));
                } else if (element.getString("type").equals("background")) {
                    widget.addElement(new CustomBackgroundWidgetElement(element.getString("id"), element.getString("value")));
                }
            }
            this.customWidgets.add(widget);
        }
        refreshWidgetList();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
Also used : CustomBackgroundWidgetElement(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomBackgroundWidgetElement) JSONObject(org.json.JSONObject) CustomTextWidgetElement(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomTextWidgetElement) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) CustomWidget(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomWidget)

Example 2 with CustomTextWidgetElement

use of nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomTextWidgetElement in project Gadgetbridge by Freeyourgadget.

the class FossilHRWatchAdapter method loadWidgets.

private void loadWidgets() {
    Version firmwareVersion = getCleanFWVersion();
    if (firmwareVersion != null && firmwareVersion.compareTo(new Version("1.0.2.20")) >= 0) {
        // this does not work on newer firmware versions
        return;
    }
    Prefs prefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(getDeviceSupport().getDevice().getAddress()));
    boolean forceWhiteBackground = prefs.getBoolean("force_white_color_scheme", false);
    String fontColor = forceWhiteBackground ? "black" : "default";
    Widget[] oldWidgets = widgets.toArray(new Widget[0]);
    widgets.clear();
    String widgetJson = GBApplication.getPrefs().getPreferences().getString("FOSSIL_HR_WIDGETS", "{}");
    String customWidgetJson = GBApplication.getPrefs().getString("QHYBRID_CUSTOM_WIDGETS", "[]");
    try {
        JSONObject widgetConfig = new JSONObject(widgetJson);
        JSONArray customWidgets = new JSONArray(customWidgetJson);
        Iterator<String> keyIterator = widgetConfig.keys();
        HashMap<String, Integer> positionMap = new HashMap<>(4);
        positionMap.put("top", 0);
        positionMap.put("right", 90);
        positionMap.put("bottom", 180);
        positionMap.put("left", 270);
        while (keyIterator.hasNext()) {
            String position = keyIterator.next();
            String identifier = widgetConfig.getString(position);
            Widget.WidgetType type = Widget.WidgetType.fromJsonIdentifier(identifier);
            Widget widget = null;
            if (type != null) {
                widget = new Widget(type, positionMap.get(position), 63, fontColor);
            } else {
                identifier = identifier.substring(7);
                for (int i = 0; i < customWidgets.length(); i++) {
                    JSONObject customWidget = customWidgets.getJSONObject(i);
                    if (customWidget.getString("name").equals(identifier)) {
                        boolean drawCircle = false;
                        if (customWidget.has("drawCircle"))
                            drawCircle = customWidget.getBoolean("drawCircle");
                        CustomWidget newWidget = new CustomWidget(customWidget.getString("name"), positionMap.get(position), 63, fontColor);
                        JSONArray elements = customWidget.getJSONArray("elements");
                        for (int i2 = 0; i2 < elements.length(); i2++) {
                            JSONObject element = elements.getJSONObject(i2);
                            if (element.getString("type").equals("text")) {
                                newWidget.addElement(new CustomTextWidgetElement(element.getString("id"), element.getString("value"), element.getInt("x"), element.getInt("y")));
                            } else if (element.getString("type").equals("background")) {
                                newWidget.addElement(new CustomBackgroundWidgetElement(element.getString("id"), element.getString("value")));
                            }
                        }
                        widget = newWidget;
                    }
                }
            }
            if (widget == null)
                continue;
            widgets.add(widget);
        }
    } catch (JSONException e) {
        LOG.error("Error while updating widgets", e);
    }
    for (Widget oldWidget : oldWidgets) {
        if (!(oldWidget instanceof CustomWidget))
            continue;
        CustomWidget customOldWidget = (CustomWidget) oldWidget;
        for (CustomWidgetElement oldElement : customOldWidget.getElements()) {
            for (Widget newWidget : widgets) {
                if (newWidget instanceof CustomWidget) {
                    ((CustomWidget) newWidget).updateElementValue(oldElement.getId(), oldElement.getValue());
                }
            }
        }
    }
    uploadWidgets();
}
Also used : CustomWidgetElement(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomWidgetElement) CustomBackgroundWidgetElement(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomBackgroundWidgetElement) HashMap(java.util.HashMap) CustomWidget(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomWidget) Widget(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.Widget) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Prefs(nodomain.freeyourgadget.gadgetbridge.util.Prefs) Paint(android.graphics.Paint) CustomWidget(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomWidget) JSONObject(org.json.JSONObject) Version(nodomain.freeyourgadget.gadgetbridge.util.Version) CustomTextWidgetElement(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomTextWidgetElement)

Example 3 with CustomTextWidgetElement

use of nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomTextWidgetElement in project Gadgetbridge by Freeyourgadget.

the class WidgetSettingsActivity method showElementDialog.

private void showElementDialog(@Nullable final CustomWidgetElement element) {
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(WidgetSettingsActivity.this).setView(R.layout.qhybrid_element_popup_view);
    if (element == null) {
        dialogBuilder.setTitle("create element").setNegativeButton("cancel", null).setPositiveButton("ok", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (((RadioButton) ((AlertDialog) dialog).findViewById(R.id.qhybrid_widget_elements_type_text)).isChecked()) {
                    subject.addElement(new CustomTextWidgetElement(((EditText) ((AlertDialog) dialog).findViewById(R.id.qhybrid_widget_element_id)).getText().toString(), ((EditText) ((AlertDialog) dialog).findViewById(R.id.qhybrid_widget_element_value)).getText().toString(), CustomWidgetElement.X_CENTER, ((RadioButton) ((AlertDialog) dialog).findViewById(R.id.qhybrid_widget_elements_position_uppper)).isChecked() ? CustomTextWidgetElement.Y_UPPER_HALF : CustomTextWidgetElement.Y_LOWER_HALF));
                } else {
                    subject.addElement(new CustomBackgroundWidgetElement(((EditText) ((AlertDialog) dialog).findViewById(R.id.qhybrid_widget_element_id)).getText().toString(), ((EditText) ((AlertDialog) dialog).findViewById(R.id.qhybrid_widget_element_value)).getText().toString()));
                }
                refreshElementsList();
            }
        });
    } else {
        dialogBuilder.setTitle("edit element").setNegativeButton("delete", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                WidgetSettingsActivity.this.subject.getElements().remove(element);
                refreshElementsList();
            }
        }).setPositiveButton("ok", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                element.setId(((EditText) ((AlertDialog) dialog).findViewById(R.id.qhybrid_widget_element_id)).getText().toString());
                element.setValue(((EditText) ((AlertDialog) dialog).findViewById(R.id.qhybrid_widget_element_value)).getText().toString());
                element.setY(((RadioButton) ((AlertDialog) dialog).findViewById(R.id.qhybrid_widget_elements_position_uppper)).isChecked() ? CustomTextWidgetElement.Y_UPPER_HALF : CustomTextWidgetElement.Y_LOWER_HALF);
                element.setWidgetElementType(CustomWidgetElement.WidgetElementType.fromRadioButtonRessource(((RadioGroup) ((AlertDialog) dialog).findViewById(R.id.qhybrid_widget_element_type)).getCheckedRadioButtonId()));
                refreshElementsList();
            }
        });
    }
    AlertDialog dialog = dialogBuilder.show();
    if (element != null) {
        String elementId = element.getId();
        String elementValue = element.getValue();
        CustomWidgetElement.WidgetElementType type = element.getWidgetElementType();
        ((EditText) dialog.findViewById(R.id.qhybrid_widget_element_id)).setText(elementId);
        ((EditText) dialog.findViewById(R.id.qhybrid_widget_element_value)).setText(elementValue);
        ((RadioGroup) dialog.findViewById(R.id.qhybrid_widget_element_type)).check(type.getRadioButtonResource());
        ((RadioGroup) dialog.findViewById(R.id.qhybrid_widget_element_position)).check(element.getY() == CustomWidgetElement.Y_UPPER_HALF ? R.id.qhybrid_widget_elements_position_uppper : R.id.qhybrid_widget_elements_position_lower);
    }
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) EditText(android.widget.EditText) CustomWidgetElement(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomWidgetElement) CustomBackgroundWidgetElement(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomBackgroundWidgetElement) RadioGroup(android.widget.RadioGroup) DialogInterface(android.content.DialogInterface) RadioButton(android.widget.RadioButton) CustomTextWidgetElement(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomTextWidgetElement)

Aggregations

CustomBackgroundWidgetElement (nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomBackgroundWidgetElement)3 CustomTextWidgetElement (nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomTextWidgetElement)3 CustomWidget (nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomWidget)2 CustomWidgetElement (nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomWidgetElement)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 DialogInterface (android.content.DialogInterface)1 Paint (android.graphics.Paint)1 EditText (android.widget.EditText)1 RadioButton (android.widget.RadioButton)1 RadioGroup (android.widget.RadioGroup)1 AlertDialog (androidx.appcompat.app.AlertDialog)1 HashMap (java.util.HashMap)1 Widget (nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.Widget)1 Prefs (nodomain.freeyourgadget.gadgetbridge.util.Prefs)1 Version (nodomain.freeyourgadget.gadgetbridge.util.Version)1