Skip to content

Instantly share code, notes, and snippets.

@Luiz-Monad
Last active January 2, 2019 22:14
Show Gist options
  • Save Luiz-Monad/8ea036020d0b543add75d4f0b25de355 to your computer and use it in GitHub Desktop.
Save Luiz-Monad/8ea036020d0b543add75d4f0b25de355 to your computer and use it in GitHub Desktop.
loop@ for (info in infos) info.apply {
val startingCount = info.mCurrentBytes
//variables with mStatus are either from info or DownloaderService
if (mStatus != STATUS_SUCCESS)
DownloadThread(info, this@DownloaderService, mNotification!!).run
{
cancelAlarms()
scheduleAlarm(Constants.ACTIVE_THREAD_WATCHDOG)
run()
cancelAlarms()
}
db.updateFromDb(info)
var setWakeWatchdog = false
var notifyStatus: Int
check@ do {
when (info.mStatus) {
STATUS_FORBIDDEN -> {
// the URL is out of date
updateLVL(this@DownloaderService)
return
}
STATUS_SUCCESS -> {
mBytesSoFar += mCurrentBytes - startingCount
db.updateMetadata(Pkg.getVersionCode(info), 0)
continue@loop
}
STATUS_FILE_DELIVERED_INCORRECTLY -> {
// we may be on a network that is returning us a web
// page on redirect
notifyStatus = IDownloaderClient.STATE_PAUSED_NETWORK_SETUP_FAILURE
mCurrentBytes = 0
db.updateDownload(info)
setWakeWatchdog = true
}
STATUS_PAUSED_BY_APP ->
notifyStatus = IDownloaderClient.STATE_PAUSED_BY_REQUEST
STATUS_WAITING_FOR_NETWORK,
STATUS_WAITING_TO_RETRY -> {
notifyStatus = IDownloaderClient.STATE_PAUSED_NETWORK_UNAVAILABLE
setWakeWatchdog = true
}
STATUS_QUEUED_FOR_WIFI_OR_CELLULAR_PERMISSION,
STATUS_QUEUED_FOR_WIFI -> {
// look for more detail here
mWifiManager?.also {
if (!it.isWifiEnabled) {
notifyStatus = IDownloaderClient.STATE_PAUSED_WIFI_DISABLED_NEED_CELLULAR_PERMISSION
setWakeWatchdog = true
break@check
}
}
notifyStatus = IDownloaderClient.STATE_PAUSED_NEED_CELLULAR_PERMISSION
setWakeWatchdog = true
}
STATUS_CANCELED -> {
notifyStatus = IDownloaderClient.STATE_FAILED_CANCELED
setWakeWatchdog = true
}
STATUS_INSUFFICIENT_SPACE_ERROR -> {
notifyStatus = IDownloaderClient.STATE_FAILED_SDCARD_FULL
setWakeWatchdog = true
}
STATUS_DEVICE_NOT_FOUND_ERROR -> {
notifyStatus = IDownloaderClient.STATE_PAUSED_SDCARD_UNAVAILABLE
setWakeWatchdog = true
}
else ->
notifyStatus = IDownloaderClient.STATE_FAILED
}
} while (false)
if (setWakeWatchdog)
scheduleAlarm(Constants.WATCHDOG_WAKE_TIMER)
else
cancelAlarms()
// failure or pause state
mNotification!!.onDownloadStateChanged(notifyStatus)
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment