DownloadWorker: Use localized error string resources
Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
@@ -85,13 +85,11 @@ class DownloadWorker @AssistedInject constructor(
|
|||||||
private val TAG = DownloadWorker::class.java.simpleName
|
private val TAG = DownloadWorker::class.java.simpleName
|
||||||
private val NOTIFICATION_ID: Int = 200
|
private val NOTIFICATION_ID: Int = 200
|
||||||
|
|
||||||
object Exceptions {
|
inner class NoNetworkException : Exception(appContext.getString(R.string.title_no_network))
|
||||||
class NoNetworkException : Exception("No network available")
|
inner class NothingToDownloadException : Exception(appContext.getString(R.string.purchase_no_file))
|
||||||
class NothingToDownloadException : Exception("Failed to purchase app")
|
inner class DownloadFailedException : Exception(appContext.getString(R.string.download_failed))
|
||||||
class DownloadFailedException : Exception("Download failed")
|
inner class DownloadCancelledException : Exception(appContext.getString(R.string.download_canceled))
|
||||||
class DownloadCancelledException : Exception("Download was cancelled")
|
inner class VerificationFailedException : Exception(appContext.getString(R.string.verification_failed))
|
||||||
class VerificationFailedException : Exception("Verification of downloaded files failed")
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun doWork(): Result {
|
override suspend fun doWork(): Result {
|
||||||
super.doWork()
|
super.doWork()
|
||||||
@@ -119,7 +117,7 @@ class DownloadWorker @AssistedInject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Bail out if file list is empty after purchase
|
// Bail out if file list is empty after purchase
|
||||||
if (download.fileList.isEmpty()) return onFailure(Exceptions.NothingToDownloadException())
|
if (download.fileList.isEmpty()) return onFailure(NothingToDownloadException())
|
||||||
|
|
||||||
// Create dirs & generate download request for files and shared libs (if any)
|
// Create dirs & generate download request for files and shared libs (if any)
|
||||||
PathUtil.getAppDownloadDir(appContext, download.packageName, download.versionCode).mkdirs()
|
PathUtil.getAppDownloadDir(appContext, download.packageName, download.versionCode).mkdirs()
|
||||||
@@ -163,14 +161,14 @@ class DownloadWorker @AssistedInject constructor(
|
|||||||
try {
|
try {
|
||||||
for (file in files) {
|
for (file in files) {
|
||||||
if (isStopped) {
|
if (isStopped) {
|
||||||
throw Exceptions.DownloadCancelledException()
|
throw DownloadCancelledException()
|
||||||
}
|
}
|
||||||
|
|
||||||
downloadFile(download.packageName, file)
|
downloadFile(download.packageName, file)
|
||||||
download.downloadedFiles++
|
download.downloadedFiles++
|
||||||
}
|
}
|
||||||
} catch (exception: Exception) {
|
} catch (exception: Exception) {
|
||||||
if (exception is Exceptions.DownloadCancelledException) {
|
if (exception is DownloadCancelledException) {
|
||||||
Log.i(TAG, "Download cancelled for ${download.packageName}")
|
Log.i(TAG, "Download cancelled for ${download.packageName}")
|
||||||
// Try to delete all downloaded files
|
// Try to delete all downloaded files
|
||||||
runCatching { files.forEach { deleteFile(it) } }
|
runCatching { files.forEach { deleteFile(it) } }
|
||||||
@@ -180,7 +178,7 @@ class DownloadWorker @AssistedInject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Report failure if download was stopped or failed
|
// Report failure if download was stopped or failed
|
||||||
if (isStopped) return onFailure(Exceptions.DownloadFailedException())
|
if (isStopped) return onFailure(DownloadFailedException())
|
||||||
|
|
||||||
// Verify downloaded files
|
// Verify downloaded files
|
||||||
try {
|
try {
|
||||||
@@ -188,7 +186,7 @@ class DownloadWorker @AssistedInject constructor(
|
|||||||
files.forEach { file -> require(verifyFile(file)) }
|
files.forEach { file -> require(verifyFile(file)) }
|
||||||
} catch (exception: Exception) {
|
} catch (exception: Exception) {
|
||||||
Log.e(TAG, "Failed to verify ${download.packageName}", exception)
|
Log.e(TAG, "Failed to verify ${download.packageName}", exception)
|
||||||
onFailure(Exceptions.VerificationFailedException())
|
onFailure(VerificationFailedException())
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.i(TAG, "Finished downloading & verifying ${download.packageName}")
|
Log.i(TAG, "Finished downloading & verifying ${download.packageName}")
|
||||||
@@ -216,11 +214,11 @@ class DownloadWorker @AssistedInject constructor(
|
|||||||
notifyStatus(DownloadStatus.CANCELLED)
|
notifyStatus(DownloadStatus.CANCELLED)
|
||||||
} else {
|
} else {
|
||||||
when (exception) {
|
when (exception) {
|
||||||
is Exceptions.DownloadCancelledException -> {
|
is DownloadCancelledException -> {
|
||||||
notifyStatus(DownloadStatus.CANCELLED)
|
notifyStatus(DownloadStatus.CANCELLED)
|
||||||
}
|
}
|
||||||
|
|
||||||
is Exceptions.NoNetworkException -> {
|
is NoNetworkException -> {
|
||||||
// TODO: Notify user of network failure, maybe a notification & retry option
|
// TODO: Notify user of network failure, maybe a notification & retry option
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -317,11 +315,11 @@ class DownloadWorker @AssistedInject constructor(
|
|||||||
is SocketException,
|
is SocketException,
|
||||||
is SocketTimeoutException,
|
is SocketTimeoutException,
|
||||||
is UnknownHostException -> {
|
is UnknownHostException -> {
|
||||||
throw Exceptions.NoNetworkException()
|
throw NoNetworkException()
|
||||||
}
|
}
|
||||||
|
|
||||||
is CancellationException -> {
|
is CancellationException -> {
|
||||||
throw Exceptions.DownloadCancelledException()
|
throw DownloadCancelledException()
|
||||||
}
|
}
|
||||||
|
|
||||||
else -> throw exception
|
else -> throw exception
|
||||||
|
|||||||
@@ -500,4 +500,7 @@
|
|||||||
<string name="pref_updates_restrictions_metered">Only on unmetered networks</string>
|
<string name="pref_updates_restrictions_metered">Only on unmetered networks</string>
|
||||||
<string name="pref_updates_restrictions_idle">When device is idle</string>
|
<string name="pref_updates_restrictions_idle">When device is idle</string>
|
||||||
<string name="pref_updates_restrictions_battery">When battery is not low</string>
|
<string name="pref_updates_restrictions_battery">When battery is not low</string>
|
||||||
|
|
||||||
|
<!-- DownloadWorker -->
|
||||||
|
<string name="verification_failed">Failed to verify downloaded files</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
Reference in New Issue
Block a user