Refactor context menu for objects - make it quickaction independent and special case for accessibility

This commit is contained in:
Victor Shcherb 2014-07-06 03:01:37 +02:00
parent 43784a1260
commit 7fa56e1268
8 changed files with 199 additions and 161 deletions

View file

@ -22,6 +22,7 @@ public class ContextMenuAdapter {
}
private final Context ctx;
private View anchor;
final TIntArrayList items = new TIntArrayList();
final ArrayList<String> itemNames = new ArrayList<String>();
final ArrayList<OnContextMenuClick> listeners = new ArrayList<ContextMenuAdapter.OnContextMenuClick>();
@ -33,6 +34,14 @@ public class ContextMenuAdapter {
this.ctx = ctx;
}
public void setAnchor(View anchor) {
this.anchor = anchor;
}
public View getAnchor() {
return anchor;
}
public int length(){
return items.size();
}

View file

@ -6,9 +6,9 @@ package net.osmand.plus.activities;
import java.util.Comparator;
import java.util.List;
import net.londatiga.android.QuickAction;
import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
import net.osmand.plus.ContextMenuAdapter;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
@ -103,7 +103,6 @@ public class FavouritesListFragment extends SherlockListFragment implements Sear
public void onListItemClick(ListView l, View v, int position, long id) {
if (!isSelectFavoriteMode()) {
QuickAction qa = new QuickAction(v);
FavouritePoint point = favouritesAdapter.getItem(position);
String name = getString(R.string.favorite) + ": " + point.getName();
LatLon location = new LatLon(point.getLatitude(), point.getLongitude());
@ -114,9 +113,11 @@ public class FavouritesListFragment extends SherlockListFragment implements Sear
settings.SHOW_FAVORITES.set(true);
}
};
ContextMenuAdapter qa = new ContextMenuAdapter(v.getContext());
qa.setAnchor(v);
MapActivityActions.createDirectionsActions(qa, location, point, name, settings.getLastKnownMapZoom(), getActivity(),
true, onshow, false);
qa.show();
true, false);
MapActivityActions.showObjectContextMenu(qa, getActivity(), onshow);
} else {
Intent intent = getActivity().getIntent();
intent.putExtra(SELECT_FAVORITE_POINT_INTENT_KEY, favouritesAdapter.getItem(position));

View file

@ -12,11 +12,12 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import net.londatiga.android.ActionItem;
import net.londatiga.android.QuickAction;
import net.osmand.access.AccessibleToast;
import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
import net.osmand.plus.ContextMenuAdapter;
import net.osmand.plus.ContextMenuAdapter.Item;
import net.osmand.plus.ContextMenuAdapter.OnContextMenuClick;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.FavouritesDbHelper.FavoriteGroup;
import net.osmand.plus.GPXUtilities;
@ -155,7 +156,8 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment {
}
updateSelectionMode(actionMode);
} else {
final QuickAction qa = new QuickAction(v);
ContextMenuAdapter qa = new ContextMenuAdapter(v.getContext());
qa.setAnchor(v);
final OsmandSettings settings = getMyApplication().getSettings();
final FavouritePoint point = (FavouritePoint) favouritesAdapter.getChild(groupPosition, childPosition);
String name = getString(R.string.favorite) + ": " + point.getName();
@ -167,32 +169,28 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment {
}
};
MapActivityActions.createDirectionsActions(qa, location, point, name, settings.getLastKnownMapZoom(),
getActivity(), true, onshow, false);
ActionItem edit = new ActionItem();
edit.setIcon(getResources().getDrawable(R.drawable.ic_action_edit_light));
edit.setTitle(getString(R.string.favourites_context_menu_edit));
edit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
editPoint(point);
qa.dismiss();
}
});
qa.addActionItem(edit);
ActionItem delete = new ActionItem();
delete.setTitle(getString(R.string.favourites_context_menu_delete));
delete.setIcon(getResources().getDrawable(R.drawable.ic_action_delete_light));
delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
deletePoint(point);
qa.dismiss();
}
});
qa.addActionItem(delete);
qa.show();
getActivity(), true, false);
Item edit = qa.item(R.string.favourites_context_menu_edit).icons(R.drawable.ic_action_edit_light ,
R.drawable.ic_action_edit_dark);
edit.listen(
new OnContextMenuClick() {
@Override
public void onContextMenuClick(int itemId, int pos, boolean isChecked, DialogInterface dialog) {
editPoint(point);
}
}).reg();
Item delete = qa.item(R.string.favourites_context_menu_delete).icons(R.drawable.ic_action_delete_light ,
R.drawable.ic_action_delete_dark);
delete.listen(
new OnContextMenuClick() {
@Override
public void onContextMenuClick(int itemId, int pos, boolean isChecked, DialogInterface dialog) {
deletePoint(point);
}
}).reg();
MapActivityActions.showObjectContextMenu(qa, getActivity(), onshow);
}
return true;
}

