~aleteoryx/muditaos

f8c83ce25c99b8e2d425908415f80ca662c91c60 — SP2FET 5 years ago 97f6073
[EGD-4869] Add bluetooth message status to application settings

This change demonstrates how to work with Bluetooth messages
in application settings windows.
Responses to bluetooth status messages are mocked because
settings storage and message handlers in Service Bluetooth
are not ready yet.
M module-apps/application-settings-new/ApplicationSettings.cpp => module-apps/application-settings-new/ApplicationSettings.cpp +8 -2
@@ 30,8 30,7 @@
#include <service-bluetooth/BluetoothMessage.hpp>
#include <service-cellular/CellularServiceAPI.hpp>
#include <service-db/Settings.hpp>
#include <module-services/service-db/agents/settings/SystemSettings.hpp>
#include <i18n/i18n.hpp>
#include <module-services/service-bluetooth/service-bluetooth/messages/Status.hpp>

namespace app
{


@@ 91,6 90,13 @@ namespace app
                currentWindow->rebuild();
            }
        }
        else if (auto responseStatusMsg = dynamic_cast<message::bluetooth::ResponseStatus *>(msgl);
                 nullptr != responseStatusMsg) {
            if (gui::window::name::bluetooth == getCurrentWindow()->getName()) {
                auto btStatusData = std::make_unique<gui::BluetoothStatusData>(responseStatusMsg->getStatus());
                switchWindow(gui::window::name::bluetooth, std::move(btStatusData));
            }
        }

        return std::make_shared<sys::ResponseMessage>();
    }

M module-apps/application-settings-new/windows/BluetoothWindow.cpp => module-apps/application-settings-new/windows/BluetoothWindow.cpp +22 -19
@@ 6,8 6,9 @@

#include "OptionSetting.hpp"

#include <i18n/i18n.hpp>
#include <service-bluetooth/BluetoothMessage.hpp>
#include <service-bluetooth/Constants.hpp>
#include <service-bluetooth/messages/Status.hpp>
#include <service-bluetooth/messages/SetStatus.hpp>

namespace gui
{


@@ 16,10 17,17 @@ namespace gui
    {
        topBar->setActive(TopBar::Elements::BATTERY, false);
        topBar->setActive(TopBar::Elements::SIM, false);
        sys::Bus::SendUnicast(
            std::make_shared<message::bluetooth::RequestStatus>(), service::name::bluetooth, application);
    }

