Context menu refactoring

This commit is contained in:
Alexey Kulish 2015-10-25 19:58:13 +03:00
parent 5d810c3f57
commit 3c953d048a
16 changed files with 98 additions and 150 deletions

View file

@ -65,7 +65,6 @@ public abstract class BaseMenuController {
protected Drawable getIcon(int iconId, int colorLightId, int colorDarkId) {
IconsCache iconsCache = getMapActivity().getMyApplication().getIconsCache();
return iconsCache.getIcon(iconId,
isLight() ? colorLightId : colorDarkId);
return iconsCache.getIcon(iconId, isLight() ? colorLightId : colorDarkId);
}
}

View file

@ -125,7 +125,7 @@ public class MapContextMenu extends MenuTitleController {
initTitle();
if (menuController != null) {
menuController.addPlainMenuItems(typeStr, this.pointDescription);
menuController.addPlainMenuItems(typeStr, this.pointDescription, this.latLon);
}
if (mapPosition != 0) {
@ -190,7 +190,7 @@ public class MapContextMenu extends MenuTitleController {
}
private void acquireMenuController() {
menuController = MenuController.getMenuController(mapActivity, latLon, pointDescription, object);
menuController = MenuController.getMenuController(mapActivity, pointDescription, object);
}
public void onSingleTapOnMap() {
@ -313,7 +313,7 @@ public class MapContextMenu extends MenuTitleController {
acquireIcons();
if (menuController != null) {
menuController.addPlainMenuItems(typeStr, this.pointDescription);
menuController.addPlainMenuItems(typeStr, pointDescription, latLon);
}
}

View file

@ -23,7 +23,7 @@ import java.util.LinkedList;
import static android.util.TypedValue.COMPLEX_UNIT_DIP;
public abstract class MenuBuilder {
public class MenuBuilder {
public static final float SHADOW_HEIGHT_TOP_DP = 16f;
public static final float SHADOW_HEIGHT_BOTTOM_DP = 6f;
@ -59,6 +59,19 @@ public abstract class MenuBuilder {
public void build(View view) {
firstRow = true;
if (needBuildPlainMenuItems()) {
buildPlainMenuItems(view);
}
}
protected void buildPlainMenuItems(View view) {
for (PlainMenuItem item : plainMenuItems) {
buildRow(view, item.getIconId(), item.getText(), 0);
}
}
protected boolean needBuildPlainMenuItems() {
return true;
}
protected boolean isFirstRow() {
@ -69,6 +82,10 @@ public abstract class MenuBuilder {
firstRow = false;
}
private void buildRow(View view, int iconId, String text, int textColor) {
buildRow(view, getRowIcon(iconId), text, textColor);
}
protected void buildRow(final View view, Drawable icon, String text, int textColor) {
LinearLayout ll = new LinearLayout(view.getContext());
ll.setOrientation(LinearLayout.HORIZONTAL);

View file

@ -8,7 +8,6 @@ import net.osmand.data.Amenity;
import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
import net.osmand.data.PointDescription;
import net.osmand.plus.IconsCache;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
@ -41,7 +40,7 @@ public abstract class MenuController extends BaseMenuController {
}
public static MenuController getMenuController(MapActivity mapActivity,
LatLon latLon, PointDescription pointDescription, Object object) {
PointDescription pointDescription, Object object) {
OsmandApplication app = mapActivity.getMyApplication();
MenuController menuController = null;
if (object != null) {
@ -57,7 +56,7 @@ public abstract class MenuController extends BaseMenuController {
}
}
} else {
menuController = new PointDescriptionMenuController(app, mapActivity, pointDescription, latLon);
menuController = new PointDescriptionMenuController(app, mapActivity, pointDescription);
}
return menuController;
}
@ -66,7 +65,15 @@ public abstract class MenuController extends BaseMenuController {
builder.addPlainMenuItem(iconId, text);
}
public void addPlainMenuItems(String typeStr, PointDescription pointDescription) {
public void addPlainMenuItems(String typeStr, PointDescription pointDescription, LatLon latLon) {
addMyLocationToPlainItems(pointDescription, latLon);
}
protected void addMyLocationToPlainItems(PointDescription pointDescription, LatLon latLon) {
if (pointDescription != null) {
addPlainMenuItem(R.drawable.map_my_location, PointDescription.getLocationName(getMapActivity(),
latLon.getLatitude(), latLon.getLongitude(), true).replaceAll("\n", ""));
}
}
public int getInitialMenuState() {

View file

@ -119,14 +119,9 @@ public class AmenityMenuBuilder extends MenuBuilder {
super.build(view);
boolean hasWiki = false;
for (PlainMenuItem item : plainMenuItems) {
buildRow(view, item.getIconId(), item.getText(), 0, false);
}
MapPoiTypes poiTypes = app.getPoiTypes();
for (Map.Entry<String, String> e : amenity.getAdditionalInfo().entrySet()) {
int iconId = 0;
int iconId;
Drawable icon = null;
int textColor = 0;
boolean isWiki = false;

View file

@ -3,6 +3,7 @@ package net.osmand.plus.mapcontextmenu.details;
import android.os.Bundle;
import net.osmand.data.Amenity;
import net.osmand.data.LatLon;
import net.osmand.data.PointDescription;
import net.osmand.osm.PoiCategory;
import net.osmand.osm.PoiType;
@ -74,14 +75,11 @@ public class AmenityMenuController extends MenuController {
}
@Override
public void addPlainMenuItems(String typeStr, PointDescription pointDescription) {
public void addPlainMenuItems(String typeStr, PointDescription pointDescription, LatLon latLon) {
if (!Algorithms.isEmpty(typeStr)) {
addPlainMenuItem(R.drawable.ic_action_info_dark, typeStr);
}
if (pointDescription != null) {
addPlainMenuItem(R.drawable.map_my_location, PointDescription.getLocationName(getMapActivity(),
amenity.getLocation().getLatitude(), amenity.getLocation().getLongitude(), true).replaceAll("\n", ""));
}
addMyLocationToPlainItems(pointDescription, amenity.getLocation());
}
@Override

View file

@ -21,6 +21,11 @@ public class FavouritePointMenuBuilder extends MenuBuilder {
buildRow(view, getRowIcon(iconId), text, textColor);
}
@Override
protected boolean needBuildPlainMenuItems() {
return false;
}
@Override
public void build(View view) {
super.build(view);
@ -29,8 +34,6 @@ public class FavouritePointMenuBuilder extends MenuBuilder {
buildRow(view, R.drawable.ic_action_note_dark, fav.getDescription(), 0);
}
for (PlainMenuItem item : plainMenuItems) {
buildRow(view, item.getIconId(), item.getText(), 0);
}
buildPlainMenuItems(view);
}
}

View file

@ -5,7 +5,6 @@ import android.os.Bundle;
import android.support.v4.app.Fragment;
import net.osmand.data.FavouritePoint;
import net.osmand.data.PointDescription;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
@ -79,14 +78,6 @@ public class FavouritePointMenuController extends MenuController {
return fav.getName();
}
@Override
public void addPlainMenuItems(String typeStr, PointDescription pointDescription) {
if (pointDescription != null) {
addPlainMenuItem(R.drawable.map_my_location, PointDescription.getLocationName(getMapActivity(),
fav.getLatitude(), fav.getLongitude(), true).replaceAll("\n", ""));
}
}
@Override
public void saveEntityState(Bundle bundle, String key) {
bundle.putSerializable(key, fav);

View file

@ -1,29 +0,0 @@
package net.osmand.plus.mapcontextmenu.details;
import android.view.View;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.helpers.SearchHistoryHelper.HistoryEntry;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
public class HistoryMenuBuilder extends MenuBuilder {
private final HistoryEntry entry;
public HistoryMenuBuilder(OsmandApplication app, final HistoryEntry entry) {
super(app);
this.entry = entry;
}
private void buildRow(View view, int iconId, String text, int textColor) {
buildRow(view, getRowIcon(iconId), text, textColor);
}
@Override
public void build(View view) {
super.build(view);
for (PlainMenuItem item : plainMenuItems) {
buildRow(view, item.getIconId(), item.getText(), 0);
}
}
}

View file

@ -3,12 +3,12 @@ package net.osmand.plus.mapcontextmenu.details;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import net.osmand.data.PointDescription;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.search.SearchHistoryFragment;
import net.osmand.plus.helpers.SearchHistoryHelper.HistoryEntry;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
import net.osmand.plus.mapcontextmenu.MenuController;
public class HistoryMenuController extends MenuController {
@ -16,7 +16,7 @@ public class HistoryMenuController extends MenuController {
private HistoryEntry entry;
public HistoryMenuController(OsmandApplication app, MapActivity mapActivity, final HistoryEntry entry) {
super(new HistoryMenuBuilder(app, entry), mapActivity);
super(new MenuBuilder(app), mapActivity);
this.entry = entry;
}
@ -69,14 +69,6 @@ public class HistoryMenuController extends MenuController {
return !entry.getName().isAddress();
}
@Override
public void addPlainMenuItems(String typeStr, PointDescription pointDescription) {
if (pointDescription != null) {
addPlainMenuItem(R.drawable.map_my_location, PointDescription.getLocationName(getMapActivity(),
entry.getLat(), entry.getLon(), true).replaceAll("\n", ""));
}
}
@Override
public void saveEntityState(Bundle bundle, String key) {
bundle.putSerializable(key, entry);

View file

@ -0,0 +1,47 @@
package net.osmand.plus.mapcontextmenu.details;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import net.osmand.data.LatLon;
import net.osmand.data.PointDescription;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.search.SearchHistoryFragment;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
import net.osmand.plus.mapcontextmenu.MenuController;
public class MyLocationMenuController extends MenuController {
public MyLocationMenuController(OsmandApplication app, MapActivity mapActivity) {
super(new MenuBuilder(app), mapActivity);
}
@Override
protected int getInitialMenuStatePortrait() {
return MenuState.HEADER_ONLY;
}
@Override
protected int getSupportedMenuStatesPortrait() {
return MenuState.HEADER_ONLY | MenuState.HALF_SCREEN;
}
@Override
public Drawable getLeftIcon() {
ApplicationMode appMode = getMapActivity().getMyApplication().getSettings().getApplicationMode();
return getMapActivity().getResources().getDrawable(appMode.getResourceLocation());
}
@Override
public String getNameStr() {
return getMapActivity().getString(R.string.shared_string_my_location);
}
@Override
public void saveEntityState(Bundle bundle, String key) {
}
}

View file

@ -1,26 +0,0 @@
package net.osmand.plus.mapcontextmenu.details;
import android.view.View;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
public class ParkingPositionBuilder extends MenuBuilder {
public ParkingPositionBuilder(OsmandApplication app) {
super(app);
}
private void buildRow(View view, int iconId, String text, int textColor) {
buildRow(view, getRowIcon(iconId), text, textColor);
}
@Override
public void build(View view) {
super.build(view);
for (PlainMenuItem item : plainMenuItems) {
buildRow(view, item.getIconId(), item.getText(), 0);
}
}
}

View file

@ -9,6 +9,7 @@ import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
import net.osmand.plus.mapcontextmenu.MenuController;
import net.osmand.plus.parkingpoint.ParkingPositionPlugin;
import net.osmand.util.Algorithms;
@ -21,7 +22,7 @@ public class ParkingPositionController extends MenuController {
String parkingDescription = "";
public ParkingPositionController(OsmandApplication app, MapActivity mapActivity, final PointDescription pointDescription, LatLon latLon) {
super(new ParkingPositionBuilder(app), mapActivity);
super(new MenuBuilder(app), mapActivity);
this.pointDescription = pointDescription;
this.latLon = latLon;
plugin = OsmandPlugin.getPlugin(ParkingPositionPlugin.class);
@ -88,14 +89,6 @@ public class ParkingPositionController extends MenuController {
return false;
}
@Override
public void addPlainMenuItems(String typeStr, PointDescription pointDescription) {
if (pointDescription != null) {
addPlainMenuItem(R.drawable.map_my_location, PointDescription.getLocationName(getMapActivity(),
latLon.getLatitude(), latLon.getLongitude(), true).replaceAll("\n", ""));
}
}
@Override
public void saveEntityState(Bundle bundle, String key) {
bundle.putSerializable(key, latLon);

View file

@ -1,29 +0,0 @@
package net.osmand.plus.mapcontextmenu.details;
import android.view.View;
import net.osmand.data.PointDescription;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
public class PointDescriptionMenuBuilder extends MenuBuilder {
private final PointDescription pointDescription;
public PointDescriptionMenuBuilder(OsmandApplication app, final PointDescription pointDescription) {
super(app);
this.pointDescription = pointDescription;
}
private void buildRow(View view, int iconId, String text, int textColor) {
buildRow(view, getRowIcon(iconId), text, textColor);
}
@Override
public void build(View view) {
super.build(view);
for (MenuBuilder.PlainMenuItem item : plainMenuItems) {
buildRow(view, item.getIconId(), item.getText(), 0);
}
}
}

View file

@ -3,23 +3,21 @@ package net.osmand.plus.mapcontextmenu.details;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import net.osmand.data.LatLon;
import net.osmand.data.PointDescription;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.search.SearchHistoryFragment;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
import net.osmand.plus.mapcontextmenu.MenuController;
public class PointDescriptionMenuController extends MenuController {
private PointDescription pointDescription;
private LatLon latLon;
public PointDescriptionMenuController(OsmandApplication app, MapActivity mapActivity, final PointDescription pointDescription, LatLon latLon) {
super(new PointDescriptionMenuBuilder(app, pointDescription), mapActivity);
public PointDescriptionMenuController(OsmandApplication app, MapActivity mapActivity, final PointDescription pointDescription) {
super(new MenuBuilder(app), mapActivity);
this.pointDescription = pointDescription;
this.latLon = latLon;
}
@Override
@ -71,14 +69,6 @@ public class PointDescriptionMenuController extends MenuController {
return !pointDescription.isAddress();
}
@Override
public void addPlainMenuItems(String typeStr, PointDescription pointDescription) {
if (pointDescription != null) {
addPlainMenuItem(R.drawable.map_my_location, PointDescription.getLocationName(getMapActivity(),
latLon.getLatitude(), latLon.getLongitude(), true).replaceAll("\n", ""));
}
}
@Override
public void saveEntityState(Bundle bundle, String key) {
}

View file

@ -47,7 +47,7 @@ public class ObjectSelectionMenu extends BaseMenuController {
}
protected void init() {
controller = MenuController.getMenuController(mapActivity, latLon, pointDescription, object);
controller = MenuController.getMenuController(mapActivity, pointDescription, object);
initTitle();
}