OSDN Git Service

Merge "Changed text and appearance when Wifi/Bluetooth is off"
[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.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;
38
39 import com.android.settings.ProgressCategory;
40 import com.android.settings.R;
41
42 /**
43  * BluetoothSettings is the Settings screen for Bluetooth configuration and
44  * connection management.
45  */
46 public final class BluetoothSettings extends DeviceListPreferenceFragment {
47     private static final String TAG = "BluetoothSettings";
48
49     private static final int MENU_ID_SCAN = Menu.FIRST;
50     private static final int MENU_ID_ADVANCED = Menu.FIRST + 1;
51
52     private BluetoothEnabler mBluetoothEnabler;
53
54     private PreferenceGroup mFoundDevicesCategory;
55     private boolean mFoundDevicesCategoryIsPresent;
56
57     private View mView;
58     private TextView mEmptyView;
59
60     @Override
61     public View onCreateView(LayoutInflater inflater, ViewGroup container,
62             Bundle savedInstanceState) {
63         mView = inflater.inflate(R.layout.custom_preference_list_fragment, container, false);
64         return mView;
65     }
66
67     @Override
68     public void onActivityCreated(Bundle savedInstanceState) {
69         super.onActivityCreated(savedInstanceState);
70
71         mEmptyView = (TextView) mView.findViewById(R.id.empty);
72         getListView().setEmptyView(mEmptyView);
73     }
74
75     @Override
76     void addPreferencesForActivity() {
77         addPreferencesFromResource(R.xml.bluetooth_settings);
78
79         Activity activity = getActivity();
80
81         Switch actionBarSwitch = new Switch(activity);
82
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));
95             }
96         }
97
98         mBluetoothEnabler = new BluetoothEnabler(activity, actionBarSwitch);
99
100         if (mLocalAdapter != null && mLocalAdapter.isEnabled()) {
101             activity.getActionBar().setSubtitle(mLocalAdapter.getName());
102         }
103
104         setHasOptionsMenu(true);
105     }
106
107     @Override
108     public void onResume() {
109         super.onResume();
110
111         mBluetoothEnabler.resume();
112
113         updateContent(mLocalAdapter.getBluetoothState());
114     }
115
116     @Override
117     public void onPause() {
118         super.onPause();
119
120         mBluetoothEnabler.pause();
121     }
122
123     @Override
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);
136     }
137
138     @Override
139     public boolean onOptionsItemSelected(MenuItem item) {
140         switch (item.getItemId()) {
141             case MENU_ID_SCAN:
142                 if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
143                     startScanning();
144                 }
145                 return true;
146             case MENU_ID_ADVANCED:
147                 if (getActivity() instanceof PreferenceActivity) {
148                     ((PreferenceActivity) getActivity()).startPreferencePanel(
149                             AdvancedBluetoothSettings.class.getCanonicalName(),
150                             null,
151                             R.string.bluetooth_advanced_titlebar, null,
152                             this, 0);
153                 } else {
154                     startFragment(this, AdvancedBluetoothSettings.class.getCanonicalName(), -1, null);
155                 }
156                 return true;
157         }
158         return super.onOptionsItemSelected(item);
159     }
160
161     private void startScanning() {
162         if (!mFoundDevicesCategoryIsPresent) {
163             getPreferenceScreen().addPreference(mFoundDevicesCategory);
164         }
165         mLocalAdapter.startScanning(true);
166     }
167
168     @Override
169     void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
170         mLocalAdapter.stopScanning();
171         super.onDevicePreferenceClick(btPreference);
172     }
173
174     private void updateContent(int bluetoothState) {
175         final PreferenceScreen preferenceScreen = getPreferenceScreen();
176         getActivity().invalidateOptionsMenu();
177         int messageId = 0;
178
179         switch (bluetoothState) {
180             case BluetoothAdapter.STATE_ON:
181                 preferenceScreen.removeAll();
182
183                 // Add bonded devices from cache first
184                 setFilter(BluetoothDeviceFilter.BONDED_DEVICE_FILTER);
185                 setDeviceListGroup(preferenceScreen);
186                 preferenceScreen.setOrderingAsAdded(true);
187
188                 addCachedDevices();
189                 int numberOfPairedDevices = preferenceScreen.getPreferenceCount();
190
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;
196
197                 // Unbonded found devices from cache
198                 setFilter(BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER);
199                 setDeviceListGroup(mFoundDevicesCategory);
200                 addCachedDevices();
201
202                 int numberOfUnpairedDevices = mFoundDevicesCategory.getPreferenceCount();
203                 if (numberOfUnpairedDevices == 0) {
204                     preferenceScreen.removePreference(mFoundDevicesCategory);
205                     mFoundDevicesCategoryIsPresent = false;
206                 }
207
208                 if (numberOfPairedDevices == 0) startScanning();
209
210                 return;
211
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);
216                 }
217                 return;
218
219             case BluetoothAdapter.STATE_OFF:
220                 messageId = R.string.bluetooth_empty_list_bluetooth_off;
221                 break;
222
223             case BluetoothAdapter.STATE_TURNING_ON:
224                 messageId = R.string.bluetooth_turning_on;
225                 break;
226         }
227
228         setDeviceListGroup(preferenceScreen);
229         removeAllDevices();
230         mEmptyView.setText(messageId);
231     }
232
233     @Override
234     public void onBluetoothStateChanged(int bluetoothState) {
235         super.onBluetoothStateChanged(bluetoothState);
236         updateContent(bluetoothState);
237     }
238
239     @Override
240     public void onScanningStateChanged(boolean started) {
241         super.onScanningStateChanged(started);
242         // Update options' enabled state
243         getActivity().invalidateOptionsMenu();
244     }
245
246     public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
247         setDeviceListGroup(getPreferenceScreen());
248         removeAllDevices();
249         updateContent(mLocalAdapter.getBluetoothState());
250     }
251
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();
257
258                 Preference pref = new Preference(getActivity());
259                 pref.setTitle(device.getName());
260                 pref.setFragment(DeviceProfilesSettings.class.getName());
261                 pref.getExtras().putParcelable(DeviceProfilesSettings.EXTRA_DEVICE,
262                         device.getDevice());
263                 ((PreferenceActivity) getActivity()).onPreferenceStartFragment(
264                         BluetoothSettings.this, pref);
265             } else {
266                 Log.w(TAG, "onClick() called for other View: " + v); // TODO remove
267             }
268         }
269     };
270
271     /**
272      * Add a listener, which enables the advanced settings icon.
273      * @param preference the newly added preference
274      */
275     @Override
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);
281         }
282     }
283 }