From 311a944d3d9901d55c793197dd0afcf6e9f44ecd Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Wed, 11 Jun 2014 22:15:22 +0200 Subject: [PATCH] Allow save gpx points as favorites --- OsmAnd/res/values/strings.xml | 1 + .../net/osmand/plus/TargetPointsHelper.java | 4 +- .../plus/activities/AvailableGPXFragment.java | 1 + .../activities/FavouritesTreeFragment.java | 17 +++-- .../activities/IntermediatePointsDialog.java | 18 +++--- .../osmand/plus/activities/MapActivity.java | 2 +- .../plus/activities/MapActivityActions.java | 2 +- .../plus/activities/SelectedGPXFragment.java | 62 ++++++++++++++++++- .../osmand/plus/base/FailSafeFuntions.java | 2 +- .../osmand/plus/routing/RoutingHelper.java | 2 +- .../controls/MapRoutePreferencesControl.java | 4 +- 11 files changed, 91 insertions(+), 24 deletions(-) diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 926c41df1a..7a1c647165 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -9,6 +9,7 @@ 3. All your modified/created strings are in the top of the file (to make easier find what\'s translated). PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy --> + Save as group of favorites Select waypoints Text labels Loading %1$s … diff --git a/OsmAnd/src/net/osmand/plus/TargetPointsHelper.java b/OsmAnd/src/net/osmand/plus/TargetPointsHelper.java index 180524edc6..910120c03f 100644 --- a/OsmAnd/src/net/osmand/plus/TargetPointsHelper.java +++ b/OsmAnd/src/net/osmand/plus/TargetPointsHelper.java @@ -133,7 +133,7 @@ public class TargetPointsHelper { updateRouteAndReferesh(updateRoute); } - private void updateRouteAndReferesh(boolean updateRoute) { + public void updateRouteAndReferesh(boolean updateRoute) { if(updateRoute && ( routingHelper.isRouteBeingCalculated() || routingHelper.isRouteCalculated() || routingHelper.isFollowingMode() || routingHelper.isRoutePlanningMode())) { updateRoutingHelper(); @@ -141,7 +141,7 @@ public class TargetPointsHelper { updateListeners(); } - public void updateRoutingHelper() { + private void updateRoutingHelper() { LatLon start = settings.getPointToStart(); Location lastKnownLocation = ctx.getLocationProvider().getLastKnownLocation(); if((routingHelper.isFollowingMode() && lastKnownLocation != null) || start == null) { diff --git a/OsmAnd/src/net/osmand/plus/activities/AvailableGPXFragment.java b/OsmAnd/src/net/osmand/plus/activities/AvailableGPXFragment.java index 4519964017..8ae0b9d8b3 100644 --- a/OsmAnd/src/net/osmand/plus/activities/AvailableGPXFragment.java +++ b/OsmAnd/src/net/osmand/plus/activities/AvailableGPXFragment.java @@ -360,6 +360,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment { Builder b = new AlertDialog.Builder(getActivity()); if(f.exists()){ final EditText editText = new EditText(getActivity()); + editText.setPadding(7, 3, 7, 3); editText.setText(f.getName()); b.setView(editText); b.setPositiveButton(R.string.default_buttons_save, new DialogInterface.OnClickListener() { diff --git a/OsmAnd/src/net/osmand/plus/activities/FavouritesTreeFragment.java b/OsmAnd/src/net/osmand/plus/activities/FavouritesTreeFragment.java index ad0ee25127..6da9373458 100644 --- a/OsmAnd/src/net/osmand/plus/activities/FavouritesTreeFragment.java +++ b/OsmAnd/src/net/osmand/plus/activities/FavouritesTreeFragment.java @@ -61,7 +61,6 @@ import com.actionbarsherlock.view.ActionMode.Callback; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuItem; -import com.actionbarsherlock.view.MenuItem.OnActionExpandListener; import com.actionbarsherlock.widget.SearchView; import com.actionbarsherlock.widget.SearchView.OnQueryTextListener; @@ -160,6 +159,10 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { // Sort Favs by distance on Search tab, but sort alphabetically here favouritesAdapter.sort(favoritesComparator); + if(favouritesAdapter.getGroupCount() > 0 && + "".equals(favouritesAdapter.getGroup(0))) { + getListView().expandGroup(0); + } } @@ -397,7 +400,6 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { createMenuItem(menu, SELECT_DESTINATIONS_ACTION_MODE_ID, R.string.select_destination_and_intermediate_points, R.drawable.ic_action_flage_light, R.drawable.ic_action_flage_dark, MenuItem.SHOW_AS_ACTION_IF_ROOM); - updateSelectionMode(actionMode); favoritesSelected.clear(); groupsToDelete.clear(); favouritesAdapter.notifyDataSetInvalidated(); @@ -419,6 +421,7 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { if (item.getItemId() == SELECT_DESTINATIONS_ACTION_MODE_ID) { + mode.finish(); selectDestinationImpl(); } return true; @@ -435,7 +438,9 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { targetPointsHelper.getIntermediatePoints().size() + 1, getString(R.string.favorite) + ": " + fp.getName()); } - targetPointsHelper.updateRoutingHelper(); + if(getMyApplication().getRoutingHelper().isRouteCalculated()) { + targetPointsHelper.updateRouteAndReferesh(true); + } IntermediatePointsDialog.openIntermediatePointsDialog(getActivity(), getMyApplication(), false); //MapActivity.launchMapActivityMoveToTop(getActivity()); } @@ -471,6 +476,7 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { if (item.getItemId() == DELETE_ACTION_ID) { + mode.finish(); deleteFavoritesAction(); } return true; @@ -658,6 +664,7 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { Map> favoriteGroups = new LinkedHashMap>(); List groups = new ArrayList(); Filter myFilter; + public void setFavoriteGroups(Map> favoriteGroups) { this.sourceFavoriteGroups = favoriteGroups; @@ -754,7 +761,7 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { adjustIndicator(groupPosition, isExpanded, row); TextView label = (TextView) row.findViewById(R.id.category_name); final String model = getGroup(groupPosition); - label.setText(model); + label.setText(model.length() == 0? getString(R.string.favourites_activity) : model); final CheckBox ch = (CheckBox) row.findViewById(R.id.check_item); if (selectionMode) { @@ -774,6 +781,7 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { } else { groupsToDelete.remove(model); } + updateSelectionMode(actionMode); } }); } else { @@ -822,6 +830,7 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { favouritesAdapter.notifyDataSetInvalidated(); } } + updateSelectionMode(actionMode); } }); } else { diff --git a/OsmAnd/src/net/osmand/plus/activities/IntermediatePointsDialog.java b/OsmAnd/src/net/osmand/plus/activities/IntermediatePointsDialog.java index 2ea11d796a..9befcfe438 100644 --- a/OsmAnd/src/net/osmand/plus/activities/IntermediatePointsDialog.java +++ b/OsmAnd/src/net/osmand/plus/activities/IntermediatePointsDialog.java @@ -105,20 +105,20 @@ public class IntermediatePointsDialog { } }); - if (!changeOrder && intermediates.size()>1) { + if (!changeOrder && intermediates.size() > 1) { builder.setNeutralButton(R.string.intermediate_points_change_order, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { openIntermediatePointsDialog(activity, app, true); } }); - } else if(intermediates.size()>1) { - builder.setNeutralButton(R.string.intermediate_items_sort_by_distance, new Dialog.OnClickListener() { - @Override - public void onClick(DialogInterface d, int which) { - //Do nothing here. We override the onclick - } - }); + } else if (intermediates.size() > 1) { + builder.setNeutralButton(R.string.intermediate_items_sort_by_distance, new Dialog.OnClickListener() { + @Override + public void onClick(DialogInterface d, int which) { + // Do nothing here. We override the onclick + } + }); } AlertDialog dlg = builder.create(); if (changeOrder) { @@ -289,7 +289,7 @@ public class IntermediatePointsDialog { app.getTargetPointsHelper().removeWayPoint(cnt == 0, i); } } - // FIXME + // FIXME delete location when point is removed // if(mapActivity instanceof MapActivity) { // ((MapActivity) mapActivity).getMapLayers().getContextMenuLayer().setLocation(null, ""); // } diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 5a3c1dbf33..18d2358dce 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -290,7 +290,7 @@ public class MapActivity extends AccessibleActivity { !Algorithms.objectEquals(targets.getPointToNavigate(), routingHelper.getFinalLocation() )|| !Algorithms.objectEquals(targets.getIntermediatePoints(), routingHelper.getIntermediatePoints()) )) { - targets.updateRoutingHelper(); + targets.updateRouteAndReferesh(true); } app.getLocationProvider().resumeAllUpdates(); diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java index 23cdd0e9d1..3966a35c91 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java @@ -545,7 +545,7 @@ public class MapActivityActions implements DialogProvider { // then set gpx setGPXRouteParams(gpxFile); // then update start and destination point - targets.updateRoutingHelper(); + targets.updateRouteAndReferesh(true); mapActivity.getMapViewTrackingUtilities().switchToRoutePlanningMode(); mapActivity.getMapView().refreshMap(true); diff --git a/OsmAnd/src/net/osmand/plus/activities/SelectedGPXFragment.java b/OsmAnd/src/net/osmand/plus/activities/SelectedGPXFragment.java index 2cac5083db..3204de31fa 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SelectedGPXFragment.java +++ b/OsmAnd/src/net/osmand/plus/activities/SelectedGPXFragment.java @@ -2,10 +2,14 @@ package net.osmand.plus.activities; import gnu.trove.list.array.TIntArrayList; +import java.io.File; import java.text.Collator; import java.util.ArrayList; import java.util.List; +import net.osmand.access.AccessibleToast; +import net.osmand.data.FavouritePoint; +import net.osmand.plus.FavouritesDbHelper; import net.osmand.plus.GpxSelectionHelper; import net.osmand.plus.GpxSelectionHelper.GpxDisplayGroup; import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem; @@ -14,6 +18,7 @@ import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings.MetricsConstants; import net.osmand.plus.R; +import net.osmand.plus.activities.AvailableGPXFragment.LoadGpxTask; import net.osmand.plus.base.FavoriteImageDrawable; import net.osmand.util.Algorithms; import android.app.Activity; @@ -26,11 +31,13 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; +import android.widget.EditText; import android.widget.ExpandableListView; import android.widget.Filter; import android.widget.ImageView; import android.widget.SectionIndexer; import android.widget.TextView; +import android.widget.Toast; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuInflater; @@ -85,6 +92,40 @@ public class SelectedGPXFragment extends OsmandExpandableListFragment { public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) { return super.onOptionsItemSelected(item); } + + protected void saveAsFavorites(final GpxDisplayGroup model) { + Builder b = new AlertDialog.Builder(getActivity()); + final EditText editText = new EditText(getActivity()); + String name = model.getName(); + if(name.indexOf('\n') > 0) { + name = name.substring(0, name.indexOf('\n')); + } + editText.setText(name); + editText.setPadding(7, 3, 7, 3); + b.setTitle(R.string.save_as_favorites_points); + b.setView(editText); + b.setPositiveButton(R.string.default_buttons_save, new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + saveFavoritesImpl(model.getModifiableList(), editText.getText().toString()); + + } + }); + b.setNegativeButton(R.string.default_buttons_cancel, null); + b.show(); + } + + protected void saveFavoritesImpl(List modifiableList, String category) { + FavouritesDbHelper fdb = getMyApplication().getFavorites(); + for(GpxDisplayItem i : modifiableList) { + if (i.locationStart != null) { + FavouritePoint fp = new FavouritePoint(i.locationStart.lat, i.locationStart.lon, i.locationStart.name, + category); + fdb.addFavourite(fp); + } + } + } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { @@ -345,9 +386,8 @@ public class SelectedGPXFragment extends OsmandExpandableListFragment { final GpxDisplayGroup model = getGroup(groupPosition); label.setText(model.getGroupName()); final ImageView ch = (ImageView) row.findViewById(R.id.check_item); - if(model.getType() != GpxDisplayItemType.TRACK_SEGMENT) { - ch.setVisibility(View.INVISIBLE); - } else { + + if(model.getType() == GpxDisplayItemType.TRACK_SEGMENT) { ch.setVisibility(View.VISIBLE); ch.setImageDrawable(getActivity().getResources().getDrawable( app.getSettings().isLightContent() ? R.drawable.ic_action_settings_light @@ -359,11 +399,27 @@ public class SelectedGPXFragment extends OsmandExpandableListFragment { } }); + } else if(model.getType() == GpxDisplayItemType.TRACK_POINTS || + model.getType() == GpxDisplayItemType.TRACK_ROUTE_POINTS) { + ch.setVisibility(View.VISIBLE); + ch.setImageDrawable(getActivity().getResources().getDrawable( + app.getSettings().isLightContent() ? R.drawable.ic_action_fav_light + : R.drawable.ic_action_fav_dark)); + ch.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + saveAsFavorites(model); + } + + }); + } else { + ch.setVisibility(View.INVISIBLE); } return row; } + @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { diff --git a/OsmAnd/src/net/osmand/plus/base/FailSafeFuntions.java b/OsmAnd/src/net/osmand/plus/base/FailSafeFuntions.java index 09b0796b43..0eca4bcbfd 100644 --- a/OsmAnd/src/net/osmand/plus/base/FailSafeFuntions.java +++ b/OsmAnd/src/net/osmand/plus/base/FailSafeFuntions.java @@ -174,7 +174,7 @@ public class FailSafeFuntions { app.getTargetPointsHelper().setStartPoint(null, false, null); app.getSettings().FOLLOW_THE_ROUTE.set(true); routingHelper.setFollowingMode(true); - app.getTargetPointsHelper().updateRoutingHelper(); + app.getTargetPointsHelper().updateRouteAndReferesh(true); app.initVoiceCommandPlayer(ma); } diff --git a/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java b/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java index 079d27b611..416982c1af 100644 --- a/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java +++ b/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java @@ -290,7 +290,7 @@ public class RoutingHelper { recalculateRouteInBackground(false, currentLocation, finalLocation, intermediatePoints, currentGPXRoute, previousRoute.isCalculated() ? previousRoute : null); } - double projectDist = mode.hasFastSpeed() ? posTolerance : posTolerance / 2; + double projectDist = mode != null && mode.hasFastSpeed() ? posTolerance : posTolerance / 2; if(returnUpdatedLocation && locationProjection != null && currentLocation.distanceTo(locationProjection) < projectDist) { return locationProjection; } else { diff --git a/OsmAnd/src/net/osmand/plus/views/controls/MapRoutePreferencesControl.java b/OsmAnd/src/net/osmand/plus/views/controls/MapRoutePreferencesControl.java index b0f1f4bab1..821e83806c 100644 --- a/OsmAnd/src/net/osmand/plus/views/controls/MapRoutePreferencesControl.java +++ b/OsmAnd/src/net/osmand/plus/views/controls/MapRoutePreferencesControl.java @@ -183,7 +183,7 @@ public class MapRoutePreferencesControl extends MapControls { update = true; } if(update) { - tg.updateRoutingHelper(); + tg.updateRouteAndReferesh(true); } } } else if (gpxParam.id == R.string.gpx_option_calculate_first_last_segment) { @@ -361,7 +361,7 @@ public class MapRoutePreferencesControl extends MapControls { @Override public boolean processResult(GPXFile[] result) { mapActivity.getMapActions().setGPXRouteParams(result[0]); - mapActivity.getMyApplication().getTargetPointsHelper().updateRoutingHelper(); + mapActivity.getMyApplication().getTargetPointsHelper().updateRouteAndReferesh(true); updateSpinnerItems(gpxSpinner); updateParameters(); mapActivity.getRoutingHelper().recalculateRouteDueToSettingsChange();