View file

@ -24,6 +24,7 @@ import net.osmand.data.RotatedTileBox;
import net.osmand.map.ITileSource;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.ContextMenuAdapter;
import net.osmand.plus.ContextMenuAdapter.Item;
import net.osmand.plus.ContextMenuAdapter.OnContextMenuClick;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.FavouritesDbHelper.FavoriteGroup;
@ -64,7 +65,6 @@ import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
@ -1022,88 +1022,116 @@ public class MapActivityActions implements DialogProvider {
menu.show();
}
public static void createDirectionsActions(final QuickAction qa , final LatLon location, final Object obj, final String name,
final int z, final Activity activity, final boolean saveHistory, final OnClickListener onShow) {
createDirectionsActions(qa, location, obj, name, z, activity, saveHistory, onShow, true);
public static void createDirectionsActions(final ContextMenuAdapter qa , final LatLon location, final Object obj, final String name,
final int z, final Activity activity, final boolean saveHistory) {
createDirectionsActions(qa, location, obj, name, z, activity, saveHistory, true);
}
public static void createDirectionsActions(final QuickAction qa , final LatLon location, final Object obj, final String name,
final int z, final Activity activity, final boolean saveHistory, final OnClickListener onShow, boolean favorite) {
public static void createDirectionsActions(final ContextMenuAdapter qa , final LatLon location, final Object obj, final String name,
final int z, final Activity activity, final boolean saveHistory, boolean favorite) {
final OsmandApplication app = ((OsmandApplication) activity.getApplication());
final TargetPointsHelper targetPointsHelper = app.getTargetPointsHelper();
ActionItem setAsDestination = new ActionItem();
setAsDestination.setIcon(activity.getResources().getDrawable(R.drawable.ic_action_gdirections_light));
setAsDestination.setTitle(activity.getString(R.string.get_directions));
setAsDestination.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (onShow != null) {
onShow.onClick(v);
}
qa.dismiss();
MapActivityActions.directionsToDialogAndLaunchMap(activity, location.getLatitude(), location.getLongitude(), name);
}
});
qa.addActionItem(setAsDestination);
ActionItem intermediate = new ActionItem();
if (targetPointsHelper.getPointToNavigate() != null) {
intermediate.setIcon(activity.getResources().getDrawable(R.drawable.ic_action_flage_light));
intermediate.setTitle(activity.getString(R.string.context_menu_item_intermediate_point));
} else {
intermediate.setIcon(activity.getResources().getDrawable(R.drawable.ic_action_flag_light));
intermediate.setTitle(activity.getString(R.string.context_menu_item_destination_point));
}
intermediate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (onShow != null) {
onShow.onClick(v);
}
addWaypointDialogAndLaunchMap(activity, location.getLatitude(), location.getLongitude(), name);
qa.dismiss();
}
});
qa.addActionItem(intermediate);
ActionItem showOnMap = new ActionItem();
showOnMap.setIcon(activity.getResources().getDrawable(R.drawable.ic_action_marker_light));
showOnMap.setTitle(activity.getString(R.string.show_poi_on_map));
showOnMap.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (onShow != null) {
onShow.onClick(v);
}
app.getSettings().setMapLocationToShow(location.getLatitude(), location.getLongitude(), z, saveHistory ? name : null, name,
obj); //$NON-NLS-1$
MapActivity.launchMapActivityMoveToTop(activity);
qa.dismiss();
}
});
qa.addActionItem(showOnMap);
if (favorite) {
ActionItem addToFavorite = new ActionItem();
addToFavorite.setIcon(activity.getResources().getDrawable(R.drawable.ic_action_fav_light));
addToFavorite.setTitle(activity.getString(R.string.add_to_favourite));
addToFavorite.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (onShow != null) {
onShow.onClick(v);
Item dir = qa.item(R.string.get_directions).icons(R.drawable.ic_action_gdirections_light ,
R.drawable.ic_action_gdirections_dark);
dir.listen(
new OnContextMenuClick() {
@Override
public void onContextMenuClick(int itemId, int pos, boolean isChecked, DialogInterface dialog) {
MapActivityActions.directionsToDialogAndLaunchMap(activity, location.getLatitude(), location.getLongitude(), name);
}
qa.dismiss();
}).reg();
Item intermediate;
if (targetPointsHelper.getPointToNavigate() != null) {
intermediate = qa.item(R.string.context_menu_item_intermediate_point).icons(R.drawable.ic_action_flage_light,
R.drawable.ic_action_flage_dark);
} else {
intermediate = qa.item(R.string.context_menu_item_destination_point).icons(R.drawable.ic_action_flag_light,
R.drawable.ic_action_flag_dark);
}
intermediate.listen(new OnContextMenuClick() {
@Override
public void onContextMenuClick(int itemId, int pos, boolean isChecked, DialogInterface dialog) {
addWaypointDialogAndLaunchMap(activity, location.getLatitude(), location.getLongitude(), name);
}
}).reg();
Item showOnMap = qa.item(R.string.show_poi_on_map).icons(R.drawable.ic_action_marker_light ,
R.drawable.ic_action_marker_dark);
showOnMap.listen(
new OnContextMenuClick() {
@Override
public void onContextMenuClick(int itemId, int pos, boolean isChecked, DialogInterface dialog) {
app.getSettings().setMapLocationToShow(location.getLatitude(), location.getLongitude(), z, saveHistory ? name : null, name,
obj); //$NON-NLS-1$
MapActivity.launchMapActivityMoveToTop(activity);
}
}).reg();
if (favorite) {
Item addToFavorite = qa.item(R.string.add_to_favourite).icons(R.drawable.ic_action_fav_light,
R.drawable.ic_action_fav_dark);
addToFavorite.listen(new OnContextMenuClick() {
@Override
public void onContextMenuClick(int itemId, int pos, boolean isChecked, DialogInterface dialog) {
Bundle args = new Bundle();
Dialog dlg = createAddFavouriteDialog(activity, args);
dlg.show();
prepareAddFavouriteDialog(activity, dlg, args, location.getLatitude(), location.getLongitude(), name);
prepareAddFavouriteDialog(activity, dlg, args, location.getLatitude(), location.getLongitude(),
name);
}
}).reg();
}
}
public static void showObjectContextMenu(final ContextMenuAdapter qa, final Activity activity,
final OnClickListener onShow) {
OsmandApplication app = (OsmandApplication) activity.getApplication();
if(app.accessibilityEnabled()) {
Builder builder = new AlertDialog.Builder(activity);
String[] values = qa.getItemNames();
builder.setItems(values, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
OnContextMenuClick clk = qa.getClickAdapter(which);
if (clk != null) {
clk.onContextMenuClick(qa.getItemId(which), which, false, dialog);
}
}
});
qa.addActionItem(addToFavorite);
builder.show();
} else {
final QuickAction view = new QuickAction(qa.getAnchor());
for (int i = 0; i < qa.length(); i++) {
ActionItem ai = new ActionItem();
int id = qa.getImageId(0, true);
if (id != 0) {
ai.setIcon(activity.getResources().getDrawable(id));
}
final int ki = i;
ai.setTitle(qa.getItemName(i));
ai.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (onShow != null) {
onShow.onClick(v);
}
view.dismiss();
qa.getClickAdapter(ki).onContextMenuClick(qa.getItemId(ki), ki, false, null);
}
});
view.addActionItem(ai);
}
view.show();
}
}

