From 5b5a2791f824f71c859adc288a6bfbbc9ad7164b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jo=C5=84ski?= Date: Tue, 5 Oct 2021 13:42:03 +0200 Subject: [PATCH] [BH-887] Back to homescreen after idle Back to homescreen after idle --- module-apps/apps-common/ApplicationCommon.hpp | 12 ++-- products/BellHybrid/apps/Application.cpp | 56 +++++++++++++++++++ products/BellHybrid/apps/CMakeLists.txt | 1 + .../ApplicationBellMain.cpp | 21 +++++++ .../ApplicationBellMain.hpp | 2 + .../ApplicationBellPowerNap.cpp | 4 ++ .../ApplicationBellPowerNap.hpp | 3 + .../BellHybrid/apps/include/Application.hpp | 13 +++++ .../services/appmgr/ApplicationManager.cpp | 16 +++++- .../BellHybrid/services/appmgr/CMakeLists.txt | 2 + .../services/appmgr/IdleHandler.cpp | 41 ++++++++++++++ .../include/appmgr/ApplicationManager.hpp | 5 +- .../appmgr/include/appmgr/IdleHandler.hpp | 32 +++++++++++ .../appmgr/messages/IdleTimerMessage.hpp | 31 ++++++++++ 14 files changed, 232 insertions(+), 7 deletions(-) create mode 100644 products/BellHybrid/services/appmgr/IdleHandler.cpp create mode 100644 products/BellHybrid/services/appmgr/include/appmgr/IdleHandler.hpp create mode 100644 products/BellHybrid/services/appmgr/include/appmgr/messages/IdleTimerMessage.hpp diff --git a/module-apps/apps-common/ApplicationCommon.hpp b/module-apps/apps-common/ApplicationCommon.hpp index c9f0b7ca0bfd8eaa6368670095de0db2e286b3a6..79329e5a68d0ddb8f8973bf62f1a1144624308b4 100644 --- a/module-apps/apps-common/ApplicationCommon.hpp +++ b/module-apps/apps-common/ApplicationCommon.hpp @@ -173,6 +173,13 @@ namespace app using OnActionReceived = std::function; + protected: + virtual sys::MessagePointer handleKBDKeyEvent(sys::Message *msgl); + virtual sys::MessagePointer handleApplicationSwitch(sys::Message *msgl); + virtual sys::MessagePointer handleAppClose(sys::Message *msgl); + virtual sys::MessagePointer handleSwitchWindow(sys::Message *msgl); + virtual sys::MessagePointer handleAppFocusLost(sys::Message *msgl); + private: std::string default_window; State state = State::DEACTIVATED; @@ -180,20 +187,15 @@ namespace app sys::MessagePointer handleSignalStrengthUpdate(sys::Message *msgl); sys::MessagePointer handleNetworkAccessTechnologyUpdate(sys::Message *msgl); sys::MessagePointer handleInputEvent(sys::Message *msgl); - sys::MessagePointer handleKBDKeyEvent(sys::Message *msgl); sys::MessagePointer handleBatteryStatusChange(); sys::MessagePointer handleMinuteUpdated(sys::Message *msgl); sys::MessagePointer handleAction(sys::Message *msgl); - sys::MessagePointer handleApplicationSwitch(sys::Message *msgl); sys::MessagePointer handleApplicationSwitchLaunch(sys::Message *msgl); sys::MessagePointer handleApplicationSwitchOnAction(sys::Message *msgl); - sys::MessagePointer handleSwitchWindow(sys::Message *msgl); sys::MessagePointer handleUpdateWindow(sys::Message *msgl); - sys::MessagePointer handleAppClose(sys::Message *msgl); sys::MessagePointer handleAppRebuild(sys::Message *msgl); sys::MessagePointer handleAppRefresh(sys::Message *msgl); sys::MessagePointer handleGetDOM(sys::Message *msgl); - sys::MessagePointer handleAppFocusLost(sys::Message *msgl); sys::MessagePointer handleSimStateUpdateMessage(sys::Message *msgl); virtual bool isPopupPermitted(gui::popup::ID popupId) const; diff --git a/products/BellHybrid/apps/Application.cpp b/products/BellHybrid/apps/Application.cpp index 57180ebbbcad9d223f1fa76954d55b1bc2f2dc9e..1972de6624f21a6be07a47058aa32688aed1330b 100644 --- a/products/BellHybrid/apps/Application.cpp +++ b/products/BellHybrid/apps/Application.cpp @@ -5,6 +5,7 @@ #include +#include #include #include #include @@ -13,6 +14,7 @@ #include #include #include +#include namespace app { @@ -68,4 +70,58 @@ namespace app (isCurrentWindow(gui::popup::resolveWindowName(gui::popup::ID::PowerOff))) || (isCurrentWindow(gui::BellTurnOffWindow::name))); } + + sys::MessagePointer Application::handleKBDKeyEvent(sys::Message *msgl) + { + onKeyPressed(); + return ApplicationCommon::handleKBDKeyEvent(msgl); + } + + sys::MessagePointer Application::handleApplicationSwitch(sys::Message *msgl) + { + onStart(); + return ApplicationCommon::handleApplicationSwitch(msgl); + } + + sys::MessagePointer Application::handleAppClose(sys::Message *msgl) + { + onStop(); + return ApplicationCommon::handleAppClose(msgl); + } + + sys::MessagePointer Application::handleAppFocusLost(sys::Message *msgl) + { + onStop(); + return ApplicationCommon::handleAppFocusLost(msgl); + } + + void Application::onKeyPressed() + { + restartIdleTimer(); + } + + void Application::onStart() + { + startIdleTimer(); + } + + void Application::onStop() + { + stopIdleTimer(); + } + + void Application::startIdleTimer() + { + bus.sendUnicast(std::make_shared(), service::name::appmgr); + } + + void Application::restartIdleTimer() + { + bus.sendUnicast(std::make_shared(), service::name::appmgr); + } + + void Application::stopIdleTimer() + { + bus.sendUnicast(std::make_shared(), service::name::appmgr); + } } // namespace app diff --git a/products/BellHybrid/apps/CMakeLists.txt b/products/BellHybrid/apps/CMakeLists.txt index 33e65cdede5e98dbe41023333a8acf8dec4423f2..9af1883361e339339933e1e96e5bc363bc388619 100644 --- a/products/BellHybrid/apps/CMakeLists.txt +++ b/products/BellHybrid/apps/CMakeLists.txt @@ -16,6 +16,7 @@ target_link_libraries(app PRIVATE apps-common bell::app-common + bell::appmgr ) add_subdirectory(application-bell-main) diff --git a/products/BellHybrid/apps/application-bell-main/ApplicationBellMain.cpp b/products/BellHybrid/apps/application-bell-main/ApplicationBellMain.cpp index 7d5bf5de1eb0c53a3b43351be819dd16152edb98..5922ff6f6c74b165b7a21858b40fdea63f588ee5 100644 --- a/products/BellHybrid/apps/application-bell-main/ApplicationBellMain.cpp +++ b/products/BellHybrid/apps/application-bell-main/ApplicationBellMain.cpp @@ -9,6 +9,7 @@ #include "windows/BellHomeScreenWindow.hpp" #include "windows/BellMainMenuWindow.hpp" +#include #include #include #include @@ -102,4 +103,24 @@ namespace app { return GetName() == app::applicationBellName && getCurrentWindow()->getName() == gui::name::window::main_window; } + + // Empty: do not start idleTimer on application run + void ApplicationBellMain::onStart() + {} + + sys::MessagePointer ApplicationBellMain::handleSwitchWindow(sys::Message *msgl) + { + auto msg = static_cast(msgl); + if (msg) { + const auto newWindowName = msg->getWindowName(); + if (newWindowName == gui::name::window::main_window) { + stopIdleTimer(); + } + else if (newWindowName == gui::window::name::bell_main_menu || + newWindowName == gui::window::name::bell_main_menu_dialog) { + startIdleTimer(); + } + } + return ApplicationCommon::handleSwitchWindow(msgl); + } } // namespace app diff --git a/products/BellHybrid/apps/application-bell-main/include/application-bell-main/ApplicationBellMain.hpp b/products/BellHybrid/apps/application-bell-main/include/application-bell-main/ApplicationBellMain.hpp index 9b79747aff688ba35b73889f73ada278a1b35f88..b1eb15502765645135dad5a1891be4f55ef8566c 100644 --- a/products/BellHybrid/apps/application-bell-main/include/application-bell-main/ApplicationBellMain.hpp +++ b/products/BellHybrid/apps/application-bell-main/include/application-bell-main/ApplicationBellMain.hpp @@ -41,6 +41,8 @@ namespace app private: void showPopup(gui::popup::ID id, const gui::PopupRequestParams *params) override; auto isHomeScreenFocused() -> bool; + void onStart() override; + sys::MessagePointer handleSwitchWindow(sys::Message *msgl) override; }; template <> struct ManifestTraits diff --git a/products/BellHybrid/apps/application-bell-powernap/ApplicationBellPowerNap.cpp b/products/BellHybrid/apps/application-bell-powernap/ApplicationBellPowerNap.cpp index 92d94ef0a5f898499d57168f75ac72a2af9fc577..3e067d88e4fcf1f176aeed92693375af1986febb 100644 --- a/products/BellHybrid/apps/application-bell-powernap/ApplicationBellPowerNap.cpp +++ b/products/BellHybrid/apps/application-bell-powernap/ApplicationBellPowerNap.cpp @@ -71,4 +71,8 @@ namespace app return handleAsyncResponse(resp); } + + // Empty: do not start idleTimer on application run + void ApplicationBellPowerNap::onStart() + {} } // namespace app diff --git a/products/BellHybrid/apps/application-bell-powernap/include/application-bell-powernap/ApplicationBellPowerNap.hpp b/products/BellHybrid/apps/application-bell-powernap/include/application-bell-powernap/ApplicationBellPowerNap.hpp index b1c6c5708d89f5b78161bc079108ba831f468fc5..ed86adeb7dccdfda835b74bd8bc5a4735e50ffb5 100644 --- a/products/BellHybrid/apps/application-bell-powernap/include/application-bell-powernap/ApplicationBellPowerNap.hpp +++ b/products/BellHybrid/apps/application-bell-powernap/include/application-bell-powernap/ApplicationBellPowerNap.hpp @@ -41,6 +41,9 @@ namespace app { return sys::ReturnCodes::Success; } + + private: + void onStart() override; }; template <> struct ManifestTraits diff --git a/products/BellHybrid/apps/include/Application.hpp b/products/BellHybrid/apps/include/Application.hpp index 6cf9624b0a05c2e25bd3e90d1d84f459138a8044..5ab4f8d845324d9ac2844aa6a64047a442e64922 100644 --- a/products/BellHybrid/apps/include/Application.hpp +++ b/products/BellHybrid/apps/include/Application.hpp @@ -15,5 +15,18 @@ namespace app protected: void attachPopups(const std::vector &popupsList) override; bool isPopupPermitted(gui::popup::ID popupId) const override; + void startIdleTimer(); + void restartIdleTimer(); + void stopIdleTimer(); + + private: + sys::MessagePointer handleKBDKeyEvent(sys::Message *msgl) override; + sys::MessagePointer handleApplicationSwitch(sys::Message *msgl) override; + sys::MessagePointer handleAppClose(sys::Message *msgl) override; + sys::MessagePointer handleAppFocusLost(sys::Message *msgl) override; + + virtual void onKeyPressed(); + virtual void onStart(); + virtual void onStop(); }; } // namespace app diff --git a/products/BellHybrid/services/appmgr/ApplicationManager.cpp b/products/BellHybrid/services/appmgr/ApplicationManager.cpp index bb4b48439a36a3d5ec216f8788c3c7e7964cd629..febebc423a498bfc78ef9cbd9790e9303fc951b0 100644 --- a/products/BellHybrid/services/appmgr/ApplicationManager.cpp +++ b/products/BellHybrid/services/appmgr/ApplicationManager.cpp @@ -3,6 +3,8 @@ #include #include +#include + #include namespace app::manager @@ -10,7 +12,7 @@ namespace app::manager ApplicationManager::ApplicationManager(const ApplicationName &serviceName, std::vector> &&launchers, const ApplicationName &_rootApplicationName) - : ApplicationManagerCommon(serviceName, std::move(launchers), _rootApplicationName) + : ApplicationManagerCommon(serviceName, std::move(launchers), _rootApplicationName), IdleHandler(this) { registerMessageHandlers(); } @@ -37,6 +39,18 @@ namespace app::manager ApplicationManagerCommon::registerMessageHandlers(); auto convertibleToActionHandler = [this](sys::Message *request) { return handleMessageAsAction(request); }; + connect(typeid(StartIdleTimerMessage), [this](sys::Message *request) { + handleStartIdleTimer(request); + return sys::msgHandled(); + }); + connect(typeid(RestartIdleTimerMessage), [this](sys::Message *request) { + handleRestartIdleTimer(request); + return sys::msgHandled(); + }); + connect(typeid(StopIdleTimerMessage), [this](sys::Message *request) { + handleStopIdleTimer(request); + return sys::msgHandled(); + }); connect(typeid(AlarmActivated), convertibleToActionHandler); connect(typeid(AlarmDeactivated), convertibleToActionHandler); } diff --git a/products/BellHybrid/services/appmgr/CMakeLists.txt b/products/BellHybrid/services/appmgr/CMakeLists.txt index ebb7cba275536669ee83ff47840184df1e1762eb..85ecd701115a4044c93965ecef0d9611f98ad1e9 100644 --- a/products/BellHybrid/services/appmgr/CMakeLists.txt +++ b/products/BellHybrid/services/appmgr/CMakeLists.txt @@ -4,9 +4,11 @@ add_library(bell::appmgr ALIAS appmgr) target_sources(appmgr PRIVATE ApplicationManager.cpp + IdleHandler.cpp PUBLIC include/appmgr/messages/AlarmMessage.hpp include/appmgr/ApplicationManager.hpp + include/appmgr/IdleHandler.hpp include/appmgr/messages/PowerOffPopupRequestParams.hpp include/appmgr/messages/RebootPopupRequestParams.hpp ) diff --git a/products/BellHybrid/services/appmgr/IdleHandler.cpp b/products/BellHybrid/services/appmgr/IdleHandler.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87595b6bd43c8a9fc6c2f9b1efc7df8d0adf236f --- /dev/null +++ b/products/BellHybrid/services/appmgr/IdleHandler.cpp @@ -0,0 +1,41 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#include +#include + +#include +#include + +namespace app::manager +{ + IdleHandler::IdleHandler(sys::Service *serv) : serv{serv} + { + idleTimer = sys::TimerFactory::createPeriodicTimer( + serv, "IdleReturn", idleReturnTimeout, [this](sys::Timer &) { idleTimerCallback(); }); + } + + void IdleHandler::handleStartIdleTimer(sys::Message *request) + { + idleTimer.restart(idleReturnTimeout); + } + + void IdleHandler::handleRestartIdleTimer(sys::Message *request) + { + if (idleTimer.isActive()) { + idleTimer.restart(idleReturnTimeout); + } + } + + void IdleHandler::handleStopIdleTimer(sys::Message *request) + { + idleTimer.stop(); + } + + void IdleHandler::idleTimerCallback() + { + LOG_INFO("User is idle - going back to home screen"); + idleTimer.stop(); + app::manager::Controller::sendAction(serv, app::manager::actions::Home); + } +} // namespace app::manager diff --git a/products/BellHybrid/services/appmgr/include/appmgr/ApplicationManager.hpp b/products/BellHybrid/services/appmgr/include/appmgr/ApplicationManager.hpp index 74e2314f3262c04fb01c6361949603dcc60670b5..4bd21c534ad5e555b74f0bef932763eba480bd22 100644 --- a/products/BellHybrid/services/appmgr/include/appmgr/ApplicationManager.hpp +++ b/products/BellHybrid/services/appmgr/include/appmgr/ApplicationManager.hpp @@ -3,11 +3,12 @@ #pragma once +#include #include namespace app::manager { - class ApplicationManager : public ApplicationManagerCommon + class ApplicationManager : public ApplicationManagerCommon, public IdleHandler { public: ApplicationManager(const ApplicationName &serviceName, @@ -15,6 +16,8 @@ namespace app::manager const ApplicationName &_rootApplicationName); private: + sys::TimerHandle idleTimer; + auto startApplication(ApplicationHandle &app) -> bool override; auto resolveHomeApplication() -> std::string override; void registerMessageHandlers() override; diff --git a/products/BellHybrid/services/appmgr/include/appmgr/IdleHandler.hpp b/products/BellHybrid/services/appmgr/include/appmgr/IdleHandler.hpp new file mode 100644 index 0000000000000000000000000000000000000000..07326883ff3df895c7ae40818be0b83c653f47b0 --- /dev/null +++ b/products/BellHybrid/services/appmgr/include/appmgr/IdleHandler.hpp @@ -0,0 +1,32 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#pragma once + +#include +#include +#include + +#include + +namespace app::manager +{ + constexpr auto idleReturnTimeout = std::chrono::seconds{5}; + using connectFunction = std::function; + + class IdleHandler + { + public: + IdleHandler(sys::Service *serv); + + protected: + void handleStartIdleTimer(sys::Message *request); + void handleRestartIdleTimer(sys::Message *request); + void handleStopIdleTimer(sys::Message *request); + void idleTimerCallback(); + + private: + sys::Service *serv; + sys::TimerHandle idleTimer; + }; +} // namespace app::manager diff --git a/products/BellHybrid/services/appmgr/include/appmgr/messages/IdleTimerMessage.hpp b/products/BellHybrid/services/appmgr/include/appmgr/messages/IdleTimerMessage.hpp new file mode 100644 index 0000000000000000000000000000000000000000..fb6868825b025325139cafe44bce6d31dd7f9c1c --- /dev/null +++ b/products/BellHybrid/services/appmgr/include/appmgr/messages/IdleTimerMessage.hpp @@ -0,0 +1,31 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#pragma once + +#include +#include +#include +#include +#include + +class StartIdleTimerMessage : public sys::DataMessage +{ + public: + StartIdleTimerMessage() : sys::DataMessage{MessageType::MessageTypeUninitialized} + {} +}; + +class RestartIdleTimerMessage : public sys::DataMessage +{ + public: + RestartIdleTimerMessage() : sys::DataMessage{MessageType::MessageTypeUninitialized} + {} +}; + +class StopIdleTimerMessage : public sys::DataMessage +{ + public: + StopIdleTimerMessage() : sys::DataMessage{MessageType::MessageTypeUninitialized} + {} +};