Fix NPE correctly

This commit is contained in:
Victor Shcherb 2012-06-18 23:39:08 +02:00
parent d87ec826c8
commit aba5c382af
6 changed files with 57 additions and 62 deletions

View file

@ -116,7 +116,7 @@ public class ShowRouteInfoActivity extends OsmandListActivity {
if(!(icon.getDrawable() instanceof TurnPathHelper.RouteDrawable)){ if(!(icon.getDrawable() instanceof TurnPathHelper.RouteDrawable)){
icon.setImageDrawable(new TurnPathHelper.RouteDrawable(getResources())); 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)); distanceLabel.setText(OsmAndFormatter.getFormattedDistance(model.distance, ShowRouteInfoActivity.this));
label.setText(model.descriptionRoute); label.setText(model.descriptionRoute);
int seconds = model.getExpectedTime() % 60; int seconds = model.getExpectedTime() % 60;

View file

@ -49,7 +49,7 @@ public class RouteCalculationResult {
RouteSegmentResult s = list.get(routeInd); RouteSegmentResult s = list.get(routeInd);
boolean plus = s.getStartPointIndex() < s.getEndPointIndex(); boolean plus = s.getStartPointIndex() < s.getEndPointIndex();
if (routeInd > 0) { if (routeInd > 0) {
RouteDirectionInfo info = new RouteDirectionInfo(s.getSegmentSpeed()); // RouteDirectionInfo info = new RouteDirectionInfo(s.getSegmentSpeed());
// String stype = item.getExtensionsToRead().get("turn"); //$NON-NLS-1$ // String stype = item.getExtensionsToRead().get("turn"); //$NON-NLS-1$
// if (stype != null) { // if (stype != null) {
// dirInfo.turnType = TurnType.valueOf(stype.toUpperCase(), leftSide); // dirInfo.turnType = TurnType.valueOf(stype.toUpperCase(), leftSide);
@ -60,7 +60,7 @@ public class RouteCalculationResult {
// if (sturn != null) { // if (sturn != null) {
// dirInfo.turnType.setTurnAngle((float) Double.parseDouble(sturn)); // dirInfo.turnType.setTurnAngle((float) Double.parseDouble(sturn));
// } // }
directions.add(info); // directions.add(info);
} }
int i = s.getStartPointIndex(); int i = s.getStartPointIndex();
while (true) { while (true) {
@ -98,7 +98,7 @@ public class RouteCalculationResult {
if (directions != null && directions.size() > 1) { if (directions != null && directions.size() > 1) {
for (int i = 1; i < directions.size();) { for (int i = 1; i < directions.size();) {
RouteDirectionInfo r = directions.get(i); 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); RouteDirectionInfo prev = directions.get(i - 1);
prev.setAverageSpeed((prev.distance + r.distance) prev.setAverageSpeed((prev.distance + r.distance)
/ (prev.distance / prev.getAverageSpeed() + r.distance / r.getAverageSpeed())); / (prev.distance / prev.getAverageSpeed() + r.distance / r.getAverageSpeed()));
@ -144,8 +144,8 @@ public class RouteCalculationResult {
for (RouteDirectionInfo i : directions) { for (RouteDirectionInfo i : directions) {
i.routePointOffset++; i.routePointOffset++;
} }
RouteDirectionInfo info = new RouteDirectionInfo(directions.get(0).getAverageSpeed()); RouteDirectionInfo info = new RouteDirectionInfo(directions.get(0).getAverageSpeed(),
info.turnType = TurnType.valueOf(TurnType.C, false); TurnType.valueOf(TurnType.C, false));
info.routePointOffset = 0; info.routePointOffset = 0;
info.descriptionRoute = "";//getString(ctx, R.string.route_head); //$NON-NLS-1$ info.descriptionRoute = "";//getString(ctx, R.string.route_head); //$NON-NLS-1$
directions.add(0, info); directions.add(0, info);

View file

@ -5,7 +5,7 @@ public class RouteDirectionInfo {
private float averageSpeed; private float averageSpeed;
// Constructor to verify average speed always > 0 // Constructor to verify average speed always > 0
public RouteDirectionInfo(float averageSpeed) { public RouteDirectionInfo(float averageSpeed, TurnType turnType) {
this.averageSpeed = averageSpeed == 0 ? 1 : averageSpeed; this.averageSpeed = averageSpeed == 0 ? 1 : averageSpeed;
} }
@ -23,8 +23,11 @@ public class RouteDirectionInfo {
return (int) (distance / averageSpeed); return (int) (distance / averageSpeed);
} }
// FIXME How it can be null? (fix by constructor and revert MapInfoLayer) private TurnType turnType;
public TurnType turnType;
public TurnType getTurnType() {
return turnType;
}
// location when you should action (turn or go ahead) // location when you should action (turn or go ahead)
public int routePointOffset; public int routePointOffset;

View file

@ -220,10 +220,9 @@ public class RouteProvider {
List<RouteDirectionInfo> subdirections = new ArrayList<RouteDirectionInfo>(); List<RouteDirectionInfo> subdirections = new ArrayList<RouteDirectionInfo>();
for (RouteDirectionInfo info : params.directions) { for (RouteDirectionInfo info : params.directions) {
if(info.routePointOffset >= startI && info.routePointOffset < endI){ 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.routePointOffset = info.routePointOffset - startI;
ch.descriptionRoute = info.descriptionRoute; ch.descriptionRoute = info.descriptionRoute;
ch.turnType = info.turnType;
// recalculate // recalculate
ch.distance = 0; ch.distance = 0;
@ -268,9 +267,7 @@ public class RouteProvider {
int previousLocation = 0; int previousLocation = 0;
int prevBearingLocation = 0; int prevBearingLocation = 0;
RouteDirectionInfo previousInfo = new RouteDirectionInfo(speed); RouteDirectionInfo previousInfo = new RouteDirectionInfo(speed, TurnType.valueOf(TurnType.C, leftSide));
previousInfo.turnType = TurnType.valueOf(TurnType.C, leftSide);
previousInfo.routePointOffset = 0; previousInfo.routePointOffset = 0;
previousInfo.descriptionRoute = getString(ctx, R.string.route_head); previousInfo.descriptionRoute = getString(ctx, R.string.route_head);
directions.add(previousInfo); directions.add(previousInfo);
@ -348,10 +345,8 @@ public class RouteProvider {
// calculate for previousRoute // calculate for previousRoute
previousInfo.distance = listDistance[previousLocation]- listDistance[i]; previousInfo.distance = listDistance[previousLocation]- listDistance[i];
previousInfo.descriptionRoute += " " + OsmAndFormatter.getFormattedDistance(previousInfo.distance, ctx); //$NON-NLS-1$ previousInfo.descriptionRoute += " " + OsmAndFormatter.getFormattedDistance(previousInfo.distance, ctx); //$NON-NLS-1$
type.setTurnAngle(360 - delta);
previousInfo = new RouteDirectionInfo(speed); previousInfo = new RouteDirectionInfo(speed, type);
previousInfo.turnType = type;
previousInfo.turnType.setTurnAngle(360 - delta);
previousInfo.descriptionRoute = description; previousInfo.descriptionRoute = description;
previousInfo.routePointOffset = startTurnPoint; previousInfo.routePointOffset = startTurnPoint;
directions.add(previousInfo); directions.add(previousInfo);
@ -367,10 +362,9 @@ public class RouteProvider {
// add last direction go straight (to show arrow in screen after all turns) // add last direction go straight (to show arrow in screen after all turns)
if(previousInfo.distance > 80){ if(previousInfo.distance > 80){
RouteDirectionInfo info = new RouteDirectionInfo(speed); RouteDirectionInfo info = new RouteDirectionInfo(speed, TurnType.valueOf(TurnType.C, leftSide));
info.distance = 0; info.distance = 0;
info.descriptionRoute = ""; //$NON-NLS-1$ info.descriptionRoute = ""; //$NON-NLS-1$
info.turnType = TurnType.valueOf(TurnType.C, leftSide);
info.routePointOffset = locations.size() - 1; info.routePointOffset = locations.size() - 1;
directions.add(info); directions.add(info);
} }
@ -603,29 +597,27 @@ public class RouteProvider {
if(!iterator.hasNext() && time > 0) { if(!iterator.hasNext() && time > 0) {
avgSpeed = distanceToEnd[offset] / time; 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$ String stype = item.getExtensionsToRead().get("turn"); //$NON-NLS-1$
TurnType turnType;
if (stype != null) { if (stype != null) {
dirInfo.turnType = TurnType.valueOf(stype.toUpperCase(), leftSide); turnType = TurnType.valueOf(stype.toUpperCase(), leftSide);
} else { } else {
dirInfo.turnType = TurnType.valueOf(TurnType.C, leftSide); turnType = TurnType.valueOf(TurnType.C, leftSide);
} }
String sturn = item.getExtensionsToRead().get("turn-angle"); //$NON-NLS-1$ String sturn = item.getExtensionsToRead().get("turn-angle"); //$NON-NLS-1$
if (sturn != null) { 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; dirInfo.routePointOffset = offset;
if (previous != null && !TurnType.C.equals(previous.getTurnType().getValue()) &&
if (previous != null && previous.turnType != null && !TurnType.C.equals(previous.turnType.getValue()) &&
!osmandRouter) { !osmandRouter) {
// calculate angle // calculate angle
if (previous.routePointOffset > 0) { if (previous.routePointOffset > 0) {
float paz = res.get(previous.routePointOffset - 1).bearingTo(res.get(previous.routePointOffset)); float paz = res.get(previous.routePointOffset - 1).bearingTo(res.get(previous.routePointOffset));
float caz; 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)); caz = res.get(dirInfo.routePointOffset).bearingTo(res.get(dirInfo.routePointOffset + 1));
} else { } else {
caz = res.get(dirInfo.routePointOffset - 1).bearingTo(res.get(dirInfo.routePointOffset)); 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 // that magic number helps to fix some errors for turn
angle += 75; angle += 75;
if (previous.turnType.getTurnAngle() < 0.5f) { if (previous.getTurnType().getTurnAngle() < 0.5f) {
previous.turnType.setTurnAngle(angle); 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 // calculate angle
if (previous.routePointOffset > 0 && previous.routePointOffset < res.size() - 1) { if (previous.routePointOffset > 0 && previous.routePointOffset < res.size() - 1) {
float paz = res.get(previous.routePointOffset - 1).bearingTo(res.get(previous.routePointOffset)); float paz = res.get(previous.routePointOffset - 1).bearingTo(res.get(previous.routePointOffset));
@ -664,8 +656,8 @@ public class RouteProvider {
if (angle < 0) { if (angle < 0) {
angle += 360; angle += 360;
} }
if (previous.turnType.getTurnAngle() < 0.5f) { if (previous.getTurnType().getTurnAngle() < 0.5f) {
previous.turnType.setTurnAngle(angle); previous.getTurnType().setTurnAngle(angle);
} }
} }
} }
@ -781,13 +773,13 @@ public class RouteProvider {
pt.desc = dirInfo.descriptionRoute; pt.desc = dirInfo.descriptionRoute;
Map<String, String> extensions = pt.getExtensionsToWrite(); Map<String, String> extensions = pt.getExtensionsToWrite();
extensions.put("time", dirInfo.getExpectedTime() + ""); extensions.put("time", dirInfo.getExpectedTime() + "");
String turnType = dirInfo.turnType.getValue(); String turnType = dirInfo.getTurnType().getValue();
if (dirInfo.turnType.isRoundAbout()) { if (dirInfo.getTurnType().isRoundAbout()) {
turnType += dirInfo.turnType.getExitOut(); turnType += dirInfo.getTurnType().getExitOut();
} }
if(!TurnType.C.equals(turnType)){ if(!TurnType.C.equals(turnType)){
extensions.put("turn", 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) + ""); extensions.put("offset", (dirInfo.routePointOffset - cRoute) + "");
route.points.add(pt); route.points.add(pt);

View file

@ -306,12 +306,12 @@ public class VoiceRouter {
private void playPrepareTurn(RouteDirectionInfo next, int dist) { private void playPrepareTurn(RouteDirectionInfo next, int dist) {
CommandBuilder play = getNewCommandPlayerToPlay(); CommandBuilder play = getNewCommandPlayerToPlay();
if(play != null){ if(play != null){
String tParam = getTurnType(next.turnType); String tParam = getTurnType(next.getTurnType());
if(tParam != null){ if(tParam != null){
play.prepareTurn(tParam, dist).play(); play.prepareTurn(tParam, dist).play();
} else if(next.turnType.isRoundAbout()){ } else if(next.getTurnType().isRoundAbout()){
play.prepareRoundAbout(dist).play(); 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(); play.prepareMakeUT(dist).play();
} }
} }
@ -320,22 +320,22 @@ public class VoiceRouter {
private void playMakeTurnIn(RouteDirectionInfo next, int dist, RouteDirectionInfo pronounceNextNext) { private void playMakeTurnIn(RouteDirectionInfo next, int dist, RouteDirectionInfo pronounceNextNext) {
CommandBuilder play = getNewCommandPlayerToPlay(); CommandBuilder play = getNewCommandPlayerToPlay();
if (play != null) { if (play != null) {
String tParam = getTurnType(next.turnType); String tParam = getTurnType(next.getTurnType());
boolean isPlay = true; boolean isPlay = true;
if (tParam != null) { if (tParam != null) {
play.turn(tParam, dist); play.turn(tParam, dist);
} else if (next.turnType.isRoundAbout()) { } else if (next.getTurnType().isRoundAbout()) {
play.roundAbout(dist, next.turnType.getTurnAngle(), next.turnType.getExitOut()); play.roundAbout(dist, next.getTurnType().getTurnAngle(), next.getTurnType().getExitOut());
} 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.makeUT(dist); play.makeUT(dist);
} else { } else {
isPlay = false; isPlay = false;
} }
// small preparation to next after next // small preparation to next after next
if (pronounceNextNext != null) { if (pronounceNextNext != null) {
TurnType t = pronounceNextNext.turnType; TurnType t = pronounceNextNext.getTurnType();
isPlay = true; isPlay = true;
if (next.turnType.getValue().equals(TurnType.C) && if (next.getTurnType().getValue().equals(TurnType.C) &&
!TurnType.C.equals(t.getValue())) { !TurnType.C.equals(t.getValue())) {
play.goAhead(dist); play.goAhead(dist);
} }
@ -355,30 +355,30 @@ public class VoiceRouter {
private void playMakeTurn(RouteDirectionInfo next, RouteDirectionInfo nextNext) { private void playMakeTurn(RouteDirectionInfo next, RouteDirectionInfo nextNext) {
CommandBuilder play = getNewCommandPlayerToPlay(); CommandBuilder play = getNewCommandPlayerToPlay();
if(play != null){ if(play != null){
String tParam = getTurnType(next.turnType); String tParam = getTurnType(next.getTurnType());
boolean isplay = true; boolean isplay = true;
if(tParam != null){ if(tParam != null){
play.turn(tParam); play.turn(tParam);
} else if(next.turnType.isRoundAbout()){ } else if(next.getTurnType().isRoundAbout()){
play.roundAbout(next.turnType.getTurnAngle(), next.turnType.getExitOut()); play.roundAbout(next.getTurnType().getTurnAngle(), next.getTurnType().getExitOut());
} 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.makeUT(); play.makeUT();
// do not say it // do not say it
// } else if(next.turnType.getValue().equals(TurnType.C)){ // } else if(next.getTurnType().getValue().equals(TurnType.C)){
// play.goAhead(); // play.goAhead();
} else { } else {
isplay = false; isplay = false;
} }
// add turn after next // add turn after next
if (nextNext != null) { if (nextNext != null) {
String t2Param = getTurnType(nextNext.turnType); String t2Param = getTurnType(nextNext.getTurnType());
if (t2Param != null) { if (t2Param != null) {
if(isplay) { play.then(); } if(isplay) { play.then(); }
play.turn(t2Param, next.distance); play.turn(t2Param, next.distance);
} else if (nextNext.turnType.isRoundAbout()) { } else if (nextNext.getTurnType().isRoundAbout()) {
if(isplay) { play.then(); } if(isplay) { play.then(); }
play.roundAbout(next.distance, nextNext.turnType.getTurnAngle(), nextNext.turnType.getExitOut()); play.roundAbout(next.distance, nextNext.getTurnType().getTurnAngle(), nextNext.getTurnType().getExitOut());
} else if (nextNext.turnType.getValue().equals(TurnType.TU)) { } else if (nextNext.getTurnType().getValue().equals(TurnType.TU)) {
if(isplay) { play.then(); } if(isplay) { play.then(); }
play.makeUT(next.distance); play.makeUT(next.distance);
} }

View file

@ -472,8 +472,8 @@ public class MapInfoLayer extends OsmandMapLayer {
turnType = null; turnType = null;
invalidate(); invalidate();
} }
} else if (!Algoritms.objectEquals(turnType, next.turnType)) { } else if (!Algoritms.objectEquals(turnType, next.getTurnType())) {
turnType = next.turnType; turnType = next.getTurnType();
TurnPathHelper.calcTurnPath(pathForTurn, turnType, pathTransform); TurnPathHelper.calcTurnPath(pathForTurn, turnType, pathTransform);
invalidate(); invalidate();
} }
@ -544,13 +544,13 @@ public class MapInfoLayer extends OsmandMapLayer {
if (d >= 0 && !showMiniMap) { if (d >= 0 && !showMiniMap) {
visible = true; visible = true;
RouteDirectionInfo next = routeLayer.getHelper().getNextRouteDirectionInfo(); RouteDirectionInfo next = routeLayer.getHelper().getNextRouteDirectionInfo();
if (next == null || next.turnType == null) { if (next == null) {
if (turnType != null) { if (turnType != null) {
turnType = null; turnType = null;
invalidate(); invalidate();
} }
} else if (!Algoritms.objectEquals(turnType, next.turnType)) { } else if (!Algoritms.objectEquals(turnType, next.getTurnType())) {
turnType = next.turnType; turnType = next.getTurnType();
TurnPathHelper.calcTurnPath(pathForTurn, turnType, pathTransform); TurnPathHelper.calcTurnPath(pathForTurn, turnType, pathTransform);
if (turnType.getExitOut() > 0) { if (turnType.getExitOut() > 0) {
exitOut = turnType.getExitOut() + ""; //$NON-NLS-1$ exitOut = turnType.getExitOut() + ""; //$NON-NLS-1$