View file

@ -6,7 +6,6 @@ import java.text.Collator;
import java.util.ArrayList;
import java.util.List;
import net.londatiga.android.QuickAction;
import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
import net.osmand.plus.ContextMenuAdapter;
@ -595,13 +594,14 @@ public class SelectedGPXFragment extends OsmandExpandableListFragment {
GpxDisplayItem child = adapter.getChild(groupPosition, childPosition);
if(child.group.getType() == GpxDisplayItemType.TRACK_POINTS ||
child.group.getType() == GpxDisplayItemType.TRACK_ROUTE_POINTS) {
QuickAction qa = new QuickAction(v);
ContextMenuAdapter qa = new ContextMenuAdapter(v.getContext());
qa.setAnchor(v);
String name = getString(R.string.favorite) + ": " + child.name;
LatLon location = new LatLon(child.locationStart.lat, child.locationStart.lon);
OsmandSettings settings = getMyApplication().getSettings();
MapActivityActions.createDirectionsActions(qa, location, child.locationStart, name, settings.getLastKnownMapZoom(), getActivity(),
true, null, false);
qa.show();
true, false);
MapActivityActions.showObjectContextMenu(qa, getActivity(), null);
} else {
child.expanded = !child.expanded;
adapter.notifyDataSetInvalidated();

View file

@ -8,10 +8,10 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import net.londatiga.android.QuickAction;
import net.osmand.PlatformUtil;
import net.osmand.access.AccessibleToast;
import net.osmand.data.LatLon;
import net.osmand.plus.ContextMenuAdapter;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
@ -228,11 +228,12 @@ public class SearchAddressOnlineFragment extends SherlockFragment implements Sea
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Place item = adapter.getItem(position);
QuickAction qa = new QuickAction(view);
ContextMenuAdapter qa = new ContextMenuAdapter(view.getContext());
qa.setAnchor(view);
MapActivityActions.createDirectionsActions(qa, new LatLon(item.lat, item.lon), item,
getString(R.string.address)+ " : " + item.displayName, Math.max(15, settings.getLastKnownMapZoom()),
getActivity(), true, null);
qa.show();
getActivity(), true);
MapActivityActions.showObjectContextMenu(qa, getActivity(), null);
}
private static class Place {

View file

@ -2,8 +2,8 @@ package net.osmand.plus.activities.search;
import java.util.List;
import net.londatiga.android.QuickAction;
import net.osmand.data.LatLon;
import net.osmand.plus.ContextMenuAdapter;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
@ -109,7 +109,8 @@ public class SearchHistoryFragment extends SherlockListFragment implements Sear
}
private void selectModel(final HistoryEntry model, View v) {
QuickAction qa = new QuickAction(v);
ContextMenuAdapter qa = new ContextMenuAdapter(v.getContext());
qa.setAnchor(v);
String name = model.getName();
OsmandSettings settings = ((OsmandApplication) getActivity().getApplication()).getSettings();
OnClickListener onShow = new View.OnClickListener() {
@ -119,8 +120,8 @@ public class SearchHistoryFragment extends SherlockListFragment implements Sear
}
};
MapActivityActions.createDirectionsActions(qa, new LatLon(model.getLat(), model.getLon()),
model, name, settings.getLastKnownMapZoom(), getActivity(), false, onShow);
qa.show();
model, name, settings.getLastKnownMapZoom(), getActivity(), false);
MapActivityActions.showObjectContextMenu(qa, getActivity(), onShow);
}
class HistoryAdapter extends ArrayAdapter<HistoryEntry> {

View file

@ -15,8 +15,6 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.londatiga.android.ActionItem;
import net.londatiga.android.QuickAction;
import net.osmand.ResultMatcher;
import net.osmand.access.AccessibleToast;
import net.osmand.access.NavigationInfo;
@ -24,11 +22,14 @@ import net.osmand.data.Amenity;
import net.osmand.data.AmenityType;
import net.osmand.data.LatLon;
import net.osmand.osm.MapRenderingTypes;
import net.osmand.plus.ContextMenuAdapter;
import net.osmand.plus.ContextMenuAdapter.Item;
import net.osmand.plus.ContextMenuAdapter.OnContextMenuClick;
import net.osmand.plus.NameFinderPoiFilter;
import net.osmand.plus.OsmAndConstants;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmAndLocationProvider.OsmAndCompassListener;
import net.osmand.plus.OsmAndLocationProvider.OsmAndLocationListener;
import net.osmand.plus.OsmAndConstants;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.PoiFilter;
@ -70,9 +71,8 @@ import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
@ -557,57 +557,57 @@ public class SearchPOIActivity extends OsmandListActivity implements OsmAndCompa
@Override
public void onListItemClick(ListView parent, View v, int position, long id) {
public void onListItemClick(ListView parent, final View v, int position, long id) {
final Amenity amenity = ((AmenityAdapter) getListAdapter()).getItem(position);
final QuickAction qa = new QuickAction(v);
ContextMenuAdapter adapter = new ContextMenuAdapter(v.getContext());
adapter.setAnchor(v);
String poiSimpleFormat = OsmAndFormatter.getPoiSimpleFormat(amenity, getMyApplication(), settings.usingEnglishNames());
String name = poiSimpleFormat;
int z = Math.max(16, settings.getLastKnownMapZoom());
MapActivityActions.createDirectionsActions(qa, amenity.getLocation(), amenity, name, z, this, true , null);
ActionItem poiDescription = new ActionItem();
poiDescription.setIcon(getResources().getDrawable(R.drawable.ic_action_note_light));
poiDescription.setTitle(getString(R.string.poi_context_menu_showdescription));
MapActivityActions.createDirectionsActions(adapter, amenity.getLocation(), amenity, name, z, this, true );
final String d = OsmAndFormatter.getAmenityDescriptionContent(getMyApplication(), amenity, false);
poiDescription.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Build text(amenity)
// Find and format links
SpannableString spannable = new SpannableString(d);
Linkify.addLinks(spannable, Linkify.ALL);
// Create dialog
Builder bs = new AlertDialog.Builder(v.getContext());
bs.setTitle(OsmAndFormatter.getPoiSimpleFormat(amenity, getMyApplication(),
settings.USE_ENGLISH_NAMES.get()));
bs.setMessage(spannable);
AlertDialog dialog = bs.show();
// Make links clickable
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setLinksClickable(true);
}
});
if(d.toString().trim().length() > 0) {
qa.addActionItem(poiDescription);
}
if (((OsmandApplication)getApplication()).accessibilityEnabled()) {
ActionItem showDetails = new ActionItem();
showDetails.setTitle(getString(R.string.show_details));
showDetails.setOnClickListener(new OnClickListener() {
Item poiDescr = adapter.item(R.string.poi_context_menu_showdescription).icons(R.drawable.ic_action_note_light,
R.drawable.ic_action_note_dark);
poiDescr.listen(new OnContextMenuClick() {
@Override
public void onClick(View v) {
public void onContextMenuClick(int itemId, int pos, boolean isChecked, DialogInterface dlg) {
// Build text(amenity)
// Find and format links
SpannableString spannable = new SpannableString(d);
Linkify.addLinks(spannable, Linkify.ALL);
// Create dialog
Builder bs = new AlertDialog.Builder(v.getContext());
bs.setTitle(OsmAndFormatter.getPoiSimpleFormat(amenity, getMyApplication(),
settings.USE_ENGLISH_NAMES.get()));
bs.setMessage(spannable);
AlertDialog dialog = bs.show();
// Make links clickable
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setLinksClickable(true);
}
}).reg();
}
if (((OsmandApplication)getApplication()).accessibilityEnabled()) {
Item showDetails = adapter.item(R.string.show_details);
showDetails.listen(new OnContextMenuClick() {
@Override
public void onContextMenuClick(int itemId, int pos, boolean isChecked, DialogInterface dlg) {
showPOIDetails(amenity, settings.usingEnglishNames());
}
});
qa.addActionItem(showDetails);
}).reg();
}
qa.show();
MapActivityActions.showObjectContextMenu(adapter, this, null);
}