Search in sources :

Example 1 with Widget

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

the class FossilHRWatchAdapter method renderWidgets.

private void renderWidgets() {
    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);
    boolean drawCircles = prefs.getBoolean("widget_draw_circles", false);
    Bitmap circleBitmap = null;
    if (drawCircles) {
        circleBitmap = Bitmap.createBitmap(76, 76, Bitmap.Config.ARGB_8888);
        Canvas circleCanvas = new Canvas(circleBitmap);
        Paint circlePaint = new Paint();
        circlePaint.setAntiAlias(true);
        circlePaint.setColor(forceWhiteBackground ? Color.WHITE : Color.BLACK);
        circlePaint.setStyle(Paint.Style.FILL);
        circlePaint.setStrokeWidth(3);
        circleCanvas.drawCircle(38, 38, 35, circlePaint);
        circlePaint.setColor(forceWhiteBackground ? Color.BLACK : Color.WHITE);
        circlePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setStrokeWidth(3);
        circleCanvas.drawCircle(38, 38, 35, circlePaint);
    }
    try {
        ArrayList<AssetImage> widgetImages = new ArrayList<>();
        if (this.backGroundImage != null) {
            widgetImages.add(this.backGroundImage);
        }
        for (int i = 0; i < widgets.size(); i++) {
            Widget w = widgets.get(i);
            if (!(w instanceof CustomWidget)) {
                if (w.getWidgetType() == Widget.WidgetType.LAST_NOTIFICATION) {
                    Bitmap widgetBitmap = Bitmap.createBitmap(76, 76, Bitmap.Config.ARGB_8888);
                    Canvas widgetCanvas = new Canvas(widgetBitmap);
                    if (drawCircles) {
                        widgetCanvas.drawBitmap(circleBitmap, 0, 0, null);
                    }
                    Paint p = new Paint();
                    p.setStyle(Paint.Style.FILL);
                    p.setTextSize(10);
                    p.setColor(Color.WHITE);
                    if (this.lastPostedApp != null) {
                        Bitmap icon = Bitmap.createScaledBitmap(appIconCache.get(this.lastPostedApp), 40, 40, true);
                        if (icon != null) {
                            widgetCanvas.drawBitmap(icon, (float) (38 - (icon.getWidth() / 2.0)), (float) (38 - (icon.getHeight() / 2.0)), null);
                        }
                    }
                    widgetImages.add(AssetImageFactory.createAssetImage(widgetBitmap, true, w.getAngle(), w.getDistance(), 1));
                } else if (drawCircles) {
                    widgetImages.add(AssetImageFactory.createAssetImage(circleBitmap, true, w.getAngle(), w.getDistance(), 1));
                }
                continue;
            }
            CustomWidget widget = (CustomWidget) w;
            Bitmap widgetBitmap = Bitmap.createBitmap(76, 76, Bitmap.Config.ARGB_8888);
            Canvas widgetCanvas = new Canvas(widgetBitmap);
            if (drawCircles) {
                widgetCanvas.drawBitmap(circleBitmap, 0, 0, null);
            }
            for (CustomWidgetElement element : widget.getElements()) {
                if (element.getWidgetElementType() == CustomWidgetElement.WidgetElementType.TYPE_BACKGROUND) {
                    File imageFile = new File(element.getValue());
                    if (!imageFile.exists() || !imageFile.isFile()) {
                        LOG.debug("Image file " + element.getValue() + " not found");
                        continue;
                    }
                    Bitmap imageBitmap = BitmapFactory.decodeFile(element.getValue());
                    if (imageBitmap == null) {
                        LOG.debug("image file " + element.getValue() + " could not be decoded");
                        continue;
                    }
                    Bitmap scaledBitmap = Bitmap.createScaledBitmap(imageBitmap, 76, 76, false);
                    widgetCanvas.drawBitmap(scaledBitmap, 0, 0, null);
                    break;
                }
            }
            for (CustomWidgetElement element : widget.getElements()) {
                if (element.getWidgetElementType() == CustomWidgetElement.WidgetElementType.TYPE_TEXT) {
                    Paint textPaint = new Paint();
                    textPaint.setStrokeWidth(4);
                    textPaint.setTextSize(17f);
                    textPaint.setStyle(Paint.Style.FILL);
                    textPaint.setColor(forceWhiteBackground ? Color.BLACK : Color.WHITE);
                    textPaint.setTextAlign(Paint.Align.CENTER);
                    widgetCanvas.drawText(element.getValue(), element.getX(), element.getY() - (textPaint.descent() + textPaint.ascent()) / 2f, textPaint);
                } else if (element.getWidgetElementType() == CustomWidgetElement.WidgetElementType.TYPE_IMAGE) {
                    Bitmap imageBitmap = BitmapFactory.decodeFile(element.getValue());
                    widgetCanvas.drawBitmap(imageBitmap, element.getX() - imageBitmap.getWidth() / 2f, element.getY() - imageBitmap.getHeight() / 2f, null);
                }
            }
            widgetImages.add(AssetImageFactory.createAssetImage(widgetBitmap, true, widget.getAngle(), widget.getDistance(), 1));
        }
        AssetImage[] images = widgetImages.toArray(new AssetImage[0]);
        ArrayList<AssetImage> pushFiles = new ArrayList<>(4);
        imgloop: for (AssetImage image : images) {
            for (AssetImage pushedImage : pushFiles) {
                // no need to send same file multiple times, filtering by name since name is hash
                if (image.getFileName().equals(pushedImage.getFileName()))
                    continue imgloop;
            }
            pushFiles.add(image);
        }
        if (pushFiles.size() > 0) {
            queueWrite(new AssetFilePutRequest(pushFiles.toArray(new AssetImage[0]), FileHandle.ASSET_BACKGROUND_IMAGES, this));
        }
        // queueWrite(new FileDeleteRequest((short) 0x0503));
        queueWrite(new ImagesSetRequest(images, this));
    } catch (IOException e) {
        LOG.error("Error while rendering widgets", e);
    }
}
Also used : CustomWidgetElement(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomWidgetElement) AssetImage(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.image.AssetImage) Canvas(android.graphics.Canvas) AssetFilePutRequest(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.file.AssetFilePutRequest) ArrayList(java.util.ArrayList) CustomWidget(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomWidget) Widget(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.Widget) Paint(android.graphics.Paint) IOException(java.io.IOException) Prefs(nodomain.freeyourgadget.gadgetbridge.util.Prefs) Paint(android.graphics.Paint) CustomWidget(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomWidget) BitmapUtil.convertDrawableToBitmap(nodomain.freeyourgadget.gadgetbridge.util.BitmapUtil.convertDrawableToBitmap) Bitmap(android.graphics.Bitmap) ImagesSetRequest(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.image.ImagesSetRequest) Version(nodomain.freeyourgadget.gadgetbridge.util.Version) File(java.io.File)

Example 2 with Widget

use of nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.Widget 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 Widget

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

the class FossilHRWatchAdapter method uploadWidgets.

private void uploadWidgets() {
    ArrayList<Widget> systemWidgets = new ArrayList<>(widgets.size());
    for (Widget widget : widgets) {
        if (!(widget instanceof CustomWidget) && !widget.getWidgetType().isCustom())
            systemWidgets.add(widget);
    }
    queueWrite(new WidgetsPutRequest(systemWidgets.toArray(new Widget[0]), this));
}
Also used : CustomWidget(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomWidget) Widget(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.Widget) ArrayList(java.util.ArrayList) WidgetsPutRequest(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.WidgetsPutRequest) CustomWidget(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomWidget)

Example 4 with Widget

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

the class HRConfigActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_qhybrid_hr_settings);
    sharedPreferences = GBApplication.getPrefs().getPreferences();
    initMappings();
    loadWidgetConfigs();
    final ListView widgetListView = findViewById(R.id.qhybrid_widget_list);
    widgetListAdapter = new WidgetListAdapter(customWidgets);
    widgetListView.setAdapter(widgetListAdapter);
    widgetListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Widget widget = widgetListAdapter.getItem(position);
            Intent startIntent = new Intent(HRConfigActivity.this, WidgetSettingsActivity.class);
            startIntent.putExtra("EXTRA_WIDGET", widget);
            startIntent.putExtra("EXTRA_WIDGET_IDNEX", position);
            startIntent.putExtra("EXTRA_WIDGET_INITIAL_NAME", ((CustomWidget) widget).getName());
            startActivityForResult(startIntent, REQUEST_CODE_WIDGET_EDIT);
        }
    });
    loadCustomWidgetList();
    findViewById(R.id.qhybrid_widget_add).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent startIntent = new Intent(HRConfigActivity.this, WidgetSettingsActivity.class);
            startActivityForResult(startIntent, REQUEST_CODE_WIDGET_EDIT);
        }
    });
    findViewById(R.id.qhybrid_set_background).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            new AlertDialog.Builder(HRConfigActivity.this).setTitle("whoop whoop").setMessage("background has to be pushed every time a custom widget changes, causing traffic and battery drain. Consider that when using custom widgets.").setPositiveButton("ok", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Intent pickIntent = new Intent(Intent.ACTION_PICK);
                    pickIntent.setType("image/*");
                    startActivityForResult(pickIntent, REQUEST_CODE_IMAGE_PICK);
                }
            }).setNegativeButton("nah", null).show();
        }
    });
    findViewById(R.id.qhybrid_unset_background).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent intent = new Intent(QHybridSupport.QHYBRID_COMMAND_SET_BACKGROUND_IMAGE);
            intent.putIntegerArrayListExtra("EXTRA_PIXELS", null);
            LocalBroadcastManager.getInstance(HRConfigActivity.this).sendBroadcast(intent);
        }
    });
    for (int i = 0; i < widgetButtonsMapping.size(); i++) {
        final int widgetButtonId = widgetButtonsMapping.keyAt(i);
        findViewById(widgetButtonId).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Widget.WidgetType[] types = Widget.WidgetType.values();
                final ArrayList<String> names = new ArrayList<>(types.length);
                for (Widget.WidgetType type : types) {
                    names.add(getResources().getString(type.getStringResource()));
                }
                for (CustomWidget customWidget : customWidgets) {
                    names.add(customWidget.getName());
                }
                final String[] nameStrings = names.toArray(new String[0]);
                new AlertDialog.Builder(HRConfigActivity.this).setItems(nameStrings, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        saveWidgetSetting(widgetButtonId, which, nameStrings);
                    }
                }).show();
            }
        });
    }
    // Disable some functions on watches with too new firmware (from official app 4.6.0 and higher)
    String fwVersion_str = GBApplication.app().getDeviceManager().getSelectedDevice().getFirmwareVersion();
    fwVersion_str = fwVersion_str.replaceFirst("^DN", "").replaceFirst("r\\.v.*", "");
    Version fwVersion = new Version(fwVersion_str);
    if (fwVersion.compareTo(new Version("1.0.2.20")) >= 0) {
        findViewById(R.id.qhybrid_widget_add).setEnabled(false);
        for (int i = 0; i < widgetButtonsMapping.size(); i++) {
            final int widgetButtonId = widgetButtonsMapping.keyAt(i);
            findViewById(widgetButtonId).setEnabled(false);
        }
        findViewById(R.id.qhybrid_set_background).setEnabled(false);
        findViewById(R.id.qhybrid_unset_background).setEnabled(false);
        GB.toast(getString(R.string.fossil_hr_warning_firmware_too_new), Toast.LENGTH_LONG, GB.INFO);
    }
}
Also used : AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) Widget(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.Widget) CustomWidget(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomWidget) ArrayList(java.util.ArrayList) ListView(android.widget.ListView) Version(nodomain.freeyourgadget.gadgetbridge.util.Version) Intent(android.content.Intent) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) CustomWidget(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomWidget) AdapterView(android.widget.AdapterView)

