Skip to content

Commit

Permalink
Filtering bluetooth devices in the chrome os UI.
Browse files Browse the repository at this point in the history
Add utility method FilterDeviceList in device/bluetooth/chromeos
to filter the devices before updating in bluetooth UI.

Extension change is in a separate CL:
https://chromium-review.googlesource.com/c/chromium/src/+/1004414/

Design doc: http://go/cros-bluetooth-filtering

Bug: 817599
Cq-Include-Trybots: master.tryserver.chromium.linux:closure_compilation
Change-Id: I2a5cfb2ecdacbbe633770bb0c59b2e914b4e4b1f
Reviewed-on: https://chromium-review.googlesource.com/999014
Commit-Queue: Xiaoyin Hu <xiaoyinh@chromium.org>
Reviewed-by: Steven Bennetts <stevenjb@chromium.org>
Reviewed-by: Miao-chen Chou <mcchou@chromium.org>
Reviewed-by: Rahul Chaturvedi <rkc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#550046}
  • Loading branch information
Sarah Hu authored and Commit Bot committed Apr 12, 2018
1 parent 64a3223 commit ad95e80
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 16 deletions.
23 changes: 7 additions & 16 deletions ash/system/bluetooth/tray_bluetooth_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_discovery_session.h"
#include "device/bluetooth/chromeos/bluetooth_utils.h"

namespace ash {
namespace {
Expand Down Expand Up @@ -75,22 +76,12 @@ void TrayBluetoothHelper::InitializeOnAdapterReady(

BluetoothDeviceList TrayBluetoothHelper::GetAvailableBluetoothDevices() const {
BluetoothDeviceList device_list;
device::BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
for (device::BluetoothDevice* device : devices) {
if (device_list.size() == kMaximumDevicesShown)
break;

if (device->IsPaired() || device->IsConnecting())
device_list.push_back(GetBluetoothDeviceInfo(device));
}

for (device::BluetoothDevice* device : devices) {
if (device_list.size() == kMaximumDevicesShown)
break;

if (!device->IsPaired() && !device->IsConnecting())
device_list.push_back(GetBluetoothDeviceInfo(device));
}
device::BluetoothAdapter::DeviceList devices =
device::FilterBluetoothDeviceList(adapter_->GetDevices(),
device::BluetoothFilterType::KNOWN,
kMaximumDevicesShown);
for (device::BluetoothDevice* device : devices)
device_list.push_back(GetBluetoothDeviceInfo(device));

return device_list;
}
Expand Down
6 changes: 6 additions & 0 deletions device/bluetooth/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,12 @@ component("bluetooth") {
"dbus/dbus_thread_manager_linux.h",
]
}
if (is_chromeos) {
sources += [
"chromeos/bluetooth_utils.cc",
"chromeos/bluetooth_utils.h",
]
}
deps += [ "//dbus" ]
} else { # !use_dbus
if (is_chromecast && is_linux) {
Expand Down
63 changes: 63 additions & 0 deletions device/bluetooth/chromeos/bluetooth_utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "device/bluetooth/chromeos/bluetooth_utils.h"

namespace device {

namespace {

// Get limited number of devices from |devices| and
// prioritize paired/connecting devices over other devices.
BluetoothAdapter::DeviceList GetLimitedNumDevices(
size_t max_device_num,
const BluetoothAdapter::DeviceList& devices) {
// If |max_device_num| is 0, it means there's no limit.
if (max_device_num == 0)
return devices;

BluetoothAdapter::DeviceList result;
for (BluetoothDevice* device : devices) {
if (result.size() == max_device_num)
break;

if (device->IsPaired() || device->IsConnecting())
result.push_back(device);
}

for (BluetoothDevice* device : devices) {
if (result.size() == max_device_num)
break;

if (!device->IsPaired() && !device->IsConnecting())
result.push_back(device);
}

return result;
}

// Filter out unknown devices from the list.
BluetoothAdapter::DeviceList FilterUnknownDevices(
const BluetoothAdapter::DeviceList& devices) {
BluetoothAdapter::DeviceList result;
for (BluetoothDevice* device : devices) {
if (device->GetDeviceType() != device::BluetoothDeviceType::UNKNOWN)
result.push_back(device);
}
return result;
}

} // namespace

device::BluetoothAdapter::DeviceList FilterBluetoothDeviceList(
const BluetoothAdapter::DeviceList& devices,
BluetoothFilterType filter_type,
int max_devices) {
BluetoothAdapter::DeviceList filtered_devices =
filter_type == BluetoothFilterType::KNOWN ? FilterUnknownDevices(devices)
: devices;
return GetLimitedNumDevices(max_devices, filtered_devices);
}

} // namespace device
31 changes: 31 additions & 0 deletions device/bluetooth/chromeos/bluetooth_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef DEVICE_BLUETOOTH_CHROMEOS_BLUETOOTH_UTILS_H_
#define DEVICE_BLUETOOTH_CHROMEOS_BLUETOOTH_UTILS_H_

#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_export.h"

// This file contains common utilities for filtering the bluetooth devices
// based on the filter criteria.
namespace device {

enum class BluetoothFilterType {
// No filtering, all bluetooth devices will be returned.
ALL = 0,
// Return bluetooth devices that are known to the UI.
// I.e. bluetooth device type != UNKNOWN
KNOWN,
};

// Return filtered devices based on the filter type and max number of devices.
device::BluetoothAdapter::DeviceList DEVICE_BLUETOOTH_EXPORT
FilterBluetoothDeviceList(const BluetoothAdapter::DeviceList& devices,
BluetoothFilterType filter_type,
int max_devices);

} // namespace device

#endif // DEVICE_BLUETOOTH_CHROMEOS_BLUETOOTH_UTILS_H_

0 comments on commit ad95e80

Please sign in to comment.