diff --git a/android/res/layout/item_discovery_search.xml b/android/res/layout/item_discovery_search.xml index 9717b18a11..8fa91904d6 100644 --- a/android/res/layout/item_discovery_search.xml +++ b/android/res/layout/item_discovery_search.xml @@ -11,49 +11,54 @@ android:layout_height="wrap_content"> - + - - + android:paddingBottom="@dimen/margin_half_double_plus" + android:foreground="?clickableBackground" + android:orientation="vertical" + android:layout_width="match_parent" + android:layout_height="wrap_content"> + + + + mTasks = new Stack<>(); @@ -212,7 +214,7 @@ public class MwmActivity extends BaseMwmFragmentActivity if (!PermissionsUtils.isLocationGranted()) { if (PermissionsUtils.isLocationExplanationNeeded(MwmActivity.this)) - PermissionsUtils.requestLocationPermission(MwmActivity.this, LOCATION_REQUEST); + PermissionsUtils.requestLocationPermission(MwmActivity.this, REQ_CODE_LOCATION_PERMISSION); else Toast.makeText(MwmActivity.this, R.string.enable_location_services, Toast.LENGTH_SHORT) .show(); @@ -846,7 +848,8 @@ public class MwmActivity extends BaseMwmFragmentActivity break; case DISCOVERY: - DiscoveryActivity.start(MwmActivity.this); + Intent i = new Intent(MwmActivity.this, DiscoveryActivity.class); + startActivityForResult(i, REQ_CODE_DISCOVERY); break; case BOOKMARKS: @@ -1027,12 +1030,47 @@ public class MwmActivity extends BaseMwmFragmentActivity mFilterController.onRestoreState(savedInstanceState); } + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) + { + super.onActivityResult(requestCode, resultCode, data); + + if (resultCode != Activity.RESULT_OK) + return; + + switch (requestCode) + { + case REQ_CODE_DISCOVERY: + handleDiscoveryResult(data); + break; + } + } + + private void handleDiscoveryResult(@NonNull Intent data) + { + final MapObject destination = data.getParcelableExtra(DiscoveryActivity + .EXTRA_DISCOVERY_OBJECT); + if (destination == null) + return; + + addTask(new MapTask() + { + @Override + public boolean run(MwmActivity target) + { + RoutingController.get().setRouterType(Framework.ROUTER_TYPE_PEDESTRIAN); + RoutingController.get().prepare(true, destination); + return false; + } + }); + } + @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); - if (requestCode != LOCATION_REQUEST || grantResults.length == 0) + if (requestCode != REQ_CODE_LOCATION_PERMISSION || grantResults.length == 0) return; PermissionsResult result = PermissionsUtils.computePermissionsResult(permissions, grantResults); diff --git a/android/src/com/mapswithme/maps/discovery/DiscoveryActivity.java b/android/src/com/mapswithme/maps/discovery/DiscoveryActivity.java index e011f51cd4..d1df701519 100644 --- a/android/src/com/mapswithme/maps/discovery/DiscoveryActivity.java +++ b/android/src/com/mapswithme/maps/discovery/DiscoveryActivity.java @@ -1,19 +1,12 @@ package com.mapswithme.maps.discovery; -import android.app.Activity; -import android.content.Intent; -import android.support.annotation.NonNull; import android.support.v4.app.Fragment; import com.mapswithme.maps.base.BaseMwmFragmentActivity; public class DiscoveryActivity extends BaseMwmFragmentActivity { - public static void start(@NonNull Activity activity) - { - final Intent i = new Intent(activity, DiscoveryActivity.class); - activity.startActivity(i); - } + public static final String EXTRA_DISCOVERY_OBJECT = "extra_discovery_object"; @Override protected Class getFragmentClass() diff --git a/android/src/com/mapswithme/maps/discovery/DiscoveryFragment.java b/android/src/com/mapswithme/maps/discovery/DiscoveryFragment.java index b333ae73a6..ac60cd05a1 100644 --- a/android/src/com/mapswithme/maps/discovery/DiscoveryFragment.java +++ b/android/src/com/mapswithme/maps/discovery/DiscoveryFragment.java @@ -1,6 +1,8 @@ package com.mapswithme.maps.discovery; +import android.app.Activity; import android.content.Context; +import android.content.Intent; import android.os.Bundle; import android.support.annotation.IdRes; import android.support.annotation.MainThread; @@ -15,7 +17,11 @@ import android.view.ViewGroup; import com.mapswithme.maps.R; import com.mapswithme.maps.base.BaseMwmToolbarFragment; -import com.mapswithme.maps.gallery.GalleryAdapter; +import com.mapswithme.maps.bookmarks.data.FeatureId; +import com.mapswithme.maps.bookmarks.data.MapObject; +import com.mapswithme.maps.gallery.ItemSelectedListener; +import com.mapswithme.maps.gallery.Items; +import com.mapswithme.maps.gallery.impl.BaseItemSelectedListener; import com.mapswithme.maps.gallery.impl.Factory; import com.mapswithme.maps.search.SearchResult; import com.mapswithme.maps.viator.ViatorProduct; @@ -32,7 +38,8 @@ public class DiscoveryFragment extends BaseMwmToolbarFragment implements UICallb private static final int[] ITEM_TYPES = { DiscoveryParams.ITEM_TYPE_VIATOR, DiscoveryParams.ITEM_TYPE_ATTRACTIONS, DiscoveryParams.ITEM_TYPE_CAFES }; - private static final GalleryAdapter.ItemSelectedListener LISTENER = new BaseItemSelectedListener(); + @Nullable + private BaseItemSelectedListener mDefaultListener; private boolean mOnlineMode; @Nullable @@ -99,6 +106,7 @@ public class DiscoveryFragment extends BaseMwmToolbarFragment implements UICallb { super.onViewCreated(view, savedInstanceState); mToolbarController.setTitle(R.string.discovery_button_title); + mDefaultListener = new BaseItemSelectedListener<>(getActivity()); view.findViewById(R.id.viatorLogo).setOnClickListener(new View.OnClickListener() { @Override @@ -142,7 +150,7 @@ public class DiscoveryFragment extends BaseMwmToolbarFragment implements UICallb // TODO: set loading adapter for local experts here. RecyclerView thinsToDo = getGallery(R.id.thingsToDo); thinsToDo.setAdapter(Factory.createViatorLoadingAdapter(DiscoveryManager.nativeGetViatorUrl(), - LISTENER)); + mDefaultListener)); return; } @@ -157,7 +165,8 @@ public class DiscoveryFragment extends BaseMwmToolbarFragment implements UICallb { UiUtils.show(getView(), R.id.thingsToDoLayout, R.id.thingsToDo); RecyclerView thinsToDo = getGallery(R.id.thingsToDo); - thinsToDo.setAdapter(Factory.createViatorOfflineAdapter(new ViatorOfflineSelectedListener())); + thinsToDo.setAdapter(Factory.createViatorOfflineAdapter(new ViatorOfflineSelectedListener + (getActivity()))); } } @@ -185,7 +194,8 @@ public class DiscoveryFragment extends BaseMwmToolbarFragment implements UICallb if (results == null) return; - getGallery(R.id.attractions).setAdapter(Factory.createSearchBasedAdapter(results, LISTENER)); + ItemSelectedListener listener = new SearchBasedListener(getActivity()); + getGallery(R.id.attractions).setAdapter(Factory.createSearchBasedAdapter(results, listener)); } @MainThread @@ -195,7 +205,8 @@ public class DiscoveryFragment extends BaseMwmToolbarFragment implements UICallb if (results == null) return; - getGallery(R.id.food).setAdapter(Factory.createSearchBasedAdapter(results, LISTENER)); + ItemSelectedListener listener = new SearchBasedListener(getActivity()); + getGallery(R.id.food).setAdapter(Factory.createSearchBasedAdapter(results, listener)); } @MainThread @@ -206,7 +217,8 @@ public class DiscoveryFragment extends BaseMwmToolbarFragment implements UICallb return; String url = DiscoveryManager.nativeGetViatorUrl(); - getGallery(R.id.thingsToDo).setAdapter(Factory.createViatorAdapter(products, url, LISTENER)); + ItemSelectedListener listener = new BaseItemSelectedListener<>(getActivity()); + getGallery(R.id.thingsToDo).setAdapter(Factory.createViatorAdapter(products, url, listener)); } @MainThread @@ -223,7 +235,7 @@ public class DiscoveryFragment extends BaseMwmToolbarFragment implements UICallb { case VIATOR: String url = DiscoveryManager.nativeGetViatorUrl(); - getGallery(R.id.thingsToDo).setAdapter(Factory.createViatorErrorAdapter(url, LISTENER)); + getGallery(R.id.thingsToDo).setAdapter(Factory.createViatorErrorAdapter(url, mDefaultListener)); break; case ATTRACTIONS: getGallery(R.id.attractions).setAdapter(Factory.createSearchBasedErrorAdapter()); @@ -239,37 +251,45 @@ public class DiscoveryFragment extends BaseMwmToolbarFragment implements UICallb } } - private static class BaseItemSelectedListener implements GalleryAdapter.ItemSelectedListener + private static class ViatorOfflineSelectedListener extends BaseItemSelectedListener { - - @Override - public void onItemSelected(@NonNull Context context, @NonNull String url) + private ViatorOfflineSelectedListener(@NonNull Activity context) { - Utils.openUrl(context, url); + super(context); } @Override - public void onMoreItemSelected(@NonNull Context context, @NonNull String url) + public void onActionButtonSelected(@NonNull Items.Item item) { - Utils.openUrl(context, url); - } - - @Override - public void onDetailsSelected(@NonNull Context context, @Nullable String url) - { - if (TextUtils.isEmpty(url)) - return; - - Utils.openUrl(context, url); + Utils.showWirelessSettings(getContext()); } } - private static class ViatorOfflineSelectedListener extends BaseItemSelectedListener + private static class SearchBasedListener extends BaseItemSelectedListener { - @Override - public void onDetailsSelected(@NonNull Context context, @Nullable String url) + private SearchBasedListener(@NonNull Activity context) { - Utils.showWirelessSettings(context); + super(context); + + } + + @Override + public void onItemSelected(@NonNull Items.SearchItem item) + { + // TODO: Show point on the map. Coming soon. + } + + @Override + public void onActionButtonSelected(@NonNull Items.SearchItem item) + { + Intent intent = new Intent(); + String title = TextUtils.isEmpty(item.getTitle()) ? "" : item.getTitle(); + String subtitle = TextUtils.isEmpty(item.getSubtitle()) ? "" : item.getSubtitle(); + MapObject poi = MapObject.createMapObject(FeatureId.EMPTY, MapObject.SEARCH, title, subtitle, + item.getLat(), item.getLon()); + intent.putExtra(DiscoveryActivity.EXTRA_DISCOVERY_OBJECT, poi); + getContext().setResult(Activity.RESULT_OK, intent); + getContext().finish(); } } } diff --git a/android/src/com/mapswithme/maps/gallery/AdapterStrategy.java b/android/src/com/mapswithme/maps/gallery/AdapterStrategy.java index b15d5665c6..8b1afeeb87 100644 --- a/android/src/com/mapswithme/maps/gallery/AdapterStrategy.java +++ b/android/src/com/mapswithme/maps/gallery/AdapterStrategy.java @@ -12,7 +12,8 @@ public abstract class AdapterStrategy, I ex protected final List mItems = new ArrayList<>(); @NonNull - abstract VH createViewHolder(@NonNull ViewGroup parent, int viewType, @NonNull GalleryAdapter adapter); + abstract VH createViewHolder(@NonNull ViewGroup parent, int viewType, + @NonNull GalleryAdapter adapter); protected abstract void onBindViewHolder(Holders.BaseViewHolder holder, int position); diff --git a/android/src/com/mapswithme/maps/gallery/GalleryAdapter.java b/android/src/com/mapswithme/maps/gallery/GalleryAdapter.java index 77dec2672b..ddd9c6b7b1 100644 --- a/android/src/com/mapswithme/maps/gallery/GalleryAdapter.java +++ b/android/src/com/mapswithme/maps/gallery/GalleryAdapter.java @@ -1,6 +1,5 @@ package com.mapswithme.maps.gallery; -import android.content.Context; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.widget.RecyclerView; @@ -12,20 +11,21 @@ public class GalleryAdapter, I extends Item @NonNull private final AdapterStrategy mStrategy; @Nullable - private ItemSelectedListener mListener; - @Nullable - public ItemSelectedListener getListener() - { - return mListener; - } + private final ItemSelectedListener mListener; public GalleryAdapter(@NonNull AdapterStrategy strategy, - @Nullable ItemSelectedListener listener) + @Nullable ItemSelectedListener listener) { mStrategy = strategy; mListener = listener; } + @Nullable + public ItemSelectedListener getListener() + { + return mListener; + } + @Override public VH onCreateViewHolder(ViewGroup parent, int viewType) { @@ -49,11 +49,4 @@ public class GalleryAdapter, I extends Item { return mStrategy.getItemViewType(position); } - - public interface ItemSelectedListener - { - void onItemSelected(@NonNull Context context, @NonNull String url); - void onMoreItemSelected(@NonNull Context context, @NonNull String url); - void onDetailsSelected(@NonNull Context context, @Nullable String url); - } } diff --git a/android/src/com/mapswithme/maps/gallery/Holders.java b/android/src/com/mapswithme/maps/gallery/Holders.java index be35e7066c..e272cb1968 100644 --- a/android/src/com/mapswithme/maps/gallery/Holders.java +++ b/android/src/com/mapswithme/maps/gallery/Holders.java @@ -40,7 +40,7 @@ public class Holders Context mContext; public ViatorProductViewHolder(@NonNull View itemView, @NonNull List items, - @NonNull GalleryAdapter adapter) + @NonNull GalleryAdapter adapter) { super(itemView, items, adapter); mContext = itemView.getContext(); @@ -72,13 +72,13 @@ public class Holders } @Override - protected void onItemSelected(@NonNull Context context, @NonNull ViatorItem item) + protected void onItemSelected(@NonNull ViatorItem item) { - GalleryAdapter.ItemSelectedListener listener = mAdapter.getListener(); + ItemSelectedListener listener = mAdapter.getListener(); if (listener == null || TextUtils.isEmpty(item.getUrl())) return; - listener.onItemSelected(context, item.getUrl()); + listener.onItemSelected(item); } } @@ -86,19 +86,19 @@ public class Holders { public ViatorMoreItemViewHolder(@NonNull View itemView, @NonNull List items, - @NonNull GalleryAdapter adapter) + @NonNull GalleryAdapter adapter) { super(itemView, items, adapter); } @Override - protected void onItemSelected(@NonNull Context context, @NonNull Items.ViatorItem item) + protected void onItemSelected(@NonNull Items.ViatorItem item) { - GalleryAdapter.ItemSelectedListener listener = mAdapter.getListener(); + ItemSelectedListener listener = mAdapter.getListener(); if (listener == null || TextUtils.isEmpty(item.getUrl())) return; - listener.onMoreItemSelected(context, item.getUrl()); + listener.onMoreItemSelected(item); } } @@ -109,7 +109,8 @@ public class Holders @NonNull TextView mAddress; - public CianProductViewHolder(@NonNull View itemView, @NonNull List items, @NonNull GalleryAdapter adapter) + public CianProductViewHolder(@NonNull View itemView, @NonNull List items, + @NonNull GalleryAdapter adapter) { super(itemView, items, adapter); mPrice = (TextView) itemView.findViewById(R.id.tv__price); @@ -129,19 +130,19 @@ public class Holders { public CianMoreItemViewHolder(@NonNull View itemView, @NonNull List items, - @NonNull GalleryAdapter adapter) + @NonNull GalleryAdapter adapter) { super(itemView, items, adapter); } @Override - protected void onItemSelected(@NonNull Context context, @NonNull T item) + protected void onItemSelected(@NonNull T item) { - GalleryAdapter.ItemSelectedListener listener = mAdapter.getListener(); + ItemSelectedListener listener = mAdapter.getListener(); if (listener == null || TextUtils.isEmpty(item.getUrl())) return; - listener.onMoreItemSelected(context, item.getUrl()); + listener.onMoreItemSelected(item); } } @@ -155,13 +156,15 @@ public class Holders private final TextView mButton; public SearchViewHolder(@NonNull View itemView, @NonNull List items, - @NonNull GalleryAdapter adapter) + @NonNull GalleryAdapter adapter) { super(itemView, items, adapter); mTitle = (TextView) itemView.findViewById(R.id.title); mSubtitle = (TextView) itemView.findViewById(R.id.subtitle); mDistance = (TextView) itemView.findViewById(R.id.distance); mButton = (TextView) itemView.findViewById(R.id.button); + mButton.setOnClickListener(this); + itemView.findViewById(R.id.infoLayout).setOnClickListener(this); mButton.setText(R.string.p2p_to_here); } @@ -173,6 +176,29 @@ public class Holders UiUtils.setTextAndHideIfEmpty(mSubtitle, item.getSubtitle()); UiUtils.setTextAndHideIfEmpty(mDistance, item.getDistance()); } + + @Override + public void onClick(View v) + { + int position = getAdapterPosition(); + if (position == RecyclerView.NO_POSITION || mItems.isEmpty()) + return; + + ItemSelectedListener listener = mAdapter.getListener(); + if (listener == null) + return; + + Items.SearchItem item = mItems.get(position); + switch (v.getId()) + { + case R.id.infoLayout: + listener.onItemSelected(item); + break; + case R.id.button: + listener.onActionButtonSelected(item); + break; + } + } } public static class BaseViewHolder extends RecyclerView.ViewHolder @@ -183,10 +209,10 @@ public class Holders @NonNull protected List mItems; @NonNull - GalleryAdapter mAdapter; + GalleryAdapter mAdapter; BaseViewHolder(@NonNull View itemView, @NonNull List items, - @NonNull GalleryAdapter adapter) + @NonNull GalleryAdapter adapter) { super(itemView); mTitle = (TextView) itemView.findViewById(R.id.tv__title); @@ -208,10 +234,10 @@ public class Holders if (position == RecyclerView.NO_POSITION || mItems.isEmpty()) return; - onItemSelected(mTitle.getContext(), mItems.get(position)); + onItemSelected(mItems.get(position)); } - protected void onItemSelected(@NonNull Context context, @NonNull I item) + protected void onItemSelected(@NonNull I item) { } } @@ -226,7 +252,8 @@ public class Holders @NonNull TextView mMore; - LoadingViewHolder(@NonNull View itemView, @NonNull List items, @NonNull GalleryAdapter adapter) + LoadingViewHolder(@NonNull View itemView, @NonNull List items, + @NonNull GalleryAdapter adapter) { super(itemView, items, adapter); mProgressBar = (ProgressBar) itemView.findViewById(R.id.pb__progress); @@ -249,23 +276,23 @@ public class Holders if (position == RecyclerView.NO_POSITION) return; - onItemSelected(mProgressBar.getContext(), mItems.get(position)); + onItemSelected(mItems.get(position)); } @Override - protected void onItemSelected(@NonNull Context context, @NonNull Items.Item item) + protected void onItemSelected(@NonNull Items.Item item) { if (mAdapter.getListener() == null || TextUtils.isEmpty(item.getUrl())) return; - mAdapter.getListener().onDetailsSelected(context, item.getUrl()); + mAdapter.getListener().onActionButtonSelected(item); } } public static class SimpleViewHolder extends BaseViewHolder { public SimpleViewHolder(@NonNull View itemView, @NonNull List items, - @NonNull GalleryAdapter adapter) + @NonNull GalleryAdapter adapter) { super(itemView, items, adapter); mTitle = (TextView) itemView.findViewById(R.id.message); @@ -275,7 +302,8 @@ public class Holders static class ErrorViewHolder extends LoadingViewHolder { - ErrorViewHolder(@NonNull View itemView, @NonNull List items, @NonNull GalleryAdapter adapter) + ErrorViewHolder(@NonNull View itemView, @NonNull List items, + @NonNull GalleryAdapter adapter) { super(itemView, items, adapter); UiUtils.hide(mProgressBar); @@ -285,7 +313,7 @@ public class Holders public static class OfflineViewHolder extends LoadingViewHolder { OfflineViewHolder(@NonNull View itemView, @NonNull List items, - @NonNull GalleryAdapter adapter) + @NonNull GalleryAdapter adapter) { super(itemView, items, adapter); UiUtils.hide(mProgressBar); @@ -300,12 +328,12 @@ public class Holders } @Override - protected void onItemSelected(@NonNull Context context, @NonNull Items.Item item) + protected void onItemSelected(@NonNull Items.Item item) { if (mAdapter.getListener() == null) return; - mAdapter.getListener().onDetailsSelected(mTitle.getContext(), item.getUrl()); + mAdapter.getListener().onActionButtonSelected(item); } } } diff --git a/android/src/com/mapswithme/maps/gallery/ItemSelectedListener.java b/android/src/com/mapswithme/maps/gallery/ItemSelectedListener.java new file mode 100644 index 0000000000..aa50d9eb3c --- /dev/null +++ b/android/src/com/mapswithme/maps/gallery/ItemSelectedListener.java @@ -0,0 +1,12 @@ +package com.mapswithme.maps.gallery; + +import android.support.annotation.NonNull; + +public interface ItemSelectedListener +{ + void onItemSelected(@NonNull I item); + + void onMoreItemSelected(@NonNull I item); + + void onActionButtonSelected(@NonNull I item); +} diff --git a/android/src/com/mapswithme/maps/gallery/Items.java b/android/src/com/mapswithme/maps/gallery/Items.java index fe87b04a59..234bc9fe19 100644 --- a/android/src/com/mapswithme/maps/gallery/Items.java +++ b/android/src/com/mapswithme/maps/gallery/Items.java @@ -5,6 +5,7 @@ import android.support.annotation.Nullable; import com.mapswithme.maps.MwmApplication; import com.mapswithme.maps.R; +import com.mapswithme.maps.search.SearchResult; import static com.mapswithme.maps.gallery.Constants.TYPE_MORE; import static com.mapswithme.maps.gallery.Constants.TYPE_PRODUCT; @@ -77,19 +78,29 @@ public class Items public static class SearchItem extends RegularAdapterStrategy.Item { @NonNull - private final String mDistance; + private final SearchResult mResult; - public SearchItem(@NonNull String title, @Nullable String url, - @Nullable String subtitle, @NonNull String distance) + public SearchItem(@NonNull SearchResult result) { - super(TYPE_PRODUCT, title, url, subtitle); - mDistance = distance; + super(TYPE_PRODUCT, result.name, null, result.description.featureType); + mResult = result; } @NonNull public String getDistance() { - return mDistance; + SearchResult.Description d = mResult.description; + return d != null ? d.distance : ""; + } + + public double getLat() + { + return mResult.lat; + } + + public double getLon() + { + return mResult.lon; } } diff --git a/android/src/com/mapswithme/maps/gallery/RegularAdapterStrategy.java b/android/src/com/mapswithme/maps/gallery/RegularAdapterStrategy.java index f1d074c567..9f2ddc2881 100644 --- a/android/src/com/mapswithme/maps/gallery/RegularAdapterStrategy.java +++ b/android/src/com/mapswithme/maps/gallery/RegularAdapterStrategy.java @@ -30,7 +30,7 @@ public abstract class RegularAdapterStrategy createViewHolder(@NonNull ViewGroup parent, int viewType, - @NonNull GalleryAdapter adapter) + @NonNull GalleryAdapter adapter) { switch (viewType) { @@ -59,11 +59,11 @@ public abstract class RegularAdapterStrategy createProductViewHodler(@NonNull ViewGroup parent, int viewType, - @NonNull GalleryAdapter adapter); + @NonNull GalleryAdapter adapter); @NonNull protected abstract Holders.BaseViewHolder createMoreProductsViewHolder(@NonNull ViewGroup parent, int viewType, - @NonNull GalleryAdapter adapter); + @NonNull GalleryAdapter adapter); public static class Item extends Items.Item { diff --git a/android/src/com/mapswithme/maps/gallery/impl/BaseItemSelectedListener.java b/android/src/com/mapswithme/maps/gallery/impl/BaseItemSelectedListener.java new file mode 100644 index 0000000000..fd5c4701d5 --- /dev/null +++ b/android/src/com/mapswithme/maps/gallery/impl/BaseItemSelectedListener.java @@ -0,0 +1,44 @@ +package com.mapswithme.maps.gallery.impl; + +import android.app.Activity; +import android.support.annotation.NonNull; + +import com.mapswithme.maps.gallery.ItemSelectedListener; +import com.mapswithme.maps.gallery.Items; +import com.mapswithme.util.Utils; + +public class BaseItemSelectedListener + implements ItemSelectedListener +{ + @NonNull + private final Activity mContext; + + public BaseItemSelectedListener(@NonNull Activity context) + { + mContext = context; + } + + @NonNull + protected Activity getContext() + { + return mContext; + } + + @Override + public void onItemSelected(@NonNull I item) + { + Utils.openUrl(mContext, item.getUrl()); + } + + @Override + public void onMoreItemSelected(@NonNull I item) + { + Utils.openUrl(mContext, item.getUrl()); + } + + @Override + public void onActionButtonSelected(@NonNull I item) + { + Utils.openUrl(mContext, item.getUrl()); + } +} diff --git a/android/src/com/mapswithme/maps/gallery/impl/CianAdapterStrategy.java b/android/src/com/mapswithme/maps/gallery/impl/CianAdapterStrategy.java index 5523fee046..0c00466120 100644 --- a/android/src/com/mapswithme/maps/gallery/impl/CianAdapterStrategy.java +++ b/android/src/com/mapswithme/maps/gallery/impl/CianAdapterStrategy.java @@ -35,9 +35,8 @@ class CianAdapterStrategy extends RegularAdapterStrategy @NonNull @Override - protected Holders.BaseViewHolder createProductViewHodler(@NonNull ViewGroup parent, - int viewType, - @NonNull GalleryAdapter adapter) + protected Holders.BaseViewHolder createProductViewHodler + (@NonNull ViewGroup parent, int viewType, @NonNull GalleryAdapter adapter) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cian_product, parent, false); @@ -46,9 +45,8 @@ class CianAdapterStrategy extends RegularAdapterStrategy @NonNull @Override - protected Holders.BaseViewHolder createMoreProductsViewHolder(@NonNull ViewGroup parent, - int viewType, - @NonNull GalleryAdapter adapter) + protected Holders.BaseViewHolder createMoreProductsViewHolder + (@NonNull ViewGroup parent, int viewType, @NonNull GalleryAdapter adapter) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cian_more, parent, false); diff --git a/android/src/com/mapswithme/maps/gallery/impl/Factory.java b/android/src/com/mapswithme/maps/gallery/impl/Factory.java index 4136a203a9..1eb7db89ce 100644 --- a/android/src/com/mapswithme/maps/gallery/impl/Factory.java +++ b/android/src/com/mapswithme/maps/gallery/impl/Factory.java @@ -5,6 +5,8 @@ import android.support.annotation.Nullable; import com.mapswithme.maps.cian.RentPlace; import com.mapswithme.maps.gallery.GalleryAdapter; +import com.mapswithme.maps.gallery.ItemSelectedListener; +import com.mapswithme.maps.gallery.Items; import com.mapswithme.maps.search.SearchResult; import com.mapswithme.maps.viator.ViatorProduct; @@ -12,34 +14,38 @@ public class Factory { @NonNull public static GalleryAdapter createViatorLoadingAdapter(@Nullable String cityUrl, - @Nullable GalleryAdapter.ItemSelectedListener listener) + @Nullable ItemSelectedListener + listener) { return new GalleryAdapter<>(new ViatorLoadingAdapterStrategy(cityUrl), listener); } @NonNull - public static GalleryAdapter createViatorOfflineAdapter(@Nullable GalleryAdapter.ItemSelectedListener listener) + public static GalleryAdapter createViatorOfflineAdapter(@Nullable ItemSelectedListener listener) { return new GalleryAdapter<>(new ViatorOfflineAdapterStrategy(null), listener); } @NonNull public static GalleryAdapter createViatorErrorAdapter(@Nullable String url, - @Nullable GalleryAdapter.ItemSelectedListener listener) + @Nullable ItemSelectedListener + listener) { return new GalleryAdapter<>(new ViatorErrorAdapterStrategy(url), listener); } @NonNull public static GalleryAdapter createCianLoadingAdapter(@Nullable String url, - @Nullable GalleryAdapter.ItemSelectedListener listener) + @Nullable ItemSelectedListener + listener) { return new GalleryAdapter<>(new CianLoadingAdapterStrategy(url), listener); } @NonNull public static GalleryAdapter createCianErrorAdapter(@Nullable String url, - @Nullable GalleryAdapter.ItemSelectedListener listener) + @Nullable ItemSelectedListener + listener) { return new GalleryAdapter<>(new CianErrorAdapterStrategy(url), listener); } @@ -47,21 +53,23 @@ public class Factory @NonNull public static GalleryAdapter createViatorAdapter(@NonNull ViatorProduct[] products, @Nullable String cityUrl, - @Nullable GalleryAdapter.ItemSelectedListener listener) + @Nullable ItemSelectedListener + listener) { return new GalleryAdapter<>(new ViatorAdapterStrategy(products, cityUrl), listener); } @NonNull public static GalleryAdapter createCianAdapter(@NonNull RentPlace[] products, @NonNull String url, - @Nullable GalleryAdapter.ItemSelectedListener listener) + @Nullable ItemSelectedListener listener) { return new GalleryAdapter<>(new CianAdapterStrategy(products, url), listener); } @NonNull public static GalleryAdapter createSearchBasedAdapter(@NonNull SearchResult[] results, - @Nullable GalleryAdapter.ItemSelectedListener listener) + @Nullable ItemSelectedListener listener) { return new GalleryAdapter<>(new SearchBasedAdapterStrategy(results), listener); } diff --git a/android/src/com/mapswithme/maps/gallery/impl/SearchBasedAdapterStrategy.java b/android/src/com/mapswithme/maps/gallery/impl/SearchBasedAdapterStrategy.java index 01427f4b07..e84e45e90b 100644 --- a/android/src/com/mapswithme/maps/gallery/impl/SearchBasedAdapterStrategy.java +++ b/android/src/com/mapswithme/maps/gallery/impl/SearchBasedAdapterStrategy.java @@ -31,9 +31,8 @@ class SearchBasedAdapterStrategy extends RegularAdapterStrategy createProductViewHodler(@NonNull ViewGroup parent, - int viewType, - @NonNull GalleryAdapter adapter) + protected Holders.BaseViewHolder createProductViewHodler + (@NonNull ViewGroup parent, int viewType, @NonNull GalleryAdapter adapter) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_discovery_search, parent, false); @@ -42,9 +41,8 @@ class SearchBasedAdapterStrategy extends RegularAdapterStrategy createMoreProductsViewHolder(@NonNull ViewGroup parent, - int viewType, - @NonNull GalleryAdapter adapter) + protected final Holders.BaseViewHolder createMoreProductsViewHolder + (@NonNull ViewGroup parent, int viewType, @NonNull GalleryAdapter adapter) { throw new UnsupportedOperationException("More item is not supported yet for this strategy!"); } @@ -54,10 +52,7 @@ class SearchBasedAdapterStrategy extends RegularAdapterStrategy viewItems = new ArrayList<>(); for (SearchResult result : results) - { - SearchResult.Description d = result.description; - viewItems.add(new Items.SearchItem(result.name, null, d.featureType, d.distance)); - } + viewItems.add(new Items.SearchItem(result)); return viewItems; } } diff --git a/android/src/com/mapswithme/maps/gallery/impl/ViatorAdapterStrategy.java b/android/src/com/mapswithme/maps/gallery/impl/ViatorAdapterStrategy.java index 26dafeeecf..3c0c49e58b 100644 --- a/android/src/com/mapswithme/maps/gallery/impl/ViatorAdapterStrategy.java +++ b/android/src/com/mapswithme/maps/gallery/impl/ViatorAdapterStrategy.java @@ -33,9 +33,8 @@ public class ViatorAdapterStrategy @NonNull @Override - protected Holders.BaseViewHolder createProductViewHodler(@NonNull ViewGroup parent, - int viewType, - @NonNull GalleryAdapter adapter) + protected Holders.BaseViewHolder createProductViewHodler + (@NonNull ViewGroup parent, int viewType, @NonNull GalleryAdapter adapter) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_viator_product, parent, @@ -45,8 +44,8 @@ public class ViatorAdapterStrategy @NonNull @Override - protected Holders.BaseViewHolder createMoreProductsViewHolder( - @NonNull ViewGroup parent, int viewType, @NonNull GalleryAdapter adapter) + protected Holders.BaseViewHolder createMoreProductsViewHolder + (@NonNull ViewGroup parent, int viewType, @NonNull GalleryAdapter adapter) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_viator_more, parent, false); diff --git a/android/src/com/mapswithme/maps/routing/RoutingController.java b/android/src/com/mapswithme/maps/routing/RoutingController.java index fd5333287f..e23f7e512a 100644 --- a/android/src/com/mapswithme/maps/routing/RoutingController.java +++ b/android/src/com/mapswithme/maps/routing/RoutingController.java @@ -409,7 +409,7 @@ public class RoutingController implements TaxiManager.TaxiListener prepare(canUseMyPositionAsStart, endPoint, false); } - public void prepare(boolean canUseMyPositionAsStart, @Nullable MapObject endPoint, boolean fromApi) + public void prepare(boolean canUseMyPositionAsStart, @Nullable MapObject endPoint, boolean fromApi) { MapObject startPoint = canUseMyPositionAsStart ? LocationHelper.INSTANCE.getMyPosition() : null; prepare(startPoint, endPoint, fromApi); diff --git a/android/src/com/mapswithme/maps/widget/placepage/PlacePageView.java b/android/src/com/mapswithme/maps/widget/placepage/PlacePageView.java index 7b3f92d1b9..eaf0f82335 100644 --- a/android/src/com/mapswithme/maps/widget/placepage/PlacePageView.java +++ b/android/src/com/mapswithme/maps/widget/placepage/PlacePageView.java @@ -1,5 +1,6 @@ package com.mapswithme.maps.widget.placepage; +import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Context; import android.content.res.Resources; @@ -63,6 +64,9 @@ import com.mapswithme.maps.editor.data.Timetable; import com.mapswithme.maps.gallery.FullScreenGalleryActivity; import com.mapswithme.maps.gallery.GalleryActivity; import com.mapswithme.maps.gallery.Image; +import com.mapswithme.maps.gallery.ItemSelectedListener; +import com.mapswithme.maps.gallery.Items; +import com.mapswithme.maps.gallery.impl.BaseItemSelectedListener; import com.mapswithme.maps.gallery.impl.Factory; import com.mapswithme.maps.location.LocationHelper; import com.mapswithme.maps.review.Review; @@ -121,7 +125,6 @@ public class PlacePageView extends RelativeLayout EditBookmarkFragment.EditBookmarkListener, BannerController.BannerListener, Viator.ViatorListener, - com.mapswithme.maps.gallery.GalleryAdapter.ItemSelectedListener, Cian.CianListener { private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC); @@ -226,6 +229,8 @@ public class PlacePageView extends RelativeLayout private final NearbyAdapter mNearbyAdapter = new NearbyAdapter(this); @NonNull private final ReviewAdapter mReviewAdapter = new ReviewAdapter(); + @NonNull + private final ItemSelectedListener mDefaultGalleryItemListener; // Downloader`s stuff private DownloaderStatusIcon mDownloaderIcon; @@ -294,7 +299,7 @@ public class PlacePageView extends RelativeLayout public PlacePageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs); - + mDefaultGalleryItemListener = new SponsoredItemSelectedListener<>((Activity) context); mIsLatLonDms = MwmApplication.prefs().getBoolean(PREF_USE_DMS, false); mGalleryAdapter = new com.mapswithme.maps.widget.placepage.GalleryAdapter(context); mMarginBase = (int) getResources().getDimension(R.dimen.margin_base); @@ -834,7 +839,7 @@ public class PlacePageView extends RelativeLayout public void onErrorReceived(int errorCode) { String url = mSponsored != null ? mSponsored.getUrl() : ""; - mRvSponsoredProducts.setAdapter(Factory.createCianErrorAdapter(url, this)); + mRvSponsoredProducts.setAdapter(Factory.createCianErrorAdapter(url, mDefaultGalleryItemListener)); Statistics.INSTANCE.trackSponsoredGalleryError(Sponsored.TYPE_CIAN, String.valueOf(errorCode)); } @@ -857,13 +862,16 @@ public class PlacePageView extends RelativeLayout { if (products.length == 0) { - mRvSponsoredProducts.setAdapter(Factory.createViatorErrorAdapter(cityUrl, this)); + mRvSponsoredProducts.setAdapter(Factory.createViatorErrorAdapter(cityUrl, + mDefaultGalleryItemListener)); Statistics.INSTANCE.trackSponsoredGalleryError(Sponsored.TYPE_VIATOR, Statistics.ParamValue.NO_PRODUCTS); } else { - mRvSponsoredProducts.setAdapter(Factory.createViatorAdapter(products, cityUrl, this)); + ItemSelectedListener listener + = new SponsoredItemSelectedListener<>(getActivity()); + mRvSponsoredProducts.setAdapter(Factory.createViatorAdapter(products, cityUrl, listener)); } } @@ -871,13 +879,14 @@ public class PlacePageView extends RelativeLayout { if (products.length == 0) { - mRvSponsoredProducts.setAdapter(Factory.createCianErrorAdapter(url, this)); + mRvSponsoredProducts.setAdapter(Factory.createCianErrorAdapter(url, mDefaultGalleryItemListener)); Statistics.INSTANCE.trackSponsoredGalleryError(Sponsored.TYPE_CIAN, Statistics.ParamValue.NO_PRODUCTS); } else { - mRvSponsoredProducts.setAdapter(Factory.createCianAdapter(products, url, this)); + ItemSelectedListener listener = new SponsoredItemSelectedListener<>(getActivity()); + mRvSponsoredProducts.setAdapter(Factory.createCianAdapter(products, url, listener)); } } @@ -897,13 +906,15 @@ public class PlacePageView extends RelativeLayout private void showLoadingViatorProducts(@NonNull String id, @NonNull String cityUrl) { if (!Viator.hasCache(id)) - mRvSponsoredProducts.setAdapter(Factory.createViatorLoadingAdapter(cityUrl, this)); + mRvSponsoredProducts.setAdapter(Factory.createViatorLoadingAdapter(cityUrl, + mDefaultGalleryItemListener)); } private void showLoadingCianProducts(@NonNull FeatureId id, @NonNull String url) { if (!Cian.hasCache(id)) - mRvSponsoredProducts.setAdapter(Factory.createCianLoadingAdapter(url, this)); + mRvSponsoredProducts.setAdapter(Factory.createCianLoadingAdapter(url, + mDefaultGalleryItemListener)); } private void updateGallerySponsoredTitle(@Sponsored.SponsoredType int type) @@ -922,33 +933,6 @@ public class PlacePageView extends RelativeLayout mRvSponsoredProducts.swapAdapter(null /* adapter */ , false); } - @Override - public void onItemSelected(@NonNull Context context, @NonNull String url) - { - Utils.openUrl(context, url); - if (mSponsored != null) - Statistics.INSTANCE.trackSponsoredEvent(Statistics.EventName.PP_SPONSOR_ITEM_SELECTED, - mSponsored.getType()); - } - - @Override - public void onMoreItemSelected(@NonNull Context context, @NonNull String url) - { - Utils.openUrl(context, url); - if (mSponsored != null) - Statistics.INSTANCE.trackSponsoredEvent(Statistics.EventName.PP_SPONSOR_MORE_SELECTED, - mSponsored.getType()); - } - - @Override - public void onDetailsSelected(@NonNull Context context, @Nullable String url) - { - if (TextUtils.isEmpty(url)) - return; - - Utils.openUrl(context, url); - } - @Override public void onLineCountCalculated(boolean grater) { @@ -2178,4 +2162,38 @@ public class PlacePageView extends RelativeLayout getPaddingRight(), mMarginBase); } } + + private class SponsoredItemSelectedListener + extends BaseItemSelectedListener + { + + SponsoredItemSelectedListener(@NonNull Activity context) + { + super(context); + } + + @Override + public void onItemSelected(@NonNull I item) + { + Utils.openUrl(getActivity(), item.getUrl()); + if (mSponsored != null) + Statistics.INSTANCE.trackSponsoredEvent(Statistics.EventName.PP_SPONSOR_ITEM_SELECTED, + mSponsored.getType()); + } + + @Override + public void onMoreItemSelected(@NonNull I item) + { + Utils.openUrl(getActivity(), item.getUrl()); + if (mSponsored != null) + Statistics.INSTANCE.trackSponsoredEvent(Statistics.EventName.PP_SPONSOR_MORE_SELECTED, + mSponsored.getType()); + } + + @Override + public void onActionButtonSelected(@NonNull I item) + { + Utils.openUrl(getActivity(), item.getUrl()); + } + } } diff --git a/android/src/com/mapswithme/util/Utils.java b/android/src/com/mapswithme/util/Utils.java index 808ec89730..f8e29830c6 100644 --- a/android/src/com/mapswithme/util/Utils.java +++ b/android/src/com/mapswithme/util/Utils.java @@ -211,8 +211,11 @@ public class Utils activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Constants.Url.TWITTER_MAPSME_HTTP))); } - public static void openUrl(@NonNull Context activity, @NonNull String url) + public static void openUrl(@NonNull Context activity, @Nullable String url) { + if (TextUtils.isEmpty(url)) + return; + final Intent intent = new Intent(Intent.ACTION_VIEW); if (!url.startsWith("http://") && !url.startsWith("https://")) url = "http://" + url;