diff options
Diffstat (limited to 'src/com/android/settings/bluetooth/BluetoothSettings.java')
-rw-r--r-- | src/com/android/settings/bluetooth/BluetoothSettings.java | 290 |
1 files changed, 226 insertions, 64 deletions
diff --git a/src/com/android/settings/bluetooth/BluetoothSettings.java b/src/com/android/settings/bluetooth/BluetoothSettings.java index 5e4e130..2208223 100644 --- a/src/com/android/settings/bluetooth/BluetoothSettings.java +++ b/src/com/android/settings/bluetooth/BluetoothSettings.java @@ -16,16 +16,28 @@ 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.os.Bundle; import android.preference.Preference; import android.preference.PreferenceActivity; +import android.preference.PreferenceCategory; +import android.preference.PreferenceGroup; import android.preference.PreferenceScreen; import android.util.Log; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; +import android.view.ViewGroup; +import android.widget.Switch; +import android.widget.TextView; +import com.android.settings.ProgressCategory; import com.android.settings.R; /** @@ -35,107 +47,253 @@ 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_SCAN = Menu.FIRST; + private static final int MENU_ID_ADVANCED = Menu.FIRST + 1; - private BluetoothEnabler mEnabler; - private BluetoothDiscoverableEnabler mDiscoverableEnabler; - private BluetoothNamePreference mNamePreference; + private BluetoothEnabler mBluetoothEnabler; - /* 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 PreferenceGroup mAvailableDevicesCategory; + private boolean mAvailableDevicesCategoryIsPresent; - /** Initialize the filter to show bonded devices only. */ - public BluetoothSettings() { - super(BluetoothDeviceFilter.BONDED_DEVICE_FILTER); + private View mView; + private TextView mEmptyView; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + mView = inflater.inflate(R.layout.custom_preference_list_fragment, container, false); + return mView; + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + mEmptyView = (TextView) mView.findViewById(R.id.empty); + getListView().setEmptyView(mEmptyView); } @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); - mDiscoverableEnabler = new BluetoothDiscoverableEnabler(getActivity(), - mLocalAdapter, - (CheckBoxPreference) findPreference(KEY_BT_DISCOVERABLE), - (ListPreference) findPreference(KEY_BT_DISCOVERABLE_TIMEOUT)); + 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)); + } + } - mNamePreference = (BluetoothNamePreference) findPreference(KEY_BT_NAME); + mBluetoothEnabler = new BluetoothEnabler(activity, actionBarSwitch); + + 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; + boolean isDiscovering = mLocalAdapter.isDiscovering(); + int textId = isDiscovering ? R.string.bluetooth_searching_for_devices : + R.string.bluetooth_search_for_devices; + menu.add(Menu.NONE, MENU_ID_SCAN, 0, textId) + //.setIcon(R.drawable.ic_menu_scan_network) + .setEnabled(bluetoothIsEnabled && !isDiscovering) + .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); + menu.add(Menu.NONE, MENU_ID_ADVANCED, 0, R.string.bluetooth_menu_advanced) + //.setIcon(android.R.drawable.ic_menu_manage) + .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case MENU_ID_SCAN: + if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) { + startScanning(); + } + 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 void startScanning() { + if (!mAvailableDevicesCategoryIsPresent) { + getPreferenceScreen().addPreference(mAvailableDevicesCategory); + } + mLocalAdapter.startScanning(true); + } + + @Override + void onDevicePreferenceClick(BluetoothDevicePreference btPreference) { + mLocalAdapter.stopScanning(); + super.onDevicePreferenceClick(btPreference); + } + + private void addDeviceCategory(PreferenceGroup preferenceGroup, int titleId, + BluetoothDeviceFilter.Filter filter) { + preferenceGroup.setTitle(titleId); + getPreferenceScreen().addPreference(preferenceGroup); + setFilter(filter); + setDeviceListGroup(preferenceGroup); + addCachedDevices(); + preferenceGroup.setEnabled(true); + } + + private void updateContent(int bluetoothState) { + final PreferenceScreen preferenceScreen = getPreferenceScreen(); + getActivity().invalidateOptionsMenu(); + int messageId = 0; + + switch (bluetoothState) { + case BluetoothAdapter.STATE_ON: + preferenceScreen.removeAll(); + preferenceScreen.setOrderingAsAdded(true); + + // This device + if (mMyDevicePreference == null) { + mMyDevicePreference = new Preference(getActivity()); + } + if (mLocalAdapter != null) { + mMyDevicePreference.setTitle(mLocalAdapter.getName()); + } + mMyDevicePreference.setEnabled(true); + preferenceScreen.addPreference(mMyDevicePreference); + + // Paired devices category + if (mPairedDevicesCategory == null) { + mPairedDevicesCategory = new PreferenceCategory(getActivity()); + } else { + mPairedDevicesCategory.removeAll(); + } + addDeviceCategory(mPairedDevicesCategory, + R.string.bluetooth_preference_paired_devices, + BluetoothDeviceFilter.BONDED_DEVICE_FILTER); + int numberOfPairedDevices = mPairedDevicesCategory.getPreferenceCount(); + + // Available devices category + if (mAvailableDevicesCategory == null) { + mAvailableDevicesCategory = new ProgressCategory(getActivity(), null); + } else { + mAvailableDevicesCategory.removeAll(); + } + addDeviceCategory(mAvailableDevicesCategory, + R.string.bluetooth_preference_found_devices, + BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER); + int numberOfAvailableDevices = mAvailableDevicesCategory.getPreferenceCount(); + mAvailableDevicesCategoryIsPresent = true; + + if (numberOfAvailableDevices == 0) { + preferenceScreen.removePreference(mAvailableDevicesCategory); + mAvailableDevicesCategoryIsPresent = false; + } + + if (numberOfPairedDevices == 0) { + preferenceScreen.removePreference(mPairedDevicesCategory); + startScanning(); + } + return; // not break + + case BluetoothAdapter.STATE_TURNING_OFF: + int preferenceCount = preferenceScreen.getPreferenceCount(); + for (int i = 0; i < preferenceCount; i++) { + preferenceScreen.getPreference(i).setEnabled(false); + } + return; // not break + + 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; + } + + setDeviceListGroup(preferenceScreen); + removeAllDevices(); + mEmptyView.setText(messageId); + } + + @Override + public void onBluetoothStateChanged(int bluetoothState) { + super.onBluetoothStateChanged(bluetoothState); + updateContent(bluetoothState); } - private final View.OnClickListener mListener = new View.OnClickListener() { + @Override + public void onScanningStateChanged(boolean started) { + super.onScanningStateChanged(started); + // Update options' enabled state + getActivity().invalidateOptionsMenu(); + } + + public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) { + setDeviceListGroup(getPreferenceScreen()); + removeAllDevices(); + updateContent(mLocalAdapter.getBluetoothState()); + } + + private final View.OnClickListener mDeviceProfilesListener = 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); + Log.w(TAG, "onClick() called for other View: " + v); // TODO remove } } }; - @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; - } + private Preference mMyDevicePreference; - return super.onPreferenceTreeClick(preferenceScreen, preference); - } - - 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) { - createDevicePreference(cachedDevice); - } - } else if (bondState == BluetoothDevice.BOND_NONE) { - // remove unpaired device from paired devices list - onDeviceDeleted(cachedDevice); - } - } + private PreferenceGroup mPairedDevicesCategory; /** * Add a listener, which enables the advanced settings icon. @@ -143,6 +301,10 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment { */ @Override void initDevicePreference(BluetoothDevicePreference preference) { - preference.setOnSettingsClickListener(mListener); + CachedBluetoothDevice cachedDevice = preference.getCachedDevice(); + if (cachedDevice.getBondState() == BluetoothDevice.BOND_BONDED) { + // Only paired device have an associated advanced settings screen + preference.setOnSettingsClickListener(mDeviceProfilesListener); + } } } |