Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e093a979 authored by Caitlin Shkuratov's avatar Caitlin Shkuratov
Browse files

[Misc] Convert CastDevice to a Kotlin data class.

For the screen sharing chips implementation (b/332662551), we need to
add a new interactor to listen to CastController, and it'll be slightly
nicer to add that interactor on top of a Kotlin data class.

Also adds:
1) CastDevice#isCasting method instead of re-implementing that logic in
   many places
2) CastDevice.origin, which will be needed for the screen sharing chips

Bug: 332662551
Flag: NONE refactor
Test: Use Cast tile to cast to another device, then stop that cast
Test: Start sharing your screen to an app, and verify the Cast tile
shows cast as active
Test: atest CastAutoAddableTest CastTileTest CastControllerImplTest

Change-Id: Iaa4a15a6938098b0ce359946558381e45166ec7c
parent ab3d4836
Loading
Loading
Loading
Loading
+31 −11
Original line number Original line Diff line number Diff line
@@ -25,6 +25,7 @@ import com.android.systemui.qs.pipeline.domain.model.AutoAddSignal
import com.android.systemui.qs.pipeline.shared.TileSpec
import com.android.systemui.qs.pipeline.shared.TileSpec
import com.android.systemui.qs.tiles.CastTile
import com.android.systemui.qs.tiles.CastTile
import com.android.systemui.statusbar.policy.CastController
import com.android.systemui.statusbar.policy.CastController
import com.android.systemui.statusbar.policy.CastDevice
import com.android.systemui.util.mockito.capture
import com.android.systemui.util.mockito.capture
import com.android.systemui.util.mockito.whenever
import com.android.systemui.util.mockito.whenever
import com.google.common.truth.Truth.assertThat
import com.google.common.truth.Truth.assertThat
@@ -73,9 +74,12 @@ class CastAutoAddableTest : SysuiTestCase() {
    @Test
    @Test
    fun onCastDevicesChanged_deviceNotConnectedOrConnecting_noSignal() = runTest {
    fun onCastDevicesChanged_deviceNotConnectedOrConnecting_noSignal() = runTest {
        val device =
        val device =
            CastController.CastDevice().apply {
            CastDevice(
                state = CastController.CastDevice.STATE_DISCONNECTED
                id = "id",
            }
                name = null,
                state = CastDevice.CastState.Disconnected,
                origin = CastDevice.CastOrigin.MediaProjection,
            )
        whenever(castController.castDevices).thenReturn(listOf(device))
        whenever(castController.castDevices).thenReturn(listOf(device))


        val signal by collectLastValue(underTest.autoAddSignal(0))
        val signal by collectLastValue(underTest.autoAddSignal(0))
@@ -91,11 +95,19 @@ class CastAutoAddableTest : SysuiTestCase() {
    @Test
    @Test
    fun onCastDevicesChanged_someDeviceConnecting_addSignal() = runTest {
    fun onCastDevicesChanged_someDeviceConnecting_addSignal() = runTest {
        val disconnectedDevice =
        val disconnectedDevice =
            CastController.CastDevice().apply {
            CastDevice(
                state = CastController.CastDevice.STATE_DISCONNECTED
                id = "id",
            }
                name = null,
                state = CastDevice.CastState.Disconnected,
                origin = CastDevice.CastOrigin.MediaProjection,
            )
        val connectingDevice =
        val connectingDevice =
            CastController.CastDevice().apply { state = CastController.CastDevice.STATE_CONNECTING }
            CastDevice(
                id = "id",
                name = null,
                state = CastDevice.CastState.Connecting,
                origin = CastDevice.CastOrigin.MediaProjection,
            )
        whenever(castController.castDevices)
        whenever(castController.castDevices)
            .thenReturn(listOf(disconnectedDevice, connectingDevice))
            .thenReturn(listOf(disconnectedDevice, connectingDevice))


@@ -112,11 +124,19 @@ class CastAutoAddableTest : SysuiTestCase() {
    @Test
    @Test
    fun onCastDevicesChanged_someDeviceConnected_addSignal() = runTest {
    fun onCastDevicesChanged_someDeviceConnected_addSignal() = runTest {
        val disconnectedDevice =
        val disconnectedDevice =
            CastController.CastDevice().apply {
            CastDevice(
                state = CastController.CastDevice.STATE_DISCONNECTED
                id = "id",
            }
                name = null,
                state = CastDevice.CastState.Disconnected,
                origin = CastDevice.CastOrigin.MediaProjection,
            )
        val connectedDevice =
        val connectedDevice =
            CastController.CastDevice().apply { state = CastController.CastDevice.STATE_CONNECTED }
            CastDevice(
                id = "id",
                name = null,
                state = CastDevice.CastState.Connected,
                origin = CastDevice.CastOrigin.MediaProjection,
            )
        whenever(castController.castDevices).thenReturn(listOf(disconnectedDevice, connectedDevice))
        whenever(castController.castDevices).thenReturn(listOf(disconnectedDevice, connectedDevice))


        val signal by collectLastValue(underTest.autoAddSignal(0))
        val signal by collectLastValue(underTest.autoAddSignal(0))
+1 −6
Original line number Original line Diff line number Diff line
@@ -41,12 +41,7 @@ constructor(


    override fun ProducerScope<AutoAddSignal>.getCallback(): CastController.Callback {
    override fun ProducerScope<AutoAddSignal>.getCallback(): CastController.Callback {
        return CastController.Callback {
        return CastController.Callback {
            val isCasting =
            if (controller.castDevices.any { it.isCasting }) {
                controller.castDevices.any {
                    it.state == CastController.CastDevice.STATE_CONNECTED ||
                        it.state == CastController.CastDevice.STATE_CONNECTING
                }
            if (isCasting) {
                sendAdd()
                sendAdd()
            }
            }
        }
        }
+6 −7
Original line number Original line Diff line number Diff line
@@ -60,7 +60,7 @@ import com.android.systemui.statusbar.phone.SystemUIDialog;
import com.android.systemui.statusbar.pipeline.shared.data.model.DefaultConnectionModel;
import com.android.systemui.statusbar.pipeline.shared.data.model.DefaultConnectionModel;
import com.android.systemui.statusbar.pipeline.shared.data.repository.ConnectivityRepository;
import com.android.systemui.statusbar.pipeline.shared.data.repository.ConnectivityRepository;
import com.android.systemui.statusbar.policy.CastController;
import com.android.systemui.statusbar.policy.CastController;
import com.android.systemui.statusbar.policy.CastController.CastDevice;
import com.android.systemui.statusbar.policy.CastDevice;
import com.android.systemui.statusbar.policy.HotspotController;
import com.android.systemui.statusbar.policy.HotspotController;
import com.android.systemui.statusbar.policy.KeyguardStateController;
import com.android.systemui.statusbar.policy.KeyguardStateController;
import com.android.systemui.util.DialogKt;
import com.android.systemui.util.DialogKt;
@@ -193,14 +193,13 @@ public class CastTile extends QSTileImpl<BooleanState> {
    // case where multiple devices were active :-/.
    // case where multiple devices were active :-/.
    private boolean willPopDialog() {
    private boolean willPopDialog() {
        List<CastDevice> activeDevices = getActiveDevices();
        List<CastDevice> activeDevices = getActiveDevices();
        return activeDevices.isEmpty() || (activeDevices.get(0).tag instanceof RouteInfo);
        return activeDevices.isEmpty() || (activeDevices.get(0).getTag() instanceof RouteInfo);
    }
    }


    private List<CastDevice> getActiveDevices() {
    private List<CastDevice> getActiveDevices() {
        ArrayList<CastDevice> activeDevices = new ArrayList<>();
        ArrayList<CastDevice> activeDevices = new ArrayList<>();
        for (CastDevice device : mController.getCastDevices()) {
        for (CastDevice device : mController.getCastDevices()) {
            if (device.state == CastDevice.STATE_CONNECTED
            if (device.isCasting()) {
                    || device.state == CastDevice.STATE_CONNECTING) {
                activeDevices.add(device);
                activeDevices.add(device);
            }
            }
        }
        }
@@ -276,7 +275,7 @@ public class CastTile extends QSTileImpl<BooleanState> {
        // We always choose the first device that's in the CONNECTED state in the case where
        // We always choose the first device that's in the CONNECTED state in the case where
        // multiple devices are CONNECTED at the same time.
        // multiple devices are CONNECTED at the same time.
        for (CastDevice device : devices) {
        for (CastDevice device : devices) {
            if (device.state == CastDevice.STATE_CONNECTED) {
            if (device.getState() == CastDevice.CastState.Connected) {
                state.value = true;
                state.value = true;
                state.secondaryLabel = getDeviceName(device);
                state.secondaryLabel = getDeviceName(device);
                state.stateDescription = state.stateDescription + ","
                state.stateDescription = state.stateDescription + ","
@@ -284,7 +283,7 @@ public class CastTile extends QSTileImpl<BooleanState> {
                        R.string.accessibility_cast_name, state.label);
                        R.string.accessibility_cast_name, state.label);
                connecting = false;
                connecting = false;
                break;
                break;
            } else if (device.state == CastDevice.STATE_CONNECTING) {
            } else if (device.getState() == CastDevice.CastState.Connecting) {
                connecting = true;
                connecting = true;
            }
            }
        }
        }
@@ -315,7 +314,7 @@ public class CastTile extends QSTileImpl<BooleanState> {
    }
    }


    private String getDeviceName(CastDevice device) {
    private String getDeviceName(CastDevice device) {
        return device.name != null ? device.name
        return device.getName() != null ? device.getName()
                : mContext.getString(R.string.quick_settings_cast_device_default_name);
                : mContext.getString(R.string.quick_settings_cast_device_default_name);
    }
    }


+2 −3
Original line number Original line Diff line number Diff line
@@ -39,7 +39,7 @@ import com.android.systemui.qs.external.CustomTile;
import com.android.systemui.qs.pipeline.shared.QSPipelineFlagsRepository;
import com.android.systemui.qs.pipeline.shared.QSPipelineFlagsRepository;
import com.android.systemui.res.R;
import com.android.systemui.res.R;
import com.android.systemui.statusbar.policy.CastController;
import com.android.systemui.statusbar.policy.CastController;
import com.android.systemui.statusbar.policy.CastController.CastDevice;
import com.android.systemui.statusbar.policy.CastDevice;
import com.android.systemui.statusbar.policy.DataSaverController;
import com.android.systemui.statusbar.policy.DataSaverController;
import com.android.systemui.statusbar.policy.DataSaverController.Listener;
import com.android.systemui.statusbar.policy.DataSaverController.Listener;
import com.android.systemui.statusbar.policy.DeviceControlsController;
import com.android.systemui.statusbar.policy.DeviceControlsController;
@@ -430,8 +430,7 @@ public class AutoTileManager implements UserAwareController {


            boolean isCasting = false;
            boolean isCasting = false;
            for (CastDevice device : mCastController.getCastDevices()) {
            for (CastDevice device : mCastController.getCastDevices()) {
                if (device.state == CastDevice.STATE_CONNECTED
                if (device.isCasting()) {
                        || device.state == CastDevice.STATE_CONNECTING) {
                    isCasting = true;
                    isCasting = true;
                    break;
                    break;
                }
                }
+2 −3
Original line number Original line Diff line number Diff line
@@ -62,7 +62,7 @@ import com.android.systemui.statusbar.CommandQueue;
import com.android.systemui.statusbar.phone.ui.StatusBarIconController;
import com.android.systemui.statusbar.phone.ui.StatusBarIconController;
import com.android.systemui.statusbar.policy.BluetoothController;
import com.android.systemui.statusbar.policy.BluetoothController;
import com.android.systemui.statusbar.policy.CastController;
import com.android.systemui.statusbar.policy.CastController;
import com.android.systemui.statusbar.policy.CastController.CastDevice;
import com.android.systemui.statusbar.policy.CastDevice;
import com.android.systemui.statusbar.policy.DataSaverController;
import com.android.systemui.statusbar.policy.DataSaverController;
import com.android.systemui.statusbar.policy.DataSaverController.Listener;
import com.android.systemui.statusbar.policy.DataSaverController.Listener;
import com.android.systemui.statusbar.policy.DeviceProvisionedController;
import com.android.systemui.statusbar.policy.DeviceProvisionedController;
@@ -521,8 +521,7 @@ public class PhoneStatusBarPolicy
    private void updateCast() {
    private void updateCast() {
        boolean isCasting = false;
        boolean isCasting = false;
        for (CastDevice device : mCast.getCastDevices()) {
        for (CastDevice device : mCast.getCastDevices()) {
            if (device.state == CastDevice.STATE_CONNECTING
            if (device.isCasting()) {
                    || device.state == CastDevice.STATE_CONNECTED) {
                isCasting = true;
                isCasting = true;
                break;
                break;
            }
            }
Loading