2021-06-20 14:17:18 +02:00
|
|
|
/* Copyright (C) 2021 Arjan Schrijver
|
|
|
|
|
|
|
|
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 <http://www.gnu.org/licenses/>. */
|
|
|
|
package nodomain.freeyourgadget.gadgetbridge.devices.qhybrid;
|
|
|
|
|
2021-07-11 20:51:44 +02:00
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
|
2022-07-22 12:28:19 +02:00
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2021-07-11 20:51:44 +02:00
|
|
|
import java.io.IOException;
|
2022-07-22 12:28:19 +02:00
|
|
|
import java.io.Serializable;
|
2021-07-14 22:40:02 +02:00
|
|
|
import java.util.LinkedHashMap;
|
2021-07-11 20:51:44 +02:00
|
|
|
|
2021-07-16 12:12:58 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2022-07-24 22:32:53 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.BitmapUtil;
|
2021-07-16 12:12:58 +02:00
|
|
|
|
2021-07-16 15:11:45 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.util.BitmapUtil.invertBitmapColors;
|
|
|
|
|
2022-07-22 12:28:19 +02:00
|
|
|
public class HybridHRWatchfaceWidget implements Serializable {
|
2021-06-20 14:17:18 +02:00
|
|
|
private String widgetType;
|
|
|
|
private int posX;
|
|
|
|
private int posY;
|
2022-01-10 22:07:33 +01:00
|
|
|
private int width;
|
|
|
|
private int height;
|
|
|
|
private int color;
|
2022-07-24 22:32:53 +02:00
|
|
|
private String background;
|
2022-07-22 12:28:19 +02:00
|
|
|
private String extraConfigJSON;
|
2021-07-16 15:11:45 +02:00
|
|
|
|
2021-07-22 13:07:09 +02:00
|
|
|
public static int COLOR_WHITE = 0;
|
|
|
|
public static int COLOR_BLACK = 1;
|
2021-06-20 14:17:18 +02:00
|
|
|
|
2022-07-22 12:28:19 +02:00
|
|
|
static HybridHRWidgetPosition[] defaultPositions = new HybridHRWidgetPosition[]{
|
|
|
|
new HybridHRWidgetPosition(120, 58, R.string.watchface_dialog_widget_preset_top),
|
|
|
|
new HybridHRWidgetPosition(182, 120, R.string.watchface_dialog_widget_preset_right),
|
|
|
|
new HybridHRWidgetPosition(120, 182, R.string.watchface_dialog_widget_preset_bottom),
|
|
|
|
new HybridHRWidgetPosition(58, 120, R.string.watchface_dialog_widget_preset_left),
|
|
|
|
};
|
|
|
|
|
2022-06-20 14:45:02 +02:00
|
|
|
public HybridHRWatchfaceWidget(String widgetType, int posX, int posY, int width, int height, int color, JSONObject extraConfig) {
|
2021-06-20 14:17:18 +02:00
|
|
|
this.widgetType = widgetType;
|
|
|
|
this.posX = posX;
|
|
|
|
this.posY = posY;
|
2022-01-10 22:07:33 +01:00
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
2021-07-16 15:11:45 +02:00
|
|
|
this.color = color;
|
2022-07-24 22:32:53 +02:00
|
|
|
this.background = "";
|
2022-07-22 12:28:19 +02:00
|
|
|
try {
|
|
|
|
this.extraConfigJSON = extraConfig.toString();
|
|
|
|
} catch (Exception e) {
|
|
|
|
this.extraConfigJSON = "{}";
|
|
|
|
}
|
2021-11-17 13:42:14 +01:00
|
|
|
}
|
2021-08-08 22:47:25 +02:00
|
|
|
|
2021-07-14 22:40:02 +02:00
|
|
|
public static LinkedHashMap<String, String> getAvailableWidgetTypes(Context context) {
|
|
|
|
LinkedHashMap<String, String> widgetTypes = new LinkedHashMap<>();
|
2021-07-16 12:12:58 +02:00
|
|
|
widgetTypes.put("widgetDate", context.getString(R.string.watchface_widget_type_date));
|
|
|
|
widgetTypes.put("widgetWeather", context.getString(R.string.watchface_widget_type_weather));
|
|
|
|
widgetTypes.put("widgetSteps", context.getString(R.string.watchface_widget_type_steps));
|
|
|
|
widgetTypes.put("widgetHR", context.getString(R.string.watchface_widget_type_heart_rate));
|
2021-07-27 14:50:06 +02:00
|
|
|
widgetTypes.put("widgetBattery", context.getString(R.string.watchface_widget_type_battery));
|
|
|
|
widgetTypes.put("widgetCalories", context.getString(R.string.watchface_widget_type_calories));
|
2021-07-28 16:09:51 +02:00
|
|
|
widgetTypes.put("widget2ndTZ", context.getString(R.string.watchface_widget_type_2nd_tz));
|
|
|
|
widgetTypes.put("widgetActiveMins", context.getString(R.string.watchface_widget_type_active_mins));
|
2023-06-18 22:11:07 +02:00
|
|
|
widgetTypes.put("widgetChanceOfRain", context.getString(R.string.watchface_widget_type_chance_rain));
|
|
|
|
widgetTypes.put("widgetUV", context.getString(R.string.watchface_widget_type_uv_index));
|
2021-11-12 15:34:57 +01:00
|
|
|
widgetTypes.put("widgetCustom", context.getString(R.string.watchface_widget_type_custom));
|
2021-07-14 22:40:02 +02:00
|
|
|
return widgetTypes;
|
|
|
|
}
|
|
|
|
|
2022-07-22 12:28:19 +02:00
|
|
|
public void setWidgetType(String widgetType) {
|
|
|
|
this.widgetType = widgetType;
|
|
|
|
}
|
2021-06-20 14:17:18 +02:00
|
|
|
public String getWidgetType() {
|
|
|
|
return widgetType;
|
|
|
|
}
|
|
|
|
|
2022-07-22 12:28:19 +02:00
|
|
|
|
2021-07-11 20:51:44 +02:00
|
|
|
public Bitmap getPreviewImage(Context context) throws IOException {
|
2021-07-16 15:11:45 +02:00
|
|
|
Bitmap preview = BitmapFactory.decodeStream(context.getAssets().open("fossil_hr/" + widgetType + "_preview.png"));
|
2022-07-24 22:32:53 +02:00
|
|
|
if (getBackground() != "") {
|
|
|
|
try {
|
|
|
|
Bitmap background = BitmapFactory.decodeStream(context.getAssets().open("fossil_hr/" + getBackground() + ".png"));
|
|
|
|
preview = BitmapUtil.overlay(background, preview);
|
|
|
|
} catch (Exception e) {
|
|
|
|
// continue silently without background
|
|
|
|
}
|
|
|
|
}
|
2021-07-22 13:07:09 +02:00
|
|
|
if (color == COLOR_WHITE) {
|
2021-07-16 15:11:45 +02:00
|
|
|
return preview;
|
|
|
|
} else {
|
|
|
|
return invertBitmapColors(preview);
|
|
|
|
}
|
2021-07-11 20:51:44 +02:00
|
|
|
}
|
|
|
|
|
2021-06-20 14:17:18 +02:00
|
|
|
public int getPosX() {
|
|
|
|
return posX;
|
|
|
|
}
|
|
|
|
public int getPosY() {
|
|
|
|
return posY;
|
|
|
|
}
|
2021-07-16 11:36:46 +02:00
|
|
|
|
|
|
|
public void setPosX(int posX) {
|
|
|
|
this.posX = posX;
|
|
|
|
}
|
|
|
|
public void setPosY(int posY) {
|
|
|
|
this.posY = posY;
|
|
|
|
}
|
2021-07-16 15:11:45 +02:00
|
|
|
|
2022-01-10 22:07:33 +01:00
|
|
|
public int getWidth() {
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
public int getHeight() {
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setWidth(int width) {
|
|
|
|
this.width = width;
|
|
|
|
}
|
|
|
|
public void setHeight(int height) {
|
|
|
|
this.height = height;
|
|
|
|
}
|
|
|
|
|
2021-07-16 15:11:45 +02:00
|
|
|
public int getColor() {
|
|
|
|
return color;
|
|
|
|
}
|
2022-06-17 22:42:58 +02:00
|
|
|
public void setColor(int color) {
|
|
|
|
this.color = color;
|
|
|
|
}
|
2021-08-08 22:47:25 +02:00
|
|
|
|
2022-07-24 22:32:53 +02:00
|
|
|
public String getBackground() {
|
|
|
|
return background;
|
|
|
|
}
|
|
|
|
public void setBackground(String background) {
|
|
|
|
this.background = background;
|
|
|
|
}
|
|
|
|
|
2022-06-20 14:45:02 +02:00
|
|
|
public int getExtraConfigInt(String name, int fallback) {
|
2022-07-22 12:28:19 +02:00
|
|
|
try {
|
|
|
|
return new JSONObject(extraConfigJSON).optInt(name, fallback);
|
|
|
|
} catch (Exception e) {
|
2022-06-20 14:45:02 +02:00
|
|
|
return fallback;
|
|
|
|
}
|
2021-11-17 13:42:14 +01:00
|
|
|
}
|
2022-06-20 14:45:02 +02:00
|
|
|
public String getExtraConfigString(String name, String fallback) {
|
2022-07-22 12:28:19 +02:00
|
|
|
try {
|
|
|
|
return new JSONObject(extraConfigJSON).optString(name, fallback);
|
|
|
|
} catch (Exception e) {
|
2022-06-20 14:45:02 +02:00
|
|
|
return fallback;
|
|
|
|
}
|
2021-11-22 21:28:47 +01:00
|
|
|
}
|
2022-06-20 14:45:02 +02:00
|
|
|
public Boolean getExtraConfigBoolean(String name, Boolean fallback) {
|
2022-07-22 12:28:19 +02:00
|
|
|
try {
|
|
|
|
return new JSONObject(extraConfigJSON).optBoolean(name, fallback);
|
|
|
|
} catch (Exception e) {
|
2022-06-20 14:45:02 +02:00
|
|
|
return fallback;
|
2022-07-22 12:28:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setExtraConfigInt(String name, int value) {
|
|
|
|
JSONObject extraConfig;
|
|
|
|
try {
|
|
|
|
extraConfig = new JSONObject(extraConfigJSON);
|
|
|
|
} catch (Exception e) {
|
|
|
|
extraConfig = new JSONObject();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
extraConfig.put(name, value);
|
|
|
|
extraConfigJSON = extraConfig.toString();
|
|
|
|
} catch (Exception e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public void setExtraConfigString(String name, String value) {
|
|
|
|
JSONObject extraConfig;
|
|
|
|
try {
|
|
|
|
extraConfig = new JSONObject(extraConfigJSON);
|
|
|
|
} catch (Exception e) {
|
|
|
|
extraConfig = new JSONObject();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
extraConfig.put(name, value);
|
|
|
|
extraConfigJSON = extraConfig.toString();
|
|
|
|
} catch (Exception e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public void setExtraConfigBoolean(String name, Boolean value) {
|
|
|
|
JSONObject extraConfig;
|
|
|
|
try {
|
|
|
|
extraConfig = new JSONObject(extraConfigJSON);
|
|
|
|
} catch (Exception e) {
|
|
|
|
extraConfig = new JSONObject();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
extraConfig.put(name, value);
|
|
|
|
extraConfigJSON = extraConfig.toString();
|
|
|
|
} catch (Exception e) {
|
|
|
|
return;
|
2022-06-20 14:45:02 +02:00
|
|
|
}
|
2021-11-22 21:28:47 +01:00
|
|
|
}
|
2021-06-20 14:17:18 +02:00
|
|
|
}
|