diff --git a/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java b/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java index c924902889..38a7ebf6bc 100644 --- a/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java +++ b/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java @@ -6,7 +6,6 @@ import android.graphics.Typeface; import android.graphics.drawable.Drawable; import android.os.Build; import android.support.annotation.DrawableRes; -import android.support.annotation.IdRes; import android.support.annotation.LayoutRes; import android.support.annotation.StringRes; import android.support.v4.content.ContextCompat; @@ -38,53 +37,12 @@ import gnu.trove.list.array.TIntArrayList; public class ContextMenuAdapter { private static final Log LOG = PlatformUtil.getLog(ContextMenuAdapter.class); - - public interface OnContextMenuClick { - //boolean return type needed to desribe if drawer needed to be close or not - boolean onContextMenuClick(ArrayAdapter adapter, int itemId, int pos, boolean isChecked); - } - - public interface OnIntegerValueChangedListener { - boolean onIntegerValueChangedListener(int newValue); - } - - public static abstract class OnRowItemClick implements OnContextMenuClick { - - public OnRowItemClick() { - } - - //boolean return type needed to desribe if drawer needed to be close or not - public boolean onRowItemClick(ArrayAdapter adapter, View view, int itemId, int pos) { - 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); - } - } - } - - public class BooleanResult { - private boolean result = false; - - public void setResult(boolean value) { - result = value; - } - - public boolean getResult() { - return result; - } - } - private final Context ctx; - private View anchor; @LayoutRes private int defaultLayoutId = Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ? R.layout.list_menu_item : R.layout.list_menu_item_native; - final TIntArrayList items = new TIntArrayList(); + final TIntArrayList titleResList = new TIntArrayList(); final TIntArrayList isCategory = new TIntArrayList(); - final ArrayList itemNames = new ArrayList(); final ArrayList checkListeners = new ArrayList<>(); final ArrayList integerListeners = new ArrayList<>(); final TIntArrayList selectedList = new TIntArrayList(); @@ -94,8 +52,8 @@ public class ContextMenuAdapter { final TIntArrayList iconList = new TIntArrayList(); final TIntArrayList lightIconList = new TIntArrayList(); final TIntArrayList secondaryLightIconList = new TIntArrayList(); - final ArrayList itemDescription = new ArrayList(); - private List visibleModes = new ArrayList(); + final ArrayList itemDescription = new ArrayList<>(); + private List visibleModes = new ArrayList<>(); private ConfigureMapMenu.OnClickListener changeAppModeListener = null; //neded to detect whether user opened all modes or not private BooleanResult allModes = new BooleanResult(); @@ -109,20 +67,37 @@ public class ContextMenuAdapter { this.ctx = ctx; } - public void setAnchor(View anchor) { - this.anchor = anchor; - } - - public View getAnchor() { - return anchor; - } - + // Related to whole adapter public int length() { - return items.size(); + return titleResList.size(); } - public int getElementId(int pos) { - return items.get(pos); + public void setDefaultLayoutId(int defaultLayoutId) { + this.defaultLayoutId = defaultLayoutId; + } + + public void setChangeAppModeListener(ConfigureMapMenu.OnClickListener changeAppModeListener) { + this.changeAppModeListener = changeAppModeListener; + } + + public ArrayAdapter createListAdapter(final Activity activity, final boolean holoLight) { + final int layoutId = defaultLayoutId; + final OsmandApplication app = ((OsmandApplication) activity.getApplication()); + String[] names = new String[titleResList.size()]; + for (int i = 0; i < titleResList.size(); i++) { + names[i] = activity.getString(titleResList.get(i)); + } + return new ContextMenuArrayAdapter(activity, layoutId, R.id.title, names, app, holoLight); + } + + public int[] getTitleResources() { + return titleResList.toArray(); + } + + // Item related + @StringRes + public int getTitleRes(int pos) { + return titleResList.get(pos); } public OnContextMenuClick getClickAdapter(int i) { @@ -133,18 +108,10 @@ public class ContextMenuAdapter { return integerListeners.get(i); } - public String getItemName(int pos) { - return itemNames.get(pos); - } - public String getItemDescr(int pos) { return itemDescription.get(pos); } - public void setItemName(int pos, String str) { - itemNames.set(pos, str); - } - public void setItemDescription(int pos, String str) { itemDescription.set(pos, str); } @@ -169,7 +136,6 @@ public class ContextMenuAdapter { progressList.set(pos, s); } - public Drawable getImage(OsmandApplication ctx, int pos, boolean light) { int lst = iconList.get(pos); if (lst != 0) { @@ -191,22 +157,35 @@ public class ContextMenuAdapter { return null; } - public int getBackgroundColor(Context ctx, boolean holoLight) { - if (holoLight) { - return ctx.getResources().getColor(R.color.bg_color_light); - } else { - return ctx.getResources().getColor(R.color.bg_color_dark); - } - } - - public boolean isCategory(int pos) { return isCategory.get(pos) > 0; } + public void removeItem(int pos) { + titleResList.removeAt(pos); + selectedList.removeAt(pos); + progressList.removeAt(pos); + iconList.removeAt(pos); + lightIconList.removeAt(pos); + secondaryLightIconList.removeAt(pos); + checkListeners.remove(pos); + integerListeners.remove(pos); + isCategory.removeAt(pos); + layoutIds.removeAt(pos); + loadingList.removeAt(pos); + } + + public int getLayoutId(int position) { + int l = layoutIds.get(position); + if (l != -1) { + return l; + } + return defaultLayoutId; + } + public Item item(String name) { Item i = new Item(); - i.id = (name.hashCode() << 4) | items.size(); + i.title = (name.hashCode() << 4) | titleResList.size(); i.name = name; return i; } @@ -214,7 +193,7 @@ public class ContextMenuAdapter { public Item item(@StringRes int resId) { Item i = new Item(); - i.id = resId; + i.title = resId; i.name = ctx.getString(resId); return i; } @@ -223,11 +202,11 @@ public class ContextMenuAdapter { @DrawableRes int icon = 0; @DrawableRes - int lightIcon = 0; + int secondaryIcon = 0; @DrawableRes - int secondaryLightIcon = 0; - @IdRes - int id; + int lightIcon = 0; + @StringRes + int title; String name; int selected = -1; int progress = -1; @@ -248,13 +227,14 @@ public class ContextMenuAdapter { return this; } + public Item colorIcon(@DrawableRes int icon) { this.lightIcon = icon; return this; } public Item secondaryIconColor(@DrawableRes int icon) { - this.secondaryLightIcon = icon; + this.secondaryIcon = icon; return this; } @@ -299,11 +279,10 @@ public class ContextMenuAdapter { } public void reg() { - if (pos >= items.size() || pos < 0) { - pos = items.size(); + if (pos >= titleResList.size() || pos < 0) { + pos = titleResList.size(); } - items.insert(pos, id); - itemNames.add(pos, name); + titleResList.insert(pos, title); itemDescription.add(pos, description); selectedList.insert(pos, selected); progressList.insert(pos, progress); @@ -311,7 +290,7 @@ public class ContextMenuAdapter { layoutIds.insert(pos, layout); iconList.insert(pos, icon); lightIconList.insert(pos, lightIcon); - secondaryLightIconList.insert(pos, secondaryLightIcon); + secondaryLightIconList.insert(pos, secondaryIcon); checkListeners.add(pos, checkBoxListener); integerListeners.add(pos, integerListener); isCategory.insert(pos, cat ? 1 : 0); @@ -329,51 +308,6 @@ public class ContextMenuAdapter { } - public String[] getItemNames() { - return itemNames.toArray(new String[itemNames.size()]); - } - - public void removeItem(int pos) { - items.removeAt(pos); - itemNames.remove(pos); - selectedList.removeAt(pos); - progressList.removeAt(pos); - iconList.removeAt(pos); - lightIconList.removeAt(pos); - secondaryLightIconList.removeAt(pos); - checkListeners.remove(pos); - integerListeners.remove(pos); - isCategory.removeAt(pos); - layoutIds.removeAt(pos); - loadingList.removeAt(pos); - } - - public int getLayoutId(int position) { - int l = layoutIds.get(position); - if (l != -1) { - return l; - } - return defaultLayoutId; - } - - - public void setDefaultLayoutId(int defaultLayoutId) { - this.defaultLayoutId = defaultLayoutId; - } - - - public void setChangeAppModeListener(ConfigureMapMenu.OnClickListener changeAppModeListener) { - this.changeAppModeListener = changeAppModeListener; - } - - - public ArrayAdapter createListAdapter(final Activity activity, final boolean holoLight) { - final int layoutId = defaultLayoutId; - final OsmandApplication app = ((OsmandApplication) activity.getApplication()); - return new ContextMenuArrayAdapter(activity, layoutId, R.id.title, - getItemNames(), app, holoLight); - } - public class ContextMenuArrayAdapter extends ArrayAdapter { private Activity activity; private OsmandApplication app; @@ -381,8 +315,8 @@ public class ContextMenuAdapter { private int layoutId; public ContextMenuArrayAdapter(Activity context, int resource, int textViewResourceId, - String[] objects, OsmandApplication app, boolean holoLight) { - super(context, resource, textViewResourceId, objects); + String[] names, OsmandApplication app, boolean holoLight) { + super(context, resource, textViewResourceId, names); activity = context; this.app = app; this.holoLight = holoLight; @@ -394,7 +328,7 @@ public class ContextMenuAdapter { // User super class to create the View Integer lid = getLayoutId(position); if (lid == R.layout.mode_toggles) { - final Set selected = new LinkedHashSet(); + final Set selected = new LinkedHashSet<>(); return AppModeDialog.prepareAppModeDrawerView(activity, visibleModes, selected, allModes, true, new View.OnClickListener() { @Override public void onClick(View view) { @@ -417,7 +351,7 @@ public class ContextMenuAdapter { if (!isCategory(position)) { AndroidUtils.setTextPrimaryColor(ctx, tv, !holoLight); } - tv.setText(isCategory(position) ? getItemName(position).toUpperCase() : getItemName(position)); + tv.setText(isCategory(position) ? getItem(position).toUpperCase() : getItem(position)); if (layoutId == R.layout.simple_list_menu_item) { int color = activity.getResources() @@ -477,7 +411,7 @@ public class ContextMenuAdapter { OnContextMenuClick ca = getClickAdapter(position); selectedList.set(position, isChecked ? 1 : 0); if (ca != null) { - ca.onContextMenuClick(la, getElementId(position), position, isChecked); + ca.onContextMenuClick(la, getTitleRes(position), position, isChecked); } } }; @@ -532,4 +466,42 @@ public class ContextMenuAdapter { return convertView; } } + + public interface OnContextMenuClick { + //boolean return type needed to desribe if drawer needed to be close or not + boolean onContextMenuClick(ArrayAdapter adapter, int itemId, int pos, boolean isChecked); + } + + public interface OnIntegerValueChangedListener { + boolean onIntegerValueChangedListener(int newValue); + } + + public static abstract class OnRowItemClick implements OnContextMenuClick { + + public OnRowItemClick() { + } + + //boolean return type needed to desribe if drawer needed to be close or not + public boolean onRowItemClick(ArrayAdapter adapter, View view, int itemId, int pos) { + 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); + } + } + } + + public class BooleanResult { + private boolean result = false; + + public void setResult(boolean value) { + result = value; + } + + public boolean getResult() { + return result; + } + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java b/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java index b71428e589..c540e9f675 100644 --- a/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java +++ b/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java @@ -271,10 +271,6 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment { @Override public boolean onOptionsItemSelected(MenuItem item) { -// if (item.getElementId() == EXPORT_ID) { -// export(); -// return true; -// } else if (item.getItemId() == SELECT_MAP_MARKERS_ID) { selectMapMarkers(); return true; diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java index c4ffe64caf..0c2e138cce 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java @@ -298,7 +298,7 @@ public class MapActivityActions implements DialogProvider { @Override public void onClick(DialogInterface dialog, int which) { - int standardId = adapter.getElementId(which); + int standardId = adapter.getTitleRes(which); OnContextMenuClick click = adapter.getClickAdapter(which); if (click != null) { click.onContextMenuClick(listAdapter, standardId, which, false); @@ -883,7 +883,7 @@ public class MapActivityActions implements DialogProvider { ContextMenuAdapter.OnContextMenuClick click = contextMenuAdapter.getClickAdapter(position); if (click.onContextMenuClick(simpleListAdapter, - contextMenuAdapter.getElementId(position), position, false)) { + contextMenuAdapter.getTitleRes(position), position, false)) { mapActivity.closeDrawer(); } } diff --git a/OsmAnd/src/net/osmand/plus/dashboard/DashboardOnMap.java b/OsmAnd/src/net/osmand/plus/dashboard/DashboardOnMap.java index 94caadf133..5ccf3c6362 100644 --- a/OsmAnd/src/net/osmand/plus/dashboard/DashboardOnMap.java +++ b/OsmAnd/src/net/osmand/plus/dashboard/DashboardOnMap.java @@ -1011,7 +1011,7 @@ public class DashboardOnMap implements ObservableScrollViewCallbacks, DynamicLis public void onItemClick(AdapterView parent, View view, int which, long id) { OnContextMenuClick click = cm.getClickAdapter(which); if (click instanceof OnRowItemClick) { - boolean cl = ((OnRowItemClick) click).onRowItemClick(listAdapter, view, cm.getElementId(which), which); + boolean cl = ((OnRowItemClick) click).onRowItemClick(listAdapter, view, cm.getTitleRes(which), which); if (cl) { hideDashboard(); } @@ -1020,7 +1020,7 @@ public class DashboardOnMap implements ObservableScrollViewCallbacks, DynamicLis if (btn != null && btn.getVisibility() == View.VISIBLE) { btn.setChecked(!btn.isChecked()); } else { - if (click.onContextMenuClick(listAdapter, cm.getElementId(which), which, false)) { + if (click.onContextMenuClick(listAdapter, cm.getTitleRes(which), which, false)) { hideDashboard(); } } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java index 6b424e13ef..6eb23900a5 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java @@ -175,13 +175,17 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement basicFileOperation(info, adapter); OsmandPlugin.onContextMenuActivity(getActivity(), null, info, adapter); - String[] values = adapter.getItemNames(); + int[] titleResources = adapter.getTitleResources(); + String[] values = new String[titleResources.length]; + for (int i = 0; i < titleResources.length; i++) { + values[i] = getString(titleResources[i]); + } builder.setItems(values, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { OnContextMenuClick clk = adapter.getClickAdapter(which); if (clk != null) { - clk.onContextMenuClick(null, adapter.getElementId(which), which, false); + clk.onContextMenuClick(null, adapter.getTitleRes(which), which, false); } } @@ -611,10 +615,10 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement split.getItem(); MenuItemCompat.setShowAsAction(split.getItem(), MenuItemCompat.SHOW_AS_ACTION_ALWAYS); } - item = split.add(0, optionsMenuAdapter.getElementId(j), j + 1, optionsMenuAdapter.getItemName(j)); + item = split.add(0, optionsMenuAdapter.getTitleRes(j), j + 1, optionsMenuAdapter.getTitleRes(j)); MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_ALWAYS); } else { - item = menu.add(0, optionsMenuAdapter.getElementId(j), j + 1, optionsMenuAdapter.getItemName(j)); + item = menu.add(0, optionsMenuAdapter.getTitleRes(j), j + 1, optionsMenuAdapter.getTitleRes(j)); MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_ALWAYS); } OsmandApplication app = getMyApplication(); @@ -635,7 +639,7 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement public boolean onOptionsItemSelected(MenuItem item) { int itemId = item.getItemId(); for (int i = 0; i < optionsMenuAdapter.length(); i++) { - if (itemId == optionsMenuAdapter.getElementId(i)) { + if (itemId == optionsMenuAdapter.getTitleRes(i)) { optionsMenuAdapter.getClickAdapter(i).onContextMenuClick(null, itemId, i, false); return true; } diff --git a/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java b/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java index c94f636d1f..d444f4a6b4 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java @@ -243,7 +243,8 @@ public class GpxUiHelper { @Override public boolean processResult(GPXFile[] result) { - cmAdapter.setItemName(position, cmAdapter.getItemName(position) + "\n" + getDescription((OsmandApplication) app, result[0], f, false)); + cmAdapter.setItemDescription(position, + getDescription((OsmandApplication) app, result[0], f, false)); adapter.notifyDataSetInvalidated(); return true; } @@ -260,8 +261,13 @@ public class GpxUiHelper { final boolean light = app.getSettings().isLightContent(); final int layout = R.layout.list_menu_item_native; - final ArrayAdapter listAdapter = new ArrayAdapter(activity, layout, R.id.title, - adapter.getItemNames()) { + int[] tileIds = adapter.getTitleResources(); + String[] titles = new String[tileIds.length]; + for (int i = 0; i < tileIds.length; i++) { + titles[i] = activity.getString(tileIds[i]); + } + final ArrayAdapter listAdapter = new ArrayAdapter(activity, layout, + R.id.title, titles) { @Override public View getView(final int position, View convertView, ViewGroup parent) { // User super class to create the View @@ -278,11 +284,10 @@ public class GpxUiHelper { if (showCurrentGpx && position == 0) { return; } - int nline = adapter.getItemName(position).indexOf('\n'); - if (nline == -1) { + if (adapter.getItemDescr(position) == null) { setDescripionInDialog(arrayAdapter, adapter, activity, dir, list.get(position), position); } else { - adapter.setItemName(position, adapter.getItemName(position).substring(0, nline)); + adapter.setItemDescription(position, null); arrayAdapter.notifyDataSetInvalidated(); } } @@ -294,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(adapter.getTitleRes(position)); tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); // Put the image on the TextView diff --git a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java index 83e468ba40..6879e7b00b 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java @@ -78,8 +78,6 @@ import java.util.Set; public class AvailableGPXFragment extends OsmandExpandableListFragment { public static final int SEARCH_ID = -1; - // public static final int ACTION_ID = 0; - // protected static final int DELETE_ACTION_ID = 1; private boolean selectionMode = false; private List selectedItems = new ArrayList<>(); private ActionMode actionMode; @@ -357,7 +355,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment { OsmandPlugin.onOptionsMenuActivity(getActivity(), this, optionsMenuAdapter); for (int j = 0; j < optionsMenuAdapter.length(); j++) { final MenuItem item; - item = menu.add(0, optionsMenuAdapter.getElementId(j), j + 1, optionsMenuAdapter.getItemName(j)); + item = menu.add(0, optionsMenuAdapter.getTitleRes(j), j + 1, optionsMenuAdapter.getTitleRes(j)); MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_ALWAYS); if (AndroidUiHelper.isOrientationPortrait(getActivity())) { item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @@ -392,7 +390,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment { public boolean onOptionsItemSelected(MenuItem item) { int itemId = item.getItemId(); for (int i = 0; i < optionsMenuAdapter.length(); i++) { - if (itemId == optionsMenuAdapter.getElementId(i)) { + if (itemId == optionsMenuAdapter.getTitleRes(i)) { optionsMenuAdapter.getClickAdapter(i).onContextMenuClick(null, itemId, i, false); return true; } diff --git a/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapWidgetRegistry.java b/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapWidgetRegistry.java index 12087c52ea..d0b23dfcfc 100644 --- a/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapWidgetRegistry.java +++ b/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapWidgetRegistry.java @@ -1,6 +1,5 @@ package net.osmand.plus.views.mapwidgets; -import android.content.Context; import android.content.DialogInterface; import android.support.annotation.DrawableRes; import android.support.annotation.StringRes; @@ -370,10 +369,6 @@ public class MapWidgetRegistry { addControlsAppearance(map, cm, mode); } - public String getText(Context ctx, final ApplicationMode mode, final MapWidgetRegInfo r) { - return (r.visibleCollapsed(mode) ? " + " : " ") + ctx.getString(r.messageId); - } - public Set getRight() { return right; } @@ -405,14 +400,12 @@ public class MapWidgetRegistry { if (mil != null) { mil.recreateControls(); } - adapter.setItemName(pos, getText(mil.getMapActivity(), mode, r)); adapter.setSelection(pos, r.visibleCollapsed(mode) || r.visible(mode) ? 1 : 0); a.notifyDataSetChanged(); return false; } }) .reg(); - adapter.setItemName(adapter.length() - 1, getText(map, mode, r)); } }