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

SearchActivity.cpp « maps « mapswithme « com « jni « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 97677c6f523b5d1fb1279f6ce198246f133568be (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
#include "Framework.hpp"

#include "../../../../../search/result.hpp"

#include "../../../../../base/thread.hpp"

#include "../core/jni_helper.hpp"


class SearchAdapter
{
  /// @name Results holder. Store last valid results from search threads (m_storeID)
  /// and current result to show in GUI (m_ID).
  //@{
  search::Results m_storeResults, m_results;
  int m_storeID, m_ID;
  //@}

  threads::Mutex m_updateMutex;

  /// Last saved activity to run update UI.
  jobject m_activity;

  // This function may be called several times for one queryID.
  // In that case we should increment m_storeID to distinguish different results.
  // Main queryID is incremented by 5-step to leave space for middle queries.
  // This constant should be equal with SearchActivity.QUERY_STEP;
  static int const QUERY_STEP = 5;

  void OnResults(search::Results const & res, int queryID)
  {
    if (s_pInstance == 0)
    {
      // In case when activity is destroyed, but search thread passed any results.
      return;
    }

    threads::MutexGuard guard(m_updateMutex);

    // store current results
    m_storeResults = res;

    if (m_storeID >= queryID && m_storeID < queryID + QUERY_STEP)
    {
      ++m_storeID;
      // not more than QUERY_STEP results per query
      ASSERT_LESS ( m_storeID, queryID + QUERY_STEP, () );
    }
    else
    {
      ASSERT_LESS ( m_storeID, queryID, () );
      m_storeID = queryID;
    }

    // get new environment pointer here because of different thread
    JNIEnv * env = jni::GetEnv();

    // post message to update ListView in UI thread
    jmethodID const id = jni::GetJavaMethodID(env, m_activity, "updateData", "(II)V");
    env->CallVoidMethod(m_activity, id,
                          static_cast<jint>(m_storeResults.GetCount()),
                          static_cast<jint>(m_storeID));
  }

  bool AcquireShowResults(int resultID)
  {
    if (resultID != m_ID)
    {
      {
        // Grab last results.
        threads::MutexGuard guard(m_updateMutex);
        m_results.Swap(m_storeResults);
        m_ID = m_storeID;
      }

      if (resultID != m_ID)
      {
        // It happens only when better results came faster than GUI.
        // It is a rare case, skip this query.
        ASSERT_GREATER ( m_ID, resultID, () );
        return false;
      }
    }

    return true;
  }

  bool CheckPosition(int position) const
  {
    int const count = static_cast<int>(m_results.GetCount());

    // for safety reasons do actual check always
    ASSERT_LESS ( position, count, () );
    return (position < count);
  }

  SearchAdapter(JNIEnv * env, jobject activity)
    : m_ID(0), m_storeID(0)
  {
    m_activity = env->NewGlobalRef(activity);
  }

  void Delete(JNIEnv * env)
  {
    env->DeleteGlobalRef(m_activity);
  }

  static SearchAdapter * s_pInstance;

public:
  /// @name Instance lifetime functions.
  //@{
  static void CreateInstance(JNIEnv * env, jobject activity)
  {
    ASSERT ( s_pInstance == 0, () );
    if (s_pInstance)
      delete s_pInstance;

    s_pInstance = new SearchAdapter(env, activity);
  }

  static void DestroyInstance(JNIEnv * env)
  {
    ASSERT ( s_pInstance, () );
    if (s_pInstance)
    {
      s_pInstance->Delete(env);
      delete s_pInstance;
      s_pInstance = 0;
    }
  }

  static SearchAdapter & Instance()
  {
    ASSERT ( s_pInstance, () );
    return *s_pInstance;
  }
  //@}

  bool RunSearch(JNIEnv * env, search::SearchParams & params, int queryID)
  {
    params.m_callback = bind(&SearchAdapter::OnResults, this, _1, queryID);

    return g_framework->Search(params);
  }

  void ShowItem(int position)
  {
    if (CheckPosition(position))
      g_framework->ShowSearchResult(m_results.GetResult(position));
  }

  search::Result const * GetResult(int position, int resultID)
  {
    if (AcquireShowResults(resultID) && CheckPosition(position))
      return &(m_results.GetResult(position));
    return 0;
  }
};

SearchAdapter * SearchAdapter::s_pInstance = 0;

extern "C"
{

JNIEXPORT void JNICALL
Java_com_mapswithme_maps_SearchActivity_nativeInitSearch(JNIEnv * env, jobject thiz)
{
  SearchAdapter::CreateInstance(env, thiz);
}

JNIEXPORT void JNICALL
Java_com_mapswithme_maps_SearchActivity_nativeFinishSearch(JNIEnv * env, jobject thiz)
{
  SearchAdapter::DestroyInstance(env);
}

JNIEXPORT jboolean JNICALL
Java_com_mapswithme_maps_SearchActivity_nativeRunSearch(
    JNIEnv * env, jobject thiz,
    jstring s, jstring lang, jdouble lat, jdouble lon, jint mode, jint queryID)
{
  search::SearchParams params;
  params.m_query = jni::ToNativeString(env, s);
  params.SetInputLanguage(jni::ToNativeString(env, lang));

  if (mode % 2 == 0) params.SetResetMode(true);
  if (mode >= 2) params.SetPosition(lat, lon);

  return SearchAdapter::Instance().RunSearch(env, params, queryID);
}

JNIEXPORT void JNICALL
Java_com_mapswithme_maps_SearchActivity_nativeShowItem(JNIEnv * env, jobject thiz, jint position)
{
  SearchAdapter::Instance().ShowItem(position);
}

JNIEXPORT jobject JNICALL
Java_com_mapswithme_maps_SearchActivity_nativeGetResult(
    JNIEnv * env, jobject thiz, jint position, jint queryID,
    jdouble lat, jdouble lon, jint mode, jdouble north)
{
  search::Result const * res = SearchAdapter::Instance().GetResult(position, queryID);
  if (res == 0) return 0;

  jclass klass = env->FindClass("com/mapswithme/maps/SearchActivity$SearchAdapter$SearchResult");
  ASSERT ( klass, () );

  if (res->GetResultType() == search::Result::RESULT_FEATURE)
  {
    jmethodID methodID = env->GetMethodID(
        klass, "<init>",
        "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;D)V");
    ASSERT ( methodID, () );

    string distance;
    double azimut = -1.0;
    if (mode >= 2)
    {
      if (!g_framework->NativeFramework()->GetDistanceAndAzimut(
          res->GetFeatureCenter(), lat, lon, north, distance, azimut))
      {
        // do not show the arrow for far away features
        azimut = -1.0;
      }
    }

    return env->NewObject(klass, methodID,
                          jni::ToJavaString(env, res->GetString()),
                          jni::ToJavaString(env, res->GetRegionString()),
                          jni::ToJavaString(env, res->GetFeatureType()),
                          jni::ToJavaString(env, res->GetRegionFlag()),
                          jni::ToJavaString(env, distance.c_str()),
                          static_cast<jdouble>(azimut));
  }
  else
  {
    jmethodID methodID = env->GetMethodID(klass, "<init>", "(Ljava/lang/String;)V");
    ASSERT ( methodID, () );

    return env->NewObject(klass, methodID, jni::ToJavaString(env, res->GetSuggestionString()));
  }
}

}