[android] Implemented 'ErrorCalback' for Uber in JNI layer

This commit is contained in:
alexzatsepin 2016-10-26 16:29:08 +03:00
parent 6348e02a11
commit 52890356c8
7 changed files with 77 additions and 26 deletions

View file

@ -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);
}

View file

@ -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);
};
}

View file

@ -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(

View file

@ -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));
}
}

View file

@ -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
}
}

View file

@ -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

View file

@ -101,4 +101,6 @@ private:
shared_ptr<ProductMaker> m_maker = make_shared<ProductMaker>();
uint64_t m_requestId = 0;
};
string DebugPrint(ErrorCode error);
} // namespace uber