    void BluetoothWindow::onBeforeShow(ShowMode mode, SwitchData *data)
    {
        if (data != nullptr) {
            const auto newData        = static_cast<BluetoothStatusData *>(data);
            isBluetoothSwitchOn       = newData->getState();
            isPhoneVisibilitySwitchOn = newData->getVisibility();
        }
        rebuildOptionList();
    }



@@ 30,7 38,7 @@ namespace gui
        optionsList.emplace_back(std::make_unique<gui::OptionSettings>(
            utils::translateI18("app_settings_bluetooth_main"),
            [=](gui::Item &item) {
                bluetoothSwitchHandler(isBluetoothSwitchOn);
                switchHandler(isBluetoothSwitchOn);
                return true;
            },
            [=](gui::Item &item) {


@@ 62,7 70,7 @@ namespace gui
            optionsList.emplace_back(std::make_unique<gui::OptionSettings>(
                utils::translateI18("app_settings_bluetooth_phone_visibility"),
                [=](gui::Item &item) {
                    phoneVisibilitySwitchHandler(isPhoneVisibilitySwitchOn);
                    switchHandler(isPhoneVisibilitySwitchOn);
                    return true;
                },
                [=](gui::Item &item) {


@@ 96,27 104,22 @@ namespace gui
        return optionsList;
    }

    void BluetoothWindow::bluetoothSwitchHandler(bool &switchState)
    void BluetoothWindow::switchHandler(bool &switchState)
    {
        if (switchState) {
            sys::Bus::SendUnicast(
                std::make_shared<BluetoothMessage>(BluetoothMessage::Request::Stop), "ServiceBluetooth", application);
        switchState = !switchState;
        BluetoothStatus btStatus;

        if (isBluetoothSwitchOn) {
            btStatus.state = BluetoothStatus::BluetoothState::On;
        }
        else {
            sys::Bus::SendUnicast(
                std::make_shared<BluetoothMessage>(BluetoothMessage::Request::Start), "ServiceBluetooth", application);
            btStatus.state = BluetoothStatus::BluetoothState::Off;
        }
        switchState = !switchState;
        rebuildOptionList();
    }
        btStatus.visibility = isPhoneVisibilitySwitchOn;
        message::bluetooth::SetStatus setStatus(btStatus);

    void BluetoothWindow::phoneVisibilitySwitchHandler(bool &switchState)
    {
        sys::Bus::SendUnicast(
            std::make_shared<BluetoothMessage>(BluetoothMessage::Request::Visible), "ServiceBluetooth", application);

        switchState = !switchState;
        rebuildOptionList();
            std::make_shared<message::bluetooth::SetStatus>(setStatus), service::name::bluetooth, application);
    }

    void BluetoothWindow::rebuildOptionList()

M module-apps/application-settings-new/windows/BluetoothWindow.hpp => module-apps/application-settings-new/windows/BluetoothWindow.hpp +24 -3
@@ 4,6 4,7 @@
#pragma once

#include <OptionWindow.hpp>
#include <service-bluetooth/BluetoothMessage.hpp>

namespace gui
{


@@ 17,8 18,28 @@ namespace gui
        bool isBluetoothSwitchOn       = false;
        bool isPhoneVisibilitySwitchOn = false;
        auto bluetoothOptionsList() -> std::list<gui::Option>;
        void bluetoothSwitchHandler(bool &switchState);
        void phoneVisibilitySwitchHandler(bool &switchState);
        void switchHandler(bool &switchState);
        void rebuildOptionList();
    };
}; // namespace gui

    class BluetoothStatusData : public SwitchData
    {
      public:
        explicit BluetoothStatusData(BluetoothStatus status) : SwitchData(), status(std::move(status))
        {}
        [[nodiscard]] auto getState() const noexcept -> bool
        {
            if (status.state == BluetoothStatus::BluetoothState::On) {
                return true;
            }
            return false;
        }
        [[nodiscard]] auto getVisibility() const noexcept -> bool
        {
            return status.visibility;
        }

      private:
        BluetoothStatus status;
    };
} // namespace gui

M module-services/service-bluetooth/ServiceBluetooth.cpp => module-services/service-bluetooth/ServiceBluetooth.cpp +15 -1
@@ 13,6 13,8 @@
#include <Service/Service.hpp>
#include <Service/Message.hpp>
#include <service-db/Settings.hpp>
#include "service-bluetooth/messages/Status.hpp"
#include "service-bluetooth/messages/SetStatus.hpp"

#include <log/log.hpp>



@@ 37,7 39,8 @@ sys::ReturnCodes ServiceBluetooth::InitHandler()
{
    LOG_ERROR("Bluetooth experimental!");
    worker = std::make_unique<BluetoothWorker>(this);

    btStatus.state      = BluetoothStatus::BluetoothState::On;
    btStatus.visibility = true;
    return sys::ReturnCodes::Success;
}



@@ 49,6 52,17 @@ sys::ReturnCodes ServiceBluetooth::DeinitHandler()

sys::MessagePointer ServiceBluetooth::DataReceivedHandler(sys::DataMessage *msg, sys::ResponseMessage *resp)
{
    // mock response on message::bluetooth::RequestStatus
    if (auto requestStatusMsg = dynamic_cast<message::bluetooth::RequestStatus *>(msg); nullptr != requestStatusMsg) {
        sys::Bus::SendUnicast(std::make_shared<message::bluetooth::ResponseStatus>(btStatus), msg->sender, this);
    }

    // temporary solution for handling message::bluetooth::SetStatus
    if (auto setStatusMsg = dynamic_cast<message::bluetooth::SetStatus *>(msg); nullptr != setStatusMsg) {
        btStatus = setStatusMsg->getStatus();
        sys::Bus::SendBroadcast(std::make_shared<message::bluetooth::ResponseStatus>(btStatus), this);
    }

    try {
        switch (static_cast<MessageType>(msg->messageType)) {
        case MessageType::BluetoothRequest: {

M module-services/service-bluetooth/service-bluetooth/ServiceBluetooth.hpp => module-services/service-bluetooth/service-bluetooth/ServiceBluetooth.hpp +2 -0
@@ 8,6 8,7 @@
#include <Service/Message.hpp>
#include <Service/Service.hpp>
#include "service-bluetooth/SettingsHolder.hpp"
#include "BluetoothMessage.hpp"

#include <memory> // for unique_ptr



@@ 32,4 33,5 @@ class ServiceBluetooth : public sys::Service
  private:
    std::unique_ptr<BluetoothWorker> worker;
    std::unique_ptr<Bluetooth::SettingsHolder> settingsHolder;
    BluetoothStatus btStatus; // will be replaced with settings storage introduced in [EGD-4579]
};