From db86384daea81d624aa645a0aa13dbdac6f8a514 Mon Sep 17 00:00:00 2001 From: Caitlin Shkuratov Date: Fri, 28 Apr 2023 17:58:25 +0000 Subject: [PATCH 1/2] [Bluetooth] Remove unused methods from BluetoothController interface. Removes 5 un-called methods from BluetoothController and any now-dead code associated with them. Bug: 271058380 Test: compiles Test: atest BluetoothControllerImplTest Change-Id: I006ca18d4b9fe5665206ca732db17b3ac7a10589 --- .../statusbar/policy/BluetoothController.java | 7 +- .../policy/BluetoothControllerImpl.java | 78 +------------------ .../policy/BluetoothControllerImplTest.java | 57 -------------- .../utils/leaks/FakeBluetoothController.java | 26 ------- 4 files changed, 4 insertions(+), 164 deletions(-) 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(); From bef50c9e8b12dd3ba00bf71a96f37ead2b4ac61e Mon Sep 17 00:00:00 2001 From: Caitlin Shkuratov Date: Tue, 2 May 2023 17:37:54 +0000 Subject: [PATCH 2/2] [Bluetooth] Add NEW_BLUETOOTH_REPOSITORY flag. To be used in an upcoming CL. Bug: 271058380 Bug: 280426085 Test: compiles Change-Id: I602b1fa82039ce6c981aa1acde2cb1fe765b2a78 --- packages/SystemUI/src/com/android/systemui/flags/Flags.kt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/SystemUI/src/com/android/systemui/flags/Flags.kt b/packages/SystemUI/src/com/android/systemui/flags/Flags.kt index 6ca409f07f6fc..85503aaebfbbf 100644 --- a/packages/SystemUI/src/com/android/systemui/flags/Flags.kt +++ b/packages/SystemUI/src/com/android/systemui/flags/Flags.kt @@ -330,6 +330,11 @@ object Flags { // TODO(b/265892345): Tracking Bug val PLUG_IN_STATUS_BAR_CHIP = releasedFlag(265892345, "plug_in_status_bar_chip") + // TODO(b/280426085): Tracking Bug + @JvmField + val NEW_BLUETOOTH_REPOSITORY = + unreleasedFlag(612, "new_bluetooth_repository", teamfood = true) + // 700 - dialer/calls // TODO(b/254512734): Tracking Bug val ONGOING_CALL_STATUS_BAR_CHIP = releasedFlag(700, "ongoing_call_status_bar_chip")