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.os.Bundle;
24 import android.preference.Preference;
25 import android.preference.PreferenceActivity;
26 import android.preference.PreferenceGroup;
27 import android.preference.PreferenceScreen;
28 import android.util.Log;
29 import android.view.Gravity;
30 import android.view.LayoutInflater;
31 import android.view.Menu;
32 import android.view.MenuInflater;
33 import android.view.MenuItem;
34 import android.view.View;
35 import android.view.ViewGroup;
36 import android.widget.Switch;
37 import android.widget.TextView;
39 import com.android.settings.ProgressCategory;
40 import com.android.settings.R;
43 * BluetoothSettings is the Settings screen for Bluetooth configuration and
44 * connection management.
46 public final class BluetoothSettings extends DeviceListPreferenceFragment {
47 private static final String TAG = "BluetoothSettings";
49 private static final int MENU_ID_SCAN = Menu.FIRST;
50 private static final int MENU_ID_ADVANCED = Menu.FIRST + 1;
52 private BluetoothEnabler mBluetoothEnabler;
54 private PreferenceGroup mFoundDevicesCategory;
55 private boolean mFoundDevicesCategoryIsPresent;
58 private TextView mEmptyView;
61 public View onCreateView(LayoutInflater inflater, ViewGroup container,
62 Bundle savedInstanceState) {
63 mView = inflater.inflate(R.layout.custom_preference_list_fragment, container, false);
68 public void onActivityCreated(Bundle savedInstanceState) {
69 super.onActivityCreated(savedInstanceState);
71 mEmptyView = (TextView) mView.findViewById(R.id.empty);
72 getListView().setEmptyView(mEmptyView);
76 void addPreferencesForActivity() {
77 addPreferencesFromResource(R.xml.bluetooth_settings);
79 Activity activity = getActivity();
81 Switch actionBarSwitch = new Switch(activity);
83 if (activity instanceof PreferenceActivity) {
84 PreferenceActivity preferenceActivity = (PreferenceActivity) activity;
85 if (preferenceActivity.onIsHidingHeaders() || !preferenceActivity.onIsMultiPane()) {
86 final int padding = activity.getResources().getDimensionPixelSize(
87 R.dimen.action_bar_switch_padding);
88 actionBarSwitch.setPadding(0, 0, padding, 0);
89 activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
90 ActionBar.DISPLAY_SHOW_CUSTOM);
91 activity.getActionBar().setCustomView(actionBarSwitch, new ActionBar.LayoutParams(
92 ActionBar.LayoutParams.WRAP_CONTENT,
93 ActionBar.LayoutParams.WRAP_CONTENT,
94 Gravity.CENTER_VERTICAL | Gravity.RIGHT));
98 mBluetoothEnabler = new BluetoothEnabler(activity, actionBarSwitch);
100 if (mLocalAdapter != null && mLocalAdapter.isEnabled()) {
101 activity.getActionBar().setSubtitle(mLocalAdapter.getName());
104 setHasOptionsMenu(true);
108 public void onResume() {
111 mBluetoothEnabler.resume();
113 updateContent(mLocalAdapter.getBluetoothState());
117 public void onPause() {
120 mBluetoothEnabler.pause();
124 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
125 boolean bluetoothIsEnabled = mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON;
126 boolean isDiscovering = mLocalAdapter.isDiscovering();
127 int textId = isDiscovering ? R.string.bluetooth_searching_for_devices :
128 R.string.bluetooth_search_for_devices;
129 menu.add(Menu.NONE, MENU_ID_SCAN, 0, textId)
130 //.setIcon(R.drawable.ic_menu_scan_network)
131 .setEnabled(bluetoothIsEnabled && !isDiscovering)
132 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
133 menu.add(Menu.NONE, MENU_ID_ADVANCED, 0, R.string.bluetooth_menu_advanced)
134 //.setIcon(android.R.drawable.ic_menu_manage)
135 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
139 public boolean onOptionsItemSelected(MenuItem item) {
140 switch (item.getItemId()) {
142 if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
146 case MENU_ID_ADVANCED:
147 if (getActivity() instanceof PreferenceActivity) {
148 ((PreferenceActivity) getActivity()).startPreferencePanel(
149 AdvancedBluetoothSettings.class.getCanonicalName(),
151 R.string.bluetooth_advanced_titlebar, null,
154 startFragment(this, AdvancedBluetoothSettings.class.getCanonicalName(), -1, null);
158 return super.onOptionsItemSelected(item);
161 private void startScanning() {
162 if (!mFoundDevicesCategoryIsPresent) {
163 getPreferenceScreen().addPreference(mFoundDevicesCategory);
165 mLocalAdapter.startScanning(true);
169 void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
170 mLocalAdapter.stopScanning();
171 super.onDevicePreferenceClick(btPreference);
174 private void updateContent(int bluetoothState) {
175 final PreferenceScreen preferenceScreen = getPreferenceScreen();
176 getActivity().invalidateOptionsMenu();
179 switch (bluetoothState) {
180 case BluetoothAdapter.STATE_ON:
181 preferenceScreen.removeAll();
183 // Add bonded devices from cache first
184 setFilter(BluetoothDeviceFilter.BONDED_DEVICE_FILTER);
185 setDeviceListGroup(preferenceScreen);
186 preferenceScreen.setOrderingAsAdded(true);
189 int numberOfPairedDevices = preferenceScreen.getPreferenceCount();
191 // Found devices category
192 mFoundDevicesCategory = new ProgressCategory(getActivity(), null);
193 mFoundDevicesCategory.setTitle(R.string.bluetooth_preference_found_devices);
194 preferenceScreen.addPreference(mFoundDevicesCategory);
195 mFoundDevicesCategoryIsPresent = true;
197 // Unbonded found devices from cache
198 setFilter(BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER);
199 setDeviceListGroup(mFoundDevicesCategory);
202 int numberOfUnpairedDevices = mFoundDevicesCategory.getPreferenceCount();
203 if (numberOfUnpairedDevices == 0) {
204 preferenceScreen.removePreference(mFoundDevicesCategory);
205 mFoundDevicesCategoryIsPresent = false;
208 if (numberOfPairedDevices == 0) startScanning();
212 case BluetoothAdapter.STATE_TURNING_OFF:
213 int preferenceCount = preferenceScreen.getPreferenceCount();
214 for (int i = 0; i < preferenceCount; i++) {
215 preferenceScreen.getPreference(i).setEnabled(false);
219 case BluetoothAdapter.STATE_OFF:
220 messageId = R.string.bluetooth_empty_list_bluetooth_off;
223 case BluetoothAdapter.STATE_TURNING_ON:
224 messageId = R.string.bluetooth_turning_on;
228 setDeviceListGroup(preferenceScreen);
230 mEmptyView.setText(messageId);
234 public void onBluetoothStateChanged(int bluetoothState) {
235 super.onBluetoothStateChanged(bluetoothState);
236 updateContent(bluetoothState);
240 public void onScanningStateChanged(boolean started) {
241 super.onScanningStateChanged(started);
242 // Update options' enabled state
243 getActivity().invalidateOptionsMenu();
246 public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
247 setDeviceListGroup(getPreferenceScreen());
249 updateContent(mLocalAdapter.getBluetoothState());
252 private final View.OnClickListener mDeviceProfilesListener = new View.OnClickListener() {
253 public void onClick(View v) {
254 // User clicked on advanced options icon for a device in the list
255 if (v.getTag() instanceof CachedBluetoothDevice) {
256 CachedBluetoothDevice device = (CachedBluetoothDevice) v.getTag();
258 Preference pref = new Preference(getActivity());
259 pref.setTitle(device.getName());
260 pref.setFragment(DeviceProfilesSettings.class.getName());
261 pref.getExtras().putParcelable(DeviceProfilesSettings.EXTRA_DEVICE,
263 ((PreferenceActivity) getActivity()).onPreferenceStartFragment(
264 BluetoothSettings.this, pref);
266 Log.w(TAG, "onClick() called for other View: " + v); // TODO remove
272 * Add a listener, which enables the advanced settings icon.
273 * @param preference the newly added preference
276 void initDevicePreference(BluetoothDevicePreference preference) {
277 CachedBluetoothDevice cachedDevice = preference.getCachedDevice();
278 if (cachedDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
279 // Only paired device have an associated advanced settings screen
280 preference.setOnSettingsClickListener(mDeviceProfilesListener);