Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Merge #7698
Browse files Browse the repository at this point in the history
7698: For #7673: Move DownloadProgress and Status to DownloadState r=Amejia481 a=kglazko



Co-authored-by: Kate Glazko <[email protected]>
  • Loading branch information
MozLando and Kate Glazko committed Jul 28, 2020
2 parents bc67b17 + 1003535 commit ba26a92
Show file tree
Hide file tree
Showing 14 changed files with 321 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import mozilla.components.browser.state.action.MediaAction
import mozilla.components.browser.state.action.TrackingProtectionAction
import mozilla.components.browser.state.state.content.DownloadState
import mozilla.components.browser.state.state.content.FindResultState
import mozilla.components.browser.state.state.content.DownloadState.Status.INITIATED
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.concept.engine.EngineSession
import mozilla.components.concept.engine.HitResult
Expand Down Expand Up @@ -203,6 +204,8 @@ internal class EngineObserver(
fileName,
contentType,
fileSize,
0,
INITIATED,
userAgent,
Environment.DIRECTORY_DOWNLOADS
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import kotlin.random.Random
* @property fileName A canonical filename for this download.
* @property contentType Content type (MIME type) to indicate the media type of the download.
* @property contentLength The file size reported by the server.
* @property currentBytesCopied The number of current bytes copied.
* @property status The current status of the download.
* @property userAgent The user agent to be used for the download.
* @property destinationDirectory The matching destination directory for this type of download.
* @property filePath The file path the file was saved at.
Expand All @@ -32,6 +34,8 @@ data class DownloadState(
val fileName: String? = null,
val contentType: String? = null,
val contentLength: Long? = null,
val currentBytesCopied: Long = 0,
val status: Status = Status.INITIATED,
val userAgent: String? = null,
val destinationDirectory: String = Environment.DIRECTORY_DOWNLOADS,
val referrerUrl: String? = null,
Expand All @@ -41,4 +45,35 @@ data class DownloadState(
) : Parcelable {
val filePath: String get() =
Environment.getExternalStoragePublicDirectory(destinationDirectory).path + "/" + fileName

/**
* Status that represents every state that a download can be in.
*/
enum class Status {
/**
* Indicates that the download is in the first state after creation but not yet [DOWNLOADING].
*/
INITIATED,
/**
* Indicates that an [INITIATED] download is now actively being downloaded.
*/
DOWNLOADING,
/**
* Indicates that the download that has been [DOWNLOADING] has been paused.
*/
PAUSED,
/**
* Indicates that the download that has been [DOWNLOADING] has been cancelled.
*/
CANCELLED,
/**
* Indicates that the download that has been [DOWNLOADING] has moved to failed because
* something unexpected has happened.
*/
FAILED,
/**
* Indicates that the [DOWNLOADING] download has been completed.
*/
COMPLETED
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ import androidx.core.app.NotificationManagerCompat
import androidx.core.app.NotificationManagerCompat.IMPORTANCE_NONE
import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService
import mozilla.components.browser.state.state.content.DownloadState.Status.INITIATED
import mozilla.components.browser.state.state.content.DownloadState.Status.DOWNLOADING
import mozilla.components.browser.state.state.content.DownloadState.Status.PAUSED
import mozilla.components.browser.state.state.content.DownloadState.Status.COMPLETED
import mozilla.components.browser.state.state.content.DownloadState.Status.CANCELLED
import mozilla.components.browser.state.state.content.DownloadState.Status.FAILED
import mozilla.components.feature.downloads.AbstractFetchDownloadService.Companion.ACTION_CANCEL
import mozilla.components.feature.downloads.AbstractFetchDownloadService.Companion.ACTION_DISMISS
import mozilla.components.feature.downloads.AbstractFetchDownloadService.Companion.ACTION_OPEN
import mozilla.components.feature.downloads.AbstractFetchDownloadService.Companion.ACTION_PAUSE
import mozilla.components.feature.downloads.AbstractFetchDownloadService.Companion.ACTION_RESUME
import mozilla.components.feature.downloads.AbstractFetchDownloadService.Companion.ACTION_TRY_AGAIN
import mozilla.components.feature.downloads.AbstractFetchDownloadService.DownloadJobState
import mozilla.components.feature.downloads.AbstractFetchDownloadService.DownloadJobStatus.CANCELLED
import mozilla.components.feature.downloads.AbstractFetchDownloadService.DownloadJobStatus.COMPLETED
import mozilla.components.feature.downloads.AbstractFetchDownloadService.DownloadJobStatus.ACTIVE
import mozilla.components.feature.downloads.AbstractFetchDownloadService.DownloadJobStatus.FAILED
import mozilla.components.feature.downloads.AbstractFetchDownloadService.DownloadJobStatus.PAUSED
import kotlin.random.Random

@Suppress("LargeClass", "TooManyFunctions")
Expand All @@ -48,7 +49,7 @@ internal object DownloadNotification {
context: Context,
notifications: List<DownloadJobState>
): Notification {
val allDownloadsHaveFinished = notifications.all { it.status != ACTIVE }
val allDownloadsHaveFinished = notifications.all { it.state.status != DOWNLOADING }
val icon = if (allDownloadsHaveFinished) {
R.drawable.mozac_feature_download_ic_download_complete
} else {
Expand Down Expand Up @@ -78,7 +79,7 @@ internal object DownloadNotification {
downloadJobState: DownloadJobState
): Notification {
val downloadState = downloadJobState.state
val bytesCopied = downloadJobState.currentBytesCopied
val bytesCopied = downloadJobState.state.currentBytesCopied
val channelId = ensureChannelExists(context)
val isIndeterminate = downloadState.contentLength == null

Expand Down Expand Up @@ -279,7 +280,7 @@ internal fun NotificationCompat.Builder.setCompatGroup(groupKey: String): Notifi

@VisibleForTesting
internal fun DownloadJobState.getProgress(): String {
val bytesCopied = currentBytesCopied
val bytesCopied = state.currentBytesCopied
val isIndeterminate = state.contentLength == null || bytesCopied == 0L
return if (isIndeterminate) {
""
Expand All @@ -290,8 +291,8 @@ internal fun DownloadJobState.getProgress(): String {

@VisibleForTesting
internal fun DownloadJobState.getStatusDescription(context: Context): String {
return when (this.status) {
ACTIVE -> {
return when (this.state.status) {
DOWNLOADING -> {
getProgress()
}

Expand All @@ -307,6 +308,6 @@ internal fun DownloadJobState.getStatusDescription(context: Context): String {
context.getString(R.string.mozac_feature_downloads_failed_notification_text2)
}

CANCELLED -> ""
CANCELLED, INITIATED -> ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.core.content.getSystemService
import androidx.core.net.toUri
import androidx.core.util.set
import mozilla.components.browser.state.action.DownloadAction
import mozilla.components.browser.state.state.content.DownloadState.Status
import mozilla.components.browser.state.state.content.DownloadState
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.concept.fetch.Headers.Names.COOKIE
Expand Down Expand Up @@ -107,9 +108,9 @@ class AndroidDownloadManager(
val downloadID = intent.getLongExtra(EXTRA_DOWNLOAD_ID, -1)
val download = store.state.queuedDownloads[downloadID]
val downloadStatus = intent.getSerializableExtra(AbstractFetchDownloadService.EXTRA_DOWNLOAD_STATUS)
as AbstractFetchDownloadService.DownloadJobStatus
as Status

if (downloadStatus == AbstractFetchDownloadService.DownloadJobStatus.COMPLETED) {
if (downloadStatus == Status.COMPLETED) {
store.dispatch(DownloadAction.RemoveQueuedDownloadAction(downloadID))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
package mozilla.components.feature.downloads.manager

import android.content.Context
import mozilla.components.browser.state.state.content.DownloadState.Status
import mozilla.components.browser.state.state.content.DownloadState
import mozilla.components.feature.downloads.AbstractFetchDownloadService
import mozilla.components.support.ktx.android.content.isPermissionGranted

typealias onDownloadStopped = (DownloadState, Long, AbstractFetchDownloadService.DownloadJobStatus) -> Unit
typealias onDownloadStopped = (DownloadState, Long, Status) -> Unit

interface DownloadManager {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import android.os.Build.VERSION.SDK_INT
import android.os.Build.VERSION_CODES.P
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import mozilla.components.browser.state.action.DownloadAction
import mozilla.components.browser.state.state.content.DownloadState.Status
import mozilla.components.browser.state.state.content.DownloadState
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.feature.downloads.AbstractFetchDownloadService
Expand Down Expand Up @@ -103,9 +104,9 @@ class FetchDownloadManager<T : AbstractFetchDownloadService>(
val downloadID = intent.getLongExtra(EXTRA_DOWNLOAD_ID, -1)
val download = store.state.queuedDownloads[downloadID]
val downloadStatus = intent.getSerializableExtra(EXTRA_DOWNLOAD_STATUS)
as AbstractFetchDownloadService.DownloadJobStatus
as Status

if (downloadStatus == AbstractFetchDownloadService.DownloadJobStatus.COMPLETED) {
if (downloadStatus == Status.COMPLETED) {
store.dispatch(DownloadAction.RemoveQueuedDownloadAction(downloadID))
}
if (download != null) {
Expand Down
Loading

0 comments on commit ba26a92

Please sign in to comment.