diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothController.java index 3429e25abfc75..aab35096d8a6b 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothController.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothController.java @@ -22,7 +22,6 @@ import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.systemui.Dumpable; import com.android.systemui.statusbar.policy.BluetoothController.Callback; -import java.util.Collection; import java.util.List; import java.util.concurrent.Executor; @@ -38,13 +37,9 @@ public interface BluetoothController extends CallbackController, Dumpa boolean isBluetoothAudioActive(); String getConnectedDeviceName(); void setBluetoothEnabled(boolean enabled); - Collection getDevices(); - void connect(CachedBluetoothDevice device); - void disconnect(CachedBluetoothDevice device); + boolean canConfigBluetooth(); - int getMaxConnectionState(CachedBluetoothDevice device); - int getBondState(CachedBluetoothDevice device); List getConnectedDevices(); void addOnMetadataChangedListener(CachedBluetoothDevice device, Executor executor, 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 c804fe76d8821..5208064d2c0bb 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothControllerImpl.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothControllerImpl.java @@ -18,7 +18,6 @@ package com.android.systemui.statusbar.policy; import android.annotation.Nullable; import android.bluetooth.BluetoothAdapter; -import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothProfile; import android.content.Context; import android.os.Handler; @@ -37,17 +36,15 @@ import com.android.settingslib.bluetooth.LocalBluetoothProfile; import com.android.settingslib.bluetooth.LocalBluetoothProfileManager; import com.android.systemui.bluetooth.BluetoothLogger; import com.android.systemui.dagger.SysUISingleton; -import com.android.systemui.dagger.qualifiers.Background; import com.android.systemui.dagger.qualifiers.Main; import com.android.systemui.dump.DumpManager; import com.android.systemui.settings.UserTracker; import java.io.PrintWriter; -import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.List; -import java.util.WeakHashMap; import java.util.concurrent.Executor; import javax.inject.Inject; @@ -64,9 +61,6 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa private final LocalBluetoothManager mLocalBluetoothManager; private final UserManager mUserManager; private final int mCurrentUser; - private final WeakHashMap mCachedState = - new WeakHashMap<>(); - private final Handler mBgHandler; @GuardedBy("mConnectedDevices") private final List mConnectedDevices = new ArrayList<>(); @@ -88,14 +82,12 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa UserTracker userTracker, DumpManager dumpManager, BluetoothLogger logger, - @Background Looper bgLooper, @Main Looper mainLooper, @Nullable LocalBluetoothManager localBluetoothManager, @Nullable BluetoothAdapter bluetoothAdapter) { mDumpManager = dumpManager; mLogger = logger; mLocalBluetoothManager = localBluetoothManager; - mBgHandler = new Handler(bgLooper); mHandler = new H(mainLooper); if (mLocalBluetoothManager != null) { mLocalBluetoothManager.getEventManager().registerCallback(this); @@ -151,7 +143,6 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa private String getDeviceString(CachedBluetoothDevice device) { return device.getName() - + " bondState=" + device.getBondState() + " connected=" + device.isConnected() + " active[A2DP]=" + device.isActiveDevice(BluetoothProfile.A2DP) + " active[HEADSET]=" + device.isActiveDevice(BluetoothProfile.HEADSET) @@ -159,11 +150,6 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa + " active[LE_AUDIO]=" + device.isActiveDevice(BluetoothProfile.LE_AUDIO); } - @Override - public int getBondState(CachedBluetoothDevice device) { - return getCachedState(device).mBondState; - } - @Override public List getConnectedDevices() { List out; @@ -173,11 +159,6 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa return out; } - @Override - public int getMaxConnectionState(CachedBluetoothDevice device) { - return getCachedState(device).mMaxConnectionState; - } - @Override public void addCallback(@NonNull Callback cb) { mHandler.obtainMessage(H.MSG_ADD_CALLBACK, cb).sendToTarget(); @@ -231,18 +212,6 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa return mLocalBluetoothManager != null; } - @Override - public void connect(final CachedBluetoothDevice device) { - if (mLocalBluetoothManager == null || device == null) return; - device.connect(true); - } - - @Override - public void disconnect(CachedBluetoothDevice device) { - if (mLocalBluetoothManager == null || device == null) return; - device.disconnect(); - } - @Override public String getConnectedDeviceName() { synchronized (mConnectedDevices) { @@ -253,11 +222,10 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa return null; } - @Override - public Collection getDevices() { + private Collection getDevices() { return mLocalBluetoothManager != null ? mLocalBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy() - : null; + : Collections.emptyList(); } private void updateConnected() { @@ -354,7 +322,6 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa @Override public void onDeviceDeleted(@NonNull CachedBluetoothDevice cachedDevice) { mLogger.logDeviceDeleted(cachedDevice.getAddress()); - mCachedState.remove(cachedDevice); updateConnected(); mHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED); } @@ -363,7 +330,6 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa public void onDeviceBondStateChanged( @NonNull CachedBluetoothDevice cachedDevice, int bondState) { mLogger.logBondStateChange(cachedDevice.getAddress(), bondState); - mCachedState.remove(cachedDevice); updateConnected(); mHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED); } @@ -381,7 +347,6 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa @ConnectionState int state) { mLogger.logDeviceConnectionStateChanged( getAddressOrNull(cachedDevice), connectionStateToString(state)); - mCachedState.remove(cachedDevice); updateConnected(); mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED); } @@ -393,7 +358,6 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa int bluetoothProfile) { mLogger.logProfileConnectionStateChanged( cachedDevice.getAddress(), connectionStateToString(state), bluetoothProfile); - mCachedState.remove(cachedDevice); updateConnected(); mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED); } @@ -411,7 +375,6 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa @NonNull CachedBluetoothDevice cachedDevice, int state) { mLogger.logAclConnectionStateChanged( cachedDevice.getAddress(), connectionStateToString(state)); - mCachedState.remove(cachedDevice); updateConnected(); mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED); } @@ -440,17 +403,6 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa ); } - private ActuallyCachedState getCachedState(CachedBluetoothDevice device) { - ActuallyCachedState state = mCachedState.get(device); - if (state == null) { - state = new ActuallyCachedState(device, mHandler); - mBgHandler.post(state); - mCachedState.put(device, state); - return state; - } - return state; - } - @Nullable private String getAddressOrNull(@Nullable CachedBluetoothDevice device) { return device == null ? null : device.getAddress(); @@ -465,30 +417,6 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa @Override public void onServiceDisconnected() {} - private static class ActuallyCachedState implements Runnable { - - private final WeakReference mDevice; - private final Handler mUiHandler; - private int mBondState = BluetoothDevice.BOND_NONE; - private int mMaxConnectionState = BluetoothProfile.STATE_DISCONNECTED; - - private ActuallyCachedState(CachedBluetoothDevice device, Handler uiHandler) { - mDevice = new WeakReference<>(device); - mUiHandler = uiHandler; - } - - @Override - public void run() { - CachedBluetoothDevice device = mDevice.get(); - if (device != null) { - mBondState = device.getBondState(); - mMaxConnectionState = device.getMaxConnectionState(); - mUiHandler.removeMessages(H.MSG_PAIRED_DEVICES_CHANGED); - mUiHandler.sendEmptyMessage(H.MSG_PAIRED_DEVICES_CHANGED); - } - } - } - private final class H extends Handler { private final ArrayList mCallbacks = new ArrayList<>(); diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/BluetoothControllerImplTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/BluetoothControllerImplTest.java index 7d64eaff0845a..2b1370544bfd0 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/BluetoothControllerImplTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/BluetoothControllerImplTest.java @@ -94,7 +94,6 @@ public class BluetoothControllerImplTest extends SysuiTestCase { mMockDumpManager, mock(BluetoothLogger.class), mTestableLooper.getLooper(), - mTestableLooper.getLooper(), mMockBluetoothManager, mMockAdapter); } @@ -113,62 +112,6 @@ public class BluetoothControllerImplTest extends SysuiTestCase { assertTrue(mBluetoothControllerImpl.isBluetoothConnected()); } - @Test - public void testDefaultConnectionState() { - CachedBluetoothDevice device = mock(CachedBluetoothDevice.class); - assertEquals(BluetoothDevice.BOND_NONE, mBluetoothControllerImpl.getBondState(device)); - assertEquals(BluetoothProfile.STATE_DISCONNECTED, - mBluetoothControllerImpl.getMaxConnectionState(device)); - } - - @Test - public void testAsyncBondState() throws Exception { - CachedBluetoothDevice device = mock(CachedBluetoothDevice.class); - when(device.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); - BluetoothController.Callback callback = mock(BluetoothController.Callback.class); - mBluetoothControllerImpl.addCallback(callback); - - // Trigger the state getting. - assertEquals(BluetoothDevice.BOND_NONE, mBluetoothControllerImpl.getBondState(device)); - - mTestableLooper.processAllMessages(); - - assertEquals(BluetoothDevice.BOND_BONDED, mBluetoothControllerImpl.getBondState(device)); - verify(callback).onBluetoothDevicesChanged(); - } - - @Test - public void testAsyncConnectionState() throws Exception { - CachedBluetoothDevice device = mock(CachedBluetoothDevice.class); - when(device.getMaxConnectionState()).thenReturn(BluetoothProfile.STATE_CONNECTED); - BluetoothController.Callback callback = mock(BluetoothController.Callback.class); - mBluetoothControllerImpl.addCallback(callback); - - // Trigger the state getting. - assertEquals(BluetoothProfile.STATE_DISCONNECTED, - mBluetoothControllerImpl.getMaxConnectionState(device)); - - mTestableLooper.processAllMessages(); - - assertEquals(BluetoothProfile.STATE_CONNECTED, - mBluetoothControllerImpl.getMaxConnectionState(device)); - verify(callback).onBluetoothDevicesChanged(); - } - - @Test - public void testNullAsync_DoesNotCrash() throws Exception { - CachedBluetoothDevice device = mock(CachedBluetoothDevice.class); - when(device.getMaxConnectionState()).thenReturn(BluetoothProfile.STATE_CONNECTED); - BluetoothController.Callback callback = mock(BluetoothController.Callback.class); - mBluetoothControllerImpl.addCallback(callback); - - // Trigger the state getting. - assertEquals(BluetoothProfile.STATE_DISCONNECTED, - mBluetoothControllerImpl.getMaxConnectionState(null)); - - mTestableLooper.processAllMessages(); - } - @Test public void testOnServiceConnected_updatesConnectionState() { when(mMockLocalAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_CONNECTING); diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/utils/leaks/FakeBluetoothController.java b/packages/SystemUI/tests/utils/src/com/android/systemui/utils/leaks/FakeBluetoothController.java index 4025ade5f7152..e685d4fb815da 100644 --- a/packages/SystemUI/tests/utils/src/com/android/systemui/utils/leaks/FakeBluetoothController.java +++ b/packages/SystemUI/tests/utils/src/com/android/systemui/utils/leaks/FakeBluetoothController.java @@ -21,7 +21,6 @@ import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.systemui.statusbar.policy.BluetoothController; import com.android.systemui.statusbar.policy.BluetoothController.Callback; -import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.concurrent.Executor; @@ -78,36 +77,11 @@ public class FakeBluetoothController extends BaseLeakChecker implement } - @Override - public Collection getDevices() { - return null; - } - - @Override - public void connect(CachedBluetoothDevice device) { - - } - - @Override - public void disconnect(CachedBluetoothDevice device) { - - } - @Override public boolean canConfigBluetooth() { return false; } - @Override - public int getMaxConnectionState(CachedBluetoothDevice device) { - return 0; - } - - @Override - public int getBondState(CachedBluetoothDevice device) { - return 0; - } - @Override public List getConnectedDevices() { return Collections.emptyList();