Skip to content

Commit c70a074

Browse files
committed
osmandapp#8108, osmandapp#3721 Code review fixes part 2:
- Refactor AlarmInfo to always accept maxSpeed in meters per second, and provide it either in meters per second or in accordance with requested speed constants settings.
1 parent 7bd8351 commit c70a074

File tree

5 files changed

+31
-38
lines changed

5 files changed

+31
-38
lines changed

OsmAnd/src/net/osmand/plus/helpers/WaypointHelper.java

+7-13
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@ public AlarmInfo getMostImportantAlarm(SpeedConstants sc, boolean showCameras) {
225225
Location lastProjection = app.getRoutingHelper().getLastProjection();
226226
float mxspeed = route.getCurrentMaxSpeed();
227227
float delta = app.getSettings().SPEED_LIMIT_EXCEED_KMH.get() / 3.6f;
228-
AlarmInfo speedAlarm = createSpeedAlarm(sc, mxspeed, lastProjection, delta);
228+
AlarmInfo speedAlarm = createSpeedAlarm(mxspeed, lastProjection, delta);
229229
if (speedAlarm != null) {
230-
getVoiceRouter().announceSpeedAlarm(speedAlarm.getIntValue(), lastProjection.getSpeed());
230+
getVoiceRouter().announceSpeedAlarm(speedAlarm.getMaxSpeed(sc), lastProjection.getSpeed());
231231
}
232232
AlarmInfo mostImportant = speedAlarm;
233233
int value = speedAlarm != null ? speedAlarm.updateDistanceAndGetPriority(0, 0) : Integer.MAX_VALUE;
@@ -329,9 +329,9 @@ public AlarmInfo calculateMostImportantAlarm(RouteDataObject ro, Location loc, M
329329
SpeedConstants sc, boolean showCameras) {
330330
float mxspeed = ro.getMaximumSpeed(ro.bearingVsRouteDirection(loc));
331331
float delta = app.getSettings().SPEED_LIMIT_EXCEED_KMH.get() / 3.6f;
332-
AlarmInfo speedAlarm = createSpeedAlarm(sc, mxspeed, loc, delta);
332+
AlarmInfo speedAlarm = createSpeedAlarm(mxspeed, loc, delta);
333333
if (speedAlarm != null) {
334-
getVoiceRouter().announceSpeedAlarm(speedAlarm.getIntValue(), loc.getSpeed());
334+
getVoiceRouter().announceSpeedAlarm(speedAlarm.getMaxSpeed(sc), loc.getSpeed());
335335
return speedAlarm;
336336
}
337337
for (int i = 0; i < ro.getPointsLength(); i++) {
@@ -366,17 +366,11 @@ public AlarmInfo calculateMostImportantAlarm(RouteDataObject ro, Location loc, M
366366
return null;
367367
}
368368

369-
private static AlarmInfo createSpeedAlarm(SpeedConstants sc, float mxspeed, Location loc, float delta) {
369+
private static AlarmInfo createSpeedAlarm(float mxspeed, Location loc, float delta) {
370370
AlarmInfo speedAlarm = null;
371371
if (mxspeed != 0 && loc != null && loc.hasSpeed() && mxspeed != RouteDataObject.NONE_MAX_SPEED) {
372372
if (loc.getSpeed() > mxspeed + delta) {
373-
int speed;
374-
if (sc.imperial) {
375-
speed = Math.round(mxspeed * 3.6f / 1.6f);
376-
} else {
377-
speed = Math.round(mxspeed * 3.6f);
378-
}
379-
speedAlarm = AlarmInfo.createSpeedLimit(speed, loc);
373+
speedAlarm = AlarmInfo.createSpeedLimit(mxspeed, loc);
380374
}
381375
}
382376
return speedAlarm;
@@ -493,7 +487,7 @@ public void announceVisibleLocations() {
493487
// Set actual distance and copy max speed to speed camera
494488
if (alarmCopy.getType() == AlarmInfoType.SPEED_CAMERA) {
495489
alarmCopy.setFloatValue(route.getDistanceToPoint(alarm.getLocationIndex()));
496-
alarmCopy.setIntValue(alarm.getIntValue());
490+
alarmCopy.setMaxSpeed(alarm.getMaxSpeed());
497491
}
498492

499493
voiceRouter.announceAlarm(alarmCopy, lastKnownLocation.getSpeed());

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

+15-12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import net.osmand.data.LocationPoint;
77
import net.osmand.data.PointDescription;
88
import net.osmand.plus.R;
9+
import net.osmand.plus.settings.enums.SpeedConstants;
910

1011
public class AlarmInfo implements LocationPoint {
1112
public enum AlarmInfoType {
@@ -42,7 +43,7 @@ public String getVisualName(Context ctx) {
4243
private final AlarmInfoType type;
4344
protected final int locationIndex;
4445
private int lastLocationIndex = -1;
45-
private int intValue;
46+
private float maxSpeed;
4647
private float floatValue;
4748
private double latitude;
4849
private double longitude;
@@ -64,7 +65,17 @@ public float getFloatValue() {
6465
public void setFloatValue(float floatValue) {
6566
this.floatValue = floatValue;
6667
}
67-
68+
69+
public int getMaxSpeed(SpeedConstants sc) {
70+
return sc.imperial
71+
? Math.round(getMaxSpeed() * 3.6f / 1.6f)
72+
: Math.round(getMaxSpeed() * 3.6f);
73+
}
74+
75+
public float getMaxSpeed() { return maxSpeed; }
76+
77+
public void setMaxSpeed(float maxSpeed) { this.maxSpeed = maxSpeed; }
78+
6879
@Override
6980
public double getLatitude() {
7081
return latitude;
@@ -75,10 +86,6 @@ public double getLongitude() {
7586
return longitude;
7687
}
7788

78-
public int getIntValue() {
79-
return intValue;
80-
}
81-
8289
public int getLocationIndex() {
8390
return locationIndex;
8491
}
@@ -91,14 +98,10 @@ public void setLastLocationIndex(int lastLocationIndex) {
9198
this.lastLocationIndex = lastLocationIndex;
9299
}
93100

94-
public void setIntValue(int intValue) {
95-
this.intValue = intValue;
96-
}
97-
98-
public static AlarmInfo createSpeedLimit(int speed, Location loc){
101+
public static AlarmInfo createSpeedLimit(float maxSpeed, Location loc){
99102
AlarmInfo info = new AlarmInfo(AlarmInfoType.SPEED_LIMIT, 0);
100103
info.setLatLon(loc.getLatitude(), loc.getLongitude());
101-
info.setIntValue(speed);
104+
info.setMaxSpeed(maxSpeed);
102105
return info;
103106
}
104107

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ private static double getDistanceToLocation(List<Location> locations, LatLon p,
273273
locations.get(currentLocation).getLatitude(), locations.get(currentLocation).getLongitude());
274274
}
275275

276-
private static void attachAlarmInfo(List<AlarmInfo> alarms, RouteSegmentResult res, int intId, int locInd, SpeedConstants sc) {
276+
private static void attachAlarmInfo(List<AlarmInfo> alarms, RouteSegmentResult res, int intId, int locInd) {
277277
int[] pointTypes = res.getObject().getPointTypes(intId);
278278
if (pointTypes != null) {
279279
RouteRegion reg = res.getObject().region;
@@ -308,11 +308,7 @@ private static void attachAlarmInfo(List<AlarmInfo> alarms, RouteSegmentResult r
308308
}
309309

310310
if (maxSpeed > 0 && maxSpeed != RouteDataObject.NONE_MAX_SPEED) {
311-
if (sc.imperial) {
312-
speedCameraAlarmInfo.setIntValue(Math.round(maxSpeed * 3.6f / 1.6f));
313-
} else {
314-
speedCameraAlarmInfo.setIntValue(Math.round(maxSpeed * 3.6f));
315-
}
311+
speedCameraAlarmInfo.setMaxSpeed(maxSpeed);
316312
}
317313
}
318314
}
@@ -416,7 +412,7 @@ private static List<RouteSegmentResult> convertVectorResult(List<RouteDirectionI
416412
lastHeight = h;
417413
}
418414
locations.add(n);
419-
attachAlarmInfo(alarms, s, i, locations.size(), ctx.getSettings().SPEED_SYSTEM.get());
415+
attachAlarmInfo(alarms, s, i, locations.size());
420416
segmentsToPopulate.add(s);
421417
if (i == s.getEndPointIndex()) {
422418
break;

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,15 @@ protected String getText(Location location, List<LocationPointWrapper> points, d
319319

320320
public void announceAlarm(AlarmInfo info, float speed) {
321321
AlarmInfoType type = info.getType();
322+
OsmandSettings settings = router.getSettings();
323+
322324
if (type == AlarmInfoType.SPEED_LIMIT) {
323-
announceSpeedAlarm(info.getIntValue(), speed);
325+
announceSpeedAlarm(info.getMaxSpeed(settings.SPEED_SYSTEM.get()), speed);
324326
} else if (type == AlarmInfoType.SPEED_CAMERA) {
325-
OsmandSettings settings = router.getSettings();
326327
if (settings.SPEAK_SPEED_CAMERA.get()) {
327-
announceSpeedCameraAlarm(info.getFloatValue(), info.getIntValue());
328+
announceSpeedCameraAlarm(info.getFloatValue(), info.getMaxSpeed(settings.SPEED_SYSTEM.get()));
328329
}
329330
} else {
330-
OsmandSettings settings = router.getSettings();
331331
boolean speakTrafficWarnings = settings.SPEAK_TRAFFIC_WARNINGS.get();
332332
boolean speakTunnels = type == AlarmInfoType.TUNNEL && settings.SPEAK_TUNNELS.get();
333333
boolean speakPedestrian = type == AlarmInfoType.PEDESTRIAN && settings.SPEAK_PEDESTRIAN.get();

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,9 @@ public AlarmWidgetInfo createWidgetInfo(@NonNull AlarmInfo alarm) {
316316
locImgId = R.drawable.warnings_speed_limit_us;
317317
//else case is done by drawing red ring
318318
}
319-
text = alarm.getIntValue() + "";
319+
text = alarm.getMaxSpeed(settings.SPEED_SYSTEM.get()) + "";
320320
} else if (alarm.getType() == AlarmInfo.AlarmInfoType.SPEED_CAMERA) {
321-
int maxSpeed = alarm.getIntValue();
321+
int maxSpeed = alarm.getMaxSpeed(settings.SPEED_SYSTEM.get());
322322
if (maxSpeed > 0) {
323323
if (americanType) {
324324
locImgId = R.drawable.warnings_speed_camera_dist_lim_us;

0 commit comments

Comments
 (0)