Merge pull request #464 from fokin33/master

OsmoDroid
This commit is contained in:
vshcherb 2013-07-01 15:07:20 -07:00
commit 604e8ed44e
7 changed files with 607 additions and 105 deletions

View file

@ -0,0 +1,9 @@
package com.OsMoDroid;
interface IRemoteOsMoDroidListener {
void channelUpdated();
void channelsListUpdated();
}

View file

@ -1,27 +1,46 @@
package com.OsMoDroid;
import com.OsMoDroid.IRemoteOsMoDroidListener;
interface IRemoteOsMoDroidService {
int getVersion();
/*
int getNumberOfLayers();
int getLayerId(int pos);
String getLayerName(int layerId);
String getLayerDescription(int layerId);
int getNumberOfObjects(int layerId);
int getObjectId(int layerId, int pos);
float getObjectLat(int layerId, int objectId);
float getObjectLon(int layerId, int objectId);
String getObjectName(int layerId, int objectId);
String getObjectDescription(int layerId, int objectId);
*/
void registerListener(IRemoteOsMoDroidListener listener);
void unregisterListener(IRemoteOsMoDroidListener listener);
int getVersion();
int getBackwardCompatibleVersion();
void Deactivate();
void Activate();
boolean isActive();
int getNumberOfLayers();
int getLayerId(int pos);
String getLayerName(int layerId);
String getLayerDescription(int layerId);
int getNumberOfObjects(int layerId);
int getObjectId(int layerId, int pos);
float getObjectLat(int layerId, int objectId);
float getObjectLon(int layerId, int objectId);
String getObjectSpeed(int layerId, int objectId);
String getObjectName(int layerId, int objectId);
String getObjectDescription(int layerId, int objectId);
String getObjectColor(int layerId, int objectId);
}

View file

