CachedBluetoothDeviceManager.java revision 79be0b3e6b5639c4cbe2bbcd9adb0ec4c28716ed
1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.settings.bluetooth; 18 19import android.bluetooth.BluetoothDevice; 20 21import java.util.ArrayList; 22import java.util.Collection; 23import java.util.List; 24 25/** 26 * CachedBluetoothDeviceManager manages the set of remote Bluetooth devices. 27 */ 28final class CachedBluetoothDeviceManager { 29 30 private final List<CachedBluetoothDevice> mCachedDevices = 31 new ArrayList<CachedBluetoothDevice>(); 32 33 public synchronized Collection<CachedBluetoothDevice> getCachedDevicesCopy() { 34 return new ArrayList<CachedBluetoothDevice>(mCachedDevices); 35 } 36 37 public static boolean onDeviceDisappeared(CachedBluetoothDevice cachedDevice) { 38 cachedDevice.setVisible(false); 39 return cachedDevice.getBondState() == BluetoothDevice.BOND_NONE; 40 } 41 42 public void onDeviceNameUpdated(BluetoothDevice device) { 43 CachedBluetoothDevice cachedDevice = findDevice(device); 44 if (cachedDevice != null) { 45 cachedDevice.refreshName(); 46 } 47 } 48 49 /** 50 * Search for existing {@link CachedBluetoothDevice} or return null 51 * if this device isn't in the cache. Use {@link #addDevice} 52 * to create and return a new {@link CachedBluetoothDevice} for 53 * a newly discovered {@link BluetoothDevice}. 54 * 55 * @param device the address of the Bluetooth device 56 * @return the cached device object for this device, or null if it has 57 * not been previously seen 58 */ 59 CachedBluetoothDevice findDevice(BluetoothDevice device) { 60 for (CachedBluetoothDevice cachedDevice : mCachedDevices) { 61 if (cachedDevice.getDevice().equals(device)) { 62 return cachedDevice; 63 } 64 } 65 return null; 66 } 67 68 /** 69 * Create and return a new {@link CachedBluetoothDevice}. This assumes 70 * that {@link #findDevice} has already been called and returned null. 71 * @param device the address of the new Bluetooth device 72 * @return the newly created CachedBluetoothDevice object 73 */ 74 CachedBluetoothDevice addDevice(LocalBluetoothAdapter adapter, 75 LocalBluetoothProfileManager profileManager, 76 BluetoothDevice device) { 77 CachedBluetoothDevice newDevice = new CachedBluetoothDevice(adapter, profileManager, 78 device); 79 mCachedDevices.add(newDevice); 80 return newDevice; 81 } 82 83 /** 84 * Attempts to get the name of a remote device, otherwise returns the address. 85 * 86 * @param device The remote device. 87 * @return The name, or if unavailable, the address. 88 */ 89 public String getName(BluetoothDevice device) { 90 CachedBluetoothDevice cachedDevice = findDevice(device); 91 if (cachedDevice != null) { 92 return cachedDevice.getName(); 93 } 94 95 String name = device.getAliasName(); 96 if (name != null) { 97 return name; 98 } 99 100 return device.getAddress(); 101 } 102 103 public synchronized void onScanningStateChanged(boolean started) { 104 if (!started) return; 105 106 // If starting a new scan, clear old visibility 107 // Iterate in reverse order since devices may be removed. 108 for (int i = mCachedDevices.size() - 1; i >= 0; i--) { 109 CachedBluetoothDevice cachedDevice = mCachedDevices.get(i); 110 cachedDevice.setVisible(false); 111 } 112 } 113 114 public synchronized void onBtClassChanged(BluetoothDevice device) { 115 CachedBluetoothDevice cachedDevice = findDevice(device); 116 if (cachedDevice != null) { 117 cachedDevice.refreshBtClass(); 118 } 119 } 120 121 public synchronized void onUuidChanged(BluetoothDevice device) { 122 CachedBluetoothDevice cachedDevice = findDevice(device); 123 if (cachedDevice != null) { 124 cachedDevice.onUuidChanged(); 125 } 126 } 127} 128