Skip to content

Commit 7bd8351

Browse files
committed
osmandapp#8108, osmandapp#3721 Code review fixes part 1:
- Add fallback for C_SPEED_CAMERA_ALARM in JsCommandBuilder - Combine checks for text change / bottom text change in the AlarmWidget.updateInfo into one conditional statement and extract updateTextWidget / updateBottomTextWidget methods.
1 parent 09688ec commit 7bd8351

File tree

5 files changed

+73
-65
lines changed

5 files changed

+73
-65
lines changed

OsmAnd/src/net/osmand/plus/plugins/development/TestVoiceActivity.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ private void addButtons(final LinearLayout ll, CommandPlayer p) {
214214

215215
addButton(ll, "Attention prompts:", builder(p));
216216
addButton(ll, "\u25BA (9.1) You are exceeding the speed limit '50' (18 m/s)", builder(p).speedAlarm(50, 18f));
217-
addButton(ll, "\u25BA (9.2) Attention, speed camera, distance 650m, speed limit '50' (18 m/s)", builder(p).speedCameraAlarm(650f, 50));
217+
addButton(ll, "\u25BA (9.2) Attention, speed camera, distance 650m, speed limit '50' (18 m/s)", builder(p).speedCameraAlarm(650f, 50, "SPEED_CAMERA"));
218218
addButton(ll, "\u25BA (9.3) Attention, speed camera", builder(p).attention("SPEED_CAMERA"));
219219
addButton(ll, "\u25BA (9.4) Attention, border control", builder(p).attention("BORDER_CONTROL"));
220220
addButton(ll, "\u25BA (9.5) Attention, railroad crossing", builder(p).attention("RAILWAY"));

OsmAnd/src/net/osmand/plus/routing/VoiceRouter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ private void announceSpeedCameraAlarm(double dist, int maxSpeed) {
374374
CommandBuilder p = getNewCommandPlayerToPlay();
375375
if (p != null) {
376376
if (dist > 0 && maxSpeed > 0) {
377-
p.speedCameraAlarm(dist, maxSpeed);
377+
p.speedCameraAlarm(dist, maxSpeed, String.valueOf(AlarmInfoType.SPEED_CAMERA));
378378
} else {
379379
p.attention(String.valueOf(AlarmInfoType.SPEED_CAMERA));
380380
}

OsmAnd/src/net/osmand/plus/views/mapwidgets/widgets/AlarmWidget.java

+66-60
Original file line numberDiff line numberDiff line change
@@ -144,69 +144,17 @@ public boolean updateInfo(DrawSettings drawSettings, boolean drawBitmap) {
144144
}
145145
}
146146

147-
boolean isRegionChanged = cachedRegion != info.region;
148-
if (isRegionChanged) {
149-
cachedRegion = info.region;
150-
}
151-
152-
boolean isAlarmTypeChanged = cachedAlarmType != alarm.getType();
153-
if (isAlarmTypeChanged) {
154-
cachedAlarmType = alarm.getType();
155-
}
156-
157-
if (!Algorithms.objectEquals(info.text, cachedText) || isRegionChanged || isAlarmTypeChanged) {
147+
if (!Algorithms.objectEquals(info.text, cachedText) ||
148+
!Algorithms.objectEquals(info.bottomText, cachedBottomText) ||
149+
cachedRegion != info.region || cachedAlarmType != alarm.getType()) {
158150
changed = true;
159151
cachedText = info.text;
160-
if (layout != null && widgetText != null) {
161-
widgetText.setText(cachedText);
162-
Resources res = layout.getContext().getResources();
163-
if (cachedAlarmType == AlarmInfo.AlarmInfoType.SPEED_LIMIT && info.americanType && !info.isCanadianRegion) {
164-
int topPadding = res.getDimensionPixelSize(R.dimen.map_alarm_text_top_padding);
165-
widgetText.setPadding(0, topPadding, 0, 0);
166-
} else {
167-
widgetText.setPadding(0, 0, 0, 0);
168-
}
169-
170-
if (cachedAlarmType == AlarmInfo.AlarmInfoType.SPEED_CAMERA) {
171-
FrameLayout.LayoutParams widgetTextLayoutParams = new FrameLayout.LayoutParams(
172-
res.getDimensionPixelSize(R.dimen.map_alarm_speed_camera_speed_info_size),
173-
res.getDimensionPixelSize(R.dimen.map_alarm_speed_camera_speed_info_size));
174-
175-
widgetTextLayoutParams.gravity = Gravity.RIGHT | Gravity.TOP;
176-
177-
widgetText.setLayoutParams(widgetTextLayoutParams);
178-
widgetText.setGravity(Gravity.CENTER);
179-
widgetText.setTextSize(COMPLEX_UNIT_PX, res.getDimensionPixelSize(R.dimen.map_alarm_speed_camera_speed_info_text_size));
180-
} else {
181-
FrameLayout.LayoutParams widgetTextLayoutParams = new FrameLayout.LayoutParams(
182-
FrameLayout.LayoutParams.WRAP_CONTENT,
183-
FrameLayout.LayoutParams.WRAP_CONTENT);
184-
185-
widgetTextLayoutParams.gravity = Gravity.CENTER;
186-
187-
widgetText.setLayoutParams(widgetTextLayoutParams);
188-
widgetText.setGravity(Gravity.NO_GRAVITY);
189-
widgetText.setTextSize(COMPLEX_UNIT_PX, res.getDimensionPixelSize(R.dimen.map_alarm_text_size));
190-
}
191-
}
192-
}
193-
if (!Algorithms.objectEquals(info.bottomText, cachedBottomText) || isRegionChanged || isAlarmTypeChanged) {
194-
changed = true;
195152
cachedBottomText = info.bottomText;
196-
if (layout != null && widgetBottomText != null) {
197-
widgetBottomText.setText(cachedBottomText);
198-
Resources res = layout.getContext().getResources();
199-
if (cachedAlarmType == AlarmInfo.AlarmInfoType.SPEED_LIMIT && info.isCanadianRegion) {
200-
int bottomPadding = res.getDimensionPixelSize(R.dimen.map_button_margin);
201-
widgetBottomText.setPadding(0, 0, 0, bottomPadding);
202-
widgetBottomText.setTextSize(COMPLEX_UNIT_PX, res.getDimensionPixelSize(R.dimen.map_alarm_bottom_si_text_size));
203-
} else {
204-
widgetBottomText.setPadding(0, 0, 0, 0);
205-
widgetBottomText.setTextSize(COMPLEX_UNIT_PX, res.getDimensionPixelSize(R.dimen.map_alarm_bottom_text_size));
206-
}
207-
widgetBottomText.setTextColor(ContextCompat.getColor(layout.getContext(),
208-
info.americanType ? R.color.color_black : R.color.color_white));
209-
}
153+
cachedRegion = info.region;
154+
cachedAlarmType = alarm.getType();
155+
156+
updateTextWidget(info);
157+
updateBottomTextWidget(info);
210158
}
211159
}
212160
}
@@ -234,6 +182,64 @@ public boolean updateInfo(DrawSettings drawSettings, boolean drawBitmap) {
234182
return true;
235183
}
236184

185+
private void updateTextWidget(@NonNull AlarmWidgetInfo info) {
186+
if (layout == null || widgetText == null) {
187+
return;
188+
}
189+
190+
widgetText.setText(info.text);
191+
Resources res = layout.getContext().getResources();
192+
193+
if (info.alarm.getType() == AlarmInfo.AlarmInfoType.SPEED_LIMIT && info.americanType && !info.isCanadianRegion) {
194+
int topPadding = res.getDimensionPixelSize(R.dimen.map_alarm_text_top_padding);
195+
widgetText.setPadding(0, topPadding, 0, 0);
196+
} else {
197+
widgetText.setPadding(0, 0, 0, 0);
198+
}
199+
200+
if (info.alarm.getType() == AlarmInfo.AlarmInfoType.SPEED_CAMERA) {
201+
FrameLayout.LayoutParams widgetTextLayoutParams = new FrameLayout.LayoutParams(
202+
res.getDimensionPixelSize(R.dimen.map_alarm_speed_camera_speed_info_size),
203+
res.getDimensionPixelSize(R.dimen.map_alarm_speed_camera_speed_info_size));
204+
205+
widgetTextLayoutParams.gravity = Gravity.RIGHT | Gravity.TOP;
206+
207+
widgetText.setLayoutParams(widgetTextLayoutParams);
208+
widgetText.setGravity(Gravity.CENTER);
209+
widgetText.setTextSize(COMPLEX_UNIT_PX, res.getDimensionPixelSize(R.dimen.map_alarm_speed_camera_speed_info_text_size));
210+
} else {
211+
FrameLayout.LayoutParams widgetTextLayoutParams = new FrameLayout.LayoutParams(
212+
FrameLayout.LayoutParams.WRAP_CONTENT,
213+
FrameLayout.LayoutParams.WRAP_CONTENT);
214+
215+
widgetTextLayoutParams.gravity = Gravity.CENTER;
216+
217+
widgetText.setLayoutParams(widgetTextLayoutParams);
218+
widgetText.setGravity(Gravity.NO_GRAVITY);
219+
widgetText.setTextSize(COMPLEX_UNIT_PX, res.getDimensionPixelSize(R.dimen.map_alarm_text_size));
220+
}
221+
}
222+
223+
private void updateBottomTextWidget(@NonNull AlarmWidgetInfo info) {
224+
if (layout == null || widgetBottomText == null) {
225+
return;
226+
}
227+
228+
widgetBottomText.setText(info.bottomText);
229+
Resources res = layout.getContext().getResources();
230+
231+
if (info.alarm.getType() == AlarmInfo.AlarmInfoType.SPEED_LIMIT && info.isCanadianRegion) {
232+
int bottomPadding = res.getDimensionPixelSize(R.dimen.map_button_margin);
233+
widgetBottomText.setPadding(0, 0, 0, bottomPadding);
234+
widgetBottomText.setTextSize(COMPLEX_UNIT_PX, res.getDimensionPixelSize(R.dimen.map_alarm_bottom_si_text_size));
235+
} else {
236+
widgetBottomText.setPadding(0, 0, 0, 0);
237+
widgetBottomText.setTextSize(COMPLEX_UNIT_PX, res.getDimensionPixelSize(R.dimen.map_alarm_bottom_text_size));
238+
}
239+
widgetBottomText.setTextColor(ContextCompat.getColor(layout.getContext(),
240+
info.americanType ? R.color.color_black : R.color.color_white));
241+
}
242+
237243
@NonNull
238244
private Bitmap createWidgetBitmap(@NonNull AlarmWidgetInfo info, float density) {
239245
Bitmap bitmap = Bitmap.createBitmap((int) (WIDGET_BITMAP_SIZE_DP * density),

OsmAnd/src/net/osmand/plus/voice/CommandBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public List<String> getCommandsList() {
7070

7171
public abstract CommandBuilder speedAlarm(int maxSpeed, float speed);
7272

73-
public abstract CommandBuilder speedCameraAlarm(double dist, int maxSpeed);
73+
public abstract CommandBuilder speedCameraAlarm(double dist, int maxSpeed, String fallbackAttentionType);
7474

7575
public abstract CommandBuilder attention(String type);
7676

OsmAnd/src/net/osmand/plus/voice/JsCommandBuilder.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ public CommandBuilder speedAlarm(int maxSpeed, float speed) {
8080
}
8181

8282
@Override
83-
public CommandBuilder speedCameraAlarm(double dist, int maxSpeed) {
84-
return addCommand(C_SPEED_CAMERA_ALARM, dist, maxSpeed);
83+
public CommandBuilder speedCameraAlarm(double dist, int maxSpeed, String fallbackAttentionType) {
84+
return isJsCommandExists(C_SPEED_CAMERA_ALARM)
85+
? addCommand(C_SPEED_CAMERA_ALARM, dist, maxSpeed)
86+
: attention(fallbackAttentionType);
8587
}
8688

8789
@Override

0 commit comments

Comments
 (0)