Move Yandex Traffic layer to common tile source management

This commit is contained in:
Victor Shcherb 2011-06-24 11:20:41 +02:00
parent 58c8eea285
commit 796d4a27a6
8 changed files with 97 additions and 49 deletions

View file

@ -12,7 +12,6 @@ public class ToDoConstants {
// Map Refactoring
// Remove notification from OsmAndMapTileView (?)
// Yandex Traffic make downloadable
// Index version
// 1. POI inside obf
// 2. Multiple attributes for one point (amenity=circle, type=...)

View file

@ -35,6 +35,7 @@ import bsh.Interpreter;
public class TileSourceManager {
private static final Log log = LogUtil.getLog(TileSourceManager.class);
private static final String RULE_BEANSHELL = "beanshell";
public static final String RULE_YANDEX_TRAFFIC = "yandex_traffic";
private static final String RULE_WMS = "wms_tile";
public static class TileSourceTemplate implements ITileSource {
@ -363,13 +364,14 @@ public class TileSourceManager {
TileSourceTemplate template = null;
String rule = attrs.get("rule");
if(rule == null){
template = createSimpleTileSourceTemplate(attrs);
template = createSimpleTileSourceTemplate(attrs, false);
} else if(RULE_BEANSHELL.equalsIgnoreCase(rule)){
template = createBeanshellTileSourceTemplate(attrs);
} else if (RULE_WMS.equalsIgnoreCase(rule)) {
template = createWmsTileSourceTemplate(attrs);
} else if (RULE_YANDEX_TRAFFIC.equalsIgnoreCase(rule)) {
template = createSimpleTileSourceTemplate(attrs, true);
} else {
// TODO rule == yandex_traffic
return null;
}
if(template != null){
@ -400,13 +402,15 @@ public class TileSourceManager {
private static TileSourceTemplate createSimpleTileSourceTemplate(Map<String, String> attributes) {
private static TileSourceTemplate createSimpleTileSourceTemplate(Map<String, String> attributes, boolean ignoreTemplate) {
String name = attributes.get("name");
String urlTemplate = attributes.get("url_template");
if (name == null || urlTemplate == null) {
if (name == null || (urlTemplate == null && !ignoreTemplate)) {
return null;
}
urlTemplate.replace("${x}", "{1}").replace("${y}", "{2}").replace("${z}", "{0}");
if(urlTemplate != null){
urlTemplate.replace("${x}", "{1}").replace("${y}", "{2}").replace("${z}", "{0}");
}
int maxZoom = parseInt(attributes, "max_zoom", 18);
int minZoom = parseInt(attributes, "min_zoom", 5);
int tileSize = parseInt(attributes, "tile_size", 256);

View file

@ -8,7 +8,6 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import net.osmand.Version;
import net.osmand.map.ITileSource;
import net.osmand.map.TileSourceManager;
import net.osmand.map.TileSourceManager.TileSourceTemplate;
@ -30,7 +29,6 @@ import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.provider.MediaStore.Audio;
import android.widget.Toast;
public class OsmandSettings {
@ -451,9 +449,6 @@ public class OsmandSettings {
// this value string is synchronized with settings_pref.xml preference name
public final OsmandPreference<Boolean> DEBUG_RENDERING_INFO = new BooleanPreference("debug_rendering", false, true);
// this value string is synchronized with settings_pref.xml preference name
public final OsmandPreference<Boolean> SHOW_YANDEX_TRAFFIC = new BooleanPreference("show_yandex_traffic", false, false);
// this value string is synchronized with settings_pref.xml preference name
public final OsmandPreference<Boolean> SHOW_FAVORITES = new BooleanPreference("show_favorites", false, false);

View file

@ -39,7 +39,6 @@ import net.osmand.plus.views.RouteInfoLayer;
import net.osmand.plus.views.RouteLayer;
import net.osmand.plus.views.TransportInfoLayer;
import net.osmand.plus.views.TransportStopsLayer;
import net.osmand.plus.views.YandexTrafficLayer;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
@ -62,7 +61,6 @@ public class MapActivityLayers {
private MapTileLayer underlayLayer;
private GPXLayer gpxLayer;
private RouteLayer routeLayer;
private YandexTrafficLayer trafficLayer;
private OsmBugsLayer osmBugsLayer;
private POIMapLayer poiMapLayer;
private FavoritesLayer favoritesLayer;
@ -111,10 +109,6 @@ public class MapActivityLayers {
routeLayer = new RouteLayer(routingHelper);
mapView.addLayer(routeLayer, 1);
// 1.5. traffic layer
trafficLayer = new YandexTrafficLayer();
mapView.addLayer(trafficLayer, 1.5f);
// 2. osm bugs layer
osmBugsLayer = new OsmBugsLayer(activity);
// 3. poi layer
@ -182,7 +176,6 @@ public class MapActivityLayers {
}
}
updateGPXLayer();
trafficLayer.setVisible(settings.SHOW_YANDEX_TRAFFIC.get());
}
public void updateMapSource(OsmandMapTileView mapView, CommonPreference<String> settingsToWarnAboutMap){
@ -257,8 +250,6 @@ public class MapActivityLayers {
if(transportRouteInfoInd > -1){
layersList.add(getString(R.string.layer_transport_route));
}
final int trafficInd = layersList.size();
layersList.add(getString(R.string.layer_yandex_traffic));
final boolean[] selected = new boolean[layersList.size()];
Arrays.fill(selected, true);
@ -269,7 +260,6 @@ public class MapActivityLayers {
selected[5] = gpxLayer.isVisible();
selected[6] = settings.SHOW_TRANSPORT_OVER_MAP.get();
selected[7] = settings.SHOW_OSM_BUGS.get();
selected[trafficInd] = trafficLayer.isVisible();
if(routeInfoInd != -1){
selected[routeInfoInd] = routeInfoLayer.isUserDefinedVisible();
}
@ -326,8 +316,6 @@ public class MapActivityLayers {
routeInfoLayer.setVisible(isChecked);
} else if(item == transportRouteInfoInd){
transportInfoLayer.setVisible(isChecked);
} else if(item == trafficInd){
settings.SHOW_YANDEX_TRAFFIC.set(isChecked);
}
updateLayers(mapView);
mapView.refreshMap();

View file

@ -1,5 +1,26 @@
package net.osmand.plus.views;
public class MapTileAdapter {
import android.graphics.Canvas;
import android.graphics.RectF;
import net.osmand.map.ITileSource;
/**
* This class is designed to represent adapter for specific map sources
* that requires additional computation or updates
*/
public abstract class MapTileAdapter {
protected MapTileLayer layer;
protected OsmandMapTileView view;
public void initLayerAdapter(MapTileLayer layer, OsmandMapTileView view){
this.layer = layer;
this.view = view;
}
public abstract void onDraw(Canvas canvas, RectF latlonRect, RectF tilesRect, boolean nightMode);
public abstract void onClear();
public abstract void onInit();
}

View file

@ -1,6 +1,8 @@
package net.osmand.plus.views;
import net.osmand.map.ITileSource;
import net.osmand.map.TileSourceManager;
import net.osmand.map.TileSourceManager.TileSourceTemplate;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.ResourceManager;
@ -15,11 +17,13 @@ import android.widget.Toast;
public class MapTileLayer extends BaseMapLayer {
protected final int emptyTileDivisor = 16;
public static final int OVERZOOM_IN = 2;
private final boolean mainMap;
protected ITileSource map = null;
protected MapTileAdapter mapTileAdapter = null;
Paint paintBitmap;
protected RectF tilesRect = new RectF();
@ -52,6 +56,10 @@ public class MapTileLayer extends BaseMapLayer {
paintBitmap = new Paint();
paintBitmap.setFilterBitmap(true);
paintBitmap.setAlpha(getAlpha());
if(mapTileAdapter != null && view != null){
mapTileAdapter.initLayerAdapter(this, view);
}
}
@Override
@ -62,12 +70,52 @@ public class MapTileLayer extends BaseMapLayer {
}
}
public void setMapTileAdapter(MapTileAdapter mapTileAdapter) {
if(this.mapTileAdapter == mapTileAdapter){
return;
}
if(this.mapTileAdapter != null){
this.mapTileAdapter.onClear();
}
this.mapTileAdapter = mapTileAdapter;
if(mapTileAdapter != null && view != null){
mapTileAdapter.initLayerAdapter(this, view);
mapTileAdapter.onInit();
}
}
public void setMapForMapTileAdapter(ITileSource map, MapTileAdapter mapTileAdapter) {
if(mapTileAdapter == this.mapTileAdapter){
this.map = map;
}
}
public void setMap(ITileSource map) {
MapTileAdapter target = null;
if(map instanceof TileSourceTemplate){
if(TileSourceManager.RULE_YANDEX_TRAFFIC.equals(((TileSourceTemplate) map).getRule())){
map = null;
target = new YandexTrafficAdapter();
}
}
this.map = map;
setMapTileAdapter(target);
}
public MapTileAdapter getMapTileAdapter() {
return mapTileAdapter;
}
@Override
public void onDraw(Canvas canvas, RectF latlonRect, RectF tilesRect, boolean nightMode) {
if (map == null || !visible) {
if ((map == null && mapTileAdapter == null) || !visible) {
return;
}
if(mapTileAdapter != null){
mapTileAdapter.onDraw(canvas, latlonRect, tilesRect, nightMode);
}
drawTileMap(canvas, tilesRect);
}
@ -190,6 +238,7 @@ public class MapTileLayer extends BaseMapLayer {
@Override
public void destroyLayer() {
// TODO clear map cache
setMapTileAdapter(null);
}
public boolean isVisible() {
@ -205,9 +254,7 @@ public class MapTileLayer extends BaseMapLayer {
return map;
}
public void setMap(ITileSource map) {
this.map = map;
}
@Override
public boolean onLongPressEvent(PointF point) {

View file

@ -23,6 +23,7 @@ public class PointNavigationLayer implements OsmandMapLayer {
protected final static int DIST_TO_SHOW = 120;
private Paint point;
private Paint bitmapPaint;
protected LatLon pointToNavigate = null;
private OsmandMapTileView view;
@ -31,10 +32,16 @@ public class PointNavigationLayer implements OsmandMapLayer {
private DisplayMetrics dm;
private Bitmap targetPoint;
private void initUI() {
point = new Paint();
point.setColor(Color.rgb(250, 80, 80));
point.setAntiAlias(true);
point.setStyle(Style.FILL);
bitmapPaint = new Paint();
targetPoint = BitmapFactory.decodeResource(view.getResources(), R.drawable.target_point);
pathForDirection = new Path();
@ -62,7 +69,7 @@ public class PointNavigationLayer implements OsmandMapLayer {
int locationX = view.getMapXForPoint(pointToNavigate.getLongitude());
int locationY = view.getMapYForPoint(pointToNavigate.getLatitude());
canvas.rotate(-view.getRotate(), locationX, locationY);
canvas.drawBitmap(targetPoint, locationX - marginX, locationY - marginY, point);
canvas.drawBitmap(targetPoint, locationX - marginX, locationY - marginY, bitmapPaint);
} else {
Location.distanceBetween(view.getLatitude(), view.getLongitude(), pointToNavigate.getLatitude(),
pointToNavigate.getLongitude(), calculations);

View file

@ -19,7 +19,7 @@ import android.graphics.Canvas;
import android.graphics.RectF;
import android.widget.Toast;
public class YandexTrafficLayer extends MapTileLayer {
public class YandexTrafficAdapter extends MapTileAdapter {
private final static Log log = LogUtil.getLog(MapTileLayer.class);
private final static String YANDEX_PREFFIX = ".YandexTraffic_";
@ -29,26 +29,14 @@ public class YandexTrafficLayer extends MapTileLayer {
private String mTimestamp = null;
private boolean updateThreadRan = false;
public YandexTrafficLayer() {
super(false);
}
public void setVisible(boolean visible) {
if(isVisible() != visible){
if(visible){
Toast.makeText(view.getContext(), R.string.thanks_yandex_traffic, Toast.LENGTH_LONG).show();
}
super.setVisible(visible);
}
public void onInit() {
Toast.makeText(view.getContext(), R.string.thanks_yandex_traffic, Toast.LENGTH_LONG).show();
}
@Override
public void onDraw(Canvas canvas, RectF latlonRect, RectF tilesRect, boolean nightMode) {
if(isVisible()){
updateTimeStamp();
}
super.onDraw(canvas, latlonRect, tilesRect, nightMode);
updateTimeStamp();
}
protected void updateTimeStamp() {
@ -97,7 +85,7 @@ public class YandexTrafficLayer extends MapTileLayer {
"http://jgo.maps.yandex.net/tiles?l=trf&x={1}&y={2}&z={0}&tm=" + mTimestamp, ".png", 17, 7, 256, 8, 18000);
template.setEllipticYTile(true);
clearCache();
this.map = template;
this.layer.setMapForMapTileAdapter(template, this);
}
} catch (IOException e) {
log.info("Exception while updating yandex traffic template", e);
@ -106,8 +94,7 @@ public class YandexTrafficLayer extends MapTileLayer {
}
@Override
public void destroyLayer() {
super.destroyLayer();
public void onClear() {
clearCache();
}