// 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 #include #include #include #include #include #include
namespace gui { AllDevicesWindow::AllDevicesWindow(app::Application *app) : BaseSettingsWindow(app, window::name::all_devices) { bluetoothSettingsModel = std::make_unique(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(data); bondedDevicesData != nullptr) { devices = bondedDevicesData->getDevices(); activeDevice.address = bondedDevicesData->getAddressOfConnectedDevice(); activeDevice.state = ActiveDevice::DeviceState::Connected; } else if (const auto pairingDeviceData = dynamic_cast(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::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