diff options
author | Arsentiy Milchakov <milcars@mapswithme.com> | 2019-01-21 15:59:56 +0300 |
---|---|---|
committer | mpimenov <mpimenov@users.noreply.github.com> | 2019-01-22 15:01:32 +0300 |
commit | 1eece0bef3428cd1394d74841e98b823ef6ef490 (patch) | |
tree | 5ec3bd3ce0f314d37087f5c6d54cb9180a9c913f /platform | |
parent | d5eb25cfb0f7eb8392ee01cd40f97ef505b6ea08 (diff) |
[core][power_manager] review fixes
Diffstat (limited to 'platform')
-rw-r--r-- | platform/CMakeLists.txt | 2 | ||||
-rw-r--r-- | platform/battery_tracker.cpp | 68 | ||||
-rw-r--r-- | platform/battery_tracker.hpp | 30 | ||||
-rw-r--r-- | platform/platform.cpp | 3 | ||||
-rw-r--r-- | platform/platform.hpp | 8 | ||||
-rw-r--r-- | platform/platform_ios.mm | 4 | ||||
-rw-r--r-- | platform/platform_linux.cpp | 1 | ||||
-rw-r--r-- | platform/platform_mac.mm | 1 |
8 files changed, 117 insertions, 0 deletions
diff --git a/platform/CMakeLists.txt b/platform/CMakeLists.txt index 36cdf3c03b..5f66116e94 100644 --- a/platform/CMakeLists.txt +++ b/platform/CMakeLists.txt @@ -6,6 +6,8 @@ set(CMAKE_AUTOMOC ON) set( SRC + battery_tracker.cpp + battery_tracker.hpp chunks_download_strategy.cpp chunks_download_strategy.hpp constants.hpp diff --git a/platform/battery_tracker.cpp b/platform/battery_tracker.cpp new file mode 100644 index 0000000000..a80ce09eed --- /dev/null +++ b/platform/battery_tracker.cpp @@ -0,0 +1,68 @@ +#include "platform/battery_tracker.hpp" + +#include "platform/platform.hpp" + +namespace +{ +auto const kBatteryTrackingInterval = std::chrono::minutes(10); + +bool IsLevelExpired(std::chrono::system_clock::time_point lastRequestTime) +{ + return std::chrono::system_clock::now() - lastRequestTime > kBatteryTrackingInterval; +} +} // namespace + +namespace platform +{ +void BatteryLevelTracker::Subscribe(Subscriber * subscriber) +{ + m_subscribers.push_back(subscriber); + + if (IsLevelExpired(m_lastRequestTime)) + { + // Run periodic requests when the first subscriber is added. + if (m_subscribers.size() == 1) + RequestBatteryLevel(); + } + else + { + subscriber->OnBatteryLevelReceived(m_lastReceivedLevel); + } +} + +void BatteryLevelTracker::Unsubscribe(Subscriber * subscriber) +{ + m_subscribers.erase(std::remove(m_subscribers.begin(), m_subscribers.end(), subscriber), + m_subscribers.end()); +} + +void BatteryLevelTracker::UnsubscribeAll() +{ + m_subscribers.clear(); +} + +void BatteryLevelTracker::RequestBatteryLevel() +{ + if (m_subscribers.empty()) + return; + + if (IsLevelExpired(m_lastRequestTime)) + { + m_lastReceivedLevel = GetPlatform().GetBatteryLevel(); + m_lastRequestTime = std::chrono::system_clock::now(); + } + + for (auto s : m_subscribers) + { + s->OnBatteryLevelReceived(m_lastReceivedLevel); + } + + GetPlatform().RunDelayedTask(Platform::Thread::Background, kBatteryTrackingInterval, [this] + { + GetPlatform().RunTask(Platform::Thread::Gui, [this] + { + RequestBatteryLevel(); + }); + }); +} +} // namespace platform diff --git a/platform/battery_tracker.hpp b/platform/battery_tracker.hpp new file mode 100644 index 0000000000..65a6b35f3b --- /dev/null +++ b/platform/battery_tracker.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include <chrono> +#include <cstdint> +#include <vector> + +namespace platform +{ +// Note: this class is NOT thread-safe. +class BatteryLevelTracker +{ +public: + class Subscriber + { + public: + virtual void OnBatteryLevelReceived(uint8_t level) = 0; + }; + + void Subscribe(Subscriber * subscriber); + void Unsubscribe(Subscriber * subscriber); + void UnsubscribeAll(); + +private: + void RequestBatteryLevel(); + + std::vector<Subscriber *> m_subscribers; + std::chrono::system_clock::time_point m_lastRequestTime; + uint8_t m_lastReceivedLevel = 0; +}; +} // platform diff --git a/platform/platform.cpp b/platform/platform.cpp index add77316ce..9303d4ad86 100644 --- a/platform/platform.cpp +++ b/platform/platform.cpp @@ -329,6 +329,9 @@ unsigned Platform::CpuCores() const void Platform::ShutdownThreads() { ASSERT(m_networkThread && m_fileThread && m_backgroundThread, ()); + + m_batteryTracker.UnsubscribeAll(); + m_networkThread->ShutdownAndJoin(); m_fileThread->ShutdownAndJoin(); m_backgroundThread->ShutdownAndJoin(); diff --git a/platform/platform.hpp b/platform/platform.hpp index 9d92513670..3f832bcd07 100644 --- a/platform/platform.hpp +++ b/platform/platform.hpp @@ -1,5 +1,6 @@ #pragma once +#include "platform/battery_tracker.hpp" #include "platform/country_defines.hpp" #include "platform/gui_thread.hpp" #include "platform/http_user_agent.hpp" @@ -131,6 +132,8 @@ protected: std::unique_ptr<base::WorkerThread> m_fileThread; std::unique_ptr<base::WorkerThread> m_backgroundThread; + platform::BatteryLevelTracker m_batteryTracker; + public: Platform(); virtual ~Platform() = default; @@ -291,6 +294,9 @@ public: static bool IsConnected() { return ConnectionStatus() != EConnectionType::CONNECTION_NONE; } static ChargingStatus GetChargingStatus(); + + // Returns current battery level. Possible values are from 0 to 100. + // Returns 100 when actual level is unknown. static uint8_t GetBatteryLevel(); void SetupMeasurementSystem() const; @@ -347,6 +353,8 @@ public: // Use this method for testing purposes only. void SetGuiThread(std::unique_ptr<base::TaskLoop> guiThread); + platform::BatteryLevelTracker & GetBatteryTracker() { return m_batteryTracker; } + private: void RunThreads(); void ShutdownThreads(); diff --git a/platform/platform_ios.mm b/platform/platform_ios.mm index cd8c068800..e88d565870 100644 --- a/platform/platform_ios.mm +++ b/platform/platform_ios.mm @@ -228,6 +228,10 @@ Platform::ChargingStatus Platform::GetChargingStatus() uint8_t Platform::GetBatteryLevel() { auto const level = UIDevice.currentDevice.batteryLevel; + + ASSERT_GREATER_OR_EQUAL(level, -1.0, ()); + ASSERT_LESS_OR_EQUAL(level, 1.0, ()); + if (level == -1.0) return 100; diff --git a/platform/platform_linux.cpp b/platform/platform_linux.cpp index 0f014e0a9d..8a0a1306b0 100644 --- a/platform/platform_linux.cpp +++ b/platform/platform_linux.cpp @@ -262,6 +262,7 @@ Platform::ChargingStatus Platform::GetChargingStatus() uint8_t Platform::GetBatteryLevel() { + // This value is always 100 for desktop. return 100; } diff --git a/platform/platform_mac.mm b/platform/platform_mac.mm index 63f1ff6957..8f353108db 100644 --- a/platform/platform_mac.mm +++ b/platform/platform_mac.mm @@ -176,6 +176,7 @@ Platform::ChargingStatus Platform::GetChargingStatus() uint8_t Platform::GetBatteryLevel() { + // This value is always 100 for desktop. return 100; } |