OSDN Git Service

Merge "Fix up storage UI for old USB storage."
[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         menu.add(Menu.NONE, MENU_ID_SCAN, 0, R.string.bluetooth_scan_nearby_devices)
105                 //.setIcon(R.drawable.ic_menu_scan_network)
106                 .setEnabled(bluetoothIsEnabled && !mLocalAdapter.isDiscovering())
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);
111     }
112
113     @Override
114     public boolean onOptionsItemSelected(MenuItem item) {
115         switch (item.getItemId()) {
116             case MENU_ID_SCAN:
117                 if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
118                     startScanning();
119                 }
120                 return true;
121             case MENU_ID_ADVANCED:
122                 if (getActivity() instanceof PreferenceActivity) {
123                     ((PreferenceActivity) getActivity()).startPreferencePanel(
124                             AdvancedBluetoothSettings.class.getCanonicalName(),
125                             null,
126                             R.string.bluetooth_advanced_titlebar, null,
127                             this, 0);
128                 } else {
129                     startFragment(this, AdvancedBluetoothSettings.class.getCanonicalName(), -1, null);
130                 }
131                 return true;
132         }
133         return super.onOptionsItemSelected(item);
134     }
135
136     private void startScanning() {
137         if (!mFoundDevicesCategoryIsPresent) {
138             getPreferenceScreen().addPreference(mFoundDevicesCategory);
139         }
140         mLocalAdapter.startScanning(true);
141     }
142
143     @Override
144     void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
145         mLocalAdapter.stopScanning();
146         super.onDevicePreferenceClick(btPreference);
147     }
148
149     private void updateContent(int bluetoothState) {
150         final PreferenceScreen preferenceScreen = getPreferenceScreen();
151         getActivity().invalidateOptionsMenu();
152         int messageId = 0;
153
154         switch (bluetoothState) {
155             case BluetoothAdapter.STATE_ON:
156                 preferenceScreen.removeAll();
157
158                 // Add bonded devices from cache first
159                 setFilter(BluetoothDeviceFilter.BONDED_DEVICE_FILTER);
160                 setDeviceListGroup(preferenceScreen);
161                 preferenceScreen.setOrderingAsAdded(true);
162
163                 addCachedDevices();
164                 int numberOfPairedDevices = preferenceScreen.getPreferenceCount();
165
166                 // Found devices category
167                 mFoundDevicesCategory = new ProgressCategory(getActivity(), null);
168                 mFoundDevicesCategory.setTitle(R.string.bluetooth_preference_found_devices);
169                 preferenceScreen.addPreference(mFoundDevicesCategory);
170                 mFoundDevicesCategoryIsPresent = true;
171
172                 // Unbonded found devices from cache
173                 setFilter(BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER);
174                 setDeviceListGroup(mFoundDevicesCategory);
175                 addCachedDevices();
176
177                 int numberOfUnpairedDevices = mFoundDevicesCategory.getPreferenceCount();
178                 if (numberOfUnpairedDevices == 0) {
179                     preferenceScreen.removePreference(mFoundDevicesCategory);
180                     mFoundDevicesCategoryIsPresent = false;
181                 }
182
183                 if (numberOfPairedDevices == 0) startScanning();
184
185                 return;
186
187             case BluetoothAdapter.STATE_TURNING_OFF:
188                 int preferenceCount = preferenceScreen.getPreferenceCount();
189                 for (int i = 0; i < preferenceCount; i++) {
190                     preferenceScreen.getPreference(i).setEnabled(false);
191                 }
192                 return;
193
194             case BluetoothAdapter.STATE_OFF:
195                 messageId = R.string.bluetooth_empty_list_bluetooth_off;
196                 break;
197
198             case BluetoothAdapter.STATE_TURNING_ON:
199                 messageId = R.string.bluetooth_turning_on;
200                 break;
201         }
202
203         setDeviceListGroup(preferenceScreen);
204         removeAllDevices();
205
206         // TODO: from xml, add top padding. Same as in wifi
207         Preference emptyListPreference = new Preference(getActivity());
208         emptyListPreference.setTitle(messageId);
209         preferenceScreen.addPreference(emptyListPreference);
210     }
211
212     @Override
213     public void onBluetoothStateChanged(int bluetoothState) {
214         super.onBluetoothStateChanged(bluetoothState);
215         updateContent(bluetoothState);
216     }
217
218     @Override
219     public void onScanningStateChanged(boolean started) {
220         super.onScanningStateChanged(started);
221         // Update 'Scan' option enabled state
222         getActivity().invalidateOptionsMenu();
223     }
224
225     public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
226         setDeviceListGroup(getPreferenceScreen());
227         removeAllDevices();
228         updateContent(mLocalAdapter.getBluetoothState());
229     }
230
231     private final View.OnClickListener mDeviceProfilesListener = new View.OnClickListener() {
232         public void onClick(View v) {
233             // User clicked on advanced options icon for a device in the list
234             if (v.getTag() instanceof CachedBluetoothDevice) {
235                 CachedBluetoothDevice device = (CachedBluetoothDevice) v.getTag();
236
237                 Preference pref = new Preference(getActivity());
238                 pref.setTitle(device.getName());
239                 pref.setFragment(DeviceProfilesSettings.class.getName());
240                 pref.getExtras().putParcelable(DeviceProfilesSettings.EXTRA_DEVICE,
241                         device.getDevice());
242                 ((PreferenceActivity) getActivity()).onPreferenceStartFragment(
243                         BluetoothSettings.this, pref);
244             } else {
245                 Log.w(TAG, "onClick() called for other View: " + v); // TODO remove
246             }
247         }
248     };
249
250     /**
251      * Add a listener, which enables the advanced settings icon.
252      * @param preference the newly added preference
253      */
254     @Override
255     void initDevicePreference(BluetoothDevicePreference preference) {
256         CachedBluetoothDevice cachedDevice = preference.getCachedDevice();
257         if (cachedDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
258             // Only paired device have an associated advanced settings screen
259             preference.setOnSettingsClickListener(mDeviceProfilesListener);
260         }
261     }
262 }