浏览代码

Better notification for folder download

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 年之前
父节点
当前提交
09b4cf44b2

+ 21 - 0
app/src/main/java/com/nextcloud/client/files/downloader/DownloadNotificationManager.kt

@@ -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)
             }
@@ -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

+ 21 - 3
app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt

@@ -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,
@@ -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"
@@ -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>()
 
@@ -119,6 +121,9 @@ class FileDownloadWorker(
             }
 
             setIdleWorkerState()
+            folder?.let {
+                notifyForFolderResult(it)
+            }
 
             Log_OC.e(TAG, "FilesDownloadWorker successfully completed")
             Result.success()
@@ -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? ?: ""
@@ -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
@@ -321,11 +335,13 @@ class FileDownloadWorker(
 
         currentDownload?.run {
             notifyDownloadResult(this, downloadResult)
+
             val downloadFinishedIntent = intents.downloadFinishedIntent(
                 this,
                 downloadResult,
                 removeResult.second
             )
+
             localBroadcastManager.sendBroadcast(downloadFinishedIntent)
         }
     }
@@ -353,7 +369,9 @@ class FileDownloadWorker(
                 setContentIntent(intents.detailsIntent(null), PendingIntent.FLAG_IMMUTABLE)
             }
 
-            notifyForResult(downloadResult, download)
+            if (folder == null) {
+                notifyForResult(downloadResult, download)
+            }
         }
     }
 

+ 1 - 1
app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt

@@ -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()
         )

+ 0 - 1
app/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -1585,7 +1585,6 @@ public class FileDisplayActivity extends FileActivity
         return new ListServiceConnection();
     }
 
-    // FIXME ServiceConnection will not trigger anymore
     /**
      * Defines callbacks for service binding, passed to bindService()
      */

+ 2 - 0
app/src/main/res/values/strings.xml

@@ -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>