forked from organicmaps/organicmaps
Added bookmarks catalog pinger
This commit is contained in:
parent
37797b6f3d
commit
7466969a82
4 changed files with 88 additions and 0 deletions
|
@ -46,6 +46,8 @@ jmethodID g_catalogCustomPropertyOptionConstructor;
|
|||
jclass g_catalogCustomPropertyClass;
|
||||
jmethodID g_catalogCustomPropertyConstructor;
|
||||
|
||||
jmethodID g_onPingFinishedMethod;
|
||||
|
||||
void PrepareClassRefs(JNIEnv * env)
|
||||
{
|
||||
if (g_bookmarkManagerClass)
|
||||
|
@ -95,6 +97,8 @@ void PrepareClassRefs(JNIEnv * env)
|
|||
g_onUploadFinishedMethod =
|
||||
jni::GetMethodID(env, bookmarkManagerInstance, "onUploadFinished", "(ILjava/lang/String;JJ)V");
|
||||
|
||||
g_onPingFinishedMethod = jni::GetMethodID(env, bookmarkManagerInstance, "onPingFinished", "(Z)V");
|
||||
|
||||
g_bookmarkCategoryClass =
|
||||
jni::GetGlobalClassRef(env, "com/mapswithme/maps/bookmarks/data/BookmarkCategory");
|
||||
//public BookmarkCategory(long id,
|
||||
|
@ -351,6 +355,17 @@ void OnCustomPropertiesReceived(JNIEnv * env, bool successful,
|
|||
jni::HandleJavaException(env);
|
||||
}
|
||||
|
||||
void OnPingFinished(JNIEnv * env, bool isSuccessful)
|
||||
{
|
||||
ASSERT(g_bookmarkManagerClass, ());
|
||||
|
||||
jobject bookmarkManagerInstance = env->GetStaticObjectField(g_bookmarkManagerClass,
|
||||
g_bookmarkManagerInstanceField);
|
||||
env->CallVoidMethod(bookmarkManagerInstance, g_onPingFinishedMethod,
|
||||
static_cast<jboolean>(isSuccessful));
|
||||
jni::HandleJavaException(env);
|
||||
}
|
||||
|
||||
void OnUploadStarted(JNIEnv * env, kml::MarkGroupId originCategoryId)
|
||||
{
|
||||
ASSERT(g_bookmarkManagerClass, ());
|
||||
|
@ -908,6 +923,17 @@ Java_com_mapswithme_maps_bookmarks_data_BookmarkManager_nativeRequestCatalogCust
|
|||
});
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_mapswithme_maps_bookmarks_data_BookmarkManager_nativePingBookmarkCatalog(
|
||||
JNIEnv * env, jobject)
|
||||
{
|
||||
auto & bm = frm()->GetBookmarkManager();
|
||||
bm.GetCatalog().Ping([env](bool isSuccessful)
|
||||
{
|
||||
OnPingFinished(env, isSuccessful);
|
||||
});
|
||||
}
|
||||
|
||||
JNIEXPORT jobjectArray JNICALL
|
||||
Java_com_mapswithme_maps_bookmarks_data_BookmarkManager_nativeGetBookmarkCategories(JNIEnv *env, jobject thiz)
|
||||
{
|
||||
|
|
|
@ -65,6 +65,9 @@ public enum BookmarkManager
|
|||
|
||||
@NonNull
|
||||
private final List<BookmarksCatalogListener> mCatalogListeners = new ArrayList<>();
|
||||
|
||||
@NonNull
|
||||
private final List<BookmarksCatalogPingListener> mCatalogPingListeners = new ArrayList<>();
|
||||
|
||||
static
|
||||
{
|
||||
|
@ -314,6 +317,15 @@ public enum BookmarkManager
|
|||
}
|
||||
}
|
||||
|
||||
// Called from JNI.
|
||||
@SuppressWarnings("unused")
|
||||
@MainThread
|
||||
public void onPingFinished(boolean serviceIsAvailable)
|
||||
{
|
||||
for (BookmarksCatalogPingListener listener : mCatalogPingListeners)
|
||||
listener.onPingFinished(serviceIsAvailable);
|
||||
}
|
||||
|
||||
public boolean isVisible(long catId)
|
||||
{
|
||||
return nativeIsVisible(catId);
|
||||
|
@ -643,6 +655,11 @@ public enum BookmarkManager
|
|||
nativeRequestCatalogCustomProperties();
|
||||
}
|
||||
|
||||
public void pingBookmarkCatalog()
|
||||
{
|
||||
nativePingBookmarkCatalog();
|
||||
}
|
||||
|
||||
public boolean isCategoryFromCatalog(long catId)
|
||||
{
|
||||
return nativeIsCategoryFromCatalog(catId);
|
||||
|
@ -785,6 +802,8 @@ public enum BookmarkManager
|
|||
|
||||
private static native void nativeRequestCatalogCustomProperties();
|
||||
|
||||
private static native void nativePingBookmarkCatalog();
|
||||
|
||||
public interface BookmarksLoadingListener
|
||||
{
|
||||
void onBookmarksLoadingStarted();
|
||||
|
@ -840,6 +859,11 @@ public enum BookmarkManager
|
|||
void onRestoredFilesPrepared();
|
||||
}
|
||||
|
||||
public interface BookmarksCatalogPingListener
|
||||
{
|
||||
void onPingFinished(boolean serviceIsAvailable);
|
||||
}
|
||||
|
||||
public interface BookmarksCatalogListener
|
||||
{
|
||||
/**
|
||||
|
|
|
@ -70,6 +70,13 @@ std::string BuildWebEditorUrl(std::string const & serverId, std::string const &
|
|||
return kCatalogEditorServer + "webeditor/" + language + "/edit/" + serverId;
|
||||
}
|
||||
|
||||
std::string BuildPingUrl()
|
||||
{
|
||||
if (kCatalogFrontendServer.empty())
|
||||
return {};
|
||||
return kCatalogFrontendServer + "storage/ping";
|
||||
}
|
||||
|
||||
struct SubtagData
|
||||
{
|
||||
std::string m_name;
|
||||
|
@ -591,6 +598,34 @@ void BookmarkCatalog::Upload(UploadData uploadData, std::string const & accessTo
|
|||
});
|
||||
}
|
||||
|
||||
void BookmarkCatalog::Ping(PingCallback && callback) const
|
||||
{
|
||||
auto const url = BuildPingUrl();
|
||||
if (url.empty())
|
||||
{
|
||||
if (callback)
|
||||
callback(false /* isSuccessful */);
|
||||
return;
|
||||
}
|
||||
|
||||
GetPlatform().RunTask(Platform::Thread::Network, [url, callback = std::move(callback)]()
|
||||
{
|
||||
platform::HttpClient request(url);
|
||||
request.SetRawHeader("User-Agent", GetPlatform().GetAppUserAgent());
|
||||
uint32_t constexpr kPingTimeoutInSec = 15;
|
||||
request.SetTimeout(kPingTimeoutInSec);
|
||||
if (request.RunHttpRequest())
|
||||
{
|
||||
static std::string const kExpectedResponse = "pong";
|
||||
auto const resultCode = request.ErrorCode();
|
||||
if (callback && resultCode >= 200 && resultCode < 300 && request.ServerResponse() == kExpectedResponse)
|
||||
callback(true /* isSuccessful */);
|
||||
}
|
||||
if (callback)
|
||||
callback(false /* isSuccessful */);
|
||||
});
|
||||
}
|
||||
|
||||
void BookmarkCatalog::SetInvalidTokenHandler(InvalidTokenHandler && onInvalidToken)
|
||||
{
|
||||
m_onInvalidToken = std::move(onInvalidToken);
|
||||
|
|
|
@ -113,6 +113,9 @@ public:
|
|||
UploadSuccessCallback && uploadSuccessCallback,
|
||||
UploadErrorCallback && uploadErrorCallback);
|
||||
|
||||
using PingCallback = platform::SafeCallback<void(bool isSuccessful)>;
|
||||
void Ping(PingCallback && callback) const;
|
||||
|
||||
// Handler can be called from non-UI thread.
|
||||
void SetInvalidTokenHandler(InvalidTokenHandler && onInvalidToken);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue