From 51aa200990bc94536bb7ed07123acdb0fe3f1851 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Sun, 7 Feb 2016 13:22:38 +0300 Subject: [PATCH] Markers in progress --- OsmAnd/res/layout/map_markers.xml | 2 + .../src/net/osmand/data/PointDescription.java | 4 + .../src/net/osmand/plus/MapMarkersHelper.java | 32 ++++- .../src/net/osmand/plus/OsmandSettings.java | 14 ++ .../plus/activities/MapActivityActions.java | 28 ++-- .../plus/activities/MapMarkersActivity.java | 136 ++++++++++++++---- .../plus/mapcontextmenu/MapContextMenu.java | 6 +- .../controllers/MapMarkerMenuController.java | 3 +- 8 files changed, 178 insertions(+), 47 deletions(-) diff --git a/OsmAnd/res/layout/map_markers.xml b/OsmAnd/res/layout/map_markers.xml index 885ad6464f..c314497764 100644 --- a/OsmAnd/res/layout/map_markers.xml +++ b/OsmAnd/res/layout/map_markers.xml @@ -10,5 +10,7 @@ android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" + android:divider="@null" + android:drawSelectorOnTop="true" tools:listitem="@layout/waypoint_reached"/> diff --git a/OsmAnd/src/net/osmand/data/PointDescription.java b/OsmAnd/src/net/osmand/data/PointDescription.java index 4bd1cc441a..a0c05bd6ee 100644 --- a/OsmAnd/src/net/osmand/data/PointDescription.java +++ b/OsmAnd/src/net/osmand/data/PointDescription.java @@ -192,6 +192,10 @@ public class PointDescription { return POINT_TYPE_TARGET.equals(type); } + public boolean isMapMarker() { + return POINT_TYPE_MAP_MARKER.equals(type); + } + public boolean isParking() { return POINT_TYPE_PARKING_MARKER.equals(type); } diff --git a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java index 1fd859c3b2..452d5cc3c9 100644 --- a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java +++ b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java @@ -10,11 +10,15 @@ import net.osmand.plus.GeocodingLookupService; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; +import net.osmand.util.Algorithms; import java.util.ArrayList; import java.util.List; public class MapMarkersHelper { + public static final int MAP_MARKERS_COLORS_COUNT = 5; + public static final int MAP_MARKERS_HISTORY_LIMIT = 30; + private List mapMarkers = new ArrayList<>(); private List mapMarkersHistory = new ArrayList<>(); private OsmandSettings settings; @@ -176,7 +180,7 @@ public class MapMarkersHelper { public void removeActiveMarkers() { cancelAddressRequests(); - settings.clearIntermediatePoints(); + settings.clearActiveMapMarkers(); mapMarkers.clear(); readFromSettings(); refresh(); @@ -185,12 +189,36 @@ public class MapMarkersHelper { public void removeMarkersHistory() { cancelAddressRequests(); - settings.clearIntermediatePoints(); + settings.clearMapMarkersHistory(); mapMarkersHistory.clear(); readFromSettings(); refresh(); } + public void addMapMarker(final LatLon point, PointDescription historyName) { + if (point != null) { + final PointDescription pointDescription; + if (historyName == null) { + pointDescription = new PointDescription(PointDescription.POINT_TYPE_LOCATION, ""); + } else { + pointDescription = historyName; + } + if (pointDescription.isLocation() && Algorithms.isEmpty(pointDescription.getName())) { + pointDescription.setName(PointDescription.getSearchAddressStr(ctx)); + } + int colorIndex; + if (mapMarkers.size() > 0) { + colorIndex = (mapMarkers.get(mapMarkers.size() - 1).colorIndex + 1) % MAP_MARKERS_COLORS_COUNT; + } else { + colorIndex = 0; + } + settings.insertMapMarker(point.getLatitude(), point.getLongitude(), + pointDescription, colorIndex, mapMarkers.size()); + } + readFromSettings(); + refresh(); + } + public void addListener(StateChangedListener l) { listeners.add(l); } diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 7530e57995..c5ed649abe 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -1529,6 +1529,20 @@ public class OsmandSettings { return settingsAPI.edit(globalPreferences).remove(INTERMEDIATE_POINTS).remove(INTERMEDIATE_POINTS_DESCRIPTION).commit(); } + public boolean clearActiveMapMarkers() { + return settingsAPI.edit(globalPreferences) + .remove(MAP_MARKERS_POINT) + .remove(MAP_MARKERS_DESCRIPTION) + .remove(MAP_MARKERS_COLOR).commit(); + } + + public boolean clearMapMarkersHistory() { + return settingsAPI.edit(globalPreferences) + .remove(MAP_MARKERS_HISTORY_POINT) + .remove(MAP_MARKERS_HISTORY_DESCRIPTION) + .remove(MAP_MARKERS_HISTORY_COLOR).commit(); + } + public final CommonPreference USE_INTERMEDIATE_POINTS_NAVIGATION = new BooleanPreference("use_intermediate_points_navigation", false).makeGlobal().cache(); diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java index 6b47af84fa..33b3ad8723 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java @@ -33,6 +33,7 @@ import net.osmand.plus.ContextMenuAdapter.OnContextMenuClick; import net.osmand.plus.GPXUtilities; import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; +import net.osmand.plus.MapMarkersHelper; import net.osmand.plus.OsmAndLocationProvider; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; @@ -97,7 +98,6 @@ public class MapActivityActions implements DialogProvider { openIntermediateEditPointsDialog(); } - */ public void addAsTarget(double latitude, double longitude, PointDescription pd) { TargetPointsHelper targets = getMyApplication().getTargetPointsHelper(); @@ -105,6 +105,13 @@ public class MapActivityActions implements DialogProvider { pd); openIntermediatePointsDialog(); } + */ + + public void addMapMarker(double latitude, double longitude, PointDescription pd) { + MapMarkersHelper markersHelper = getMyApplication().getMapMarkersHelper(); + markersHelper.addMapMarker(new LatLon(latitude, longitude), pd); + openMapMarkersActivity(); + } public void editWaypoints() { openIntermediatePointsDialog(); @@ -570,11 +577,8 @@ public class MapActivityActions implements DialogProvider { @Override public boolean onContextMenuClick(ArrayAdapter adapter, int itemId, int pos, boolean isChecked) { MapActivity.clearPrevActivityIntent(); - Intent newIntent = new Intent(mapActivity, mapActivity.getMyApplication().getAppCustomization() - .getMapMarkersActivity()); - newIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); - mapActivity.startActivity(newIntent); - return false; + openMapMarkersActivity(); + return true; } }).reg(); optionsMenuHelper.item(R.string.get_directions).iconColor(R.drawable.ic_action_gdirections_dark) @@ -742,17 +746,17 @@ public class MapActivityActions implements DialogProvider { return optionsMenuHelper; } + public void openMapMarkersActivity() { + Intent newIntent = new Intent(mapActivity, mapActivity.getMyApplication().getAppCustomization() + .getMapMarkersActivity()); + newIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); + mapActivity.startActivity(newIntent); + } public void openIntermediatePointsDialog() { mapActivity.getDashboard().setDashboardVisibility(true, DashboardType.WAYPOINTS); } - /* - public void openIntermediateEditPointsDialog() { - mapActivity.getDashboard().setDashboardVisibility(true, DashboardType.WAYPOINTS_EDIT); - } - */ - public void openRoutePreferencesDialog() { mapActivity.getDashboard().setDashboardVisibility(true, DashboardType.ROUTE_PREFERENCES); } diff --git a/OsmAnd/src/net/osmand/plus/activities/MapMarkersActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapMarkersActivity.java index d6c0d53e2a..f24a25676e 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapMarkersActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapMarkersActivity.java @@ -21,19 +21,23 @@ import net.osmand.plus.MapMarkersHelper.MapMarker; import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; +import net.osmand.plus.views.controls.DynamicListView; +import net.osmand.plus.views.controls.DynamicListViewCallbacks; import net.osmand.plus.views.controls.ListDividerShape; import net.osmand.plus.views.controls.StableArrayAdapter; +import net.osmand.plus.views.controls.SwipeDismissListViewTouchListener; import net.osmand.util.Algorithms; import net.osmand.util.MapUtils; import java.util.ArrayList; import java.util.List; -public class MapMarkersActivity extends OsmandListActivity { +public class MapMarkersActivity extends OsmandListActivity implements DynamicListViewCallbacks { public static final int ACTIVE_MARKERS = 0; public static final int MARKERS_HISTORY = 1; + private SwipeDismissListViewTouchListener swipeDismissListener; private boolean nightMode; @Override @@ -43,6 +47,56 @@ public class MapMarkersActivity extends OsmandListActivity { setContentView(R.layout.map_markers); getSupportActionBar().setTitle(R.string.map_markers); + ((DynamicListView) getListView()).setDynamicListViewCallbacks(this); + swipeDismissListener = new SwipeDismissListViewTouchListener(getListView(), + new SwipeDismissListViewTouchListener.DismissCallbacks() { + @Override + public boolean canDismiss(int position) { + List activeObjects = getListAdapter().getActiveObjects(); + Object obj = getListAdapter().getItem(position); + return activeObjects.contains(obj); + } + + @Override + public SwipeDismissListViewTouchListener.Undoable onDismiss(final int position) { + final Object item; + final StableArrayAdapter stableAdapter = getListAdapter(); + final int activeObjPos; + item = stableAdapter.getItem(position); + + stableAdapter.setNotifyOnChange(false); + stableAdapter.remove(item); + stableAdapter.getObjects().remove(item); + activeObjPos = stableAdapter.getActiveObjects().indexOf(item); + stableAdapter.getActiveObjects().remove(item); + stableAdapter.refreshData(); + stableAdapter.notifyDataSetChanged(); + + return new SwipeDismissListViewTouchListener.Undoable() { + @Override + public void undo() { + if (item != null) { + stableAdapter.setNotifyOnChange(false); + stableAdapter.insert(item, position); + stableAdapter.getObjects().add(position, item); + stableAdapter.getActiveObjects().add(activeObjPos, item); + stableAdapter.refreshData(); + } + } + }; + } + + @Override + public void onHidePopup() { + StableArrayAdapter stableAdapter = getListAdapter(); + stableAdapter.refreshData(); + // do delete + if (stableAdapter.getActiveObjects().size() == 0) { + finish(); + } + } + }); + //nightMode = getMyApplication().getDaynightHelper().isNightModeForMapControls(); nightMode = !getMyApplication().getSettings().isLightContent(); setListAdapter(getMapMarkersListAdapter()); @@ -55,8 +109,10 @@ public class MapMarkersActivity extends OsmandListActivity { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { - getListAdapter().getItem(position); - // + Object obj = getListAdapter().getItem(position); + if (obj instanceof MapMarker) { + showOnMap((MapMarker) obj); + } } @Override @@ -78,6 +134,12 @@ public class MapMarkersActivity extends OsmandListActivity { dividers = getCustomDividers(getObjects()); } + @Override + public boolean isEnabled(int position) { + Object obj = getItem(position); + return obj instanceof MapMarker; + } + @Override public View getView(final int position, View convertView, ViewGroup parent) { // User super class to create the View @@ -176,18 +238,21 @@ public class MapMarkersActivity extends OsmandListActivity { v.findViewById(R.id.check_item).setVisibility(View.GONE); v.findViewById(R.id.ProgressBar).setVisibility(View.GONE); - final Button btn = (Button) v.findViewById(R.id.header_button); - btn.setTextColor(!nightMode ? getResources().getColor(R.color.map_widget_blue) - : getResources().getColor(R.color.osmand_orange)); - btn.setText(getString(R.string.shared_string_clear)); - btn.setVisibility(View.VISIBLE); - btn.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - getListAdapter().notifyDataSetInvalidated(); - reloadListAdapter(); - } - }); + if (type == MARKERS_HISTORY) { + final Button btn = (Button) v.findViewById(R.id.header_button); + btn.setTextColor(!nightMode ? getResources().getColor(R.color.map_widget_blue) + : getResources().getColor(R.color.osmand_orange)); + btn.setText(getString(R.string.shared_string_clear)); + btn.setVisibility(View.VISIBLE); + btn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + getListAdapter().notifyDataSetInvalidated(); + getMyApplication().getMapMarkersHelper().removeMarkersHistory(); + reloadListAdapter(); + } + }); + } TextView tv = (TextView) v.findViewById(R.id.header_text); AndroidUtils.setTextPrimaryColor(this, tv, nightMode); @@ -214,26 +279,19 @@ public class MapMarkersActivity extends OsmandListActivity { TextView text = (TextView) localView.findViewById(R.id.waypoint_text); AndroidUtils.setTextPrimaryColor(this, text, nightMode); TextView textShadow = (TextView) localView.findViewById(R.id.waypoint_text_shadow); - localView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - showOnMap(marker); - } - }); - TextView textDist = (TextView) localView.findViewById(R.id.waypoint_dist); ((ImageView) localView.findViewById(R.id.waypoint_icon)) - .setImageDrawable(getMapMarkerIcon(marker.colorIndex)); + .setImageDrawable(getMapMarkerIcon(app, marker.colorIndex)); LatLon lastKnownMapLocation = app.getSettings().getLastKnownMapLocation(); int dist = (int) (MapUtils.getDistance(marker.getLatitude(), marker.getLongitude(), lastKnownMapLocation.getLatitude(), lastKnownMapLocation.getLongitude())); - if (dist > 0) { - textDist.setText(OsmAndFormatter.getFormattedDistance(dist, app)); - } else { - textDist.setText(""); - } + //if (dist > 0) { + textDist.setText(OsmAndFormatter.getFormattedDistance(dist, app)); + //} else { + // textDist.setText(""); + //} localView.findViewById(R.id.waypoint_deviation).setVisibility(View.GONE); @@ -250,6 +308,8 @@ public class MapMarkersActivity extends OsmandListActivity { } text.setText(descr); + localView.findViewById(R.id.waypoint_desc_text).setVisibility(View.GONE); + /* String pointDescription = ""; TextView descText = (TextView) localView.findViewById(R.id.waypoint_desc_text); if (descText != null) { @@ -266,6 +326,7 @@ public class MapMarkersActivity extends OsmandListActivity { if (descText != null) { descText.setText(pointDescription); } + */ } public void showOnMap(MapMarker marker) { @@ -332,8 +393,8 @@ public class MapMarkersActivity extends OsmandListActivity { return activeObjects; } - private Drawable getMapMarkerIcon(int colorIndex) { - IconsCache iconsCache = getMyApplication().getIconsCache(); + public static Drawable getMapMarkerIcon(OsmandApplication app, int colorIndex) { + IconsCache iconsCache = app.getIconsCache(); switch (colorIndex) { case 0: return iconsCache.getIcon(R.drawable.map_marker_blue); @@ -349,4 +410,19 @@ public class MapMarkersActivity extends OsmandListActivity { return iconsCache.getIcon(R.drawable.map_marker_blue); } } + + @Override + public void onWindowVisibilityChanged(int visibility) { + if (visibility != View.VISIBLE && swipeDismissListener != null) { + swipeDismissListener.discardUndo(); + } + } + + @Override + public void onItemsSwapped(List items) { + } + + @Override + public void onItemSwapping(int position) { + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java index 4fd3711339..e15bf69579 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java @@ -470,9 +470,11 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL public void buttonWaypointPressed() { if (pointDescription.isDestination()) { mapActivity.getMapActions().editWaypoints(); + } else if (pointDescription.isMapMarker()) { + mapActivity.getMapActions().openMapMarkersActivity(); } else { - mapActivity.getMapActions().addAsTarget(latLon.getLatitude(), latLon.getLongitude(), - getPointDescriptionForTarget()); + mapActivity.getMapActions().addMapMarker(latLon.getLatitude(), latLon.getLongitude(), + pointDescription); } close(); } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/MapMarkerMenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/MapMarkerMenuController.java index aa5e32cebf..b70b786ecd 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/MapMarkerMenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/MapMarkerMenuController.java @@ -8,6 +8,7 @@ import net.osmand.plus.MapMarkersHelper.MapMarker; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.activities.MapMarkersActivity; import net.osmand.plus.mapcontextmenu.MenuBuilder; import net.osmand.plus.mapcontextmenu.MenuController; import net.osmand.util.Algorithms; @@ -59,7 +60,7 @@ public class MapMarkerMenuController extends MenuController { @Override public Drawable getLeftIcon() { - return getIconOrig(R.drawable.list_intermediate); + return MapMarkersActivity.getMapMarkerIcon(getMapActivity().getMyApplication(), mapMarker.colorIndex); } @Override