2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.bluetooth;
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;
28 import com.android.settings.ProgressCategory;
29 import com.android.settings.SettingsPreferenceFragment;
31 import java.util.Collection;
32 import java.util.WeakHashMap;
35 * Parent class for settings fragments that contain a list of Bluetooth
38 * @see BluetoothSettings
39 * @see DevicePickerFragment
41 public abstract class DeviceListPreferenceFragment extends
42 SettingsPreferenceFragment implements BluetoothCallback {
44 private static final String TAG = "DeviceListPreferenceFragment";
46 private static final String KEY_BT_DEVICE_LIST = "bt_device_list";
47 private static final String KEY_BT_SCAN = "bt_scan";
49 private BluetoothDeviceFilter.Filter mFilter;
51 BluetoothDevice mSelectedDevice;
53 LocalBluetoothAdapter mLocalAdapter;
54 LocalBluetoothManager mLocalManager;
56 private PreferenceGroup mDeviceListGroup;
58 final WeakHashMap<CachedBluetoothDevice, BluetoothDevicePreference> mDevicePreferenceMap =
59 new WeakHashMap<CachedBluetoothDevice, BluetoothDevicePreference>();
61 DeviceListPreferenceFragment() {
62 mFilter = BluetoothDeviceFilter.ALL_FILTER;
65 final void setFilter(BluetoothDeviceFilter.Filter filter) {
69 final void setFilter(int filterType) {
70 mFilter = BluetoothDeviceFilter.getFilter(filterType);
74 public void onCreate(Bundle savedInstanceState) {
75 super.onCreate(savedInstanceState);
77 mLocalManager = LocalBluetoothManager.getInstance(getActivity());
78 if (mLocalManager == null) {
79 Log.e(TAG, "Bluetooth is not supported on this device");
82 mLocalAdapter = mLocalManager.getBluetoothAdapter();
84 addPreferencesForActivity();
86 mDeviceListGroup = (PreferenceCategory) findPreference(KEY_BT_DEVICE_LIST);
89 void setDeviceListGroup(PreferenceGroup preferenceGroup) {
90 mDeviceListGroup = preferenceGroup;
93 /** Add preferences from the subclass. */
94 abstract void addPreferencesForActivity();
97 public void onResume() {
99 if (mLocalManager == null) return;
101 mLocalManager.setForegroundActivity(getActivity());
102 mLocalManager.getEventManager().registerCallback(this);
104 updateProgressUi(mLocalAdapter.isDiscovering());
108 public void onPause() {
110 if (mLocalManager == null) return;
113 mLocalManager.setForegroundActivity(null);
114 mLocalManager.getEventManager().unregisterCallback(this);
117 void removeAllDevices() {
118 mLocalAdapter.stopScanning();
119 mDevicePreferenceMap.clear();
120 mDeviceListGroup.removeAll();
123 void addCachedDevices() {
124 Collection<CachedBluetoothDevice> cachedDevices =
125 mLocalManager.getCachedDeviceManager().getCachedDevicesCopy();
126 for (CachedBluetoothDevice cachedDevice : cachedDevices) {
127 onDeviceAdded(cachedDevice);
132 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
133 Preference preference) {
134 if (KEY_BT_SCAN.equals(preference.getKey())) {
135 mLocalAdapter.startScanning(true);
139 if (preference instanceof BluetoothDevicePreference) {
140 BluetoothDevicePreference btPreference = (BluetoothDevicePreference) preference;
141 CachedBluetoothDevice device = btPreference.getCachedDevice();
142 mSelectedDevice = device.getDevice();
143 onDevicePreferenceClick(btPreference);
147 return super.onPreferenceTreeClick(preferenceScreen, preference);
150 void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
151 btPreference.onClicked();
154 public void onDeviceAdded(CachedBluetoothDevice cachedDevice) {
155 if (mDevicePreferenceMap.get(cachedDevice) != null) {
159 // Prevent updates while the list shows one of the state messages
160 if (mLocalAdapter.getBluetoothState() != BluetoothAdapter.STATE_ON) return;
162 if (mFilter.matches(cachedDevice.getDevice())) {
163 createDevicePreference(cachedDevice);
167 void createDevicePreference(CachedBluetoothDevice cachedDevice) {
168 BluetoothDevicePreference preference = new BluetoothDevicePreference(
169 getActivity(), cachedDevice);
171 initDevicePreference(preference);
172 mDeviceListGroup.addPreference(preference);
173 mDevicePreferenceMap.put(cachedDevice, preference);
177 * Overridden in {@link BluetoothSettings} to add a listener.
178 * @param preference the newly added preference
180 void initDevicePreference(BluetoothDevicePreference preference) {
181 // Does nothing by default
184 public void onDeviceDeleted(CachedBluetoothDevice cachedDevice) {
185 BluetoothDevicePreference preference = mDevicePreferenceMap.remove(cachedDevice);
186 if (preference != null) {
187 mDeviceListGroup.removePreference(preference);
191 public void onScanningStateChanged(boolean started) {
192 updateProgressUi(started);
195 private void updateProgressUi(boolean start) {
196 if (mDeviceListGroup instanceof ProgressCategory) {
197 ((ProgressCategory) mDeviceListGroup).setProgress(start);
201 public void onBluetoothStateChanged(int bluetoothState) {
202 if (bluetoothState == BluetoothAdapter.STATE_OFF) {
203 updateProgressUi(false);