Selaa lähdekoodia

Merge pull request #12246 from nextcloud/10571-allow-to-dismiss-all-grouped-upload-failed-notifications

Fix: To many old notifications
Alper Öztürk 1 vuosi sitten
vanhempi
commit
63ec726dbc

+ 29 - 4
app/src/main/java/com/nextcloud/client/jobs/FilesUploadWorker.kt

@@ -261,8 +261,14 @@ class FilesUploadWorker(
         uploadResult: RemoteOperationResult<Any?>
     ) {
         Log_OC.d(TAG, "NotifyUploadResult with resultCode: " + uploadResult.code)
+
+        if (uploadResult.isSuccess) {
+            cancelOldErrorNotification(uploadFileOperation)
+            return
+        }
+
         // Only notify if the upload fails
-        if (uploadResult.isSuccess || uploadResult.isCancelled) {
+        if (uploadResult.isCancelled) {
             return
         }
 
@@ -312,9 +318,12 @@ class FilesUploadWorker(
                 )
             }
             notificationBuilder.setContentText(content)
-            if (!uploadResult.isSuccess) {
-                notificationManager.notify(SecureRandom().nextInt(), notificationBuilder.build())
-            }
+
+            notificationManager.notify(
+                NotificationUtils.createUploadNotificationTag(uploadFileOperation.file),
+                NOTIFICATION_ERROR_ID,
+                notificationBuilder.build()
+            )
         }
     }
 
@@ -386,10 +395,25 @@ class FilesUploadWorker(
                 totalToTransfer,
                 fileAbsoluteName
             )
+            currentUploadFileOperation?.let { cancelOldErrorNotification(it) }
         }
         lastPercent = percent
     }
 
+    private fun cancelOldErrorNotification(uploadFileOperation: UploadFileOperation) {
+        // cancel for old file because of file conflicts
+        if (uploadFileOperation.oldFile != null) {
+            notificationManager.cancel(
+                NotificationUtils.createUploadNotificationTag(uploadFileOperation.oldFile),
+                NOTIFICATION_ERROR_ID
+            )
+        }
+        notificationManager.cancel(
+            NotificationUtils.createUploadNotificationTag(uploadFileOperation.file),
+            NOTIFICATION_ERROR_ID
+        )
+    }
+
     override fun onStopped() {
         super.onStopped()
         currentUploadFileOperation?.cancel(null)
@@ -399,6 +423,7 @@ class FilesUploadWorker(
     companion object {
         val TAG: String = FilesUploadWorker::class.java.simpleName
         private const val FOREGROUND_SERVICE_ID: Int = 412
+        const val NOTIFICATION_ERROR_ID: Int = 413
         private const val MAX_PROGRESS: Int = 100
         const val ACCOUNT = "data_account"
         var currentUploadFileOperation: UploadFileOperation? = null

+ 26 - 0
app/src/main/java/com/owncloud/android/files/services/FileUploader.java

@@ -135,6 +135,7 @@ public class FileUploader extends Service
     public static final String ACTION_PAUSE_BROADCAST = "PAUSE";
 
     private static final int FOREGROUND_SERVICE_ID = 411;
+    private static final int NOTIFICATION_ERROR_ID = FilesUploadWorker.NOTIFICATION_ERROR_ID;
 
     public static final String KEY_FILE = "FILE";
     public static final String KEY_LOCAL_FILE = "LOCAL_FILE";
@@ -781,6 +782,7 @@ public class FileUploader extends Service
                 mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
             }
             mNotificationManager.notify(FOREGROUND_SERVICE_ID, mNotificationBuilder.build());
+            cancelOldErrorNotification(mCurrentUpload);
         }
         mLastPercent = percent;
     }
@@ -799,6 +801,10 @@ public class FileUploader extends Service
             mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
         }
 
+        if (uploadResult.isSuccess()){
+            cancelOldErrorNotification(upload);
+        }
+
         // Only notify if the upload fails
         if (!uploadResult.isCancelled() &&
             !uploadResult.isSuccess() &&
@@ -1436,6 +1442,26 @@ public class FileUploader extends Service
         }
     }
 
