forked from organicmaps/organicmaps
Added JNI for Discovery manager
This commit is contained in:
parent
52a40aa177
commit
e49fad312f
14 changed files with 319 additions and 141 deletions
|
@ -25,7 +25,10 @@ set(
|
|||
com/mapswithme/core/logging.hpp
|
||||
com/mapswithme/core/ScopedEnv.hpp
|
||||
com/mapswithme/core/ScopedLocalRef.hpp
|
||||
com/mapswithme/maps/discovery/Locals.hpp
|
||||
com/mapswithme/maps/Framework.hpp
|
||||
com/mapswithme/maps/SearchEngine.hpp
|
||||
com/mapswithme/maps/viator/Viator.hpp
|
||||
com/mapswithme/opengl/android_gl_utils.hpp
|
||||
com/mapswithme/opengl/androidoglcontext.hpp
|
||||
com/mapswithme/opengl/androidoglcontextfactory.hpp
|
||||
|
@ -40,6 +43,8 @@ set(
|
|||
com/mapswithme/maps/bookmarks/data/BookmarkManager.cpp
|
||||
com/mapswithme/maps/bookmarks/data/BookmarkCategory.cpp
|
||||
com/mapswithme/maps/cian/Cian.cpp
|
||||
com/mapswithme/maps/discovery/DiscoveryManager.cpp
|
||||
com/mapswithme/maps/discovery/Locals.cpp
|
||||
com/mapswithme/maps/DisplayedCategories.cpp
|
||||
com/mapswithme/maps/DownloadResourcesLegacyActivity.cpp
|
||||
com/mapswithme/maps/editor/OpeningHours.cpp
|
||||
|
|
|
@ -1567,52 +1567,4 @@ Java_com_mapswithme_maps_Framework_nativeIsUserAuthenticated()
|
|||
{
|
||||
return frm()->GetUser().IsAuthenticated();
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_mapswithme_maps_Framework_nativeDiscover(JNIEnv * env, jclass, jobject params)
|
||||
{
|
||||
discovery::ClientParams p;
|
||||
auto const paramsClass = env->GetObjectClass(params);
|
||||
static auto const currencyField = env->GetFieldID(paramsClass, "mCurrency", "Ljava/lang/String;");
|
||||
{
|
||||
auto const currency = static_cast<jstring>(env->GetObjectField(params, currencyField));
|
||||
string const res = jni::ToNativeString(env, currency);
|
||||
if (!res.empty())
|
||||
p.m_currency = res;
|
||||
}
|
||||
|
||||
static auto const langField = env->GetFieldID(paramsClass, "mLang", "Ljava/lang/String;");
|
||||
{
|
||||
auto const lang = static_cast<jstring>(env->GetObjectField(params, langField));
|
||||
string const res = jni::ToNativeString(env, lang);
|
||||
if (!res.empty())
|
||||
p.m_lang = res;
|
||||
}
|
||||
|
||||
static auto const itemsCountField = env->GetFieldID(paramsClass, "mItemsCount", "I");
|
||||
{
|
||||
auto const count = env->GetIntField(params, itemsCountField);
|
||||
ASSERT_GREATER(count, 0, ());
|
||||
p.m_itemsCount = static_cast<size_t>(count);
|
||||
}
|
||||
|
||||
static auto const itemTypesField = env->GetFieldID(paramsClass, "mItemTypes", "[I");
|
||||
{
|
||||
auto const array = static_cast<jintArray>(env->GetObjectField(params, itemTypesField));
|
||||
auto const length = env->GetArrayLength(array);
|
||||
ASSERT_GREATER(length, 0, ());
|
||||
|
||||
auto const dtor = [array, env](jint * data) { env->ReleaseIntArrayElements(array, data, 0); };
|
||||
std::unique_ptr<jint, decltype(dtor)> data{env->GetIntArrayElements(array, nullptr), dtor};
|
||||
|
||||
vector<discovery::ItemType> itemTypes;
|
||||
for (jsize i = 0; i < length; ++i)
|
||||
itemTypes.emplace_back(static_cast<discovery::ItemType>(data.get()[i]));
|
||||
|
||||
p.m_itemTypes = std::move(itemTypes);
|
||||
}
|
||||
|
||||
// TODO: Use callbacks for success and for error.
|
||||
frm()->Discover(std::move(p), g_framework->GetDiscoveryCallback(), [](discovery::ItemType const t) {});
|
||||
}
|
||||
} // extern "C"
|
||||
|
|
|
@ -42,13 +42,6 @@ struct EverywhereSearchParams;
|
|||
|
||||
namespace android
|
||||
{
|
||||
struct DiscoveryCallback
|
||||
{
|
||||
void operator()(search::Results const & results, discovery::ItemType const type) const { }
|
||||
void operator()(vector<viator::Product> const & products) const { }
|
||||
void operator()(vector<locals::LocalExpert> const & experts) const { }
|
||||
};
|
||||
|
||||
class Framework
|
||||
{
|
||||
private:
|
||||
|
@ -78,16 +71,12 @@ namespace android
|
|||
|
||||
place_page::Info m_info;
|
||||
|
||||
DiscoveryCallback m_discoveryCallback;
|
||||
|
||||
public:
|
||||
Framework();
|
||||
|
||||
storage::Storage & GetStorage();
|
||||
Index const & GetIndex();
|
||||
|
||||
DiscoveryCallback const & GetDiscoveryCallback() const { return m_discoveryCallback; }
|
||||
|
||||
void ShowNode(storage::TCountryId const & countryId, bool zoomToDownloadButton);
|
||||
|
||||
void OnLocationError(int/* == location::TLocationStatus*/ newStatus);
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
#include "Framework.hpp"
|
||||
#include "com/mapswithme/maps/SearchEngine.hpp"
|
||||
#include "com/mapswithme/maps/Framework.hpp"
|
||||
#include "com/mapswithme/platform/Platform.hpp"
|
||||
|
||||
#include "map/everywhere_search_params.hpp"
|
||||
#include "map/viewport_search_params.hpp"
|
||||
|
@ -10,9 +12,6 @@
|
|||
#include "base/assert.hpp"
|
||||
#include "base/logging.hpp"
|
||||
|
||||
#include "com/mapswithme/core/jni_helper.hpp"
|
||||
#include "com/mapswithme/platform/Platform.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
|
@ -387,26 +386,6 @@ jobject ToJavaResult(Result & result, bool isLocalAdsCustomer, bool hasPosition,
|
|||
return ret;
|
||||
}
|
||||
|
||||
jobjectArray BuildJavaResults(Results const & results, vector<bool> const & isLocalAdsCustomer,
|
||||
bool hasPosition, double lat, double lon)
|
||||
{
|
||||
JNIEnv * env = jni::GetEnv();
|
||||
|
||||
g_results = results;
|
||||
|
||||
int const count = g_results.GetCount();
|
||||
jobjectArray const jResults = env->NewObjectArray(count, g_resultClass, nullptr);
|
||||
|
||||
ASSERT_EQUAL(results.GetCount(), isLocalAdsCustomer.size(), ());
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
jni::TScopedLocalRef jRes(
|
||||
env, ToJavaResult(g_results[i], isLocalAdsCustomer[i], hasPosition, lat, lon));
|
||||
env->SetObjectArrayElement(jResults, i, jRes.get());
|
||||
}
|
||||
return jResults;
|
||||
}
|
||||
|
||||
void OnResults(Results const & results, vector<bool> const & isLocalAdsCustomer,
|
||||
long long timestamp, bool isMapAndTable, bool hasPosition, double lat, double lon)
|
||||
{
|
||||
|
@ -419,7 +398,7 @@ void OnResults(Results const & results, vector<bool> const & isLocalAdsCustomer,
|
|||
if (!results.IsEndMarker() || results.IsEndedNormal())
|
||||
{
|
||||
jni::TScopedLocalObjectArrayRef jResults(
|
||||
env, BuildJavaResults(results, isLocalAdsCustomer, hasPosition, lat, lon));
|
||||
env, BuildSearchResults(results, isLocalAdsCustomer, hasPosition, lat, lon));
|
||||
env->CallVoidMethod(g_javaListener, g_updateResultsId, jResults.get(),
|
||||
static_cast<jlong>(timestamp),
|
||||
search::HotelsClassifier::IsHotelResults(results));
|
||||
|
@ -460,9 +439,28 @@ void OnMapSearchResults(storage::DownloaderSearchResults const & results, long l
|
|||
jni::TScopedLocalObjectArrayRef jResults(env, BuildJavaMapResults(results.m_results));
|
||||
env->CallVoidMethod(g_javaListener, g_mapResultsMethod, jResults.get(), static_cast<jlong>(timestamp), results.m_endMarker);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
jobjectArray BuildSearchResults(Results const & results, vector<bool> const & isLocalAdsCustomer,
|
||||
bool hasPosition, double lat, double lon)
|
||||
{
|
||||
JNIEnv * env = jni::GetEnv();
|
||||
|
||||
g_results = results;
|
||||
|
||||
int const count = g_results.GetCount();
|
||||
jobjectArray const jResults = env->NewObjectArray(count, g_resultClass, nullptr);
|
||||
|
||||
ASSERT_EQUAL(results.GetCount(), isLocalAdsCustomer.size(), ());
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
jni::TScopedLocalRef jRes(
|
||||
env, ToJavaResult(g_results[i], isLocalAdsCustomer[i], hasPosition, lat, lon));
|
||||
env->SetObjectArrayElement(jResults, i, jRes.get());
|
||||
}
|
||||
return jResults;
|
||||
}
|
||||
|
||||
extern "C"
|
||||
{
|
||||
JNIEXPORT void JNICALL
|
||||
|
|
11
android/jni/com/mapswithme/maps/SearchEngine.hpp
Normal file
11
android/jni/com/mapswithme/maps/SearchEngine.hpp
Normal file
|
@ -0,0 +1,11 @@
|
|||
#pragma once
|
||||
|
||||
#include "com/mapswithme/core/jni_helper.hpp"
|
||||
|
||||
#include "search/result.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
jobjectArray BuildSearchResults(search::Results const & results,
|
||||
std::vector<bool> const & isLocalAdsCustomer,
|
||||
bool hasPosition, double lat, double lon);
|
183
android/jni/com/mapswithme/maps/discovery/DiscoveryManager.cpp
Normal file
183
android/jni/com/mapswithme/maps/discovery/DiscoveryManager.cpp
Normal file
|
@ -0,0 +1,183 @@
|
|||
#include "com/mapswithme/core/jni_helper.hpp"
|
||||
#include "com/mapswithme/maps/discovery/Locals.hpp"
|
||||
#include "com/mapswithme/maps/viator/Viator.hpp"
|
||||
#include "com/mapswithme/maps/Framework.hpp"
|
||||
#include "com/mapswithme/maps/SearchEngine.hpp"
|
||||
|
||||
#include "map/discovery/discovery_manager.hpp"
|
||||
|
||||
#include "geometry/mercator.hpp"
|
||||
|
||||
#include "search/result.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
using namespace std::placeholders;
|
||||
|
||||
namespace
|
||||
{
|
||||
jclass g_discoveryManagerClass = nullptr;
|
||||
jfieldID g_discoveryManagerInstanceField;
|
||||
jmethodID g_onResultReceivedMethod;
|
||||
jmethodID g_onViatorProductsReceivedMethod;
|
||||
jmethodID g_onLocalExpertsReceivedMethod;
|
||||
jmethodID g_onErrorMethod;
|
||||
uint32_t g_lastRequestId = 0;
|
||||
|
||||
void PrepareClassRefs(JNIEnv * env)
|
||||
{
|
||||
if (g_discoveryManagerClass != nullptr)
|
||||
return;
|
||||
|
||||
g_discoveryManagerClass = jni::GetGlobalClassRef(env, "com/mapswithme/maps/discovery/DiscoveryManager");
|
||||
|
||||
g_discoveryManagerInstanceField = jni::GetStaticFieldID(env, g_discoveryManagerClass, "INSTANCE",
|
||||
"Lcom/mapswithme/maps/discovery/DiscoveryManager;");
|
||||
|
||||
jobject discoveryManagerInstance = env->GetStaticObjectField(g_discoveryManagerClass,
|
||||
g_discoveryManagerInstanceField);
|
||||
|
||||
g_onResultReceivedMethod = jni::GetMethodID(env, discoveryManagerInstance, "onResultReceived",
|
||||
"([Lcom/mapswithme/maps/search/SearchResult;I)V");
|
||||
|
||||
g_onViatorProductsReceivedMethod = jni::GetMethodID(env, discoveryManagerInstance,
|
||||
"onViatorProductsReceived",
|
||||
"([Lcom/mapswithme/maps/viator/ViatorProduct;)V");
|
||||
|
||||
g_onLocalExpertsReceivedMethod = jni::GetMethodID(env, discoveryManagerInstance,
|
||||
"onLocalExpertsReceived",
|
||||
"([Lcom/mapswithme/maps/discovery/LocalExpert;)V");
|
||||
|
||||
g_onErrorMethod = jni::GetMethodID(env, discoveryManagerInstance, "onError", "(I)V");
|
||||
}
|
||||
|
||||
struct DiscoveryCallback
|
||||
{
|
||||
void operator()(uint32_t const requestId, search::Results const & results,
|
||||
discovery::ItemType const type, m2::PointD const & viewportCenter) const
|
||||
{
|
||||
if (g_lastRequestId != requestId)
|
||||
return;
|
||||
|
||||
ASSERT(g_discoveryManagerClass != nullptr, ());
|
||||
JNIEnv * env = jni::GetEnv();
|
||||
|
||||
auto const lat = MercatorBounds::YToLat(viewportCenter.y);
|
||||
auto const lon = MercatorBounds::XToLon(viewportCenter.x);
|
||||
std::vector<bool> customers(results.GetCount(), false);
|
||||
jni::TScopedLocalObjectArrayRef jResults(env, BuildSearchResults(results, customers,
|
||||
true /* hasPosition */,
|
||||
lat, lon));
|
||||
jobject discoveryManagerInstance = env->GetStaticObjectField(g_discoveryManagerClass,
|
||||
g_discoveryManagerInstanceField);
|
||||
env->CallVoidMethod(discoveryManagerInstance, g_onResultReceivedMethod,
|
||||
jResults.get(), static_cast<jint>(type));
|
||||
|
||||
jni::HandleJavaException(env);
|
||||
}
|
||||
|
||||
void operator()(uint32_t const requestId, std::vector<viator::Product> const & products) const
|
||||
{
|
||||
if (g_lastRequestId != requestId)
|
||||
return;
|
||||
|
||||
ASSERT(g_discoveryManagerClass != nullptr, ());
|
||||
JNIEnv * env = jni::GetEnv();
|
||||
|
||||
jni::TScopedLocalObjectArrayRef jProducts(env, ToViatorProductsArray(products));
|
||||
jobject discoveryManagerInstance = env->GetStaticObjectField(g_discoveryManagerClass,
|
||||
g_discoveryManagerInstanceField);
|
||||
env->CallVoidMethod(discoveryManagerInstance, g_onViatorProductsReceivedMethod,
|
||||
jProducts.get());
|
||||
|
||||
jni::HandleJavaException(env);
|
||||
}
|
||||
|
||||
void operator()(uint32_t const requestId, std::vector<locals::LocalExpert> const & experts) const
|
||||
{
|
||||
if (g_lastRequestId != requestId)
|
||||
return;
|
||||
|
||||
ASSERT(g_discoveryManagerClass != nullptr, ());
|
||||
JNIEnv * env = jni::GetEnv();
|
||||
|
||||
jni::TScopedLocalObjectArrayRef jLocals(env, ToLocalExpertsArray(experts));
|
||||
jobject discoveryManagerInstance = env->GetStaticObjectField(g_discoveryManagerClass,
|
||||
g_discoveryManagerInstanceField);
|
||||
env->CallVoidMethod(discoveryManagerInstance, g_onLocalExpertsReceivedMethod, jLocals.get());
|
||||
|
||||
jni::HandleJavaException(env);
|
||||
}
|
||||
};
|
||||
|
||||
void OnDiscoveryError(uint32_t const requestId, discovery::ItemType const type)
|
||||
{
|
||||
if (g_lastRequestId != requestId)
|
||||
return;
|
||||
|
||||
ASSERT(g_discoveryManagerClass != nullptr, ());
|
||||
|
||||
JNIEnv * env = jni::GetEnv();
|
||||
jobject discoveryManagerInstance = env->GetStaticObjectField(g_discoveryManagerClass,
|
||||
g_discoveryManagerInstanceField);
|
||||
env->CallVoidMethod(discoveryManagerInstance, g_onErrorMethod, static_cast<jint>(type));
|
||||
|
||||
jni::HandleJavaException(env);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
extern "C" {
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_mapswithme_maps_discovery_DiscoveryManager_nativeDiscover(JNIEnv * env, jclass,
|
||||
jobject params)
|
||||
{
|
||||
PrepareClassRefs(env);
|
||||
|
||||
discovery::ClientParams p;
|
||||
auto const paramsClass = env->GetObjectClass(params);
|
||||
static auto const currencyField = env->GetFieldID(paramsClass, "mCurrency", "Ljava/lang/String;");
|
||||
{
|
||||
auto const currency = static_cast<jstring>(env->GetObjectField(params, currencyField));
|
||||
string const res = jni::ToNativeString(env, currency);
|
||||
if (!res.empty())
|
||||
p.m_currency = res;
|
||||
}
|
||||
|
||||
static auto const langField = env->GetFieldID(paramsClass, "mLang", "Ljava/lang/String;");
|
||||
{
|
||||
auto const lang = static_cast<jstring>(env->GetObjectField(params, langField));
|
||||
string const res = jni::ToNativeString(env, lang);
|
||||
if (!res.empty())
|
||||
p.m_lang = res;
|
||||
}
|
||||
|
||||
static auto const itemsCountField = env->GetFieldID(paramsClass, "mItemsCount", "I");
|
||||
{
|
||||
auto const count = env->GetIntField(params, itemsCountField);
|
||||
ASSERT_GREATER(count, 0, ());
|
||||
p.m_itemsCount = static_cast<size_t>(count);
|
||||
}
|
||||
|
||||
static auto const itemTypesField = env->GetFieldID(paramsClass, "mItemTypes", "[I");
|
||||
{
|
||||
auto const array = static_cast<jintArray>(env->GetObjectField(params, itemTypesField));
|
||||
auto const length = env->GetArrayLength(array);
|
||||
ASSERT_GREATER(length, 0, ());
|
||||
|
||||
auto const dtor = [array, env](jint * data) { env->ReleaseIntArrayElements(array, data, 0); };
|
||||
std::unique_ptr<jint, decltype(dtor)> data{env->GetIntArrayElements(array, nullptr), dtor};
|
||||
|
||||
std::vector<discovery::ItemType> itemTypes;
|
||||
for (jsize i = 0; i < length; ++i)
|
||||
itemTypes.emplace_back(static_cast<discovery::ItemType>(data.get()[i]));
|
||||
|
||||
p.m_itemTypes = std::move(itemTypes);
|
||||
}
|
||||
|
||||
g_lastRequestId = g_framework->NativeFramework()->Discover(std::move(p), DiscoveryCallback(),
|
||||
std::bind(&OnDiscoveryError, _1, _2));
|
||||
}
|
||||
} // extern "C"
|
|
@ -1,8 +1,7 @@
|
|||
#include "android/jni/com/mapswithme/core/jni_helper.hpp"
|
||||
#include "android/jni/com/mapswithme/maps/Framework.hpp"
|
||||
#include "com/mapswithme/maps/discovery/Locals.hpp"
|
||||
#include "com/mapswithme/maps/Framework.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
@ -48,15 +47,12 @@ void PrepareClassRefs(JNIEnv * env)
|
|||
g_localErrorConstructor = jni::GetConstructorID(env, g_localErrorClass,
|
||||
"(ILjava/lang/String;)V");
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void OnLocalsSuccess(uint64_t requestId, std::vector<locals::LocalExpert> const & locals,
|
||||
size_t pageNumber, size_t countPerPage, bool hasPreviousPage,
|
||||
bool hasNextPage)
|
||||
jobjectArray ToLocalExpertsArray(std::vector<locals::LocalExpert> const & locals)
|
||||
{
|
||||
if (g_lastRequestId != requestId)
|
||||
return;
|
||||
|
||||
JNIEnv * env = jni::GetEnv();
|
||||
PrepareClassRefs(env);
|
||||
|
||||
auto const localExpertBuilder = [](JNIEnv * env, locals::LocalExpert const & expert)
|
||||
{
|
||||
|
@ -77,9 +73,19 @@ void OnLocalsSuccess(uint64_t requestId, std::vector<locals::LocalExpert> const
|
|||
jOfferDescription.get(), jPageUrl.get(), jPhotoUrl.get());
|
||||
};
|
||||
|
||||
jni::TScopedLocalObjectArrayRef jLocals(env, jni::ToJavaArray(env, g_localExpertClass, locals,
|
||||
localExpertBuilder));
|
||||
return jni::ToJavaArray(env, g_localExpertClass, locals, localExpertBuilder);
|
||||
|
||||
}
|
||||
|
||||
void OnLocalsSuccess(uint64_t requestId, std::vector<locals::LocalExpert> const & locals,
|
||||
size_t pageNumber, size_t countPerPage, bool hasPreviousPage,
|
||||
bool hasNextPage)
|
||||
{
|
||||
if (g_lastRequestId != requestId)
|
||||
return;
|
||||
|
||||
JNIEnv * env = jni::GetEnv();
|
||||
jni::TScopedLocalObjectArrayRef jLocals(env, ToLocalExpertsArray(locals));
|
||||
env->CallVoidMethod(g_localsInstance, g_onLocalsReceivedMethod, jLocals.get());
|
||||
|
||||
jni::HandleJavaException(env);
|
||||
|
@ -100,12 +106,11 @@ void OnLocalsError(uint64_t requestId, int errorCode, std::string const & errorM
|
|||
|
||||
jni::HandleJavaException(env);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
extern "C" {
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_mapswithme_maps_locals_Locals_nativeRequestLocals(JNIEnv * env, jclass clazz,
|
||||
Java_com_mapswithme_maps_discovery_Locals_nativeRequestLocals(JNIEnv * env, jclass clazz,
|
||||
jobject policy, jdouble lat,
|
||||
jdouble lon)
|
||||
{
|
9
android/jni/com/mapswithme/maps/discovery/Locals.hpp
Normal file
9
android/jni/com/mapswithme/maps/discovery/Locals.hpp
Normal file
|
@ -0,0 +1,9 @@
|
|||
#pragma once
|
||||
|
||||
#include "com/mapswithme/core/jni_helper.hpp"
|
||||
|
||||
#include "partners_api/locals_api.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
extern jobjectArray ToLocalExpertsArray(std::vector<locals::LocalExpert> const & locals);
|
|
@ -1,11 +1,11 @@
|
|||
#include "com/mapswithme/maps/viator/Viator.hpp"
|
||||
#include "com/mapswithme/maps/Framework.hpp"
|
||||
|
||||
#include "com/mapswithme/core/jni_helper.hpp"
|
||||
#include "partners_api/viator_api.hpp"
|
||||
#include <string>
|
||||
|
||||
namespace
|
||||
{
|
||||
jclass g_viatorClass;
|
||||
jclass g_viatorClass = nullptr;
|
||||
jclass g_viatorProductClass;
|
||||
jmethodID g_viatorProductConstructor;
|
||||
jmethodID g_viatorCallback;
|
||||
|
@ -13,7 +13,7 @@ std::string g_lastDestId;
|
|||
|
||||
void PrepareClassRefs(JNIEnv * env)
|
||||
{
|
||||
if (g_viatorClass)
|
||||
if (g_viatorClass != nullptr)
|
||||
return;
|
||||
|
||||
g_viatorClass = jni::GetGlobalClassRef(env, "com/mapswithme/maps/viator/Viator");
|
||||
|
@ -26,6 +26,28 @@ void PrepareClassRefs(JNIEnv * env)
|
|||
jni::GetStaticMethodID(env, g_viatorClass, "onViatorProductsReceived",
|
||||
"(Ljava/lang/String;[Lcom/mapswithme/maps/viator/ViatorProduct;)V");
|
||||
}
|
||||
} // namespace
|
||||
|
||||
jobjectArray ToViatorProductsArray(std::vector<viator::Product> const & products)
|
||||
{
|
||||
JNIEnv * env = jni::GetEnv();
|
||||
PrepareClassRefs(env);
|
||||
|
||||
return jni::ToJavaArray(env, g_viatorProductClass, products,
|
||||
[](JNIEnv * env, viator::Product const & item)
|
||||
{
|
||||
jni::TScopedLocalRef jTitle(env, jni::ToJavaString(env, item.m_title));
|
||||
jni::TScopedLocalRef jDuration(env, jni::ToJavaString(env, item.m_duration));
|
||||
jni::TScopedLocalRef jPriceFormatted(env, jni::ToJavaString(env, item.m_priceFormatted));
|
||||
jni::TScopedLocalRef jCurrency(env, jni::ToJavaString(env, item.m_currency));
|
||||
jni::TScopedLocalRef jPhotoUrl(env, jni::ToJavaString(env, item.m_photoUrl));
|
||||
jni::TScopedLocalRef jPageUrl(env, jni::ToJavaString(env, item.m_pageUrl));
|
||||
return env->NewObject(g_viatorProductClass, g_viatorProductConstructor, jTitle.get(),
|
||||
item.m_rating, item.m_reviewCount, jDuration.get(), item.m_price,
|
||||
jPriceFormatted.get(), jCurrency.get(), jPhotoUrl.get(),
|
||||
jPageUrl.get());
|
||||
});
|
||||
}
|
||||
|
||||
void OnViatorProductsReceived(std::string const & destId,
|
||||
std::vector<viator::Product> const & products)
|
||||
|
@ -34,27 +56,10 @@ void OnViatorProductsReceived(std::string const & destId,
|
|||
return;
|
||||
|
||||
JNIEnv * env = jni::GetEnv();
|
||||
|
||||
jni::TScopedLocalRef jDestId(env, jni::ToJavaString(env, destId));
|
||||
jni::TScopedLocalRef jProducts(
|
||||
env,
|
||||
jni::ToJavaArray(env, g_viatorProductClass, products, [](JNIEnv * env,
|
||||
viator::Product const & item) {
|
||||
jni::TScopedLocalRef jTitle(env, jni::ToJavaString(env, item.m_title));
|
||||
jni::TScopedLocalRef jDuration(env, jni::ToJavaString(env, item.m_duration));
|
||||
jni::TScopedLocalRef jPriceFormatted(env, jni::ToJavaString(env, item.m_priceFormatted));
|
||||
jni::TScopedLocalRef jCurrency(env, jni::ToJavaString(env, item.m_currency));
|
||||
jni::TScopedLocalRef jPhotoUrl(env, jni::ToJavaString(env, item.m_photoUrl));
|
||||
jni::TScopedLocalRef jPageUrl(env, jni::ToJavaString(env, item.m_pageUrl));
|
||||
return env->NewObject(g_viatorProductClass, g_viatorProductConstructor, jTitle.get(),
|
||||
item.m_rating, item.m_reviewCount, jDuration.get(), item.m_price,
|
||||
jPriceFormatted.get(), jCurrency.get(), jPhotoUrl.get(),
|
||||
jPageUrl.get());
|
||||
}));
|
||||
|
||||
jni::TScopedLocalObjectArrayRef jProducts(env, ToViatorProductsArray(products));
|
||||
env->CallStaticVoidMethod(g_viatorClass, g_viatorCallback, jDestId.get(), jProducts.get());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
extern "C" {
|
||||
|
||||
|
|
9
android/jni/com/mapswithme/maps/viator/Viator.hpp
Normal file
9
android/jni/com/mapswithme/maps/viator/Viator.hpp
Normal file
|
@ -0,0 +1,9 @@
|
|||
#pragma once
|
||||
|
||||
#include "com/mapswithme/core/jni_helper.hpp"
|
||||
|
||||
#include "partners_api/viator_api.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
extern jobjectArray ToViatorProductsArray(std::vector<viator::Product> const & products);
|
|
@ -384,6 +384,4 @@ public class Framework
|
|||
public static native void nativeAuthenticateUser(@NonNull String socialToken,
|
||||
@SocialTokenType int socialTokenType);
|
||||
public static native boolean nativeIsUserAuthenticated();
|
||||
|
||||
public static native void nativeDiscover(@NonNull DiscoveryParams params);
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@ import android.support.annotation.MainThread;
|
|||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import com.mapswithme.maps.Framework;
|
||||
import com.mapswithme.maps.search.SearchResult;
|
||||
import com.mapswithme.maps.viator.ViatorProduct;
|
||||
|
||||
|
@ -17,7 +16,7 @@ enum DiscoveryManager
|
|||
|
||||
public void discover(@NonNull DiscoveryParams params)
|
||||
{
|
||||
Framework.nativeDiscover(params);
|
||||
DiscoveryManager.nativeDiscover(params);
|
||||
}
|
||||
|
||||
// Called from JNI.
|
||||
|
@ -60,6 +59,13 @@ enum DiscoveryManager
|
|||
mCallback.onLocalExpertsReceived(experts);
|
||||
}
|
||||
|
||||
// Called from JNI.
|
||||
@MainThread
|
||||
private void onError(@DiscoveryParams.ItemType int type)
|
||||
{
|
||||
// TODO: not implemented yet.
|
||||
}
|
||||
|
||||
void attach(@NonNull UICallback callback)
|
||||
{
|
||||
mCallback = callback;
|
||||
|
@ -69,4 +75,6 @@ enum DiscoveryManager
|
|||
{
|
||||
mCallback = null;
|
||||
}
|
||||
|
||||
public static native void nativeDiscover(@NonNull DiscoveryParams params);
|
||||
}
|
||||
|
|
|
@ -49,13 +49,14 @@ public:
|
|||
ItemTypes m_itemTypes;
|
||||
};
|
||||
|
||||
using ErrorCalback = std::function<void(ItemType const type)>;
|
||||
using ErrorCalback = std::function<void(uint32_t const requestId, ItemType const type)>;
|
||||
|
||||
Manager(Index const & index, search::CityFinder & cityFinder, APIs const & apis);
|
||||
|
||||
template <typename ResultCallback>
|
||||
void Discover(Params && params, ResultCallback const & onResult, ErrorCalback const & onError)
|
||||
uint32_t Discover(Params && params, ResultCallback const & onResult, ErrorCalback const & onError)
|
||||
{
|
||||
uint32_t const requestId = ++m_requestCounter;
|
||||
ASSERT_THREAD_CHECKER(m_threadChecker, ());
|
||||
auto const & types = params.m_itemTypes;
|
||||
ASSERT(!types.empty(), ("Types must contain at least one element."));
|
||||
|
@ -69,17 +70,17 @@ public:
|
|||
std::string const sponsoredId = GetCityViatorId(params.m_viewportCenter);
|
||||
if (sponsoredId.empty())
|
||||
{
|
||||
onError(type);
|
||||
onError(requestId, type);
|
||||
break;
|
||||
}
|
||||
|
||||
m_viatorApi.GetTop5Products(
|
||||
sponsoredId, params.m_curency,
|
||||
[this, sponsoredId, onResult](std::string const & destId,
|
||||
std::vector<viator::Product> const & products) {
|
||||
[this, requestId, sponsoredId, onResult](std::string const & destId,
|
||||
std::vector<viator::Product> const & products) {
|
||||
ASSERT_THREAD_CHECKER(m_threadChecker, ());
|
||||
if (destId == sponsoredId)
|
||||
onResult(products);
|
||||
onResult(requestId, products);
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
@ -87,11 +88,14 @@ public:
|
|||
case ItemType::Cafes:
|
||||
{
|
||||
auto p = GetSearchParams(params, type);
|
||||
p.m_onResults = [onResult, type](search::Results const & results) {
|
||||
auto const viewportCenter = params.m_viewportCenter;
|
||||
p.m_onResults = [requestId, onResult, type, viewportCenter](search::Results const & results) {
|
||||
if (!results.IsEndMarker())
|
||||
return;
|
||||
GetPlatform().RunTask(Platform::Thread::Gui,
|
||||
[onResult, type, results] { onResult(results, type); });
|
||||
[requestId, onResult, type, results, viewportCenter] {
|
||||
onResult(requestId, results, type, viewportCenter);
|
||||
});
|
||||
};
|
||||
m_searchApi.GetEngine().Search(p);
|
||||
break;
|
||||
|
@ -107,20 +111,21 @@ public:
|
|||
auto constexpr pageNumber = 1;
|
||||
m_localsApi.GetLocals(
|
||||
latLon.lat, latLon.lon, params.m_lang, params.m_itemsCount, pageNumber,
|
||||
[this, onResult](uint64_t id, std::vector<locals::LocalExpert> const & locals,
|
||||
size_t /* pageNumber */, size_t /* countPerPage */,
|
||||
bool /* hasPreviousPage */, bool /* hasNextPage */) {
|
||||
[this, requestId, onResult](uint64_t id, std::vector<locals::LocalExpert> const & locals,
|
||||
size_t /* pageNumber */, size_t /* countPerPage */,
|
||||
bool /* hasPreviousPage */, bool /* hasNextPage */) {
|
||||
ASSERT_THREAD_CHECKER(m_threadChecker, ());
|
||||
onResult(locals);
|
||||
onResult(requestId, locals);
|
||||
},
|
||||
[this, onError, type](uint64_t id, int errorCode, std::string const & errorMessage) {
|
||||
[this, requestId, onError, type](uint64_t id, int errorCode, std::string const & errorMessage) {
|
||||
ASSERT_THREAD_CHECKER(m_threadChecker, ());
|
||||
onError(type);
|
||||
onError(requestId, type);
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return requestId;
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -132,6 +137,7 @@ private:
|
|||
SearchAPI & m_searchApi;
|
||||
viator::Api const & m_viatorApi;
|
||||
locals::Api & m_localsApi;
|
||||
uint32_t m_requestCounter = 0;
|
||||
ThreadChecker m_threadChecker;
|
||||
};
|
||||
} // namespace discovery
|
||||
|
|
|
@ -749,11 +749,11 @@ public:
|
|||
|
||||
public:
|
||||
template <typename ResultCallback>
|
||||
void Discover(discovery::ClientParams && params, ResultCallback const & onResult,
|
||||
discovery::Manager::ErrorCalback const & onError) const
|
||||
uint32_t Discover(discovery::ClientParams && params, ResultCallback const & onResult,
|
||||
discovery::Manager::ErrorCalback const & onError) const
|
||||
{
|
||||
CHECK(m_discoveryManager.get(), ());
|
||||
m_discoveryManager->Discover(GetDiscoveryParams(move(params)), onResult, onError);
|
||||
return m_discoveryManager->Discover(GetDiscoveryParams(move(params)), onResult, onError);
|
||||
}
|
||||
|
||||
discovery::Manager::Params GetDiscoveryParams(discovery::ClientParams && clientParams) const;
|
||||
|
|
Loading…
Add table
Reference in a new issue