Refactor Bluetooth settings for readability and performance.

Major refactoring of Bluetooth settings classes.
- Moved all functionality from LocalBluetoothManager into new
  LocalBluetoothAdapter and LocalBluetoothPreferences, and into
  existing classes.
- Refactored functionality from BluetoothEventRedirector into new
  BluetoothEventManager class, deleting the original version. New
  version uses a HashMap from action Strings to implementers of the
  BluetoothEventManager.Handler interface.
- Created new BluetoothDiscoveryReceiver to update shared preferences
  timestamp for Bluetooth discovery start/finish. This is the only event
  handling we need to do when the settings app is not visible, so it has
  its own receiver entry in AndroidManifest.xml. Edits are written using
  QueuedWork.singleThreadExecutor(), which BroadcastReceiver knows about
  and will wait for completion, eliminating the need for PendingResult.
- Miscellaneous cleanups to code style and logic for readability.
- Pulled some large switch statement code blocks into new methods.
- Changed all Bluetooth state references to the new BluetoothProfile
  constants.
- Changed use of deprecated Notification constructor in
  BluetoothPairingRequest to use Notification.Builder.
- Moved Utf8ByteLengthFilter helper function from BluetoothNamePreference
  into its own class, and moved test cases into the same package.
- Moved all LocalBluetoothProfileManager functionality related to
  specific profiles into new top-level classes (A2dpProfile, etc.), all
  implementing the LocalBluetoothProfile interface.
- Moved all UI-related methods from CachedBluetoothDevice into the class
  that uses the method, or into the static Utils class for shared methods.

Change-Id: I6d49b7f4ae0c7d7dcf62551ee40b51ecb5fe4f47
This commit is contained in:
Jake Hamby
2011-02-07 18:21:25 -08:00
parent 2adae4e274
commit 436b29e68e
44 changed files with 3536 additions and 3041 deletions

View File

