Code refactoring & DeleteDialog added
This commit is contained in:
parent
70228e0772
commit
439e65aa40
3 changed files with 114 additions and 73 deletions
|
@ -44,9 +44,10 @@
|
|||
<string name="osmand_monitoring_name">Tracking</string>
|
||||
<string name="osmand_extra_settings_description">This plugin provides advanced map configuration and some device-specific settings.</string>
|
||||
<string name="osmand_extra_settings_name">Advanced Settings</string>
|
||||
<string name="osmand_parking_position_description">This plugin facilitates a location of a parked car/bycicle.</string>
|
||||
<string name="osmand_parking_position_description">The position of your parked car.</string>
|
||||
<string name="osmand_parking_position_name">Parking point</string>
|
||||
<string name="osmand_parking_position_toast">"The position of your parked car."</string>
|
||||
<string name="osmand_parking_plugin_description">This plugin allows to store the location of your parked car.</string>
|
||||
<string name="osmand_parking_plugin_name">Parking Position Plugin</string>
|
||||
<string name="osmand_development_plugin_description">This plugin enables development and debugging features like animated navigation or rendering performance display.</string>
|
||||
<string name="osmand_development_plugin_name">Osmand Development</string>
|
||||
<string name="plugins_screen">Plugin Manager</string>
|
||||
|
@ -938,7 +939,8 @@
|
|||
|
||||
<string name="context_menu_item_navigate_point">Set as destination</string>
|
||||
<string name="context_menu_item_add_favorite">Add to favorites</string>
|
||||
<string name="context_menu_item_add_parking_point">Add parking place</string>
|
||||
<string name="context_menu_item_add_parking_point">Add location of a parked car</string>
|
||||
<string name="context_menu_item_delete_parking_point">Delete location of a parked car</string>
|
||||
<string name="context_menu_item_update_map">Update map</string>
|
||||
<string name="context_menu_item_open_bug">Open OSM bug</string>
|
||||
<string name="context_menu_item_create_poi">Create POI</string>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package net.osmand.plus.parkingpoint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import net.osmand.OsmAndFormatter;
|
||||
|
@ -33,28 +34,35 @@ import android.view.View;
|
|||
import android.view.WindowManager;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class ParkingPositionLayer extends OsmandMapLayer implements ContextMenuLayer.IContextMenuProvider{
|
||||
/**
|
||||
* Class represents a layer which depicts the position of the parked car
|
||||
* @author Alena Fedasenka
|
||||
* @see ParkingPositionPlugin
|
||||
*
|
||||
*/
|
||||
public class ParkingPositionLayer extends OsmandMapLayer implements ContextMenuLayer.IContextMenuProvider {
|
||||
/**
|
||||
* magic number so far
|
||||
*/
|
||||
private static final int radius = 16;
|
||||
|
||||
private Paint bitmapPaint;
|
||||
|
||||
private LatLon parkingPoint = null;
|
||||
|
||||
private DisplayMetrics dm;
|
||||
private Bitmap parkingPosition;
|
||||
|
||||
|
||||
private final MapActivity map;
|
||||
private OsmandMapTileView view;
|
||||
private OsmandSettings settings;
|
||||
|
||||
private Paint bitmapPaint;
|
||||
private Paint paintText;
|
||||
private Paint paintSubText;
|
||||
|
||||
private OsmandSettings settings;
|
||||
|
||||
private Bitmap parkingPosition;
|
||||
|
||||
private TextInfoControl parkingPlaceControl;
|
||||
|
||||
public ParkingPositionLayer(MapActivity map){
|
||||
public ParkingPositionLayer(MapActivity map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
|
@ -107,18 +115,6 @@ public class ParkingPositionLayer extends OsmandMapLayer implements ContextMenuL
|
|||
canvas.drawBitmap(parkingPosition, locationX - marginX, locationY - marginY, bitmapPaint);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isLocationVisible(double latitude, double longitude){
|
||||
if(parkingPoint == null || view == null){
|
||||
return false;
|
||||
}
|
||||
return view.isPointOnTheRotatedMap(latitude, longitude);
|
||||
}
|
||||
|
||||
public void setParkingPoint(LatLon point) {
|
||||
this.parkingPoint = point;
|
||||
refreshMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroyLayer() {
|
||||
|
@ -131,9 +127,11 @@ public class ParkingPositionLayer extends OsmandMapLayer implements ContextMenuL
|
|||
|
||||
@Override
|
||||
public boolean onSingleTap(PointF point) {
|
||||
if(isParkingPointPressed(point)){
|
||||
List <LatLon> parkPos = new ArrayList<LatLon>();
|
||||
getParkingFromPoint(point, parkPos);
|
||||
if(!parkPos.isEmpty()){
|
||||
StringBuilder res = new StringBuilder();
|
||||
res.append(view.getContext().getString(R.string.osmand_parking_position_toast));
|
||||
res.append(view.getContext().getString(R.string.osmand_parking_position_description));
|
||||
AccessibleToast.makeText(view.getContext(), res.toString(), Toast.LENGTH_LONG).show();
|
||||
return true;
|
||||
}
|
||||
|
@ -142,57 +140,71 @@ public class ParkingPositionLayer extends OsmandMapLayer implements ContextMenuL
|
|||
|
||||
@Override
|
||||
public boolean onLongPressEvent(PointF point) {
|
||||
if (isParkingPointPressed(point)) {
|
||||
Builder confirm = new AlertDialog.Builder(map);
|
||||
confirm.setPositiveButton(R.string.default_buttons_yes,
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
settings.clearParkingPosition();
|
||||
refreshMap();
|
||||
}
|
||||
});
|
||||
confirm.setCancelable(true);
|
||||
confirm.setNegativeButton(R.string.default_buttons_cancel, null);
|
||||
confirm.setMessage("Do you want to remove the parking position?");
|
||||
confirm.show();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isParkingPointPressed(PointF point) {
|
||||
if (parkingPoint != null) {
|
||||
int ex = (int) point.x;
|
||||
int ey = (int) point.y;
|
||||
int x = view.getRotatedMapXForPoint(settings.getParkingPosition().getLatitude(), settings.getParkingPosition().getLongitude());
|
||||
int y = view.getRotatedMapYForPoint(settings.getParkingPosition().getLatitude(), settings.getParkingPosition().getLongitude());
|
||||
if (Math.abs(x - ex) <= radius && Math.abs(y - ey) <= radius) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void collectObjectsFromPoint(PointF point, List<Object> o) {
|
||||
getParkingFromPoint(point, o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LatLon getObjectLocation(Object o) {
|
||||
return settings.getParkingPosition();
|
||||
return parkingPoint;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getObjectDescription(Object o) {
|
||||
return "tra-ta-ta";
|
||||
return view.getContext().getString(R.string.osmand_parking_position_description);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getObjectName(Object o) {
|
||||
return map.getString(R.string.osmand_parking_position_name);
|
||||
return view.getContext().getString(R.string.osmand_parking_position_name);
|
||||
}
|
||||
|
||||
public void setParkingPoint(LatLon point) {
|
||||
this.parkingPoint = point;
|
||||
if (view != null && view.getLayers().contains(ParkingPositionLayer.this)) {
|
||||
view.refreshMap();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param latitude
|
||||
* @param longitude
|
||||
* @return true if the parking point is located on a visible part of map
|
||||
*/
|
||||
private boolean isLocationVisible(double latitude, double longitude){
|
||||
if(parkingPoint == null || view == null){
|
||||
return false;
|
||||
}
|
||||
return view.isPointOnTheRotatedMap(latitude, longitude);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param point
|
||||
* @param parkingPosition is in this case not necessarily has to be a list,
|
||||
* but it's also used in method <link>collectObjectsFromPoint(PointF point, List<Object> o)</link>
|
||||
*/
|
||||
private void getParkingFromPoint(PointF point, List<? super LatLon> parkingPosition) {
|
||||
if (parkingPoint != null && view != null) {
|
||||
int ex = (int) point.x;
|
||||
int ey = (int) point.y;
|
||||
int x = view.getRotatedMapXForPoint(settings.getParkingPosition().getLatitude(), settings.getParkingPosition().getLongitude());
|
||||
int y = view.getRotatedMapYForPoint(settings.getParkingPosition().getLatitude(), settings.getParkingPosition().getLongitude());
|
||||
if (Math.abs(x - ex) <= radius && Math.abs(y - ey) <= radius) {
|
||||
parkingPosition.add(parkingPoint);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the control to be added on a MapInfoLayer
|
||||
* that shows a distance between
|
||||
* the current position on the map
|
||||
* and the location of the parked car
|
||||
*/
|
||||
private TextInfoControl createParkingPlaceInfoControl() {
|
||||
parkingPlaceControl = new TextInfoControl(map, 0, paintText, paintSubText) {
|
||||
private float[] calculations = new float[1];
|
||||
|
@ -231,6 +243,19 @@ public class ParkingPositionLayer extends OsmandMapLayer implements ContextMenuL
|
|||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method.
|
||||
* @param oldDist
|
||||
* @param dist
|
||||
* @return
|
||||
*/
|
||||
private boolean distChanged(int oldDist, int dist){
|
||||
if(oldDist != 0 && oldDist - dist < 100 && Math.abs(((float) dist - oldDist)/oldDist) < 0.01){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
parkingPlaceControl.setOnClickListener(new View.OnClickListener() {
|
||||
|
@ -249,16 +274,21 @@ public class ParkingPositionLayer extends OsmandMapLayer implements ContextMenuL
|
|||
return parkingPlaceControl;
|
||||
}
|
||||
|
||||
public boolean distChanged(int oldDist, int dist){
|
||||
if(oldDist != 0 && oldDist - dist < 100 && Math.abs(((float) dist - oldDist)/oldDist) < 0.01){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void refreshMap(){
|
||||
if (view != null && view.getLayers().contains(ParkingPositionLayer.this)) {
|
||||
view.refreshMap();
|
||||
}
|
||||
public void showDeleteDialog() {
|
||||
Builder confirm = new AlertDialog.Builder(map);
|
||||
confirm.setTitle("Delete parking location");
|
||||
confirm.setMessage("Do you want to remove the location of the parked car?");
|
||||
confirm.setCancelable(true);
|
||||
confirm.setPositiveButton(R.string.default_buttons_yes,
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
// TODO refresh map
|
||||
settings.clearParkingPosition();
|
||||
}
|
||||
});
|
||||
confirm.setNegativeButton(R.string.default_buttons_cancel, null);
|
||||
confirm.show();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -12,6 +12,11 @@ import net.osmand.plus.views.OsmandMapTileView;
|
|||
import android.content.DialogInterface;
|
||||
import android.preference.PreferenceScreen;
|
||||
|
||||
/**
|
||||
*
|
||||
* The plugin facilitates a storage of the location of a parked car
|
||||
* @author Alena Fedasenka
|
||||
*/
|
||||
public class ParkingPositionPlugin extends OsmandPlugin {
|
||||
|
||||
private static final String ID = "osmand.parking.position";
|
||||
|
@ -36,12 +41,12 @@ public class ParkingPositionPlugin extends OsmandPlugin {
|
|||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return app.getString(R.string.osmand_parking_position_description);
|
||||
return app.getString(R.string.osmand_parking_plugin_description);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return app.getString(R.string.osmand_parking_position_name);
|
||||
return app.getString(R.string.osmand_parking_plugin_name);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -63,17 +68,21 @@ public class ParkingPositionPlugin extends OsmandPlugin {
|
|||
}
|
||||
@Override
|
||||
public void registerMapContextMenuActions(final MapActivity mapActivity, final double latitude, final double longitude, ContextMenuAdapter adapter, Object selectedObj) {
|
||||
OnContextMenuClick listener = new OnContextMenuClick() {
|
||||
OnContextMenuClick addListener = new OnContextMenuClick() {
|
||||
@Override
|
||||
public void onContextMenuClick(int resId, int pos, boolean isChecked, DialogInterface dialog) {
|
||||
if (resId == R.string.context_menu_item_add_parking_point){
|
||||
if (resId == R.string.context_menu_item_add_parking_point) {
|
||||
settings.setParkingPosition(latitude, longitude);
|
||||
if (mapActivity.getMapView().getLayers().contains(parkingLayer))
|
||||
parkingLayer.setParkingPoint(settings.getParkingPosition());
|
||||
} else if ((resId == R.string.context_menu_item_delete_parking_point)){
|
||||
parkingLayer.showDeleteDialog();
|
||||
}
|
||||
}
|
||||
};
|
||||
adapter.registerItem(R.string.context_menu_item_add_parking_point, 0, listener, -1);
|
||||
adapter.registerItem(R.string.context_menu_item_add_parking_point, 0, addListener, -1);
|
||||
if (settings.getParkingPosition() != null)
|
||||
adapter.registerItem(R.string.context_menu_item_delete_parking_point, 0, addListener, -1);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in a new issue