Browse Source

make failed notification go away when uploading makes progress

Signed-off-by: Jonas Mayer <jonas.a.mayer@gmx.net>
Jonas Mayer 1 year ago
parent
commit
c8b3f6fd3c

+ 20 - 5
app/src/main/java/com/nextcloud/client/jobs/FilesUploadWorker.kt

@@ -59,7 +59,6 @@ import com.owncloud.android.utils.ErrorMessageAdapter
 import com.owncloud.android.utils.FilesUploadHelper
 import com.owncloud.android.utils.FilesUploadHelper
 import com.owncloud.android.utils.theme.ViewThemeUtils
 import com.owncloud.android.utils.theme.ViewThemeUtils
 import java.io.File
 import java.io.File
-import java.security.SecureRandom
 
 
 @Suppress("LongParameterList")
 @Suppress("LongParameterList")
 class FilesUploadWorker(
 class FilesUploadWorker(
@@ -261,8 +260,14 @@ class FilesUploadWorker(
         uploadResult: RemoteOperationResult<Any?>
         uploadResult: RemoteOperationResult<Any?>
     ) {
     ) {
         Log_OC.d(TAG, "NotifyUploadResult with resultCode: " + uploadResult.code)
         Log_OC.d(TAG, "NotifyUploadResult with resultCode: " + uploadResult.code)
+
+        if (uploadResult.isSuccess){
+            cancelOldErrorNotification(uploadFileOperation)
+            return
+        }
+
         // Only notify if the upload fails
         // Only notify if the upload fails
-        if (uploadResult.isSuccess || uploadResult.isCancelled) {
+        if (uploadResult.isCancelled) {
             return
             return
         }
         }
 
 
@@ -312,9 +317,10 @@ class FilesUploadWorker(
                 )
                 )
             }
             }
             notificationBuilder.setContentText(content)
             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 +392,18 @@ class FilesUploadWorker(
                 totalToTransfer,
                 totalToTransfer,
                 fileAbsoluteName
                 fileAbsoluteName
             )
             )
+            currentUploadFileOperation?.let { cancelOldErrorNotification(it) }
         }
         }
         lastPercent = percent
         lastPercent = percent
     }
     }
 
 
+    private fun cancelOldErrorNotification(uploadFileOperation: UploadFileOperation){
+        notificationManager.cancel(NotificationUtils.createUploadNotificationTag(uploadFileOperation.oldFile),
+            NOTIFICATION_ERROR_ID)
+        notificationManager.cancel(NotificationUtils.createUploadNotificationTag(uploadFileOperation.file),
+            NOTIFICATION_ERROR_ID)
+    }
+
     override fun onStopped() {
     override fun onStopped() {
         super.onStopped()
         super.onStopped()
         currentUploadFileOperation?.cancel(null)
         currentUploadFileOperation?.cancel(null)
@@ -399,6 +413,7 @@ class FilesUploadWorker(
     companion object {
     companion object {
         val TAG: String = FilesUploadWorker::class.java.simpleName
         val TAG: String = FilesUploadWorker::class.java.simpleName
         private const val FOREGROUND_SERVICE_ID: Int = 412
         private const val FOREGROUND_SERVICE_ID: Int = 412
+        private const val NOTIFICATION_ERROR_ID: Int = 413
         private const val MAX_PROGRESS: Int = 100
         private const val MAX_PROGRESS: Int = 100
         const val ACCOUNT = "data_account"
         const val ACCOUNT = "data_account"
         var currentUploadFileOperation: UploadFileOperation? = null
         var currentUploadFileOperation: UploadFileOperation? = null

+ 14 - 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";
     public static final String ACTION_PAUSE_BROADCAST = "PAUSE";
 
 
     private static final int FOREGROUND_SERVICE_ID = 411;
     private static final int FOREGROUND_SERVICE_ID = 411;
+    private static final int NOTIFICATION_ERROR_ID = 410;
 
 
     public static final String KEY_FILE = "FILE";
     public static final String KEY_FILE = "FILE";
     public static final String KEY_LOCAL_FILE = "LOCAL_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 = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
             }
             }
             mNotificationManager.notify(FOREGROUND_SERVICE_ID, mNotificationBuilder.build());
             mNotificationManager.notify(FOREGROUND_SERVICE_ID, mNotificationBuilder.build());
+            cancelOldErrorNotification(mCurrentUpload);
         }
         }
         mLastPercent = percent;
         mLastPercent = percent;
     }
     }
@@ -799,6 +801,10 @@ public class FileUploader extends Service
             mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
             mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
         }
         }
 
 
+        if (uploadResult.isSuccess()){
+            cancelOldErrorNotification(upload);
+        }
+
         // Only notify if the upload fails
         // Only notify if the upload fails
         if (!uploadResult.isCancelled() &&
         if (!uploadResult.isCancelled() &&
             !uploadResult.isSuccess() &&
             !uploadResult.isSuccess() &&
@@ -1436,6 +1442,14 @@ public class FileUploader extends Service
         }
         }
     }
     }
 
 
+    private void cancelOldErrorNotification(UploadFileOperation uploadFileOperation){
+        if (uploadFileOperation == null) return;
+        mNotificationManager.cancel(NotificationUtils.createUploadNotificationTag(uploadFileOperation.getOldFile()),
+                                   NOTIFICATION_ERROR_ID);
+        mNotificationManager.cancel(NotificationUtils.createUploadNotificationTag(uploadFileOperation.getFile()),
+                                   NOTIFICATION_ERROR_ID);
+    }
+
 
 
     /**
     /**
      * Upload worker. Performs the pending uploads in the order they were requested.
      * Upload worker. Performs the pending uploads in the order they were requested.

+ 5 - 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.HandlerThread;
 import android.os.Process;
 import android.os.Process;
 
 
+import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.utils.theme.ViewThemeUtils;
 import com.owncloud.android.utils.theme.ViewThemeUtils;
 
 
 import java.security.SecureRandom;
 import java.security.SecureRandom;
@@ -81,4 +82,8 @@ public final class NotificationUtils {
             ((HandlerThread) Thread.currentThread()).getLooper().quit();
             ((HandlerThread) Thread.currentThread()).getLooper().quit();
         }, delayInMillis);
         }, delayInMillis);
     }
     }
+
+    public static String createUploadNotificationTag(OCFile file){
+        return file.getRemotePath() + file.getStoragePath();
+    }
 }
 }