Skip to content

Commit 727a56b

Browse files
Merge pull request #12213 from nextcloud/feature/add_cancel_button_to_upload_notification
Feature/Add cancel button to upload notification
2 parents 11973d6 + 32d1773 commit 727a56b

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@
169169
<receiver
170170
android:name="com.nextcloud.client.jobs.NotificationWork$NotificationReceiver"
171171
android:exported="false" />
172+
<receiver
173+
android:name="com.owncloud.android.files.services.FileUploader$UploadNotificationActionReceiver"
174+
android:exported="false" />
172175
<receiver
173176
android:name="com.nextcloud.client.widget.DashboardWidgetProvider"
174177
android:exported="false">

app/src/main/java/com/nextcloud/client/jobs/FilesUploadWorker.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import com.owncloud.android.datamodel.FileDataStorageManager
4343
import com.owncloud.android.datamodel.ThumbnailsCacheManager
4444
import com.owncloud.android.datamodel.UploadsStorageManager
4545
import com.owncloud.android.db.OCUpload
46+
import com.owncloud.android.files.services.FileUploader
4647
import com.owncloud.android.lib.common.OwnCloudAccount
4748
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory
4849
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener
@@ -197,6 +198,18 @@ class FilesUploadWorker(
197198
* adapted from [com.owncloud.android.files.services.FileUploader.notifyUploadStart]
198199
*/
199200
private fun createNotification(uploadFileOperation: UploadFileOperation) {
201+
val notificationActionIntent = Intent(context, FileUploader.UploadNotificationActionReceiver::class.java)
202+
notificationActionIntent.putExtra(FileUploader.EXTRA_ACCOUNT_NAME, uploadFileOperation.user.accountName)
203+
notificationActionIntent.putExtra(FileUploader.EXTRA_REMOTE_PATH, uploadFileOperation.remotePath)
204+
notificationActionIntent.action = FileUploader.ACTION_CANCEL_BROADCAST
205+
206+
val pendingIntent = PendingIntent.getBroadcast(
207+
context,
208+
SecureRandom().nextInt(),
209+
notificationActionIntent,
210+
PendingIntent.FLAG_IMMUTABLE
211+
)
212+
200213
notificationBuilder
201214
.setOngoing(true)
202215
.setSmallIcon(R.drawable.notification_icon)
@@ -209,6 +222,8 @@ class FilesUploadWorker(
209222
uploadFileOperation.fileName
210223
)
211224
)
225+
.clearActions() // to make sure there is only one action
226+
.addAction(R.drawable.ic_action_cancel_grey, context.getString(R.string.common_cancel), pendingIntent)
212227

213228
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
214229
notificationBuilder.setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_UPLOAD)
@@ -275,6 +290,7 @@ class FilesUploadWorker(
275290
.setAutoCancel(true)
276291
.setOngoing(false)
277292
.setProgress(0, 0, false)
293+
.clearActions()
278294

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

app/src/main/java/com/owncloud/android/files/services/FileUploader.java

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import android.app.NotificationManager;
3535
import android.app.PendingIntent;
3636
import android.app.Service;
37+
import android.content.BroadcastReceiver;
3738
import android.content.Context;
3839
import android.content.Intent;
3940
import android.content.pm.ServiceInfo;
@@ -128,6 +129,10 @@ public class FileUploader extends Service
128129
public static final String EXTRA_LINKED_TO_PATH = "LINKED_TO";
129130
public static final String ACCOUNT_NAME = "ACCOUNT_NAME";
130131

132+
public static final String EXTRA_ACCOUNT_NAME = "ACCOUNT_NAME";
133+
public static final String ACTION_CANCEL_BROADCAST = "CANCEL";
134+
public static final String ACTION_PAUSE_BROADCAST = "PAUSE";
135+
131136
private static final int FOREGROUND_SERVICE_ID = 411;
132137

133138
public static final String KEY_FILE = "FILE";
@@ -198,11 +203,13 @@ public class FileUploader extends Service
198203
private Notification mNotification;
199204
private Looper mServiceLooper;
200205
private ServiceHandler mServiceHandler;
201-
private IBinder mBinder;
206+
private static IBinder mBinder;
202207
private OwnCloudClient mUploadClient;
203208
private Account mCurrentAccount;
204209
private FileDataStorageManager mStorageManager;
205210

211+
private SecureRandom secureRandomGenerator = new SecureRandom();
212+
206213
@Inject UserAccountManager accountManager;
207214
@Inject UploadsStorageManager mUploadsStorageManager;
208215
@Inject ConnectivityService connectivityService;
@@ -233,6 +240,7 @@ public void onRenameUpload() {
233240
/**
234241
* Service initialization
235242
*/
243+
@SuppressFBWarnings("ST")
236244
@Override
237245
public void onCreate() {
238246
super.onCreate();
@@ -280,6 +288,7 @@ private void resurrection() {
280288
/**
281289
* Service clean up
282290
*/
291+
@SuppressFBWarnings("ST")
283292
@Override
284293
public void onDestroy() {
285294
Log_OC.v(TAG, "Destroying service");
@@ -708,6 +717,12 @@ private Optional<User> getCurrentUser() {
708717
*/
709718
private void notifyUploadStart(UploadFileOperation upload) {
710719
// / create status notification with a progress bar
720+
Intent notificationActionIntent = new Intent(getApplicationContext(),UploadNotificationActionReceiver.class);
721+
notificationActionIntent.putExtra(EXTRA_ACCOUNT_NAME,upload.getUser().getAccountName());
722+
notificationActionIntent.putExtra(EXTRA_REMOTE_PATH,upload.getRemotePath());
723+
notificationActionIntent.setAction(ACTION_CANCEL_BROADCAST);
724+
725+
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),secureRandomGenerator.nextInt(),notificationActionIntent, PendingIntent.FLAG_IMMUTABLE);
711726
mLastPercent = 0;
712727
mNotificationBuilder = NotificationUtils.newNotificationBuilder(this, viewThemeUtils);
713728
mNotificationBuilder
@@ -718,7 +733,10 @@ private void notifyUploadStart(UploadFileOperation upload) {
718733
.setProgress(100, 0, false)
719734
.setContentText(
720735
String.format(getString(R.string.uploader_upload_in_progress_content), 0, upload.getFileName())
721-
);
736+
)
737+
.clearActions() // to make sure there is only one action
738+
.addAction(R.drawable.ic_action_cancel_grey,getApplicationContext().getString(R.string.common_cancel),pendingIntent);
739+
722740

723741
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
724742
mNotificationBuilder.setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_UPLOAD);
@@ -811,7 +829,8 @@ private void notifyUploadResult(UploadFileOperation upload, RemoteOperationResul
811829
.setContentTitle(getString(tickerId))
812830
.setAutoCancel(true)
813831
.setOngoing(false)
814-
.setProgress(0, 0, false);
832+
.setProgress(0, 0, false)
833+
.clearActions();
815834

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

@@ -859,7 +878,7 @@ private void notifyUploadResult(UploadFileOperation upload, RemoteOperationResul
859878

860879
mNotificationBuilder.setContentText(content);
861880
if (!uploadResult.isSuccess()) {
862-
mNotificationManager.notify((new SecureRandom()).nextInt(), mNotificationBuilder.build());
881+
mNotificationManager.notify(secureRandomGenerator.nextInt(), mNotificationBuilder.build());
863882
}
864883
}
865884
}
@@ -1408,4 +1427,32 @@ public void handleMessage(Message msg) {
14081427
mService.stopSelf(msg.arg1);
14091428
}
14101429
}
1430+
1431+
1432+
/**
1433+
* When cancel action in upload notification is pressed, cancel upload of item
1434+
*/
1435+
public static class UploadNotificationActionReceiver extends BroadcastReceiver {
1436+
1437+
@Override
1438+
public void onReceive(Context context, Intent intent) {
1439+
1440+
String accountName = intent.getStringExtra(EXTRA_ACCOUNT_NAME);
1441+
String remotePath = intent.getStringExtra(EXTRA_REMOTE_PATH);
1442+
String action = intent.getAction();
1443+
1444+
if (ACTION_CANCEL_BROADCAST.equals(action)) {
1445+
Log_OC.d(TAG, "Cancel broadcast received for file " + remotePath + " at " + System.currentTimeMillis());
1446+
1447+
if (accountName == null || remotePath == null) return;
1448+
1449+
FileUploaderBinder uploadBinder = (FileUploaderBinder) mBinder;
1450+
uploadBinder.cancel(accountName, remotePath, null);
1451+
}else if(ACTION_PAUSE_BROADCAST.equals(action)){
1452+
1453+
} else {
1454+
Log_OC.d(TAG, "Unknown action to perform as UploadNotificationActionReceiver.");
1455+
}
1456+
}
1457+
}
14111458
}

0 commit comments

Comments
 (0)