From 5f9352b404b8318e8a878df7398f1aae47a0ac47 Mon Sep 17 00:00:00 2001 From: GaidamakUA Date: Mon, 28 Mar 2016 12:56:41 +0300 Subject: [PATCH] More refactoring. --- .../net/osmand/plus/ContextMenuAdapter.java | 135 ++++++------------ .../plus/activities/MapActivityActions.java | 4 +- .../plus/activities/MapActivityLayers.java | 2 +- .../activities/actions/AppModeDialog.java | 3 +- .../osmand/plus/dialogs/ConfigureMapMenu.java | 8 +- .../osmand/plus/dialogs/RasterMapMenu.java | 2 +- .../download/ui/LocalIndexesFragment.java | 4 +- .../net/osmand/plus/helpers/GpxUiHelper.java | 2 +- .../plus/mapcontextmenu/MapContextMenu.java | 2 +- .../other/RoutePreferencesMenu.java | 2 +- .../plus/myplaces/AvailableGPXFragment.java | 2 +- .../views/mapwidgets/MapWidgetRegistry.java | 4 +- 12 files changed, 58 insertions(+), 112 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java b/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java index e609ec89ad..4a2e34e78e 100644 --- a/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java +++ b/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java @@ -1,11 +1,11 @@ package net.osmand.plus; import android.app.Activity; -import android.content.Context; 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.v4.content.ContextCompat; import android.support.v4.graphics.drawable.DrawableCompat; @@ -35,60 +35,38 @@ import java.util.Set; public class ContextMenuAdapter { private static final Log LOG = PlatformUtil.getLog(ContextMenuAdapter.class); - private final Context ctx; @LayoutRes - private int defaultLayoutId = Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ? + private int DEFAULT_LAYOUT_ID = Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ? 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 - private BooleanResult allModes = new BooleanResult(); - - public ContextMenuAdapter(Context ctx, boolean allModes) { - this.ctx = ctx; - this.allModes.setResult(allModes); - } - - public ContextMenuAdapter(Context ctx) { - this.ctx = ctx; - } public int length() { return items.size(); } + @Deprecated public int getElementId(int position) { return items.get(position).getTitleId(); } + @Deprecated public OnContextMenuClick getClickAdapter(int position) { return items.get(position).getCheckBoxListener(); } - public OnIntegerValueChangedListener getIntegerLister(int position) { - return items.get(position).getIntegerListener(); - } - + @Deprecated public String getItemName(int position) { return items.get(position).getTitle(); } - public String getItemDescr(int position) { - return items.get(position).getDescription(); - } - + @Deprecated public Boolean getSelection(int position) { return items.get(position).getSelected(); } - public int getProgress(int position) { - return items.get(position).getProgress(); - } - - public int getLoading(int position) { - return items.get(position).isLoading() ? 1 : 0; - } - + @Deprecated public Drawable getImage(OsmandApplication ctx, int position, boolean light) { @DrawableRes int lst = items.get(position).getIcon(); @@ -103,39 +81,12 @@ public class ContextMenuAdapter { return null; } - public Drawable getSecondaryImage(OsmandApplication ctx, int position, boolean light) { - @DrawableRes - int secondaryDrawableId = items.get(position).getSecondaryLightIcon(); - if (secondaryDrawableId != -1) { - return ctx.getIconsCache().getIcon(secondaryDrawableId, light); - } - 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 items.get(pos).isCategory(); - } - - public int getLayoutId(int position) { - int l = items.get(position).getLayout(); - if (l != -1) { - return l; - } - return defaultLayoutId; - } - + @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); } @@ -144,6 +95,7 @@ public class ContextMenuAdapter { items.get(position).setSelected(s); } + @Deprecated public void setProgress(int position, int progress) { items.get(position).setProgress(progress); } @@ -166,7 +118,7 @@ public class ContextMenuAdapter { } public void setDefaultLayoutId(int defaultLayoutId) { - this.defaultLayoutId = defaultLayoutId; + this.DEFAULT_LAYOUT_ID = defaultLayoutId; } @@ -176,34 +128,45 @@ public class ContextMenuAdapter { public ArrayAdapter createListAdapter(final Activity activity, final boolean holoLight) { - final int layoutId = defaultLayoutId; + final int layoutId = DEFAULT_LAYOUT_ID; final OsmandApplication app = ((OsmandApplication) activity.getApplication()); return new ContextMenuArrayAdapter(activity, layoutId, R.id.title, - items.toArray(new ContextMenuItem[items.size()]), app, holoLight); + items.toArray(new ContextMenuItem[items.size()]), app, holoLight, changeAppModeListener); } public class ContextMenuArrayAdapter extends ArrayAdapter { private OsmandApplication app; private boolean holoLight; + @LayoutRes private int layoutId; + private final ConfigureMapMenu.OnClickListener changeAppModeListener; - public ContextMenuArrayAdapter(Activity context, int resource, int textViewResourceId, - ContextMenuItem[] objects, OsmandApplication app, boolean holoLight) { - super(context, resource, textViewResourceId, objects); + public ContextMenuArrayAdapter(Activity context, + @LayoutRes + int layoutRes, + @IdRes + int textViewResourceId, + ContextMenuItem[] objects, + OsmandApplication app, + boolean holoLight, + ConfigureMapMenu.OnClickListener changeAppModeListener) { + super(context, layoutRes, textViewResourceId, objects); this.app = app; this.holoLight = holoLight; - layoutId = resource; + layoutId = layoutRes; + this.changeAppModeListener = changeAppModeListener; } @Override public View getView(final int position, View convertView, ViewGroup parent) { // User super class to create the View final ContextMenuItem item = getItem(position); - Integer lid = getLayoutId(position); + int lid = item.getLayout(); + lid = lid != -1 ? lid : DEFAULT_LAYOUT_ID; if (lid == R.layout.mode_toggles) { final Set selected = new LinkedHashSet(); return AppModeDialog.prepareAppModeDrawerView((Activity) getContext(), - selected, allModes, true, new View.OnClickListener() { + selected, true, new View.OnClickListener() { @Override public void onClick(View view) { if (selected.size() > 0) { @@ -211,21 +174,20 @@ public class ContextMenuAdapter { notifyDataSetChanged(); } if (changeAppModeListener != null) { - changeAppModeListener.onClick(allModes.getResult()); + changeAppModeListener.onClick(); } } }); } - if (convertView == null || (!lid.equals(convertView.getTag()))) { + if (convertView == null || (lid != convertView.getTag())) { convertView = LayoutInflater.from(getContext()).inflate(lid, parent, false); -// AndroidUtils.setListItemBackground(ctx, convertView, !holoLight); convertView.setTag(lid); } TextView tv = (TextView) convertView.findViewById(R.id.title); - if (!isCategory(position)) { + if (!item.isCategory()) { AndroidUtils.setTextPrimaryColor(getContext(), tv, !holoLight); } - tv.setText(isCategory(position) ? getItemName(position).toUpperCase() : getItemName(position)); + tv.setText(item.isCategory() ? item.getTitle().toUpperCase() : item.getTitle()); if (layoutId == R.layout.simple_list_menu_item) { int color = ContextCompat.getColor(getContext(), @@ -253,7 +215,7 @@ public class ContextMenuAdapter { if (secondaryLightDrawable != -1) { int color = ContextCompat.getColor(getContext(), holoLight ? R.color.icon_color : R.color.dashboard_subheader_text_dark); - Drawable drawable = getSecondaryImage(app, position, holoLight); + Drawable drawable = ContextCompat.getDrawable(getContext(), item.getSecondaryLightIcon()); DrawableCompat.setTint(drawable, color); ImageView imageView = (ImageView) convertView.findViewById(R.id.secondary_icon); imageView.setImageDrawable(drawable); @@ -265,13 +227,13 @@ public class ContextMenuAdapter { } } - if (isCategory(position)) { + if (item.isCategory()) { tv.setTypeface(Typeface.DEFAULT_BOLD); } else { tv.setTypeface(null); } - if (convertView.findViewById(R.id.toggle_item) != null) { + if (convertView.findViewById(R.id.toggle_item) != null && !item.isCategory()) { final CompoundButton ch = (CompoundButton) convertView.findViewById(R.id.toggle_item); if (item.getSelected() != null) { ch.setOnCheckedChangeListener(null); @@ -282,10 +244,10 @@ public class ContextMenuAdapter { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - OnContextMenuClick ca = getClickAdapter(position); + OnContextMenuClick ca = item.getCheckBoxListener(); item.setSelected(isChecked); if (ca != null) { - ca.onContextMenuClick(la, getElementId(position), position, isChecked); + ca.onContextMenuClick(la, item.getTitleId(), position, isChecked); } } }; @@ -299,11 +261,11 @@ public class ContextMenuAdapter { if (convertView.findViewById(R.id.seekbar) != null) { SeekBar seekBar = (SeekBar) convertView.findViewById(R.id.seekbar); if (item.getProgress() != -1) { - seekBar.setProgress(getProgress(position)); + seekBar.setProgress(item.getProgress()); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - OnIntegerValueChangedListener listener = getIntegerLister(position); + OnIntegerValueChangedListener listener = item.getIntegerListener(); item.setProgress(progress); if (listener != null && fromUser) { listener.onIntegerValueChangedListener(progress); @@ -333,7 +295,7 @@ public class ContextMenuAdapter { } } - String itemDescr = getItemDescr(position); + String itemDescr = item.getDescription(); if (convertView.findViewById(R.id.description) != null) { ((TextView) convertView.findViewById(R.id.description)).setText(itemDescr); } @@ -366,17 +328,4 @@ public class ContextMenuAdapter { } } } - - 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/MapActivityActions.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java index dee089cfcc..09d57ab8cf 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java @@ -279,7 +279,7 @@ public class MapActivityActions implements DialogProvider { } public void contextMenuPoint(final double latitude, final double longitude, final ContextMenuAdapter iadapter, Object selectedObj) { - final ContextMenuAdapter adapter = iadapter == null ? new ContextMenuAdapter(mapActivity) : iadapter; + final ContextMenuAdapter adapter = iadapter == null ? new ContextMenuAdapter() : iadapter; ContextMenuItem.ItemBuilder itemBuilder = new ContextMenuItem.ItemBuilder(); adapter.addItem(itemBuilder.setTitleId(R.string.context_menu_item_search, mapActivity) .setColorIcon(R.drawable.ic_action_search_dark).createItem()); @@ -594,7 +594,7 @@ public class MapActivityActions implements DialogProvider { public ContextMenuAdapter createMainOptionsMenu() { final OsmandMapTileView mapView = mapActivity.getMapView(); final OsmandApplication app = mapActivity.getMyApplication(); - ContextMenuAdapter optionsMenuHelper = new ContextMenuAdapter(app); + ContextMenuAdapter optionsMenuHelper = new ContextMenuAdapter(); optionsMenuHelper.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.home, mapActivity) .setColorIcon(R.drawable.map_dashboard) diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java index b481d897c0..c70a315e47 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java @@ -261,7 +261,7 @@ public class MapActivityLayers { public AlertDialog selectPOIFilterLayer(final OsmandMapTileView mapView, final PoiUIFilter[] selected) { OsmandApplication app = (OsmandApplication) getApplication(); final PoiFiltersHelper poiFilters = app.getPoiFilters(); - final ContextMenuAdapter adapter = new ContextMenuAdapter(activity); + final ContextMenuAdapter adapter = new ContextMenuAdapter(); adapter.addItem(new ContextMenuItem.ItemBuilder() .setTitleId(R.string.shared_string_search, app) .setColorIcon(R.drawable.ic_action_search_dark).createItem()); diff --git a/OsmAnd/src/net/osmand/plus/activities/actions/AppModeDialog.java b/OsmAnd/src/net/osmand/plus/activities/actions/AppModeDialog.java index 676cc5687e..89ea9e603f 100644 --- a/OsmAnd/src/net/osmand/plus/activities/actions/AppModeDialog.java +++ b/OsmAnd/src/net/osmand/plus/activities/actions/AppModeDialog.java @@ -10,7 +10,6 @@ import android.widget.LinearLayout.LayoutParams; import net.osmand.AndroidUtils; import net.osmand.plus.ApplicationMode; -import net.osmand.plus.ContextMenuAdapter; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; @@ -36,7 +35,7 @@ public class AppModeDialog { //special method for drawer menu //needed because if there's more than 4 items - the don't fit in drawer - public static View prepareAppModeDrawerView(Activity a, final Set selected, ContextMenuAdapter.BooleanResult allModes, + public static View prepareAppModeDrawerView(Activity a, final Set selected, boolean useMapTheme, final View.OnClickListener onClickListener) { OsmandSettings settings = ((OsmandApplication) a.getApplication()).getSettings(); final List values = new ArrayList(ApplicationMode.values(settings)); diff --git a/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java b/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java index 5bd24db967..511c6744c7 100644 --- a/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java +++ b/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java @@ -56,22 +56,20 @@ public class ConfigureMapMenu { private static final Log LOG = PlatformUtil.getLog(ConfigureMapMenu.class); public interface OnClickListener { - public void onClick(boolean result); + void onClick(); } - ; - private boolean allModes = false; public ContextMenuAdapter createListAdapter(final MapActivity ma) { - ContextMenuAdapter adapter = new ContextMenuAdapter(ma, allModes); + ContextMenuAdapter adapter = new ContextMenuAdapter(); adapter.setDefaultLayoutId(R.layout.drawer_list_item); adapter.addItem(new ContextMenuItem.ItemBuilder() .setTitleId(R.string.app_modes_choose, ma) .setLayout(R.layout.mode_toggles).createItem()); adapter.setChangeAppModeListener(new OnClickListener() { @Override - public void onClick(boolean result) { + public void onClick() { allModes = true; ma.getDashboard().updateListAdapter(createListAdapter(ma)); } diff --git a/OsmAnd/src/net/osmand/plus/dialogs/RasterMapMenu.java b/OsmAnd/src/net/osmand/plus/dialogs/RasterMapMenu.java index 13112fce97..4407debc33 100644 --- a/OsmAnd/src/net/osmand/plus/dialogs/RasterMapMenu.java +++ b/OsmAnd/src/net/osmand/plus/dialogs/RasterMapMenu.java @@ -21,7 +21,7 @@ public class RasterMapMenu { public static ContextMenuAdapter createListAdapter(final MapActivity mapActivity, final OsmandRasterMapsPlugin.RasterMapType type) { - ContextMenuAdapter adapter = new ContextMenuAdapter(mapActivity, false); + ContextMenuAdapter adapter = new ContextMenuAdapter(); adapter.setDefaultLayoutId(R.layout.list_item_icon_and_menu); createLayersItems(adapter, mapActivity, type); return adapter; diff --git a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java index 2b3f459ac2..42a0462ed4 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java @@ -172,7 +172,7 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement private void showContextMenu(final LocalIndexInfo info) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - final ContextMenuAdapter adapter = new ContextMenuAdapter(getActivity()); + final ContextMenuAdapter adapter = new ContextMenuAdapter(); basicFileOperation(info, adapter); OsmandPlugin.onContextMenuActivity(getActivity(), null, info, adapter); @@ -593,7 +593,7 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement ActionBar actionBar = getDownloadActivity().getSupportActionBar(); //hide action bar from downloadindexfragment actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); - optionsMenuAdapter = new ContextMenuAdapter(getDownloadActivity()); + optionsMenuAdapter = new ContextMenuAdapter(); OnContextMenuClick listener = new OnContextMenuClick() { @Override public boolean onContextMenuClick(ArrayAdapter adapter, int itemId, int pos, boolean isChecked) { diff --git a/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java b/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java index a49bc65601..de2afa2e57 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java @@ -198,7 +198,7 @@ public class GpxUiHelper { private static ContextMenuAdapter createGpxContextMenuAdapter(Activity activity, List allGpxList, List selectedGpxList, boolean multipleChoice, boolean showCurrentTrack) { - final ContextMenuAdapter adapter = new ContextMenuAdapter(activity); + final ContextMenuAdapter adapter = new ContextMenuAdapter(); //element position in adapter int i = 0; for (String s : allGpxList) { diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java index 7ba2ec92ca..84f980b830 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java @@ -553,7 +553,7 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL } public void buttonMorePressed() { - final ContextMenuAdapter menuAdapter = new ContextMenuAdapter(mapActivity); + final ContextMenuAdapter menuAdapter = new ContextMenuAdapter(); for (OsmandMapLayer layer : mapActivity.getMapView().getLayers()) { layer.populateObjectContextMenu(latLon, object, menuAdapter, mapActivity); } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/RoutePreferencesMenu.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/RoutePreferencesMenu.java index 41eb776da9..988365e68f 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/RoutePreferencesMenu.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/RoutePreferencesMenu.java @@ -204,7 +204,7 @@ public class RoutePreferencesMenu { } public static void selectVoiceGuidance(final MapActivity mapActivity, final CallbackWithObject callback) { - final ContextMenuAdapter adapter = new ContextMenuAdapter(mapActivity); + final ContextMenuAdapter adapter = new ContextMenuAdapter(); String[] entries; final String[] entrieValues; diff --git a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java index 3cccf990d2..43980a45b2 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java @@ -327,7 +327,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment { ((FavoritesActivity) getActivity()).getClearToolbar(false); } - optionsMenuAdapter = new ContextMenuAdapter(getActivity()); + optionsMenuAdapter = new ContextMenuAdapter(); OnContextMenuClick listener = new OnContextMenuClick() { @Override public boolean onContextMenuClick(ArrayAdapter adapter, final int itemId, int pos, boolean isChecked) { diff --git a/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapWidgetRegistry.java b/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapWidgetRegistry.java index 7ea56c5711..b2b05968c5 100644 --- a/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapWidgetRegistry.java +++ b/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapWidgetRegistry.java @@ -496,14 +496,14 @@ public class MapWidgetRegistry { } public ContextMenuAdapter getViewConfigureMenuAdapter(final MapActivity map) { - final ContextMenuAdapter cm = new ContextMenuAdapter(map); + final ContextMenuAdapter cm = new ContextMenuAdapter(); cm.setDefaultLayoutId(R.layout.list_item_icon_and_menu); cm.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.app_modes_choose, map) .setLayout(R.layout.mode_toggles).createItem()); cm.setChangeAppModeListener(new ConfigureMapMenu.OnClickListener() { @Override - public void onClick(boolean allModes) { + public void onClick() { map.getDashboard().updateListAdapter(getViewConfigureMenuAdapter(map)); } });