@@ -16,41 +16,34 @@
package com.android.settings.bluetooth;
import com.android.settings.R;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.SystemProperties;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.CheckBoxPreference;
import android.provider.Settings;
import android.util.Log;
import com.android.settings.R;
/**
* BluetoothDiscoverableEnabler is a helper to manage the "Discoverable"
* checkbox. It sets/unsets discoverability and keeps track of how much time
* until the the discoverability is automatically turned off.
*/
public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChangeListener {
private static final String TAG = "BluetoothDiscoverableEnabler";
final class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChangeListener {
private static final String SYSTEM_PROPERTY_DISCOVERABLE_TIMEOUT =
"debug.bt.discoverable_time";
static final int DISCOVERABLE_TIMEOUT_TWO_MINUTES = 120;
static final int DISCOVERABLE_TIMEOUT_FIVE_MINUTES = 300;
static final int DISCOVERABLE_TIMEOUT_ONE_HOUR = 3600;
private static final int DISCOVERABLE_TIMEOUT_TWO_MINUTES = 120;
private static final int DISCOVERABLE_TIMEOUT_FIVE_MINUTES = 300;
private static final int DISCOVERABLE_TIMEOUT_ONE_HOUR = 3600;
static final int DISCOVERABLE_TIMEOUT_NEVER = 0;
static final String SHARED_PREFERENCES_KEY_DISCOVERABLE_END_TIMESTAMP =
"discoverable_end_timestamp";
private static final String VALUE_DISCOVERABLE_TIMEOUT_TWO_MINUTES = "twomin";
private static final String VALUE_DISCOVERABLE_TIMEOUT_FIVE_MINUTES = "fivemin";
private static final String VALUE_DISCOVERABLE_TIMEOUT_ONE_HOUR = "onehour";
@@ -63,7 +56,7 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan
private final CheckBoxPreference mCheckBoxPreference;
private final ListPreference mTimeoutListPreference;
private final LocalBluetoothManager mLocalManager;
private final LocalBluetoothAdapter mLocalAdapter;
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
@@ -84,7 +77,7 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan
}
};
public BluetoothDiscoverableEnabler(Context context,
BluetoothDiscoverableEnabler(Context context, LocalBluetoothAdapter adapter,
CheckBoxPreference checkBoxPreference, ListPreference timeoutListPreference) {
mContext = context;
mUiHandler = new Handler();
@@ -95,15 +88,15 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan
// we actually want to persist this since can't infer from BT device state
mTimeoutListPreference.setPersistent(true);
mLocalManager = LocalBluetoothManager.getInstance(context);
if (mLocalManager == null) {
mLocalAdapter = adapter;
if (adapter == null) {
// Bluetooth not supported
checkBoxPreference.setEnabled(false);
}
}
public void resume() {
if (mLocalManager == null) {
if (mLocalAdapter == null) {
return;
}
@@ -111,11 +104,11 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan
mContext.registerReceiver(mReceiver, filter);
mCheckBoxPreference.setOnPreferenceChangeListener(this);
mTimeoutListPreference.setOnPreferenceChangeListener(this);
handleModeChanged(mLocalManager.getBluetoothAdapter().getScanMode());
handleModeChanged(mLocalAdapter.getScanMode());
}
public void pause() {
if (mLocalManager == null) {
if (mLocalAdapter == null) {
return;
}
@@ -137,43 +130,37 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan
return true;
}
private void setEnabled(final boolean enable) {
BluetoothAdapter manager = mLocalManager.getBluetoothAdapter();
private void setEnabled(boolean enable) {
if (enable) {
int timeout = getDiscoverableTimeout();
manager.setDiscoverableTimeout(timeout);
mLocalAdapter.setDiscoverableTimeout(timeout);
long endTimestamp = System.currentTimeMillis() + timeout * 1000L;
persistDiscoverableEndTimestamp(endTimestamp);
LocalBluetoothPreferences.persistDiscoverableEndTimestamp(mContext, endTimestamp);
updateCountdownSummary();
manager.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, timeout);
mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, timeout);
} else {
manager.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
}
}
private void updateTimerDisplay(int timeout) {
if (getDiscoverableTimeout() == DISCOVERABLE_TIMEOUT_NEVER) {
mCheckBoxPreference.setSummaryOn(
mContext.getResources()
.getString(R.string.bluetooth_is_discoverable_always));
mContext.getString(R.string.bluetooth_is_discoverable_always));
} else {
mCheckBoxPreference.setSummaryOn(
mContext.getResources()
.getString(R.string.bluetooth_is_discoverable, timeout));
mContext.getString(R.string.bluetooth_is_discoverable, timeout));
}
}
private int getDiscoverableTimeout() {
int timeout = SystemProperties.getInt(SYSTEM_PROPERTY_DISCOVERABLE_TIMEOUT, -1);
if (timeout < 0) {
String timeoutValue = null;
if (mTimeoutListPreference != null && mTimeoutListPreference.getValue() != null) {
timeoutValue = mTimeoutListPreference.getValue().toString();
} else {
String timeoutValue = mTimeoutListPreference.getValue();
if (timeoutValue == null) {
mTimeoutListPreference.setValue(VALUE_DISCOVERABLE_TIMEOUT_TWO_MINUTES);
return DISCOVERABLE_TIMEOUT_TWO_MINUTES;
}
@@ -192,12 +179,6 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan
return timeout;
}
private void persistDiscoverableEndTimestamp(long endTimestamp) {
SharedPreferences.Editor editor = mLocalManager.getSharedPreferences().edit();
editor.putLong(SHARED_PREFERENCES_KEY_DISCOVERABLE_END_TIMESTAMP, endTimestamp);
editor.apply();
}
private void handleModeChanged(int mode) {
if (mode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
mCheckBoxPreference.setChecked(true);
@@ -208,14 +189,13 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan
}
private void updateCountdownSummary() {
int mode = mLocalManager.getBluetoothAdapter().getScanMode();
int mode = mLocalAdapter.getScanMode();
if (mode != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
return;
}
long currentTimestamp = System.currentTimeMillis();
long endTimestamp = mLocalManager.getSharedPreferences().getLong(
SHARED_PREFERENCES_KEY_DISCOVERABLE_END_TIMESTAMP, 0);
long endTimestamp = LocalBluetoothPreferences.getDiscoverableEndTimestamp(mContext);
if (currentTimestamp > endTimestamp) {
// We're still in discoverable mode, but maybe there isn't a timeout.
@@ -231,6 +211,4 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan
mUiHandler.postDelayed(mUpdateCountdownSummaryRunnable, 1000);
}
}
}