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.PreferenceScreen;
26 import android.util.Log;
27 import android.view.Gravity;
28 import android.view.Menu;
29 import android.view.MenuInflater;
30 import android.view.MenuItem;
31 import android.view.View;
32 import android.widget.Switch;
34 import com.android.settings.R;
37 * BluetoothSettings is the Settings screen for Bluetooth configuration and
38 * connection management.
40 public final class BluetoothSettings extends DeviceListPreferenceFragment {
41 private static final String TAG = "BluetoothSettings";
43 private static final int MENU_ID_MAKE_DISCOVERABLE = Menu.FIRST;
44 private static final int MENU_ID_SCAN = Menu.FIRST + 1;
45 private static final int MENU_ID_ADVANCED = Menu.FIRST + 2;
47 private BluetoothEnabler mBluetoothEnabler;
49 /** Initialize the filter to show bonded devices only. */
50 //public BluetoothSettings() {
51 // super(BluetoothDeviceFilter.BONDED_DEVICE_FILTER);
55 void addPreferencesForActivity() {
56 addPreferencesFromResource(R.xml.bluetooth_settings);
58 Activity activity = getActivity();
60 Switch actionBarSwitch = new Switch(activity);
62 if (activity instanceof PreferenceActivity) {
63 PreferenceActivity preferenceActivity = (PreferenceActivity) activity;
64 if (preferenceActivity.onIsHidingHeaders() || !preferenceActivity.onIsMultiPane()) {
65 final int padding = activity.getResources().getDimensionPixelSize(
66 R.dimen.action_bar_switch_padding);
67 actionBarSwitch.setPadding(0, 0, padding, 0);
68 activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
69 ActionBar.DISPLAY_SHOW_CUSTOM);
70 activity.getActionBar().setCustomView(actionBarSwitch, new ActionBar.LayoutParams(
71 ActionBar.LayoutParams.WRAP_CONTENT,
72 ActionBar.LayoutParams.WRAP_CONTENT,
73 Gravity.CENTER_VERTICAL | Gravity.RIGHT));
77 mBluetoothEnabler = new BluetoothEnabler(activity, actionBarSwitch);
79 if (mLocalAdapter != null && mLocalAdapter.isEnabled()) {
80 activity.getActionBar().setSubtitle(mLocalAdapter.getName());
83 // TODO activity.setTheme(android.R.style.Theme_Holo_SplitActionBarWhenNarrow);
85 setHasOptionsMenu(true);
89 public void onResume() {
92 mBluetoothEnabler.resume();
94 updateContent(mLocalAdapter.getBluetoothState());
98 public void onPause() {
101 mBluetoothEnabler.pause();
105 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
106 boolean bluetoothIsEnabled = mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON;
107 menu.add(Menu.NONE, MENU_ID_MAKE_DISCOVERABLE, 0, R.string.bluetooth_visibility)
108 .setEnabled(bluetoothIsEnabled);
109 menu.add(Menu.NONE, MENU_ID_SCAN, 0, R.string.bluetooth_preference_find_nearby_title)
110 .setIcon(R.drawable.ic_menu_scan_network).setEnabled(bluetoothIsEnabled);
111 menu.add(Menu.NONE, MENU_ID_ADVANCED, 0, R.string.bluetooth_menu_advanced)
112 .setIcon(android.R.drawable.ic_menu_manage);
116 public boolean onOptionsItemSelected(MenuItem item) {
117 switch (item.getItemId()) {
118 case MENU_ID_MAKE_DISCOVERABLE:
120 // if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
121 // onAddNetworkPressed();
125 if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
126 mLocalAdapter.startScanning(true);
129 case MENU_ID_ADVANCED:
130 if (getActivity() instanceof PreferenceActivity) {
131 ((PreferenceActivity) getActivity()).startPreferencePanel(
132 AdvancedBluetoothSettings.class.getCanonicalName(),
134 R.string.bluetooth_advanced_titlebar, null,
137 startFragment(this, AdvancedBluetoothSettings.class.getCanonicalName(), -1, null);
141 return super.onOptionsItemSelected(item);
144 private final View.OnClickListener mListener = new View.OnClickListener() {
145 public void onClick(View v) {
146 // User clicked on advanced options icon for a device in the list
147 if (v.getTag() instanceof CachedBluetoothDevice) {
148 CachedBluetoothDevice device = (CachedBluetoothDevice) v.getTag();
150 Preference pref = new Preference(getActivity());
151 pref.setTitle(device.getName());
152 pref.setFragment(DeviceProfilesSettings.class.getName());
153 pref.getExtras().putParcelable(DeviceProfilesSettings.EXTRA_DEVICE,
155 ((PreferenceActivity) getActivity()).onPreferenceStartFragment(
156 BluetoothSettings.this, pref);
158 Log.w(TAG, "onClick() called for other View: " + v);
164 void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
165 mLocalAdapter.stopScanning();
166 super.onDevicePreferenceClick(btPreference);
170 public void onBluetoothStateChanged(int bluetoothState) {
171 super.onBluetoothStateChanged(bluetoothState);
172 updateContent(bluetoothState);
175 private void updateContent(int bluetoothState) {
176 final PreferenceScreen preferenceScreen = getPreferenceScreen();
177 getActivity().invalidateOptionsMenu();
180 switch (bluetoothState) {
181 case BluetoothAdapter.STATE_ON:
182 preferenceScreen.removeAll();
183 // Repopulate (which isn't too bad since it's cached in the settings bluetooth manager)
185 mLocalAdapter.startScanning(false);
188 case BluetoothAdapter.STATE_TURNING_OFF:
189 int preferenceCount = preferenceScreen.getPreferenceCount();
190 for (int i = 0; i < preferenceCount; i++) {
191 preferenceScreen.getPreference(i).setEnabled(false);
195 case BluetoothAdapter.STATE_OFF:
196 messageId = R.string.bluetooth_empty_list_bluetooth_off;
199 case BluetoothAdapter.STATE_TURNING_ON:
200 messageId = R.string.bluetooth_turning_on;
205 // TODO: from xml, add top padding. Same as in wifi
206 Preference emptyListPreference = new Preference(getActivity());
207 emptyListPreference.setTitle(messageId);
208 preferenceScreen.addPreference(emptyListPreference);
211 public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
212 if (bondState == BluetoothDevice.BOND_BONDED) {
213 // add to "Paired devices" list after remote-initiated pairing
214 if (mDevicePreferenceMap.get(cachedDevice) == null) {
215 createDevicePreference(cachedDevice);
217 } else if (bondState == BluetoothDevice.BOND_NONE) {
218 // remove unpaired device from paired devices list
219 onDeviceDeleted(cachedDevice);
224 * Add a listener, which enables the advanced settings icon.
225 * @param preference the newly added preference
228 void initDevicePreference(BluetoothDevicePreference preference) {
229 preference.setOnSettingsClickListener(mListener);