@ -96,6 +96,42 @@ public class ContextMenuAdapter {
return itemNames.toArray(new String[itemNames.size()]);
}
private void registerSelectedItem(String string, int selected, int icon,OnContextMenuClick listener, int pos) {
if(pos >= items.size() || pos < 0) {
pos = items.size();
}
items.insert(pos, pos);
itemNames.add(pos, string);
selectedList.insert(pos, selected);
iconList.insert(pos, icon);
listeners.add(pos, listener);
}
public void registerItem(String string) {
registerSelectedItem(string, -1, 0);
}
private void registerSelectedItem(String string, int selected, int icon) {
registerSelectedItem(string, selected, icon, null, -1);
}
public void registerItem(String name, int id, OnContextMenuClick listener, int pos) {
if(pos >= items.size() || pos < 0) {
pos = items.size();
}
items.insert(pos, id);
itemNames.add(pos, name);
listeners.add(pos, listener);
}
public ListAdapter createListAdapter(final Activity activity, final int layoutId) {
final int padding = (int) (12 * activity.getResources().getDisplayMetrics().density + 0.5f);
ListAdapter listadapter = new ArrayAdapter<String>(activity, layoutId, R.id.title,

View file

@ -168,19 +168,21 @@ public class DistanceCalculatorPlugin extends OsmandPlugin {
@Override
public void onClick(DialogInterface dialog, int which) {
int id = list.get(which);
switch (id) {
case R.string.distance_measurement_start_editing :
distanceMeasurementMode = 1; startEditingHelp(activity) ; break;
case R.string.distance_measurement_finish_editing :
distanceMeasurementMode = 0; break;
case R.string.distance_measurement_finish_subtrack :
measurementPoints.add(new LinkedList<GPXUtilities.WptPt>()); break;
case R.string.distance_measurement_clear_route :
distanceMeasurementMode = 0; measurementPoints.clear(); calculateDistance(); break;
case R.string.distance_measurement_save_gpx :
saveGpx(activity); break;
case R.string.distance_measurement_load_gpx :
loadGpx(activity); break;
if (id == R.string.distance_measurement_start_editing) {
distanceMeasurementMode = 1;
startEditingHelp(activity) ;
} else if (id == R.string.distance_measurement_finish_editing) {
distanceMeasurementMode = 0;
} else if (id == R.string.distance_measurement_finish_subtrack) {
measurementPoints.add(new LinkedList<GPXUtilities.WptPt>());
} else if (id == R.string.distance_measurement_clear_route) {
distanceMeasurementMode = 0;
measurementPoints.clear();
calculateDistance();
} else if (id == R.string.distance_measurement_save_gpx) {
saveGpx(activity);
} else if (id == R.string.distance_measurement_load_gpx) {
loadGpx(activity);
}
activity.getMapView().refreshMap();
updateText();

View file

@ -0,0 +1,281 @@
package net.osmand.plus.osmodroid;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import net.osmand.access.AccessibleToast;
import net.osmand.data.Amenity;
import net.osmand.data.LatLon;
import net.osmand.plus.ContextMenuAdapter;
import net.osmand.plus.ContextMenuAdapter.OnContextMenuClick;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.views.ContextMenuLayer;
import net.osmand.plus.views.OsmandMapLayer;
import net.osmand.plus.views.OsmandMapTileView;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.os.RemoteException;
import android.text.format.DateFormat;
import android.text.format.Time;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.Toast;
import android.content.DialogInterface;
/**
* Class represents a OsMoDroidlayer which depicts the position of Esya.ru channels objects
* @author Denis Fokin
* @see OsMoDroidPlugin
*
*/
public class OsMoDroidLayer extends OsmandMapLayer implements ContextMenuLayer.IContextMenuProvider {
/**
* magic number so far
*/
private static final int radius = 10;
OsMoDroidPlugin myOsMoDroidPlugin;
private DisplayMetrics dm;
private final MapActivity map;
private OsmandMapTileView view;
private Paint textPaint;
ArrayList<OsMoDroidPoint> OsMoDroidPointArrayList;
int layerId;
String layerName;
String layerDescription;
private Bitmap opIcon;
public void refresh(){
map.refreshMap();
}
public OsMoDroidLayer(MapActivity map) {
this.map = map;
}
public OsMoDroidLayer(MapActivity map, int layerId, OsMoDroidPlugin osMoDroidPlugin, String layerName, String layerDescription) {
this.map = map;
this.layerId=layerId;
this.myOsMoDroidPlugin=osMoDroidPlugin;
this.layerName=layerName;
this.layerDescription=layerDescription;
}
@Override
public void initLayer(OsmandMapTileView view) {
this.view = view;
dm = new DisplayMetrics();
WindowManager wmgr = (WindowManager) view.getContext().getSystemService(Context.WINDOW_SERVICE);
wmgr.getDefaultDisplay().getMetrics(dm);
textPaint = new Paint();
textPaint.setDither(true);
textPaint.setAntiAlias(true);
textPaint.setFilterBitmap(true);
textPaint.setTextSize(22f);
textPaint.setTypeface(Typeface.DEFAULT_BOLD);
textPaint.setTextAlign(Paint.Align.CENTER);
opIcon=BitmapFactory.decodeResource(view.getResources(), R.drawable.bicycle_location);
OsMoDroidPointArrayList = myOsMoDroidPlugin.getOsMoDroidPointArrayList(layerId);
}
@Override
public void onDraw(Canvas canvas, RectF latLonBounds, RectF tilesRect, DrawSettings nightMode) {
for (OsMoDroidPoint op :OsMoDroidPointArrayList){
LatLon newLatlon;
try {
newLatlon= new LatLon(myOsMoDroidPlugin.mIRemoteService.getObjectLat(layerId, op.id) , myOsMoDroidPlugin.mIRemoteService.getObjectLon(layerId, op.id));
if(!op.latlon.equals(newLatlon)){
op.prevlatlon=op.latlon;
}
op.latlon=newLatlon;
op.speed=myOsMoDroidPlugin.mIRemoteService.getObjectSpeed(layerId, op.id);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
double latitude = op.latlon.getLatitude();
double longitude = op.latlon.getLongitude();
double prevlatitude = op.latlon.getLatitude();
double prevlongitude = op.latlon.getLongitude();
if (op.prevlatlon!=null){
prevlatitude = op.prevlatlon.getLatitude();
prevlongitude = op.prevlatlon.getLongitude();
}
//int locationX = view.getMapXForPoint(longitude);
//int locationY = view.getMapYForPoint(latitude);
int locationX = view.getRotatedMapXForPoint(latitude, longitude);
int locationY = view.getRotatedMapYForPoint(latitude, longitude);
int prevlocationX = view.getRotatedMapXForPoint(prevlatitude, prevlongitude);
int prevlocationY = view.getRotatedMapYForPoint(prevlatitude, prevlongitude);
//int y = opIcon.getHeight()/2;
//int x = opIcon.getWidth()/2;
textPaint.setColor(Color.parseColor("#013220"));
canvas.drawText(op.name, locationX, locationY-radius, textPaint);
canvas.drawText(op.speed, locationX, locationY-2*radius, textPaint);
textPaint.setColor(Color.parseColor("#"+op.color));
textPaint.setShadowLayer(radius, 0, 0, Color.GRAY);
canvas.drawCircle(locationX, locationY, radius, textPaint);
//canvas.drawBitmap(opIcon, locationX-x, locationY-y , textPaint);
textPaint.setStrokeWidth(radius);
canvas.drawLine(locationX, locationY, prevlocationX, prevlocationY, textPaint);
//canvas.rotate(-view.getRotate(), locationX, locationY);
// op.prevlatlon=op.latlon;
}
}
public void getOsMoDroidPointFromPoint(PointF point,
List<? super OsMoDroidPoint> om) {
if (myOsMoDroidPlugin.getOsMoDroidPointArrayList(layerId) != null) {
int ex = (int) point.x;
int ey = (int) point.y;
try {
for (int i = 0; i < myOsMoDroidPlugin
.getOsMoDroidPointArrayList(layerId).size(); i++) {
OsMoDroidPoint n = myOsMoDroidPlugin
.getOsMoDroidPointArrayList(layerId).get(i);
if (!om.contains(n)) {
int x = view
.getRotatedMapXForPoint(n.latlon.getLatitude(),
n.latlon.getLongitude());
int y = view
.getRotatedMapYForPoint(n.latlon.getLatitude(),
n.latlon.getLongitude());
if (Math.abs(x - ex) <= opIcon.getWidth()
&& Math.abs(y - ey) <= opIcon.getHeight()) {
om.add(n);
}
}
}
} catch (IndexOutOfBoundsException e) {
// that's really rare case, but is much efficient than introduce
// synchronized block
}
}
}
@Override
public void populateObjectContextMenu(Object o, ContextMenuAdapter adapter) {
if(o instanceof OsMoDroidPoint&&((OsMoDroidPoint) o).layerId==layerId) {
final OsMoDroidPoint a = (OsMoDroidPoint) o;
OnContextMenuClick listener = new ContextMenuAdapter.OnContextMenuClick() {
@Override
public void onContextMenuClick(int itemId, int pos, boolean isChecked, DialogInterface dialog) {
map.getMyApplication().getTargetPointsHelper().navigateToPoint( a.latlon, true, -1);
}
};
adapter.registerItem( "Следовать к",a.id, listener,-1);
}
}
@Override
public boolean onSingleTap(PointF point) {
List<OsMoDroidPoint> om = new ArrayList<OsMoDroidPoint>();
getOsMoDroidPointFromPoint(point, om);
if(!om.isEmpty()){
StringBuilder res = new StringBuilder();
for (int i = 0; i < om.size(); i++) {
OsMoDroidPoint n = om.get(i);
if (i > 0) {
res.append("\n\n");
}
res=res.append(n.description);
}
AccessibleToast.makeText(view.getContext(), res.toString(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
@Override
public void destroyLayer() {
}
@Override
public boolean drawInScreenPixels() {
return true;
}
@Override
public String getObjectName(Object o) {
if(o instanceof OsMoDroidPoint){
return ((OsMoDroidPoint)o).name;
}
return null;
}
@Override
public void collectObjectsFromPoint(PointF point, List<Object> o) {
getOsMoDroidPointFromPoint(point, o);
}
@Override
public LatLon getObjectLocation(Object o) {
if(o instanceof OsMoDroidPoint){
return ((OsMoDroidPoint)o).latlon;
}
return null;
}
@Override
public String getObjectDescription(Object o) {
if(o instanceof OsMoDroidPoint){
return ((OsMoDroidPoint)o).description;
}
return null;
}
}

View file

@ -1,13 +1,20 @@
package net.osmand.plus.osmodroid;
import java.util.ArrayList;
import net.osmand.PlatformUtil;
import net.osmand.data.LatLon;
import net.osmand.plus.ContextMenuAdapter;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.parkingpoint.ParkingPositionLayer;
import net.osmand.plus.views.OsmandMapTileView;
import org.apache.commons.logging.Log;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
@ -15,79 +22,185 @@ import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import com.OsMoDroid.IRemoteOsMoDroidListener;
import com.OsMoDroid.IRemoteOsMoDroidService;
public class OsMoDroidPlugin extends OsmandPlugin {
IRemoteOsMoDroidListener.Stub inter = new IRemoteOsMoDroidListener.Stub() {
public static final String ID = "osmand.osmodroid";
private static final Log log = PlatformUtil.getLog(OsMoDroidPlugin.class);
private OsmandApplication app;
IRemoteOsMoDroidService mIRemoteService;
private ServiceConnection mConnection;
private int OSMODROID_SUPPORTED_VERSION_MIN = 0;
@Override
public String getId() {
return ID;
}
public OsMoDroidPlugin(OsmandApplication app) {
this.app = app;
}
@Override
public String getDescription() {
return app.getString(R.string.osmodroid_plugin_description);
}
@Override
public String getName() {
return app.getString(R.string.osmodroid_plugin_name);
}
@Override
public boolean init(final OsmandApplication app) {
mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mIRemoteService = IRemoteOsMoDroidService.Stub.asInterface(service);
try {
System.out.println(mIRemoteService.getVersion());
if(mIRemoteService.getVersion() < OSMODROID_SUPPORTED_VERSION_MIN) {
app.showToastMessage(R.string.osmodroid_plugin_old_ver_not_supported);
shutdown(app);
}
} catch (RemoteException e) {
log.error(e.getMessage(), e);
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
mIRemoteService = null;
}
};
Intent serviceIntent = (new Intent("OsMoDroid.remote"));
app.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE);
return true;
}
@Override
public void registerLayers(MapActivity activity) {
}
@Override
public void disable(OsmandApplication app) {
shutdown(app);
}
private void shutdown(OsmandApplication app) {
if (mIRemoteService != null) {
app.unbindService(mConnection);
mIRemoteService = null;
}
}
@Override
public void channelUpdated() throws RemoteException {
if(activity!=null){
activity.refreshMap();
//test
}
}
@Override
public void channelsListUpdated() throws RemoteException {
if (activity!=null&&connected){
for (OsMoDroidLayer myOsMoDroidLayer : osmoDroidLayerList){
activity.getMapView().removeLayer(myOsMoDroidLayer);
}
osmoDroidLayerList.clear();
requestLayersFromOsMoDroid(activity);
for (OsMoDroidLayer myOsMoDroidLayer :osmoDroidLayerList){
activity.getMapView().addLayer(myOsMoDroidLayer, 4.5f);
}
}
}
};
@Override
public void updateLayers(OsmandMapTileView mapView, MapActivity activity) {
registerLayers(activity);
super.updateLayers(mapView, activity);
}
MapActivity activity;
public static final String ID = "osmand.osmodroid";
private static final Log log = PlatformUtil.getLog(OsMoDroidPlugin.class);
private OsmandApplication app;
IRemoteOsMoDroidService mIRemoteService;
private ServiceConnection mConnection;
private int OSMODROID_SUPPORTED_VERSION_MIN = 5;
private OsMoDroidLayer osmoDroidLayer;
protected boolean connected=false;
ArrayList<OsMoDroidLayer> osmoDroidLayerList = new ArrayList<OsMoDroidLayer>();
public ArrayList<OsMoDroidPoint> getOsMoDroidPointArrayList(int id) {
ArrayList<OsMoDroidPoint> result =new ArrayList<OsMoDroidPoint>();
try {
for (int i = 0; i < mIRemoteService.getNumberOfObjects(id); i++) {
result.add(new OsMoDroidPoint(mIRemoteService.getObjectLat(id, mIRemoteService.getObjectId(id, i)) , mIRemoteService.getObjectLon(id, mIRemoteService.getObjectId(id, i)), mIRemoteService.getObjectName(id, mIRemoteService.getObjectId(id, i)), mIRemoteService.getObjectDescription(id, mIRemoteService.getObjectId(id, i)), mIRemoteService.getObjectId(id, i),id,mIRemoteService.getObjectSpeed(id, mIRemoteService.getObjectId(id, i)),mIRemoteService.getObjectColor(id, mIRemoteService.getObjectId(id, i))));
}
} catch (RemoteException e) {
log.error(e.getMessage(), e);
}
return result;
}
@Override
public String getId() {
return ID;
}
public OsMoDroidPlugin(OsmandApplication app) {
this.app = app;
}
@Override
public String getDescription() {
return app.getString(R.string.osmodroid_plugin_description);
}
@Override
public String getName() {
return app.getString(R.string.osmodroid_plugin_name);
}
//test
@Override
public boolean init(final OsmandApplication app) {
mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mIRemoteService = IRemoteOsMoDroidService.Stub.asInterface(service);
try {
System.out.println(mIRemoteService.getVersion());
if(mIRemoteService.getVersion() < OSMODROID_SUPPORTED_VERSION_MIN) {
app.showToastMessage(R.string.osmodroid_plugin_old_ver_not_supported);
shutdown(app);
}else {
mIRemoteService.registerListener(inter);
connected=true;
}
} catch (RemoteException e) {
log.error(e.getMessage(), e);
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
connected=false;
mIRemoteService = null;
}
};
Intent serviceIntent = (new Intent("OsMoDroid.remote"));
app.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE);
return true;
}
void requestLayersFromOsMoDroid(MapActivity activity){
try {
for (int i = 0; i < mIRemoteService.getNumberOfLayers(); i++)
{
osmoDroidLayerList.add(new OsMoDroidLayer(activity, mIRemoteService.getLayerId(i),this,mIRemoteService.getLayerName( mIRemoteService.getLayerId(i)), mIRemoteService.getLayerDescription( mIRemoteService.getLayerId(i))));
}
} catch (RemoteException e) {
log.error(e.getMessage(), e);
}
}
@Override
public void registerLayers(MapActivity activity) {
this.activity=activity;
if (connected){
for (OsMoDroidLayer myOsMoDroidLayer : osmoDroidLayerList){
activity.getMapView().removeLayer(myOsMoDroidLayer);
}
osmoDroidLayerList.clear();
requestLayersFromOsMoDroid(activity);
for (OsMoDroidLayer myOsMoDroidLayer :osmoDroidLayerList){
activity.getMapView().addLayer(myOsMoDroidLayer, 4.5f);
}
}
}
@Override
public void registerLayerContextMenuActions(OsmandMapTileView mapView,
ContextMenuAdapter adapter, MapActivity mapActivity) {
for (OsMoDroidLayer myOsMoDroidLayer : osmoDroidLayerList){
adapter.registerItem(myOsMoDroidLayer.layerName);
}
super.registerLayerContextMenuActions(mapView, adapter, mapActivity);
}
@Override
public void disable(OsmandApplication app) {
shutdown(app);
}
private void shutdown(OsmandApplication app) {
if (mIRemoteService != null) {
if(connected){
try {
mIRemoteService.unregisterListener(inter);
} catch (RemoteException e) {
log.error(e.getMessage(), e);
}
}
app.unbindService(mConnection);
mIRemoteService = null;
}
}
}

View file

@ -0,0 +1,42 @@
package net.osmand.plus.osmodroid;
import net.osmand.data.LatLon;
public class OsMoDroidPoint {
@Override
public boolean equals(Object o) {
if((o instanceof OsMoDroidPoint) && this.id == ((OsMoDroidPoint)o).id )
{
return true;
}
else
{
return false;
}
}
LatLon latlon;
LatLon prevlatlon;
String name;
String description;
int id;
int layerId;
String speed="";
String color="AAAAAA";
public OsMoDroidPoint(float objectLat, float objectLon, String objectName, String objectDescription, int objectId, int layerId ,String speed, String color) {
this.latlon=new LatLon(objectLat, objectLon);
this.name=objectName;
this.description=objectDescription;
this.id=objectId;
this.layerId=layerId;
if(speed!=null){ this.speed=speed;}
if(color!=null){ this.color=color;}
}
}