Update to map view v2.

This commit is contained in:
Jimmy Shih
2012-10-20 16:18:50 -07:00
parent 2a676dbc9a
commit 9eca3df172
70 changed files with 1291 additions and 2401 deletions
@@ -1,139 +0,0 @@
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.android.apps.mytracks.maps;
import com.google.android.apps.mytracks.ColoredPath;
import com.google.android.apps.mytracks.MapOverlay.CachedLocation;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.Projection;
import com.google.android.maps.mytracks.R;
import com.google.common.annotations.VisibleForTesting;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import java.util.ArrayList;
import java.util.List;
/**
* A path painter that varies the path colors based on fixed speeds or average
* speed margin depending of the TrackPathDescriptor passed to its constructor.
*
* @author Vangelis S.
*/
public class DynamicSpeedTrackPathPainter implements TrackPathPainter {
private final TrackPathDescriptor trackPathDescriptor;
private final Paint slowPaint;
private final Paint mediumPaint;
private final Paint fastPaint;
private final List<ColoredPath> coloredPaths;
public DynamicSpeedTrackPathPainter(Context context, TrackPathDescriptor trackPathDescriptor) {
this.trackPathDescriptor = trackPathDescriptor;
slowPaint = TrackPathUtils.getPaint(context, R.color.slow_path);
mediumPaint = TrackPathUtils.getPaint(context, R.color.normal_path);
fastPaint = TrackPathUtils.getPaint(context, R.color.fast_path);
coloredPaths = new ArrayList<ColoredPath>();
}
@Override
public boolean hasPath() {
return !coloredPaths.isEmpty();
}
@Override
public boolean updateState() {
return trackPathDescriptor.updateState();
}
@Override
public void updatePath(
Projection projection, Rect viewRect, int startIndex, List<CachedLocation> points) {
boolean hasLastPoint = startIndex != 0 && points.get(startIndex -1).isValid();
Point point = new Point();
if (hasLastPoint) {
GeoPoint geoPoint = points.get(startIndex -1).getGeoPoint();
projection.toPixels(geoPoint, point);
}
boolean newSegment = !hasLastPoint;
// Assume if last point exists, it is visible
boolean lastPointVisible = hasLastPoint;
int slowSpeed = trackPathDescriptor.getSlowSpeed();
int normalSpeed = trackPathDescriptor.getNormalSpeed();
for (int i = startIndex; i < points.size(); ++i) {
CachedLocation cachedLocation = points.get(i);
// If not valid, start a new segment
if (!cachedLocation.isValid()) {
newSegment = true;
continue;
}
GeoPoint geoPoint = cachedLocation.getGeoPoint();
// Check if this breaks the existing segment.
boolean pointVisible = viewRect.contains(geoPoint.getLongitudeE6(), geoPoint.getLatitudeE6());
if (!pointVisible && !lastPointVisible) {
// This point and the last point are both outside visible area.
newSegment = true;
}
lastPointVisible = pointVisible;
// Either update point or draw a line from the last point
if (newSegment) {
projection.toPixels(geoPoint, point);
newSegment = false;
} else {
ColoredPath coloredPath;
if (cachedLocation.getSpeed() <= slowSpeed) {
coloredPath = new ColoredPath(slowPaint);
} else if (cachedLocation.getSpeed() <= normalSpeed) {
coloredPath = new ColoredPath(mediumPaint);
} else {
coloredPath = new ColoredPath(fastPaint);
}
coloredPath.getPath().moveTo(point.x, point.y);
projection.toPixels(geoPoint, point);
coloredPath.getPath().lineTo(point.x, point.y);
coloredPaths.add(coloredPath);
}
}
}
@Override
public void clearPath() {
coloredPaths.clear();
}
@Override
public void drawPath(Canvas canvas) {
for (int i = 0; i < coloredPaths.size(); i++) {
ColoredPath coloredPath = coloredPaths.get(i);
canvas.drawPath(coloredPath.getPath(), coloredPath.getPathPaint());
}
}
/**
* Gets the colored paths.
*/
@VisibleForTesting
List<ColoredPath> getColoredPaths() {
return coloredPaths;
}
}
@@ -0,0 +1,114 @@
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.android.apps.mytracks.maps;
import com.google.android.apps.mytracks.MapOverlay.CachedLocation;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.maps.mytracks.R;
import android.content.Context;
import java.util.ArrayList;
import java.util.List;
/**
* A path painter that varies the path colors based on fixed speeds or average
* speed margin depending of the TrackPathDescriptor passed to its constructor.
*
* @author Vangelis S.
*/
public class MultiColorTrackPath implements TrackPath {
private final TrackPathDescriptor trackPathDescriptor;
private final int slowColor;
private final int normalColor;
private final int fastColor;
public MultiColorTrackPath(Context context, TrackPathDescriptor trackPathDescriptor) {
this.trackPathDescriptor = trackPathDescriptor;
slowColor = context.getResources().getColor(R.color.slow_path);
normalColor = context.getResources().getColor(R.color.normal_path);
fastColor = context.getResources().getColor(R.color.fast_path);
}
@Override
public boolean updateState() {
return trackPathDescriptor.updateState();
}
@Override
public void updatePath(GoogleMap googleMap, ArrayList<Polyline> paths, int startIndex,
List<CachedLocation> locations) {
if (googleMap == null) {
return;
}
if (startIndex >= locations.size()) {
return;
}
boolean newSegment = startIndex == 0 || !locations.get(startIndex - 1).isValid();
LatLng lastLatLng = startIndex != 0 ? locations.get(startIndex -1).getLatLng() : null;
ArrayList<LatLng> lastSegmentPoints = new ArrayList<LatLng>();
int lastSegmentColor = paths.size() != 0 ? paths.get(paths.size() - 1).getColor() : slowColor;
boolean useLastPolyline = true;
for (int i = startIndex; i < locations.size(); ++i) {
CachedLocation cachedLocation = locations.get(i);
// If not valid, start a new segment
if (!cachedLocation.isValid()) {
newSegment = true;
lastLatLng = null;
continue;
}
LatLng latLng = cachedLocation.getLatLng();
int color = getColor(cachedLocation.getSpeed());
// Either update point or draw a line from the last point
if (newSegment) {
TrackPathUtils.addPath(googleMap, paths, lastSegmentPoints, lastSegmentColor, useLastPolyline);
useLastPolyline = false;
lastSegmentColor = color;
newSegment = false;
}
if (lastSegmentColor == color) {
lastSegmentPoints.add(latLng);
} else {
TrackPathUtils.addPath(googleMap, paths, lastSegmentPoints, lastSegmentColor, useLastPolyline);
useLastPolyline = false;
if (lastLatLng != null) {
lastSegmentPoints.add(lastLatLng);
}
lastSegmentPoints.add(latLng);
lastSegmentColor = color;
}
lastLatLng = latLng;
}
TrackPathUtils.addPath(googleMap, paths, lastSegmentPoints, lastSegmentColor, useLastPolyline);
}
private int getColor(int speed) {
if (speed <= trackPathDescriptor.getSlowSpeed()) {
return slowColor;
} else if (speed <= trackPathDescriptor.getNormalSpeed()) {
return normalColor;
} else {
return fastColor;
}
}
}
@@ -0,0 +1,78 @@
/*
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.android.apps.mytracks.maps;
import com.google.android.apps.mytracks.MapOverlay.CachedLocation;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.maps.mytracks.R;
import android.content.Context;
import java.util.ArrayList;
import java.util.List;
/**
* A single color track path.
*
* @author Jimmy Shih
*/
public class SingleColorTrackPath implements TrackPath {
final int color;
public SingleColorTrackPath(Context context) {
color = context.getResources().getColor(R.color.fast_path);
}
@Override
public boolean updateState() {
return false;
}
@Override
public void updatePath(GoogleMap googleMap, ArrayList<Polyline> paths, int startIndex,
List<CachedLocation> locations) {
if (googleMap == null) {
return;
}
if (startIndex >= locations.size()) {
return;
}
boolean newSegment = startIndex == 0 || !locations.get(startIndex - 1).isValid();
ArrayList<LatLng> lastSegmentPoints = new ArrayList<LatLng>();
boolean useLastPolyline = true;
for (int i = startIndex; i < locations.size(); i++) {
CachedLocation cachedLocation = locations.get(i);
// If not valid, start a new segment
if (!cachedLocation.isValid()) {
newSegment = true;
continue;
}
LatLng latLng = cachedLocation.getLatLng();
if (newSegment) {
TrackPathUtils.addPath(googleMap, paths, lastSegmentPoints, color, useLastPolyline);
useLastPolyline = false;
newSegment = false;
}
lastSegmentPoints.add(latLng);
}
TrackPathUtils.addPath(googleMap, paths, lastSegmentPoints, color, useLastPolyline);
}
}
@@ -1,142 +0,0 @@
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.android.apps.mytracks.maps;
import com.google.android.apps.mytracks.MapOverlay.CachedLocation;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.Projection;
import com.google.android.maps.mytracks.R;
import com.google.common.annotations.VisibleForTesting;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import java.util.List;
/**
* A path painter that not variates the path colors.
*
* @author Vangelis S.
*/
public class SingleColorTrackPathPainter implements TrackPathPainter {
private final Paint selectedTrackPaint;
private Path path;
public SingleColorTrackPathPainter(Context context) {
selectedTrackPaint = TrackPathUtils.getPaint(context, R.color.red);
}
@Override
public boolean hasPath() {
return path != null;
}
@Override
public boolean updateState() {
return false;
}
@Override
public void updatePath(
Projection projection, Rect viewRect, int startIndex, List<CachedLocation> points) {
if (!hasPath()) {
path = newPath();
}
updatePath(projection, viewRect, startIndex, points, path);
}
@Override
public void clearPath() {
path = null;
}
@Override
public void drawPath(Canvas canvas) {
if (path != null) {
canvas.drawPath(path, selectedTrackPaint);
}
}
/**
* Updates the path.
*
* @param projection the projection
* @param viewRect the view rectangle
* @param startIndex the start index
* @param points the points
* @param pathToUpdate the path to update
*/
@VisibleForTesting
void updatePath(Projection projection, Rect viewRect, int startIndex, List<CachedLocation> points,
Path pathToUpdate) {
pathToUpdate.incReserve(points.size() - startIndex);
boolean hasLastPoint = startIndex != 0 && points.get(startIndex - 1).isValid();
boolean newSegment = !hasLastPoint;
// Assume if last point exists, it is visible
boolean lastPointVisible = hasLastPoint;
Point point = new Point();
for (int i = startIndex; i < points.size(); i++) {
CachedLocation cachedLocation = points.get(i);
// If not valid, start a new segment
if (!cachedLocation.isValid()) {
newSegment = true;
continue;
}
GeoPoint geoPoint = cachedLocation.getGeoPoint();
// Check if this breaks the existing segment.
boolean pointVisible = viewRect.contains(geoPoint.getLongitudeE6(), geoPoint.getLatitudeE6());
if (!pointVisible && !lastPointVisible) {
// This point and the last point are both outside visible area.
newSegment = true;
}
lastPointVisible = pointVisible;
// Either update point or draw a line from the last point
projection.toPixels(geoPoint, point);
if (newSegment) {
pathToUpdate.moveTo(point.x, point.y);
newSegment = false;
} else {
pathToUpdate.lineTo(point.x, point.y);
}
}
}
/**
* Creates a new path.
*/
@VisibleForTesting
protected Path newPath() {
return new Path();
}
/**
* Gets the path.
*/
@VisibleForTesting
public Path getPath() {
return path;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2011 Google Inc.
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
@@ -17,50 +17,27 @@
package com.google.android.apps.mytracks.maps;
import com.google.android.apps.mytracks.MapOverlay.CachedLocation;
import com.google.android.maps.Projection;
import android.graphics.Canvas;
import android.graphics.Rect;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.Polyline;
import java.util.ArrayList;
import java.util.List;
/**
* An interface for classes which paint the track path.
* A track path.
*
* @author Vangelis S.
* @author Jimmy Shih
*/
public interface TrackPathPainter {
public interface TrackPath {
/**
* Returns true if has path.
*/
public boolean hasPath();
/**
* Updates state. Returns true if the state is updated.
*/
public boolean updateState();
/**
* Updates the path. Creates a new path if necessary
* Updates the path.
*
* @param projection the projection
* @param viewRect the view rectangle
* @param startIndex the start index
* @param points the points
*/
public void updatePath(
Projection projection, Rect viewRect, int startIndex, List<CachedLocation> points);
/**
* Clears the path.
*/
public void clearPath();
/**
* Draws the path.
*
* @param canvas the canvas
*/
public void drawPath(Canvas canvas);
public void updatePath(GoogleMap googleMap, ArrayList<Polyline> paths, int startIndex,
List<CachedLocation> points);
}
@@ -16,42 +16,37 @@
package com.google.android.apps.mytracks.maps;
import static com.google.android.apps.mytracks.Constants.TAG;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.maps.mytracks.R;
import android.content.Context;
import android.util.Log;
/**
* A factory for {@link TrackPathPainter}.
* A factory for {@link TrackPath}.
*
* @author Vangelis S.
*/
public class TrackPathPainterFactory {
public class TrackPathFactory {
private TrackPathPainterFactory() {}
private TrackPathFactory() {}
/**
* Get a new {@link TrackPathPainter}.
* Get a new {@link TrackPath}.
*
* @param context the context
*/
public static TrackPathPainter getTrackPathPainter(Context context) {
public static TrackPath getTrackPath(Context context) {
String trackColorMode = PreferencesUtils.getString(
context, R.string.track_color_mode_key, PreferencesUtils.TRACK_COLOR_MODE_DEFAULT);
Log.i(TAG, "Creating track path painter of type: " + trackColorMode);
if (context.getString(R.string.settings_map_track_color_mode_dynamic_value)
.equals(trackColorMode)) {
return new DynamicSpeedTrackPathPainter(context, new DynamicSpeedTrackPathDescriptor(
context));
return new MultiColorTrackPath(context, new DynamicSpeedTrackPathDescriptor(context));
} else if (context.getString(R.string.settings_map_track_color_mode_fixed_value)
.equals(trackColorMode)) {
return new DynamicSpeedTrackPathPainter(context, new FixedSpeedTrackPathDescriptor(context));
return new MultiColorTrackPath(context, new FixedSpeedTrackPathDescriptor(context));
} else {
return new SingleColorTrackPathPainter(context);
return new SingleColorTrackPath(context);
}
}
}
@@ -16,8 +16,12 @@
package com.google.android.apps.mytracks.maps;
import android.content.Context;
import android.graphics.Paint;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Model;
import com.google.android.gms.maps.model.Polyline;
import java.util.ArrayList;
/**
* Various utility functions for track path painting.
@@ -29,17 +33,32 @@ public class TrackPathUtils {
private TrackPathUtils() {}
/**
* Gets a paint.
* Add a path.
*
* @param context the context
* @param colorId the color id
* @param googleMap the google map
* @param paths the existing paths
* @param points the path points
* @param color the path color
* @param append true to append to the last path
*/
public static Paint getPaint(Context context, int colorId) {
Paint paint = new Paint();
paint.setColor(context.getResources().getColor(colorId));
paint.setStrokeWidth(3);
paint.setStyle(Paint.Style.STROKE);
paint.setAntiAlias(true);
return paint;
@SuppressWarnings("unchecked")
public static void addPath(GoogleMap googleMap, ArrayList<Polyline> paths,
ArrayList<LatLng> points, int color, boolean append) {
if (points.size() == 0) {
return;
}
if (append && paths.size() != 0) {
Polyline lastPolyline = paths.get(paths.size() - 1);
ArrayList<LatLng> pathPoints = new ArrayList<LatLng>();
pathPoints.addAll(lastPolyline.getPoints());
pathPoints.addAll(points);
lastPolyline.setPoints(pathPoints);
} else {
Polyline.Options options = Model.newPolylineOptions()
.addAll(points).width(5).color(color);
Polyline polyline = googleMap.addPolyline(options);
paths.add(polyline);
}
points.clear();
}
}