blob: 605115b40825f019377685f2ee03fa6e498feea6 [file] [log] [blame]
// 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