From babe2cd5f388ca34b87a9e18705e0924406fe862 Mon Sep 17 00:00:00 2001 From: Caitlin Cassidy Date: Thu, 13 Jan 2022 20:53:33 +0000 Subject: [PATCH] [Media TTT] Update chip states to not require otherDeviceName if they don't need it. Define a #getChipTextString method instead. Bug: 203800643 Bug: 203800347 Test: verify all states still trigger via adb Test: media.taptotransfer tests Change-Id: Iba8a67a0a8affc4857fafdaf1880b920db0a3fe1 --- .../taptotransfer/sender/ChipStateSender.kt | 88 +++++++++---------- .../sender/MediaTttChipControllerSender.kt | 2 +- .../sender/MediaTttSenderService.kt | 7 +- .../MediaTttChipControllerSenderTest.kt | 28 +++--- .../sender/MediaTttSenderServiceTest.kt | 6 +- 5 files changed, 66 insertions(+), 65 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/media/taptotransfer/sender/ChipStateSender.kt b/packages/SystemUI/src/com/android/systemui/media/taptotransfer/sender/ChipStateSender.kt index 1fd3af4ce8d94..0f2715c4a6e35 100644 --- a/packages/SystemUI/src/com/android/systemui/media/taptotransfer/sender/ChipStateSender.kt +++ b/packages/SystemUI/src/com/android/systemui/media/taptotransfer/sender/ChipStateSender.kt @@ -16,8 +16,8 @@ package com.android.systemui.media.taptotransfer.sender +import android.content.Context import android.graphics.drawable.Drawable -import androidx.annotation.StringRes import com.android.systemui.R import com.android.systemui.media.taptotransfer.common.MediaTttChipState @@ -27,91 +27,89 @@ import com.android.systemui.media.taptotransfer.common.MediaTttChipState * * This is a sealed class where each subclass represents a specific chip state. Each subclass can * contain additional information that is necessary for only that state. - * - * @property chipText a string resource for the text that the chip should display. - * @property otherDeviceName the name of the other device involved in the transfer. */ sealed class ChipStateSender( appIconDrawable: Drawable, - appIconContentDescription: String, - @StringRes internal val chipText: Int, - internal val otherDeviceName: String, -) : MediaTttChipState(appIconDrawable, appIconContentDescription) + appIconContentDescription: String +) : MediaTttChipState(appIconDrawable, appIconContentDescription) { + /** Returns a fully-formed string with the text that the chip should display. */ + abstract fun getChipTextString(context: Context): String +} /** * A state representing that the two devices are close but not close enough to *start* a cast to * the receiver device. The chip will instruct the user to move closer in order to initiate the * transfer to the receiver. + * + * @property otherDeviceName the name of the other device involved in the transfer. */ class MoveCloserToStartCast( appIconDrawable: Drawable, appIconContentDescription: String, - otherDeviceName: String, -) : ChipStateSender( - appIconDrawable, - appIconContentDescription, - R.string.media_move_closer_to_start_cast, - otherDeviceName -) + private val otherDeviceName: String, +) : ChipStateSender(appIconDrawable, appIconContentDescription) { + override fun getChipTextString(context: Context): String { + return context.getString(R.string.media_move_closer_to_start_cast, otherDeviceName) + } +} /** * A state representing that the two devices are close but not close enough to *end* a cast that's * currently occurring the receiver device. The chip will instruct the user to move closer in order * to initiate the transfer from the receiver and back onto this device (the original sender). + * + * @property otherDeviceName the name of the other device involved in the transfer. */ class MoveCloserToEndCast( appIconDrawable: Drawable, appIconContentDescription: String, - otherDeviceName: String, -) : ChipStateSender( - appIconDrawable, - appIconContentDescription, - R.string.media_move_closer_to_end_cast, - otherDeviceName -) + private val otherDeviceName: String, +) : ChipStateSender(appIconDrawable, appIconContentDescription) { + override fun getChipTextString(context: Context): String { + return context.getString(R.string.media_move_closer_to_end_cast, otherDeviceName) + } +} /** * A state representing that a transfer to the receiver device has been initiated (but not * completed). + * + * @property otherDeviceName the name of the other device involved in the transfer. */ class TransferToReceiverTriggered( appIconDrawable: Drawable, appIconContentDescription: String, - otherDeviceName: String -) : ChipStateSender( - appIconDrawable, - appIconContentDescription, - R.string.media_transfer_playing, - otherDeviceName -) + private val otherDeviceName: String +) : ChipStateSender(appIconDrawable, appIconContentDescription) { + override fun getChipTextString(context: Context): String { + return context.getString(R.string.media_transfer_playing, otherDeviceName) + } +} /** * A state representing that a transfer has been successfully completed. * + * @property otherDeviceName the name of the other device involved in the transfer. * @property undoRunnable if present, the runnable that should be run to undo the transfer. We will * show an Undo button on the chip if this runnable is present. */ class TransferSucceeded( appIconDrawable: Drawable, appIconContentDescription: String, - otherDeviceName: String, + private val otherDeviceName: String, val undoRunnable: Runnable? = null -) : ChipStateSender(appIconDrawable, - appIconContentDescription, - R.string.media_transfer_playing, - otherDeviceName -) +) : ChipStateSender(appIconDrawable, appIconContentDescription) { + override fun getChipTextString(context: Context): String { + return context.getString(R.string.media_transfer_playing, otherDeviceName) + } +} /** A state representing that a transfer has failed. */ class TransferFailed( appIconDrawable: Drawable, - appIconContentDescription: String, - // TODO(b/211493953): The failed chip doesn't need [otherDeviceName] so we may want to remove - // [otherDeviceName] from the superclass [ChipStateSender]. - otherDeviceName: String, -) : ChipStateSender( - appIconDrawable, - appIconContentDescription, - R.string.media_transfer_failed, - otherDeviceName -) + appIconContentDescription: String +) : ChipStateSender(appIconDrawable, appIconContentDescription) { + override fun getChipTextString(context: Context): String { + return context.getString(R.string.media_transfer_failed) + } +} diff --git a/packages/SystemUI/src/com/android/systemui/media/taptotransfer/sender/MediaTttChipControllerSender.kt b/packages/SystemUI/src/com/android/systemui/media/taptotransfer/sender/MediaTttChipControllerSender.kt index 84672ab9a31cc..88264b6e669ee 100644 --- a/packages/SystemUI/src/com/android/systemui/media/taptotransfer/sender/MediaTttChipControllerSender.kt +++ b/packages/SystemUI/src/com/android/systemui/media/taptotransfer/sender/MediaTttChipControllerSender.kt @@ -45,7 +45,7 @@ class MediaTttChipControllerSender @Inject constructor( // Text currentChipView.requireViewById(R.id.text).apply { - text = context.getString(chipState.chipText, chipState.otherDeviceName) + text = chipState.getChipTextString(context) } // Loading diff --git a/packages/SystemUI/src/com/android/systemui/media/taptotransfer/sender/MediaTttSenderService.kt b/packages/SystemUI/src/com/android/systemui/media/taptotransfer/sender/MediaTttSenderService.kt index 0fe324eb557df..cbf646209b007 100644 --- a/packages/SystemUI/src/com/android/systemui/media/taptotransfer/sender/MediaTttSenderService.kt +++ b/packages/SystemUI/src/com/android/systemui/media/taptotransfer/sender/MediaTttSenderService.kt @@ -53,7 +53,7 @@ class MediaTttSenderService @Inject constructor( override fun transferFailed( mediaInfo: MediaRoute2Info, otherDeviceInfo: DeviceInfo ) { - this@MediaTttSenderService.transferFailed(mediaInfo, otherDeviceInfo) + this@MediaTttSenderService.transferFailed(mediaInfo) } override fun transferToReceiverTriggered( @@ -91,11 +91,10 @@ class MediaTttSenderService @Inject constructor( controller.displayChip(chipState) } - private fun transferFailed(mediaInfo: MediaRoute2Info, otherDeviceInfo: DeviceInfo) { + private fun transferFailed(mediaInfo: MediaRoute2Info) { val chipState = TransferFailed( appIconDrawable = fakeAppIconDrawable, - appIconContentDescription = mediaInfo.name.toString(), - otherDeviceName = otherDeviceInfo.name + appIconContentDescription = mediaInfo.name.toString() ) controller.displayChip(chipState) } diff --git a/packages/SystemUI/tests/src/com/android/systemui/media/taptotransfer/sender/MediaTttChipControllerSenderTest.kt b/packages/SystemUI/tests/src/com/android/systemui/media/taptotransfer/sender/MediaTttChipControllerSenderTest.kt index e9ddf3d857b1c..b6c1834e68f1a 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/media/taptotransfer/sender/MediaTttChipControllerSenderTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/media/taptotransfer/sender/MediaTttChipControllerSenderTest.kt @@ -53,12 +53,13 @@ class MediaTttChipControllerSenderTest : SysuiTestCase() { @Test fun moveCloserToStartCast_appIcon_deviceName_noLoadingIcon_noUndo_noFailureIcon() { - controllerSender.displayChip(moveCloserToStartCast()) + val state = moveCloserToStartCast() + controllerSender.displayChip(state) val chipView = getChipView() assertThat(chipView.getAppIconView().drawable).isEqualTo(appIconDrawable) assertThat(chipView.getAppIconView().contentDescription).isEqualTo(APP_ICON_CONTENT_DESC) - assertThat(chipView.getChipText()).contains(DEVICE_NAME) + assertThat(chipView.getChipText()).isEqualTo(state.getChipTextString(context)) assertThat(chipView.getLoadingIconVisibility()).isEqualTo(View.GONE) assertThat(chipView.getUndoButton().visibility).isEqualTo(View.GONE) assertThat(chipView.getFailureIcon().visibility).isEqualTo(View.GONE) @@ -66,12 +67,13 @@ class MediaTttChipControllerSenderTest : SysuiTestCase() { @Test fun moveCloserToEndCast_appIcon_deviceName_noLoadingIcon_noUndo_noFailureIcon() { - controllerSender.displayChip(moveCloserToEndCast()) + val state = moveCloserToEndCast() + controllerSender.displayChip(state) val chipView = getChipView() assertThat(chipView.getAppIconView().drawable).isEqualTo(appIconDrawable) assertThat(chipView.getAppIconView().contentDescription).isEqualTo(APP_ICON_CONTENT_DESC) - assertThat(chipView.getChipText()).contains(DEVICE_NAME) + assertThat(chipView.getChipText()).isEqualTo(state.getChipTextString(context)) assertThat(chipView.getLoadingIconVisibility()).isEqualTo(View.GONE) assertThat(chipView.getUndoButton().visibility).isEqualTo(View.GONE) assertThat(chipView.getFailureIcon().visibility).isEqualTo(View.GONE) @@ -79,12 +81,13 @@ class MediaTttChipControllerSenderTest : SysuiTestCase() { @Test fun transferToReceiverTriggered_appIcon_loadingIcon_noUndo_noFailureIcon() { - controllerSender.displayChip(transferToReceiverTriggered()) + val state = transferToReceiverTriggered() + controllerSender.displayChip(state) val chipView = getChipView() assertThat(chipView.getAppIconView().drawable).isEqualTo(appIconDrawable) assertThat(chipView.getAppIconView().contentDescription).isEqualTo(APP_ICON_CONTENT_DESC) - assertThat(chipView.getChipText()).contains(DEVICE_NAME) + assertThat(chipView.getChipText()).isEqualTo(state.getChipTextString(context)) assertThat(chipView.getLoadingIconVisibility()).isEqualTo(View.VISIBLE) assertThat(chipView.getUndoButton().visibility).isEqualTo(View.GONE) assertThat(chipView.getFailureIcon().visibility).isEqualTo(View.GONE) @@ -92,12 +95,13 @@ class MediaTttChipControllerSenderTest : SysuiTestCase() { @Test fun transferSucceeded_appIcon_deviceName_noLoadingIcon_noFailureIcon() { - controllerSender.displayChip(transferSucceeded()) + val state = transferSucceeded() + controllerSender.displayChip(state) val chipView = getChipView() assertThat(chipView.getAppIconView().drawable).isEqualTo(appIconDrawable) assertThat(chipView.getAppIconView().contentDescription).isEqualTo(APP_ICON_CONTENT_DESC) - assertThat(chipView.getChipText()).contains(DEVICE_NAME) + assertThat(chipView.getChipText()).isEqualTo(state.getChipTextString(context)) assertThat(chipView.getLoadingIconVisibility()).isEqualTo(View.GONE) assertThat(chipView.getFailureIcon().visibility).isEqualTo(View.GONE) } @@ -132,12 +136,13 @@ class MediaTttChipControllerSenderTest : SysuiTestCase() { @Test fun transferFailed_appIcon_noDeviceName_noLoadingIcon_noUndo_failureIcon() { - controllerSender.displayChip(transferFailed()) + val state = transferFailed() + controllerSender.displayChip(state) val chipView = getChipView() assertThat(chipView.getAppIconView().drawable).isEqualTo(appIconDrawable) assertThat(chipView.getAppIconView().contentDescription).isEqualTo(APP_ICON_CONTENT_DESC) - assertThat(chipView.getChipText()).doesNotContain(DEVICE_NAME) + assertThat(chipView.getChipText()).isEqualTo(state.getChipTextString(context)) assertThat(chipView.getLoadingIconVisibility()).isEqualTo(View.GONE) assertThat(chipView.getUndoButton().visibility).isEqualTo(View.GONE) assertThat(chipView.getFailureIcon().visibility).isEqualTo(View.VISIBLE) @@ -219,8 +224,7 @@ class MediaTttChipControllerSenderTest : SysuiTestCase() { ) = TransferSucceeded(appIconDrawable, APP_ICON_CONTENT_DESC, DEVICE_NAME, undoRunnable) /** Helper method providing default parameters to not clutter up the tests. */ - private fun transferFailed() = - TransferFailed(appIconDrawable, APP_ICON_CONTENT_DESC, DEVICE_NAME) + private fun transferFailed() = TransferFailed(appIconDrawable, APP_ICON_CONTENT_DESC) } private const val DEVICE_NAME = "My Tablet" diff --git a/packages/SystemUI/tests/src/com/android/systemui/media/taptotransfer/sender/MediaTttSenderServiceTest.kt b/packages/SystemUI/tests/src/com/android/systemui/media/taptotransfer/sender/MediaTttSenderServiceTest.kt index ca909450c1445..8a491c220592b 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/media/taptotransfer/sender/MediaTttSenderServiceTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/media/taptotransfer/sender/MediaTttSenderServiceTest.kt @@ -44,7 +44,7 @@ class MediaTttSenderServiceTest : SysuiTestCase() { verify(controller).displayChip(capture(chipStateCaptor)) val chipState = chipStateCaptor.value!! - assertThat(chipState.otherDeviceName).isEqualTo(name) + assertThat(chipState.getChipTextString(context)).contains(name) } @Test @@ -56,7 +56,7 @@ class MediaTttSenderServiceTest : SysuiTestCase() { verify(controller).displayChip(capture(chipStateCaptor)) val chipState = chipStateCaptor.value!! - assertThat(chipState.otherDeviceName).isEqualTo(name) + assertThat(chipState.getChipTextString(context)).contains(name) } @Test @@ -68,7 +68,7 @@ class MediaTttSenderServiceTest : SysuiTestCase() { verify(controller).displayChip(capture(chipStateCaptor)) val chipState = chipStateCaptor.value!! - assertThat(chipState.otherDeviceName).isEqualTo(name) + assertThat(chipState.getChipTextString(context)).contains(name) } @Test