Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to display BG as overlay on BgGraph for users who prefer larger font #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.text.TextPaint;
import android.util.Log;

import java.util.Arrays;
import java.util.stream.Collectors;

import sk.trupici.gwatch.wear.BuildConfig;
import sk.trupici.gwatch.wear.common.data.Trend;
import sk.trupici.gwatch.wear.common.util.BgUtils;
import sk.trupici.gwatch.wear.util.UiUtils;

import static sk.trupici.gwatch.wear.common.util.CommonConstants.HOUR_IN_MINUTES;
Expand Down Expand Up @@ -65,6 +70,9 @@ public class BgGraph {

private RectF bounds;

private Integer lastBgValue;
private Trend lastBgTrend = Trend.UNKNOWN;

public void create(SharedPreferences sharedPrefs, BgGraphParams params, RectF bounds) {
this.params = params;

Expand Down Expand Up @@ -107,12 +115,17 @@ public void draw(Canvas canvas, boolean isAmbientMode) {
}
}

public void updateGraphData(Double bgValue, long timestamp, SharedPreferences sharedPrefs) {
public void updateGraphData(Double bgValue, long timestamp, Trend bgTrend, SharedPreferences sharedPrefs) {
// update or reload
// graph data might be already updated, e.g. by BgData Provider, reload
lastGraphUpdateMin = restoreChartData(sharedPrefs, graphData);
lastGraphUpdateMin = updateGraphData(bgValue, timestamp, sharedPrefs, graphData, lastGraphUpdateMin, params.refreshRateMin);

if(bgValue != null) {
lastBgValue = bgValue.intValue();
lastBgTrend = bgTrend;
}

// redraw chart
if (lastGraphUpdateMin > 0) {
recalculateDynamicRange();
Expand Down Expand Up @@ -232,7 +245,6 @@ private void drawChart() {
int valueOffset = GRAPH_DATA_LEN - count;

// draw values
int color;
float prevX = 0, prevY = 0;
for (int i = valueOffset; i < GRAPH_DATA_LEN; i++) {
int value = graphData[i];
Expand All @@ -249,19 +261,7 @@ private void drawChart() {
x = xOffset + (2 * DOT_RADIUS + padding) * (i - valueOffset);
y = yOffset - ((value - graphRange.min) * graphRange.scale);

if (value <= params.hypoThreshold) {
color = params.hypoColor;
} else if (value <= params.lowThreshold) {
color = params.lowColor;
} else if (value < params.highThreshold) {
color = params.inRangeColor;
} else if (value < params.hyperThreshold) {
color = params.highColor;
} else {
color = params.hyperColor;
}

paint.setColor(color);
paint.setColor(getColor(value));

// draw value with color
if (params.drawChartLine) { // line graph
Expand All @@ -280,6 +280,47 @@ private void drawChart() {
prevX = x;
prevY = y;
}

if(params.showBgValue) {
DrawBgValue(canvas);
}
}

private int getColor(int value){
if (value <= params.hypoThreshold) {
return params.hypoColor;
} else if (value <= params.lowThreshold) {
return params.lowColor;
} else if (value < params.highThreshold) {
return params.inRangeColor;
} else if (value < params.hyperThreshold) {
return params.highColor;
}
return params.hyperColor;
}

private void DrawBgValue(Canvas canvas){
if(lastBgValue == null) {
return;
}

TextPaint bgValueTextPaint = UiUtils.createTextPaint();
bgValueTextPaint.setColor(getColor(lastBgValue));
float textSize = (bounds.height() - params.bottomPadding - params.topPadding);
bgValueTextPaint.setTextSize(textSize);

boolean isUnitConversion = false;
String bgValueString = BgUtils.formatBgValueString(lastBgValue, lastBgTrend, isUnitConversion);

Paint bgBalueBackgroundPaint = new Paint();
bgBalueBackgroundPaint.setColor(Color.argb(200, 0, 0,0));
Rect textBounds = new Rect();
bgValueTextPaint.getTextBounds(bgValueString, 0, bgValueString.length(), textBounds);
canvas.drawRect(params.leftPadding, 0, params.leftPadding + textBounds.width() , bounds.height(), bgBalueBackgroundPaint);

float xLineAlternate = params.leftPadding;
float yLineAlternate = bounds.height() - (bounds.height() - textBounds.height())/2;
canvas.drawText(bgValueString, xLineAlternate, yLineAlternate, bgValueTextPaint);
}

private GraphRange getDynamicRange() {
Expand Down Expand Up @@ -485,6 +526,7 @@ public static class BgGraphParams {
private int hyperThreshold;

private boolean enableDynamicRange;
private boolean showBgValue;
private boolean enableVertLines;
private boolean enableCriticalLines;
private boolean enableHighLine;
Expand Down Expand Up @@ -647,6 +689,14 @@ public void setEnableDynamicRange(boolean enableDynamicRange) {
this.enableDynamicRange = enableDynamicRange;
}

public boolean showBgValue() {
return showBgValue;
}

public void setShowBgValue(boolean showBgValue) {
this.showBgValue = showBgValue;
}

public boolean isEnableVertLines() {
return enableVertLines;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class BgGraphPanel extends BroadcastReceiver implements ComponentPanel {
public static final String PREF_ENABLE_LOW_LINE = "graph_enable_low_line";

public static final String PREF_ENABLE_DYNAMIC_RANGE = "graph_enable_dynamic_range";
public static final String PREF_SHOW_BG_VALUE = "show_bg_value";

public static final String PREF_TYPE_LINE = "graph_type_draw_line";
public static final String PREF_TYPE_DOTS = "graph_type_draw_dots";
Expand Down Expand Up @@ -138,6 +139,7 @@ public void onConfigChanged(Context context, SharedPreferences sharedPrefs) {
params.setEnableLowLine(sharedPrefs.getBoolean(watchfaceConfig.getPrefsPrefix() + PREF_ENABLE_LOW_LINE, context.getResources().getBoolean(R.bool.def_graph_enable_low_line)));

params.setEnableDynamicRange(sharedPrefs.getBoolean(watchfaceConfig.getPrefsPrefix() + PREF_ENABLE_DYNAMIC_RANGE, context.getResources().getBoolean(R.bool.def_graph_enable_dynamic_range)));
params.setShowBgValue(sharedPrefs.getBoolean(watchfaceConfig.getPrefsPrefix() + PREF_SHOW_BG_VALUE, context.getResources().getBoolean(R.bool.def_graph_show_bg_value)));

params.setDrawChartLine(sharedPrefs.getBoolean(watchfaceConfig.getPrefsPrefix() + PREF_TYPE_LINE, context.getResources().getBoolean(R.bool.def_graph_type_draw_line)));
params.setDrawChartDots(sharedPrefs.getBoolean(watchfaceConfig.getPrefsPrefix() + PREF_TYPE_DOTS, context.getResources().getBoolean(R.bool.def_graph_type_draw_dots)));
Expand Down Expand Up @@ -165,7 +167,7 @@ public void onDraw(Canvas canvas, boolean isAmbientMode) {
public void refresh(long timeMs, SharedPreferences sharedPrefs) {
long currentMinute = timeMs / MINUTE_IN_MILLIS;
if (bgGraph.getLastGraphUpdateMin() != currentMinute) {
bgGraph.updateGraphData(null, timeMs, sharedPrefs);
bgGraph.updateGraphData(null, timeMs, null, sharedPrefs);
}
}

Expand All @@ -179,7 +181,7 @@ public void onReceive(Context context, Intent intent) {
if (bgData.getValue() == 0 || bgData.getTimestamp() == 0) {
return;
}
bgGraph.updateGraphData((double)bgData.getValue(), bgData.getTimestamp(), sharedPrefs);
bgGraph.updateGraphData((double)bgData.getValue(), bgData.getTimestamp(), bgData.getTrend(), sharedPrefs);
} else if (CommonConstants.REMOTE_CONFIG_ACTION.equals(intent.getAction())) {
onConfigChanged(context, sharedPrefs);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class BgPanel extends BroadcastReceiver implements ComponentPanel {
private float topOffset;
private float bottomOffset;

private Rect bounds;
private RectF bounds;
private Paint bkgPaint;
private TextPaint textPaint;
private String bgLine1;
Expand All @@ -95,7 +95,8 @@ public class BgPanel extends BroadcastReceiver implements ComponentPanel {
private long lastBgUpdate;

boolean isUnitConversion;

boolean showBgValue;

private int backgroundColor;
private int hyperColor;
private int highColor;
Expand Down Expand Up @@ -157,7 +158,8 @@ public void onReceive(Context context, Intent intent) {

@Override
public void onCreate(Context context, SharedPreferences sharedPrefs) {
RectF bounds = watchfaceConfig.getBgPanelBounds(context);
bounds = watchfaceConfig.getBgPanelBounds(context);

sizeFactors = new RectF(
bounds.left / (float)refScreenWidth,
bounds.top / (float)refScreenHeight,
Expand Down Expand Up @@ -216,11 +218,11 @@ public void onCreate(Context context, SharedPreferences sharedPrefs) {

@Override
public void onSizeChanged(Context context, int width, int height) {
bounds = new Rect(
(int) (sizeFactors.left * width),
(int) (sizeFactors.top * height),
(int) (sizeFactors.right * width),
(int) (sizeFactors.bottom * height));
bounds = new RectF(
sizeFactors.left * width,
sizeFactors.top * height,
sizeFactors.right * width,
sizeFactors.bottom * height);

float yScale = (float) height / refScreenHeight;
topOffset = watchfaceConfig.getBgPanelTopOffset(context) * yScale;
Expand All @@ -232,7 +234,7 @@ public void onSizeChanged(Context context, int width, int height) {
yLine2 = bounds.bottom - bottomOffset - paddedHeight / 10f;


bkgBitmap = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.ARGB_8888);
bkgBitmap = Bitmap.createBitmap((int)bounds.width(), (int)bounds.height(), Bitmap.Config.ARGB_8888);
drawBackgroundAndBorder();

if (showBgIndicator) {
Expand Down Expand Up @@ -262,6 +264,8 @@ public void onConfigChanged(Context context, SharedPreferences sharedPrefs) {

isUnitConversion = sharedPrefs.getBoolean(CommonConstants.PREF_IS_UNIT_CONVERSION,
context.getResources().getBoolean(R.bool.def_bg_is_unit_conversion));
showBgValue = sharedPrefs.getBoolean(CommonConstants.PREF_BG_PANEL_SHOW_VALUE,
context.getResources().getBoolean(R.bool.def_bg_show_value));

// thresholds
hyperThreshold = sharedPrefs.getInt(CommonConstants.PREF_HYPER_THRESHOLD, context.getResources().getInteger(R.integer.def_bg_threshold_hyper));
Expand Down Expand Up @@ -304,6 +308,29 @@ public void onDraw(Canvas canvas, boolean isAmbientMode) {
textPaint.setColor(getRangedColor(isNoData()));
}

DrawTextLines(canvas);

if (showBgIndicator) {
Boolean isNoData = isNoData();
Paint paint = isAmbientMode ? ambientPaint : indicatorPaint;
drawIndicatorBar(canvas, paint, lowIndicatorBounds, getLowIndicatorColor(isNoData));
drawIndicatorBar(canvas, paint, inRangeIndicatorBounds, getInRangeIndicatorColor(isNoData));
drawIndicatorBar(canvas, paint, highIndicatorBounds, getHighIndicatorColor(isNoData));
}
}

private void DrawTextLines(Canvas canvas){
if(!showBgValue){
// only line 2 centered vertically
textPaint.setTextAlign(Paint.Align.CENTER);
float textSize = bounds.width() / 4f;
textPaint.setTextSize(textSize);
textPaint.setFakeBoldText(true);
float yCenteredStart = bounds.bottom - bounds.height() / 2f + textSize / 2f;
canvas.drawText(bgLine2 != null ? bgLine2 : ComplicationConfig.NO_DATA_TEXT, xLine, yCenteredStart, textPaint);
return;
}

// line 1
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setTextSize(paddedHeight / 2f);
Expand All @@ -314,14 +341,6 @@ public void onDraw(Canvas canvas, boolean isAmbientMode) {
textPaint.setTextSize(paddedHeight / 3f);
textPaint.setFakeBoldText(false);
canvas.drawText(bgLine2 != null ? bgLine2 : ComplicationConfig.NO_DATA_TEXT, xLine, yLine2, textPaint);

if (showBgIndicator) {
Boolean isNoData = isNoData();
Paint paint = isAmbientMode ? ambientPaint : indicatorPaint;
drawIndicatorBar(canvas, paint, lowIndicatorBounds, getLowIndicatorColor(isNoData));
drawIndicatorBar(canvas, paint, inRangeIndicatorBounds, getInRangeIndicatorColor(isNoData));
drawIndicatorBar(canvas, paint, highIndicatorBounds, getHighIndicatorColor(isNoData));
}
}

private void drawBackgroundAndBorder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public class BgGraphMenuItems {
BgGraphPanel.PREF_ENABLE_DYNAMIC_RANGE,
ConfigItem.Type.TYPE_SWITCH,
R.bool.def_graph_enable_dynamic_range),
new BoolConfigItem(
R.string.config_item_graph_show_bg_value,
BgGraphPanel.PREF_SHOW_BG_VALUE,
ConfigItem.Type.TYPE_SWITCH,
R.bool.def_graph_show_bg_value),
new BoolConfigItem(
R.string.config_item_graph_draw_dots,
BgGraphPanel.PREF_TYPE_DOTS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public class BgPanelMenuItems {
ConfigItem.Type.TYPE_SWITCH,
R.bool.def_bg_is_unit_conversion,
true),
new BoolConfigItem(
R.string.config_item_panel_show_value,
CommonConstants.PREF_BG_PANEL_SHOW_VALUE,
ConfigItem.Type.TYPE_SWITCH,
R.bool.def_bg_show_value,
true),
new BasicConfigItem(
R.string.config_item_bg_hypo_color_label,
R.drawable.config_color_edit_24,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected void onCreate(Bundle savedInstanceState) {
graphParams.setHyperThreshold(sharedPrefs.getInt(CommonConstants.PREF_HYPER_THRESHOLD, res.getInteger(R.integer.def_bg_threshold_hyper)));

bgGraph.create(sharedPrefs, graphParams, new RectF(graphBounds));
bgGraph.updateGraphData(null, System.currentTimeMillis(), sharedPrefs);
bgGraph.updateGraphData(null, System.currentTimeMillis(), null, sharedPrefs);
graphView.setBgGraph(bgGraph);

new Handler(Looper.getMainLooper()).postDelayed(this::finish, TIME_TO_CLOSE_MS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public interface CommonConstants {
String REMOTE_CONFIG_ACTION = "sk.trupici.gwatch.wear.REMOTE_CONFIG_ACTION";

String PREF_IS_UNIT_CONVERSION = "bg_is_unit_conversion";
String PREF_BG_PANEL_SHOW_VALUE = "bg_panel_show_value";

String PREF_HYPER_THRESHOLD = "bg_threshold_hyper";
String PREF_HIGH_THRESHOLD = "bg_threshold_high";
Expand Down
1 change: 1 addition & 0 deletions wearable/src/main/res/values/bg_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<bool name="def_graph_enable_low_line">true</bool>

<bool name="def_graph_enable_dynamic_range">true</bool>
<bool name="def_graph_show_bg_value">false</bool>

<bool name="def_graph_type_draw_line">true</bool>
<bool name="def_graph_type_draw_dots">false</bool>
Expand Down
1 change: 1 addition & 0 deletions wearable/src/main/res/values/bg_panel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

<integer name="def_bg_sample_period">5</integer>
<bool name="def_bg_is_unit_conversion">false</bool>
<bool name="def_bg_show_value">true</bool>

<color name="def_bg_border_color">#00000000</color>
<string name="def_bg_border_type" translatable="false">NONE</string>
Expand Down
2 changes: 2 additions & 0 deletions wearable/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<string name="config_item_no_data_color_label">No data color</string>

<string name="config_item_is_unit_conversion">Use mmol/l</string>
<string name="config_item_panel_show_value">Show BG</string>

<string name="config_item_graph_show_critical_lines">Show critical lines</string>
<string name="config_item_graph_critical_lines_color">Critical lines color</string>
Expand All @@ -57,6 +58,7 @@
<string name="config_item_graph_show_vert_lines">Show hour lines</string>
<string name="config_item_graph_vert_lines_color">Hour lines color</string>
<string name="config_item_graph_enable_dynamic_range">Dynamic range</string>
<string name="config_item_graph_show_bg_value">Show overlay with BG</string>
<string name="config_item_graph_draw_dots">Draw dots</string>
<string name="config_item_graph_draw_line">Draw line</string>

Expand Down