Skip to content

Commit

Permalink
Merge pull request #12213 from nextcloud/feature/add_cancel_button_to…
Browse files Browse the repository at this point in the history
…_upload_notification

Feature/Add cancel button to upload notification
  • Loading branch information
alperozturk96 authored Dec 4, 2023
2 parents 11973d6 + 32d1773 commit 727a56b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@
<receiver
android:name="com.nextcloud.client.jobs.NotificationWork$NotificationReceiver"
android:exported="false" />
<receiver
android:name="com.owncloud.android.files.services.FileUploader$UploadNotificationActionReceiver"
android:exported="false" />
<receiver
android:name="com.nextcloud.client.widget.DashboardWidgetProvider"
android:exported="false">
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/com/nextcloud/client/jobs/FilesUploadWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import com.owncloud.android.datamodel.FileDataStorageManager
import com.owncloud.android.datamodel.ThumbnailsCacheManager
import com.owncloud.android.datamodel.UploadsStorageManager
import com.owncloud.android.db.OCUpload
import com.owncloud.android.files.services.FileUploader
import com.owncloud.android.lib.common.OwnCloudAccount
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener
Expand Down Expand Up @@ -197,6 +198,18 @@ class FilesUploadWorker(
* adapted from [com.owncloud.android.files.services.FileUploader.notifyUploadStart]
*/
private fun createNotification(uploadFileOperation: UploadFileOperation) {
val notificationActionIntent = Intent(context, FileUploader.UploadNotificationActionReceiver::class.java)
notificationActionIntent.putExtra(FileUploader.EXTRA_ACCOUNT_NAME, uploadFileOperation.user.accountName)
notificationActionIntent.putExtra(FileUploader.EXTRA_REMOTE_PATH, uploadFileOperation.remotePath)
notificationActionIntent.action = FileUploader.ACTION_CANCEL_BROADCAST

val pendingIntent = PendingIntent.getBroadcast(
context,
SecureRandom().nextInt(),
notificationActionIntent,
PendingIntent.FLAG_IMMUTABLE
)

notificationBuilder
.setOngoing(true)
.setSmallIcon(R.drawable.notification_icon)
Expand All @@ -209,6 +222,8 @@ class FilesUploadWorker(
uploadFileOperation.fileName
)
)
.clearActions() // to make sure there is only one action
.addAction(R.drawable.ic_action_cancel_grey, context.getString(R.string.common_cancel), pendingIntent)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationBuilder.setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_UPLOAD)
Expand Down Expand Up @@ -275,6 +290,7 @@ class FilesUploadWorker(
.setAutoCancel(true)
.setOngoing(false)
.setProgress(0, 0, false)
.clearActions()

val content = ErrorMessageAdapter.getErrorCauseMessage(uploadResult, uploadFileOperation, context.resources)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ServiceInfo;
Expand Down Expand Up @@ -128,6 +129,10 @@ public class FileUploader extends Service
public static final String EXTRA_LINKED_TO_PATH = "LINKED_TO";
public static final String ACCOUNT_NAME = "ACCOUNT_NAME";

public static final String EXTRA_ACCOUNT_NAME = "ACCOUNT_NAME";
public static final String ACTION_CANCEL_BROADCAST = "CANCEL";
public static final String ACTION_PAUSE_BROADCAST = "PAUSE";

private static final int FOREGROUND_SERVICE_ID = 411;

public static final String KEY_FILE = "FILE";
Expand Down Expand Up @@ -198,11 +203,13 @@ public class FileUploader extends Service
private Notification mNotification;
private Looper mServiceLooper;
private ServiceHandler mServiceHandler;
private IBinder mBinder;
private static IBinder mBinder;
private OwnCloudClient mUploadClient;
private Account mCurrentAccount;
private FileDataStorageManager mStorageManager;

private SecureRandom secureRandomGenerator = new SecureRandom();

@Inject UserAccountManager accountManager;
@Inject UploadsStorageManager mUploadsStorageManager;
@Inject ConnectivityService connectivityService;
Expand Down Expand Up @@ -233,6 +240,7 @@ public void onRenameUpload() {
/**
* Service initialization
*/
@SuppressFBWarnings("ST")
@Override
public void onCreate() {
super.onCreate();
Expand Down Expand Up @@ -280,6 +288,7 @@ private void resurrection() {
/**
* Service clean up
*/
@SuppressFBWarnings("ST")
@Override
public void onDestroy() {
Log_OC.v(TAG, "Destroying service");
Expand Down Expand Up @@ -708,6 +717,12 @@ private Optional<User> getCurrentUser() {
*/
private void notifyUploadStart(UploadFileOperation upload) {
// / create status notification with a progress bar
Intent notificationActionIntent = new Intent(getApplicationContext(),UploadNotificationActionReceiver.class);
notificationActionIntent.putExtra(EXTRA_ACCOUNT_NAME,upload.getUser().getAccountName());
notificationActionIntent.putExtra(EXTRA_REMOTE_PATH,upload.getRemotePath());
notificationActionIntent.setAction(ACTION_CANCEL_BROADCAST);

PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),secureRandomGenerator.nextInt(),notificationActionIntent, PendingIntent.FLAG_IMMUTABLE);
mLastPercent = 0;
mNotificationBuilder = NotificationUtils.newNotificationBuilder(this, viewThemeUtils);
mNotificationBuilder
Expand All @@ -718,7 +733,10 @@ private void notifyUploadStart(UploadFileOperation upload) {
.setProgress(100, 0, false)
.setContentText(
String.format(getString(R.string.uploader_upload_in_progress_content), 0, upload.getFileName())
);
)
.clearActions() // to make sure there is only one action
.addAction(R.drawable.ic_action_cancel_grey,getApplicationContext().getString(R.string.common_cancel),pendingIntent);


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationBuilder.setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_UPLOAD);
Expand Down Expand Up @@ -811,7 +829,8 @@ private void notifyUploadResult(UploadFileOperation upload, RemoteOperationResul
.setContentTitle(getString(tickerId))
.setAutoCancel(true)
.setOngoing(false)
.setProgress(0, 0, false);
.setProgress(0, 0, false)
.clearActions();

content = ErrorMessageAdapter.getErrorCauseMessage(uploadResult, upload, getResources());

Expand Down Expand Up @@ -859,7 +878,7 @@ private void notifyUploadResult(UploadFileOperation upload, RemoteOperationResul

mNotificationBuilder.setContentText(content);
if (!uploadResult.isSuccess()) {
mNotificationManager.notify((new SecureRandom()).nextInt(), mNotificationBuilder.build());
mNotificationManager.notify(secureRandomGenerator.nextInt(), mNotificationBuilder.build());
}
}
}
Expand Down Expand Up @@ -1408,4 +1427,32 @@ public void handleMessage(Message msg) {
mService.stopSelf(msg.arg1);
}
}


/**
* When cancel action in upload notification is pressed, cancel upload of item
*/
public static class UploadNotificationActionReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

String accountName = intent.getStringExtra(EXTRA_ACCOUNT_NAME);
String remotePath = intent.getStringExtra(EXTRA_REMOTE_PATH);
String action = intent.getAction();

if (ACTION_CANCEL_BROADCAST.equals(action)) {
Log_OC.d(TAG, "Cancel broadcast received for file " + remotePath + " at " + System.currentTimeMillis());

if (accountName == null || remotePath == null) return;

FileUploaderBinder uploadBinder = (FileUploaderBinder) mBinder;
uploadBinder.cancel(accountName, remotePath, null);
}else if(ACTION_PAUSE_BROADCAST.equals(action)){

} else {
Log_OC.d(TAG, "Unknown action to perform as UploadNotificationActionReceiver.");
}
}
}
}

0 comments on commit 727a56b

Please sign in to comment.