~aleteoryx/muditaos

ref: 8e6490863a74d30442b434f91b90a2f07095e2d3 muditaos/module-apps/application-settings/windows/bluetooth/AllDevicesWindow.cpp -rw-r--r-- 9.1 KiB
8e649086 — Mateusz Grzegorzek [BH-395] Librarize application-settings 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "AllDevicesWindow.hpp"

#include <application-settings/data/BondedDevicesData.hpp>
#include <application-settings/data/PairingDeviceData.hpp>
#include <application-settings/windows/WindowNames.hpp>

#include <DialogMetadataMessage.hpp>
#include <InputEvent.hpp>
#include <OptionSetting.hpp>
#include <header/AddElementAction.hpp>

namespace gui
{

    AllDevicesWindow::AllDevicesWindow(app::Application *app) : BaseSettingsWindow(app, window::name::all_devices)
    {
        bluetoothSettingsModel = std::make_unique<BluetoothSettingsModel>(application);
        bluetoothSettingsModel->requestBondedDevices();
        buildInterface();
    }

    void AllDevicesWindow::buildInterface()
    {
        setTitle(utils::translate("app_settings_bluetooth_all_devices"));

        optionsList = new gui::ListView(this,
                                        option::window::optionsListX,
                                        option::window::optionsListY,
                                        option::window::optionsListW,
                                        option::window::optionsListH,
                                        optionsModel,
                                        listview::ScrollBarType::Proportional);

        optionsList->prepareRebuildCallback = [this]() { recreateOptions(); };
        optionsModel->createData(options);
        setFocusItem(optionsList);

        header->navigationIndicatorAdd(new gui::header::AddElementAction(), gui::header::BoxSelection::Left);
    }

    void AllDevicesWindow::onBeforeShow(ShowMode mode, SwitchData *data)
    {
        if (mode == ShowMode::GUI_SHOW_RETURN) {
            bluetoothSettingsModel->stopScan();
            if (activeDevice.state == ActiveDevice::DeviceState::Connecting) {
                activeDevice.address.clear();
                activeDevice.state = ActiveDevice::DeviceState::Unknown;
            }
            else if (activeDevice.state == ActiveDevice::DeviceState::Pairing) {
                devices.erase(std::remove_if(devices.begin(),
                                             devices.end(),
                                             [&](const auto &device) {
                                                 return (bd_addr_to_str(device.address) == activeDevice.address);
                                             }),
                              devices.end());

                activeDevice.address.clear();
                activeDevice.state = ActiveDevice::DeviceState::Unknown;
            }
        }
        if (const auto bondedDevicesData = dynamic_cast<BondedDevicesData *>(data); bondedDevicesData != nullptr) {
            devices              = bondedDevicesData->getDevices();
            activeDevice.address = bondedDevicesData->getAddressOfConnectedDevice();
            activeDevice.state   = ActiveDevice::DeviceState::Connected;
        }
        else if (const auto pairingDeviceData = dynamic_cast<PairingDeviceData *>(data); pairingDeviceData != nullptr) {
            activeDevice.address = bd_addr_to_str(pairingDeviceData->getPairingDevice().address);
            activeDevice.state   = ActiveDevice::DeviceState::Pairing;
            devices.emplace_back(pairingDeviceData->getPairingDevice());
        }
        refreshOptionsList();
    }

    auto AllDevicesWindow::onInput(const InputEvent &inputEvent) -> bool
    {
        if (!inputEvent.isShortRelease()) {
            return AppWindow::onInput(inputEvent);
        }
        if (inputEvent.is(KeyCode::KEY_LEFT)) {
            bluetoothSettingsModel->requestScan();
            application->switchWindow(gui::window::name::dialog_settings,
                                      gui::ShowMode::GUI_SHOW_INIT,
                                      std::make_unique<gui::DialogMetadataMessage>(gui::DialogMetadata{
                                          utils::translate("app_settings_bluetooth_add_device"),
                                          "search_big",
                                          utils::translate("app_settings_bluetooth_searching_devices")}));
            return true;
        }
        if (inputEvent.is(KeyCode::KEY_LF)) {
            devices.erase(std::remove_if(devices.begin(),
                                         devices.end(),
                                         [&](const auto &device) {
                                             return (bd_addr_to_str(device.address) == addressOfDeviceSelected);
                                         }),
                          devices.end());
            bottomBar->setActive(BottomBar::Side::LEFT, false);
            bottomBar->setActive(BottomBar::Side::CENTER, false);
            bluetoothSettingsModel->requestDeviceUnpair(addressOfDeviceSelected);
            refreshOptionsList();
            return true;
        }
        return AppWindow::onInput(inputEvent);
    }

