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 boolean isDiscovering = mLocalAdapter.isDiscovering();
105 int textId = isDiscovering ? R.string.bluetooth_searching_for_devices :
106 R.string.bluetooth_search_for_devices;
107 menu.add(Menu.NONE, MENU_ID_SCAN, 0, textId)
108 //.setIcon(R.drawable.ic_menu_scan_network)
109 .setEnabled(bluetoothIsEnabled && !isDiscovering)
110 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
111 menu.add(Menu.NONE, MENU_ID_ADVANCED, 0, R.string.bluetooth_menu_advanced)
112 //.setIcon(android.R.drawable.ic_menu_manage)
113 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
117 public boolean onOptionsItemSelected(MenuItem item) {
118 switch (item.getItemId()) {
120 if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
124 case MENU_ID_ADVANCED:
125 if (getActivity() instanceof PreferenceActivity) {
126 ((PreferenceActivity) getActivity()).startPreferencePanel(
127 AdvancedBluetoothSettings.class.getCanonicalName(),
129 R.string.bluetooth_advanced_titlebar, null,
132 startFragment(this, AdvancedBluetoothSettings.class.getCanonicalName(), -1, null);
136 return super.onOptionsItemSelected(item);
139 private void startScanning() {
140 if (!mFoundDevicesCategoryIsPresent) {
141 getPreferenceScreen().addPreference(mFoundDevicesCategory);
143 mLocalAdapter.startScanning(true);
147 void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
148 mLocalAdapter.stopScanning();
149 super.onDevicePreferenceClick(btPreference);
152 private void updateContent(int bluetoothState) {
153 final PreferenceScreen preferenceScreen = getPreferenceScreen();
154 getActivity().invalidateOptionsMenu();
157 switch (bluetoothState) {
158 case BluetoothAdapter.STATE_ON:
159 preferenceScreen.removeAll();
161 // Add bonded devices from cache first
162 setFilter(BluetoothDeviceFilter.BONDED_DEVICE_FILTER);
163 setDeviceListGroup(preferenceScreen);
164 preferenceScreen.setOrderingAsAdded(true);
167 int numberOfPairedDevices = preferenceScreen.getPreferenceCount();
169 // Found devices category
170 mFoundDevicesCategory = new ProgressCategory(getActivity(), null);
171 mFoundDevicesCategory.setTitle(R.string.bluetooth_preference_found_devices);
172 preferenceScreen.addPreference(mFoundDevicesCategory);
173 mFoundDevicesCategoryIsPresent = true;
175 // Unbonded found devices from cache
176 setFilter(BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER);
177 setDeviceListGroup(mFoundDevicesCategory);
180 int numberOfUnpairedDevices = mFoundDevicesCategory.getPreferenceCount();
181 if (numberOfUnpairedDevices == 0) {
182 preferenceScreen.removePreference(mFoundDevicesCategory);
183 mFoundDevicesCategoryIsPresent = false;
186 if (numberOfPairedDevices == 0) startScanning();
190 case BluetoothAdapter.STATE_TURNING_OFF:
191 int preferenceCount = preferenceScreen.getPreferenceCount();
192 for (int i = 0; i < preferenceCount; i++) {
193 preferenceScreen.getPreference(i).setEnabled(false);
197 case BluetoothAdapter.STATE_OFF:
198 messageId = R.string.bluetooth_empty_list_bluetooth_off;
201 case BluetoothAdapter.STATE_TURNING_ON:
202 messageId = R.string.bluetooth_turning_on;
206 setDeviceListGroup(preferenceScreen);
209 // TODO: from xml, add top padding. Same as in wifi
210 Preference emptyListPreference = new Preference(getActivity());
211 emptyListPreference.setTitle(messageId);
212 preferenceScreen.addPreference(emptyListPreference);
216 public void onBluetoothStateChanged(int bluetoothState) {
217 super.onBluetoothStateChanged(bluetoothState);
218 updateContent(bluetoothState);
222 public void onScanningStateChanged(boolean started) {
223 super.onScanningStateChanged(started);
224 // Update options' enabled state
225 getActivity().invalidateOptionsMenu();
228 public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
229 setDeviceListGroup(getPreferenceScreen());
231 updateContent(mLocalAdapter.getBluetoothState());
234 private final View.OnClickListener mDeviceProfilesListener = new View.OnClickListener() {
235 public void onClick(View v) {
236 // User clicked on advanced options icon for a device in the list
237 if (v.getTag() instanceof CachedBluetoothDevice) {
238 CachedBluetoothDevice device = (CachedBluetoothDevice) v.getTag();
240 Preference pref = new Preference(getActivity());
241 pref.setTitle(device.getName());
242 pref.setFragment(DeviceProfilesSettings.class.getName());
243 pref.getExtras().putParcelable(DeviceProfilesSettings.EXTRA_DEVICE,
245 ((PreferenceActivity) getActivity()).onPreferenceStartFragment(
246 BluetoothSettings.this, pref);
248 Log.w(TAG, "onClick() called for other View: " + v); // TODO remove
254 * Add a listener, which enables the advanced settings icon.
255 * @param preference the newly added preference
258 void initDevicePreference(BluetoothDevicePreference preference) {
259 CachedBluetoothDevice cachedDevice = preference.getCachedDevice();
260 if (cachedDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
261 // Only paired device have an associated advanced settings screen
262 preference.setOnSettingsClickListener(mDeviceProfilesListener);