From 8af53645c0a3d668f538d785a7a32eb4bf865ca0 Mon Sep 17 00:00:00 2001 From: Rosty Date: Wed, 28 Dec 2016 18:33:49 +0200 Subject: [PATCH] quick action add favorite functionality (ui/logic/ without categories) --- .../res/layout/quick_action_add_favorite.xml | 208 ++++++++++++++++++ OsmAnd/res/layout/quick_action_add_marker.xml | 23 ++ OsmAnd/res/values/strings.xml | 3 + .../plus/mapcontextmenu/MapContextMenu.java | 2 + .../editors/FavoritePointEditorFragment.java | 2 +- .../quickaction/CreateEditActionDialog.java | 10 +- .../osmand/plus/quickaction/QuickAction.java | 14 +- .../plus/quickaction/QuickActionFactory.java | 173 ++++++++++++--- .../plus/views/MapQuickActionLayer.java | 3 +- 9 files changed, 404 insertions(+), 34 deletions(-) create mode 100644 OsmAnd/res/layout/quick_action_add_favorite.xml create mode 100644 OsmAnd/res/layout/quick_action_add_marker.xml diff --git a/OsmAnd/res/layout/quick_action_add_favorite.xml b/OsmAnd/res/layout/quick_action_add_favorite.xml new file mode 100644 index 0000000000..83dced3886 --- /dev/null +++ b/OsmAnd/res/layout/quick_action_add_favorite.xml @@ -0,0 +1,208 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/quick_action_add_marker.xml b/OsmAnd/res/layout/quick_action_add_marker.xml new file mode 100644 index 0000000000..78551a7815 --- /dev/null +++ b/OsmAnd/res/layout/quick_action_add_marker.xml @@ -0,0 +1,23 @@ + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index a356b06de2..56d28c1d16 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -2464,6 +2464,7 @@ If you need help with OsmAnd application, please contact our support team: suppo Report for File name contains illegal character Quick action + Action %d Screen %d Add marker @@ -2480,4 +2481,6 @@ If you need help with OsmAnd application, please contact our support team: suppo Leave field blank and OsmAnd will use the address or name of a place for the favorite point Action name Name + Tap on action will add marker to the specified location. + Show favorite dialog diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java index 55d47b2d56..0985f6f676 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java @@ -671,6 +671,8 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL close(); } + + public void buttonFavoritePressed() { if (object != null && object instanceof FavouritePoint) { getFavoritePointEditor().edit((FavouritePoint) object); diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/FavoritePointEditorFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/FavoritePointEditorFragment.java index 23a88688db..b7e5f1063c 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/FavoritePointEditorFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/FavoritePointEditorFragment.java @@ -153,7 +153,7 @@ public class FavoritePointEditorFragment extends PointEditorFragment { MapContextMenu menu = getMapActivity().getContextMenu(); LatLon latLon = new LatLon(favorite.getLatitude(), favorite.getLongitude()); - if (menu.getLatLon().equals(latLon)) { + if (menu.getLatLon() != null && menu.getLatLon().equals(latLon)) { menu.update(latLon, favorite.getPointDescription(), favorite); } } diff --git a/OsmAnd/src/net/osmand/plus/quickaction/CreateEditActionDialog.java b/OsmAnd/src/net/osmand/plus/quickaction/CreateEditActionDialog.java index 87c4c28b68..0740a9a50c 100644 --- a/OsmAnd/src/net/osmand/plus/quickaction/CreateEditActionDialog.java +++ b/OsmAnd/src/net/osmand/plus/quickaction/CreateEditActionDialog.java @@ -107,15 +107,15 @@ public class CreateEditActionDialog extends DialogFragment { ? isNew = actionId == 0 : savedInstanceState.getBoolean(KEY_ACTION_IS_NEW); - action = isNew + action = QuickActionFactory.produceAction(isNew ? QuickActionFactory.newActionByType(type) - : quickActionRegistry.getQuickAction(actionId); + : quickActionRegistry.getQuickAction(actionId)); setupToolbar(view); setupHeader(view, savedInstanceState); setupFooter(view); - action.drawUI((ViewGroup) view.findViewById(R.id.container)); + action.drawUI((ViewGroup) view.findViewById(R.id.container), (MapActivity) getActivity()); } @Override @@ -175,13 +175,13 @@ public class CreateEditActionDialog extends DialogFragment { image.setImageResource(action.iconRes); } - private void setupFooter(View root){ + private void setupFooter(final View root){ root.findViewById(R.id.btnApply).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - action.fillParams(); + action.fillParams(((ViewGroup) root.findViewById(R.id.container)).getChildAt(0)); if (isNew) quickActionRegistry.addQuickAction(action); else quickActionRegistry.updateQuickAction(action); diff --git a/OsmAnd/src/net/osmand/plus/quickaction/QuickAction.java b/OsmAnd/src/net/osmand/plus/quickaction/QuickAction.java index 89cdeb04a1..3030118eeb 100644 --- a/OsmAnd/src/net/osmand/plus/quickaction/QuickAction.java +++ b/OsmAnd/src/net/osmand/plus/quickaction/QuickAction.java @@ -6,11 +6,18 @@ import android.os.Parcel; import android.os.Parcelable; import android.support.annotation.DrawableRes; import android.support.annotation.StringRes; +import android.view.View; import android.view.ViewGroup; +import com.google.gson.Gson; +import com.google.gson.annotations.Expose; +import com.google.gson.reflect.TypeToken; + import net.osmand.plus.activities.MapActivity; +import java.lang.reflect.Type; import java.util.HashMap; +import java.util.Map; public class QuickAction { @@ -62,6 +69,9 @@ public class QuickAction { } public HashMap getParams() { + + if (params == null) params = new HashMap<>(); + return params; } @@ -74,8 +84,8 @@ public class QuickAction { } public void execute(MapActivity activity){}; - public void drawUI(ViewGroup parent){}; - public void fillParams(){}; + public void drawUI(ViewGroup parent, MapActivity activity){}; + public void fillParams(View root){}; @Override diff --git a/OsmAnd/src/net/osmand/plus/quickaction/QuickActionFactory.java b/OsmAnd/src/net/osmand/plus/quickaction/QuickActionFactory.java index 703fa8ccc0..90edfecb80 100644 --- a/OsmAnd/src/net/osmand/plus/quickaction/QuickActionFactory.java +++ b/OsmAnd/src/net/osmand/plus/quickaction/QuickActionFactory.java @@ -1,21 +1,50 @@ package net.osmand.plus.quickaction; +import android.app.Activity; +import android.app.Dialog; +import android.app.ProgressDialog; +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.support.v4.app.DialogFragment; +import android.support.v7.widget.SwitchCompat; +import android.view.LayoutInflater; +import android.view.MotionEvent; +import android.view.View; import android.view.ViewGroup; +import android.view.Window; +import android.widget.EditText; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; +import net.osmand.AndroidUtils; +import net.osmand.data.FavouritePoint; +import net.osmand.data.LatLon; +import net.osmand.data.PointDescription; +import net.osmand.plus.FavouritesDbHelper; +import net.osmand.plus.GeocodingLookupService; +import net.osmand.plus.IconsCache; +import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.base.FavoriteImageDrawable; +import net.osmand.plus.dialogs.ProgressDialogFragment; +import net.osmand.plus.mapcontextmenu.editors.FavoritePointEditor; +import net.osmand.plus.mapcontextmenu.editors.SelectCategoryDialogFragment; +import net.osmand.plus.osmedit.OsmEditsUploadListener; +import net.osmand.plus.osmedit.OsmEditsUploadListenerHelper; +import net.osmand.plus.osmedit.OsmPoint; +import net.osmand.plus.widgets.AutoCompleteTextViewEx; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; +import java.util.Map; public class QuickActionFactory { - public String quickActionListToString(List quickActions){ + public String quickActionListToString(List quickActions) { String json = new Gson().toJson(quickActions); return json; @@ -23,7 +52,8 @@ public class QuickActionFactory { public List parseActiveActionsList(String json) { - Type type = new TypeToken>(){}.getType(); + Type type = new TypeToken>() { + }.getType(); ArrayList quickActions = new Gson().fromJson(json, type); return quickActions != null ? quickActions : new ArrayList(); @@ -39,31 +69,39 @@ public class QuickActionFactory { return quickActions; } - public static QuickAction newActionByType(int type){ + public static QuickAction newActionByType(int type) { - switch (type){ + switch (type) { - case NewAction.TYPE: return new NewAction(); + case NewAction.TYPE: + return new NewAction(); - case MarkerAction.TYPE: return new MarkerAction(); + case MarkerAction.TYPE: + return new MarkerAction(); - case FavoriteAction.TYPE: return new FavoriteAction(); + case FavoriteAction.TYPE: + return new FavoriteAction(); - default: return new QuickAction(); + default: + return new QuickAction(); } } - public static QuickAction produceAction(QuickAction quickAction){ + public static QuickAction produceAction(QuickAction quickAction) { - switch (quickAction.type){ + switch (quickAction.type) { - case NewAction.TYPE: return new NewAction(quickAction); + case NewAction.TYPE: + return new NewAction(quickAction); - case MarkerAction.TYPE: return new MarkerAction(quickAction); + case MarkerAction.TYPE: + return new MarkerAction(quickAction); - case FavoriteAction.TYPE: return new FavoriteAction(quickAction); + case FavoriteAction.TYPE: + return new FavoriteAction(quickAction); - default: return quickAction; + default: + return quickAction; } } @@ -71,7 +109,7 @@ public class QuickActionFactory { public static final int TYPE = 1; - protected NewAction(){ + protected NewAction() { id = System.currentTimeMillis(); type = TYPE; nameRes = R.string.quick_action_new_action; @@ -90,7 +128,7 @@ public class QuickActionFactory { } @Override - public void drawUI(ViewGroup parent) { + public void drawUI(ViewGroup parent, MapActivity activity) { //TODO inflate view & fill with params } @@ -100,7 +138,7 @@ public class QuickActionFactory { public static final int TYPE = 2; - private MarkerAction(){ + private MarkerAction() { id = System.currentTimeMillis(); type = TYPE; nameRes = R.string.quick_action_add_marker; @@ -114,13 +152,33 @@ public class QuickActionFactory { @Override public void execute(MapActivity activity) { - //TODO do some action + LatLon latLon = activity.getMapView() + .getCurrentRotatedTileBox() + .getCenterLatLon(); + + PointDescription pointDescription = new PointDescription( + latLon.getLatitude(), + latLon.getLongitude()); + + if (pointDescription.isLocation() && pointDescription.getName().equals(PointDescription.getAddressNotFoundStr(activity))) + pointDescription = new PointDescription(PointDescription.POINT_TYPE_LOCATION, ""); + + activity.getMapActions().addMapMarker( + latLon.getLatitude(), + latLon.getLongitude(), + pointDescription); } @Override - public void drawUI(ViewGroup parent) { + public void drawUI(ViewGroup parent, MapActivity activity) { - //TODO inflate view & fill with params + if (parent.getChildCount() == 0) { + + View view = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.quick_action_add_marker, parent, false); + + parent.addView(view); + } } } @@ -128,7 +186,10 @@ public class QuickActionFactory { public static final int TYPE = 3; - public FavoriteAction() { + public static final String KEY_NAME = "name"; + public static final String KEY_DIALOG = "dialog"; + + private FavoriteAction() { id = System.currentTimeMillis(); type = TYPE; nameRes = R.string.quick_action_add_favorite; @@ -140,15 +201,77 @@ public class QuickActionFactory { } @Override - public void execute(MapActivity activity) { + public void execute(final MapActivity activity) { - //TODO do some action + final LatLon latLon = activity.getMapView() + .getCurrentRotatedTileBox() + .getCenterLatLon(); + + final String title = getParams().get(KEY_NAME); + + if (title == null || title.isEmpty()) { + + final Dialog progressDialog = new ProgressDialog(activity); + progressDialog.setCancelable(false); + progressDialog.setTitle(R.string.search_address); + progressDialog.show(); + + GeocodingLookupService.AddressLookupRequest lookupRequest = new GeocodingLookupService.AddressLookupRequest(latLon, + + new GeocodingLookupService.OnAddressLookupResult() { + + @Override + public void geocodingDone(String address) { + + progressDialog.dismiss(); + activity.getContextMenu().getFavoritePointEditor().add(latLon, address, ""); + } + + }, null); + + activity.getMyApplication().getGeocodingLookupService().lookupAddress(lookupRequest); + + } else activity.getContextMenu().getFavoritePointEditor().add(latLon, title, ""); } @Override - public void drawUI(ViewGroup parent) { + public void drawUI(final ViewGroup parent, MapActivity activity) { - //TODO inflate view & fill with params + FavouritesDbHelper helper = activity.getMyApplication().getFavorites(); + + String category = helper.getFavoriteGroups().size() > 0 + ? helper.getFavoriteGroups().get(0).name + : activity.getString(R.string.shared_string_favorites); + + View root; + + if (parent.getChildCount() == 0) { + + root = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.quick_action_add_favorite, parent, false); + + parent.addView(root); + + } else root = parent.getChildAt(0); + + AutoCompleteTextViewEx categoryEdit = (AutoCompleteTextViewEx) + root.findViewById(R.id.category_edit); + + categoryEdit.setText(category); + categoryEdit.setFocusable(false); + + if (!getParams().isEmpty()){ + + ((EditText) root.findViewById(R.id.name_edit)).setText(getParams().get(KEY_NAME)); + ((SwitchCompat) root.findViewById(R.id.saveButton)).setChecked(Boolean.getBoolean(getParams().get(KEY_DIALOG))); + } + } + + @Override + public void fillParams(View root) { + + getParams().put(KEY_NAME, ((EditText) root.findViewById(R.id.name_edit)).getText().toString()); + getParams().put(KEY_DIALOG, Boolean.toString(((SwitchCompat) root.findViewById(R.id.saveButton)).isChecked())); } } } diff --git a/OsmAnd/src/net/osmand/plus/views/MapQuickActionLayer.java b/OsmAnd/src/net/osmand/plus/views/MapQuickActionLayer.java index 43bd23c641..b29710b30f 100644 --- a/OsmAnd/src/net/osmand/plus/views/MapQuickActionLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/MapQuickActionLayer.java @@ -18,6 +18,7 @@ import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.mapcontextmenu.MapContextMenu; import net.osmand.plus.quickaction.QuickAction; +import net.osmand.plus.quickaction.QuickActionFactory; import net.osmand.plus.quickaction.QuickActionRegistry; import net.osmand.plus.quickaction.QuickActionsWidget; @@ -278,7 +279,7 @@ public class MapQuickActionLayer extends OsmandMapLayer implements QuickActionRe @Override public void onActionSelected(QuickAction action) { - action.execute(mapActivity); + QuickActionFactory.produceAction(action).execute(mapActivity); } public PointF getMovableCenterPoint(RotatedTileBox tb) {