@@ -1127,6 +1127,16 @@ private MultipartBody.Part prepareFilePart(String partName, final File file, int
11271127 private NotificationManager mNotificationManager ;
11281128 private NotificationCompat .Builder mBuilder ;
11291129
1130+ private HashMap <Integer , Call <UploadResponse >> mCancelHashMap ;
1131+
1132+ public void cancelRequest (int notificationId ) throws NullPointerException {
1133+ Call <UploadResponse > call = mCancelHashMap .get (notificationId );
1134+ if (call != null ) {
1135+ call .cancel ();
1136+ mCancelHashMap .remove (notificationId );
1137+ }
1138+ }
1139+
11301140 /**
11311141 * Modifies notification {@link Notifications} of specifies {@param notificationId}
11321142 * and sets its {@param content}.
@@ -1142,6 +1152,7 @@ private void setNotification(int notificationId, String content) {
11421152 .setOngoing (false )
11431153 // Removes the progress bar
11441154 .setProgress (0 , 0 , false );
1155+ mBuilder .mActions .clear ();
11451156 PendingIntent contentIntent = PendingIntent .getActivity (
11461157 getApplicationContext (),
11471158 0 ,
@@ -1158,13 +1169,15 @@ private void setNotification(int notificationId, String content) {
11581169 * @param notificationId
11591170 * @param percentage
11601171 */
1161- private void progressNotification (int notificationId , int percentage , String progress , String title ) {
1172+ private void progressNotification (int notificationId , int percentage , String progress , String title , PendingIntent pendingIntent ) {
11621173 mBuilder .setSmallIcon (android .R .drawable .stat_sys_upload )
11631174 .setContentTitle (title )
11641175 .setContentText (progress )
11651176 .setAutoCancel (false )
11661177 .setOngoing (true )
11671178 .setProgress (100 , percentage , false );
1179+ mBuilder .mActions .clear ();
1180+ mBuilder .addAction (R .drawable .ic_cancel_black_24dp , getString (R .string .cancel_content_desp ), pendingIntent );
11681181 mNotificationManager .notify (notificationId , mBuilder .build ());
11691182 }
11701183
@@ -1188,6 +1201,7 @@ private void endNotification(int notificationId, String content) {
11881201 new Intent (),
11891202 PendingIntent .FLAG_UPDATE_CURRENT );
11901203 mBuilder .setContentIntent (contentIntent );
1204+ mBuilder .mActions .clear ();
11911205 mNotificationManager .notify (notificationId , mBuilder .build ());
11921206 }
11931207
@@ -1207,6 +1221,12 @@ private void uploadFile(final File file) {
12071221 // generate unique notification Id for this upload
12081222 final int notifId = (int ) (new Date ().getTime () % Integer .MAX_VALUE );
12091223
1224+ // cancel pending intent
1225+ Intent actionIntent = new Intent (this , GcmBroadcastReceiver .class );
1226+ actionIntent .putExtra ("id" , notifId );
1227+ actionIntent .setAction (Constants .CANCEL );
1228+ final PendingIntent piAction = PendingIntent .getBroadcast (this , notifId , actionIntent , PendingIntent .FLAG_UPDATE_CURRENT );
1229+
12101230 // update notification after every one second
12111231 final long startTime = System .currentTimeMillis ();
12121232 final int [] counter = {0 };
@@ -1215,7 +1235,7 @@ private void uploadFile(final File file) {
12151235 public void onProgressUpdate (int percentage , String progress , int notificationId ) {
12161236 if (System .currentTimeMillis () - startTime >= 1000 * counter [0 ]) {
12171237 // update notification
1218- progressNotification (notificationId , percentage , progress , file .getName ());
1238+ progressNotification (notificationId , percentage , progress , file .getName (), piAction );
12191239 counter [0 ]++;
12201240 }
12211241 }
@@ -1224,12 +1244,16 @@ public void onProgressUpdate(int percentage, String progress, int notificationId
12241244 // MultipartBody.Part is used to send also the actual file name
12251245 MultipartBody .Part body = prepareFilePart ("file" , file , notifId , progressListener );
12261246
1227- // start notification
1228- setNotification (notifId , getString (R .string .init_notif_title ) + getString (R .string .to_string ) + getNotifTitle ());
1229-
12301247 // finally, execute the request
12311248 // create upload service client
12321249 Call <UploadResponse > call = ((ZulipApp ) getApplicationContext ()).getUploadServices ().upload (body );
1250+ if (mCancelHashMap == null ) {
1251+ mCancelHashMap = new HashMap <>();
1252+ }
1253+ mCancelHashMap .put (notifId , call );
1254+ Toast .makeText (this , R .string .upload_started_str , Toast .LENGTH_SHORT ).show ();
1255+ // start notification
1256+ setNotification (notifId , getString (R .string .init_notif_title ) + getString (R .string .to_string ) + getNotifTitle ());
12331257 call .enqueue (new DefaultCallback <UploadResponse >() {
12341258 @ Override
12351259 public void onSuccess (Call <UploadResponse > call , Response <UploadResponse > response ) {
@@ -1250,8 +1274,13 @@ public void onSuccess(Call<UploadResponse> call, Response<UploadResponse> respon
12501274 } else {
12511275 endNotification (notifId , getString (R .string .failed_to_upload ));
12521276 }
1253- mNotificationManager .cancel (notifId );
1254-
1277+ if (!(mCancelHashMap != null && mCancelHashMap .size () == 1 )) {
1278+ mNotificationManager .cancel (notifId );
1279+ }
1280+ if (mCancelHashMap != null ) {
1281+ mCancelHashMap .remove (notifId );
1282+ mCancelHashMap = mCancelHashMap .isEmpty () ? null : mCancelHashMap ;
1283+ }
12551284 }
12561285
12571286 @ Override
@@ -1268,6 +1297,10 @@ public void onFailure(Call<UploadResponse> call, Throwable t) {
12681297 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN_MR1 && isDestroyed ()) {
12691298 return ;
12701299 }
1300+ if (call .isCanceled ()) {
1301+ mNotificationManager .cancel (notifId );
1302+ return ;
1303+ }
12711304 endNotification (notifId , getString (R .string .failed_to_upload ));
12721305 mNotificationManager .cancel (notifId );
12731306 ZLog .logException (t );
@@ -2501,6 +2534,11 @@ protected void onDestroy() {
25012534 mNotificationManager = (NotificationManager ) this .getSystemService (Context .NOTIFICATION_SERVICE );
25022535 }
25032536 mNotificationManager .cancelAll ();
2537+ if (mCancelHashMap != null ) {
2538+ for (Call call : mCancelHashMap .values ()) {
2539+ call .cancel ();
2540+ }
2541+ }
25042542 }
25052543
25062544 /**
0 commit comments