From aba5c382af0fae15bbc7c8cb42dabb32d2f0f35e Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 18 Jun 2012 23:39:08 +0200 Subject: [PATCH] Fix NPE correctly --- .../activities/ShowRouteInfoActivity.java | 2 +- .../plus/routing/RouteCalculationResult.java | 10 ++-- .../plus/routing/RouteDirectionInfo.java | 9 ++-- .../osmand/plus/routing/RouteProvider.java | 52 ++++++++----------- .../net/osmand/plus/routing/VoiceRouter.java | 36 ++++++------- .../net/osmand/plus/views/MapInfoLayer.java | 10 ++-- 6 files changed, 57 insertions(+), 62 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/activities/ShowRouteInfoActivity.java b/OsmAnd/src/net/osmand/plus/activities/ShowRouteInfoActivity.java index 72889cae2e..59a7034d8a 100644 --- a/OsmAnd/src/net/osmand/plus/activities/ShowRouteInfoActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/ShowRouteInfoActivity.java @@ -116,7 +116,7 @@ public class ShowRouteInfoActivity extends OsmandListActivity { if(!(icon.getDrawable() instanceof TurnPathHelper.RouteDrawable)){ icon.setImageDrawable(new TurnPathHelper.RouteDrawable(getResources())); } - ((TurnPathHelper.RouteDrawable) icon.getDrawable()).setRouteType(model.turnType); + ((TurnPathHelper.RouteDrawable) icon.getDrawable()).setRouteType(model.getTurnType()); distanceLabel.setText(OsmAndFormatter.getFormattedDistance(model.distance, ShowRouteInfoActivity.this)); label.setText(model.descriptionRoute); int seconds = model.getExpectedTime() % 60; diff --git a/OsmAnd/src/net/osmand/plus/routing/RouteCalculationResult.java b/OsmAnd/src/net/osmand/plus/routing/RouteCalculationResult.java index 935a48b4f2..e0ff3b70a5 100644 --- a/OsmAnd/src/net/osmand/plus/routing/RouteCalculationResult.java +++ b/OsmAnd/src/net/osmand/plus/routing/RouteCalculationResult.java @@ -49,7 +49,7 @@ public class RouteCalculationResult { RouteSegmentResult s = list.get(routeInd); boolean plus = s.getStartPointIndex() < s.getEndPointIndex(); if (routeInd > 0) { - RouteDirectionInfo info = new RouteDirectionInfo(s.getSegmentSpeed()); +// RouteDirectionInfo info = new RouteDirectionInfo(s.getSegmentSpeed()); // String stype = item.getExtensionsToRead().get("turn"); //$NON-NLS-1$ // if (stype != null) { // dirInfo.turnType = TurnType.valueOf(stype.toUpperCase(), leftSide); @@ -60,7 +60,7 @@ public class RouteCalculationResult { // if (sturn != null) { // dirInfo.turnType.setTurnAngle((float) Double.parseDouble(sturn)); // } - directions.add(info); +// directions.add(info); } int i = s.getStartPointIndex(); while (true) { @@ -98,7 +98,7 @@ public class RouteCalculationResult { if (directions != null && directions.size() > 1) { for (int i = 1; i < directions.size();) { RouteDirectionInfo r = directions.get(i); - if (r.turnType.getValue().equals(TurnType.C)) { + if (r.getTurnType().getValue().equals(TurnType.C)) { RouteDirectionInfo prev = directions.get(i - 1); prev.setAverageSpeed((prev.distance + r.distance) / (prev.distance / prev.getAverageSpeed() + r.distance / r.getAverageSpeed())); @@ -144,8 +144,8 @@ public class RouteCalculationResult { for (RouteDirectionInfo i : directions) { i.routePointOffset++; } - RouteDirectionInfo info = new RouteDirectionInfo(directions.get(0).getAverageSpeed()); - info.turnType = TurnType.valueOf(TurnType.C, false); + RouteDirectionInfo info = new RouteDirectionInfo(directions.get(0).getAverageSpeed(), + TurnType.valueOf(TurnType.C, false)); info.routePointOffset = 0; info.descriptionRoute = "";//getString(ctx, R.string.route_head); //$NON-NLS-1$ directions.add(0, info); diff --git a/OsmAnd/src/net/osmand/plus/routing/RouteDirectionInfo.java b/OsmAnd/src/net/osmand/plus/routing/RouteDirectionInfo.java index 7f88f1e98d..e5d5b4a0e4 100644 --- a/OsmAnd/src/net/osmand/plus/routing/RouteDirectionInfo.java +++ b/OsmAnd/src/net/osmand/plus/routing/RouteDirectionInfo.java @@ -5,7 +5,7 @@ public class RouteDirectionInfo { private float averageSpeed; // Constructor to verify average speed always > 0 - public RouteDirectionInfo(float averageSpeed) { + public RouteDirectionInfo(float averageSpeed, TurnType turnType) { this.averageSpeed = averageSpeed == 0 ? 1 : averageSpeed; } @@ -23,8 +23,11 @@ public class RouteDirectionInfo { return (int) (distance / averageSpeed); } - // FIXME How it can be null? (fix by constructor and revert MapInfoLayer) - public TurnType turnType; + private TurnType turnType; + + public TurnType getTurnType() { + return turnType; + } // location when you should action (turn or go ahead) public int routePointOffset; diff --git a/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java b/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java index 9942694bbb..381b6c8f86 100644 --- a/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java +++ b/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java @@ -220,10 +220,9 @@ public class RouteProvider { List subdirections = new ArrayList(); for (RouteDirectionInfo info : params.directions) { if(info.routePointOffset >= startI && info.routePointOffset < endI){ - RouteDirectionInfo ch = new RouteDirectionInfo(info.getAverageSpeed()); + RouteDirectionInfo ch = new RouteDirectionInfo(info.getAverageSpeed(), info.getTurnType()); ch.routePointOffset = info.routePointOffset - startI; ch.descriptionRoute = info.descriptionRoute; - ch.turnType = info.turnType; // recalculate ch.distance = 0; @@ -268,9 +267,7 @@ public class RouteProvider { int previousLocation = 0; int prevBearingLocation = 0; - RouteDirectionInfo previousInfo = new RouteDirectionInfo(speed); - - previousInfo.turnType = TurnType.valueOf(TurnType.C, leftSide); + RouteDirectionInfo previousInfo = new RouteDirectionInfo(speed, TurnType.valueOf(TurnType.C, leftSide)); previousInfo.routePointOffset = 0; previousInfo.descriptionRoute = getString(ctx, R.string.route_head); directions.add(previousInfo); @@ -348,10 +345,8 @@ public class RouteProvider { // calculate for previousRoute previousInfo.distance = listDistance[previousLocation]- listDistance[i]; previousInfo.descriptionRoute += " " + OsmAndFormatter.getFormattedDistance(previousInfo.distance, ctx); //$NON-NLS-1$ - - previousInfo = new RouteDirectionInfo(speed); - previousInfo.turnType = type; - previousInfo.turnType.setTurnAngle(360 - delta); + type.setTurnAngle(360 - delta); + previousInfo = new RouteDirectionInfo(speed, type); previousInfo.descriptionRoute = description; previousInfo.routePointOffset = startTurnPoint; directions.add(previousInfo); @@ -367,10 +362,9 @@ public class RouteProvider { // add last direction go straight (to show arrow in screen after all turns) if(previousInfo.distance > 80){ - RouteDirectionInfo info = new RouteDirectionInfo(speed); + RouteDirectionInfo info = new RouteDirectionInfo(speed, TurnType.valueOf(TurnType.C, leftSide)); info.distance = 0; info.descriptionRoute = ""; //$NON-NLS-1$ - info.turnType = TurnType.valueOf(TurnType.C, leftSide); info.routePointOffset = locations.size() - 1; directions.add(info); } @@ -603,29 +597,27 @@ public class RouteProvider { if(!iterator.hasNext() && time > 0) { avgSpeed = distanceToEnd[offset] / time; } - - RouteDirectionInfo dirInfo = new RouteDirectionInfo(avgSpeed); - dirInfo.descriptionRoute = item.desc; //$NON-NLS-1$ - String stype = item.getExtensionsToRead().get("turn"); //$NON-NLS-1$ + TurnType turnType; if (stype != null) { - dirInfo.turnType = TurnType.valueOf(stype.toUpperCase(), leftSide); + turnType = TurnType.valueOf(stype.toUpperCase(), leftSide); } else { - dirInfo.turnType = TurnType.valueOf(TurnType.C, leftSide); + turnType = TurnType.valueOf(TurnType.C, leftSide); } String sturn = item.getExtensionsToRead().get("turn-angle"); //$NON-NLS-1$ if (sturn != null) { - dirInfo.turnType.setTurnAngle((float) Double.parseDouble(sturn)); + turnType.setTurnAngle((float) Double.parseDouble(sturn)); } + RouteDirectionInfo dirInfo = new RouteDirectionInfo(avgSpeed, turnType); + dirInfo.descriptionRoute = item.desc; //$NON-NLS-1$ dirInfo.routePointOffset = offset; - - if (previous != null && previous.turnType != null && !TurnType.C.equals(previous.turnType.getValue()) && + if (previous != null && !TurnType.C.equals(previous.getTurnType().getValue()) && !osmandRouter) { // calculate angle if (previous.routePointOffset > 0) { float paz = res.get(previous.routePointOffset - 1).bearingTo(res.get(previous.routePointOffset)); float caz; - if (previous.turnType.isRoundAbout() && dirInfo.routePointOffset < res.size() - 1) { + if (previous.getTurnType().isRoundAbout() && dirInfo.routePointOffset < res.size() - 1) { caz = res.get(dirInfo.routePointOffset).bearingTo(res.get(dirInfo.routePointOffset + 1)); } else { caz = res.get(dirInfo.routePointOffset - 1).bearingTo(res.get(dirInfo.routePointOffset)); @@ -639,8 +631,8 @@ public class RouteProvider { // that magic number helps to fix some errors for turn angle += 75; - if (previous.turnType.getTurnAngle() < 0.5f) { - previous.turnType.setTurnAngle(angle); + if (previous.getTurnType().getTurnAngle() < 0.5f) { + previous.getTurnType().setTurnAngle(angle); } } } @@ -655,7 +647,7 @@ public class RouteProvider { } } } - if (previous != null && previous.turnType != null && !TurnType.C.equals(previous.turnType.getValue())) { + if (previous != null && !TurnType.C.equals(previous.getTurnType().getValue())) { // calculate angle if (previous.routePointOffset > 0 && previous.routePointOffset < res.size() - 1) { float paz = res.get(previous.routePointOffset - 1).bearingTo(res.get(previous.routePointOffset)); @@ -664,8 +656,8 @@ public class RouteProvider { if (angle < 0) { angle += 360; } - if (previous.turnType.getTurnAngle() < 0.5f) { - previous.turnType.setTurnAngle(angle); + if (previous.getTurnType().getTurnAngle() < 0.5f) { + previous.getTurnType().setTurnAngle(angle); } } } @@ -781,13 +773,13 @@ public class RouteProvider { pt.desc = dirInfo.descriptionRoute; Map extensions = pt.getExtensionsToWrite(); extensions.put("time", dirInfo.getExpectedTime() + ""); - String turnType = dirInfo.turnType.getValue(); - if (dirInfo.turnType.isRoundAbout()) { - turnType += dirInfo.turnType.getExitOut(); + String turnType = dirInfo.getTurnType().getValue(); + if (dirInfo.getTurnType().isRoundAbout()) { + turnType += dirInfo.getTurnType().getExitOut(); } if(!TurnType.C.equals(turnType)){ extensions.put("turn", turnType); - extensions.put("turn-angle", dirInfo.turnType.getTurnAngle() + ""); + extensions.put("turn-angle", dirInfo.getTurnType().getTurnAngle() + ""); } extensions.put("offset", (dirInfo.routePointOffset - cRoute) + ""); route.points.add(pt); diff --git a/OsmAnd/src/net/osmand/plus/routing/VoiceRouter.java b/OsmAnd/src/net/osmand/plus/routing/VoiceRouter.java index 1b69415bc4..f7b3216904 100644 --- a/OsmAnd/src/net/osmand/plus/routing/VoiceRouter.java +++ b/OsmAnd/src/net/osmand/plus/routing/VoiceRouter.java @@ -306,12 +306,12 @@ public class VoiceRouter { private void playPrepareTurn(RouteDirectionInfo next, int dist) { CommandBuilder play = getNewCommandPlayerToPlay(); if(play != null){ - String tParam = getTurnType(next.turnType); + String tParam = getTurnType(next.getTurnType()); if(tParam != null){ play.prepareTurn(tParam, dist).play(); - } else if(next.turnType.isRoundAbout()){ + } else if(next.getTurnType().isRoundAbout()){ play.prepareRoundAbout(dist).play(); - } else if(next.turnType.getValue().equals(TurnType.TU) || next.turnType.getValue().equals(TurnType.TRU)){ + } else if(next.getTurnType().getValue().equals(TurnType.TU) || next.getTurnType().getValue().equals(TurnType.TRU)){ play.prepareMakeUT(dist).play(); } } @@ -320,22 +320,22 @@ public class VoiceRouter { private void playMakeTurnIn(RouteDirectionInfo next, int dist, RouteDirectionInfo pronounceNextNext) { CommandBuilder play = getNewCommandPlayerToPlay(); if (play != null) { - String tParam = getTurnType(next.turnType); + String tParam = getTurnType(next.getTurnType()); boolean isPlay = true; if (tParam != null) { play.turn(tParam, dist); - } else if (next.turnType.isRoundAbout()) { - play.roundAbout(dist, next.turnType.getTurnAngle(), next.turnType.getExitOut()); - } else if (next.turnType.getValue().equals(TurnType.TU) || next.turnType.getValue().equals(TurnType.TRU)) { + } else if (next.getTurnType().isRoundAbout()) { + play.roundAbout(dist, next.getTurnType().getTurnAngle(), next.getTurnType().getExitOut()); + } else if (next.getTurnType().getValue().equals(TurnType.TU) || next.getTurnType().getValue().equals(TurnType.TRU)) { play.makeUT(dist); } else { isPlay = false; } // small preparation to next after next if (pronounceNextNext != null) { - TurnType t = pronounceNextNext.turnType; + TurnType t = pronounceNextNext.getTurnType(); isPlay = true; - if (next.turnType.getValue().equals(TurnType.C) && + if (next.getTurnType().getValue().equals(TurnType.C) && !TurnType.C.equals(t.getValue())) { play.goAhead(dist); } @@ -355,30 +355,30 @@ public class VoiceRouter { private void playMakeTurn(RouteDirectionInfo next, RouteDirectionInfo nextNext) { CommandBuilder play = getNewCommandPlayerToPlay(); if(play != null){ - String tParam = getTurnType(next.turnType); + String tParam = getTurnType(next.getTurnType()); boolean isplay = true; if(tParam != null){ play.turn(tParam); - } else if(next.turnType.isRoundAbout()){ - play.roundAbout(next.turnType.getTurnAngle(), next.turnType.getExitOut()); - } else if(next.turnType.getValue().equals(TurnType.TU) || next.turnType.getValue().equals(TurnType.TRU)){ + } else if(next.getTurnType().isRoundAbout()){ + play.roundAbout(next.getTurnType().getTurnAngle(), next.getTurnType().getExitOut()); + } else if(next.getTurnType().getValue().equals(TurnType.TU) || next.getTurnType().getValue().equals(TurnType.TRU)){ play.makeUT(); // do not say it -// } else if(next.turnType.getValue().equals(TurnType.C)){ +// } else if(next.getTurnType().getValue().equals(TurnType.C)){ // play.goAhead(); } else { isplay = false; } // add turn after next if (nextNext != null) { - String t2Param = getTurnType(nextNext.turnType); + String t2Param = getTurnType(nextNext.getTurnType()); if (t2Param != null) { if(isplay) { play.then(); } play.turn(t2Param, next.distance); - } else if (nextNext.turnType.isRoundAbout()) { + } else if (nextNext.getTurnType().isRoundAbout()) { if(isplay) { play.then(); } - play.roundAbout(next.distance, nextNext.turnType.getTurnAngle(), nextNext.turnType.getExitOut()); - } else if (nextNext.turnType.getValue().equals(TurnType.TU)) { + play.roundAbout(next.distance, nextNext.getTurnType().getTurnAngle(), nextNext.getTurnType().getExitOut()); + } else if (nextNext.getTurnType().getValue().equals(TurnType.TU)) { if(isplay) { play.then(); } play.makeUT(next.distance); } diff --git a/OsmAnd/src/net/osmand/plus/views/MapInfoLayer.java b/OsmAnd/src/net/osmand/plus/views/MapInfoLayer.java index 32f5087b97..2a27b742b6 100644 --- a/OsmAnd/src/net/osmand/plus/views/MapInfoLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/MapInfoLayer.java @@ -472,8 +472,8 @@ public class MapInfoLayer extends OsmandMapLayer { turnType = null; invalidate(); } - } else if (!Algoritms.objectEquals(turnType, next.turnType)) { - turnType = next.turnType; + } else if (!Algoritms.objectEquals(turnType, next.getTurnType())) { + turnType = next.getTurnType(); TurnPathHelper.calcTurnPath(pathForTurn, turnType, pathTransform); invalidate(); } @@ -544,13 +544,13 @@ public class MapInfoLayer extends OsmandMapLayer { if (d >= 0 && !showMiniMap) { visible = true; RouteDirectionInfo next = routeLayer.getHelper().getNextRouteDirectionInfo(); - if (next == null || next.turnType == null) { + if (next == null) { if (turnType != null) { turnType = null; invalidate(); } - } else if (!Algoritms.objectEquals(turnType, next.turnType)) { - turnType = next.turnType; + } else if (!Algoritms.objectEquals(turnType, next.getTurnType())) { + turnType = next.getTurnType(); TurnPathHelper.calcTurnPath(pathForTurn, turnType, pathTransform); if (turnType.getExitOut() > 0) { exitOut = turnType.getExitOut() + ""; //$NON-NLS-1$