1 /* 2 * Copyright (C) 2011 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.BluetoothAdapter; 20 import android.bluetooth.BluetoothDevice; 21 import android.os.Bundle; 22 import android.preference.Preference; 23 import android.preference.PreferenceCategory; 24 import android.preference.PreferenceGroup; 25 import android.preference.PreferenceScreen; 26 import android.util.Log; 27 28 import com.android.settings.RestrictedSettingsFragment; 29 import com.android.settingslib.bluetooth.BluetoothCallback; 30 import com.android.settingslib.bluetooth.BluetoothDeviceFilter; 31 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 32 import com.android.settingslib.bluetooth.LocalBluetoothAdapter; 33 import com.android.settingslib.bluetooth.LocalBluetoothManager; 34 35 import java.util.Collection; 36 import java.util.WeakHashMap; 37 38 /** 39 * Parent class for settings fragments that contain a list of Bluetooth 40 * devices. 41 * 42 * @see BluetoothSettings 43 * @see DevicePickerFragment 44 */ 45 public abstract class DeviceListPreferenceFragment extends 46 RestrictedSettingsFragment implements BluetoothCallback { 47 48 private static final String TAG = "DeviceListPreferenceFragment"; 49 50 private static final String KEY_BT_DEVICE_LIST = "bt_device_list"; 51 private static final String KEY_BT_SCAN = "bt_scan"; 52 53 private BluetoothDeviceFilter.Filter mFilter; 54 55 BluetoothDevice mSelectedDevice; 56 57 LocalBluetoothAdapter mLocalAdapter; 58 LocalBluetoothManager mLocalManager; 59 60 private PreferenceGroup mDeviceListGroup; 61 62 final WeakHashMap<CachedBluetoothDevice, BluetoothDevicePreference> mDevicePreferenceMap = 63 new WeakHashMap<CachedBluetoothDevice, BluetoothDevicePreference>(); 64 65 DeviceListPreferenceFragment(String restrictedKey) { 66 super(restrictedKey); 67 mFilter = BluetoothDeviceFilter.ALL_FILTER; 68 } 69 70 final void setFilter(BluetoothDeviceFilter.Filter filter) { 71 mFilter = filter; 72 } 73 74 final void setFilter(int filterType) { 75 mFilter = BluetoothDeviceFilter.getFilter(filterType); 76 } 77 78 @Override 79 public void onCreate(Bundle savedInstanceState) { 80 super.onCreate(savedInstanceState); 81 82 mLocalManager = Utils.getLocalBtManager(getActivity()); 83 if (mLocalManager == null) { 84 Log.e(TAG, "Bluetooth is not supported on this device"); 85 return; 86 } 87 mLocalAdapter = mLocalManager.getBluetoothAdapter(); 88 89 addPreferencesForActivity(); 90 91 mDeviceListGroup = (PreferenceCategory) findPreference(KEY_BT_DEVICE_LIST); 92 } 93 94 void setDeviceListGroup(PreferenceGroup preferenceGroup) { 95 mDeviceListGroup = preferenceGroup; 96 } 97 98 /** Add preferences from the subclass. */ 99 abstract void addPreferencesForActivity(); 100 101 @Override 102 public void onResume() { 103 super.onResume(); 104 if (mLocalManager == null || isUiRestricted()) return; 105 106 mLocalManager.setForegroundActivity(getActivity()); 107 mLocalManager.getEventManager().registerCallback(this); 108 109 updateProgressUi(mLocalAdapter.isDiscovering()); 110 } 111 112 @Override 113 public void onPause() { 114 super.onPause(); 115 if (mLocalManager == null || isUiRestricted()) { 116 return; 117 } 118 119 removeAllDevices(); 120 mLocalManager.setForegroundActivity(null); 121 mLocalManager.getEventManager().unregisterCallback(this); 122 } 123 124 void removeAllDevices() { 125 mLocalAdapter.stopScanning(); 126 mDevicePreferenceMap.clear(); 127 mDeviceListGroup.removeAll(); 128 } 129 130 void addCachedDevices() { 131 Collection<CachedBluetoothDevice> cachedDevices = 132 mLocalManager.getCachedDeviceManager().getCachedDevicesCopy(); 133 for (CachedBluetoothDevice cachedDevice : cachedDevices) { 134 onDeviceAdded(cachedDevice); 135 } 136 } 137 138 @Override 139 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, 140 Preference preference) { 141 if (KEY_BT_SCAN.equals(preference.getKey())) { 142 mLocalAdapter.startScanning(true); 143 return true; 144 } 145 146 if (preference instanceof BluetoothDevicePreference) { 147 BluetoothDevicePreference btPreference = (BluetoothDevicePreference) preference; 148 CachedBluetoothDevice device = btPreference.getCachedDevice(); 149 mSelectedDevice = device.getDevice(); 150 onDevicePreferenceClick(btPreference); 151 return true; 152 } 153 154 return super.onPreferenceTreeClick(preferenceScreen, preference); 155 } 156 157 void onDevicePreferenceClick(BluetoothDevicePreference btPreference) { 158 btPreference.onClicked(); 159 } 160 161 public void onDeviceAdded(CachedBluetoothDevice cachedDevice) { 162 if (mDevicePreferenceMap.get(cachedDevice) != null) { 163 return; 164 } 165 166 // Prevent updates while the list shows one of the state messages 167 if (mLocalAdapter.getBluetoothState() != BluetoothAdapter.STATE_ON) return; 168 169 if (mFilter.matches(cachedDevice.getDevice())) { 170 createDevicePreference(cachedDevice); 171 } 172 } 173 174 void createDevicePreference(CachedBluetoothDevice cachedDevice) { 175 if (mDeviceListGroup == null) { 176 Log.w(TAG, "Trying to create a device preference before the list group/category " 177 + "exists!"); 178 return; 179 } 180 181 BluetoothDevicePreference preference = new BluetoothDevicePreference( 182 getActivity(), cachedDevice); 183 184 initDevicePreference(preference); 185 mDeviceListGroup.addPreference(preference); 186 mDevicePreferenceMap.put(cachedDevice, preference); 187 } 188 189 /** 190 * Overridden in {@link BluetoothSettings} to add a listener. 191 * @param preference the newly added preference 192 */ 193 void initDevicePreference(BluetoothDevicePreference preference) { 194 // Does nothing by default 195 } 196 197 public void onDeviceDeleted(CachedBluetoothDevice cachedDevice) { 198 BluetoothDevicePreference preference = mDevicePreferenceMap.remove(cachedDevice); 199 if (preference != null) { 200 mDeviceListGroup.removePreference(preference); 201 } 202 } 203 204 public void onScanningStateChanged(boolean started) { 205 updateProgressUi(started); 206 } 207 208 private void updateProgressUi(boolean start) { 209 if (mDeviceListGroup instanceof BluetoothProgressCategory) { 210 ((BluetoothProgressCategory) mDeviceListGroup).setProgress(start); 211 } 212 } 213 214 public void onBluetoothStateChanged(int bluetoothState) { 215 if (bluetoothState == BluetoothAdapter.STATE_OFF) { 216 updateProgressUi(false); 217 } 218 } 219 220 public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) { } 221 } 222