Bluetooth: remove unnecessary state tracking in BluetoothSummaryUpdater

* LocalBluetoothAdapter is already a cache of BluetoothAdapter and its
  methods should be used directly to obtain states instead of caching them
  in BluetoothSummaryUpdater
    - Use LocalBluetoothAdapter.isEnabled() to check whether Bluetooth
      is enabled
    - Use LocalBluetoothAdapter.getBondedDevices() to get list of bonded
      devices
* BluetoothDevice is a stable Bluetooth API and its methods should not
  incur large latency. We should use API methods as much as possible to
  avoid intermediate wrappers
    - Use BluetoothDevice.isConnected() to check if a device is connected
* Add more logging messages in error conditions
* Show status as "Not Connected" when there is a state mismatch (i.e.
  adapter says it is connected, but no bonded device is connected)
* Updated unit tests to reflect the latest behavior

Bug: 65591907
Test: make, unit test, pair with Bluetooth devices, check Settings UI
Change-Id: I0fa54959c8bed8ac67a935f150785ba8197d0411
This commit is contained in:
Jack He
2017-09-12 16:26:06 -07:00
parent b9da1117d0
commit ac040e3b1f
2 changed files with 124 additions and 123 deletions

View File

@@ -42,31 +42,21 @@ public final class BluetoothSummaryUpdater extends SummaryUpdater implements Blu
private final LocalBluetoothManager mBluetoothManager;
private final LocalBluetoothAdapter mBluetoothAdapter;
private boolean mEnabled;
private int mConnectionState;
public BluetoothSummaryUpdater(Context context, OnSummaryChangeListener listener,
LocalBluetoothManager bluetoothManager) {
super(context, listener);
mBluetoothManager = bluetoothManager;
mBluetoothAdapter = mBluetoothManager != null
? mBluetoothManager.getBluetoothAdapter() : null;
? mBluetoothManager.getBluetoothAdapter() : null;
}
@Override
public void onBluetoothStateChanged(int bluetoothState) {
mEnabled = bluetoothState == BluetoothAdapter.STATE_ON
|| bluetoothState == BluetoothAdapter.STATE_TURNING_ON;
if (!mEnabled) {
mConnectionState = BluetoothAdapter.STATE_DISCONNECTED;
}
notifyChangeIfNeeded();
}
@Override
public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) {
mConnectionState = state;
updateConnected();
notifyChangeIfNeeded();
}
@@ -92,8 +82,6 @@ public final class BluetoothSummaryUpdater extends SummaryUpdater implements Blu
return;
}
if (listening) {
mEnabled = mBluetoothAdapter.isEnabled();
mConnectionState = mBluetoothAdapter.getConnectionState();
notifyChangeIfNeeded();
mBluetoothManager.getEventManager().registerCallback(this);
} else {
@@ -103,10 +91,10 @@ public final class BluetoothSummaryUpdater extends SummaryUpdater implements Blu
@Override
public String getSummary() {
if (!mEnabled) {
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
return mContext.getString(R.string.bluetooth_disabled);
}
switch (mConnectionState) {
switch (mBluetoothAdapter.getConnectionState()) {
case BluetoothAdapter.STATE_CONNECTED:
return getConnectedDeviceSummary();
case BluetoothAdapter.STATE_CONNECTING:
@@ -118,50 +106,17 @@ public final class BluetoothSummaryUpdater extends SummaryUpdater implements Blu
}
}
private void updateConnected() {
if (mBluetoothAdapter == null) {
return;
}
// Make sure our connection state is up to date.
int state = mBluetoothAdapter.getConnectionState();
if (state != mConnectionState) {
mConnectionState = state;
return;
}
final Collection<CachedBluetoothDevice> devices = getDevices();
if (devices == null) {
mConnectionState = BluetoothAdapter.STATE_DISCONNECTED;
return;
}
if (mConnectionState == BluetoothAdapter.STATE_CONNECTED) {
CachedBluetoothDevice connectedDevice = null;
for (CachedBluetoothDevice device : devices) {
if (device.isConnected()) {
connectedDevice = device;
break;
}
}
if (connectedDevice == null) {
// If somehow we think we are connected, but have no connected devices, we
// aren't connected.
mConnectionState = BluetoothAdapter.STATE_DISCONNECTED;
}
}
}
private Collection<CachedBluetoothDevice> getDevices() {
return mBluetoothManager != null
? mBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy()
: null;
}
@VisibleForTesting
String getConnectedDeviceSummary() {
String deviceName = null;
int count = 0;
final Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();
if (devices == null || devices.isEmpty()) {
return null;
if (devices == null) {
Log.e(TAG, "getConnectedDeviceSummary, bonded devices are null");
return mContext.getString(R.string.bluetooth_disabled);
} else if (devices.isEmpty()) {
Log.e(TAG, "getConnectedDeviceSummary, no bonded devices");
return mContext.getString(R.string.disconnected);
}
for (BluetoothDevice device : devices) {
if (device.isConnected()) {
@@ -173,12 +128,13 @@ public final class BluetoothSummaryUpdater extends SummaryUpdater implements Blu
}
}
if (deviceName == null) {
Log.w(TAG, "getConnectedDeviceSummary, deviceName is null, numBondedDevices="
Log.e(TAG, "getConnectedDeviceSummary, deviceName is null, numBondedDevices="
+ devices.size());
for (BluetoothDevice device : devices) {
Log.w(TAG, "getConnectedDeviceSummary, device=" + device.getName() + "["
Log.e(TAG, "getConnectedDeviceSummary, device=" + device.getName() + "["
+ device.getAddress() + "]" + ", isConnected=" + device.isConnected());
}
return mContext.getString(R.string.disconnected);
}
return count > 1 ? mContext.getString(R.string.bluetooth_connected_multiple_devices_summary)
: mContext.getString(R.string.bluetooth_connected_summary, deviceName);