    auto AllDevicesWindow::buildOptionsList() -> std::list<Option>
    {
        bottomBar->setActive(BottomBar::Side::CENTER, !devices.empty());

        std::list<gui::Option> optionsList;
        for (const auto &device : devices) {
            ActiveDevice newDevice(bd_addr_to_str(device.address));
            newDevice.address == activeDevice.address ? newDevice.state = activeDevice.state
                                                      : newDevice.state = ActiveDevice::DeviceState::Paired;
            UTF8 textOnCenter                                           = getTextOnCenter(newDevice.state);
            option::SettingRightItem rightItem                          = getRightItem(newDevice.state);
            UTF8 textOnRight                                            = getTextOnRight(newDevice.state);

            optionsList.emplace_back(std::make_unique<gui::option::OptionSettings>(
                device.name,
                [=](gui::Item & /*item*/) {
                    return handleDeviceAction(newDevice);
                },
                [=](gui::Item &item) {
                    if (item.focus) {
                        this->setBottomBarText(textOnCenter, BottomBar::Side::CENTER);
                        if (newDevice.state != ActiveDevice::DeviceState::Pairing) {
                            this->setBottomBarText(utils::translate("common_forget"), BottomBar::Side::LEFT);
                            this->bottomBar->setActive(BottomBar::Side::LEFT, true);
                        }
                        addressOfDeviceSelected = newDevice.address;
                    }
                    return true;
                },
                nullptr,
                rightItem,
                false,
                std::move(textOnRight)));
        }
        return optionsList;
    }

    UTF8 AllDevicesWindow::getTextOnCenter(const ActiveDevice::DeviceState &state) const
    {
        switch (state) {
        case ActiveDevice::DeviceState::Connected:
            return utils::translate("common_disconnect");
        case ActiveDevice::DeviceState::Paired:
            return utils::translate("common_connect");
        case ActiveDevice::DeviceState::Pairing:
        case ActiveDevice::DeviceState::Connecting:
        case ActiveDevice::DeviceState::Unknown:
            break;
        }
        return UTF8();
    }

    UTF8 AllDevicesWindow::getTextOnRight(const ActiveDevice::DeviceState &state) const
    {
        switch (state) {
        case ActiveDevice::DeviceState::Connected:
            return utils::translate("app_settings_option_connected");
        case ActiveDevice::DeviceState::Connecting:
            return utils::translate("app_settings_option_connecting");
        case ActiveDevice::DeviceState::Pairing:
            return utils::translate("app_settings_option_pairing");
        case ActiveDevice::DeviceState::Paired:
        case ActiveDevice::DeviceState::Unknown:
            break;
        }
        return UTF8();
    }

    option::SettingRightItem AllDevicesWindow::getRightItem(const ActiveDevice::DeviceState &state) const
    {
        switch (state) {
        case ActiveDevice::DeviceState::Connected:
        case ActiveDevice::DeviceState::Connecting:
        case ActiveDevice::DeviceState::Pairing:
            return option::SettingRightItem::Text;
        case ActiveDevice::DeviceState::Paired:
            return option::SettingRightItem::Bt;
        case ActiveDevice::DeviceState::Unknown:
            break;
        }
        return option::SettingRightItem::Disabled;
    }

    auto AllDevicesWindow::handleDeviceAction(const ActiveDevice &newDevice) -> bool
    {
        if (newDevice.state == ActiveDevice::DeviceState::Connected) {
            activeDevice.address.clear();
            activeDevice.state = ActiveDevice::DeviceState::Unknown;
            bluetoothSettingsModel->requestDisconnection();
            refreshOptionsList();
        }
        else if (newDevice.state == ActiveDevice::DeviceState::Paired) {
            activeDevice.address = newDevice.address;
            activeDevice.state   = ActiveDevice::DeviceState::Connecting;
            bluetoothSettingsModel->requestConnection(newDevice.address);
            refreshOptionsList();
        }
        return true;
    }

} // namespace gui