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_SCAN = Menu.FIRST;
44 private static final int MENU_ID_ADVANCED = Menu.FIRST + 1;
46 private BluetoothEnabler mBluetoothEnabler;
48 /** Initialize the filter to show bonded devices only. */
49 //public BluetoothSettings() {
50 // super(BluetoothDeviceFilter.BONDED_DEVICE_FILTER);
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_preference_find_nearby_title)
105 //.setIcon(R.drawable.ic_menu_scan_network)
106 .setEnabled(bluetoothIsEnabled)
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) {
118 // onAddNetworkPressed();
121 if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
122 mLocalAdapter.startScanning(true);
125 case MENU_ID_ADVANCED:
126 if (getActivity() instanceof PreferenceActivity) {
127 ((PreferenceActivity) getActivity()).startPreferencePanel(
128 AdvancedBluetoothSettings.class.getCanonicalName(),
130 R.string.bluetooth_advanced_titlebar, null,
133 startFragment(this, AdvancedBluetoothSettings.class.getCanonicalName(), -1, null);
137 return super.onOptionsItemSelected(item);
140 private final View.OnClickListener mListener = new View.OnClickListener() {
141 public void onClick(View v) {
142 // User clicked on advanced options icon for a device in the list
143 if (v.getTag() instanceof CachedBluetoothDevice) {
144 CachedBluetoothDevice device = (CachedBluetoothDevice) v.getTag();
146 Preference pref = new Preference(getActivity());
147 pref.setTitle(device.getName());
148 pref.setFragment(DeviceProfilesSettings.class.getName());
149 pref.getExtras().putParcelable(DeviceProfilesSettings.EXTRA_DEVICE,
151 ((PreferenceActivity) getActivity()).onPreferenceStartFragment(
152 BluetoothSettings.this, pref);
154 Log.w(TAG, "onClick() called for other View: " + v);
160 void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
161 mLocalAdapter.stopScanning();
162 super.onDevicePreferenceClick(btPreference);
166 public void onBluetoothStateChanged(int bluetoothState) {
167 super.onBluetoothStateChanged(bluetoothState);
168 updateContent(bluetoothState);
171 private void updateContent(int bluetoothState) {
172 final PreferenceScreen preferenceScreen = getPreferenceScreen();
173 getActivity().invalidateOptionsMenu();
176 switch (bluetoothState) {
177 case BluetoothAdapter.STATE_ON:
178 preferenceScreen.removeAll();
179 // Repopulate (which isn't too bad since it's cached in the settings bluetooth manager)
181 mLocalAdapter.startScanning(false);
184 case BluetoothAdapter.STATE_TURNING_OFF:
185 int preferenceCount = preferenceScreen.getPreferenceCount();
186 for (int i = 0; i < preferenceCount; i++) {
187 preferenceScreen.getPreference(i).setEnabled(false);
191 case BluetoothAdapter.STATE_OFF:
192 messageId = R.string.bluetooth_empty_list_bluetooth_off;
195 case BluetoothAdapter.STATE_TURNING_ON:
196 messageId = R.string.bluetooth_turning_on;
201 // TODO: from xml, add top padding. Same as in wifi
202 Preference emptyListPreference = new Preference(getActivity());
203 emptyListPreference.setTitle(messageId);
204 preferenceScreen.addPreference(emptyListPreference);
207 public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
208 if (bondState == BluetoothDevice.BOND_BONDED) {
209 // add to "Paired devices" list after remote-initiated pairing
210 if (mDevicePreferenceMap.get(cachedDevice) == null) {
211 createDevicePreference(cachedDevice);
213 } else if (bondState == BluetoothDevice.BOND_NONE) {
214 // remove unpaired device from paired devices list
215 onDeviceDeleted(cachedDevice);
220 * Add a listener, which enables the advanced settings icon.
221 * @param preference the newly added preference
224 void initDevicePreference(BluetoothDevicePreference preference) {
225 preference.setOnSettingsClickListener(mListener);