OSDN Git Service

Merge "AdvancedSettings replaced by AdvancedWifiSettings"
[android-x86/packages-apps-Settings.git] / src / com / android / settings / bluetooth / BluetoothSettings.java
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.settings.bluetooth;
18
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;
34
35 import com.android.settings.ProgressCategory;
36 import com.android.settings.R;
37
38 /**
39  * BluetoothSettings is the Settings screen for Bluetooth configuration and
40  * connection management.
41  */
42 public final class BluetoothSettings extends DeviceListPreferenceFragment {
43     private static final String TAG = "BluetoothSettings";
44
45     private static final int MENU_ID_SCAN = Menu.FIRST;
46     private static final int MENU_ID_ADVANCED = Menu.FIRST + 1;
47
48     private BluetoothEnabler mBluetoothEnabler;
49
50     private PreferenceGroup mFoundDevicesCategory;
51     private boolean mFoundDevicesCategoryIsPresent;
52
53     @Override
54     void addPreferencesForActivity() {
55         addPreferencesFromResource(R.xml.bluetooth_settings);
56
57         Activity activity = getActivity();
58
59         Switch actionBarSwitch = new Switch(activity);
60
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));
73             }
74         }
75
76         mBluetoothEnabler = new BluetoothEnabler(activity, actionBarSwitch);
77
78         if (mLocalAdapter != null && mLocalAdapter.isEnabled()) {
79             activity.getActionBar().setSubtitle(mLocalAdapter.getName());
80         }
81
82         setHasOptionsMenu(true);
83     }
84
85     @Override
86     public void onResume() {
87         super.onResume();
88
89         mBluetoothEnabler.resume();
90
91         updateContent(mLocalAdapter.getBluetoothState());
92     }
93
94     @Override
95     public void onPause() {
96         super.onPause();
97
98         mBluetoothEnabler.pause();
99     }
100
101     @Override
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);
114     }
115
116     @Override
117     public boolean onOptionsItemSelected(MenuItem item) {
118         switch (item.getItemId()) {
119             case MENU_ID_SCAN:
120                 if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
121                     startScanning();
122                 }
123                 return true;
124             case MENU_ID_ADVANCED:
125                 if (getActivity() instanceof PreferenceActivity) {
126                     ((PreferenceActivity) getActivity()).startPreferencePanel(
127                             AdvancedBluetoothSettings.class.getCanonicalName(),
128                             null,
129                             R.string.bluetooth_advanced_titlebar, null,
130                             this, 0);
131                 } else {
132                     startFragment(this, AdvancedBluetoothSettings.class.getCanonicalName(), -1, null);
133                 }
134                 return true;
135         }
136         return super.onOptionsItemSelected(item);
137     }
138
139     private void startScanning() {
140         if (!mFoundDevicesCategoryIsPresent) {
141             getPreferenceScreen().addPreference(mFoundDevicesCategory);
142         }
143         mLocalAdapter.startScanning(true);
144     }
145
146     @Override
147     void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
148         mLocalAdapter.stopScanning();
149         super.onDevicePreferenceClick(btPreference);
150     }
151
152     private void updateContent(int bluetoothState) {
153         final PreferenceScreen preferenceScreen = getPreferenceScreen();
154         getActivity().invalidateOptionsMenu();
155         int messageId = 0;
156
157         switch (bluetoothState) {
158             case BluetoothAdapter.STATE_ON:
159                 preferenceScreen.removeAll();
160
161                 // Add bonded devices from cache first
162                 setFilter(BluetoothDeviceFilter.BONDED_DEVICE_FILTER);
163                 setDeviceListGroup(preferenceScreen);
164                 preferenceScreen.setOrderingAsAdded(true);
165
166                 addCachedDevices();
167                 int numberOfPairedDevices = preferenceScreen.getPreferenceCount();
168
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;
174
175                 // Unbonded found devices from cache
176                 setFilter(BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER);
177                 setDeviceListGroup(mFoundDevicesCategory);
178                 addCachedDevices();
179
180                 int numberOfUnpairedDevices = mFoundDevicesCategory.getPreferenceCount();
181                 if (numberOfUnpairedDevices == 0) {
182                     preferenceScreen.removePreference(mFoundDevicesCategory);
183                     mFoundDevicesCategoryIsPresent = false;
184                 }
185
186                 if (numberOfPairedDevices == 0) startScanning();
187
188                 return;
189
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);
194                 }
195                 return;
196
197             case BluetoothAdapter.STATE_OFF:
198                 messageId = R.string.bluetooth_empty_list_bluetooth_off;
199                 break;
200
201             case BluetoothAdapter.STATE_TURNING_ON:
202                 messageId = R.string.bluetooth_turning_on;
203                 break;
204         }
205
206         setDeviceListGroup(preferenceScreen);
207         removeAllDevices();
208
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);
213     }
214
215     @Override
216     public void onBluetoothStateChanged(int bluetoothState) {
217         super.onBluetoothStateChanged(bluetoothState);
218         updateContent(bluetoothState);
219     }
220
221     @Override
222     public void onScanningStateChanged(boolean started) {
223         super.onScanningStateChanged(started);
224         // Update options' enabled state
225         getActivity().invalidateOptionsMenu();
226     }
227
228     public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
229         setDeviceListGroup(getPreferenceScreen());
230         removeAllDevices();
231         updateContent(mLocalAdapter.getBluetoothState());
232     }
233
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();
239
240                 Preference pref = new Preference(getActivity());
241                 pref.setTitle(device.getName());
242                 pref.setFragment(DeviceProfilesSettings.class.getName());
243                 pref.getExtras().putParcelable(DeviceProfilesSettings.EXTRA_DEVICE,
244                         device.getDevice());
245                 ((PreferenceActivity) getActivity()).onPreferenceStartFragment(
246                         BluetoothSettings.this, pref);
247             } else {
248                 Log.w(TAG, "onClick() called for other View: " + v); // TODO remove
249             }
250         }
251     };
252
253     /**
254      * Add a listener, which enables the advanced settings icon.
255      * @param preference the newly added preference
256      */
257     @Override
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);
263         }
264     }
265 }