Commit d5fc8c59 authored by iorgamgabriel's avatar iorgamgabriel Committed by giorga@mozilla.com
Browse files

Bug 1964683 - Refactor DownloadNotification.createOngoingDownloadNotification...

Bug 1964683 - Refactor DownloadNotification.createOngoingDownloadNotification to not take downloadJobState as param r=android-reviewers,npoon

Differential Revision: https://phabricator.services.mozilla.com/D250252
parent 74eb44d6
Loading
Loading
Loading
Loading
+10 −5
Changes for mobile/android/android-components/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/AbstractFetchDownloadService.kt: 10 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -406,25 +406,29 @@ abstract class AbstractFetchDownloadService : Service() {
        val notification = when (latestUIStatus) {
            DOWNLOADING -> DownloadNotification.createOngoingDownloadNotification(
                context = context,
                downloadJobState = download,
                downloadState = download.state,
                fileSizeFormatter = fileSizeFormatter,
                notificationAccentColor = style.notificationAccentColor,
                downloadEstimator = download.downloadEstimator,
            )
            PAUSED -> DownloadNotification.createPausedDownloadNotification(
                context,
                download,
                download.state,
                download.createdTime,
                style.notificationAccentColor,
            )
            FAILED -> DownloadNotification.createDownloadFailedNotification(
                context,
                download,
                download.state,
                download.createdTime,
                style.notificationAccentColor,
            )
            COMPLETED -> {
                addToDownloadSystemDatabaseCompat(download.state, scope)
                DownloadNotification.createDownloadCompletedNotification(
                    context,
                    download,
                    download.state,
                    download.createdTime,
                    style.notificationAccentColor,
                )
            }
@@ -633,9 +637,10 @@ abstract class AbstractFetchDownloadService : Service() {
        val notification =
            DownloadNotification.createOngoingDownloadNotification(
                context = context,
                downloadJobState = downloadJobState,
                downloadState = downloadJobState.state,
                fileSizeFormatter = fileSizeFormatter,
                notificationAccentColor = style.notificationAccentColor,
                downloadEstimator = downloadJobState.downloadEstimator,
            )
        compatForegroundNotificationId = downloadJobState.foregroundServiceId

+25 −25
Changes for mobile/android/android-components/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/DownloadNotification.kt: 25 added lines, 25 removed lines.
Original line number Diff line number Diff line
@@ -86,14 +86,14 @@ internal object DownloadNotification {
     */
    fun createOngoingDownloadNotification(
        context: Context,
        downloadJobState: DownloadJobState,
        downloadState: DownloadState,
        fileSizeFormatter: FileSizeFormatter,
        notificationAccentColor: Int,
        downloadEstimator: DownloadEstimator?,
    ): Notification {
        val downloadState = downloadJobState.state
        val channelId = ensureChannelExists(context)
        val isIndeterminate = downloadJobState.isIndeterminate()
        val percentCopied = downloadJobState.getPercent() ?: -1
        val isIndeterminate = downloadState.isIndeterminate()
        val percentCopied = downloadState.getPercent() ?: -1

        return NotificationCompat.Builder(context, channelId)
            .setStyle(
@@ -102,7 +102,7 @@ internal object DownloadNotification {
                    .setSummaryText(
                        formatDownloadTimeRemaining(
                            context = context,
                            downloadEstimator = downloadJobState.downloadEstimator,
                            downloadEstimator = downloadEstimator,
                            curBytes = downloadState.currentBytesCopied,
                        ),
                    ),
@@ -110,13 +110,13 @@ internal object DownloadNotification {
            .setSmallIcon(R.drawable.mozac_feature_download_ic_ongoing_download)
            .setContentTitle(downloadState.fileName.orEmpty())
            .setContentText(
                downloadJobState.getProgress(fileSizeFormatter = fileSizeFormatter),
                downloadState.getProgress(fileSizeFormatter = fileSizeFormatter),
            )
            .setColor(ContextCompat.getColor(context, notificationAccentColor))
            .setCategory(NotificationCompat.CATEGORY_PROGRESS)
            .setProgress(PERCENTAGE_MULTIPLIER, percentCopied, isIndeterminate)
            .setOngoing(true)
            .setWhen(downloadJobState.createdTime)
            .setWhen(downloadState.createdTime)
            .setOnlyAlertOnce(true)
            .addAction(getPauseAction(context, downloadState.id))
            .addAction(getCancelAction(context, downloadState.id))
@@ -130,12 +130,12 @@ internal object DownloadNotification {
     */
    fun createPausedDownloadNotification(
        context: Context,
        downloadJobState: DownloadJobState,
        downloadState: DownloadState,
        createdTime: Long,
        notificationAccentColor: Int,
    ): Notification {
        val channelId = ensureChannelExists(context)

        val downloadState = downloadJobState.state
        return NotificationCompat.Builder(context, channelId)
            .setSmallIcon(R.drawable.mozac_feature_download_ic_download)
            .setContentTitle(downloadState.fileName)
@@ -145,7 +145,7 @@ internal object DownloadNotification {
            .setColor(ContextCompat.getColor(context, notificationAccentColor))
            .setCategory(NotificationCompat.CATEGORY_PROGRESS)
            .setOngoing(true)
            .setWhen(downloadJobState.createdTime)
            .setWhen(createdTime)
            .setOnlyAlertOnce(true)
            .addAction(getResumeAction(context, downloadState.id))
            .addAction(getCancelAction(context, downloadState.id))
@@ -159,17 +159,17 @@ internal object DownloadNotification {
     */
    fun createDownloadCompletedNotification(
        context: Context,
        downloadJobState: DownloadJobState,
        downloadState: DownloadState,
        createdTime: Long,
        notificationAccentColor: Int,
        contentIntent: PendingIntent = createOpenFilePendingIntent(context, downloadJobState.state),
        contentIntent: PendingIntent = createOpenFilePendingIntent(context, downloadState),
    ): Notification {
        val channelId = ensureChannelExists(context)
        val downloadState = downloadJobState.state

        return NotificationCompat.Builder(context, channelId)
            .setSmallIcon(R.drawable.mozac_feature_download_ic_download_complete)
            .setContentTitle(downloadState.fileName)
            .setWhen(downloadJobState.createdTime)
            .setWhen(createdTime)
            .setOnlyAlertOnce(true)
            .setContentText(
                context.applicationContext.getString(R.string.mozac_feature_downloads_completed_notification_text2),
@@ -187,11 +187,11 @@ internal object DownloadNotification {
     */
    fun createDownloadFailedNotification(
        context: Context,
        downloadJobState: DownloadJobState,
        downloadState: DownloadState,
        createdTime: Long,
        notificationAccentColor: Int,
    ): Notification {
        val channelId = ensureChannelExists(context)
        val downloadState = downloadJobState.state

        return NotificationCompat.Builder(context, channelId)
            .setSmallIcon(R.drawable.mozac_feature_download_ic_download_failed)
@@ -203,7 +203,7 @@ internal object DownloadNotification {
            .setCategory(NotificationCompat.CATEGORY_ERROR)
            .addAction(getTryAgainAction(context, downloadState.id))
            .addAction(getCancelAction(context, downloadState.id))
            .setWhen(downloadJobState.createdTime)
            .setWhen(createdTime)
            .setOnlyAlertOnce(true)
            .setPriority(NotificationCompat.PRIORITY_LOW)
            .setDeleteIntent(createDismissPendingIntent(context, downloadState.id))
@@ -218,7 +218,7 @@ internal object DownloadNotification {
        notifications: List<DownloadJobState>,
    ): List<String> {
        return notifications.take(2).map { downloadState ->
            "${downloadState.state.fileName} ${downloadState.getStatusDescription(
            "${downloadState.state.fileName} ${downloadState.state.getStatusDescription(
                context = context,
                fileSizeFormatter = fileSizeFormatter,
            )}"
@@ -349,9 +349,9 @@ internal fun NotificationCompat.Builder.setCompatGroup(groupKey: String): Notifi
    }
}

private fun DownloadJobState.getPercent(): Int? {
private fun DownloadState.getPercent(): Int? {
    val bytesCopied = currentBytesCopied
    val contentLength = state.contentLength
    val contentLength = contentLength
    return if (contentLength == null || contentLength == 0L) {
        null
    } else {
@@ -360,22 +360,22 @@ private fun DownloadJobState.getPercent(): Int? {
}

@VisibleForTesting
internal fun DownloadJobState.getProgress(fileSizeFormatter: FileSizeFormatter): String {
internal fun DownloadState.getProgress(fileSizeFormatter: FileSizeFormatter): String {
    return if (isIndeterminate()) {
        ""
    } else {
        "${fileSizeFormatter.formatSizeInBytes(currentBytesCopied)} / " +
            fileSizeFormatter.formatSizeInBytes(state.contentLength!!)
            fileSizeFormatter.formatSizeInBytes(contentLength!!)
    }
}

private fun DownloadJobState.isIndeterminate(): Boolean {
private fun DownloadState.isIndeterminate(): Boolean {
    val bytesCopied = currentBytesCopied
    return state.contentLength == null || bytesCopied == 0L || state.contentLength == 0L
    return contentLength == null || bytesCopied == 0L || contentLength == 0L
}

@VisibleForTesting
internal fun DownloadJobState.getStatusDescription(
internal fun DownloadState.getStatusDescription(
    context: Context,
    fileSizeFormatter: FileSizeFormatter,
): String {
+2 −1
Changes for mobile/android/android-components/components/feature/downloads/src/test/java/mozilla/components/feature/downloads/AbstractFetchDownloadServiceTest.kt: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -1562,9 +1562,10 @@ class AbstractFetchDownloadServiceTest {
        val notificationStyle = AbstractFetchDownloadService.Style()
        val notification = DownloadNotification.createOngoingDownloadNotification(
            context = testContext,
            downloadJobState = downloadState,
            downloadState = downloadState.state,
            fileSizeFormatter = fakeFileSizeFormatter,
            notificationAccentColor = notificationStyle.notificationAccentColor,
            downloadEstimator = downloadState.downloadEstimator,
        )

        NotificationManagerCompat.from(testContext).notify(downloadState.foregroundServiceId, notification)
+26 −19
Changes for mobile/android/android-components/components/feature/downloads/src/test/java/mozilla/components/feature/downloads/DownloadNotificationTest.kt: 26 added lines, 19 removed lines.
Original line number Diff line number Diff line
@@ -46,7 +46,7 @@ class DownloadNotificationTest {

        assertEquals(
            "10 / 100",
            downloadJobState.getProgress(
            downloadJobState.state.getProgress(
                fileSizeFormatter = fakeFileSizeFormatter,
            ),
        )
@@ -55,7 +55,7 @@ class DownloadNotificationTest {

        assertEquals(
            "",
            newDownload.getProgress(
            newDownload.state.getProgress(
                fileSizeFormatter = fakeFileSizeFormatter,
            ),
        )
@@ -64,7 +64,7 @@ class DownloadNotificationTest {

        assertEquals(
            "",
            downloadWithNoSize.getProgress(
            downloadWithNoSize.state.getProgress(
                fileSizeFormatter = fakeFileSizeFormatter,
            ),
        )
@@ -73,7 +73,7 @@ class DownloadNotificationTest {

        assertEquals(
            "",
            downloadWithNullSize.getProgress(
            downloadWithNullSize.state.getProgress(
                fileSizeFormatter = fakeFileSizeFormatter,
            ),
        )
@@ -117,10 +117,10 @@ class DownloadNotificationTest {
        )

        assertEquals(
            downloadJobState.getProgress(
            downloadJobState.state.getProgress(
                fakeFileSizeFormatter,
            ),
            downloadJobState.getStatusDescription(
            downloadJobState.state.getStatusDescription(
                context = testContext,
                fileSizeFormatter = fakeFileSizeFormatter,
            ),
@@ -142,7 +142,7 @@ class DownloadNotificationTest {

        assertEquals(
            pausedText,
            downloadJobState.getStatusDescription(
            downloadJobState.state.getStatusDescription(
                context = testContext,
                fileSizeFormatter = fakeFileSizeFormatter,
            ),
@@ -164,7 +164,7 @@ class DownloadNotificationTest {

        assertEquals(
            completedText,
            downloadJobState.getStatusDescription(
            downloadJobState.state.getStatusDescription(
                context = testContext,
                fileSizeFormatter = fakeFileSizeFormatter,
            ),
@@ -186,7 +186,7 @@ class DownloadNotificationTest {

        assertEquals(
            failedText,
            downloadJobState.getStatusDescription(
            downloadJobState.state.getStatusDescription(
                context = testContext,
                fileSizeFormatter = fakeFileSizeFormatter,
            ),
@@ -208,7 +208,7 @@ class DownloadNotificationTest {

        assertEquals(
            "",
            downloadJobState.getStatusDescription(
            downloadJobState.state.getStatusDescription(
                context = testContext,
                fileSizeFormatter = fakeFileSizeFormatter,
            ),
@@ -277,9 +277,10 @@ class DownloadNotificationTest {

        val notification = DownloadNotification.createOngoingDownloadNotification(
            context = testContext,
            downloadJobState = downloadJobState,
            downloadState = downloadJobState.state,
            fileSizeFormatter = fakeFileSizeFormatter,
            notificationAccentColor = style.notificationAccentColor,
            downloadEstimator = downloadJobState.downloadEstimator,
        )

        assertEquals(
@@ -291,18 +292,20 @@ class DownloadNotificationTest {

        val notificationNewDownload = DownloadNotification.createOngoingDownloadNotification(
            context = testContext,
            downloadJobState = downloadJobState.copy(state = downloadJobState.state.copy(contentLength = null)),
            downloadState = downloadJobState.state.copy(contentLength = null),
            fileSizeFormatter = fakeFileSizeFormatter,
            notificationAccentColor = style.notificationAccentColor,
            downloadEstimator = downloadJobState.downloadEstimator,
        )

        assertEquals(true, notificationNewDownload.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE))

        val notificationDownloadWithNoSize = DownloadNotification.createOngoingDownloadNotification(
            context = testContext,
            downloadJobState = downloadJobState.copy(state = downloadJobState.state.copy(contentLength = 0)),
            downloadState = downloadJobState.state.copy(contentLength = 0),
            fileSizeFormatter = fakeFileSizeFormatter,
            notificationAccentColor = style.notificationAccentColor,
            downloadEstimator = downloadJobState.downloadEstimator,
        )

        assertEquals(true, notificationDownloadWithNoSize.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE))
@@ -329,9 +332,10 @@ class DownloadNotificationTest {

        val notification = DownloadNotification.createOngoingDownloadNotification(
            context = testContext,
            downloadJobState = download,
            downloadState = download.state,
            fileSizeFormatter = fakeFileSizeFormatter,
            notificationAccentColor = style.notificationAccentColor,
            downloadEstimator = download.downloadEstimator,
        )

        val accentColor = ContextCompat.getColor(testContext, style.notificationAccentColor)
@@ -360,7 +364,8 @@ class DownloadNotificationTest {

        val notification = DownloadNotification.createPausedDownloadNotification(
            testContext,
            download,
            download.state,
            download.createdTime,
            notificationAccentColor = style.notificationAccentColor,
        )

@@ -389,10 +394,11 @@ class DownloadNotificationTest {
        val style = AbstractFetchDownloadService.Style()

        val notification = DownloadNotification.createDownloadCompletedNotification(
            testContext,
            download,
            context = testContext,
            downloadState = download.state,
            createdTime = download.createdTime,
            notificationAccentColor = style.notificationAccentColor,
            mock(PendingIntent::class.java),
            contentIntent = mock(PendingIntent::class.java),
        )

        val accentColor = ContextCompat.getColor(testContext, style.notificationAccentColor)
@@ -421,7 +427,8 @@ class DownloadNotificationTest {

        val notification = DownloadNotification.createDownloadFailedNotification(
            testContext,
            download,
            download.state,
            download.createdTime,
            notificationAccentColor = style.notificationAccentColor,
        )