Skip to content

Commit

Permalink
Closes issue mozilla-mobile#8190 ArithmeticException: divide by zero…
Browse files Browse the repository at this point in the history
… in Download notification
  • Loading branch information
Amejia481 committed Sep 1, 2020
1 parent 8d092d1 commit d3d96a6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ internal object DownloadNotification {
val downloadState = downloadJobState.state
val bytesCopied = downloadJobState.currentBytesCopied
val channelId = ensureChannelExists(context)
val isIndeterminate = downloadState.contentLength == null
val isIndeterminate = downloadJobState.isIndeterminate()

return NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.mozac_feature_download_ic_ongoing_download)
Expand Down Expand Up @@ -281,14 +281,18 @@ internal fun NotificationCompat.Builder.setCompatGroup(groupKey: String): Notifi
@VisibleForTesting
internal fun DownloadJobState.getProgress(): String {
val bytesCopied = currentBytesCopied
val isIndeterminate = state.contentLength == null || bytesCopied == 0L
return if (isIndeterminate) {
return if (isIndeterminate()) {
""
} else {
"${DownloadNotification.PERCENTAGE_MULTIPLIER * bytesCopied / state.contentLength!!}%"
}
}

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

@VisibleForTesting
internal fun DownloadJobState.getStatusDescription(context: Context): String {
return when (this.status) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ class DownloadNotificationTest {
val newDownload = downloadJobState.copy(state = downloadJobState.state.copy(contentLength = null))

assertEquals("", newDownload.getProgress())

val downloadWithNoSize = downloadJobState.copy(state = downloadJobState.state.copy(contentLength = 0))

assertEquals("", downloadWithNoSize.getProgress())

val downloadWithNullSize = downloadJobState.copy(state = downloadJobState.state.copy(contentLength = null))

assertEquals("", downloadWithNullSize.getProgress())
}

@Test
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ permalink: /changelog/
* ⚠️ **This is a breaking change**: `BrowserState.downloads` has changed it's type from `Map<Long, DownloadState>` to `Map<String, DownloadState>`.
* 🌟 Added support for persisting/restoring downloads see issue [#7762](https://github.com/mozilla-mobile/android-components/issues/7762).
* 🌟 Added `DownloadStorage` for querying stored download metadata.
* 🚒 Bug [issue #8190](https://github.com/mozilla-mobile/android-components/issues/8190) ArithmeticException: divide by zero in Download notification.


* **ui-widgets**
* 🆕 New VerticalSwipeRefreshLayout that comes to resolve many of the issues of the platform SwipeRefreshLayout and filters out other gestures than swipe down/up.
Expand Down

0 comments on commit d3d96a6

Please sign in to comment.