From 5b15df71087421f5faef9bf61b1829feaa5a97f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80=20?= =?UTF-8?q?=D0=97=D0=B0=D1=86=D0=B5=D0=BF=D0=B8=D0=BD?= Date: Thu, 29 Jun 2017 14:52:06 +0300 Subject: [PATCH] [android] Implemented taxi link support --- .../routing/RoutingBottomMenuController.java | 40 ++++++++------ .../maps/routing/RoutingController.java | 14 ----- .../com/mapswithme/maps/taxi/TaxiInfo.java | 6 +++ .../com/mapswithme/maps/taxi/TaxiManager.java | 19 +++++-- android/src/com/mapswithme/util/Utils.java | 53 +++++++++++++++---- .../util/statistics/Statistics.java | 9 ++-- 6 files changed, 95 insertions(+), 46 deletions(-) diff --git a/android/src/com/mapswithme/maps/routing/RoutingBottomMenuController.java b/android/src/com/mapswithme/maps/routing/RoutingBottomMenuController.java index 2ee5a8f985..308a4d10da 100644 --- a/android/src/com/mapswithme/maps/routing/RoutingBottomMenuController.java +++ b/android/src/com/mapswithme/maps/routing/RoutingBottomMenuController.java @@ -27,6 +27,7 @@ import com.mapswithme.maps.location.LocationHelper; import com.mapswithme.maps.taxi.TaxiAdapter; import com.mapswithme.maps.taxi.TaxiInfo; import com.mapswithme.maps.taxi.TaxiLinks; +import com.mapswithme.maps.taxi.TaxiManager; import com.mapswithme.maps.widget.DotPager; import com.mapswithme.util.Graphics; import com.mapswithme.util.UiUtils; @@ -207,26 +208,16 @@ final class RoutingBottomMenuController implements View.OnClickListener void setStartButton() { - if (RoutingController.get().isTaxiRouterType()) + if (RoutingController.get().isTaxiRouterType() && mTaxiInfo != null) { - // TODO: add getting the link by provider (Yandex, Uber) - final boolean isTaxiInstalled = Utils.isUberInstalled(mContext); - mStart.setText(isTaxiInstalled ? R.string.taxi_order : R.string.install_app); + mStart.setText(Utils.isTaxiAppInstalled(mContext, mTaxiInfo.getType()) + ? R.string.taxi_order : R.string.install_app); mStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - if (mTaxiProduct != null) - { - TaxiLinks links = RoutingController.get().getTaxiLink(mTaxiProduct.getProductId()); - //TODO: check product type and launch corresponding application - if (links != null) - { - Utils.launchUber(mContext, links); - trackUberStatistics(isTaxiInstalled); - } - } + handleTaxiClick(); } }); } else @@ -253,6 +244,23 @@ final class RoutingBottomMenuController implements View.OnClickListener showStartButton(true); } + private void handleTaxiClick() + { + if (mTaxiProduct == null || mTaxiInfo == null) + return; + + boolean isTaxiInstalled = Utils.isTaxiAppInstalled(mContext, mTaxiInfo.getType()); + MapObject startPoint = RoutingController.get().getStartPoint(); + MapObject endPoint = RoutingController.get().getEndPoint(); + TaxiLinks links = TaxiManager.getTaxiLink(mTaxiProduct.getProductId(), mTaxiInfo.getType(), + startPoint, endPoint); + if (links != null) + { + Utils.launchTaxiApp(mContext, links, mTaxiInfo.getType()); + trackTaxiStatistics(mTaxiInfo.getType(), isTaxiInstalled); + } + } + void showError(@StringRes int message) { UiUtils.hide(mTaxiFrame, mAltitudeChartFrame); @@ -336,12 +344,12 @@ final class RoutingBottomMenuController implements View.OnClickListener } } - private static void trackUberStatistics(boolean isUberInstalled) + private static void trackTaxiStatistics(@TaxiManager.TaxiType int type, boolean isTaxiAppInstalled) { MapObject from = RoutingController.get().getStartPoint(); MapObject to = RoutingController.get().getEndPoint(); Location location = LocationHelper.INSTANCE.getLastKnownLocation(); - Statistics.INSTANCE.trackUber(from, to, location, isUberInstalled); + Statistics.INSTANCE.trackTaxi(from, to, location, type, isTaxiAppInstalled); } @Override diff --git a/android/src/com/mapswithme/maps/routing/RoutingController.java b/android/src/com/mapswithme/maps/routing/RoutingController.java index 78857e4881..884bdb069a 100644 --- a/android/src/com/mapswithme/maps/routing/RoutingController.java +++ b/android/src/com/mapswithme/maps/routing/RoutingController.java @@ -23,7 +23,6 @@ import com.mapswithme.maps.downloader.MapManager; import com.mapswithme.maps.location.LocationHelper; import com.mapswithme.maps.taxi.TaxiInfo; import com.mapswithme.maps.taxi.TaxiInfoError; -import com.mapswithme.maps.taxi.TaxiLinks; import com.mapswithme.maps.taxi.TaxiManager; import com.mapswithme.util.Config; import com.mapswithme.util.ConnectionState; @@ -912,19 +911,6 @@ public class RoutingController implements TaxiManager.TaxiListener mContainer.updateBuildProgress(0, mLastRouterType); } - @Nullable - TaxiLinks getTaxiLink(@NonNull String productId) - { - if (mStartPoint == null || mEndPoint == null) - return null; - - //TODO: implement getting provider type by product id. - return TaxiManager.INSTANCE.nativeGetTaxiLinks(NetworkPolicy.newInstance(true /* canUse */), - TaxiManager.PROVIDER_YANDEX, productId, - mStartPoint.getLat(), mStartPoint.getLon(), - mEndPoint.getLat(), mEndPoint.getLon()); - } - @Override public void onTaxiProviderReceived(@NonNull TaxiInfo provider) { diff --git a/android/src/com/mapswithme/maps/taxi/TaxiInfo.java b/android/src/com/mapswithme/maps/taxi/TaxiInfo.java index df1318e03d..17dae45b09 100644 --- a/android/src/com/mapswithme/maps/taxi/TaxiInfo.java +++ b/android/src/com/mapswithme/maps/taxi/TaxiInfo.java @@ -45,6 +45,12 @@ public class TaxiInfo implements Parcelable mProducts = products; } + @TaxiManager.TaxiType + public int getType() + { + return mType; + } + @NonNull public List getProducts() { diff --git a/android/src/com/mapswithme/maps/taxi/TaxiManager.java b/android/src/com/mapswithme/maps/taxi/TaxiManager.java index b7671762ed..5b4bf2c465 100644 --- a/android/src/com/mapswithme/maps/taxi/TaxiManager.java +++ b/android/src/com/mapswithme/maps/taxi/TaxiManager.java @@ -5,6 +5,7 @@ import android.support.annotation.MainThread; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import com.mapswithme.maps.bookmarks.data.MapObject; import com.mapswithme.util.NetworkPolicy; import com.mapswithme.util.concurrency.UiThread; @@ -14,18 +15,17 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; - @MainThread public class TaxiManager { - static final int PROVIDER_UBER = 0; + public static final int PROVIDER_UBER = 0; public static final int PROVIDER_YANDEX = 1; public static final TaxiManager INSTANCE = new TaxiManager(); @Retention(RetentionPolicy.SOURCE) @IntDef({ PROVIDER_UBER, PROVIDER_YANDEX }) - @interface TaxiType {} + public @interface TaxiType {} @NonNull private final List mProviders = new ArrayList<>(); @@ -79,6 +79,19 @@ public class TaxiManager } } + @Nullable + public static TaxiLinks getTaxiLink(@NonNull String productId, @TaxiType int type, + @Nullable MapObject startPoint, @Nullable MapObject endPoint) + { + if (startPoint == null || endPoint == null) + return null; + + return TaxiManager.INSTANCE.nativeGetTaxiLinks(NetworkPolicy.newInstance(true /* canUse */), + type, productId, startPoint.getLat(), + startPoint.getLon(), endPoint.getLat(), + endPoint.getLon()); + } + public void setTaxiListener(@Nullable TaxiListener listener) { mListener = listener; diff --git a/android/src/com/mapswithme/util/Utils.java b/android/src/com/mapswithme/util/Utils.java index 6fa7234a1e..09e4709c05 100644 --- a/android/src/com/mapswithme/util/Utils.java +++ b/android/src/com/mapswithme/util/Utils.java @@ -30,6 +30,7 @@ import com.mapswithme.maps.MwmApplication; import com.mapswithme.maps.R; import com.mapswithme.maps.activity.CustomNavigateUpListener; import com.mapswithme.maps.taxi.TaxiLinks; +import com.mapswithme.maps.taxi.TaxiManager; import com.mapswithme.util.concurrency.UiThread; import com.mapswithme.util.log.Logger; import com.mapswithme.util.log.LoggerFactory; @@ -284,12 +285,12 @@ public class Utils return installationId; } - public static boolean isUberInstalled(@NonNull Activity context) + private static boolean isAppInstalled(@NonNull Activity context, @NonNull String packageName) { try { PackageManager pm = context.getPackageManager(); - pm.getPackageInfo("com.ubercab", PackageManager.GET_ACTIVITIES); + pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { @@ -297,20 +298,54 @@ public class Utils } } - public static void launchUber(@NonNull Activity context, @NonNull TaxiLinks links) + public static boolean isTaxiAppInstalled(@NonNull Activity context, @TaxiManager.TaxiType int type) { - //TODO: handle type of taxi merely - final Intent intent = new Intent(Intent.ACTION_VIEW); -/* if (isUberInstalled(context)) + switch (type) { + case TaxiManager.PROVIDER_UBER: + return Utils.isAppInstalled(context, "com.ubercab"); + case TaxiManager.PROVIDER_YANDEX: + return Utils.isAppInstalled(context, "ru.yandex.taxi"); + default: + throw new AssertionError("Unsupported taxi type: " + type); + } + } - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);*/ + public static void launchTaxiApp(@NonNull Activity context, @NonNull TaxiLinks links, + @TaxiManager.TaxiType int type) + { + switch (type) + { + case TaxiManager.PROVIDER_UBER: + launchUber(context, links, isTaxiAppInstalled(context, type)); + break; + case TaxiManager.PROVIDER_YANDEX: + launchYandexTaxi(context, links); + break; + default: + throw new AssertionError("Unsupported taxi type: " + type); + } + } + + private static void launchUber(@NonNull Activity context, @NonNull TaxiLinks links, boolean isAppInstalled) + { + final Intent intent = new Intent(Intent.ACTION_VIEW); + if (isAppInstalled) + { + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setData(Uri.parse(links.getDeepLink())); -/* } else + } else { // No Taxi app! Open mobile website. intent.setData(Uri.parse(links.getUniversalLink())); - }*/ + } + context.startActivity(intent); + } + + private static void launchYandexTaxi(@NonNull Activity context, @NonNull TaxiLinks links) + { + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setData(Uri.parse(links.getDeepLink())); context.startActivity(intent); } diff --git a/android/src/com/mapswithme/util/statistics/Statistics.java b/android/src/com/mapswithme/util/statistics/Statistics.java index 202acc6a9c..d6b2249904 100644 --- a/android/src/com/mapswithme/util/statistics/Statistics.java +++ b/android/src/com/mapswithme/util/statistics/Statistics.java @@ -26,6 +26,7 @@ import com.mapswithme.maps.downloader.MapManager; import com.mapswithme.maps.editor.Editor; import com.mapswithme.maps.editor.OsmOAuth; import com.mapswithme.maps.location.LocationHelper; +import com.mapswithme.maps.taxi.TaxiManager; import com.mapswithme.maps.widget.placepage.Sponsored; import com.mapswithme.util.BatteryState; import com.mapswithme.util.Config; @@ -519,11 +520,11 @@ public enum Statistics trackEvent(EventName.EDITOR_AUTH_REQUEST, Statistics.params().add(Statistics.EventParam.TYPE, type.name)); } - public void trackUber(@Nullable MapObject from, @Nullable MapObject to, - @Nullable Location location, boolean isUberInstalled) + public void trackTaxi(@Nullable MapObject from, @Nullable MapObject to, + @Nullable Location location, @TaxiManager.TaxiType int type, boolean isAppInstalled) { Statistics.ParameterBuilder params = Statistics.params(); - params.add(Statistics.EventParam.PROVIDER, "Taxi"); + params.add(Statistics.EventParam.PROVIDER, type == TaxiManager.PROVIDER_YANDEX ? "Yandex" : "Uber"); params.add(Statistics.EventParam.FROM_LAT, from != null ? String.valueOf(from.getLat()) : "N/A") .add(Statistics.EventParam.FROM_LON, from != null ? String.valueOf(from.getLon()) : "N/A"); @@ -531,7 +532,7 @@ public enum Statistics params.add(Statistics.EventParam.TO_LAT, to != null ? String.valueOf(to.getLat()) : "N/A") .add(Statistics.EventParam.TO_LON, to != null ? String.valueOf(to.getLon()) : "N/A"); - String event = isUberInstalled ? Statistics.EventName.ROUTING_TAXI_ORDER + String event = isAppInstalled ? Statistics.EventName.ROUTING_TAXI_ORDER : Statistics.EventName.ROUTING_TAXI_INSTALL; trackEvent(event, location, params.get()); }