From 4a50d2c3eb1bc56826e97eb2b8b3135d8a4f04b6 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 24 Oct 2014 18:26:39 +0300 Subject: [PATCH] Added ContextMenuAdapter for waypoints --- OsmAnd/res/layout/drawer_list_poi_header.xml | 36 ++ OsmAnd/res/layout/drawer_list_waypoint.xml | 24 + .../drawer_list_waypoint_sub_header.xml | 8 + .../net/osmand/plus/ContextMenuAdapter.java | 89 +++- .../plus/activities/MapActivityActions.java | 4 +- .../plus/helpers/WaypointDialogHelper.java | 445 ++++++++++-------- .../osmand/plus/helpers/WaypointHelper.java | 34 +- 7 files changed, 433 insertions(+), 207 deletions(-) create mode 100644 OsmAnd/res/layout/drawer_list_poi_header.xml create mode 100644 OsmAnd/res/layout/drawer_list_waypoint.xml create mode 100644 OsmAnd/res/layout/drawer_list_waypoint_sub_header.xml diff --git a/OsmAnd/res/layout/drawer_list_poi_header.xml b/OsmAnd/res/layout/drawer_list_poi_header.xml new file mode 100644 index 0000000000..0f462512f2 --- /dev/null +++ b/OsmAnd/res/layout/drawer_list_poi_header.xml @@ -0,0 +1,36 @@ + + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/drawer_list_waypoint.xml b/OsmAnd/res/layout/drawer_list_waypoint.xml new file mode 100644 index 0000000000..9ca14edfe4 --- /dev/null +++ b/OsmAnd/res/layout/drawer_list_waypoint.xml @@ -0,0 +1,24 @@ + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/drawer_list_waypoint_sub_header.xml b/OsmAnd/res/layout/drawer_list_waypoint_sub_header.xml new file mode 100644 index 0000000000..752ded6d26 --- /dev/null +++ b/OsmAnd/res/layout/drawer_list_waypoint_sub_header.xml @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java b/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java index 0998e3b48f..156dc27491 100644 --- a/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java +++ b/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java @@ -14,7 +14,19 @@ import android.view.ViewGroup; import android.widget.CompoundButton.OnCheckedChangeListener; public class ContextMenuAdapter { - + + public void clearAll() { + items.clear(); + isCategory.clear(); + itemNames.clear(); + listeners.clear(); + selectedList.clear(); + layoutIds.clear(); + iconList.clear(); + iconListLight.clear(); + itemDescription.clear(); + } + public interface OnContextMenuClick { //boolean return type needed to desribe if drawer needed to be close or not public boolean onContextMenuClick(int itemId, int pos, boolean isChecked); @@ -24,6 +36,7 @@ public class ContextMenuAdapter { private View anchor; private int defaultLayoutId = Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ? R.layout.list_menu_item : R.layout.list_menu_item_native; + private ArrayAdapter listAdapter; final TIntArrayList items = new TIntArrayList(); final TIntArrayList isCategory = new TIntArrayList(); final ArrayList itemNames = new ArrayList(); @@ -70,6 +83,12 @@ public class ContextMenuAdapter { itemNames.set(pos, str); } + public void notifyDataSetChanged(){ + if (listAdapter != null){ + listAdapter.notifyDataSetChanged(); + } + } + public void setItemDescription(int pos, String str) { itemDescription.set(pos, str); } @@ -127,6 +146,7 @@ public class ContextMenuAdapter { int pos = -1; String description = ""; private OnContextMenuClick listener; + boolean enabled = false; private Item() { } @@ -188,6 +208,10 @@ public class ContextMenuAdapter { return this; } + public Item enabled(boolean checked) { + this.enabled = checked; + return this; + } } public String[] getItemNames() { @@ -217,7 +241,7 @@ public class ContextMenuAdapter { public ListAdapter createListAdapter(final Activity activity, final boolean holoLight) { final int padding = (int) (12 * activity.getResources().getDisplayMetrics().density + 0.5f); final int layoutId = defaultLayoutId; - ListAdapter listadapter = new ArrayAdapter(activity, layoutId, R.id.title, + listAdapter = new ArrayAdapter(activity, layoutId, R.id.title, getItemNames()) { @Override public View getView(final int position, View convertView, ViewGroup parent) { @@ -246,26 +270,51 @@ public class ContextMenuAdapter { tv.setTypeface(null); } - final CheckBox ch = ((CheckBox) v.findViewById(R.id.check_item)); - if(selectedList.get(position) != -1) { - ch.setOnCheckedChangeListener(null); - ch.setVisibility(View.VISIBLE); - ch.setChecked(selectedList.get(position) > 0); - ch.setOnCheckedChangeListener(new OnCheckedChangeListener() { - - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - OnContextMenuClick ca = getClickAdapter(position); - if(ca != null) { - ca.onContextMenuClick(getElementId(position), position, isChecked); + if (v.findViewById(R.id.check_item) instanceof CheckBox){ + final CheckBox ch = ((CheckBox) v.findViewById(R.id.check_item)); + + if(selectedList.get(position) != -1) { + ch.setOnCheckedChangeListener(null); + ch.setVisibility(View.VISIBLE); + ch.setChecked(selectedList.get(position) > 0); + ch.setOnCheckedChangeListener(new OnCheckedChangeListener() { + + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + OnContextMenuClick ca = getClickAdapter(position); + if(ca != null) { + ca.onContextMenuClick(getElementId(position), position, isChecked); + } } - } - }); - ch.setVisibility(View.VISIBLE); - } else if (ch != null) { - ch.setVisibility(View.GONE); + }); + ch.setVisibility(View.VISIBLE); + } else if (ch != null) { + ch.setVisibility(View.GONE); + } + } else if (v.findViewById(R.id.check_item) instanceof Switch) { + final Switch ch = (Switch) v.findViewById(R.id.check_item); + + if(selectedList.get(position) != -1) { + ch.setOnCheckedChangeListener(null); + ch.setVisibility(View.VISIBLE); + ch.setChecked(selectedList.get(position) > 0); + ch.setOnCheckedChangeListener(new OnCheckedChangeListener() { + + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + OnContextMenuClick ca = getClickAdapter(position); + if(ca != null) { + ca.onContextMenuClick(getElementId(position), position, isChecked); + } + } + }); + ch.setVisibility(View.VISIBLE); + } else if (ch != null) { + ch.setVisibility(View.GONE); + } } + String itemDescr = getItemDescr(position); if (v.findViewById(R.id.descr) != null){ ((TextView)v.findViewById(R.id.descr)).setText(itemDescr); @@ -273,7 +322,7 @@ public class ContextMenuAdapter { return v; } }; - return listadapter; + return listAdapter; } diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java index f3577f5579..3d01927984 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java @@ -898,8 +898,8 @@ public class MapActivityActions implements DialogProvider { if (getMyApplication().getWaypointHelper().isRouteCalculated()) { final List deletedPoints = new ArrayList(); - ArrayAdapter adapter = waypointDialogHelper.getWaypointsAdapter(app.getMapActivity(), deletedPoints); - mDrawerList.setAdapter(adapter); + ContextMenuAdapter cm = waypointDialogHelper.setListAdapter(app.getMapActivity(), mDrawerList, deletedPoints); + return false; } else { openIntermediatePointsDialog(); diff --git a/OsmAnd/src/net/osmand/plus/helpers/WaypointDialogHelper.java b/OsmAnd/src/net/osmand/plus/helpers/WaypointDialogHelper.java index 91e9ad5b38..511ea1ec78 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/WaypointDialogHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/WaypointDialogHelper.java @@ -6,12 +6,8 @@ import java.util.List; import net.osmand.Location; import net.osmand.data.LatLon; import net.osmand.data.LocationPoint; -import net.osmand.plus.OsmAndFormatter; +import net.osmand.plus.*; import net.osmand.plus.OsmAndLocationProvider.OsmAndLocationListener; -import net.osmand.plus.OsmandApplication; -import net.osmand.plus.PoiFilter; -import net.osmand.plus.R; -import net.osmand.plus.activities.IntermediatePointsDialog; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.helpers.WaypointHelper.LocationPointWrapper; import net.osmand.plus.views.AnimateDraggingMapThread; @@ -36,7 +32,6 @@ import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; -import android.widget.CompoundButton; import android.widget.FrameLayout; import android.widget.ImageButton; import android.widget.ImageView; @@ -140,7 +135,6 @@ public class WaypointDialogHelper implements OsmAndLocationListener { }); TextView textDist = (TextView) localView.findViewById(R.id.waypoint_dist); ((ImageView) localView.findViewById(R.id.waypoint_icon)).setImageDrawable(ps.getDrawable(ctx)); -// Location lastKnownMapLocation = app.getLocationProvider().getLastKnownLocation(); int dist = wh.getRouteDistance(ps); String dd = OsmAndFormatter.getFormattedDistance(dist, app); if (ps.deviationDistance > 0) { @@ -148,9 +142,6 @@ public class WaypointDialogHelper implements OsmAndLocationListener { } textDist.setText(dd); text.setText(point.getName(app)); -// ((Spannable) text.getText()).setSpan( -// new ForegroundColorSpan(ctx.getResources().getColor(R.color.color_distance)), 0, distance.length() - 1, -// 0); } public void removeDialog() { @@ -330,156 +321,158 @@ public class WaypointDialogHelper implements OsmAndLocationListener { } } - public ArrayAdapter getWaypointsAdapter(final Activity ctx, final List deletedPoints){ - final List points = getPoints(); - final int[] running = new int[]{-1}; - return new ArrayAdapter(ctx, - R.layout.waypoint_reached, R.id.title, points) { - - @Override - public View getView(final int position, View convertView, ViewGroup parent) { - // User super class to create the View - View v = convertView; - final ArrayAdapter thisAdapter = this; - boolean labelView = (getItem(position) instanceof Integer); - boolean viewText = v != null && v.findViewById(R.id.info_close) == null; - if (v == null || viewText != labelView) { - v = ctx.getLayoutInflater().inflate(labelView ? R.layout.waypoint_header : R.layout.waypoint_reached, null); - } - if (getItem(position) instanceof String && getItem(position).equals(POI_RADIUS)){ - v = ctx.getLayoutInflater().inflate(R.layout.radius_search_list_element, null); - v.findViewById(R.id.ProgressBar).setVisibility(position == running[0] ? View.VISIBLE : View.GONE); - final TextView radius = (TextView) v.findViewById(R.id.radius); - radius.setText(OsmAndFormatter.getFormattedDistance(waypointHelper.getPoiSearchDeviationRadius(), app)); - radius.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - int length = WaypointHelper.SEARCH_RADIUS_VALUES.length; - String[] names = new String[length]; - int selected = 0; - for (int i = 0; i < length; i++) { - names[i] = OsmAndFormatter.getFormattedDistance(WaypointHelper.SEARCH_RADIUS_VALUES[i], app); - if (WaypointHelper.SEARCH_RADIUS_VALUES[i] == waypointHelper.getPoiSearchDeviationRadius()){ - selected = i; - } - } - new AlertDialog.Builder(ctx) - .setSingleChoiceItems(names, selected, new OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - int value = WaypointHelper.SEARCH_RADIUS_VALUES[i]; - if (waypointHelper.getPoiSearchDeviationRadius() != value){ - running[0] = position; - thisAdapter.notifyDataSetInvalidated(); - waypointHelper.setPoiSearchDeviationRadius(value); - radius.setText(OsmAndFormatter.getFormattedDistance(value, app)); - recalculatePoints(running, thisAdapter, WaypointHelper.POI); - dialogInterface.dismiss(); - } - } - }).setTitle(app.getString(R.string.search_radius_proximity)+ " " + app.getString(R.string.poi)) - .setNegativeButton(R.string.default_buttons_cancel, null) - .show(); - } - }); - } else if (getItem(position) instanceof String && getItem(position).equals(SEARCH_RADIUS)){ - v = ctx.getLayoutInflater().inflate(R.layout.radius_search_list_element, null); - v.findViewById(R.id.ProgressBar).setVisibility(position == running[0] ? View.VISIBLE : View.GONE); - final TextView radius = (TextView) v.findViewById(R.id.radius); - radius.setText(OsmAndFormatter.getFormattedDistance(waypointHelper.getSearchDeviationRadius(), app)); - radius.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - int length = WaypointHelper.SEARCH_RADIUS_VALUES.length; - String[] names = new String[length]; - int selected = 0; - for (int i = 0; i < length; i++) { - names[i] = OsmAndFormatter.getFormattedDistance(WaypointHelper.SEARCH_RADIUS_VALUES[i], app); - if (WaypointHelper.SEARCH_RADIUS_VALUES[i] == waypointHelper.getSearchDeviationRadius()){ - selected = i; - } - } - new AlertDialog.Builder(ctx) - .setSingleChoiceItems(names, selected, new OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - int value = WaypointHelper.SEARCH_RADIUS_VALUES[i]; - if (waypointHelper.getSearchDeviationRadius() != value){ - running[0] = position; - thisAdapter.notifyDataSetInvalidated(); - waypointHelper.setSearchDeviationRadius(value); - radius.setText(OsmAndFormatter.getFormattedDistance(value, app)); - recalculatePoints(running, thisAdapter, -1); - dialogInterface.dismiss(); - } - } - }).setTitle(app.getString(R.string.search_radius_proximity)) - .setNegativeButton(R.string.default_buttons_cancel, null) - .show(); - } - }); - } else if (labelView) { - v = ctx.getLayoutInflater().inflate(R.layout.waypoint_header, null); - final int type = (Integer) getItem(position); - ImageView sort = (ImageView) v.findViewById(R.id.sort); - //sort button in Destination header - if (type == 0 && sort != null){ - sort.setVisibility(View.VISIBLE); - if (app.getSettings().isLightContent()){ - sort.setImageResource(R.drawable.ic_sort_waypoint_white); - } else { - sort.setImageResource(R.drawable.ic_sort_waypoint_dark); - } - sort.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - IntermediatePointsDialog.openIntermediatePointsDialog(ctx, app, true); - } - }); - } - final CompoundButton btn = (CompoundButton) v.findViewById(R.id.check_item); - btn.setVisibility(waypointHelper.isTypeConfigurable(type) ? View.VISIBLE : View.GONE); - btn.setOnCheckedChangeListener(null); - final boolean checked = waypointHelper.isTypeEnabled(type); - btn.setChecked(checked); - btn.setEnabled(running[0] == -1); - v.findViewById(R.id.ProgressBar).setVisibility(position == running[0] ? View.VISIBLE : View.GONE); - btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { - - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - running[0] = position; - thisAdapter.notifyDataSetInvalidated(); - if (type == WaypointHelper.POI && isChecked) { - selectPoi(running, thisAdapter, type, isChecked, mapActivity); - } else { - enableType(running, thisAdapter, type, isChecked); - } - } - - }); - TextView tv = (TextView) v.findViewById(R.id.header_text); - tv.setText(getHeader(ctx, type, checked)); - } else { - v = ctx.getLayoutInflater().inflate(R.layout.waypoint_reached, null); - updatePointInfoView(app, ctx, v, (LocationPointWrapper) getItem(position), null); - View remove = v.findViewById(R.id.info_close); - ((ImageButton) remove).setImageDrawable(ctx.getResources().getDrawable( - app.getSettings().isLightContent() ? R.drawable.ic_action_gremove_light - : R.drawable.ic_action_gremove_dark)); - remove.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - LocationPointWrapper point = (LocationPointWrapper) points.get(position); - remove(point); - deletedPoints.add(point); - notifyDataSetChanged(); - } - }); - } - return v; - } - }; + public ContextMenuAdapter setListAdapter(final Activity ctx, ListView mDrawerList, final List deletedPoints){ + final ContextMenuAdapter adapter = new ContextMenuAdapter(ctx); + registerPoints(adapter, ctx, mDrawerList); + mDrawerList.setAdapter(adapter.createListAdapter(mapActivity, mapActivity.getMyApplication().getSettings().isLightContentMenu())); + return adapter; +// return new ArrayAdapter(ctx, +// R.layout.waypoint_reached, R.id.title, points) { +// +// @Override +// public View getView(final int position, View convertView, ViewGroup parent) { +// // User super class to create the View +// View v = convertView; +// final ArrayAdapter thisAdapter = this; +// boolean labelView = (getItem(position) instanceof Integer); +// boolean viewText = v != null && v.findViewById(R.id.info_close) == null; +// if (v == null || viewText != labelView) { +// v = ctx.getLayoutInflater().inflate(labelView ? R.layout.waypoint_header : R.layout.waypoint_reached, null); +// } +// if (getItem(position) instanceof String && getItem(position).equals(POI_RADIUS)){ +// v = ctx.getLayoutInflater().inflate(R.layout.radius_search_list_element, null); +// v.findViewById(R.id.ProgressBar).setVisibility(position == running[0] ? View.VISIBLE : View.GONE); +// final TextView radius = (TextView) v.findViewById(R.id.radius); +// radius.setText(OsmAndFormatter.getFormattedDistance(waypointHelper.getPoiSearchDeviationRadius(), app)); +// radius.setOnClickListener(new View.OnClickListener() { +// @Override +// public void onClick(View view) { +// int length = WaypointHelper.SEARCH_RADIUS_VALUES.length; +// String[] names = new String[length]; +// int selected = 0; +// for (int i = 0; i < length; i++) { +// names[i] = OsmAndFormatter.getFormattedDistance(WaypointHelper.SEARCH_RADIUS_VALUES[i], app); +// if (WaypointHelper.SEARCH_RADIUS_VALUES[i] == waypointHelper.getPoiSearchDeviationRadius()){ +// selected = i; +// } +// } +// new AlertDialog.Builder(ctx) +// .setSingleChoiceItems(names, selected, new OnClickListener() { +// @Override +// public void onClick(DialogInterface dialogInterface, int i) { +// int value = WaypointHelper.SEARCH_RADIUS_VALUES[i]; +// if (waypointHelper.getPoiSearchDeviationRadius() != value){ +// running[0] = position; +// thisAdapter.notifyDataSetInvalidated(); +// waypointHelper.setPoiSearchDeviationRadius(value); +// radius.setText(OsmAndFormatter.getFormattedDistance(value, app)); +// recalculatePoints(running, thisAdapter, WaypointHelper.POI); +// dialogInterface.dismiss(); +// } +// } +// }).setTitle(app.getString(R.string.search_radius_proximity)+ " " + app.getString(R.string.poi)) +// .setNegativeButton(R.string.default_buttons_cancel, null) +// .show(); +// } +// }); +// } else if (getItem(position) instanceof String && getItem(position).equals(SEARCH_RADIUS)){ +// v = ctx.getLayoutInflater().inflate(R.layout.radius_search_list_element, null); +// v.findViewById(R.id.ProgressBar).setVisibility(position == running[0] ? View.VISIBLE : View.GONE); +// final TextView radius = (TextView) v.findViewById(R.id.radius); +// radius.setText(OsmAndFormatter.getFormattedDistance(waypointHelper.getSearchDeviationRadius(), app)); +// radius.setOnClickListener(new View.OnClickListener() { +// @Override +// public void onClick(View view) { +// int length = WaypointHelper.SEARCH_RADIUS_VALUES.length; +// String[] names = new String[length]; +// int selected = 0; +// for (int i = 0; i < length; i++) { +// names[i] = OsmAndFormatter.getFormattedDistance(WaypointHelper.SEARCH_RADIUS_VALUES[i], app); +// if (WaypointHelper.SEARCH_RADIUS_VALUES[i] == waypointHelper.getSearchDeviationRadius()){ +// selected = i; +// } +// } +// new AlertDialog.Builder(ctx) +// .setSingleChoiceItems(names, selected, new OnClickListener() { +// @Override +// public void onClick(DialogInterface dialogInterface, int i) { +// int value = WaypointHelper.SEARCH_RADIUS_VALUES[i]; +// if (waypointHelper.getSearchDeviationRadius() != value){ +// running[0] = position; +// thisAdapter.notifyDataSetInvalidated(); +// waypointHelper.setSearchDeviationRadius(value); +// radius.setText(OsmAndFormatter.getFormattedDistance(value, app)); +// recalculatePoints(running, thisAdapter, -1); +// dialogInterface.dismiss(); +// } +// } +// }).setTitle(app.getString(R.string.search_radius_proximity)) +// .setNegativeButton(R.string.default_buttons_cancel, null) +// .show(); +// } +// }); +// } else if (labelView) { +// v = ctx.getLayoutInflater().inflate(R.layout.waypoint_header, null); +// final int type = (Integer) getItem(position); +// ImageView sort = (ImageView) v.findViewById(R.id.sort); +// //sort button in Destination header +// if (type == 0 && sort != null){ +// sort.setVisibility(View.VISIBLE); +// if (app.getSettings().isLightContent()){ +// sort.setImageResource(R.drawable.ic_sort_waypoint_white); +// } else { +// sort.setImageResource(R.drawable.ic_sort_waypoint_dark); +// } +// sort.setOnClickListener(new View.OnClickListener() { +// @Override +// public void onClick(View view) { +// IntermediatePointsDialog.openIntermediatePointsDialog(ctx, app, true); +// } +// }); +// } +// final CompoundButton btn = (CompoundButton) v.findViewById(R.id.check_item); +// btn.setVisibility(waypointHelper.isTypeConfigurable(type) ? View.VISIBLE : View.GONE); +// btn.setOnCheckedChangeListener(null); +// final boolean checked = waypointHelper.isTypeEnabled(type); +// btn.setChecked(checked); +// btn.setEnabled(running[0] == -1); +// v.findViewById(R.id.ProgressBar).setVisibility(position == running[0] ? View.VISIBLE : View.GONE); +// btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { +// +// @Override +// public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { +// running[0] = position; +// thisAdapter.notifyDataSetInvalidated(); +// if (type == WaypointHelper.POI && isChecked) { +// selectPoi(running, thisAdapter, type, isChecked, mapActivity); +// } else { +// enableType(running, thisAdapter, type, isChecked); +// } +// } +// +// }); +// TextView tv = (TextView) v.findViewById(R.id.header_text); +// tv.setText(getHeader(ctx, type, checked)); +// } else { +// v = ctx.getLayoutInflater().inflate(R.layout.waypoint_reached, null); +// updatePointInfoView(app, ctx, v, (LocationPointWrapper) getItem(position), null); +// View remove = v.findViewById(R.id.info_close); +// ((ImageButton) remove).setImageDrawable(ctx.getResources().getDrawable( +// app.getSettings().isLightContent() ? R.drawable.ic_action_gremove_light +// : R.drawable.ic_action_gremove_dark)); +// remove.setOnClickListener(new View.OnClickListener() { +// @Override +// public void onClick(View view) { +// LocationPointWrapper point = (LocationPointWrapper) points.get(position); +// remove(point); +// deletedPoints.add(point); +// notifyDataSetChanged(); +// } +// }); +// } +// return v; +// } +// }; } protected String getHeader(Activity ctx, int type, boolean checked) { @@ -505,8 +498,8 @@ public class WaypointDialogHelper implements OsmAndLocationListener { return str; } - private void selectPoi(final int[] running, final ArrayAdapter listAdapter, final int type, - final boolean enable, MapActivity map) { + private void selectPoi(final int[] running, final ContextMenuAdapter menuAdapter, final int type, + final boolean enable, MapActivity map, final ListView drawerlist) { if (!PoiFilter.CUSTOM_FILTER_ID.equals(app.getSettings().getPoiFilterForMap())) { final PoiFilter[] selected = new PoiFilter[1]; AlertDialog dlg = map.getMapLayers().selectPOIFilterLayer(map.getMapView(), selected); @@ -515,16 +508,16 @@ public class WaypointDialogHelper implements OsmAndLocationListener { @Override public void onDismiss(DialogInterface dialog) { if (selected != null) { - enableType(running, listAdapter, type, enable); + enableType(running, menuAdapter, type, enable, drawerlist); } } }); } else { - enableType(running, listAdapter, type, enable); + enableType(running, menuAdapter, type, enable, drawerlist); } } - private void recalculatePoints(final int[] running, final ArrayAdapter listAdapter, final int type){ + private void recalculatePoints(final int[] running, final ContextMenuAdapter menuAdapter, final int type, final ListView mDrawerList){ new AsyncTask() { @Override @@ -535,36 +528,118 @@ public class WaypointDialogHelper implements OsmAndLocationListener { protected void onPostExecute(Void result) { running[0] = -1; - listAdapter.clear(); - for (Object point : getPoints()) { - listAdapter.add(point); - } - listAdapter.notifyDataSetChanged(); + menuAdapter.clearAll(); + registerPoints(menuAdapter, mapActivity, mDrawerList); + } }.execute((Void) null); } - protected List getPoints() { - final List points = new ArrayList(); + protected void registerPoints(final ContextMenuAdapter adapter, final Activity ctx, final ListView drawerList) { + final int[] running = new int[]{-1}; for (int i = 0; i < WaypointHelper.MAX; i++) { List tp = waypointHelper.getWaypoints(i); if (waypointHelper.isTypeVisible(i)) { - points.add(new Integer(i)); + final boolean checked = waypointHelper.isTypeEnabled(i); + final int type = i; + adapter.item(getHeader(ctx, i, checked)).enabled(checked).layout(R.layout.drawer_list_poi_header).listen(new ContextMenuAdapter.OnContextMenuClick() { + @Override + public boolean onContextMenuClick(int itemId, int pos, boolean isChecked) { + running[0] = pos; + adapter.notifyDataSetChanged(); + if (type == WaypointHelper.POI && isChecked) { + selectPoi(running, adapter, type, isChecked, mapActivity, drawerList); + } else { + enableType(running, adapter, type, isChecked, drawerList); + } + return false; + } + }).reg(); + if (i == WaypointHelper.POI && waypointHelper.isTypeEnabled(WaypointHelper.POI)){ - points.add(POI_RADIUS); + adapter.item(ctx.getString(R.string.search_radius_proximity)).listen(new ContextMenuAdapter.OnContextMenuClick() { + @Override + public boolean onContextMenuClick(int itemId, final int pos, boolean isChecked) { + int length = WaypointHelper.SEARCH_RADIUS_VALUES.length; + String[] names = new String[length]; + int selected = 0; + for (int i = 0; i < length; i++) { + names[i] = OsmAndFormatter.getFormattedDistance(WaypointHelper.SEARCH_RADIUS_VALUES[i], app); + if (WaypointHelper.SEARCH_RADIUS_VALUES[i] == waypointHelper.getPoiSearchDeviationRadius()) { + selected = i; + } + } + new AlertDialog.Builder(ctx) + .setSingleChoiceItems(names, selected, new OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + int value = WaypointHelper.SEARCH_RADIUS_VALUES[i]; + if (waypointHelper.getPoiSearchDeviationRadius() != value) { + running[0] = pos; + adapter.notifyDataSetChanged(); + waypointHelper.setPoiSearchDeviationRadius(value); + //radius.setText(OsmAndFormatter.getFormattedDistance(value, app)); + recalculatePoints(running, adapter, WaypointHelper.POI, drawerList); + dialogInterface.dismiss(); + } + } + }).setTitle(app.getString(R.string.search_radius_proximity) + " " + app.getString(R.string.poi)) + .setNegativeButton(R.string.default_buttons_cancel, null) + .show(); + return false; + } + }).reg(); } else if (i == WaypointHelper.FAVORITES && waypointHelper.isTypeEnabled(WaypointHelper.FAVORITES)){ - points.add(SEARCH_RADIUS); + adapter.item(ctx.getString(R.string.search_radius_proximity)).listen(new ContextMenuAdapter.OnContextMenuClick() { + @Override + public boolean onContextMenuClick(int itemId, final int pos, boolean isChecked) { + int length = WaypointHelper.SEARCH_RADIUS_VALUES.length; + String[] names = new String[length]; + int selected = 0; + for (int i = 0; i < length; i++) { + names[i] = OsmAndFormatter.getFormattedDistance(WaypointHelper.SEARCH_RADIUS_VALUES[i], app); + if (WaypointHelper.SEARCH_RADIUS_VALUES[i] == waypointHelper.getSearchDeviationRadius()){ + selected = i; + } + } + new AlertDialog.Builder(ctx) + .setSingleChoiceItems(names, selected, new OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + int value = WaypointHelper.SEARCH_RADIUS_VALUES[i]; + if (waypointHelper.getSearchDeviationRadius() != value){ + running[0] = pos; + waypointHelper.setSearchDeviationRadius(value); + //radius.setText(OsmAndFormatter.getFormattedDistance(value, app)); + recalculatePoints(running, adapter, -1, drawerList); + dialogInterface.dismiss(); + } + } + }).setTitle(app.getString(R.string.search_radius_proximity)) + .setNegativeButton(R.string.default_buttons_cancel, null) + .show(); + return false; + } + }).reg(); } if (tp != null && tp.size() > 0) { - points.addAll(tp); + for (LocationPointWrapper p : tp){ + WaypointHelper wh =app.getWaypointHelper(); + int dist = wh.getRouteDistance(p); + String dd = OsmAndFormatter.getFormattedDistance(dist, app); + if (p.deviationDistance > 0) { + dd += "\n+" + OsmAndFormatter.getFormattedDistance(p.deviationDistance, app); + } + adapter.item(p.getPoint().getName(ctx)).icon(p.getDrawableId(ctx)).description(dd).layout(R.layout.drawer_list_waypoint).reg(); + } + //points.addAll(tp); } } } - return points; } - private void enableType(final int[] running, final ArrayAdapter listAdapter, final int type, - final boolean enable) { + private void enableType(final int[] running, final ContextMenuAdapter menuAdapter, final int type, + final boolean enable, final ListView drawerList) { new AsyncTask() { @Override @@ -575,11 +650,13 @@ public class WaypointDialogHelper implements OsmAndLocationListener { protected void onPostExecute(Void result) { running[0] = -1; - listAdapter.clear(); - for (Object point : getPoints()) { - listAdapter.add(point); - } - listAdapter.notifyDataSetChanged(); + menuAdapter.clearAll(); + registerPoints(menuAdapter, mapActivity, drawerList); +// menuAdapter.clear(); +// for (Object point : registerPoints()) { +// menuAdapter.add(point); +// } +// menuAdapter.notifyDataSetChanged(); } }.execute((Void) null); } diff --git a/OsmAnd/src/net/osmand/plus/helpers/WaypointHelper.java b/OsmAnd/src/net/osmand/plus/helpers/WaypointHelper.java index 2e432d82d5..580eed6c7b 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/WaypointHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/WaypointHelper.java @@ -650,7 +650,39 @@ public class WaypointHelper { public LocationPoint getPoint() { return point; } - + + + public int getDrawableId(Context uiCtx) { + if(type == POI) { + Amenity amenity = ((AmenityLocationPoint) point).a; + StringBuilder tag = new StringBuilder(); + StringBuilder value = new StringBuilder(); + MapRenderingTypes.getDefault().getAmenityTagValue(amenity.getType(), amenity.getSubType(), + tag, value); + if(RenderingIcons.containsBigIcon(tag + "_" + value)) { + return RenderingIcons.getBigIconResourceId(tag + "_" + value); + } else if(RenderingIcons.containsBigIcon(value.toString())) { + return RenderingIcons.getBigIconResourceId(value.toString()); + } + return 0; + } else if(type == TARGETS) { + return !((TargetPoint)point).intermediate? R.drawable.list_destination: + R.drawable.list_intermediate; + } else if(type == FAVORITES || type == WAYPOINTS) { + //return FavoriteImageDrawable.getOrCreate(uiCtx, point.getColor()); + return 0; + } else if(type == ALARMS) { + //TODO: Looks like this does not work yet, not sure why: + if(RenderingIcons.containsBigIcon("list_" + ((AlarmInfo) point).getType().toString().toLowerCase())) { + return RenderingIcons.getBigIconResourceId("list_" + ((AlarmInfo) point).getType().toString().toLowerCase()); + } else { + return 0; + } + } else { + return 0; + } + } + public Drawable getDrawable(Context uiCtx) { if(type == POI) { Amenity amenity = ((AmenityLocationPoint) point).a;