34
34
import android .app .NotificationManager ;
35
35
import android .app .PendingIntent ;
36
36
import android .app .Service ;
37
+ import android .content .BroadcastReceiver ;
37
38
import android .content .Context ;
38
39
import android .content .Intent ;
39
40
import android .content .pm .ServiceInfo ;
@@ -128,6 +129,10 @@ public class FileUploader extends Service
128
129
public static final String EXTRA_LINKED_TO_PATH = "LINKED_TO" ;
129
130
public static final String ACCOUNT_NAME = "ACCOUNT_NAME" ;
130
131
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
+
131
136
private static final int FOREGROUND_SERVICE_ID = 411 ;
132
137
133
138
public static final String KEY_FILE = "FILE" ;
@@ -198,11 +203,13 @@ public class FileUploader extends Service
198
203
private Notification mNotification ;
199
204
private Looper mServiceLooper ;
200
205
private ServiceHandler mServiceHandler ;
201
- private IBinder mBinder ;
206
+ private static IBinder mBinder ;
202
207
private OwnCloudClient mUploadClient ;
203
208
private Account mCurrentAccount ;
204
209
private FileDataStorageManager mStorageManager ;
205
210
211
+ private SecureRandom secureRandomGenerator = new SecureRandom ();
212
+
206
213
@ Inject UserAccountManager accountManager ;
207
214
@ Inject UploadsStorageManager mUploadsStorageManager ;
208
215
@ Inject ConnectivityService connectivityService ;
@@ -233,6 +240,7 @@ public void onRenameUpload() {
233
240
/**
234
241
* Service initialization
235
242
*/
243
+ @ SuppressFBWarnings ("ST" )
236
244
@ Override
237
245
public void onCreate () {
238
246
super .onCreate ();
@@ -280,6 +288,7 @@ private void resurrection() {
280
288
/**
281
289
* Service clean up
282
290
*/
291
+ @ SuppressFBWarnings ("ST" )
283
292
@ Override
284
293
public void onDestroy () {
285
294
Log_OC .v (TAG , "Destroying service" );
@@ -708,6 +717,12 @@ private Optional<User> getCurrentUser() {
708
717
*/
709
718
private void notifyUploadStart (UploadFileOperation upload ) {
710
719
// / 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 );
711
726
mLastPercent = 0 ;
712
727
mNotificationBuilder = NotificationUtils .newNotificationBuilder (this , viewThemeUtils );
713
728
mNotificationBuilder
@@ -718,7 +733,10 @@ private void notifyUploadStart(UploadFileOperation upload) {
718
733
.setProgress (100 , 0 , false )
719
734
.setContentText (
720
735
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
+
722
740
723
741
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
724
742
mNotificationBuilder .setChannelId (NotificationUtils .NOTIFICATION_CHANNEL_UPLOAD );
@@ -811,7 +829,8 @@ private void notifyUploadResult(UploadFileOperation upload, RemoteOperationResul
811
829
.setContentTitle (getString (tickerId ))
812
830
.setAutoCancel (true )
813
831
.setOngoing (false )
814
- .setProgress (0 , 0 , false );
832
+ .setProgress (0 , 0 , false )
833
+ .clearActions ();
815
834
816
835
content = ErrorMessageAdapter .getErrorCauseMessage (uploadResult , upload , getResources ());
817
836
@@ -859,7 +878,7 @@ private void notifyUploadResult(UploadFileOperation upload, RemoteOperationResul
859
878
860
879
mNotificationBuilder .setContentText (content );
861
880
if (!uploadResult .isSuccess ()) {
862
- mNotificationManager .notify (( new SecureRandom ()) .nextInt (), mNotificationBuilder .build ());
881
+ mNotificationManager .notify (secureRandomGenerator .nextInt (), mNotificationBuilder .build ());
863
882
}
864
883
}
865
884
}
@@ -1408,4 +1427,32 @@ public void handleMessage(Message msg) {
1408
1427
mService .stopSelf (msg .arg1 );
1409
1428
}
1410
1429
}
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
+ }
1411
1458
}
0 commit comments