Skip to content

Commit

Permalink
Better notification for folder download
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Jan 4, 2024
1 parent 04e0031 commit 857d479
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class DownloadNotificationManager(private val context: Context, private val view
setContentText(context.resources.getString(R.string.worker_download))
setSmallIcon(R.drawable.notification_icon)
setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.notification_icon))

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_DOWNLOAD)
}
Expand Down Expand Up @@ -117,6 +118,26 @@ class DownloadNotificationManager(private val context: Context, private val view
)
}

fun notifyForFolderResult(isAnyOperationFailed: Boolean, folderName: String) {
val notifyId = SecureRandom().nextInt()
val message = if (!isAnyOperationFailed) {
context.getString(R.string.downloader_folder_downloaded, folderName)
} else {
context.getString(R.string.downloader_folder_download_failed, folderName)
}

notificationBuilder.run {
setContentText(message)
notificationManager.notify(notifyId, this.build())
}

NotificationUtils.cancelWithDelay(
notificationManager,
notifyId,
2000
)
}

private fun getResultText(result: RemoteOperationResult<*>, download: DownloadFileOperation): String {
return if (result.isSuccess) {
download.file.fileName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import java.util.Vector

@Suppress("LongParameterList", "TooManyFunctions")
class FileDownloadWorker(
private val viewThemeUtils: ViewThemeUtils,
viewThemeUtils: ViewThemeUtils,
private val accountManager: UserAccountManager,
private val uploadsStorageManager: UploadsStorageManager,
private var localBroadcastManager: LocalBroadcastManager,
Expand All @@ -68,7 +68,7 @@ class FileDownloadWorker(
companion object {
private val TAG = FileDownloadWorker::class.java.simpleName

const val FOLDER_ID = "FOLDER_ID"
const val FOLDER = "FOLDER"
const val USER_NAME = "USER"
const val FILE = "FILE"
const val FILES = "FILES"
Expand Down Expand Up @@ -103,6 +103,8 @@ class FileDownloadWorker(
private var storageManager: FileDataStorageManager? = null
private var downloadClient: OwnCloudClient? = null
private var user: User? = null
private var folder: OCFile? = null
private var isAnyOperationFailed = true
private val gson = Gson()
private val pendingDownloads = IndexedForest<DownloadFileOperation>()

Expand All @@ -119,6 +121,9 @@ class FileDownloadWorker(
}

setIdleWorkerState()
folder?.let {
notifyForFolderResult(it)
}

Log_OC.e(TAG, "FilesDownloadWorker successfully completed")
Result.success()
Expand All @@ -139,11 +144,16 @@ class FileDownloadWorker(
super.onStopped()
}

private fun notifyForFolderResult(folder: OCFile) {
notificationManager.notifyForFolderResult(isAnyOperationFailed, folder.fileName)
}

private fun getRequestDownloads(): AbstractList<String> {
val files = getFiles()
val downloadType = getDownloadType()
setUser()

folder = gson.fromJson(inputData.keyValueMap[FOLDER] as? String, OCFile::class.java) ?: null
conflictUploadId = inputData.keyValueMap[CONFLICT_UPLOAD_ID] as Long?
val behaviour = inputData.keyValueMap[BEHAVIOUR] as String? ?: ""
val activityName = inputData.keyValueMap[ACTIVITY_NAME] as String? ?: ""
Expand Down Expand Up @@ -312,6 +322,10 @@ class FileDownloadWorker(
}

private fun cleanupDownloadProcess(result: RemoteOperationResult<*>?) {
result?.let {
isAnyOperationFailed = !it.isSuccess
}

val removeResult = pendingDownloads.removePayload(
currentDownload?.user?.accountName,
currentDownload?.remotePath
Expand All @@ -321,11 +335,13 @@ class FileDownloadWorker(

currentDownload?.run {
notifyDownloadResult(this, downloadResult)

val downloadFinishedIntent = intents.downloadFinishedIntent(
this,
downloadResult,
removeResult.second
)

localBroadcastManager.sendBroadcast(downloadFinishedIntent)
}
}
Expand Down Expand Up @@ -353,7 +369,9 @@ class FileDownloadWorker(
setContentIntent(intents.detailsIntent(null), PendingIntent.FLAG_IMMUTABLE)
}

notifyForResult(downloadResult, download)
if (folder == null) {
notifyForResult(downloadResult, download)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ internal class BackgroundJobManagerImpl(
override fun startFolderDownloadJob(folder: OCFile, user: User, files: List<OCFile>) {
val data = workDataOf(
FileDownloadWorker.USER_NAME to user.accountName,
FileDownloadWorker.FOLDER_ID to folder.fileId,
FileDownloadWorker.FOLDER to gson.toJson(folder),
FileDownloadWorker.FILES to gson.toJson(files),
FileDownloadWorker.DOWNLOAD_TYPE to DownloadType.DOWNLOAD.toString()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1585,7 +1585,6 @@ protected ServiceConnection newTransferenceServiceConnection() {
return new ListServiceConnection();
}

// FIXME ServiceConnection will not trigger anymore
/**
* Defines callbacks for service binding, passed to bindService()
*/
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@
<string name="downloader_download_failed_ticker">Download failed</string>
<string name="downloader_download_failed_content">Could not download %1$s</string>
<string name="downloader_not_downloaded_yet">Not downloaded yet</string>
<string name="downloader_folder_download_failed">Error occurred while downloading %s folder</string>
<string name="downloader_folder_downloaded">%s folder successfully downloaded</string>
<string name="downloader_download_failed_credentials_error">Download failed, log in again</string>
<string name="common_choose_account">Choose account</string>
<string name="common_switch_account">Switch account</string>
Expand Down

0 comments on commit 857d479

Please sign in to comment.