OSDN Git Service

Split ActionBar in Settings
[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_SCAN = Menu.FIRST;
44     private static final int MENU_ID_ADVANCED = Menu.FIRST + 1;
45
46     private BluetoothEnabler mBluetoothEnabler;
47
48     /** Initialize the filter to show bonded devices only. */
49     //public BluetoothSettings() {
50     //    super(BluetoothDeviceFilter.BONDED_DEVICE_FILTER);
51     //}
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_preference_find_nearby_title)
105                 //.setIcon(R.drawable.ic_menu_scan_network)
106                 .setEnabled(bluetoothIsEnabled)
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                 // TODO
117 //                if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
118 //                    onAddNetworkPressed();
119 //                }
120             case MENU_ID_SCAN:
121                 if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
122                     mLocalAdapter.startScanning(true);
123                 }
124                 return true;
125             case MENU_ID_ADVANCED:
126                 if (getActivity() instanceof PreferenceActivity) {
127                     ((PreferenceActivity) getActivity()).startPreferencePanel(
128                             AdvancedBluetoothSettings.class.getCanonicalName(),
129                             null,
130                             R.string.bluetooth_advanced_titlebar, null,
131                             this, 0);
132                 } else {
133                     startFragment(this, AdvancedBluetoothSettings.class.getCanonicalName(), -1, null);
134                 }
135                 return true;
136         }
137         return super.onOptionsItemSelected(item);
138     }
139
140     private final View.OnClickListener mListener = new View.OnClickListener() {
141         public void onClick(View v) {
142             // User clicked on advanced options icon for a device in the list
143             if (v.getTag() instanceof CachedBluetoothDevice) {
144                 CachedBluetoothDevice device = (CachedBluetoothDevice) v.getTag();
145
146                 Preference pref = new Preference(getActivity());
147                 pref.setTitle(device.getName());
148                 pref.setFragment(DeviceProfilesSettings.class.getName());
149                 pref.getExtras().putParcelable(DeviceProfilesSettings.EXTRA_DEVICE,
150                         device.getDevice());
151                 ((PreferenceActivity) getActivity()).onPreferenceStartFragment(
152                         BluetoothSettings.this, pref);
153             } else {
154                 Log.w(TAG, "onClick() called for other View: " + v);
155             }
156         }
157     };
158
159     @Override
160     void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
161         mLocalAdapter.stopScanning();
162         super.onDevicePreferenceClick(btPreference);
163     }
164
165     @Override
166     public void onBluetoothStateChanged(int bluetoothState) {
167         super.onBluetoothStateChanged(bluetoothState);
168         updateContent(bluetoothState);
169     }
170
171     private void updateContent(int bluetoothState) {
172         final PreferenceScreen preferenceScreen = getPreferenceScreen();
173         getActivity().invalidateOptionsMenu();
174         int messageId = 0;
175
176         switch (bluetoothState) {
177             case BluetoothAdapter.STATE_ON:
178                 preferenceScreen.removeAll();
179                 // Repopulate (which isn't too bad since it's cached in the settings bluetooth manager)
180                 addDevices();
181                 mLocalAdapter.startScanning(false);
182                 return;
183
184             case BluetoothAdapter.STATE_TURNING_OFF:
185                 int preferenceCount = preferenceScreen.getPreferenceCount();
186                 for (int i = 0; i < preferenceCount; i++) {
187                     preferenceScreen.getPreference(i).setEnabled(false);
188                 }
189                 return;
190
191             case BluetoothAdapter.STATE_OFF:
192                 messageId = R.string.bluetooth_empty_list_bluetooth_off;
193                 break;
194
195             case BluetoothAdapter.STATE_TURNING_ON:
196                 messageId = R.string.bluetooth_turning_on;
197                 break;
198         }
199
200         removeAllDevices();
201         // TODO: from xml, add top padding. Same as in wifi
202         Preference emptyListPreference = new Preference(getActivity());
203         emptyListPreference.setTitle(messageId);
204         preferenceScreen.addPreference(emptyListPreference);
205     }
206
207     public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
208         if (bondState == BluetoothDevice.BOND_BONDED) {
209             // add to "Paired devices" list after remote-initiated pairing
210             if (mDevicePreferenceMap.get(cachedDevice) == null) {
211                 createDevicePreference(cachedDevice);
212             }
213         } else if (bondState == BluetoothDevice.BOND_NONE) {
214             // remove unpaired device from paired devices list
215             onDeviceDeleted(cachedDevice);
216         }
217     }
218
219     /**
220      * Add a listener, which enables the advanced settings icon.
221      * @param preference the newly added preference
222      */
223     @Override
224     void initDevicePreference(BluetoothDevicePreference preference) {
225         preference.setOnSettingsClickListener(mListener);
226     }
227 }