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

SearchEngine.java « search « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7f8eaf39cfa578761c5de237147f1dd987cf6b0d (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package com.mapswithme.maps.search;

import android.support.annotation.MainThread;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.mapswithme.maps.Framework;
import com.mapswithme.maps.api.ParsedMwmRequest;
import com.mapswithme.maps.bookmarks.data.FeatureId;
import com.mapswithme.util.Language;
import com.mapswithme.util.Listeners;
import com.mapswithme.util.concurrency.UiThread;

import java.io.UnsupportedEncodingException;

public enum SearchEngine implements NativeSearchListener,
                                    NativeMapSearchListener,
                                    NativeBookingFilterListener
{
  INSTANCE;

  // Query, which results are shown on the map.
  @Nullable
  private String mQuery;

  @Override
  public void onResultsUpdate(final SearchResult[] results, final long timestamp,
                              final boolean isHotel)
  {
    UiThread.run(
        () ->
        {
          for (NativeSearchListener listener : mListeners)
            listener.onResultsUpdate(results, timestamp, isHotel);
          mListeners.finishIterate();
        });
  }

  @Override
  public void onResultsEnd(final long timestamp)
  {
    UiThread.run(
        () ->
        {
          for (NativeSearchListener listener : mListeners)
            listener.onResultsEnd(timestamp);
          mListeners.finishIterate();
        });
  }

  @Override
  public void onMapSearchResults(final NativeMapSearchListener.Result[] results, final long timestamp, final boolean isLast)
  {
    UiThread.run(
        () ->
        {
          for (NativeMapSearchListener listener : mMapListeners)
            listener.onMapSearchResults(results, timestamp, isLast);
          mMapListeners.finishIterate();
        });
  }

  public void onBookmarksResultsUpdate(@Nullable long[] bookmarkIds, long timestamp)
  {
    // Dummy. Will be implemented soon.
  }

  public void onBookmarksResultsEnd(@Nullable long[] bookmarkIds, long timestamp)
  {
    // Dummy. Will be implemented soon.
  }

  @Override
  public void onFilterHotels(@BookingFilter.Type int type, @Nullable FeatureId[] hotels)
  {
    UiThread.run(
        () ->
        {
          for (NativeBookingFilterListener listener : mHotelListeners)
            listener.onFilterHotels(type, hotels);
          mHotelListeners.finishIterate();
        });
  }

  @NonNull
  private final Listeners<NativeSearchListener> mListeners = new Listeners<>();
  @NonNull
  private final Listeners<NativeMapSearchListener> mMapListeners = new Listeners<>();
  @NonNull
  private final Listeners<NativeBookingFilterListener> mHotelListeners = new Listeners<>();

  public void addListener(NativeSearchListener listener)
  {
    mListeners.register(listener);
  }

  public void removeListener(NativeSearchListener listener)
  {
    mListeners.unregister(listener);
  }

  public void addMapListener(NativeMapSearchListener listener)
  {
    mMapListeners.register(listener);
  }

  public void removeMapListener(NativeMapSearchListener listener)
  {
    mMapListeners.unregister(listener);
  }

  public void addHotelListener(@NonNull NativeBookingFilterListener listener)
  {
    mHotelListeners.register(listener);
  }

  public void removeHotelListener(@NonNull NativeBookingFilterListener listener)
  {
    mHotelListeners.unregister(listener);
  }

  SearchEngine()
  {
    nativeInit();
  }

  private native void nativeInit();

  /**
   * @param timestamp Search results are filtered according to it after multiple requests.
   * @return whether search was actually started.
   */
  @MainThread
  public boolean search(String query, long timestamp, boolean hasLocation,
                               double lat, double lon, @Nullable HotelsFilter hotelsFilter,
                               @Nullable BookingFilterParams bookingParams)
  {
    try
    {
      return nativeRunSearch(query.getBytes("utf-8"), Language.getKeyboardLocale(),
                             timestamp, hasLocation, lat, lon, hotelsFilter, bookingParams);
    } catch (UnsupportedEncodingException ignored) { }

    return false;
  }

  @MainThread
  public void searchInteractive(@NonNull String query, @NonNull String locale, long timestamp,
                                       boolean isMapAndTable, @Nullable HotelsFilter hotelsFilter,
                                       @Nullable BookingFilterParams bookingParams)
  {
    try
    {
      nativeRunInteractiveSearch(query.getBytes("utf-8"), locale, timestamp, isMapAndTable,
                                 hotelsFilter, bookingParams);
    } catch (UnsupportedEncodingException ignored) { }
  }

  @MainThread
  public void searchInteractive(@NonNull String query, long timestamp, boolean isMapAndTable,
                                       @Nullable HotelsFilter hotelsFilter, @Nullable BookingFilterParams bookingParams)
  {
    searchInteractive(query, Language.getKeyboardLocale(), timestamp, isMapAndTable, hotelsFilter, bookingParams);
  }

  @MainThread
  public static void searchMaps(String query, long timestamp)
  {
    try
    {
      nativeRunSearchMaps(query.getBytes("utf-8"), Language.getKeyboardLocale(), timestamp);
    } catch (UnsupportedEncodingException ignored) { }
  }

  public void setQuery(@Nullable String query)
  {
    mQuery = query;
  }

  @Nullable
  public String getQuery()
  {
    return mQuery;
  }

  @MainThread
  public void cancel()
  {
    cancelApiCall();
    cancelAllSearches();
  }
  @MainThread
  private static void cancelApiCall()
  {
    if (ParsedMwmRequest.hasRequest())
      ParsedMwmRequest.setCurrentRequest(null);
    Framework.nativeClearApiPoints();
  }

  @MainThread
  public void cancelInteractiveSearch()
  {
    mQuery = "";
    nativeCancelInteractiveSearch();
  }

  @MainThread
  private void cancelAllSearches()
  {
    mQuery = "";
    nativeCancelAllSearches();
  }

  @MainThread
  public void showResult(int index)
  {
    mQuery = "";
    nativeShowResult(index);
  }

  /**
   * @param bytes utf-8 formatted bytes of query.
   */
  private static native boolean nativeRunSearch(byte[] bytes, String language, long timestamp, boolean hasLocation,
                                                double lat, double lon, @Nullable HotelsFilter hotelsFilter,
                                                @Nullable BookingFilterParams bookingParams);

  /**
   * @param bytes utf-8 formatted query bytes
   * @param bookingParams
   */
  private static native void nativeRunInteractiveSearch(byte[] bytes, String language, long timestamp,
                                                        boolean isMapAndTable, @Nullable HotelsFilter hotelsFilter,
                                                        @Nullable BookingFilterParams bookingParams);

  /**
   * @param bytes utf-8 formatted query bytes
   */
  private static native void nativeRunSearchMaps(byte[] bytes, String language, long timestamp);

  private static native void nativeShowResult(int index);

  private static native void nativeCancelInteractiveSearch();

  private static native void nativeCancelEverywhereSearch();

  private static native void nativeCancelAllSearches();

  /**
   * @return all existing hotel types
   */
  @NonNull
  static native HotelsFilter.HotelType[] nativeGetHotelTypes();
}