Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TaxiManager.java « taxi « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c1c500cdab1fe4e7d3f07bcdb01cb7d96ec797f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.mapswithme.maps.taxi;

import android.content.Context;
import android.location.Location;
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.maps.location.LocationHelper;
import com.mapswithme.maps.routing.RoutingController;
import com.mapswithme.util.NetworkPolicy;
import com.mapswithme.util.SponsoredLinks;
import com.mapswithme.util.Utils;
import com.mapswithme.util.concurrency.UiThread;
import com.mapswithme.util.statistics.Statistics;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@MainThread
public class TaxiManager
{
  public static final TaxiManager INSTANCE = new TaxiManager();

  @NonNull
  private final List<TaxiInfo> mProviders = new ArrayList<>();
  @NonNull
  private final List<TaxiInfoError> mErrors = new ArrayList<>();
  @Nullable
  private TaxiListener mListener;

  private TaxiManager()
  {
  }

  // Called from JNI.
  @MainThread
  void onTaxiProvidersReceived(@NonNull TaxiInfo[] providers)
  {
    if (!UiThread.currentThreadIsUi())
      throw new AssertionError("Must be called from UI thread!");

    if (providers.length == 0)
    {
      if (mListener != null)
        mListener.onNoTaxiProviders();
      return;
    }

    mProviders.clear();
    mProviders.addAll(Arrays.asList(providers));

    if (mListener != null)
    {
      // Taxi provider list must contain only one element until we implement taxi aggregator feature.
      mListener.onTaxiProviderReceived(mProviders.get(0));
    }
  }

  // Called from JNI.
  @MainThread
  void onTaxiErrorsReceived(@NonNull TaxiInfoError[] errors)
  {
    if (!UiThread.currentThreadIsUi())
      throw new AssertionError("Must be called from UI thread!");

    if (errors.length == 0)
      throw new AssertionError("Taxi error array must be non-empty!");

    mErrors.clear();
    mErrors.addAll(Arrays.asList(errors));

    if (mListener != null)
    {
      // Taxi error list must contain only one element until we implement taxi aggregator feature.
      mListener.onTaxiErrorReceived(mErrors.get(0));
    }
  }

  @Nullable
  public static SponsoredLinks getTaxiLink(@NonNull String productId, @NonNull TaxiType type,
                                           @Nullable MapObject startPoint, @Nullable MapObject endPoint)
  {
    if (startPoint == null || endPoint == null)
      return null;

    return TaxiManager.INSTANCE.nativeGetTaxiLinks(NetworkPolicy.newInstance(true /* canUse */),
                                                   type.ordinal(), productId, startPoint.getLat(),
                                                   startPoint.getLon(), endPoint.getLat(),
                                                   endPoint.getLon());
  }

  public static void launchTaxiApp(@NonNull Context context, @NonNull SponsoredLinks links,
                                   @NonNull TaxiType type)
  {
    Utils.openPartner(context, links, type.getPackageName(), type.getOpenMode());

    boolean isTaxiInstalled = Utils.isAppInstalled(context, type.getPackageName());
    trackTaxiStatistics(type.getProviderName(), isTaxiInstalled);
  }

  private static void trackTaxiStatistics(@NonNull String taxiName, boolean isTaxiAppInstalled)
  {
    MapObject from = RoutingController.get().getStartPoint();
    MapObject to = RoutingController.get().getEndPoint();
    Location location = LocationHelper.INSTANCE.getLastKnownLocation();
    Statistics.INSTANCE.trackTaxiInRoutePlanning(from, to, location, taxiName, isTaxiAppInstalled);
  }

  public void setTaxiListener(@Nullable TaxiListener listener)
  {
    mListener = listener;
  }

  public native void nativeRequestTaxiProducts(@NonNull NetworkPolicy policy, double srcLat,
                                               double srcLon, double dstLat, double dstLon);

  @NonNull
  public native SponsoredLinks nativeGetTaxiLinks(@NonNull NetworkPolicy policy, int type,
                                                  @NonNull String productId, double srcLon,
                                                  double srcLat, double dstLat, double dstLon);

  public enum ErrorCode
  {
    NoProducts, RemoteError, NoProviders
  }

  public interface TaxiListener
  {
    void onTaxiProviderReceived(@NonNull TaxiInfo provider);

    void onTaxiErrorReceived(@NonNull TaxiInfoError error);

    void onNoTaxiProviders();
  }
}