DownloadWorker: fix download status misinformation

-- cancellation ≠ failure
-- report cause of other failures
This commit is contained in:
Rahul Patel
2025-02-26 02:12:55 +05:30
parent 41a4db751c
commit b867d4a527

View File

@@ -44,6 +44,7 @@ import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
import java.security.DigestInputStream import java.security.DigestInputStream
import java.security.MessageDigest import java.security.MessageDigest
import kotlin.coroutines.cancellation.CancellationException
import kotlin.properties.Delegates import kotlin.properties.Delegates
import com.aurora.gplayapi.data.models.File as GPlayFile import com.aurora.gplayapi.data.models.File as GPlayFile
@@ -98,7 +99,7 @@ class DownloadWorker @AssistedInject constructor(
icon = Bitmap.createScaledBitmap(bitmap, 96, 96, true) icon = Bitmap.createScaledBitmap(bitmap, 96, 96, true)
} catch (exception: Exception) { } catch (exception: Exception) {
Log.e(TAG, "Failed to parse download data", exception) Log.e(TAG, "Failed to parse download data", exception)
onFailure() onFailure(exception)
return Result.failure() return Result.failure()
} }
@@ -109,9 +110,10 @@ class DownloadWorker @AssistedInject constructor(
download.fileList = download.fileList.ifEmpty { download.fileList = download.fileList.ifEmpty {
purchase(download.packageName, download.versionCode, download.offerType) purchase(download.packageName, download.versionCode, download.offerType)
} }
if (download.fileList.isEmpty()) { if (download.fileList.isEmpty()) {
Log.i(TAG, "Nothing to download!") Log.i(TAG, "Nothing to download!")
onFailure(appContext.getString(R.string.purchase_failed)) onFailure()
return Result.failure() return Result.failure()
} }
@@ -153,14 +155,16 @@ class DownloadWorker @AssistedInject constructor(
.onFailure { .onFailure {
Log.e(TAG, "Failed to download ${download.packageName}", it) Log.e(TAG, "Failed to download ${download.packageName}", it)
downloading = false downloading = false
onFailure()
onFailure(it as Exception)
return Result.failure() return Result.failure()
} }
while (downloading) { while (downloading) {
delay(1000) delay(1000)
val d = downloadDao.getDownload(download.packageName) val d = downloadDao.getDownload(download.packageName)
if (isStopped || d.downloadStatus == DownloadStatus.CANCELLED) { if (isStopped || d.downloadStatus == DownloadStatus.CANCELLED) {
onFailure() onFailure(CancellationException())
break break
} }
} }
@@ -173,7 +177,7 @@ class DownloadWorker @AssistedInject constructor(
requestList.forEach { require(verifyFile(it)) } requestList.forEach { require(verifyFile(it)) }
} catch (exception: Exception) { } catch (exception: Exception) {
Log.e(TAG, "Failed to verify downloaded files!", exception) Log.e(TAG, "Failed to verify downloaded files!", exception)
onFailure() onFailure(exception)
return Result.failure() return Result.failure()
} }
@@ -195,18 +199,22 @@ class DownloadWorker @AssistedInject constructor(
} }
} }
private suspend fun onFailure(errorMessage: String? = null, exception: Exception? = null) { private suspend fun onFailure(exception: Exception = Exception("Something went wrong!")) {
withContext(NonCancellable) { withContext(NonCancellable) {
Log.i(TAG, "Failed downloading ${download.packageName}") Log.i(TAG, "Failed downloading ${download.packageName}")
val cancelReasons = listOf(STOP_REASON_USER, STOP_REASON_CANCELLED_BY_APP) val cancelReasons = listOf(STOP_REASON_USER, STOP_REASON_CANCELLED_BY_APP)
if (isSAndAbove && stopReason in cancelReasons) { if (isSAndAbove && stopReason in cancelReasons) {
notifyStatus(DownloadStatus.CANCELLED) notifyStatus(DownloadStatus.CANCELLED)
} else { } else {
notifyStatus(DownloadStatus.FAILED) if (exception is CancellationException) {
AuroraApp.events.send(InstallerEvent.Failed(download.packageName).apply { notifyStatus(DownloadStatus.CANCELLED)
extra = errorMessage ?: appContext.getString(R.string.download_failed) } else {
error = exception?.stackTraceToString() ?: String() notifyStatus(DownloadStatus.FAILED)
}) AuroraApp.events.send(InstallerEvent.Failed(download.packageName).apply {
extra = exception.message ?: appContext.getString(R.string.download_failed)
error = exception.stackTraceToString()
})
}
} }
notificationManager.cancel(NOTIFICATION_ID) notificationManager.cancel(NOTIFICATION_ID)