3434import android .app .NotificationManager ;
3535import android .app .PendingIntent ;
3636import android .app .Service ;
37+ import android .content .BroadcastReceiver ;
3738import android .content .Context ;
3839import android .content .Intent ;
3940import 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