From 14c3bea3a90d54577ae1f6a124f61e90fb05c731 Mon Sep 17 00:00:00 2001 From: Raquel Medina Date: Tue, 23 Jun 2020 13:59:15 +0200 Subject: add checks to avoid duplicates in qtappfw-bt model Duplicate bluetooth entries have been seen on settings app' bluetooth page, following disconnect/connect cycles. They were caused by multiple connected events associated to the same device on the same adapter. This patch avoids the duplicates in the model when only one bluetooth adapter is present on the target, duplicates in the model when more than one bluetooth adapter is present is to be covered by SPEC-3294. The current patch: - tests the device id present on "device_changes" events data, as this id is used to index devices in the model. - treats "device_changes added" events on an existing device in the model as if a "changed" action was received for it. Bug-AGL: SPEC-3424 Signed-off-by: Raquel Medina Change-Id: Ia5f273f456383880b2d855e567bdf6b41ed98352 --- bluetooth/bluetooth.cpp | 25 ++++++++++++++++--------- bluetooth/bluetoothmodel.cpp | 3 +++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/bluetooth/bluetooth.cpp b/bluetooth/bluetooth.cpp index cea89ad..fe1212f 100644 --- a/bluetooth/bluetooth.cpp +++ b/bluetooth/bluetooth.cpp @@ -228,16 +228,23 @@ void Bluetooth::processDeviceChangesEvent(QJsonObject data) QString action = data.value("action").toString(); QString id = data.value("device").toString(); - if (action == "added") { - BluetoothDevice *device = m_bluetooth->updateDeviceProperties(nullptr, data); - m_bluetooth->addDevice(device); - } else if (action == "changed") { - BluetoothDevice *device = m_bluetooth->getDevice(id); - m_bluetooth->updateDeviceProperties(device, data); - } else if (action == "removed") { - BluetoothDevice *device = m_bluetooth->getDevice(id); - m_bluetooth->removeDevice(device); + if (id.isEmpty()) + return; + + BluetoothDevice *device = m_bluetooth->getDevice(id); + if (action == "removed") { + if (device != nullptr) + m_bluetooth->removeDevice(device); + return; + } + + BluetoothDevice *ndevice = m_bluetooth->updateDeviceProperties(device, data); + if (ndevice == nullptr) { + qDebug() << "bt - failed to create device object with id: " << id; + return; } + if (device == nullptr) //device not previously in model + m_bluetooth->addDevice(ndevice); } void Bluetooth::processAdapterChangesEvent(QJsonObject data) diff --git a/bluetooth/bluetoothmodel.cpp b/bluetooth/bluetoothmodel.cpp index f6c3d09..294b50b 100644 --- a/bluetooth/bluetoothmodel.cpp +++ b/bluetooth/bluetoothmodel.cpp @@ -156,6 +156,9 @@ BluetoothDevice *BluetoothModel::updateDeviceProperties(BluetoothDevice *device, bool paired = properties.value("paired").toBool(); bool connected = properties.value("connected").toBool(); + if (id.isEmpty()) + return nullptr; + if (device == nullptr) return new BluetoothDevice(id, address, name, paired, connected); -- cgit 1.2.3-korg