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

DiscoveryManager.java « discovery « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 232c8077f8895d10104800ecc02780a513df50b0 (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
package com.mapswithme.maps.discovery;

import android.annotation.SuppressLint;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.mapswithme.maps.promo.PromoCityGallery;
import com.mapswithme.maps.search.SearchResult;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;

import java.util.EnumSet;

public enum DiscoveryManager
{
  INSTANCE;
  private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC);
  private static final String TAG = DiscoveryManager.class.getSimpleName();
  private static final EnumSet<ItemType> AGGREGATE_EMPTY_RESULTS = EnumSet.noneOf(ItemType.class);
  @Nullable
  private DiscoveryResultReceiver mCallback;
  private int mRequestedTypesCount;


  public void discover(@NonNull DiscoveryParams params)
  {
    LOGGER.d(TAG, "discover: " + params);
    AGGREGATE_EMPTY_RESULTS.clear();
    mRequestedTypesCount = params.getItemTypes().length;
    DiscoveryManager.nativeDiscover(params);
  }

  // Called from JNI.
  @SuppressLint("SwitchIntDef")
  @MainThread
  private void onResultReceived(final @NonNull SearchResult[] results,
                                final @DiscoveryParams.ItemType int typeIndex)
  {
    if (typeIndex >= ItemType.values().length)
    {
      throw new AssertionError("Unsupported discovery item type " +
                               "'" + typeIndex + "' for search results!");

    }
    ItemType type = ItemType.values()[typeIndex];
    notifyUiWithCheck(results, type, callback -> onResultReceivedSafely(callback, type, results));
  }

  private void onResultReceivedSafely(@NonNull DiscoveryResultReceiver callback,
                                      @NonNull ItemType type,
                                      @NonNull SearchResult[] results)
  {
    type.onResultReceived(callback, results);
  }

  // Called from JNI.
  @SuppressWarnings("unused")
  @MainThread
  private void onLocalExpertsReceived(@NonNull final LocalExpert[] experts)
  {
    notifyUiWithCheck(experts, ItemType.LOCAL_EXPERTS,
                      callback -> callback.onLocalExpertsReceived(experts));
  }

  // Called from JNI.
  @SuppressWarnings("unused")
  @MainThread
  private void onPromoCityGalleryReceived(@NonNull PromoCityGallery gallery)
  {
    notifyUiWithCheck(gallery.getItems(), ItemType.PROMO,
                      callback -> callback.onCatalogPromoResultReceived(gallery));
  }

  // Called from JNI.
  @SuppressWarnings("unused")
  @MainThread
  private void onError(@DiscoveryParams.ItemType int type)
  {
    LOGGER.w(TAG, "onError for type: " + type);
    if (mCallback != null)
      mCallback.onError(ItemType.values()[type]);
  }

  private <T> void notifyUiWithCheck(@NonNull T[] results, @NonNull ItemType type,
                                     @NonNull Action action)
  {
    LOGGER.d(TAG, "Results size = " + results.length + " for type: " + type);
    if (mCallback == null)
      return;

    if (isAggregateResultsEmpty(results, type))
    {
      mCallback.onNotFound();
      return;
    }

    action.run(mCallback);
  }

  private <T> boolean isAggregateResultsEmpty(@NonNull T[] results, @NonNull ItemType type)
  {
    if (results.length == 0)
      AGGREGATE_EMPTY_RESULTS.add(type);

    return mRequestedTypesCount == AGGREGATE_EMPTY_RESULTS.size();
  }

  void attach(@NonNull DiscoveryResultReceiver callback)
  {
    LOGGER.d(TAG, "attach callback: " + callback);
    mCallback = callback;
  }

  void detach()
  {
    LOGGER.d(TAG, "detach callback: " + mCallback);
    mCallback = null;
  }

  public static native void nativeDiscover(@NonNull DiscoveryParams params);

  @NonNull
  public static native String nativeGetLocalExpertsUrl();

  interface Action
  {
    void run(@NonNull DiscoveryResultReceiver callback);
  }
}