From 5cf1cf8a11abb6aa30935574ee2a138d4bd4f5ef Mon Sep 17 00:00:00 2001 From: Robert Borzecki Date: Wed, 10 Jul 2019 11:40:20 +0200 Subject: [PATCH] Added change language flow. --- module-apps/Application.cpp | 22 +++-- module-apps/Application.hpp | 1 + .../windows/MenuWindow.cpp | 2 +- .../ApplicationSettings.hpp | 2 +- .../windows/LanguageWindow.cpp | 5 ++ .../windows/SettingsMainWindow.cpp | 2 +- module-apps/messages/AppMessage.hpp | 30 ++++--- module-apps/windows/AppWindow.hpp | 1 + .../service-appmgr/ApplicationManager.cpp | 84 ++++++++++++++++--- .../service-appmgr/ApplicationManager.hpp | 8 +- .../service-appmgr/messages/APMMessage.hpp | 13 +++ .../service-evtmgr/EventManager.cpp | 5 +- module-utils/i18/i18.hpp | 6 +- source/MessageType.hpp | 3 + 14 files changed, 146 insertions(+), 38 deletions(-) diff --git a/module-apps/Application.cpp b/module-apps/Application.cpp index aa1917ef3..5dd3a9ea2 100644 --- a/module-apps/Application.cpp +++ b/module-apps/Application.cpp @@ -87,13 +87,13 @@ int Application::switchWindow( const std::string& windowName, uint32_t cmd, std: return 0; } int Application::switchBackWindow( const std::string& windowName, uint32_t cmd, std::unique_ptr data ) { - auto msg = std::make_shared( MessageType::AppSwitchWindowBack, this->GetName() ); + auto msg = std::make_shared( MessageType::AppSwitchWindowBack ); sys::Bus::SendUnicast(msg, this->GetName(), this ); return 0; } int Application::refreshWindow(gui::RefreshModes mode) { - auto msg = std::make_shared( this->GetName(), mode ); + auto msg = std::make_shared( mode ); sys::Bus::SendUnicast(msg, this->GetName(), this ); return 0; @@ -203,6 +203,10 @@ sys::Message_t Application::DataReceivedHandler(sys::DataMessage* msgl) { //here should go all the cleaning handled = true; } + else if( msgl->messageType == static_cast(MessageType::AppRebuild )) { + + LOG_INFO("Application %s rebuilding gui", GetName().c_str() ); + } else if( msgl->messageType == static_cast(MessageType::AppRefresh)) { AppRefreshMessage* msg = reinterpret_cast( msgl ); //currentWindow->onBeforeShow( gui::ShowMode::GUI_SHOW_RETURN, 0, nullptr ); @@ -216,7 +220,6 @@ sys::Message_t Application::DataReceivedHandler(sys::DataMessage* msgl) { } sys::ReturnCodes Application::InitHandler() { -// LOG_INFO("!!!!!!!!!!!!!!!!!INIT TIME 1: %d", xTaskGetTickCount() ); bool initState= true; state = State::INITIALIZING; uint32_t start = xTaskGetTickCount(); @@ -226,11 +229,8 @@ sys::ReturnCodes Application::InitHandler() { initState = (settings.dbID == 1); //send response to application manager true if successful, false otherwise. -// LOG_INFO("!!!!!!!!!!!!!!!!!INIT TIME 2: %d", xTaskGetTickCount() ); sapm::ApplicationManager::messageRegisterApplication( this, initState ); -// LOG_INFO("!!!!!!!!!!!!!!!!!INIT TIME 3: %d", xTaskGetTickCount() ); sys::ReturnCodes retCode = (initState?sys::ReturnCodes::Success:sys::ReturnCodes::Failure); -// LOG_INFO("*****************ReturnCode %d",static_cast(retCode)); return retCode; } @@ -257,14 +257,20 @@ bool Application::messageSwitchApplication( sys::Service* sender, std::string ap } bool Application::messageRefreshApplication( sys::Service* sender, std::string application, std::string window, gui::SwitchData* data ) { - auto msg = std::make_shared( MessageType::AppRefresh, application ); + auto msg = std::make_shared( MessageType::AppRefresh ); sys::Bus::SendUnicast(msg, application, sender ); return true; } bool Application::messageCloseApplication( sys::Service* sender, std::string application ) { - auto msg = std::make_shared( MessageType::AppClose, application ); + auto msg = std::make_shared( MessageType::AppClose ); + sys::Bus::SendUnicast(msg, application, sender ); + return true; +} + +bool Application::messageRebuildApplication( sys::Service* sender, std::string application ) { + auto msg = std::make_shared(); sys::Bus::SendUnicast(msg, application, sender ); return true; } diff --git a/module-apps/Application.hpp b/module-apps/Application.hpp index 4cc35d45c..d598c88bb 100644 --- a/module-apps/Application.hpp +++ b/module-apps/Application.hpp @@ -105,6 +105,7 @@ public: static bool messageSwitchApplication( sys::Service* sender, std::string application, std::string window, std::unique_ptr data ); static bool messageRefreshApplication( sys::Service* sender, std::string application, std::string window, gui::SwitchData* data=nullptr ); static bool messageCloseApplication( sys::Service* sender, std::string application ); + static bool messageRebuildApplication( sys::Service* sender, std::string application ); /** * @brief This method is used to send message to set focus of the application. * Application can gain or lose focus depending on the provided focus flag. diff --git a/module-apps/application-desktop/windows/MenuWindow.cpp b/module-apps/application-desktop/windows/MenuWindow.cpp index ff3328888..04218c8c6 100644 --- a/module-apps/application-desktop/windows/MenuWindow.cpp +++ b/module-apps/application-desktop/windows/MenuWindow.cpp @@ -191,7 +191,7 @@ void MenuWindow::switchPage( uint32_t index ) { //give focus to element Item* item = pages[currentPage]->tiles[0]; setFocusItem(item); - application->refreshWindow( RefreshModes::GUI_REFRESH_DEEP ); + application->refreshWindow( RefreshModes::GUI_REFRESH_DEEP ); } } /* namespace gui */ diff --git a/module-apps/application-settings/ApplicationSettings.hpp b/module-apps/application-settings/ApplicationSettings.hpp index 16be29352..98ad5b07e 100644 --- a/module-apps/application-settings/ApplicationSettings.hpp +++ b/module-apps/application-settings/ApplicationSettings.hpp @@ -32,7 +32,7 @@ public: class ApplicationSettingsLauncher : public ApplicationLauncher { public: - ApplicationSettingsLauncher() : ApplicationLauncher("ApplicationSettings", false) {}; + ApplicationSettingsLauncher() : ApplicationLauncher("ApplicationSettings", true) {}; bool run(sys::SystemManager* sysmgr) override { return sysmgr->CreateService(std::make_shared(name),sysmgr, 1000); }; diff --git a/module-apps/application-settings/windows/LanguageWindow.cpp b/module-apps/application-settings/windows/LanguageWindow.cpp index e2fc6a8be..2cb45b0d4 100644 --- a/module-apps/application-settings/windows/LanguageWindow.cpp +++ b/module-apps/application-settings/windows/LanguageWindow.cpp @@ -45,20 +45,24 @@ LanguageWindow::LanguageWindow( app::Application* app ) : AppWindow(app,"Languag //add option connectivity option options.push_back( addOptionLabel( utils::localize.get("app_settings_language_english"), [=] (gui::Item& item){ LOG_INFO("selected language: english" ); + sapm::ApplicationManager::messageChangeLanguage( application, utils::Lang::En ); return true;} )); //add option date and time option options.push_back( addOptionLabel( utils::localize.get("app_settings_language_polish"), [=](gui::Item&){ LOG_INFO("selected language: polish" ); + sapm::ApplicationManager::messageChangeLanguage( application, utils::Lang::Pl ); return true;} )); //add option display option options.push_back( addOptionLabel( utils::localize.get("app_settings_language_german"), [=](gui::Item&){ LOG_INFO("selected language: german" ); + sapm::ApplicationManager::messageChangeLanguage( application, utils::Lang::De ); return true;} )); options.push_back( addOptionLabel( utils::localize.get("app_settings_language_spanish"), [=](gui::Item&){ LOG_INFO("selected language: spanish" ); + sapm::ApplicationManager::messageChangeLanguage( application, utils::Lang::Sp ); return true;} )); //set possition and navigation for labels @@ -84,6 +88,7 @@ gui::Item* LanguageWindow::addOptionLabel( const std::string& text, std::functio label->setFont("gt_pressura_regular_24"); label->setAlignement( gui::Alignment(gui::Alignment::ALIGN_HORIZONTAL_LEFT, gui::Alignment::ALIGN_VERTICAL_CENTER)); label->setRadius(11); + label->activatedCallback = activatedCallback; return label; } diff --git a/module-apps/application-settings/windows/SettingsMainWindow.cpp b/module-apps/application-settings/windows/SettingsMainWindow.cpp index e43066a52..6fc9d225f 100644 --- a/module-apps/application-settings/windows/SettingsMainWindow.cpp +++ b/module-apps/application-settings/windows/SettingsMainWindow.cpp @@ -120,7 +120,7 @@ bool SettingsMainWindow::onInput( const InputEvent& inputEvent ) { LOG_INFO("Enter pressed"); } else if( inputEvent.keyCode == KeyCode::KEY_RF ) { - sapm::ApplicationManager::messageSwitchApplication( application, "ApplicationDesktop", "MainWindow", nullptr ); + sapm::ApplicationManager::messageSwitchApplication( application, "ApplicationDesktop", "MenuWindow", nullptr ); return true; } diff --git a/module-apps/messages/AppMessage.hpp b/module-apps/messages/AppMessage.hpp index aac4a6609..0f2a2016a 100644 --- a/module-apps/messages/AppMessage.hpp +++ b/module-apps/messages/AppMessage.hpp @@ -20,16 +20,10 @@ namespace app { * @brief Template for all messages that go to application manager */ class AppMessage: public sys::DataMessage { -protected: - //name of the application to which switch is performed. - std::string application; public: - AppMessage( MessageType messageType, const std::string& application ) : - sys::DataMessage( static_cast(messageType)), - application{application} {}; + AppMessage( MessageType messageType ) : sys::DataMessage( static_cast(messageType)) {}; virtual ~AppMessage() {}; - std::string getApplicationName() { return application;}; }; //this message is used to notify application about switching event. Application will gain or lose focus upon receiving this message. @@ -38,23 +32,28 @@ class AppSwitchMessage : public AppMessage { protected: std::string window; std::unique_ptr data; + //name of the application to which switch is performed. + std::string application; public: AppSwitchMessage( const std::string& application, const std::string& window, std::unique_ptr data ) : - AppMessage( MessageType::AppSwitch, application), + AppMessage( MessageType::AppSwitch ), window{window}, - data {std::move(data)} {}; + data {std::move(data)}, + application{ application} {}; virtual ~AppSwitchMessage() {}; std::string getWindowName() { return window; }; std::unique_ptr& getData() { return data; }; + std::string getApplicationName() { return application;}; }; class AppRefreshMessage : public AppMessage { protected: gui::RefreshModes mode; public: - AppRefreshMessage( const std::string& application, gui::RefreshModes mode ) : - AppMessage( MessageType::AppRefresh, application), +// AppRefreshMessage( const std::string& application, gui::RefreshModes mode ) : + AppRefreshMessage( gui::RefreshModes mode ) : + AppMessage( MessageType::AppRefresh ), mode{ mode }{}; virtual ~AppRefreshMessage() {}; @@ -68,7 +67,7 @@ protected: std::unique_ptr data; public: AppSwitchWindowMessage( const std::string& window, uint32_t command, std::unique_ptr data ) : - AppMessage( MessageType::AppSwitchWindow, "" ), + AppMessage( MessageType::AppSwitchWindow ), window{window}, command{ command }, data {std::move(data)} {}; @@ -84,13 +83,18 @@ protected: gui::InputEvent event; public: AppInputEventMessage( gui::InputEvent evt ) : - AppMessage( MessageType::AppInputEvent, "" ), + AppMessage( MessageType::AppInputEvent ), event{evt} {}; virtual ~AppInputEventMessage() {}; const gui::InputEvent& getEvent() { return event; }; }; +class AppRebuildMessage : public AppMessage { +public: + AppRebuildMessage(): AppMessage( MessageType::AppRebuild ) {}; + virtual ~AppRebuildMessage() {}; }; +}; #endif /* MODULE_APPS_MESSAGES_APPMESSAGE_HPP_ */ diff --git a/module-apps/windows/AppWindow.hpp b/module-apps/windows/AppWindow.hpp index 3878bb7ea..bcb64cc1c 100644 --- a/module-apps/windows/AppWindow.hpp +++ b/module-apps/windows/AppWindow.hpp @@ -30,6 +30,7 @@ public: app::Application* getApplication() { return application; }; void setApplication( app::Application* app ) { application = app; }; + void rebuild() {}; }; } /* namespace gui */ diff --git a/module-services/service-appmgr/ApplicationManager.cpp b/module-services/service-appmgr/ApplicationManager.cpp index f8e9adda2..10612cd45 100644 --- a/module-services/service-appmgr/ApplicationManager.cpp +++ b/module-services/service-appmgr/ApplicationManager.cpp @@ -86,6 +86,16 @@ sys::Message_t ApplicationManager::DataReceivedHandler(sys::DataMessage* msgl) { LOG_INFO("APMregister %s %s", msg->getSenderName().c_str(), (msg->getStatus()?"true":"false")); handleRegisterApplication( msg ); } break; + case static_cast(MessageType::APMChangeLanguage) : { + sapm::APMChangeLanguage* msg = reinterpret_cast( msgl ); + std::string lang; + if( msg->getLanguage() == utils::Lang::En ) lang = "English"; + if( msg->getLanguage() == utils::Lang::Pl ) lang = "Polish"; + if( msg->getLanguage() == utils::Lang::De ) lang = "German"; + if( msg->getLanguage() == utils::Lang::Sp ) lang = "Spanish"; + LOG_INFO("APChangeLanguage; %s %s", msg->getSenderName().c_str(), lang.c_str()); + handleLanguageChange( msg ); + } break; default : { LOG_FATAL("Received unknown massage %d", msgType ); } break; @@ -103,14 +113,17 @@ void ApplicationManager::TickHandler(uint32_t id) { sys::ReturnCodes ApplicationManager::InitHandler() { //get settings to initialize language in applications - SettingsRecord settings = DBServiceAPI::SettingsGet(this); + settings = DBServiceAPI::SettingsGet(this); if( settings.language == SettingsLanguage::ENGLISH ) { utils::localize.Switch( utils::Lang::En ); } - if( settings.language == SettingsLanguage::POLISH ) { + else if( settings.language == SettingsLanguage::POLISH ) { utils::localize.Switch( utils::Lang::Pl ); } + else if( settings.language == SettingsLanguage::GERMAN ) { + utils::localize.Switch( utils::Lang::De ); + } else if( settings.language == SettingsLanguage::SPANISH ) { utils::localize.Switch( utils::Lang::Sp ); } @@ -288,6 +301,45 @@ bool ApplicationManager::handleRegisterApplication( APMRegister* msg ) { return true; } +bool ApplicationManager::handleLanguageChange( sapm::APMChangeLanguage* msg ) { + + //check if selected language is different than the one that is in the settings + //if they are the same, return doing nothing + SettingsLanguage requestedLanguage; + switch( msg->getLanguage()) { + case utils::Lang::En : requestedLanguage = SettingsLanguage::ENGLISH; break; + case utils::Lang::Pl : requestedLanguage = SettingsLanguage::POLISH; break; + case utils::Lang::De : requestedLanguage = SettingsLanguage::GERMAN; break; + case utils::Lang::Sp : requestedLanguage = SettingsLanguage::SPANISH; break; + default: requestedLanguage = SettingsLanguage::ENGLISH; break; + }; + + //if requested language is different than current update settings and i18 translations + if( requestedLanguage != settings.language ) { + settings = DBServiceAPI::SettingsGet(this); + settings.language = requestedLanguage; + DBServiceAPI::SettingsUpdate( this, settings ); + utils::localize.Switch( msg->getLanguage() ); + } + else { + LOG_WARN("Selected language is already set. Ignoring command."); + return true; + } + + //iterate over all applications in the background or foreground state and send them rebuild command + for( auto it = applications.begin(); it!=applications.end(); it++ ) { + if( it->second->state == app::Application::State::ACTIVE_BACKGROUND || + it->second->state == app::Application::State::ACTIVE_FORGROUND ) { + + app::Application::messageRebuildApplication(this, it->second->name); + } + } + + + + return true; +} + bool ApplicationManager::handleSwitchConfirmation( APMConfirmSwitch* msg ) { //this is the case when application manager is waiting for newly started application to confim that it has //successfully gained focus. @@ -353,30 +405,42 @@ bool ApplicationManager::messageSwitchApplication( sys::Service* sender, const s bool ApplicationManager::messageConfirmSwitch( sys::Service* sender) { auto msg = std::make_shared(sender->GetName() ); - - auto ret = sys::Bus::SendUnicast(msg, "ApplicationManager", sender ); -// return (ret.first == sys::ReturnCodes::Success )?true:false; + sys::Bus::SendUnicast(msg, "ApplicationManager", sender); return true; +// auto ret = sys::Bus::SendUnicast(msg, "ApplicationManager", sender, 500 ); +// return (ret.first == sys::ReturnCodes::Success )?true:false; } bool ApplicationManager::messageConfirmClose( sys::Service* sender) { auto msg = std::make_shared(sender->GetName() ); - auto ret = sys::Bus::SendUnicast(msg, "ApplicationManager", sender ); -// return (ret.first == sys::ReturnCodes::Success )?true:false; + sys::Bus::SendUnicast(msg, "ApplicationManager", sender); return true; +// auto ret = sys::Bus::SendUnicast(msg, "ApplicationManager", sender, 500 ); +// return (ret.first == sys::ReturnCodes::Success )?true:false; } bool ApplicationManager::messageSwitchPreviousApplication( sys::Service* sender ) { auto msg = std::make_shared(sender->GetName() ); - auto ret = sys::Bus::SendUnicast(msg, "ApplicationManager", sender); -// return (ret.first == sys::ReturnCodes::Success )?true:false; + sys::Bus::SendUnicast(msg, "ApplicationManager", sender); return true; +// auto ret = sys::Bus::SendUnicast(msg, "ApplicationManager", sender, 500); +// return (ret.first == sys::ReturnCodes::Success )?true:false; } bool ApplicationManager::messageRegisterApplication( sys::Service* sender, const bool& status ) { auto msg = std::make_shared(sender->GetName(), status ); - sys::Bus::SendUnicast(msg, "ApplicationManager", sender ); + sys::Bus::SendUnicast(msg, "ApplicationManager", sender); return true; +// auto ret = sys::Bus::SendUnicast(msg, "ApplicationManager", sender, 500 ); +// return (ret.first == sys::ReturnCodes::Success )?true:false; +} + +bool ApplicationManager::messageChangeLanguage( sys::Service* sender, utils::Lang language ) { + auto msg = std::make_shared( sender->GetName(), language ); + sys::Bus::SendUnicast(msg, "ApplicationManager", sender); + return true; +// auto ret = sys::Bus::SendUnicast(msg, "ApplicationManager", sender, 500 ); +// return (ret.first == sys::ReturnCodes::Success )?true:false; } } /* namespace sapm */ diff --git a/module-services/service-appmgr/ApplicationManager.hpp b/module-services/service-appmgr/ApplicationManager.hpp index fe461a7f4..d0a76a3ec 100644 --- a/module-services/service-appmgr/ApplicationManager.hpp +++ b/module-services/service-appmgr/ApplicationManager.hpp @@ -23,7 +23,6 @@ namespace sapm { class ApplicationDescription { public: - ApplicationDescription( std::string name, std::unique_ptr lanucher, bool closeable ); virtual ~ApplicationDescription() {} //name of the application. It's used to find proper application during switching @@ -54,6 +53,8 @@ class ApplicationManager: public sys::Service { WAITING_GET_FOCUS_CONFIRMATION }; + SettingsRecord settings; + std::map< std::string, ApplicationDescription* > applications; sys::SystemManager* systemManager; @@ -76,6 +77,7 @@ class ApplicationManager: public sys::Service { bool handleSwitchConfirmation( APMConfirmSwitch* msg ); bool handleSwitchPrevApplication( APMSwitchPrevApp* msg ); bool handleRegisterApplication( APMRegister* msg ); + bool handleLanguageChange( sapm::APMChangeLanguage* msg ); bool startApplication( const std::string& appName ); public: ApplicationManager( const std::string& name, sys::SystemManager* sysmgr, std::vector< std::unique_ptr >& launchers ); @@ -120,6 +122,10 @@ public: * If successful message will contain name and true value, otherwise false value will be transmitted. */ static bool messageRegisterApplication( sys::Service* sender, const bool& status ); + /** + * @brief Sends message to application manager to inform it about change of the phone's language performed by the user. + */ + static bool messageChangeLanguage( sys::Service* sender, utils::Lang language ); }; } /* namespace sapm */ diff --git a/module-services/service-appmgr/messages/APMMessage.hpp b/module-services/service-appmgr/messages/APMMessage.hpp index 0a4159ebe..58cac0105 100644 --- a/module-services/service-appmgr/messages/APMMessage.hpp +++ b/module-services/service-appmgr/messages/APMMessage.hpp @@ -14,6 +14,8 @@ #include "SwitchData.hpp" +#include "i18/i18.hpp" + namespace sapm { /* @@ -97,6 +99,17 @@ public: const std::string& getApplication() { return application; }; }; +class APMChangeLanguage: public APMMessage { +protected: + utils::Lang language; +public: + APMChangeLanguage( const std::string& senderName, utils::Lang language ) : + APMMessage( MessageType::APMChangeLanguage, senderName ), + language{ language } {} + + const utils::Lang& getLanguage() { return language; }; +}; + } /* namespace sapm */ diff --git a/module-services/service-evtmgr/EventManager.cpp b/module-services/service-evtmgr/EventManager.cpp index 6232bdf0b..29bae005d 100644 --- a/module-services/service-evtmgr/EventManager.cpp +++ b/module-services/service-evtmgr/EventManager.cpp @@ -100,9 +100,10 @@ sys::ReturnCodes EventManager::SleepHandler() { bool EventManager::messageSetApplication( sys::Service* sender, const std::string& applicationName ) { auto msg = std::make_shared( applicationName ); - auto ret = sys::Bus::SendUnicast(msg, "EventManager", sender ); - return true; +// auto ret = sys::Bus::SendUnicast(msg, "EventManager", sender, 200 ); // return (ret.first == sys::ReturnCodes::Success )?true:false; + sys::Bus::SendUnicast(msg, "EventManager", sender ); + return true; } diff --git a/module-utils/i18/i18.hpp b/module-utils/i18/i18.hpp index 9f1cfa008..c7ec9c553 100644 --- a/module-utils/i18/i18.hpp +++ b/module-utils/i18/i18.hpp @@ -22,6 +22,7 @@ namespace utils { enum class Lang{ En, Pl, + De, Sp }; @@ -29,6 +30,7 @@ namespace utils { const char* langEN_path = "sys/assets/lang/lang_en.json"; const char* langPL_path = "sys/assets/lang/lang_pl.json"; + const char* langDE_path = "sys/assets/lang/lang_de.json"; const char* langSP_path = "sys/assets/lang/lang_sp.json"; @@ -48,10 +50,12 @@ namespace utils { case Lang::Pl: path = langPL_path; break; - case Lang::En: path = langEN_path; break; + case Lang::De: + path = langDE_path; + break; case Lang::Sp: path = langSP_path; break; diff --git a/source/MessageType.hpp b/source/MessageType.hpp index 78f4740ff..1fe78d6f9 100644 --- a/source/MessageType.hpp +++ b/source/MessageType.hpp @@ -53,6 +53,8 @@ enum class MessageType { APMConfirmClose, //Sent by application to confirm completion of the close procedure APMRegister, //when application finishes initHandler it is sending this messag to inform whether init was successful or not. APMDeleydClose, //this message is sent internally from and to application manager to close specified application. + APMChangeLanguage,//this message is sent from any application to inform application manager that it should send gui rebuild command to all + //applications in background and currently active application. //keyboard messages KBDKeyEvent, @@ -62,6 +64,7 @@ enum class MessageType { AppSwitchWindow, //This is internal message transmitted within application to change window. Additional command and data are transmitted with it. AppInputEvent,//used after key event translation to send input event to application AppRefresh, + AppRebuild,//message used to notify application that it should recreate it's GUI (i.e. language has beed changed by the user) AppClose, AppFocus,