From 69a7c634178d9846e0205dd985d5df03c14133f7 Mon Sep 17 00:00:00 2001 From: Lefucjusz Date: Tue, 5 Sep 2023 19:08:02 +0200 Subject: [PATCH] [MOS-1031] Fix Meditation app screen lock Fix of the issue that Meditation app would prevent phone from locking on every screen, though it should do so only on timer screen. --- .../ApplicationMeditation.cpp | 27 +++--- .../ApplicationMeditation.hpp | 3 +- .../windows/MeditationTimerWindow.cpp | 83 ++++++++++--------- .../windows/MeditationWindow.cpp | 2 +- .../windows/MeditationWindow.hpp | 2 +- .../locks/handlers/LockPolicyHandler.cpp | 4 +- module-gui/gui/core/FontManager.cpp | 3 +- pure_changelog.md | 1 + 8 files changed, 67 insertions(+), 58 deletions(-) diff --git a/module-apps/application-meditation/ApplicationMeditation.cpp b/module-apps/application-meditation/ApplicationMeditation.cpp index 9cd578bc5d08ab739c5ed73b2bc037e29d29d98c..a7fe6cf282b1adcbb841a6090b43fae859e13bac 100644 --- a/module-apps/application-meditation/ApplicationMeditation.cpp +++ b/module-apps/application-meditation/ApplicationMeditation.cpp @@ -18,23 +18,24 @@ namespace app std::string parent, StatusIndicators statusIndicators, StartInBackground startInBackground) - : Application{name, parent, statusIndicators, startInBackground} + : Application{std::move(name), std::move(parent), statusIndicators, startInBackground} {} auto ApplicationMeditation::InitHandler() -> sys::ReturnCodes { const auto ret = Application::InitHandler(); - if (ret != sys::ReturnCodes::Success) + if (ret != sys::ReturnCodes::Success) { return ret; + } - auto counterVisible = + const auto counterVisible = getLocalSettingsValue(settings::Meditation::showCounter, Constants::Params::defaultCounterVisible); auto counterVisibleOnChangeCallback = [this](bool newValue) { settings->setValue( settings::Meditation::showCounter, utils::to_string(newValue), settings::SettingsScope::AppLocal); }; - auto preparationTime = std::chrono::seconds(getLocalSettingsValue( + const auto preparationTime = std::chrono::seconds(getLocalSettingsValue( settings::Meditation::preparationTime, Constants::Params::defaultPreparationTime.count())); auto preparationTimeOnChangeCallback = [this](std::chrono::seconds newValue) { settings->setValue(settings::Meditation::preparationTime, @@ -42,8 +43,8 @@ namespace app settings::SettingsScope::AppLocal); }; gui::OptionsData::OptionParams params = { - .preparationTime{std::move(preparationTime), std::move(preparationTimeOnChangeCallback)}, - .showCounter{std::move(counterVisible), std::move(counterVisibleOnChangeCallback)}}; + .preparationTime{preparationTime, std::move(preparationTimeOnChangeCallback)}, + .showCounter{counterVisible, std::move(counterVisibleOnChangeCallback)}}; state = std::make_unique(std::move(params)); @@ -55,7 +56,7 @@ namespace app auto ApplicationMeditation::DataReceivedHandler(sys::DataMessage *msgl, sys::ResponseMessage *resp) -> sys::MessagePointer { - auto retMsg = Application::DataReceivedHandler(msgl); + const auto retMsg = Application::DataReceivedHandler(msgl); // if message was handled by application's template there is no need to process further. if (retMsg && (dynamic_cast(retMsg.get())->retCode == sys::ReturnCodes::Success)) { return retMsg; @@ -64,7 +65,7 @@ namespace app return handleAsyncResponse(resp); } - auto ApplicationMeditation::SwitchPowerModeHandler(sys::ServicePowerMode mode) -> sys::ReturnCodes + auto ApplicationMeditation::SwitchPowerModeHandler([[maybe_unused]] sys::ServicePowerMode mode) -> sys::ReturnCodes { return sys::ReturnCodes::Success; } @@ -73,13 +74,13 @@ namespace app { windowsFactory.attach( app::window::name::meditation_main_window, [&](ApplicationCommon *app, const std::string &name) { - auto durationInitValue = + const auto durationInitValue = getLocalSettingsValue(settings::Meditation::duration, Constants::Params::defaultMeditationDuration); auto durationOnChangeCallback = [this](std::int32_t newValue) { settings->setValue( settings::Meditation::duration, utils::to_string(newValue), settings::SettingsScope::AppLocal); }; - auto intervalChimeInitValue = std::chrono::minutes(getLocalSettingsValue( + const auto intervalChimeInitValue = std::chrono::minutes(getLocalSettingsValue( settings::Meditation::intervalChime, Constants::Params::defaultChimeInterval.count())); auto intervalChimeOnChangeCallback = [this](std::chrono::minutes newValue) { settings->setValue(settings::Meditation::intervalChime, @@ -87,9 +88,9 @@ namespace app settings::SettingsScope::AppLocal); }; MeditationParams params = { - .meditationDuration{.initValue = std::move(durationInitValue), + .meditationDuration{.initValue = durationInitValue, .onChangeCallback = std::move(durationOnChangeCallback)}, - .intervalChime{.initValue = std::move(intervalChimeInitValue), + .intervalChime{.initValue = intervalChimeInitValue, .onChangeCallback = std::move(intervalChimeOnChangeCallback)}}; return std::make_unique(app, std::move(params)); @@ -121,7 +122,7 @@ namespace app template T ApplicationMeditation::getLocalSettingsValue(const std::string &variableName, T defaultValue) { - const std::string value = settings->getValue(variableName, settings::SettingsScope::AppLocal); + const auto &value = settings->getValue(variableName, settings::SettingsScope::AppLocal); return value.empty() ? defaultValue : utils::getNumericValue(value); } } // namespace app diff --git a/module-apps/application-meditation/include/application-meditation/ApplicationMeditation.hpp b/module-apps/application-meditation/include/application-meditation/ApplicationMeditation.hpp index d66a7a663a098f52eb44e1358332d6cd83cdeefa..4c90d8f508d191b5553eb55d36bb22423e5554db 100644 --- a/module-apps/application-meditation/include/application-meditation/ApplicationMeditation.hpp +++ b/module-apps/application-meditation/include/application-meditation/ApplicationMeditation.hpp @@ -42,8 +42,7 @@ namespace app return {{manager::actions::Launch, manager::actions::PhoneModeChanged, manager::actions::BluetoothModeChanged, - manager::actions::AlarmClockStatusChanged}, - locks::AutoLockPolicy::PreventPermanently}; + manager::actions::AlarmClockStatusChanged}}; } }; } // namespace app diff --git a/module-apps/application-meditation/windows/MeditationTimerWindow.cpp b/module-apps/application-meditation/windows/MeditationTimerWindow.cpp index 405a00937da11a7699f1060d3ba3167e5a6a2cf3..43f9ec7cbf45eeba6b42088de9d6e5e7ea4a1f9b 100644 --- a/module-apps/application-meditation/windows/MeditationTimerWindow.cpp +++ b/module-apps/application-meditation/windows/MeditationTimerWindow.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #include "MeditationTimer.hpp" @@ -38,8 +38,8 @@ void MeditationTimerWindow::rebuild() void MeditationTimerWindow::buildInterface() { - auto app = dynamic_cast(application); - assert(app != nullptr); // Pre-condition check. + const auto app = dynamic_cast(application); + assert(app != nullptr); AppWindow::buildInterface(); setTitle(utils::translate("app_meditation_title_main")); @@ -57,6 +57,8 @@ void MeditationTimerWindow::buildInterface() style::meditation::timer::infoText::Width, style::meditation::timer::infoText::Height); meditationInfo->setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Center)); + + preventsAutoLock = true; } void MeditationTimerWindow::destroyInterface() @@ -65,45 +67,47 @@ void MeditationTimerWindow::destroyInterface() invalidate(); } -void MeditationTimerWindow::onBeforeShow(ShowMode mode, SwitchData *data) +void MeditationTimerWindow::onBeforeShow([[maybe_unused]] ShowMode mode, SwitchData *data) { - auto timerData = dynamic_cast(data); - if (timerData != nullptr) { - setVisiblePreparation(); - meditationTime = timerData->getMeditationTime(); - meditationIntervalPeriod = timerData->getMeditationIntervals(); - - auto onPreparation = [&]() -> void { - setVisibleRunning(); - auto onMeditationEnd = [&]() -> void { - // Workaround for the issue of phone self-unlocking after meditation has ended. - // If the phone was manually locked on meditation timer screen, the window stack - // is as follows: MainWindow->Timer->PhoneLockPopup. - // setVisibleMeditationEnd() triggers window switch to MainWindow, so Timer and - // PhoneLockPopup are getting dropped. This workaround prevents such behaviour - // when phone is locked, in such case only meditation timer window is being dropped. - if (application->getCurrentWindow()->getName() == gui::popup::window::phone_lock_window) { - application->popWindow(app::window::name::meditation_timer); - } - else { - setVisibleMeditationEnd(); - application->refreshWindow(RefreshModes::GUI_REFRESH_DEEP); - } - timer->playSound(); - }; - timer->getTimer().registerOnFinishedCallback(onMeditationEnd); - timer->getTimer().reset(meditationTime, meditationIntervalPeriod); - timer->getTimer().start(); - timer->getProgress().setMaximum(meditationTime.count()); + const auto timerData = dynamic_cast(data); + if (timerData == nullptr) { + return; + } + + setVisiblePreparation(); + meditationTime = timerData->getMeditationTime(); + meditationIntervalPeriod = timerData->getMeditationIntervals(); + + auto onPreparation = [&]() -> void { + setVisibleRunning(); + auto onMeditationEnd = [&]() -> void { + // Workaround for the issue of phone self-unlocking after meditation has ended. + // If the phone was manually locked on meditation timer screen, the window stack + // is as follows: MainWindow->Timer->PhoneLockPopup. + // setVisibleMeditationEnd() triggers window switch to MainWindow, so Timer and + // PhoneLockPopup are getting dropped. This workaround prevents such behaviour + // when phone is locked, in such case only meditation timer window is being dropped. + if (application->getCurrentWindow()->getName() == gui::popup::window::phone_lock_window) { + application->popWindow(app::window::name::meditation_timer); + } + else { + setVisibleMeditationEnd(); + application->refreshWindow(RefreshModes::GUI_REFRESH_DEEP); + } timer->playSound(); - application->refreshWindow(RefreshModes::GUI_REFRESH_DEEP); }; - timer->getTimer().registerOnFinishedCallback(onPreparation); - timer->getTimer().reset(timerData->getPreparationTime()); + timer->getTimer().registerOnFinishedCallback(std::move(onMeditationEnd)); + timer->getTimer().reset(meditationTime, meditationIntervalPeriod); timer->getTimer().start(); - timer->getProgress().setMaximum(timerData->getPreparationTime().count()); - timer->setCounterVisible(timerData->isCounterEnabled()); - } + timer->getProgress().setMaximum(meditationTime.count()); + timer->playSound(); + application->refreshWindow(RefreshModes::GUI_REFRESH_DEEP); + }; + timer->getTimer().registerOnFinishedCallback(std::move(onPreparation)); + timer->getTimer().reset(timerData->getPreparationTime()); + timer->getTimer().start(); + timer->getProgress().setMaximum(timerData->getPreparationTime().count()); + timer->setCounterVisible(timerData->isCounterEnabled()); } auto MeditationTimerWindow::onInput(const InputEvent &inputEvent) -> bool @@ -147,6 +151,7 @@ void MeditationTimerWindow::setWidgetVisible(bool sBar, bool bBar, bool counter) navBar->setActive(nav_bar::Side::Left, bBar); timer->setVisible(counter); } + void MeditationTimerWindow::setVisibleRunning() { setWidgetVisible(false, true, true); @@ -154,12 +159,14 @@ void MeditationTimerWindow::setVisibleRunning() navBar->setText(nav_bar::Side::Right, utils::translate(style::strings::common::stop)); meditationInfo->setVisible(false); } + void MeditationTimerWindow::setVisiblePaused() { setWidgetVisible(true, true, true); navBar->setText(nav_bar::Side::Center, utils::translate(style::strings::common::resume)); navBar->setText(nav_bar::Side::Right, utils::translate(style::strings::common::stop)); } + void MeditationTimerWindow::setVisiblePreparation() { setWidgetVisible(false, false, false); diff --git a/module-apps/application-meditation/windows/MeditationWindow.cpp b/module-apps/application-meditation/windows/MeditationWindow.cpp index 847b9c629ac89d303a523c7f2daad3f546a71a5f..861c4514c4a86276ac37afde67fe26f1fd0ef45d 100644 --- a/module-apps/application-meditation/windows/MeditationWindow.cpp +++ b/module-apps/application-meditation/windows/MeditationWindow.cpp @@ -16,7 +16,7 @@ namespace gui { - MeditationWindow::MeditationWindow(app::ApplicationCommon *app, const MeditationParams params) + MeditationWindow::MeditationWindow(app::ApplicationCommon *app, const MeditationParams ¶ms) : AppWindow{app, name::window::main_window}, meditationParams(params) { MeditationWindow::buildInterface(); diff --git a/module-apps/application-meditation/windows/MeditationWindow.hpp b/module-apps/application-meditation/windows/MeditationWindow.hpp index 5fd3d5524b03a0eb69121b66749c083a8f299f14..9302f1817ba259f2126d00a4208eecd2271f107d 100644 --- a/module-apps/application-meditation/windows/MeditationWindow.hpp +++ b/module-apps/application-meditation/windows/MeditationWindow.hpp @@ -14,7 +14,7 @@ namespace gui class MeditationWindow : public AppWindow { public: - MeditationWindow(app::ApplicationCommon *app, const MeditationParams params); + MeditationWindow(app::ApplicationCommon *app, const MeditationParams ¶ms); auto onInput(const InputEvent &inputEvent) -> bool override; diff --git a/module-apps/apps-common/locks/handlers/LockPolicyHandler.cpp b/module-apps/apps-common/locks/handlers/LockPolicyHandler.cpp index 0cab836d79bdb472825ced9a7a56f9b0597f3f99..5178f8d05f3de8dfa3cf29fbf3b6ca47d97db4b5 100644 --- a/module-apps/apps-common/locks/handlers/LockPolicyHandler.cpp +++ b/module-apps/apps-common/locks/handlers/LockPolicyHandler.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #include "LockPolicyHandler.hpp" @@ -47,10 +47,12 @@ void LockPolicyHandler::setPreventsAutoLockByStateCallback( { preventsAutoLockByStateCallback = std::move(_preventsAutoLockByStateCallback); } + bool LockPolicyHandler::preventsAutoLockByWindow() { return owner->getCurrentWindow()->preventsAutoLocking(); } + bool LockPolicyHandler::preventsAutoLockByState() const { Expects(preventsAutoLockByStateCallback != nullptr); diff --git a/module-gui/gui/core/FontManager.cpp b/module-gui/gui/core/FontManager.cpp index e7ad4aa24c165387cae568a046949214588d9afd..644f016d3093dca84d92ce84c4e055c56d3de722 100644 --- a/module-gui/gui/core/FontManager.cpp +++ b/module-gui/gui/core/FontManager.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #include "FontManager.hpp" @@ -167,7 +167,6 @@ namespace gui FontManager &FontManager::getInstance() { - static FontManager instance; if (!instance.initialized) { LOG_WARN("Using uninitialized font manager will result in no font loaded"); diff --git a/pure_changelog.md b/pure_changelog.md index e5a502bd00d97d6c53807b6732dc99ef1e5d422e..b760b74bef353b3b1751512d20d38478f14b13d5 100644 --- a/pure_changelog.md +++ b/pure_changelog.md @@ -38,6 +38,7 @@ * Fixed showing "Copy text" option in empty note * Fixed "Copy" option missing from the options list in "New message" window * Fixed losing unsaved data on go back +* Fixed disabled screen autolock in meditation app ## [1.7.2 2023-07-28]