From f6e8f27fbaafe35981673f1ce3276a5c31559ebe Mon Sep 17 00:00:00 2001 From: Dmitry Yunitsky Date: Sun, 13 Jul 2014 13:21:35 +0200 Subject: [PATCH] Added name to GuideInfo and method for retrieving guide by id. --- android/jni/com/mapswithme/maps/Framework.cpp | 37 ++++++++++++++++++- storage/guides.cpp | 31 +++++++++++++--- storage/guides.hpp | 7 +++- storage/storage_tests/guides_tests.cpp | 1 + 4 files changed, 67 insertions(+), 9 deletions(-) diff --git a/android/jni/com/mapswithme/maps/Framework.cpp b/android/jni/com/mapswithme/maps/Framework.cpp index b10baea05f..b67af72bd5 100644 --- a/android/jni/com/mapswithme/maps/Framework.cpp +++ b/android/jni/com/mapswithme/maps/Framework.cpp @@ -867,7 +867,7 @@ extern "C" { m_giClass = m_env->FindClass("com/mapswithme/maps/guides/GuideInfo"); m_methodID = m_env->GetMethodID(m_giClass, - "", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); + "", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); m_lang = languages::CurrentLanguage(); } @@ -879,7 +879,8 @@ extern "C" jni::ToJavaString(m_env, info.GetAppID()), jni::ToJavaString(m_env, info.GetURL()), jni::ToJavaString(m_env, info.GetAdTitle(m_lang)), - jni::ToJavaString(m_env, info.GetAdMessage(m_lang))); + jni::ToJavaString(m_env, info.GetAdMessage(m_lang)), + jni::ToJavaString(m_env, info.GetName())); } }; @@ -907,6 +908,38 @@ extern "C" return g_framework->NativeFramework()->GetGuidesManager().WasAdvertised(jni::ToNativeString(env, appId)); } + JNIEXPORT jobjectArray JNICALL + Java_com_mapswithme_maps_Framework_getGuideIds(JNIEnv * env, jclass clazz) + { + std::set guides; + g_framework->NativeFramework()->GetGuidesManager().GetGuidesId(guides); + + jclass klass = env->FindClass("java/lang/String"); + ASSERT ( klass, () ); + + jobjectArray arr = env->NewObjectArray(guides.size(), klass, 0); + + set::iterator it; + int i = 0; + for (it = guides.begin(); it != guides.end(); ++it) + { + string guideId = *it; + env->SetObjectArrayElement(arr, i++, jni::ToJavaString(env, guideId)); + } + + return arr; + } + + JNIEXPORT jobject JNICALL + Java_com_mapswithme_maps_Framework_getGuideById(JNIEnv * env, jclass clazz, jstring guideId) + { + guides::GuideInfo info; + if (g_framework->NativeFramework()->GetGuidesManager().GetGuideInfoByAppId(jni::ToNativeString(env, guideId), info)) + return GuideNative2Java(env).GetGuide(info); + + return NULL; + } + JNIEXPORT jint JNICALL Java_com_mapswithme_maps_Framework_getDrawScale(JNIEnv * env, jclass clazz) { diff --git a/storage/guides.cpp b/storage/guides.cpp index 5f55d9ce8e..4ec89df0a1 100644 --- a/storage/guides.cpp +++ b/storage/guides.cpp @@ -33,6 +33,16 @@ string GetStringImpl(json_t * j) return (s ? s : ""); } +GuideInfo::GuideInfo(json_struct_t * obj, const char * name) + : m_obj(obj), m_name(name ? name : "") +{ +} + +string GuideInfo::GetName() const +{ + return m_name; +} + string GuideInfo::GetString(char const * key) const { return GetStringImpl(json_object_get(m_obj.get(), key)); @@ -155,9 +165,9 @@ void GuidesManager::UpdateGuidesData() } } -bool GuidesManager::GetGuideInfo(string const & id, GuideInfo & appInfo) const +bool GuidesManager::GetGuideInfo(string const & countryFile, GuideInfo & appInfo) const { - MapT::const_iterator const it = m_file2Info.find(id); + MapT::const_iterator const it = m_file2Info.find(countryFile); if (it != m_file2Info.end()) { appInfo = it->second; @@ -166,9 +176,20 @@ bool GuidesManager::GetGuideInfo(string const & id, GuideInfo & appInfo) const return false; } +bool GuidesManager::GetGuideInfoByAppId(string const & id, GuideInfo & appInfo) const +{ + for (MapT::const_iterator it = m_file2Info.begin(); it != m_file2Info.end(); ++it) + if (it->second.GetAppID() == id) + { + appInfo = it->second; + return true; + } + return false; +} + void GuidesManager::GetGuidesIds(set & s) { - for (MapT::iterator it = m_file2Info.begin(); it != m_file2Info.end();++it) + for (MapT::iterator it = m_file2Info.begin(); it != m_file2Info.end(); ++it) s.insert(it->second.GetAppID()); } @@ -252,7 +273,7 @@ int GuidesManager::ParseGuidesData(string const & jsonData, MapT & guidesInfo) version = json_integer_value(entry); else { - GuideInfo info(entry); + GuideInfo info(entry, json_object_iter_key(iter)); if (info.IsValid()) { json_t * keys = json_object_get(entry, "keys"); @@ -260,7 +281,7 @@ int GuidesManager::ParseGuidesData(string const & jsonData, MapT & guidesInfo) { char const * key = json_string_value(json_array_get(keys, i)); if (key) - temp[key] = info; + temp.insert(MapT::value_type(key, info)); } } } diff --git a/storage/guides.hpp b/storage/guides.hpp index 30423048a3..bd3c4970e5 100644 --- a/storage/guides.hpp +++ b/storage/guides.hpp @@ -16,13 +16,15 @@ namespace guides class GuideInfo { my::JsonHandle m_obj; + string m_name; string GetString(char const * key) const; string GetAdForLang(string const & lang, char const * key) const; public: - GuideInfo(json_struct_t * obj = 0) : m_obj(obj) {} + GuideInfo(json_struct_t * obj = 0, char const * name = 0); + string GetName() const; string GetURL() const; string GetAppID() const; string GetAdTitle(string const & lang) const; @@ -50,7 +52,8 @@ public: static string GetDataFileFullPath(); /// @param[in] id MWM file name without extension as a key. - bool GetGuideInfo(string const & id, GuideInfo & appInfo) const; + bool GetGuideInfo(string const & countryFile, GuideInfo & appInfo) const; + bool GetGuideInfoByAppId(string const & id, GuideInfo & appInfo) const; void GetGuidesIds(set & s); /// @param[in] appID Guide app package id. diff --git a/storage/storage_tests/guides_tests.cpp b/storage/storage_tests/guides_tests.cpp index 56521a3085..53b57d6a24 100644 --- a/storage/storage_tests/guides_tests.cpp +++ b/storage/storage_tests/guides_tests.cpp @@ -37,6 +37,7 @@ UNIT_TEST(Guides_SmokeTest) TEST(manager.GetGuideInfo("Guernsey", info), ()); TEST(info.IsValid(), ()); + TEST_EQUAL(info.GetName(), "UK", ()); TEST_EQUAL(info.GetAdTitle("en"), "UK title", ()); TEST_EQUAL(info.GetAdMessage("en"), "UK message", ()); TEST_EQUAL(info.GetAdTitle("ru"), "ВБ заголовок", ());