Search in sources :

Example 1 with AssetImage

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

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

the class ImageEditActivity method finalizeImage.

private void finalizeImage() {
    Bitmap cropped = Bitmap.createBitmap(this.mainBitmap, (int) this.x, (int) this.y, (int) this.diameter, (int) this.diameter);
    Bitmap scaled = Bitmap.createScaledBitmap(cropped, 400, 400, false);
    cropped.recycle();
    try {
        AssetImage image = AssetImageFactory.createAssetImage(scaled, false, 0, 0, 0);
        Intent resultIntent = new Intent();
        resultIntent.putExtra("EXTRA_PIXELS_ENCODED", image.getFileData());
        setResult(RESULT_CODE_EDIT_SUCCESS, resultIntent);
        finish();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        scaled.recycle();
    }
}
Also used : AssetImage(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.image.AssetImage) Bitmap(android.graphics.Bitmap) Intent(android.content.Intent) IOException(java.io.IOException)

Aggregations

Bitmap (android.graphics.Bitmap)2 IOException (java.io.IOException)2 AssetImage (nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.image.AssetImage)2 Intent (android.content.Intent)1 Canvas (android.graphics.Canvas)1 Paint (android.graphics.Paint)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 AssetFilePutRequest (nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.file.AssetFilePutRequest)1 ImagesSetRequest (nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.image.ImagesSetRequest)1 CustomWidget (nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomWidget)1 CustomWidgetElement (nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.CustomWidgetElement)1 Widget (nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.widget.Widget)1 BitmapUtil.convertDrawableToBitmap (nodomain.freeyourgadget.gadgetbridge.util.BitmapUtil.convertDrawableToBitmap)1 Prefs (nodomain.freeyourgadget.gadgetbridge.util.Prefs)1 Version (nodomain.freeyourgadget.gadgetbridge.util.Version)1