diff --git a/OsmAnd/res/layout/favorites_list_item.xml b/OsmAnd/res/layout/favorites_list_item.xml index 3e45abbe27..a5e82f59e9 100644 --- a/OsmAnd/res/layout/favorites_list_item.xml +++ b/OsmAnd/res/layout/favorites_list_item.xml @@ -63,8 +63,8 @@ android:id="@+id/direction" android:layout_width="@dimen/dashFavDirectionSize" android:layout_height="@dimen/dashFavDirectionSize" - android:layout_gravity="center_vertical" - android:visibility="gone" /> + android:src="@drawable/ic_destination_arrow_white" + android:layout_gravity="center_vertical"/> points = new ArrayList(helper.getFavouritePoints()); - final Collator ci = java.text.Collator.getInstance(); - final boolean distance = args.containsKey("DISTANCE"); - Collections.sort(points, new Comparator() { + final FavouritesAdapter favouritesAdapter = new FavouritesAdapter(activity, + ((OsmandApplication) activity.getApplication()).getFavorites().getFavouritePoints()); + final Dialog[] dlgHolder = new Dialog[1]; + OnItemClickListener click = new AdapterView.OnItemClickListener() { @Override - public int compare(FavouritePoint o1, FavouritePoint o2) { - if (distance && activity instanceof MapActivity) { - float f1 = (float) MapUtils.getDistance(((MapActivity) activity).getMapLocation(), o1.getLatitude(), - o1.getLongitude()); - float f2 = (float) MapUtils.getDistance(((MapActivity) activity).getMapLocation(), o2.getLatitude(), - o2.getLongitude()); - return Float.compare(f1, f2); + public void onItemClick(AdapterView parent, View view, int position, long id) { + FavouritePoint fp = favouritesAdapter.getItem(position); + if(dlgHolder != null && dlgHolder.length > 0 && dlgHolder[0] != null) { + dlgHolder[0].dismiss(); } - return ci.compare(o1.getCategory() + " " + o1.getName(), o2.getCategory() + " " + o2.getName()); - } - }); - final String[] names = new String[points.size()]; - if(points.size() == 0){ - AccessibleToast.makeText(activity, activity.getString(R.string.fav_points_not_exist), Toast.LENGTH_SHORT).show(); - return null; - } - - Builder b = new AlertDialog.Builder(activity); - final FavouritePoint[] favs = new FavouritePoint[points.size()]; - Iterator it = points.iterator(); - int i=0; - while (it.hasNext()) { - FavouritePoint fp = it.next(); - // filter gpx points - favs[i] = fp; - if(fp.getCategory().trim().length() ==0){ - names[i] = fp.getName(); - } else { - names[i] = fp.getCategory() + ": " + fp.getName(); - } - if(activity instanceof MapActivity) { - names[i] += " " + OsmAndFormatter.getFormattedDistance( - (float) MapUtils.getDistance(((MapActivity) activity).getMapLocation(), fp.getLatitude(), - fp.getLongitude()), ((MapActivity) activity).getMyApplication()); - } - i++; - } - final int layout; - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { - layout = R.layout.list_menu_item; - } else { - layout = R.layout.list_menu_item_native; - } - final ArrayAdapter listAdapter = new ArrayAdapter(activity, layout, R.id.title, - names) { - @Override - public View getView(final int position, View convertView, ViewGroup parent) { - // User super class to create the View - View v = convertView; - if (v == null) { - v = activity.getLayoutInflater().inflate(layout, null); - int vl = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 32, activity.getResources() - .getDisplayMetrics()); - final LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(vl, vl); - ll.setMargins(vl / 4, vl / 4, vl / 4, vl / 4); - v.findViewById(R.id.icon).setLayoutParams(ll); - } - ImageView icon = (ImageView) v.findViewById(R.id.icon); - FavouritePoint fp = points.get(position); - icon.setImageDrawable(FavoriteImageDrawable.getOrCreate(activity, fp.getColor())); - - icon.setVisibility(View.VISIBLE); - TextView tv = (TextView) v.findViewById(R.id.title); - tv.setText(names[position]); - tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); - final CheckBox ch = ((CheckBox) v.findViewById(R.id.check_item)); - ch.setVisibility(View.INVISIBLE); - return v; - } - }; - b.setAdapter(listAdapter, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - FavouritePoint fv = favs[which]; FavouritePoint point = (FavouritePoint) args.getSerializable(KEY_FAVORITE); - if (helper.editFavourite(fv, point.getLatitude(), point.getLongitude())) { + if (helper.editFavourite(fp, point.getLatitude(), point.getLongitude())) { AccessibleToast.makeText(activity, activity.getString(R.string.fav_points_edited), Toast.LENGTH_SHORT).show(); } @@ -134,24 +62,16 @@ public class FavoriteDialogs { ((MapActivity) activity).getMapView().refreshMap(); } } - }); + }; if (activity instanceof MapActivity) { - b.setPositiveButton(distance ? R.string.sort_by_name : R.string.sort_by_distance, - new DialogInterface.OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - if (distance) { - args.remove("DISTANCE"); - } else { - args.putBoolean("DISTANCE", true); - } - createReplaceFavouriteDialog(activity, args).show(); - } - }); + favouritesAdapter.updateLocation(((MapActivity) activity).getMapLocation()); } - AlertDialog al = b.create(); - return al; + final String[] names = new String[points.size()]; + if(points.size() == 0){ + AccessibleToast.makeText(activity, activity.getString(R.string.fav_points_not_exist), Toast.LENGTH_SHORT).show(); + return null; + } + return showFavoritesDialog(activity, favouritesAdapter, click, null, dlgHolder, true); } public static void prepareAddFavouriteDialog(Activity activity, Dialog dialog, Bundle args, double lat, double lon, PointDescription desc) { @@ -242,4 +162,49 @@ public class FavoriteDialogs { return builder.create(); } + public static final AlertDialog showFavoritesDialog( + final Context uiContext, + final FavouritesAdapter favouritesAdapter, final OnItemClickListener click, + final OnDismissListener dismissListener, final Dialog[] dialogHolder, final boolean sortByDist) { + ListView listView = new ListView(uiContext); + Builder bld = new AlertDialog.Builder(uiContext); + final Collator inst = Collator.getInstance(); + favouritesAdapter.sort(new Comparator() { + + @Override + public int compare(FavouritePoint lhs, FavouritePoint rhs) { + if (sortByDist) { + if (favouritesAdapter.getLocation() == null) { + return 0; + } + double ld = MapUtils.getDistance(favouritesAdapter.getLocation(), lhs.getLatitude(), + lhs.getLongitude()); + double rd = MapUtils.getDistance(favouritesAdapter.getLocation(), rhs.getLatitude(), + rhs.getLongitude()); + return Double.compare(ld, rd); + } + return inst.compare(lhs.getName(), rhs.getName()); + } + }); + + listView.setAdapter(favouritesAdapter); + listView.setOnItemClickListener(click); + bld.setPositiveButton(sortByDist ? R.string.sort_by_name : + R.string.sort_by_distance, new OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + showFavoritesDialog(uiContext, favouritesAdapter, click, dismissListener, dialogHolder, !sortByDist); + } + }); + bld.setNegativeButton(R.string.shared_string_cancel, null); + bld.setView(listView); + AlertDialog dlg = bld.show(); + if(dialogHolder != null) { + dialogHolder[0] = dlg; + } + dlg.setOnDismissListener(dismissListener); + return dlg; + } + } diff --git a/OsmAnd/src/net/osmand/plus/views/controls/MapRouteInfoControl.java b/OsmAnd/src/net/osmand/plus/views/controls/MapRouteInfoControl.java index af3e66b0a1..dc2de3faf0 100644 --- a/OsmAnd/src/net/osmand/plus/views/controls/MapRouteInfoControl.java +++ b/OsmAnd/src/net/osmand/plus/views/controls/MapRouteInfoControl.java @@ -1,6 +1,8 @@ package net.osmand.plus.views.controls; +import java.text.Collator; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import net.osmand.data.FavouritePoint; @@ -14,22 +16,25 @@ import net.osmand.plus.R; import net.osmand.plus.TargetPointsHelper; import net.osmand.plus.TargetPointsHelper.TargetPoint; import net.osmand.plus.activities.FavoritesListFragment.FavouritesAdapter; -import net.osmand.plus.activities.search.SearchActivity; -import net.osmand.plus.activities.search.SearchAddressActivity; -import net.osmand.plus.activities.search.SearchAddressFragment; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.ShowRouteInfoActivity; +import net.osmand.plus.activities.search.SearchAddressActivity; +import net.osmand.plus.activities.search.SearchAddressFragment; import net.osmand.plus.development.OsmandDevelopmentPlugin; +import net.osmand.plus.dialogs.FavoriteDialogs; import net.osmand.plus.routing.RouteDirectionInfo; import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.routing.RoutingHelper.IRouteInformationListener; import net.osmand.plus.views.ContextMenuLayer; import net.osmand.plus.views.MapControlsLayer; import net.osmand.plus.views.OsmandMapTileView; +import net.osmand.util.MapUtils; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.Dialog; +import android.content.Context; import android.content.DialogInterface; +import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnDismissListener; import android.content.Intent; import android.graphics.PointF; @@ -38,6 +43,7 @@ import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.AdapterView; +import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ImageButton; @@ -52,7 +58,6 @@ public class MapRouteInfoControl implements IRouteInformationListener { private final RoutingHelper routingHelper; private OsmandMapTileView mapView; private Dialog dialog; - private AlertDialog favoritesDialog; private boolean selectFromMapTouch; private boolean selectFromMapForTarget; @@ -229,12 +234,29 @@ public class MapRouteInfoControl implements IRouteInformationListener { } protected void selectFavorite(final View parentView, final boolean target) { - Builder bld = new AlertDialog.Builder(mapActivity); - ListView listView = new ListView(mapActivity); - final FavouritesAdapter favouritesAdapter = new FavouritesAdapter(mapActivity, mapActivity.getMyApplication().getFavorites().getFavouritePoints()); + final FavouritesAdapter favouritesAdapter = new FavouritesAdapter(mapActivity, mapActivity.getMyApplication() + .getFavorites().getFavouritePoints()); + Dialog[] dlgHolder = new Dialog[1]; + OnItemClickListener click = getOnClickListener(target, favouritesAdapter, dlgHolder); + OnDismissListener dismissListener = new DialogInterface.OnDismissListener() { + + @Override + public void onDismiss(DialogInterface dialog) { + if (target) { + setupToSpinner(parentView); + } else { + setupFromSpinner(parentView); + } + } + }; favouritesAdapter.updateLocation(mapActivity.getMapLocation()); - listView.setAdapter(favouritesAdapter); - listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + FavoriteDialogs.showFavoritesDialog(mapActivity, favouritesAdapter, click, dismissListener, dlgHolder, true); + } + + + private OnItemClickListener getOnClickListener(final boolean target, final FavouritesAdapter favouritesAdapter, + final Dialog[] dlg) { + return new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { @@ -245,25 +267,14 @@ public class MapRouteInfoControl implements IRouteInformationListener { } else { getTargets().setStartPoint(point, true, fp.getPointDescription()); } - favoritesDialog.dismiss(); + if(dlg != null && dlg.length > 0 && dlg[0] != null) { + dlg[0].dismiss(); + } //Next 2 lines ensure Dialog is shown in the right correct position after a selection been made hideDialog(); showDialog(); } - }); - bld.setView(listView); - favoritesDialog = bld.show(); - favoritesDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { - - @Override - public void onDismiss(DialogInterface dialog) { - if(target){ - setupToSpinner(parentView); - } else { - setupFromSpinner(parentView); - } - } - }); + }; } public static int getDirectionInfo() {