From 201c2b2d9ffd3cbb75c62930462b58a19b3411b9 Mon Sep 17 00:00:00 2001 From: Arsentiy Milchakov Date: Thu, 25 Apr 2019 18:44:59 +0300 Subject: [PATCH] [storage][android] autoupdate displayed sizes fix --- .../jni/com/mapswithme/maps/MapManager.cpp | 2 +- .../downloader/UpdaterDialogFragment.java | 31 ++++------ generator/mwm_diff/diff.cpp | 4 +- storage/diff_scheme/diff_manager.cpp | 56 +++++++++++++++---- storage/diff_scheme/diff_manager.hpp | 5 +- storage/diff_scheme/diff_types.hpp | 1 + storage/storage.cpp | 4 +- 7 files changed, 65 insertions(+), 38 deletions(-) diff --git a/android/jni/com/mapswithme/maps/MapManager.cpp b/android/jni/com/mapswithme/maps/MapManager.cpp index 197f24c414..ba33b64364 100644 --- a/android/jni/com/mapswithme/maps/MapManager.cpp +++ b/android/jni/com/mapswithme/maps/MapManager.cpp @@ -652,7 +652,7 @@ Java_com_mapswithme_maps_downloader_MapManager_nativeGetOverallProgress(JNIEnv * int res = 0; if (progress.second) - res = (jint) (progress.first * kMaxProgress / progress.second); + res = progress.first / progress.second; return res; } diff --git a/android/src/com/mapswithme/maps/downloader/UpdaterDialogFragment.java b/android/src/com/mapswithme/maps/downloader/UpdaterDialogFragment.java index 4da00a49fd..7cf197108a 100644 --- a/android/src/com/mapswithme/maps/downloader/UpdaterDialogFragment.java +++ b/android/src/com/mapswithme/maps/downloader/UpdaterDialogFragment.java @@ -115,7 +115,7 @@ public class UpdaterDialogFragment extends BaseMwmDialogFragment return; } - updateTotalSizes(info); + updateTotalSizes(info.totalSize); mAutoUpdate = false; mOutdatedMaps = Framework.nativeGetOutdatedCountries(); @@ -291,15 +291,11 @@ public class UpdaterDialogFragment extends BaseMwmDialogFragment } else { - final UpdateInfo info = MapManager.nativeGetUpdateInfo(CountryItem.getRootId()); - if (info == null) - { - finish(); - return; - } + CountryItem root = new CountryItem(CountryItem.getRootId()); + MapManager.nativeGetAttributes(root); - updateTotalSizes(info); - updateProgress(); + updateTotalSizes(root.bytesToDownload); + setProgress(root.progress, root.downloadedBytes, root.bytesToDownload); updateProcessedMapInfo(); setCommonStatus(mProcessedMapId, mCommonStatusResId); @@ -407,16 +403,10 @@ public class UpdaterDialogFragment extends BaseMwmDialogFragment mTitle.setText(status); } - void updateTotalSizes(@NonNull UpdateInfo info) + void updateTotalSizes(long totalSize) { - mTotalSize = StringUtils.getFileSizeString(info.totalSize); - mTotalSizeBytes = info.totalSize; - } - - void updateProgress() - { - int progress = MapManager.nativeGetOverallProgress(mOutdatedMaps); - setProgress(progress, mTotalSizeBytes * progress / 100, mTotalSizeBytes); + mTotalSize = StringUtils.getFileSizeString(totalSize); + mTotalSizeBytes = totalSize; } void updateProcessedMapInfo() @@ -567,15 +557,14 @@ public class UpdaterDialogFragment extends BaseMwmDialogFragment @Override public void onProgress(String countryId, long localSizeBytes, long remoteSizeBytes) { - if (mOutdatedMaps == null || !isFragmentAttached()) + if (!isFragmentAttached()) return; - int progress = MapManager.nativeGetOverallProgress(mOutdatedMaps); CountryItem root = new CountryItem(CountryItem.getRootId()); MapManager.nativeGetAttributes(root); //noinspection ConstantConditions - mFragment.setProgress(progress, root.downloadedBytes, root.bytesToDownload); + mFragment.setProgress(root.progress, root.downloadedBytes, root.bytesToDownload); } void attach(@NonNull UpdaterDialogFragment fragment) diff --git a/generator/mwm_diff/diff.cpp b/generator/mwm_diff/diff.cpp index a2f7f0244f..7783deca41 100644 --- a/generator/mwm_diff/diff.cpp +++ b/generator/mwm_diff/diff.cpp @@ -148,12 +148,12 @@ DiffApplicationResult ApplyDiff(string const & oldMwmPath, string const & newMwm } catch (Reader::Exception const & e) { - LOG(LERROR, ("Could not open file when applying a patch:", e.Msg())); + LOG(LERROR, ("Could not open file for reading when applying a patch:", e.Msg())); return DiffApplicationResult::Failed; } catch (Writer::Exception const & e) { - LOG(LERROR, ("Could not open file when applying a patch:", e.Msg())); + LOG(LERROR, ("Could not open file for writing when applying a patch:", e.Msg())); return DiffApplicationResult::Failed; } diff --git a/storage/diff_scheme/diff_manager.cpp b/storage/diff_scheme/diff_manager.cpp index 0b60321d7d..61fe844c00 100644 --- a/storage/diff_scheme/diff_manager.cpp +++ b/storage/diff_scheme/diff_manager.cpp @@ -10,6 +10,19 @@ #include "3party/Alohalytics/src/alohalytics.h" +namespace +{ +bool IsDiffsAvailable(storage::diffs::NameDiffInfoMap const & diffs) +{ + for (auto const & d : diffs) + { + if (!d.second.m_isApplied) + return true; + } + + return false; +} +} // namespace namespace storage { namespace diffs @@ -23,12 +36,12 @@ void Manager::Load(LocalMapsInfo && info) } m_workerThread.Push([this, localMapsInfo] { - NameDiffInfoMap const diffs = Checker::Check(localMapsInfo); + NameDiffInfoMap diffs = Checker::Check(localMapsInfo); std::lock_guard lock(m_mutex); - m_diffs = diffs; - if (diffs.empty()) + m_diffs = std::move(diffs); + if (m_diffs.empty()) { m_status = Status::NotAvailable; @@ -128,22 +141,44 @@ Status Manager::GetStatus() const bool Manager::SizeFor(storage::CountryId const & countryId, uint64_t & size) const { - return WithDiff(countryId, [&size](DiffInfo const & info) { size = info.m_size; }); + std::lock_guard lock(m_mutex); + if (m_status != Status::Available) + return false; + + auto const it = m_diffs.find(countryId); + if (it == m_diffs.cend()) + return false; + + size = it->second.m_size; + return true; } bool Manager::SizeToDownloadFor(storage::CountryId const & countryId, uint64_t & size) const { - return WithDiff(countryId, [&size](DiffInfo const & info) { size = info.m_size; }); + return WithNotAppliedDiff(countryId, [&size](DiffInfo const & info) { size = info.m_size; }); } -bool Manager::VersionFor(storage::CountryId const & countryId, uint64_t & version) const +bool Manager::VersionFor(storage::CountryId const & countryId, uint64_t & v) const { - return WithDiff(countryId, [&version](DiffInfo const & info) { version = info.m_version; }); + return WithNotAppliedDiff(countryId, [&v](DiffInfo const & info) { v = info.m_version; }); } bool Manager::HasDiffFor(storage::CountryId const & countryId) const { - return WithDiff(countryId, [](DiffInfo const &) {}); + return WithNotAppliedDiff(countryId, [](DiffInfo const &) {}); +} + +void Manager::MarkAsApplied(storage::CountryId const & countryId) +{ + std::lock_guard lock(m_mutex); + auto const it = m_diffs.find(countryId); + if (it == m_diffs.end()) + return; + + it->second.m_isApplied = true; + + if (!IsDiffsAvailable(m_diffs)) + m_status = Status::NotAvailable; } void Manager::RemoveDiffForCountry(storage::CountryId const & countryId) @@ -151,7 +186,7 @@ void Manager::RemoveDiffForCountry(storage::CountryId const & countryId) std::lock_guard lock(m_mutex); m_diffs.erase(countryId); - if (m_diffs.empty()) + if (m_diffs.empty() || !IsDiffsAvailable(m_diffs)) m_status = Status::NotAvailable; } @@ -171,7 +206,8 @@ bool Manager::IsPossibleToAutoupdate() const for (auto const & nameVersion : m_localMapsInfo.m_localMaps) { - if (m_diffs.find(nameVersion.first) == m_diffs.end()) + auto const it = m_diffs.find(nameVersion.first); + if (it == m_diffs.end() || it->second.m_isApplied) return false; } return true; diff --git a/storage/diff_scheme/diff_manager.hpp b/storage/diff_scheme/diff_manager.hpp index 6dfcf5e882..4bfcd67eea 100644 --- a/storage/diff_scheme/diff_manager.hpp +++ b/storage/diff_scheme/diff_manager.hpp @@ -60,6 +60,7 @@ public: // has been downloaded. bool HasDiffFor(storage::CountryId const & countryId) const; + void MarkAsApplied(storage::CountryId const & countryId); void RemoveDiffForCountry(storage::CountryId const & countryId); void AbortDiffScheme(); @@ -74,14 +75,14 @@ public: private: template - bool WithDiff(storage::CountryId const & countryId, Fn && fn) const + bool WithNotAppliedDiff(storage::CountryId const & countryId, Fn && fn) const { std::lock_guard lock(m_mutex); if (m_status != Status::Available) return false; auto const it = m_diffs.find(countryId); - if (it == m_diffs.cend()) + if (it == m_diffs.cend() || it->second.m_isApplied) return false; fn(it->second); diff --git a/storage/diff_scheme/diff_types.hpp b/storage/diff_scheme/diff_types.hpp index 6f31bd1a1c..1a86a1f044 100644 --- a/storage/diff_scheme/diff_types.hpp +++ b/storage/diff_scheme/diff_types.hpp @@ -24,6 +24,7 @@ struct DiffInfo final uint64_t m_size; uint64_t m_version; + bool m_isApplied = false; }; using NameDiffInfoMap = std::unordered_map; diff --git a/storage/storage.cpp b/storage/storage.cpp index 8145e140ce..25c7d5594a 100644 --- a/storage/storage.cpp +++ b/storage/storage.cpp @@ -1342,7 +1342,7 @@ uint64_t Storage::GetDownloadSize(QueuedCountry const & queuedCountry) const uint64_t size; if (queuedCountry.GetInitOptions() == MapOptions::Diff) { - CHECK_NOT_EQUAL(m_diffManager.SizeFor(countryId, size), 0, ()); + CHECK(m_diffManager.SizeToDownloadFor(countryId, size), ()); return size; } @@ -1619,7 +1619,7 @@ void Storage::ApplyDiff(CountryId const & countryId, functionGetPath(MapOptions::Map)); - m_diffManager.RemoveDiffForCountry(countryId); + m_diffManager.MarkAsApplied(countryId); fn(true); break; }