diff options
Diffstat (limited to 'src/com/android/settings/bluetooth/BluetoothSettings.java')
-rw-r--r-- | src/com/android/settings/bluetooth/BluetoothSettings.java | 179 |
1 files changed, 131 insertions, 48 deletions
diff --git a/src/com/android/settings/bluetooth/BluetoothSettings.java b/src/com/android/settings/bluetooth/BluetoothSettings.java index 5e4e130..9c90b20 100644 --- a/src/com/android/settings/bluetooth/BluetoothSettings.java +++ b/src/com/android/settings/bluetooth/BluetoothSettings.java @@ -16,15 +16,20 @@ package com.android.settings.bluetooth; +import android.app.ActionBar; +import android.app.Activity; +import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; -import android.content.Intent; -import android.preference.CheckBoxPreference; -import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceScreen; import android.util.Log; +import android.view.Gravity; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; +import android.widget.Switch; import com.android.settings.R; @@ -35,77 +40,120 @@ import com.android.settings.R; public final class BluetoothSettings extends DeviceListPreferenceFragment { private static final String TAG = "BluetoothSettings"; - private static final String KEY_BT_CHECKBOX = "bt_checkbox"; - private static final String KEY_BT_DISCOVERABLE = "bt_discoverable"; - private static final String KEY_BT_DISCOVERABLE_TIMEOUT = "bt_discoverable_timeout"; - private static final String KEY_BT_NAME = "bt_name"; - private static final String KEY_BT_SHOW_RECEIVED = "bt_show_received_files"; + private static final int MENU_ID_MAKE_DISCOVERABLE = Menu.FIRST; + private static final int MENU_ID_SCAN = Menu.FIRST + 1; + private static final int MENU_ID_ADVANCED = Menu.FIRST + 2; - private BluetoothEnabler mEnabler; - private BluetoothDiscoverableEnabler mDiscoverableEnabler; - private BluetoothNamePreference mNamePreference; - - /* Private intent to show the list of received files */ - private static final String BTOPP_ACTION_OPEN_RECEIVED_FILES = - "android.btopp.intent.action.OPEN_RECEIVED_FILES"; + private BluetoothEnabler mBluetoothEnabler; /** Initialize the filter to show bonded devices only. */ - public BluetoothSettings() { - super(BluetoothDeviceFilter.BONDED_DEVICE_FILTER); - } + //public BluetoothSettings() { + // super(BluetoothDeviceFilter.BONDED_DEVICE_FILTER); + //} @Override void addPreferencesForActivity() { addPreferencesFromResource(R.xml.bluetooth_settings); - mEnabler = new BluetoothEnabler(getActivity(), - (CheckBoxPreference) findPreference(KEY_BT_CHECKBOX)); + Activity activity = getActivity(); + + Switch actionBarSwitch = new Switch(activity); + + if (activity instanceof PreferenceActivity) { + PreferenceActivity preferenceActivity = (PreferenceActivity) activity; + if (preferenceActivity.onIsHidingHeaders() || !preferenceActivity.onIsMultiPane()) { + final int padding = activity.getResources().getDimensionPixelSize( + R.dimen.action_bar_switch_padding); + actionBarSwitch.setPadding(0, 0, padding, 0); + activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, + ActionBar.DISPLAY_SHOW_CUSTOM); + activity.getActionBar().setCustomView(actionBarSwitch, new ActionBar.LayoutParams( + ActionBar.LayoutParams.WRAP_CONTENT, + ActionBar.LayoutParams.WRAP_CONTENT, + Gravity.CENTER_VERTICAL | Gravity.RIGHT)); + } + } + + mBluetoothEnabler = new BluetoothEnabler(activity, actionBarSwitch); - mDiscoverableEnabler = new BluetoothDiscoverableEnabler(getActivity(), - mLocalAdapter, - (CheckBoxPreference) findPreference(KEY_BT_DISCOVERABLE), - (ListPreference) findPreference(KEY_BT_DISCOVERABLE_TIMEOUT)); + if (mLocalAdapter != null && mLocalAdapter.isEnabled()) { + activity.getActionBar().setSubtitle(mLocalAdapter.getName()); + } + + // TODO activity.setTheme(android.R.style.Theme_Holo_SplitActionBarWhenNarrow); - mNamePreference = (BluetoothNamePreference) findPreference(KEY_BT_NAME); + setHasOptionsMenu(true); } @Override public void onResume() { super.onResume(); - // Repopulate (which isn't too bad since it's cached in the settings - // bluetooth manager) - addDevices(); + mBluetoothEnabler.resume(); - mEnabler.resume(); - mDiscoverableEnabler.resume(); - mNamePreference.resume(); + updateContent(mLocalAdapter.getBluetoothState()); } @Override public void onPause() { super.onPause(); - mNamePreference.pause(); - mDiscoverableEnabler.pause(); - mEnabler.pause(); + mBluetoothEnabler.pause(); + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + boolean bluetoothIsEnabled = mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON; + menu.add(Menu.NONE, MENU_ID_MAKE_DISCOVERABLE, 0, R.string.bluetooth_visibility) + .setEnabled(bluetoothIsEnabled); + menu.add(Menu.NONE, MENU_ID_SCAN, 0, R.string.bluetooth_preference_find_nearby_title) + .setIcon(R.drawable.ic_menu_scan_network).setEnabled(bluetoothIsEnabled); + menu.add(Menu.NONE, MENU_ID_ADVANCED, 0, R.string.bluetooth_menu_advanced) + .setIcon(android.R.drawable.ic_menu_manage); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case MENU_ID_MAKE_DISCOVERABLE: + // TODO +// if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) { +// onAddNetworkPressed(); +// } + return true; + case MENU_ID_SCAN: + if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) { + mLocalAdapter.startScanning(true); + } + return true; + case MENU_ID_ADVANCED: + if (getActivity() instanceof PreferenceActivity) { + ((PreferenceActivity) getActivity()).startPreferencePanel( + AdvancedBluetoothSettings.class.getCanonicalName(), + null, + R.string.bluetooth_advanced_titlebar, null, + this, 0); + } else { + startFragment(this, AdvancedBluetoothSettings.class.getCanonicalName(), -1, null); + } + return true; + } + return super.onOptionsItemSelected(item); } private final View.OnClickListener mListener = new View.OnClickListener() { public void onClick(View v) { // User clicked on advanced options icon for a device in the list if (v.getTag() instanceof CachedBluetoothDevice) { - CachedBluetoothDevice - device = (CachedBluetoothDevice) v.getTag(); + CachedBluetoothDevice device = (CachedBluetoothDevice) v.getTag(); Preference pref = new Preference(getActivity()); pref.setTitle(device.getName()); pref.setFragment(DeviceProfilesSettings.class.getName()); pref.getExtras().putParcelable(DeviceProfilesSettings.EXTRA_DEVICE, device.getDevice()); - ((PreferenceActivity) getActivity()) - .onPreferenceStartFragment(BluetoothSettings.this, - pref); + ((PreferenceActivity) getActivity()).onPreferenceStartFragment( + BluetoothSettings.this, pref); } else { Log.w(TAG, "onClick() called for other View: " + v); } @@ -113,19 +161,54 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment { }; @Override - public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, - Preference preference) { - if (KEY_BT_SHOW_RECEIVED.equals(preference.getKey())) { - Intent intent = new Intent(BTOPP_ACTION_OPEN_RECEIVED_FILES); - getActivity().sendBroadcast(intent); - return true; + void onDevicePreferenceClick(BluetoothDevicePreference btPreference) { + mLocalAdapter.stopScanning(); + super.onDevicePreferenceClick(btPreference); + } + + @Override + public void onBluetoothStateChanged(int bluetoothState) { + super.onBluetoothStateChanged(bluetoothState); + updateContent(bluetoothState); + } + + private void updateContent(int bluetoothState) { + final PreferenceScreen preferenceScreen = getPreferenceScreen(); + getActivity().invalidateOptionsMenu(); + int messageId = 0; + + switch (bluetoothState) { + case BluetoothAdapter.STATE_ON: + preferenceScreen.removeAll(); + // Repopulate (which isn't too bad since it's cached in the settings bluetooth manager) + addDevices(); + mLocalAdapter.startScanning(false); + return; + + case BluetoothAdapter.STATE_TURNING_OFF: + int preferenceCount = preferenceScreen.getPreferenceCount(); + for (int i = 0; i < preferenceCount; i++) { + preferenceScreen.getPreference(i).setEnabled(false); + } + return; + + case BluetoothAdapter.STATE_OFF: + messageId = R.string.bluetooth_empty_list_bluetooth_off; + break; + + case BluetoothAdapter.STATE_TURNING_ON: + messageId = R.string.bluetooth_turning_on; + break; } - return super.onPreferenceTreeClick(preferenceScreen, preference); + removeAllDevices(); + // TODO: from xml, add top padding. Same as in wifi + Preference emptyListPreference = new Preference(getActivity()); + emptyListPreference.setTitle(messageId); + preferenceScreen.addPreference(emptyListPreference); } - public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, - int bondState) { + public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) { if (bondState == BluetoothDevice.BOND_BONDED) { // add to "Paired devices" list after remote-initiated pairing if (mDevicePreferenceMap.get(cachedDevice) == null) { |