From 60073c4ff32c90af730e95710ad8613d3db9ba65 Mon Sep 17 00:00:00 2001 From: GaidamakUA Date: Tue, 29 Mar 2016 16:59:03 +0300 Subject: [PATCH] Rewritten working ContextMenu. GpxUiHelper, ConfigureMapMenu. --- .../net/osmand/plus/ContextMenuAdapter.java | 42 ++++-------------- .../osmand/plus/dialogs/ConfigureMapMenu.java | 18 ++++---- .../net/osmand/plus/helpers/GpxUiHelper.java | 43 +++++++++++-------- .../views/mapwidgets/MapWidgetRegistry.java | 2 +- 4 files changed, 42 insertions(+), 63 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java b/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java index c3617c438c..092e85918e 100644 --- a/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java +++ b/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java @@ -40,26 +40,15 @@ public class ContextMenuAdapter { R.layout.list_menu_item : R.layout.list_menu_item_native; List items = new ArrayList<>(); private ConfigureMapMenu.OnClickListener changeAppModeListener = null; - //neded to detect whether user opened all modes or not public int length() { return items.size(); } - @Deprecated - public String getItemName(int position) { - return items.get(position).getTitle(); - } - - @Deprecated - public Boolean getSelection(int position) { - return items.get(position).getSelected(); - } - @Deprecated public Drawable getImage(OsmandApplication ctx, int position, boolean light) { @DrawableRes - int lst = items.get(position).getIcon(); + int lst = items.get(position).getLightIcon(); if (lst != -1) { return ContextCompat.getDrawable(ctx, lst); } @@ -71,21 +60,6 @@ public class ContextMenuAdapter { return null; } - @Deprecated - public void setItemName(int position, String str) { - items.get(position).setTitle(str); - } - - @Deprecated - public void setItemDescription(int position, String str) { - items.get(position).setDescription(str); - } - - @Deprecated - public void setSelection(int position, boolean s) { - items.get(position).setSelected(s); - } - // Adapter related public String[] getItemNames() { String[] itemNames = new String[items.size()]; @@ -99,12 +73,12 @@ public class ContextMenuAdapter { items.add(item); } - public ContextMenuItem getItem(int pos) { - return items.get(pos); + public ContextMenuItem getItem(int position) { + return items.get(position); } - public void removeItem(int pos) { - items.remove(pos); + public void removeItem(int position) { + items.remove(position); } public void setDefaultLayoutId(int defaultLayoutId) { @@ -298,7 +272,7 @@ public class ContextMenuAdapter { public interface ItemClickListener { //boolean return type needed to desribe if drawer needed to be close or not - boolean onContextMenuClick(ArrayAdapter adapter, int itemId, int pos, boolean isChecked); + boolean onContextMenuClick(ArrayAdapter adapter, int itemId, int position, boolean isChecked); } public interface OnIntegerValueChangedListener { @@ -308,13 +282,13 @@ public class ContextMenuAdapter { public static abstract class OnRowItemClick implements ItemClickListener { //boolean return type needed to describe if drawer needed to be close or not - public boolean onRowItemClick(ArrayAdapter adapter, View view, int itemId, int pos) { + public boolean onRowItemClick(ArrayAdapter adapter, View view, int itemId, int position) { CompoundButton btn = (CompoundButton) view.findViewById(R.id.toggle_item); if (btn != null && btn.getVisibility() == View.VISIBLE) { btn.setChecked(!btn.isChecked()); return false; } else { - return onContextMenuClick(adapter, itemId, pos, false); + return onContextMenuClick(adapter, itemId, position, false); } } } diff --git a/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java b/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java index 1686956442..006d6434e3 100644 --- a/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java +++ b/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java @@ -104,7 +104,7 @@ public class ConfigureMapMenu { if (itemId == R.string.layer_poi) { selectPOILayer(ma.getMyApplication().getSettings()); return false; - } else if (itemId == R.string.layer_gpx_layer && cm.getSelection(pos)) { + } else if (itemId == R.string.layer_gpx_layer && cm.getItem(pos).getSelected()) { ma.getMapLayers().showGPXFileLayer(getAlreadySelectedGpx(), ma.getMapView()); return false; } else { @@ -134,7 +134,7 @@ public class ConfigureMapMenu { public void onDismiss(DialogInterface dialog) { boolean areAnyGpxTracksVisible = ma.getMyApplication().getSelectedGpxHelper().isShowingAnyGpxFiles(); - cm.setSelection(pos, areAnyGpxTracksVisible); + cm.getItem(pos).setSelected(areAnyGpxTracksVisible); adapter.notifyDataSetChanged(); } }); @@ -266,7 +266,7 @@ public class ConfigureMapMenu { } else { AccessibleToast.makeText(app, R.string.renderer_load_exception, Toast.LENGTH_SHORT).show(); } - adapter.setItemDescription(pos, getRenderDescr(activity)); + adapter.getItem(pos).setDescription(getRenderDescr(activity)); activity.getDashboard().refreshContent(true); dialog.dismiss(); } @@ -299,7 +299,7 @@ public class ConfigureMapMenu { refreshMapComplete(activity); dialog.dismiss(); activity.getDashboard().refreshContent(true); - //adapter.setItemDescription(pos, getDayNightDescr(activity)); + //adapter.getItem(pos).setDescription(s, getDayNightDescr(activity)); //ad.notifyDataSetInvalidated(); } }); @@ -350,7 +350,7 @@ public class ConfigureMapMenu { if (mapContext != null) { mapContext.updateMapSettings(); } - adapter.setItemDescription(pos, String.format("%.0f", 100f * activity.getMyApplication().getSettings().MAP_DENSITY.get()) + " %"); + adapter.getItem(pos).setDescription(String.format("%.0f", 100f * activity.getMyApplication().getSettings().MAP_DENSITY.get()) + " %"); ad.notifyDataSetInvalidated(); dialog.dismiss(); } @@ -385,7 +385,7 @@ public class ConfigureMapMenu { public void onClick(DialogInterface dialog, int which) { view.getSettings().TEXT_SCALE.set(txtValues[which]); refreshMapComplete(activity); - adapter.setItemDescription(pos, getScale(activity)); + adapter.getItem(pos).setDescription(getScale(activity)); ad.notifyDataSetInvalidated(); dialog.dismiss(); } @@ -418,7 +418,7 @@ public class ConfigureMapMenu { public void onClick(DialogInterface dialog, int which) { view.getSettings().MAP_PREFERRED_LOCALE.set(txtIds[which]); refreshMapComplete(activity); - adapter.setItemDescription(pos, txtIds[which]); + adapter.getItem(pos).setDescription(txtIds[which]); ad.notifyDataSetInvalidated(); dialog.dismiss(); } @@ -579,7 +579,7 @@ public class ConfigureMapMenu { for (int i = 0; i < prefs.size(); i++) { prefs.get(i).set(tempPrefs[i]); } - adapter.setItemDescription(pos, getDescription(prefs)); + adapter.getItem(pos).setDescription(getDescription(prefs)); a.notifyDataSetInvalidated(); refreshMapComplete(activity); activity.getMapLayers().updateLayers(activity.getMapView()); @@ -680,7 +680,7 @@ public class ConfigureMapMenu { pref.set(p.getPossibleValues()[which - 1]); } refreshMapComplete(activity); - adapter.setItemDescription(pos, SettingsActivity.getStringPropertyValue(activity, pref.get())); + adapter.getItem(pos).setDescription(SettingsActivity.getStringPropertyValue(activity, pref.get())); dialog.dismiss(); ad.notifyDataSetInvalidated(); } diff --git a/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java b/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java index de2afa2e57..5ec620ab11 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java @@ -5,6 +5,7 @@ import android.app.Application; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; +import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; import android.util.TypedValue; import android.view.View; @@ -68,11 +69,11 @@ public class GpxUiHelper { public static String getDescription(OsmandApplication app, GPXTrackAnalysis analysis, boolean html) { StringBuilder description = new StringBuilder(); String nl = html ? "
" : "\n"; - String timeSpanClr = Algorithms.colorToString(app.getResources().getColor(R.color.gpx_time_span_color)); - String distanceClr = Algorithms.colorToString(app.getResources().getColor(R.color.gpx_distance_color)); - String speedClr = Algorithms.colorToString(app.getResources().getColor(R.color.gpx_speed)); - String ascClr = Algorithms.colorToString(app.getResources().getColor(R.color.gpx_altitude_asc)); - String descClr = Algorithms.colorToString(app.getResources().getColor(R.color.gpx_altitude_desc)); + String timeSpanClr = Algorithms.colorToString(ContextCompat.getColor(app, R.color.gpx_time_span_color)); + String distanceClr = Algorithms.colorToString(ContextCompat.getColor(app, R.color.gpx_distance_color)); + String speedClr = Algorithms.colorToString(ContextCompat.getColor(app, R.color.gpx_speed)); + String ascClr = Algorithms.colorToString(ContextCompat.getColor(app, R.color.gpx_altitude_asc)); + String descClr = Algorithms.colorToString(ContextCompat.getColor(app, R.color.gpx_altitude_desc)); // OUTPUT: // 1. Total distance, Start time, End time description.append(app.getString(R.string.gpx_info_distance, getColorValue(distanceClr, @@ -221,15 +222,16 @@ public class GpxUiHelper { } protected static void updateSelection(List selectedGpxList, boolean showCurrentTrack, - final ContextMenuAdapter adapter, int i, String fileName) { - if (i == 0 && showCurrentTrack) { + final ContextMenuAdapter adapter, int position, String fileName) { + ContextMenuItem item = adapter.getItem(position); + if (position == 0 && showCurrentTrack) { if (selectedGpxList.contains("")) { - adapter.setSelection(i, true); + item.setSelected(true); } } else { for (String file : selectedGpxList) { if (file.endsWith(fileName)) { - adapter.setSelection(i, true); + item.setSelected(true); break; } } @@ -244,7 +246,8 @@ public class GpxUiHelper { @Override public boolean processResult(GPXFile[] result) { - cmAdapter.setItemName(position, cmAdapter.getItemName(position) + "\n" + getDescription((OsmandApplication) app, result[0], f, false)); + ContextMenuItem item = cmAdapter.getItem(position); + item.setTitle(item.getTitle() + "\n" + getDescription((OsmandApplication) app, result[0], f, false)); adapter.notifyDataSetInvalidated(); return true; } @@ -270,6 +273,7 @@ public class GpxUiHelper { if (v == null) { v = activity.getLayoutInflater().inflate(layout, null); } + final ContextMenuItem item = adapter.getItem(position); ImageView icon = (ImageView) v.findViewById(R.id.icon); icon.setImageDrawable(adapter.getImage(app, position, light)); final ArrayAdapter arrayAdapter = this; @@ -279,11 +283,11 @@ public class GpxUiHelper { if (showCurrentGpx && position == 0) { return; } - int nline = adapter.getItemName(position).indexOf('\n'); + int nline = item.getTitle().indexOf('\n'); if (nline == -1) { setDescripionInDialog(arrayAdapter, adapter, activity, dir, list.get(position), position); } else { - adapter.setItemName(position, adapter.getItemName(position).substring(0, nline)); + item.setTitle(item.getTitle().substring(0, nline)); arrayAdapter.notifyDataSetInvalidated(); } } @@ -295,7 +299,7 @@ public class GpxUiHelper { icon.setVisibility(View.VISIBLE); } TextView tv = (TextView) v.findViewById(R.id.title); - tv.setText(adapter.getItemName(position)); + tv.setText(item.getTitle()); tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); // Put the image on the TextView @@ -304,15 +308,15 @@ public class GpxUiHelper { // } // tv.setCompoundDrawablePadding(padding); final CheckBox ch = ((CheckBox) v.findViewById(R.id.toggle_item)); - if (adapter.getSelection(position) == null) { + if (item.getSelected() == null) { ch.setVisibility(View.INVISIBLE); } else { ch.setOnCheckedChangeListener(null); - ch.setChecked(adapter.getSelection(position)); + ch.setChecked(item.getSelected()); ch.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - adapter.setSelection(position, isChecked); + item.setSelected(isChecked); } }); } @@ -338,12 +342,12 @@ public class GpxUiHelper { if (app != null && app.getSelectedGpxHelper() != null) { app.getSelectedGpxHelper().clearAllGpxFileToShow(); } - if (showCurrentGpx && adapter.getSelection(0)) { + if (showCurrentGpx && adapter.getItem(0).getSelected()) { currentGPX = app.getSavingTrackHelper().getCurrentGpx(); } List s = new ArrayList<>(); for (int i = (showCurrentGpx ? 1 : 0); i < adapter.length(); i++) { - if (adapter.getSelection(i)) { + if (adapter.getItem(i).getSelected()) { s.add(list.get(i)); } } @@ -361,7 +365,8 @@ public class GpxUiHelper { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { if (multipleChoice) { - adapter.setSelection(position, !adapter.getSelection(position)); + ContextMenuItem item = adapter.getItem(position); + item.setSelected(!item.getSelected()); listAdapter.notifyDataSetInvalidated(); } else { dlg.dismiss(); diff --git a/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapWidgetRegistry.java b/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapWidgetRegistry.java index 54814c7b74..79b38767ff 100644 --- a/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapWidgetRegistry.java +++ b/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapWidgetRegistry.java @@ -302,7 +302,7 @@ public class MapWidgetRegistry { } map.refreshMap(); dialog.dismiss(); - cm.setItemDescription(pos, settings.MAP_MARKERS_MODE.get().toHumanString(map)); + cm.getItem(pos).setDescription(settings.MAP_MARKERS_MODE.get().toHumanString(map)); ad.notifyDataSetChanged(); } });