From 9fbe5893b096db31e90039209d9ea0c5d21c9526 Mon Sep 17 00:00:00 2001 From: vng Date: Thu, 9 Oct 2014 18:34:49 +0300 Subject: [PATCH] =?UTF-8?q?[storage]=20CountryTree=20now=20initialized=20i?= =?UTF-8?q?n=20Framework::AddMaps.=20It=E2=80=99s=20correct=20way=20for=20?= =?UTF-8?q?Android,=20when=20active=20maps=20set=20can=20be=20changed.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- android/jni/com/mapswithme/maps/Framework.cpp | 6 +- coding/zip_creator.cpp | 3 +- map/active_maps_layout.cpp | 38 +++--- map/active_maps_layout.hpp | 4 +- map/benchmark_engine.cpp | 5 +- map/country_tree.cpp | 11 ++ map/country_tree.hpp | 4 + map/framework.cpp | 118 +++++++++--------- map/framework.hpp | 17 ++- map/map_tests/bookmarks_test.cpp | 2 +- 10 files changed, 112 insertions(+), 96 deletions(-) diff --git a/android/jni/com/mapswithme/maps/Framework.cpp b/android/jni/com/mapswithme/maps/Framework.cpp index 308574f9f8..58c7c10d28 100644 --- a/android/jni/com/mapswithme/maps/Framework.cpp +++ b/android/jni/com/mapswithme/maps/Framework.cpp @@ -493,12 +493,12 @@ namespace android void Framework::AddLocalMaps() { - m_work.AddLocalMaps(); + m_work.AddMaps(); } void Framework::RemoveLocalMaps() { - m_work.RemoveLocalMaps(); + m_work.RemoveMaps(); } void Framework::GetMapsWithoutSearch(vector & out) const @@ -508,7 +508,7 @@ namespace android ::Platform const & pl = GetPlatform(); vector v; - m_work.GetLocalMaps(v); + m_work.GetMaps(v); for (size_t i = 0; i < v.size(); ++i) { diff --git a/coding/zip_creator.cpp b/coding/zip_creator.cpp index 20a2af6e14..15d8a9dec5 100644 --- a/coding/zip_creator.cpp +++ b/coding/zip_creator.cpp @@ -66,8 +66,7 @@ bool CreateZipFromPathDeflatedAndDefaultCompression(string const & filePath, str if (!zip.Handle()) return false; - // Special syntax to initialize struct with zeroes - zip_fileinfo zipInfo = zip_fileinfo(); + zip_fileinfo zipInfo = {}; CreateTMZip(zipInfo.tmz_date); string fileName = filePath; diff --git a/map/active_maps_layout.cpp b/map/active_maps_layout.cpp index 02c913a115..0921454df1 100644 --- a/map/active_maps_layout.cpp +++ b/map/active_maps_layout.cpp @@ -1,34 +1,38 @@ #include "active_maps_layout.hpp" - #include "framework.hpp" +#include "../std/algorithm.hpp" + + namespace storage { ActiveMapsLayout::ActiveMapsLayout(Framework & framework) : m_framework(framework) { - m_subscribeSlotID = m_framework.Storage().Subscribe(bind(&ActiveMapsLayout::StatusChangedCallback, this, _1), - bind(&ActiveMapsLayout::ProgressChangedCallback, this, _1, _2)); - - Init(); + m_subscribeSlotID = GetStorage().Subscribe(bind(&ActiveMapsLayout::StatusChangedCallback, this, _1), + bind(&ActiveMapsLayout::ProgressChangedCallback, this, _1, _2)); } ActiveMapsLayout::~ActiveMapsLayout() { - m_framework.Storage().Unsubscribe(m_subscribeSlotID); + GetStorage().Unsubscribe(m_subscribeSlotID); } -void ActiveMapsLayout::Init() +void ActiveMapsLayout::Init(vector const & maps) { Storage & storage = GetStorage(); - auto insertIndexFn = [&](TIndex const & index) + auto insertIndexFn = [&] (TIndex const & index) { - TStatus status; - TMapOptions options; - storage.CountryStatusEx(index, status, options); - if (status == TStatus::EOnDisk || status == TStatus::EOnDiskOutOfDate) - m_items.push_back({ index, status, options, options }); + string const fName = storage.CountryFileNameWithoutExt(index) + DATA_FILE_EXTENSION; + if (binary_search(maps.begin(), maps.end(), fName)) + { + TStatus status; + TMapOptions options; + storage.CountryStatusEx(index, status, options); + if (status == TStatus::EOnDisk || status == TStatus::EOnDiskOutOfDate) + m_items.push_back({ index, status, options, options }); + } }; TIndex root; @@ -63,7 +67,7 @@ void ActiveMapsLayout::Init() sort(m_items.begin(), m_items.end(), comparatorFn); - m_split = make_pair(0, m_items.size()); + m_split = { 0, m_items.size() }; for (size_t i = 0; i < m_items.size(); ++i) { if (m_items[i].m_status == TStatus::EOnDisk) @@ -74,6 +78,12 @@ void ActiveMapsLayout::Init() } } +void ActiveMapsLayout::Clear() +{ + m_items.clear(); + m_split = { 0, 0 }; +} + void ActiveMapsLayout::UpdateAll() { vector toDownload; diff --git a/map/active_maps_layout.hpp b/map/active_maps_layout.hpp index c8cd535b03..382dbe653d 100644 --- a/map/active_maps_layout.hpp +++ b/map/active_maps_layout.hpp @@ -83,10 +83,12 @@ private: Storage const & GetStorage() const; Storage & GetStorage(); + void Init(vector const & maps); + void Clear(); + bool GetGuideInfo(TIndex const & index, guides::GuideInfo & info) const; void ShowMap(TIndex const & index); - void Init(); private: void StatusChangedCallback(TIndex const & index); diff --git a/map/benchmark_engine.cpp b/map/benchmark_engine.cpp index 98612f55ef..26c414d13a 100644 --- a/map/benchmark_engine.cpp +++ b/map/benchmark_engine.cpp @@ -126,10 +126,7 @@ struct MapsCollector void BenchmarkEngine::PrepareMaps() { // remove all previously added maps in framework constructor - Platform::FilesList files; - m_framework->GetLocalMaps(files); - for_each(files.begin(), files.end(), - bind(&Framework::RemoveMap, m_framework, _1)); + m_framework->RemoveMaps(); // add only maps needed for benchmarks MapsCollector collector; diff --git a/map/country_tree.cpp b/map/country_tree.cpp index 4a19f05e20..e82dc28e8a 100644 --- a/map/country_tree.cpp +++ b/map/country_tree.cpp @@ -52,6 +52,17 @@ CountryTree::~CountryTree() GetStorage().Unsubscribe(m_subscribeSlotID); } +void CountryTree::Init(vector const & maps) +{ + m_layout.Init(maps); +} + +void CountryTree::Clear() +{ + ResetRoot(); + m_layout.Clear(); +} + ActiveMapsLayout & CountryTree::GetActiveMapLayout() { return m_layout; diff --git a/map/country_tree.hpp b/map/country_tree.hpp index d199c3dfdb..ab87ebe8de 100644 --- a/map/country_tree.hpp +++ b/map/country_tree.hpp @@ -29,6 +29,10 @@ public: CountryTree(Framework & framework); ~CountryTree(); + /// @param[in] Sorted vector of current .mwm files. + void Init(vector const & maps); + void Clear(); + ActiveMapsLayout & GetActiveMapLayout(); ActiveMapsLayout const & GetActiveMapLayout() const; diff --git a/map/framework.cpp b/map/framework.cpp index 19eccacadd..c6405dd2ce 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -78,29 +78,23 @@ namespace static const int BM_TOUCH_PIXEL_INCREASE = 20; } -void Framework::AddMap(string const & file) +int Framework::AddMap(string const & file) { LOG(LINFO, ("Loading map:", file)); int const version = m_model.AddMap(file); - switch (version) + if (version == feature::DataHeader::v1) { - case -1: - // Error in adding map - do nothing. - break; - - case feature::DataHeader::v1: // Now we do force delete of old (April 2011) maps. LOG(LINFO, ("Deleting old map:", file)); - RemoveMap(file); - VERIFY ( my::DeleteFileX(GetPlatform().WritablePathForFile(file)), () ); - break; - default: - if (m_lowestMapVersion > version) - m_lowestMapVersion = version; - break; + RemoveMap(file); + VERIFY(my::DeleteFileX(GetPlatform().WritablePathForFile(file)), ()); + + return -1; } + + return version; } void Framework::RemoveMap(string const & file) @@ -108,8 +102,9 @@ void Framework::RemoveMap(string const & file) m_model.RemoveMap(file); } -void Framework::OnLocationError(TLocationError error) -{} +void Framework::OnLocationError(TLocationError /*error*/) +{ +} void Framework::OnLocationUpdate(GpsInfo const & info) { @@ -171,12 +166,32 @@ CountryStatusDisplay * Framework::GetCountryStatusDisplay() const return m_informationDisplay.countryStatusDisplay().get(); } -static void GetResourcesMaps(vector & outMaps) +void Framework::GetMaps(vector & maps) const { Platform & pl = GetPlatform(); - pl.GetFilesByExt(pl.ResourcesDir(), DATA_FILE_EXTENSION, outMaps); + + pl.GetFilesByExt(pl.ResourcesDir(), DATA_FILE_EXTENSION, maps); + pl.GetFilesByExt(pl.WritableDir(), DATA_FILE_EXTENSION, maps); + + // Remove duplicate maps if they're both present in resources and in WritableDir. + sort(maps.begin(), maps.end()); + maps.erase(unique(maps.begin(), maps.end()), maps.end()); + +#ifdef OMIM_OS_ANDROID + // On Android World and WorldCoasts can be stored in alternative /Android/obb/ path. + char const * arrCheck[] = { WORLD_FILE_NAME DATA_FILE_EXTENSION, + WORLD_COASTS_FILE_NAME DATA_FILE_EXTENSION }; + + for (size_t i = 0; i < ARRAY_SIZE(arrCheck); ++i) + { + auto const it = lower_bound(maps.begin(), maps.end(), arrCheck[i]); + if (it == maps.end() || *it != arrCheck[i]) + maps.insert(it, arrCheck[i]); + } +#endif } + Framework::Framework() : m_navigator(m_scales), m_animator(this), @@ -187,7 +202,6 @@ Framework::Framework() m_guiController(new gui::Controller), m_animController(new anim::Controller), m_informationDisplay(this), - m_lowestMapVersion(numeric_limits::max()), m_benchmarkEngine(0), m_bmManager(*this), m_balloonManager(*this), @@ -236,30 +250,19 @@ Framework::Framework() m_model.InitClassificator(); LOG(LDEBUG, ("Classificator initialized")); - // Get all available maps. - vector maps; - GetResourcesMaps(maps); + // To avoid possible races - init search engine once in constructor. + (void)GetSearchEngine(); + LOG(LDEBUG, ("Search engine initialized")); + #ifndef OMIM_OS_ANDROID - // On Android, local maps are added and removed when external storage is connected/disconnected. - GetLocalMaps(maps); + AddMaps(); #endif - - // Remove duplicate maps if they're both present in resources and in WritableDir. - sort(maps.begin(), maps.end()); - maps.erase(unique(maps.begin(), maps.end()), maps.end()); - - // Add founded maps to index. - for_each(maps.begin(), maps.end(), bind(&Framework::AddMap, this, _1)); - LOG(LDEBUG, ("Map index initialized")); + LOG(LDEBUG, ("Maps initialized")); // Init storage with needed callback. m_storage.Init(bind(&Framework::UpdateAfterDownload, this, _1, _2)); LOG(LDEBUG, ("Storage initialized")); - // To avoid possible races - init search engine once in constructor. - (void)GetSearchEngine(); - LOG(LDEBUG, ("Search engine initialized")); - #ifndef OMIM_OS_DESKTOP // Init guides manager m_storage.GetGuideManager().RestoreFromFile(); @@ -270,6 +273,7 @@ Framework::Framework() { return GetSearchEngine()->GetCountryFile(pt); })); + LOG(LDEBUG, ("Routing engine initialized")); LOG(LINFO, ("System languages:", languages::GetPreferred())); } @@ -367,19 +371,29 @@ void Framework::UpdateAfterDownload(string const & fileName, TMapOptions opt) } } -void Framework::AddLocalMaps() +void Framework::AddMaps() { - // add maps to the model - Platform::FilesList maps; - GetLocalMaps(maps); + //ASSERT(m_model.IsEmpty(), ()); - for_each(maps.begin(), maps.end(), bind(&Framework::AddMap, this, _1)); + int minVersion = numeric_limits::max(); - m_pSearchEngine->SupportOldFormat(m_lowestMapVersion < feature::DataHeader::v3); + vector maps; + GetMaps(maps); + for_each(maps.begin(), maps.end(), [&] (string const & file) + { + int const v = AddMap(file); + if (v != -1 && v < minVersion) + minVersion = v; + }); + + m_countryTree.Init(maps); + + GetSearchEngine()->SupportOldFormat(minVersion < feature::DataHeader::v3); } -void Framework::RemoveLocalMaps() +void Framework::RemoveMaps() { + m_countryTree.Clear(); m_model.RemoveAll(); } @@ -537,22 +551,6 @@ bool Framework::AddBookmarksFile(string const & filePath) return true; } -void Framework::GetLocalMaps(vector & outMaps) const -{ - Platform & pl = GetPlatform(); - pl.GetFilesByExt(pl.WritableDir(), DATA_FILE_EXTENSION, outMaps); - -#ifdef OMIM_OS_ANDROID - // On Android World and WorldCoasts can be stored in alternative /Android/obb/ path. - char const * arrCheck[] = { WORLD_FILE_NAME DATA_FILE_EXTENSION, - WORLD_COASTS_FILE_NAME DATA_FILE_EXTENSION }; - - for (size_t i = 0; i < ARRAY_SIZE(arrCheck); ++i) - if (find(outMaps.begin(), outMaps.end(), arrCheck[i]) == outMaps.end()) - outMaps.push_back(arrCheck[i]); -#endif -} - void Framework::PrepareToShutdown() { SetRenderPolicy(0); @@ -1124,8 +1122,6 @@ search::Engine * Framework::GetSearchEngine() const pl.GetReader(PACKED_POLYGONS_FILE), pl.GetReader(COUNTRIES_FILE), languages::GetCurrentOrig())); - - m_pSearchEngine->SupportOldFormat(m_lowestMapVersion < feature::DataHeader::v3); } catch (RootException const & e) { diff --git a/map/framework.hpp b/map/framework.hpp index 62f6d760de..c1cdeb449d 100644 --- a/map/framework.hpp +++ b/map/framework.hpp @@ -127,10 +127,6 @@ protected: return 0.0; } - /// Stores lowest loaded map version or MAX_INT if no maps added. - /// @see feature::DataHeader::Version - int m_lowestMapVersion; - void DrawAdditionalInfo(shared_ptr const & e); BenchmarkEngine * m_benchmarkEngine; @@ -148,15 +144,16 @@ public: /// @name Process storage connecting/disconnecting. //@{ - void AddLocalMaps(); - void RemoveLocalMaps(); + /// @param[out] maps File names without path. + void GetMaps(vector & maps) const; - void AddMap(string const & file); + void AddMaps(); + void RemoveMaps(); + + /// @return Inner mwm data version from header or -1 in case of error. + int AddMap(string const & file); //@} - /// @return File names without path. - void GetLocalMaps(vector & outMaps) const; - /// @name This functions is used by Downloader UI. //@{ /// options - flags that signal about parts of map that must be deleted diff --git a/map/map_tests/bookmarks_test.cpp b/map/map_tests/bookmarks_test.cpp index 9239117178..a179ae4e2a 100644 --- a/map/map_tests/bookmarks_test.cpp +++ b/map/map_tests/bookmarks_test.cpp @@ -395,7 +395,7 @@ UNIT_TEST(Bookmarks_AddressInfo) { // Maps added in constructor (we need minsk-pass.mwm only) Framework fm; - fm.RemoveLocalMaps(); + fm.RemoveMaps(); fm.AddMap("minsk-pass.mwm"); fm.OnSize(800, 600);