~aleteoryx/muditaos

ref: e8f7a57219ea7c881cf53322fe6a3c6b0c5e7c56 muditaos/module-apps/application-settings-new/windows/AllDevicesWindow.cpp -rw-r--r-- 5.0 KiB
e8f7a572 — Tomasz Langowski [EGD-5758] Restore auto locking timer in ApplicationManager 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
// 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-new/ApplicationSettings.hpp"
#include "application-settings-new/data/BondedDevicesData.hpp"
#include "application-settings-new/widgets/SettingsStyle.hpp"

#include "DialogMetadataMessage.hpp"
#include "OptionSetting.hpp"

#include <InputEvent.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::localize.get("app_settings_bluetooth_all_devices"));
        leftArrowImage = new gui::Image(this,
                                        style::settings::window::leftArrowImage::x,
                                        style::settings::window::leftArrowImage::y,
                                        style::settings::window::leftArrowImage::w,
                                        style::settings::window::leftArrowImage::h,
                                        "arrow_left");
        crossImage     = new gui::Image(this,
                                    style::settings::window::crossImage::x,
                                    style::settings::window::crossImage::y,
                                    style::settings::window::crossImage::w,
                                    style::settings::window::crossImage::h,
                                    "cross");
    }

    void AllDevicesWindow::onBeforeShow(ShowMode /*mode*/, SwitchData *data)
    {
        const auto newData = dynamic_cast<BondedDevicesData *>(data);
        if (newData != nullptr) {
            devices                  = newData->getDevices();
            addressOfConnectedDevice = newData->getAddressOfConnectedDevice();
        }
        refreshOptionsList();
    }

    auto AllDevicesWindow::onInput(const InputEvent &inputEvent) -> bool
    {
        if (!inputEvent.isShortPress()) {
            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::localize.get("app_settings_bluetooth_add_device"),
                                          "search_big",
                                          utils::localize.get("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) == addressOfSelectedDevice);
                                         }),
                          devices.end());
            refreshOptionsList();
            bluetoothSettingsModel->requestDeviceUnpair(addressOfSelectedDevice);
            return true;
        }
        return AppWindow::onInput(inputEvent);
    }

    auto AllDevicesWindow::buildOptionsList() -> std::list<Option>
    {
        std::list<gui::Option> optionsList;
        for (const auto &device : devices) {
            std::string addr{bd_addr_to_str(device.address)};
            auto isConnected = addr == addressOfConnectedDevice;
            optionsList.emplace_back(std::make_unique<gui::option::OptionSettings>(
                device.name,
                [=](gui::Item & /*item*/) {
                    LOG_DEBUG("Device: %s", device.name.c_str());
                    bluetoothSettingsModel->requestConnection(addr);
                    return true;
                },
                [=](gui::Item &item) {
                    if (item.focus) {
                        this->setBottomBarText(isConnected ? utils::translateI18("common_disconnect")
                                                           : utils::translateI18("common_connect"),
                                               BottomBar::Side::CENTER);
                        this->setBottomBarText(utils::translateI18("common_forget"), BottomBar::Side::LEFT);
                        this->bottomBar->setActive(BottomBar::Side::LEFT, true);
                        addressOfSelectedDevice = addr;
                    }
                    return true;
                },
                nullptr,
                isConnected ? gui::option::SettingRightItem::Text : gui::option::SettingRightItem::Bt,
                false,
                utils::localize.get("app_settings_option_connected")));
        }
        return optionsList;
    }
} // namespace gui