OSDN Git Service

am 47504c7b: am a7bb204a: am c6f14410: Merge "Remove CHAR LIMIT from string to fix...
[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.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;
33
34 import com.android.settings.R;
35
36 /**
37  * BluetoothSettings is the Settings screen for Bluetooth configuration and
38  * connection management.
39  */
40 public final class BluetoothSettings extends DeviceListPreferenceFragment {
41     private static final String TAG = "BluetoothSettings";
42
43     private static final int MENU_ID_MAKE_DISCOVERABLE = Menu.FIRST;
44     private static final int MENU_ID_SCAN = Menu.FIRST + 1;
45     private static final int MENU_ID_ADVANCED = Menu.FIRST + 2;
46
47     private BluetoothEnabler mBluetoothEnabler;
48
49     /** Initialize the filter to show bonded devices only. */
50     //public BluetoothSettings() {
51     //    super(BluetoothDeviceFilter.BONDED_DEVICE_FILTER);
52     //}
53
54     @Override
55     void addPreferencesForActivity() {
56         addPreferencesFromResource(R.xml.bluetooth_settings);
57
58         Activity activity = getActivity();
59
60         Switch actionBarSwitch = new Switch(activity);
61
62         if (activity instanceof PreferenceActivity) {
63             PreferenceActivity preferenceActivity = (PreferenceActivity) activity;
64             if (preferenceActivity.onIsHidingHeaders() || !preferenceActivity.onIsMultiPane()) {
65                 final int padding = activity.getResources().getDimensionPixelSize(
66                         R.dimen.action_bar_switch_padding);
67                 actionBarSwitch.setPadding(0, 0, padding, 0);
68                 activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
69                         ActionBar.DISPLAY_SHOW_CUSTOM);
70                 activity.getActionBar().setCustomView(actionBarSwitch, new ActionBar.LayoutParams(
71                         ActionBar.LayoutParams.WRAP_CONTENT,
72                         ActionBar.LayoutParams.WRAP_CONTENT,
73                         Gravity.CENTER_VERTICAL | Gravity.RIGHT));
74             }
75         }
76
77         mBluetoothEnabler = new BluetoothEnabler(activity, actionBarSwitch);
78
79         if (mLocalAdapter != null && mLocalAdapter.isEnabled()) {
80             activity.getActionBar().setSubtitle(mLocalAdapter.getName());
81         }
82
83         // TODO activity.setTheme(android.R.style.Theme_Holo_SplitActionBarWhenNarrow);
84
85         setHasOptionsMenu(true);
86     }
87
88     @Override
89     public void onResume() {
90         super.onResume();
91
92         mBluetoothEnabler.resume();
93
94         updateContent(mLocalAdapter.getBluetoothState());
95     }
96
97     @Override
98     public void onPause() {
99         super.onPause();
100
101         mBluetoothEnabler.pause();
102     }
103
104     @Override
105     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
106         boolean bluetoothIsEnabled = mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON;
107         menu.add(Menu.NONE, MENU_ID_MAKE_DISCOVERABLE, 0, R.string.bluetooth_visibility)
108                 .setEnabled(bluetoothIsEnabled);
109         menu.add(Menu.NONE, MENU_ID_SCAN, 0, R.string.bluetooth_preference_find_nearby_title)
110                 .setIcon(R.drawable.ic_menu_scan_network).setEnabled(bluetoothIsEnabled);
111         menu.add(Menu.NONE, MENU_ID_ADVANCED, 0, R.string.bluetooth_menu_advanced)
112                 .setIcon(android.R.drawable.ic_menu_manage);
113     }
114
115     @Override
116     public boolean onOptionsItemSelected(MenuItem item) {
117         switch (item.getItemId()) {
118             case MENU_ID_MAKE_DISCOVERABLE:
119                 // TODO
120 //                if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
121 //                    onAddNetworkPressed();
122 //                }
123                 return true;
124             case MENU_ID_SCAN:
125                 if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
126                     mLocalAdapter.startScanning(true);
127                 }
128                 return true;
129             case MENU_ID_ADVANCED:
130                 if (getActivity() instanceof PreferenceActivity) {
131                     ((PreferenceActivity) getActivity()).startPreferencePanel(
132                             AdvancedBluetoothSettings.class.getCanonicalName(),
133                             null,
134                             R.string.bluetooth_advanced_titlebar, null,
135                             this, 0);
136                 } else {
137                     startFragment(this, AdvancedBluetoothSettings.class.getCanonicalName(), -1, null);
138                 }
139                 return true;
140         }
141         return super.onOptionsItemSelected(item);
142     }
143
144     private final View.OnClickListener mListener = new View.OnClickListener() {
145         public void onClick(View v) {
146             // User clicked on advanced options icon for a device in the list
147             if (v.getTag() instanceof CachedBluetoothDevice) {
148                 CachedBluetoothDevice device = (CachedBluetoothDevice) v.getTag();
149
150                 Preference pref = new Preference(getActivity());
151                 pref.setTitle(device.getName());
152                 pref.setFragment(DeviceProfilesSettings.class.getName());
153                 pref.getExtras().putParcelable(DeviceProfilesSettings.EXTRA_DEVICE,
154                         device.getDevice());
155                 ((PreferenceActivity) getActivity()).onPreferenceStartFragment(
156                         BluetoothSettings.this, pref);
157             } else {
158                 Log.w(TAG, "onClick() called for other View: " + v);
159             }
160         }
161     };
162
163     @Override
164     void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
165         mLocalAdapter.stopScanning();
166         super.onDevicePreferenceClick(btPreference);
167     }
168
169     @Override
170     public void onBluetoothStateChanged(int bluetoothState) {
171         super.onBluetoothStateChanged(bluetoothState);
172         updateContent(bluetoothState);
173     }
174
175     private void updateContent(int bluetoothState) {
176         final PreferenceScreen preferenceScreen = getPreferenceScreen();
177         getActivity().invalidateOptionsMenu();
178         int messageId = 0;
179
180         switch (bluetoothState) {
181             case BluetoothAdapter.STATE_ON:
182                 preferenceScreen.removeAll();
183                 // Repopulate (which isn't too bad since it's cached in the settings bluetooth manager)
184                 addDevices();
185                 mLocalAdapter.startScanning(false);
186                 return;
187
188             case BluetoothAdapter.STATE_TURNING_OFF:
189                 int preferenceCount = preferenceScreen.getPreferenceCount();
190                 for (int i = 0; i < preferenceCount; i++) {
191                     preferenceScreen.getPreference(i).setEnabled(false);
192                 }
193                 return;
194
195             case BluetoothAdapter.STATE_OFF:
196                 messageId = R.string.bluetooth_empty_list_bluetooth_off;
197                 break;
198
199             case BluetoothAdapter.STATE_TURNING_ON:
200                 messageId = R.string.bluetooth_turning_on;
201                 break;
202         }
203
204         removeAllDevices();
205         // TODO: from xml, add top padding. Same as in wifi
206         Preference emptyListPreference = new Preference(getActivity());
207         emptyListPreference.setTitle(messageId);
208         preferenceScreen.addPreference(emptyListPreference);
209     }
210
211     public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
212         if (bondState == BluetoothDevice.BOND_BONDED) {
213             // add to "Paired devices" list after remote-initiated pairing
214             if (mDevicePreferenceMap.get(cachedDevice) == null) {
215                 createDevicePreference(cachedDevice);
216             }
217         } else if (bondState == BluetoothDevice.BOND_NONE) {
218             // remove unpaired device from paired devices list
219             onDeviceDeleted(cachedDevice);
220         }
221     }
222
223     /**
224      * Add a listener, which enables the advanced settings icon.
225      * @param preference the newly added preference
226      */
227     @Override
228     void initDevicePreference(BluetoothDevicePreference preference) {
229         preference.setOnSettingsClickListener(mListener);
230     }
231 }