+    private void cancelOldErrorNotification(UploadFileOperation uploadFileOperation){
+        if (mNotificationManager == null) {
+            mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
+        }
+
+        if (uploadFileOperation == null) return;
+
+        mNotificationManager.cancel(NotificationUtils.createUploadNotificationTag(uploadFileOperation.getFile()),
+                                    NOTIFICATION_ERROR_ID);
+
+        //cancel for old file because of file conflicts
+        OCFile oldFile = uploadFileOperation.getOldFile();
+        if ( oldFile != null) {
+            mNotificationManager.cancel(NotificationUtils.createUploadNotificationTag(oldFile),
+                                        NOTIFICATION_ERROR_ID);
+        }
+
+
+    }
+
 
     /**
      * Upload worker. Performs the pending uploads in the order they were requested.

+ 4 - 0
app/src/main/java/com/owncloud/android/ui/activity/UploadListActivity.java

@@ -48,6 +48,7 @@ import com.owncloud.android.R;
 import com.owncloud.android.databinding.UploadListLayoutBinding;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.UploadsStorageManager;
+import com.owncloud.android.db.OCUpload;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.lib.common.operations.RemoteOperation;
@@ -263,6 +264,9 @@ public class UploadListActivity extends FileActivity {
                 openDrawer();
             }
         } else if (itemId == R.id.action_clear_failed_uploads) {
+            for (OCUpload upload : uploadsStorageManager.getFailedButNotDelayedUploadsForCurrentAccount()){
+                uploadListAdapter.cancelOldErrorNotification(upload);
+            }
             uploadsStorageManager.clearFailedButNotDelayedUploads();
             uploadListAdapter.loadUploadItemsFromDb();
         } else {

+ 18 - 0
app/src/main/java/com/owncloud/android/ui/adapter/UploadListAdapter.java

@@ -24,6 +24,7 @@
 
 package com.owncloud.android.ui.adapter;
 
+import android.app.NotificationManager;
 import android.content.ActivityNotFoundException;
 import android.content.Context;
 import android.content.Intent;
@@ -42,6 +43,7 @@ import com.nextcloud.client.account.User;
 import com.nextcloud.client.account.UserAccountManager;
 import com.nextcloud.client.core.Clock;
 import com.nextcloud.client.device.PowerManagementService;
+import com.nextcloud.client.jobs.FilesUploadWorker;
 import com.nextcloud.client.network.ConnectivityService;
 import com.nextcloud.java.util.Optional;
 import com.owncloud.android.MainApp;
@@ -63,6 +65,7 @@ import com.owncloud.android.operations.RefreshFolderOperation;
 import com.owncloud.android.ui.activity.ConflictsResolveActivity;
 import com.owncloud.android.ui.activity.FileActivity;
 import com.owncloud.android.ui.activity.FileDisplayActivity;
+import com.owncloud.android.ui.notifications.NotificationUtils;
 import com.owncloud.android.ui.preview.PreviewImageFragment;
 import com.owncloud.android.utils.DisplayUtils;
 import com.owncloud.android.utils.MimeTypeUtil;
@@ -86,6 +89,7 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
     private ConnectivityService connectivityService;
     private PowerManagementService powerManagementService;
     private UserAccountManager accountManager;
+    private NotificationManager mNotificationManager;
     private Clock clock;
     private UploadGroup[] uploadGroups;
     private boolean showUser;
@@ -556,6 +560,7 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
 
     private void removeUpload(OCUpload item) {
         uploadsStorageManager.removeUpload(item);
+        cancelOldErrorNotification(item);
         loadUploadItemsFromDb();
     }
 
@@ -873,4 +878,17 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
             return items == null ? 0 : items.length;
         }
     }
+
+    public void cancelOldErrorNotification(OCUpload upload){
+
+        if (mNotificationManager == null) {
+            mNotificationManager = (NotificationManager) parentActivity.getSystemService(parentActivity.NOTIFICATION_SERVICE);
+        }
+
+        if (upload == null) return;
+        mNotificationManager.cancel(NotificationUtils.createUploadNotificationTag(upload.getRemotePath(),upload.getLocalPath()),
+                                    FilesUploadWorker.NOTIFICATION_ERROR_ID);
+
+    }
+
 }

+ 8 - 0
app/src/main/java/com/owncloud/android/ui/notifications/NotificationUtils.java

@@ -25,6 +25,7 @@ import android.os.Handler;
 import android.os.HandlerThread;
 import android.os.Process;
 
+import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.utils.theme.ViewThemeUtils;
 
 import java.security.SecureRandom;
@@ -81,4 +82,11 @@ public final class NotificationUtils {
             ((HandlerThread) Thread.currentThread()).getLooper().quit();
         }, delayInMillis);
     }
+
+    public static String createUploadNotificationTag(OCFile file){
+        return createUploadNotificationTag(file.getRemotePath(), file.getStoragePath());
+    }
+    public static String createUploadNotificationTag(String remotePath, String localPath){
+        return remotePath + localPath;
+    }
 }