CachedBluetoothDeviceManager.java revision 2036ebd8896bbabbbe04db34c9e7d8a1be6fe32a
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// private static final String TAG = "CachedBluetoothDeviceManager"; 30 31 private final List<CachedBluetoothDevice> mCachedDevices = 32 new ArrayList<CachedBluetoothDevice>(); 33 34 public synchronized Collection<CachedBluetoothDevice> getCachedDevicesCopy() { 35 return new ArrayList<CachedBluetoothDevice>(mCachedDevices); 36 } 37 38 public boolean onDeviceDisappeared(CachedBluetoothDevice cachedDevice) { 39 cachedDevice.setVisible(false); 40 return checkForDeviceRemoval(cachedDevice); 41 } 42 43 private boolean checkForDeviceRemoval( 44 CachedBluetoothDevice cachedDevice) { 45 if (cachedDevice.getBondState() == BluetoothDevice.BOND_NONE && 46 !cachedDevice.isVisible()) { 47 // If device isn't paired, remove it altogether 48 mCachedDevices.remove(cachedDevice); 49 return true; // dispatch device deleted 50 } 51 return false; 52 } 53 54 public void onDeviceNameUpdated(BluetoothDevice device) { 55 CachedBluetoothDevice cachedDevice = findDevice(device); 56 if (cachedDevice != null) { 57 cachedDevice.refreshName(); 58 } 59 } 60 61 /** 62 * Search for existing {@link CachedBluetoothDevice} or return null 63 * if this device isn't in the cache. Use {@link #addDevice} 64 * to create and return a new {@link CachedBluetoothDevice} for 65 * a newly discovered {@link BluetoothDevice}. 66 * 67 * @param device the address of the Bluetooth device 68 * @return the cached device object for this device, or null if it has 69 * not been previously seen 70 */ 71 CachedBluetoothDevice findDevice(BluetoothDevice device) { 72 for (CachedBluetoothDevice cachedDevice : mCachedDevices) { 73 if (cachedDevice.getDevice().equals(device)) { 74 return cachedDevice; 75 } 76 } 77 return null; 78 } 79 80 /** 81 * Create and return a new {@link CachedBluetoothDevice}. This assumes 82 * that {@link #findDevice} has already been called and returned null. 83 * @param device the address of the new Bluetooth device 84 * @return the newly created CachedBluetoothDevice object 85 */ 86 CachedBluetoothDevice addDevice(LocalBluetoothAdapter adapter, 87 LocalBluetoothProfileManager profileManager, 88 BluetoothDevice device) { 89 CachedBluetoothDevice newDevice = new CachedBluetoothDevice(adapter, profileManager, 90 device); 91 mCachedDevices.add(newDevice); 92 return newDevice; 93 } 94 95 /** 96 * Attempts to get the name of a remote device, otherwise returns the address. 97 * 98 * @param device The remote device. 99 * @return The name, or if unavailable, the address. 100 */ 101 public String getName(BluetoothDevice device) { 102 CachedBluetoothDevice cachedDevice = findDevice(device); 103 if (cachedDevice != null) { 104 return cachedDevice.getName(); 105 } 106 107 String name = device.getAliasName(); 108 if (name != null) { 109 return name; 110 } 111 112 return device.getAddress(); 113 } 114 115 public synchronized void onScanningStateChanged(boolean started) { 116 if (!started) return; 117 118 // If starting a new scan, clear old visibility 119 // Iterate in reverse order since devices may be removed. 120 for (int i = mCachedDevices.size() - 1; i >= 0; i--) { 121 CachedBluetoothDevice cachedDevice = mCachedDevices.get(i); 122 cachedDevice.setVisible(false); 123 checkForDeviceRemoval(cachedDevice); 124 } 125 } 126 127 public synchronized void onBtClassChanged(BluetoothDevice device) { 128 CachedBluetoothDevice cachedDevice = findDevice(device); 129 if (cachedDevice != null) { 130 cachedDevice.refreshBtClass(); 131 } 132 } 133 134 public synchronized void onUuidChanged(BluetoothDevice device) { 135 CachedBluetoothDevice cachedDevice = findDevice(device); 136 if (cachedDevice != null) { 137 cachedDevice.onUuidChanged(); 138 } 139 } 140} 141