From cda64d4f8af13fe1d6c4c75dc4b435590f548409 Mon Sep 17 00:00:00 2001 From: Pawel Olejniczak Date: Wed, 17 Feb 2021 17:12:24 +0100 Subject: [PATCH] [EGD-4340] Refactor bluetooth settings and add device windows Rewrote these windows in a proper way. Fixed few minor issues. GUI adjustments. --- .../ApplicationSettings.cpp | 42 +++++++------ .../application-settings-new/CMakeLists.txt | 1 + .../data/BluetoothStatusData.hpp | 37 ++++++++++++ .../data/DeviceData.hpp | 23 +++++++ .../models/BluetoothSettingsModel.cpp | 34 +++++++++++ .../models/BluetoothSettingsModel.hpp | 25 ++++++++ .../windows/AddDeviceWindow.cpp | 43 ++++++------- .../windows/AddDeviceWindow.hpp | 31 ++++------ .../windows/AllDevicesWindow.cpp | 10 ++-- .../windows/BluetoothWindow.cpp | 60 +++++++------------ .../windows/BluetoothWindow.hpp | 55 ++++------------- .../Bluetooth/interface/profiles/GAP/GAP.cpp | 3 +- .../messages/BondedDevices.hpp | 8 +-- .../service-bluetooth/messages/DeviceName.hpp | 6 +- .../messages/ResponseVisibleDevices.hpp | 8 +-- .../messages/SetBondedDevices.hpp | 8 +-- .../messages/SetDeviceName.hpp | 6 +- .../messages/SetScanState.hpp | 7 ++- .../service-bluetooth/messages/SetStatus.hpp | 8 +-- .../service-bluetooth/messages/Status.hpp | 8 +-- 20 files changed, 242 insertions(+), 181 deletions(-) create mode 100644 module-apps/application-settings-new/data/BluetoothStatusData.hpp create mode 100644 module-apps/application-settings-new/data/DeviceData.hpp create mode 100644 module-apps/application-settings-new/models/BluetoothSettingsModel.cpp create mode 100644 module-apps/application-settings-new/models/BluetoothSettingsModel.hpp diff --git a/module-apps/application-settings-new/ApplicationSettings.cpp b/module-apps/application-settings-new/ApplicationSettings.cpp index 9a63e80fcd36dc8ad19fc6a23adbf859827c275e..3965d9182e38aa70153b9e7027339bd488f2eb08 100644 --- a/module-apps/application-settings-new/ApplicationSettings.cpp +++ b/module-apps/application-settings-new/ApplicationSettings.cpp @@ -42,9 +42,12 @@ #include #include #include +#include #include #include #include +#include +#include #include #include #include @@ -69,7 +72,7 @@ namespace app ApplicationSettingsNew::ApplicationSettingsNew(std::string name, std::string parent, StartInBackground startInBackground) - : Application(name, parent, startInBackground) + : Application(std::move(name), std::move(parent), startInBackground) { if ((Store::GSM::SIM::SIM1 == selectedSim || Store::GSM::SIM::SIM2 == selectedSim) && Store::GSM::get()->sim == selectedSim) { @@ -78,8 +81,7 @@ namespace app } ApplicationSettingsNew::~ApplicationSettingsNew() - { - } + {} // Invoked upon receiving data message auto ApplicationSettingsNew::DataReceivedHandler(sys::DataMessage *msgl, @@ -92,16 +94,7 @@ namespace app return retMsg; } - if (auto btMsg = dynamic_cast(msgl); btMsg != nullptr) { - auto scannedBtDevices = btMsg->devices; - LOG_INFO("Received BT Scan message!"); - - auto data = std::make_unique(scannedBtDevices); - windowsFactory.build(this, gui::window::name::add_device); - switchWindow(gui::window::name::add_device, gui::ShowMode::GUI_SHOW_INIT, std::move(data)); - render(gui::RefreshModes::GUI_REFRESH_FAST); - } - else if (auto phoneMsg = dynamic_cast(msgl); nullptr != phoneMsg) { + if (auto phoneMsg = dynamic_cast(msgl); nullptr != phoneMsg) { selectedSim = Store::GSM::get()->selected; if (CellularNotificationMessage::Type::SIM_READY == phoneMsg->type) { selectedSimNumber = CellularServiceAPI::GetOwnNumber(this); @@ -155,6 +148,19 @@ namespace app return sys::MessageNone{}; }); + connect(typeid(::message::bluetooth::ResponseVisibleDevices), [&](sys::Message *msg) { + auto responseVisibleDevicesMsg = static_cast<::message::bluetooth::ResponseVisibleDevices *>(msg); + if (gui::window::name::add_device == getCurrentWindow()->getName() || + gui::window::name::dialog_settings == getCurrentWindow()->getName()) { + auto visibleDevicesData = std::make_unique(responseVisibleDevicesMsg->getDevices()); + if (gui::window::name::dialog_settings == getCurrentWindow()->getName()) { + visibleDevicesData->ignoreCurrentWindowOnStack = true; + } + switchWindow(gui::window::name::add_device, std::move(visibleDevicesData)); + } + return sys::MessageNone{}; + }); + connect(typeid(CellularGetAPNResponse), [&](sys::Message *msg) { if (gui::window::name::apn_settings == getCurrentWindow()->getName()) { auto apns = dynamic_cast(msg); @@ -189,11 +195,11 @@ namespace app ::settings::SettingsScope::Global); settings->registerValueChange( ::settings::SystemProperties::lockPassHash, - [this](std::string value) { lockPassHash = utils::getNumericValue(value); }, + [this](const std::string &value) { lockPassHash = utils::getNumericValue(value); }, ::settings::SettingsScope::Global); settings->registerValueChange( ::settings::Bluetooth::state, - [this](std::string value) { + [this](const std::string &value) { if (gui::window::name::bluetooth == getCurrentWindow()->getName()) { const auto isBtOn = utils::getNumericValue(value); auto btStatusData = std::make_unique( @@ -204,7 +210,7 @@ namespace app ::settings::SettingsScope::Global); settings->registerValueChange( ::settings::Bluetooth::deviceVisibility, - [this](std::string value) { + [this](const std::string &value) { if (gui::window::name::bluetooth == getCurrentWindow()->getName()) { const auto isVisible = utils::getNumericValue(value); auto btStatusData = std::make_unique(isVisible); @@ -346,7 +352,7 @@ namespace app { operatorsOn = value; LOG_DEBUG("[ApplicationSettingsNew::setOperatorsOn] to %d", operatorsOn); - settings->setValue(settings::operators_on, std::to_string(value)); + settings->setValue(settings::operators_on, std::to_string(static_cast(value))); } void ApplicationSettingsNew::setVoLTEOn(bool value) @@ -432,7 +438,7 @@ namespace app return false; } - return {msgState->success}; + return msgState->success; } return false; } diff --git a/module-apps/application-settings-new/CMakeLists.txt b/module-apps/application-settings-new/CMakeLists.txt index 857944c7fe0a13fed8a711a12a3029dd399385db..c5233a36366ff0bd8b854a82de916de79787212c 100644 --- a/module-apps/application-settings-new/CMakeLists.txt +++ b/module-apps/application-settings-new/CMakeLists.txt @@ -16,6 +16,7 @@ target_sources( ${PROJECT_NAME} PRIVATE ApplicationSettings.cpp models/ApnSettingsModel.cpp + models/BluetoothSettingsModel.cpp models/NewApnModel.cpp widgets/timeWidget.cpp widgets/ChangePasscodeLockHandler.cpp diff --git a/module-apps/application-settings-new/data/BluetoothStatusData.hpp b/module-apps/application-settings-new/data/BluetoothStatusData.hpp new file mode 100644 index 0000000000000000000000000000000000000000..624ae15ca581f5925197bf7e3abb578e6b1ac958 --- /dev/null +++ b/module-apps/application-settings-new/data/BluetoothStatusData.hpp @@ -0,0 +1,37 @@ +// 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 + +namespace gui +{ + class BluetoothStatusData : public SwitchData + { + public: + explicit BluetoothStatusData(BluetoothStatus::State state) : state{state} + {} + explicit BluetoothStatusData(bool visibility) : visibility{visibility} + {} + BluetoothStatusData(BluetoothStatus::State state, bool visibility) : state{state}, visibility{visibility} + {} + + [[nodiscard]] auto getState() const noexcept -> std::optional + { + if (state == BluetoothStatus::State::On) { + return true; + } + return false; + } + [[nodiscard]] auto getVisibility() const noexcept -> std::optional + { + return visibility; + } + + private: + std::optional state{}; + std::optional visibility{}; + }; +} // namespace gui diff --git a/module-apps/application-settings-new/data/DeviceData.hpp b/module-apps/application-settings-new/data/DeviceData.hpp new file mode 100644 index 0000000000000000000000000000000000000000..237899b74ba82322d9bad8265474f278cdd91a3a --- /dev/null +++ b/module-apps/application-settings-new/data/DeviceData.hpp @@ -0,0 +1,23 @@ +// 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 + +namespace gui +{ + class DeviceData : public SwitchData + { + public: + explicit DeviceData(std::vector devices) : devices(std::move(devices)) + {} + auto getDevices() -> const std::vector & + { + return devices; + } + + private: + std::vector devices{}; + }; +} // namespace gui diff --git a/module-apps/application-settings-new/models/BluetoothSettingsModel.cpp b/module-apps/application-settings-new/models/BluetoothSettingsModel.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c485caca283bb44f93c73cf0dce7f880f46e9323 --- /dev/null +++ b/module-apps/application-settings-new/models/BluetoothSettingsModel.cpp @@ -0,0 +1,34 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#include "BluetoothSettingsModel.hpp" + +#include +#include +#include + +BluetoothSettingsModel::BluetoothSettingsModel(app::Application *application) : application{application} +{} + +void BluetoothSettingsModel::requestStatus() +{ + application->bus.sendUnicast(std::make_shared<::message::bluetooth::RequestStatus>(), service::name::bluetooth); +} +void BluetoothSettingsModel::setStatus(bool desiredBluetoothState, bool desiredVisibility) +{ + BluetoothStatus status{.state = desiredBluetoothState ? BluetoothStatus::State::On : BluetoothStatus::State::Off, + .visibility = desiredVisibility}; + message::bluetooth::SetStatus setStatus(status); + application->bus.sendUnicast(std::make_shared<::message::bluetooth::SetStatus>(std::move(setStatus)), + service::name::bluetooth); +} +void BluetoothSettingsModel::stopScan() +{ + application->bus.sendUnicast(std::make_shared(BluetoothMessage::Request::StopScan), + service::name::bluetooth); +} + +void BluetoothSettingsModel::setAddrForAudioProfiles(std::string addr) +{ + application->bus.sendUnicast(std::make_shared(std::move(addr)), service::name::bluetooth); +} diff --git a/module-apps/application-settings-new/models/BluetoothSettingsModel.hpp b/module-apps/application-settings-new/models/BluetoothSettingsModel.hpp new file mode 100644 index 0000000000000000000000000000000000000000..af495a1de16862b199b99967c03de1596cb2c8b2 --- /dev/null +++ b/module-apps/application-settings-new/models/BluetoothSettingsModel.hpp @@ -0,0 +1,25 @@ +// 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 "Application.hpp" + +extern "C" +{ +#include +} + +class BluetoothSettingsModel +{ + public: + explicit BluetoothSettingsModel(app::Application *application); + + void requestStatus(); + void setStatus(bool desiredBluetoothState, bool desiredVisibility); + void stopScan(); + void setAddrForAudioProfiles(std::string addr); + + private: + app::Application *application = nullptr; +}; diff --git a/module-apps/application-settings-new/windows/AddDeviceWindow.cpp b/module-apps/application-settings-new/windows/AddDeviceWindow.cpp index b81afca520a0139f54ece79478b89fe715e0a485..947c627614e6dd7151f6f8f1d1759ef050d665eb 100644 --- a/module-apps/application-settings-new/windows/AddDeviceWindow.cpp +++ b/module-apps/application-settings-new/windows/AddDeviceWindow.cpp @@ -3,12 +3,10 @@ #include "AddDeviceWindow.hpp" #include "application-settings-new/ApplicationSettings.hpp" +#include "application-settings-new/data/DeviceData.hpp" #include "OptionSetting.hpp" -#include -#include - extern "C" { #include @@ -17,30 +15,35 @@ extern "C" namespace gui { - AddDeviceWindow::AddDeviceWindow(app::Application *app) : OptionWindow(app, gui::window::name::add_device) - {} + AddDeviceWindow::AddDeviceWindow(app::Application *app, std::string name) : BaseSettingsWindow(app, std::move(name)) + { + bluetoothSettingsModel = std::make_unique(application); + } - void AddDeviceWindow::onBeforeShow(ShowMode mode, SwitchData *data) + void AddDeviceWindow::onBeforeShow(ShowMode /*mode*/, SwitchData *data) { - if (data != nullptr) { - auto newData = static_cast(data); - devices = newData->getDevices(); + const auto newData = dynamic_cast(data); + if (newData != nullptr) { + devices = newData->getDevices(); } - rebuildOptionList(); + refreshOptionsList(); + } + + void AddDeviceWindow::onClose() + { + bluetoothSettingsModel->stopScan(); } - auto AddDeviceWindow::devicesOptionsList() -> std::list + auto AddDeviceWindow::buildOptionsList() -> std::list { std::list optionsList; for (const auto &device : devices) { optionsList.emplace_back(std::make_unique( device.name, - [=](gui::Item &item) { + [=](gui::Item & /*unused*/) { LOG_DEBUG("Device: %s", device.name.c_str()); - application->bus.sendUnicast(std::make_shared(bd_addr_to_str(device.address)), - "ServiceBluetooth", - 5000); + bluetoothSettingsModel->setAddrForAudioProfiles(bd_addr_to_str(device.address)); return true; }, nullptr, @@ -48,18 +51,8 @@ namespace gui gui::option::SettingRightItem::Bt)); } - application->bus.sendUnicast(std::make_shared(BluetoothMessage::Request::StopScan), - "ServiceBluetooth"); - bottomBar->setText(BottomBar::Side::CENTER, utils::localize.get(style::strings::common::add)); return optionsList; } - - void AddDeviceWindow::rebuildOptionList() - { - clearOptions(); - addOptions(devicesOptionsList()); - } - } // namespace gui diff --git a/module-apps/application-settings-new/windows/AddDeviceWindow.hpp b/module-apps/application-settings-new/windows/AddDeviceWindow.hpp index cb6798b4bf5e720a9085182b697dcdf712ee7a4a..e7d941969a9ac04b957132544e7ea630d568ce38 100644 --- a/module-apps/application-settings-new/windows/AddDeviceWindow.hpp +++ b/module-apps/application-settings-new/windows/AddDeviceWindow.hpp @@ -1,36 +1,27 @@ -// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. +// 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 "OptionWindow.hpp" +#include "BaseSettingsWindow.hpp" +#include "application-settings-new/ApplicationSettings.hpp" +#include "application-settings-new/models/BluetoothSettingsModel.hpp" -#include +#include namespace gui { - class AddDeviceWindow : public OptionWindow + class AddDeviceWindow : public BaseSettingsWindow { public: - AddDeviceWindow(app::Application *app); - void onBeforeShow(ShowMode mode, SwitchData *data) override; + explicit AddDeviceWindow(app::Application *app, std::string name = window::name::add_device); private: - auto devicesOptionsList() -> std::list; - void rebuildOptionList(); - std::vector devices; - }; + void onBeforeShow(ShowMode mode, SwitchData *data) override; + void onClose() override; + auto buildOptionsList() -> std::list