From dce9bf1e631a900ecd535c5c3aedbe11c64f574d Mon Sep 17 00:00:00 2001 From: Benau Date: Fri, 20 Dec 2019 08:59:02 +0800 Subject: [PATCH] Save addons xml when going into background for mobile stk So the icons don't need to be redownloaded again after it's killed by OS in the background --- src/addons/addons_manager.cpp | 7 +++++++ src/addons/addons_manager.hpp | 9 ++++++++- src/guiengine/event_handler.cpp | 5 +++++ src/main_loop.cpp | 5 +++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/addons/addons_manager.cpp b/src/addons/addons_manager.cpp index c248e3a89..346f5e4d1 100644 --- a/src/addons/addons_manager.cpp +++ b/src/addons/addons_manager.cpp @@ -59,6 +59,7 @@ AddonsManager* addons_manager = 0; AddonsManager::AddonsManager() : m_addons_list(std::vector() ), m_state(STATE_INIT) { + m_downloaded_icons = false; // Clean .part file which may be left behind std::string addons_part = file_manager->getAddonsFile("addons.xml.part"); if (file_manager->fileExists(addons_part)) @@ -393,6 +394,11 @@ void AddonsManager::downloadIcons() void afterOperation() { m_addon->setIconReady(); + } // afterOperation + void callback() + { + if (!hadDownloadError()) + addons_manager->m_downloaded_icons = true; } // callback public: IconRequest(const std::string &filename, @@ -632,6 +638,7 @@ void AddonsManager::saveInstalled() } xml_installed << "" << std::endl; xml_installed.close(); + m_downloaded_icons = false; } // saveInstalled #endif diff --git a/src/addons/addons_manager.hpp b/src/addons/addons_manager.hpp index eef388632..979b98be1 100644 --- a/src/addons/addons_manager.hpp +++ b/src/addons/addons_manager.hpp @@ -53,6 +53,12 @@ private: // Synchronise the state between threads (e.g. GUI and update thread) Synchronised m_state; + /* Return true if any icons have been downloaded, so we need to call + * saveInstalled in mobile stk when pressing home button, so the icons + * won't need to be redownload when stk is killed by OS in the + * background. */ + bool m_downloaded_icons; + void loadInstalledAddons(); void downloadIcons(); @@ -88,7 +94,8 @@ public: // ------------------------------------------------------------------------ /** Returns the i-th addons. */ const Addon& getAddon(unsigned int i) { return m_addons_list.getData()[i];} - + // ------------------------------------------------------------------------ + bool hasDownloadedIcons() const { return m_downloaded_icons; } }; // class AddonsManager extern AddonsManager *addons_manager; diff --git a/src/guiengine/event_handler.cpp b/src/guiengine/event_handler.cpp index 382c43adb..9cdefb856 100644 --- a/src/guiengine/event_handler.cpp +++ b/src/guiengine/event_handler.cpp @@ -17,6 +17,9 @@ #include "guiengine/event_handler.hpp" +#ifdef ANDROID +#include "addons/addons_manager.hpp" +#endif #include "audio/music_manager.hpp" #include "audio/sfx_manager.hpp" #include "config/user_config.hpp" @@ -182,6 +185,8 @@ bool EventHandler::OnEvent (const SEvent &event) music_manager->pauseMusic(); SFXManager::get()->pauseAll(); } + if (addons_manager->hasDownloadedIcons()) + addons_manager->saveInstalled(); } else if (cmd == APP_CMD_RESUME || cmd == APP_CMD_GAINED_FOCUS) { diff --git a/src/main_loop.cpp b/src/main_loop.cpp index ca71e0480..f264b9815 100644 --- a/src/main_loop.cpp +++ b/src/main_loop.cpp @@ -19,6 +19,9 @@ #include "main_loop.hpp" +#ifdef IOS_STK +#include "addons/addons_manager.hpp" +#endif #include "audio/music_manager.hpp" #include "audio/sfx_manager.hpp" #include "config/user_config.hpp" @@ -128,6 +131,8 @@ float MainLoop::getLimitedDt() first_out_focus = false; music_manager->pauseMusic(); SFXManager::get()->pauseAll(); + if (addons_manager->hasDownloadedIcons()) + addons_manager->saveInstalled(); } dev->run(); win_active = dev->isWindowActive();