@@ -1127,6 +1127,16 @@ private MultipartBody.Part prepareFilePart(String partName, final File file, int
1127
1127
private NotificationManager mNotificationManager ;
1128
1128
private NotificationCompat .Builder mBuilder ;
1129
1129
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
+
1130
1140
/**
1131
1141
* Modifies notification {@link Notifications} of specifies {@param notificationId}
1132
1142
* and sets its {@param content}.
@@ -1142,6 +1152,7 @@ private void setNotification(int notificationId, String content) {
1142
1152
.setOngoing (false )
1143
1153
// Removes the progress bar
1144
1154
.setProgress (0 , 0 , false );
1155
+ mBuilder .mActions .clear ();
1145
1156
PendingIntent contentIntent = PendingIntent .getActivity (
1146
1157
getApplicationContext (),
1147
1158
0 ,
@@ -1158,13 +1169,15 @@ private void setNotification(int notificationId, String content) {
1158
1169
* @param notificationId
1159
1170
* @param percentage
1160
1171
*/
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 ) {
1162
1173
mBuilder .setSmallIcon (android .R .drawable .stat_sys_upload )
1163
1174
.setContentTitle (title )
1164
1175
.setContentText (progress )
1165
1176
.setAutoCancel (false )
1166
1177
.setOngoing (true )
1167
1178
.setProgress (100 , percentage , false );
1179
+ mBuilder .mActions .clear ();
1180
+ mBuilder .addAction (R .drawable .ic_cancel_black_24dp , getString (R .string .cancel_content_desp ), pendingIntent );
1168
1181
mNotificationManager .notify (notificationId , mBuilder .build ());
1169
1182
}
1170
1183
@@ -1188,6 +1201,7 @@ private void endNotification(int notificationId, String content) {
1188
1201
new Intent (),
1189
1202
PendingIntent .FLAG_UPDATE_CURRENT );
1190
1203
mBuilder .setContentIntent (contentIntent );
1204
+ mBuilder .mActions .clear ();
1191
1205
mNotificationManager .notify (notificationId , mBuilder .build ());
1192
1206
}
1193
1207
@@ -1207,6 +1221,12 @@ private void uploadFile(final File file) {
1207
1221
// generate unique notification Id for this upload
1208
1222
final int notifId = (int ) (new Date ().getTime () % Integer .MAX_VALUE );
1209
1223
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
+
1210
1230
// update notification after every one second
1211
1231
final long startTime = System .currentTimeMillis ();
1212
1232
final int [] counter = {0 };
@@ -1215,7 +1235,7 @@ private void uploadFile(final File file) {
1215
1235
public void onProgressUpdate (int percentage , String progress , int notificationId ) {
1216
1236
if (System .currentTimeMillis () - startTime >= 1000 * counter [0 ]) {
1217
1237
// update notification
1218
- progressNotification (notificationId , percentage , progress , file .getName ());
1238
+ progressNotification (notificationId , percentage , progress , file .getName (), piAction );
1219
1239
counter [0 ]++;
1220
1240
}
1221
1241
}
@@ -1224,12 +1244,16 @@ public void onProgressUpdate(int percentage, String progress, int notificationId
1224
1244
// MultipartBody.Part is used to send also the actual file name
1225
1245
MultipartBody .Part body = prepareFilePart ("file" , file , notifId , progressListener );
1226
1246
1227
- // start notification
1228
- setNotification (notifId , getString (R .string .init_notif_title ) + getString (R .string .to_string ) + getNotifTitle ());
1229
-
1230
1247
// finally, execute the request
1231
1248
// create upload service client
1232
1249
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 ());
1233
1257
call .enqueue (new DefaultCallback <UploadResponse >() {
1234
1258
@ Override
1235
1259
public void onSuccess (Call <UploadResponse > call , Response <UploadResponse > response ) {
@@ -1250,8 +1274,13 @@ public void onSuccess(Call<UploadResponse> call, Response<UploadResponse> respon
1250
1274
} else {
1251
1275
endNotification (notifId , getString (R .string .failed_to_upload ));
1252
1276
}
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
+ }
1255
1284
}
1256
1285
1257
1286
@ Override
@@ -1268,6 +1297,10 @@ public void onFailure(Call<UploadResponse> call, Throwable t) {
1268
1297
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN_MR1 && isDestroyed ()) {
1269
1298
return ;
1270
1299
}
1300
+ if (call .isCanceled ()) {
1301
+ mNotificationManager .cancel (notifId );
1302
+ return ;
1303
+ }
1271
1304
endNotification (notifId , getString (R .string .failed_to_upload ));
1272
1305
mNotificationManager .cancel (notifId );
1273
1306
ZLog .logException (t );
@@ -2501,6 +2534,11 @@ protected void onDestroy() {
2501
2534
mNotificationManager = (NotificationManager ) this .getSystemService (Context .NOTIFICATION_SERVICE );
2502
2535
}
2503
2536
mNotificationManager .cancelAll ();
2537
+ if (mCancelHashMap != null ) {
2538
+ for (Call call : mCancelHashMap .values ()) {
2539
+ call .cancel ();
2540
+ }
2541
+ }
2504
2542
}
2505
2543
2506
2544
/**
0 commit comments