Example 5 with Widget

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

the class HRConfigActivity method loadWidgetConfigs.

private void loadWidgetConfigs() {
    try {
        for (int i = 0; i < widgetButtonsMapping.size(); i++) {
            ((TextView) findViewById(widgetButtonsMapping.keyAt(i))).setText(widgetButtonsMapping.valueAt(i) + " widget");
        }
        JSONObject keyConfig = new JSONObject(sharedPreferences.getString("FOSSIL_HR_WIDGETS", "{}"));
        Iterator<String> keyIterator = keyConfig.keys();
        loop: while (keyIterator.hasNext()) {
            String position = keyIterator.next();
            for (int widgetButtonIndex = 0; widgetButtonIndex < widgetButtonsMapping.size(); widgetButtonIndex++) {
                if (position.equals(widgetButtonsMapping.valueAt(widgetButtonIndex))) {
                    int buttonId = widgetButtonsMapping.keyAt(widgetButtonIndex);
                    String function = keyConfig.getString(position);
                    Widget.WidgetType[] types = Widget.WidgetType.values();
                    if (function.startsWith("custom_")) {
                        ((TextView) findViewById(buttonId)).setText(position + " widget: " + function.substring(7));
                        continue loop;
                    }
                    for (int widgetIdIndex = 0; widgetIdIndex < types.length; widgetIdIndex++) {
                        String widgetIdMappingValue = types[widgetIdIndex].getIdentifier();
                        if (widgetIdMappingValue != null && widgetIdMappingValue.equals(function)) {
                            ((TextView) findViewById(buttonId)).setText(position + " widget: " + getResources().getText(types[widgetIdIndex].getStringResource()));
                            break;
                        }
                    }
                }
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
Also used : JSONObject(org.json.JSONObject) Widget(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.Widget) CustomWidget(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomWidget) JSONException(org.json.JSONException) TextView(android.widget.TextView)

Aggregations

CustomWidget (nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomWidget)5 Widget (nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.Widget)5 ArrayList (java.util.ArrayList)3 Version (nodomain.freeyourgadget.gadgetbridge.util.Version)3 Paint (android.graphics.Paint)2 TextView (android.widget.TextView)2 CustomWidgetElement (nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomWidgetElement)2 Prefs (nodomain.freeyourgadget.gadgetbridge.util.Prefs)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 Bitmap (android.graphics.Bitmap)1 Canvas (android.graphics.Canvas)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 ListView (android.widget.ListView)1 File (java.io.File)1 IOException (java.io.IOException)1