Skip to content

Commit

Permalink
Merge pull request #12264 from nextcloud/backport/12246/stable-3.27
Browse files Browse the repository at this point in the history
[stable-3.27] Fix: To many old notifications
  • Loading branch information
AndyScherzinger authored Dec 11, 2023
2 parents 27f47bb + 5672234 commit 72bcee0
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 4 deletions.
33 changes: 29 additions & 4 deletions app/src/main/java/com/nextcloud/client/jobs/FilesUploadWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
)
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -781,6 +782,7 @@ public void onTransferProgress(
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
mNotificationManager.notify(FOREGROUND_SERVICE_ID, mNotificationBuilder.build());
cancelOldErrorNotification(mCurrentUpload);
}
mLastPercent = percent;
}
Expand All @@ -799,6 +801,10 @@ private void notifyUploadResult(UploadFileOperation upload, RemoteOperationResul
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}

if (uploadResult.isSuccess()){
cancelOldErrorNotification(upload);
}

// Only notify if the upload fails
if (!uploadResult.isCancelled() &&
!uploadResult.isSuccess() &&
Expand Down Expand Up @@ -1436,6 +1442,26 @@ public static String buildRemoteName(String accountName, String remotePath) {
}
}

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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
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;
Expand Down Expand Up @@ -263,6 +264,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
openDrawer();
}
} else if (itemId == R.id.action_clear_failed_uploads) {
for (OCUpload upload : uploadsStorageManager.getFailedButNotDelayedUploadsForCurrentAccount()){
uploadListAdapter.cancelOldErrorNotification(upload);
}
uploadsStorageManager.clearFailedButNotDelayedUploads();
uploadListAdapter.loadUploadItemsFromDb();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,6 +43,7 @@
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;
Expand All @@ -63,6 +65,7 @@
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;
Expand All @@ -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;
Expand Down Expand Up @@ -556,6 +560,7 @@ private void showItemConflictPopup(User user,

private void removeUpload(OCUpload item) {
uploadsStorageManager.removeUpload(item);
cancelOldErrorNotification(item);
loadUploadItemsFromDb();
}

Expand Down Expand Up @@ -873,4 +878,17 @@ private int getGroupItemCount() {
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);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;
Expand Down Expand Up @@ -81,4 +82,11 @@ public static void cancelWithDelay(final NotificationManager notificationManager
((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;
}
}

0 comments on commit 72bcee0

Please sign in to comment.