Fix QS/status bar BT state
The adapter state doesn't always seem to be reported correctly, so
if in doubt, calculate the adapter state based on the list of devices
we have.
Test: runtest systemui
Change-Id: I25f68665cdcb411e6976c9efc6b61612fe81302c
Fixes: 34598634
diff --git a/packages/SystemUI/src/com/android/systemui/Dependency.java b/packages/SystemUI/src/com/android/systemui/Dependency.java
index 4dfaf45..374086d 100644
--- a/packages/SystemUI/src/com/android/systemui/Dependency.java
+++ b/packages/SystemUI/src/com/android/systemui/Dependency.java
@@ -25,6 +25,7 @@
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.app.NightDisplayController;
import com.android.internal.util.Preconditions;
+import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.systemui.assist.AssistManager;
import com.android.systemui.fragments.FragmentService;
import com.android.systemui.plugins.ActivityStarter;
@@ -248,6 +249,9 @@
mProviders.put(PluginDependencyProvider.class, () ->
new PluginDependencyProvider(get(PluginManager.class)));
+ mProviders.put(LocalBluetoothManager.class, () ->
+ LocalBluetoothManager.getInstance(mContext, null));
+
// Put all dependencies above here so the factory can override them if it wants.
SystemUIFactory.getInstance().injectDependencies(mProviders, mContext);
}
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothControllerImpl.java
index 15c4afe..36d24b3 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothControllerImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothControllerImpl.java
@@ -29,6 +29,7 @@
import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
+import com.android.systemui.Dependency;
import java.io.FileDescriptor;
import java.io.PrintWriter;
@@ -52,7 +53,7 @@
private int mState;
public BluetoothControllerImpl(Context context, Looper bgLooper) {
- mLocalBluetoothManager = LocalBluetoothManager.getInstance(context, null);
+ mLocalBluetoothManager = Dependency.get(LocalBluetoothManager.class);
if (mLocalBluetoothManager != null) {
mLocalBluetoothManager.getEventManager().setReceiverHandler(new Handler(bgLooper));
mLocalBluetoothManager.getEventManager().registerCallback(this);
@@ -174,24 +175,30 @@
private void updateConnected() {
// Make sure our connection state is up to date.
int state = mLocalBluetoothManager.getBluetoothAdapter().getConnectionState();
- if (state != mConnectionState) {
- mConnectionState = state;
- mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED);
+ if (mLastDevice != null && !mLastDevice.isConnected()) {
+ // Clear out last device if no longer connected.
+ mLastDevice = null;
}
- if (mLastDevice != null && mLastDevice.isConnected()) {
- // Our current device is still valid.
- return;
- }
- mLastDevice = null;
+ // If any of the devices are in a higher state than the adapter, move the adapter into
+ // that state.
for (CachedBluetoothDevice device : getDevices()) {
- if (device.isConnected()) {
+ int maxDeviceState = device.getMaxConnectionState();
+ if (maxDeviceState > state) {
+ state = maxDeviceState;
+ }
+ if (mLastDevice == null && device.isConnected()) {
+ // Set as last connected device only if we don't have one.
mLastDevice = device;
}
}
- if (mLastDevice == null && mConnectionState == BluetoothAdapter.STATE_CONNECTED) {
+
+ if (mLastDevice == null && state == BluetoothAdapter.STATE_CONNECTED) {
// If somehow we think we are connected, but have no connected devices, we aren't
// connected.
- mConnectionState = BluetoothAdapter.STATE_DISCONNECTED;
+ state = BluetoothAdapter.STATE_DISCONNECTED;
+ }
+ if (state != mConnectionState) {
+ mConnectionState = state;
mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED);
}
}
@@ -238,7 +245,6 @@
public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) {
mLastDevice = cachedDevice;
updateConnected();
- mConnectionState = state;
mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED);
}