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