diff options
author | alexzatsepin <alexander.zatzepin@gmail.com> | 2016-10-26 16:29:08 +0300 |
---|---|---|
committer | alexzatsepin <alexander.zatzepin@gmail.com> | 2016-10-31 13:10:36 +0300 |
commit | 52890356c8ba1a8b24b0e0670243d60e5063b347 (patch) | |
tree | bbaf954e3927ab8923fdbd73b1e3bea1df6a66d9 | |
parent | 6348e02a11facc5d40fb66c7e9078124618cc1d7 (diff) |
[android] Implemented 'ErrorCalback' for Uber in JNI layer
-rw-r--r-- | android/jni/com/mapswithme/maps/Framework.cpp | 4 | ||||
-rw-r--r-- | android/jni/com/mapswithme/maps/Framework.hpp | 4 | ||||
-rw-r--r-- | android/jni/com/mapswithme/maps/uber/Uber.cpp | 69 | ||||
-rw-r--r-- | android/src/com/mapswithme/maps/routing/RoutingController.java | 10 | ||||
-rw-r--r-- | android/src/com/mapswithme/maps/uber/Uber.java | 5 | ||||
-rw-r--r-- | partners_api/uber_api.cpp | 9 | ||||
-rw-r--r-- | partners_api/uber_api.hpp | 2 |
7 files changed, 77 insertions, 26 deletions
diff --git a/android/jni/com/mapswithme/maps/Framework.cpp b/android/jni/com/mapswithme/maps/Framework.cpp index d112688ae0..002967850a 100644 --- a/android/jni/com/mapswithme/maps/Framework.cpp +++ b/android/jni/com/mapswithme/maps/Framework.cpp @@ -514,9 +514,9 @@ void Framework::EnableDownloadOn3g() } uint64_t Framework::RequestUberProducts(ms::LatLon const & from, ms::LatLon const & to, - uber::ProductsCallback const & callback) + uber::ProductsCallback const & callback, + uber::ErrorCallback const & errorCallback) { - auto const errorCallback = [](uber::ErrorCode const code, uint64_t const requestId) {}; return m_work.GetUberApi().GetAvailableProducts(from, to, callback, errorCallback); } diff --git a/android/jni/com/mapswithme/maps/Framework.hpp b/android/jni/com/mapswithme/maps/Framework.hpp index 33ed3baeef..88d7bd25f7 100644 --- a/android/jni/com/mapswithme/maps/Framework.hpp +++ b/android/jni/com/mapswithme/maps/Framework.hpp @@ -167,7 +167,9 @@ namespace android bool IsDownloadOn3gEnabled(); void EnableDownloadOn3g(); - uint64_t RequestUberProducts(ms::LatLon const & from, ms::LatLon const & to, uber::ProductsCallback const & callback); + uint64_t RequestUberProducts(ms::LatLon const & from, ms::LatLon const & to, + uber::ProductsCallback const & callback, + uber::ErrorCallback const & errorCallback); static uber::RideRequestLinks GetUberLinks(string const & productId, ms::LatLon const & from, ms::LatLon const & to); }; } diff --git a/android/jni/com/mapswithme/maps/uber/Uber.cpp b/android/jni/com/mapswithme/maps/uber/Uber.cpp index 172fc29bd4..13b4500068 100644 --- a/android/jni/com/mapswithme/maps/uber/Uber.cpp +++ b/android/jni/com/mapswithme/maps/uber/Uber.cpp @@ -44,6 +44,51 @@ void PrepareClassRefs(JNIEnv * env) g_uberLinksConstructor = jni::GetConstructorID(env, g_uberLinksClass, "(Ljava/lang/String;Ljava/lang/String;)V"); } + +void OnUberInfoReceived(vector<uber::Product> const & products, uint64_t const requestId) +{ + GetPlatform().RunOnGuiThread([=]() { + if (g_lastRequestId != requestId) + return; + + CHECK(!products.empty(), ("List of the products cannot be empty")); + + JNIEnv * env = jni::GetEnv(); + + auto const uberProducts = jni::ToJavaArray( + env, g_productClass, products, [](JNIEnv * env, uber::Product const & item) { + return env->NewObject( + g_productClass, g_productConstructor, jni::ToJavaString(env, item.m_productId), + jni::ToJavaString(env, item.m_name), jni::ToJavaString(env, item.m_time), + jni::ToJavaString(env, item.m_price)); + }); + jobject const routingControllerInstance = + env->CallStaticObjectMethod(g_routingControllerClass, g_routingControllerGetMethod); + env->CallVoidMethod(routingControllerInstance, g_uberInfoCallbackMethod, + env->NewObject(g_uberInfoClass, g_uberInfoConstructor, uberProducts)); + }); +} + +void OnUberError(uber::ErrorCode const code, uint64_t const requestId) +{ + GetPlatform().RunOnGuiThread([=]() { + if (g_lastRequestId != requestId) + return; + + JNIEnv * env = jni::GetEnv(); + + static jclass const errCodeClass = env->FindClass("com/mapswithme/maps/uber/Uber$ErrorCode"); + ASSERT(errCodeClass, ()); + + jobject const routingControllerInstance = + env->CallStaticObjectMethod(g_routingControllerClass, g_routingControllerGetMethod); + static jmethodID const uberErrorMethod = + jni::GetMethodID(env, routingControllerInstance, "onUberError", "(Ljava/lang/String;)V"); + + env->CallVoidMethod(routingControllerInstance, uberErrorMethod, + jni::ToJavaString(env, uber::DebugPrint(code))); + }); +} } // namespace extern "C" { @@ -56,29 +101,7 @@ JNIEXPORT void JNICALL Java_com_mapswithme_maps_uber_Uber_nativeRequestUberProdu ms::LatLon const from(srcLat, srcLon); ms::LatLon const to(dstLat, dstLon); - g_lastRequestId = g_framework->RequestUberProducts( - from, to, [](vector<uber::Product> const & products, uint64_t const requestId) { - GetPlatform().RunOnGuiThread([=]() { - - if (g_lastRequestId != requestId) - return; - - JNIEnv * env = jni::GetEnv(); - - auto uberProducts = jni::ToJavaArray( - env, g_productClass, products, [](JNIEnv * env, uber::Product const & item) { - return env->NewObject( - g_productClass, g_productConstructor, jni::ToJavaString(env, item.m_productId), - jni::ToJavaString(env, item.m_name), jni::ToJavaString(env, item.m_time), - jni::ToJavaString(env, item.m_price)); - }); - - jobject const routingControllerInstance = - env->CallStaticObjectMethod(g_routingControllerClass, g_routingControllerGetMethod); - env->CallVoidMethod(routingControllerInstance, g_uberInfoCallbackMethod, - env->NewObject(g_uberInfoClass, g_uberInfoConstructor, uberProducts)); - }); - }); + g_lastRequestId = g_framework->RequestUberProducts(from, to, &OnUberInfoReceived, &OnUberError); } JNIEXPORT jobject JNICALL Java_com_mapswithme_maps_uber_Uber_nativeGetUberLinks( diff --git a/android/src/com/mapswithme/maps/routing/RoutingController.java b/android/src/com/mapswithme/maps/routing/RoutingController.java index 76c4a10a03..66180e1eb5 100644 --- a/android/src/com/mapswithme/maps/routing/RoutingController.java +++ b/android/src/com/mapswithme/maps/routing/RoutingController.java @@ -808,4 +808,14 @@ public class RoutingController mContainer.updateMenu(); } } + + /** + * Called from the native code + * @param errorCode must match the one of the values in {@link com.mapswithme.maps.uber.Uber.ErrorCode} + */ + @MainThread + private void onUberError(@NonNull String errorCode) + { + mLogger.e("onUberError error = " + Uber.ErrorCode.valueOf(errorCode)); + } } diff --git a/android/src/com/mapswithme/maps/uber/Uber.java b/android/src/com/mapswithme/maps/uber/Uber.java index 538c01cd84..9ef688e812 100644 --- a/android/src/com/mapswithme/maps/uber/Uber.java +++ b/android/src/com/mapswithme/maps/uber/Uber.java @@ -9,4 +9,9 @@ public class Uber @NonNull public static native UberLinks nativeGetUberLinks(@NonNull String productId, double srcLon, double srcLat, double dstLat, double dstLon); + + public enum ErrorCode + { + NoProducts, RemoteError + } } diff --git a/partners_api/uber_api.cpp b/partners_api/uber_api.cpp index c03cdaaea6..076f9d693e 100644 --- a/partners_api/uber_api.cpp +++ b/partners_api/uber_api.cpp @@ -249,4 +249,13 @@ RideRequestLinks Api::GetRideRequestLinks(string const & productId, ms::LatLon c return {"uber://" + url.str(), "https://m.uber.com/ul" + url.str()}; } + +string DebugPrint(ErrorCode error) +{ + switch (error) + { + case ErrorCode::NoProducts: return "NoProducts"; + case ErrorCode::RemoteError: return "RemoteError"; + } +} } // namespace uber diff --git a/partners_api/uber_api.hpp b/partners_api/uber_api.hpp index ada7db24aa..f25a0cbf62 100644 --- a/partners_api/uber_api.hpp +++ b/partners_api/uber_api.hpp @@ -101,4 +101,6 @@ private: shared_ptr<ProductMaker> m_maker = make_shared<ProductMaker>(); uint64_t m_requestId = 0; }; + +string DebugPrint(ErrorCode error); } // namespace uber |