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.app.ActionBar;
20 import android.app.Activity;
21 import android.bluetooth.BluetoothAdapter;
22 import android.bluetooth.BluetoothDevice;
23 import android.preference.Preference;
24 import android.preference.PreferenceActivity;
25 import android.preference.PreferenceGroup;
26 import android.preference.PreferenceScreen;
27 import android.util.Log;
28 import android.view.Gravity;
29 import android.view.Menu;
30 import android.view.MenuInflater;
31 import android.view.MenuItem;
32 import android.view.View;
33 import android.widget.Switch;
35 import com.android.settings.ProgressCategory;
36 import com.android.settings.R;
39 * BluetoothSettings is the Settings screen for Bluetooth configuration and
40 * connection management.
42 public final class BluetoothSettings extends DeviceListPreferenceFragment {
43 private static final String TAG = "BluetoothSettings";
45 private static final int MENU_ID_SCAN = Menu.FIRST;
46 private static final int MENU_ID_ADVANCED = Menu.FIRST + 1;
48 private BluetoothEnabler mBluetoothEnabler;
50 private PreferenceGroup mFoundDevicesCategory;
51 private boolean mFoundDevicesCategoryIsPresent;
54 void addPreferencesForActivity() {
55 addPreferencesFromResource(R.xml.bluetooth_settings);
57 Activity activity = getActivity();
59 Switch actionBarSwitch = new Switch(activity);
61 if (activity instanceof PreferenceActivity) {
62 PreferenceActivity preferenceActivity = (PreferenceActivity) activity;
63 if (preferenceActivity.onIsHidingHeaders() || !preferenceActivity.onIsMultiPane()) {
64 final int padding = activity.getResources().getDimensionPixelSize(
65 R.dimen.action_bar_switch_padding);
66 actionBarSwitch.setPadding(0, 0, padding, 0);
67 activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
68 ActionBar.DISPLAY_SHOW_CUSTOM);
69 activity.getActionBar().setCustomView(actionBarSwitch, new ActionBar.LayoutParams(
70 ActionBar.LayoutParams.WRAP_CONTENT,
71 ActionBar.LayoutParams.WRAP_CONTENT,
72 Gravity.CENTER_VERTICAL | Gravity.RIGHT));
76 mBluetoothEnabler = new BluetoothEnabler(activity, actionBarSwitch);
78 if (mLocalAdapter != null && mLocalAdapter.isEnabled()) {
79 activity.getActionBar().setSubtitle(mLocalAdapter.getName());
82 setHasOptionsMenu(true);
86 public void onResume() {
89 mBluetoothEnabler.resume();
91 updateContent(mLocalAdapter.getBluetoothState());
95 public void onPause() {
98 mBluetoothEnabler.pause();
102 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
103 boolean bluetoothIsEnabled = mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON;
104 menu.add(Menu.NONE, MENU_ID_SCAN, 0, R.string.bluetooth_scan_nearby_devices)
105 //.setIcon(R.drawable.ic_menu_scan_network)
106 .setEnabled(bluetoothIsEnabled && !mLocalAdapter.isDiscovering())
107 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
108 menu.add(Menu.NONE, MENU_ID_ADVANCED, 0, R.string.bluetooth_menu_advanced)
109 //.setIcon(android.R.drawable.ic_menu_manage)
110 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
114 public boolean onOptionsItemSelected(MenuItem item) {
115 switch (item.getItemId()) {
117 if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
121 case MENU_ID_ADVANCED:
122 if (getActivity() instanceof PreferenceActivity) {
123 ((PreferenceActivity) getActivity()).startPreferencePanel(
124 AdvancedBluetoothSettings.class.getCanonicalName(),
126 R.string.bluetooth_advanced_titlebar, null,
129 startFragment(this, AdvancedBluetoothSettings.class.getCanonicalName(), -1, null);
133 return super.onOptionsItemSelected(item);
136 private void startScanning() {
137 if (!mFoundDevicesCategoryIsPresent) {
138 getPreferenceScreen().addPreference(mFoundDevicesCategory);
140 mLocalAdapter.startScanning(true);
144 void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
145 mLocalAdapter.stopScanning();
146 super.onDevicePreferenceClick(btPreference);
149 private void updateContent(int bluetoothState) {
150 final PreferenceScreen preferenceScreen = getPreferenceScreen();
151 getActivity().invalidateOptionsMenu();
154 switch (bluetoothState) {
155 case BluetoothAdapter.STATE_ON:
156 preferenceScreen.removeAll();
158 // Add bonded devices from cache first
159 setFilter(BluetoothDeviceFilter.BONDED_DEVICE_FILTER);
160 setDeviceListGroup(preferenceScreen);
161 preferenceScreen.setOrderingAsAdded(true);
164 int numberOfPairedDevices = preferenceScreen.getPreferenceCount();
166 // Found devices category
167 mFoundDevicesCategory = new ProgressCategory(getActivity(), null);
168 mFoundDevicesCategory.setTitle(R.string.bluetooth_preference_found_devices);
169 preferenceScreen.addPreference(mFoundDevicesCategory);
170 mFoundDevicesCategoryIsPresent = true;
172 // Unbonded found devices from cache
173 setFilter(BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER);
174 setDeviceListGroup(mFoundDevicesCategory);
177 int numberOfUnpairedDevices = mFoundDevicesCategory.getPreferenceCount();
178 if (numberOfUnpairedDevices == 0) {
179 preferenceScreen.removePreference(mFoundDevicesCategory);
180 mFoundDevicesCategoryIsPresent = false;
183 if (numberOfPairedDevices == 0) startScanning();
187 case BluetoothAdapter.STATE_TURNING_OFF:
188 int preferenceCount = preferenceScreen.getPreferenceCount();
189 for (int i = 0; i < preferenceCount; i++) {
190 preferenceScreen.getPreference(i).setEnabled(false);
194 case BluetoothAdapter.STATE_OFF:
195 messageId = R.string.bluetooth_empty_list_bluetooth_off;
198 case BluetoothAdapter.STATE_TURNING_ON:
199 messageId = R.string.bluetooth_turning_on;
203 setDeviceListGroup(preferenceScreen);
206 // TODO: from xml, add top padding. Same as in wifi
207 Preference emptyListPreference = new Preference(getActivity());
208 emptyListPreference.setTitle(messageId);
209 preferenceScreen.addPreference(emptyListPreference);
213 public void onBluetoothStateChanged(int bluetoothState) {
214 super.onBluetoothStateChanged(bluetoothState);
215 updateContent(bluetoothState);
219 public void onScanningStateChanged(boolean started) {
220 super.onScanningStateChanged(started);
221 // Update 'Scan' option enabled state
222 getActivity().invalidateOptionsMenu();
225 public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
226 setDeviceListGroup(getPreferenceScreen());
228 updateContent(mLocalAdapter.getBluetoothState());
231 private final View.OnClickListener mDeviceProfilesListener = new View.OnClickListener() {
232 public void onClick(View v) {
233 // User clicked on advanced options icon for a device in the list
234 if (v.getTag() instanceof CachedBluetoothDevice) {
235 CachedBluetoothDevice device = (CachedBluetoothDevice) v.getTag();
237 Preference pref = new Preference(getActivity());
238 pref.setTitle(device.getName());
239 pref.setFragment(DeviceProfilesSettings.class.getName());
240 pref.getExtras().putParcelable(DeviceProfilesSettings.EXTRA_DEVICE,
242 ((PreferenceActivity) getActivity()).onPreferenceStartFragment(
243 BluetoothSettings.this, pref);
245 Log.w(TAG, "onClick() called for other View: " + v); // TODO remove
251 * Add a listener, which enables the advanced settings icon.
252 * @param preference the newly added preference
255 void initDevicePreference(BluetoothDevicePreference preference) {
256 CachedBluetoothDevice cachedDevice = preference.getCachedDevice();
257 if (cachedDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
258 // Only paired device have an associated advanced settings screen
259 preference.setOnSettingsClickListener(mDeviceProfilesListener);