11package com .macbitsgoa .comrades .coursematerial ;
22
33import android .app .NotificationManager ;
4+ import android .app .PendingIntent ;
5+ import android .content .BroadcastReceiver ;
46import android .content .Context ;
7+ import android .content .Intent ;
8+ import android .content .IntentFilter ;
59import android .media .RingtoneManager ;
610import android .util .Log ;
711
1014import com .google .firebase .database .DatabaseReference ;
1115import com .macbitsgoa .comrades .R ;
1216import com .macbitsgoa .comrades .persistance .Database ;
17+ import com .squareup .okhttp .Call ;
1318import com .squareup .okhttp .MediaType ;
1419import com .squareup .okhttp .OkHttpClient ;
1520import com .squareup .okhttp .Request ;
2328import java .io .File ;
2429import java .io .IOException ;
2530import java .security .SecureRandom ;
31+ import java .util .Objects ;
2632import java .util .concurrent .TimeUnit ;
2733
2834import androidx .annotation .NonNull ;
@@ -58,6 +64,9 @@ public class Uploader extends Worker {
5864 public static final String KEY_FILE_NAME = "fileName" ;
5965 public static final String KEY_COURSE_ID = "courseId" ;
6066 private static final String TAG = TAG_PREFIX + Uploader .class .getSimpleName ();
67+ private static final String ACTION_CANCEL_UPLOAD = Uploader .class .getName () + ".action.cancel.upload" ;
68+ private static final String KEY_FILE_HASH = Uploader .class .getName () + ".key.fileHash" ;
69+ private static IntentFilter cancelUploadFilter = new IntentFilter (ACTION_CANCEL_UPLOAD );
6170 private String path ;
6271 private String accessToken ;
6372 private String fileName ;
@@ -68,6 +77,8 @@ public class Uploader extends Worker {
6877 private NotificationManager notificationManager ;
6978 private NotificationCompat .Builder builder ;
7079 private int notificationId ;
80+ private Call uploadCall ;
81+ private boolean cancelledByUser = false ;
7182
7283 /**
7384 * Handy method to access this class.
@@ -108,7 +119,6 @@ public Result doWork() {
108119 notificationManager = (NotificationManager )
109120 getApplicationContext ().getSystemService (Context .NOTIFICATION_SERVICE );
110121
111- notifyInit ();
112122 fileHash = calculateMD5 (new File (path ));
113123 CourseMaterial duplicate = findDuplicate ();
114124 if (duplicate != null ) {
@@ -118,6 +128,10 @@ public Result doWork() {
118128
119129 String response = uploadFile ();
120130 if (response == null ) {
131+ if (cancelledByUser ) {
132+ notificationManager .cancel (notificationId );
133+ return Result .SUCCESS ;
134+ }
121135 notifyFailure ();
122136 return Result .FAILURE ;
123137 }
@@ -208,7 +222,9 @@ private String uploadFile() {
208222 )
209223 .post (requestBody )
210224 .build ();
211- final Response response = okHttpClient .newCall (request ).execute ();
225+ uploadCall = okHttpClient .newCall (request );
226+ notifyInit ();
227+ final Response response = uploadCall .execute ();
212228 if (response == null ) {
213229 Log .e (TAG , "null response" );
214230 return null ;
@@ -307,31 +323,45 @@ private CourseMaterial findDuplicate() {
307323
308324 private void notifyDuplicate (String originalFileName ) {
309325 builder = sendNotification (getApplicationContext (), R .drawable .ic_cloud_done_black_24dp , fileName + " couldn't be uploaded" ,
310- "A similar file already exists in this course with name " + originalFileName );
326+ "A similar file already exists in this course with name " + originalFileName , null );
311327 builder .setProgress (0 , 0 , false );
312328 builder .setOngoing (false );
313329 builder .setSound (RingtoneManager .getDefaultUri (RingtoneManager .TYPE_NOTIFICATION ));
314330 notificationManager .notify (notificationId , builder .build ());
315331 }
316332
317333 private void notifyInit () {
318- builder = sendNotification (getApplicationContext (), R .drawable .ic_launcher_foreground , "Uploading " + fileName , "Please wait..." );
334+ final int cancelUploadRc = notificationId ;
335+ Intent broadcast = new Intent (ACTION_CANCEL_UPLOAD );
336+ broadcast .putExtra (KEY_FILE_HASH , fileHash );
337+ PendingIntent cancelIntent = PendingIntent .getBroadcast (getApplicationContext (), cancelUploadRc , broadcast , PendingIntent .FLAG_ONE_SHOT );
338+ final NotificationCompat .Action cancelAction = new NotificationCompat .Action (R .drawable .ic_close_black_24dp , getApplicationContext ().getString (R .string .cancel ), cancelIntent );
339+ getApplicationContext ().registerReceiver (new BroadcastReceiver () {
340+ @ Override
341+ public void onReceive (final Context context , final Intent intent ) {
342+ if (Objects .equals (fileHash , intent .getStringExtra (KEY_FILE_HASH ))) {
343+ cancelledByUser = true ;
344+ uploadCall .cancel ();
345+ }
346+ }
347+ }, cancelUploadFilter );
348+ builder = sendNotification (getApplicationContext (), R .drawable .ic_launcher_foreground , "Uploading " + fileName , "Please wait..." , cancelAction );
319349 builder .setProgress (0 , 0 , true );
320350 builder .setOngoing (true );
321351 notificationManager .notify (notificationId , builder .build ());
322352 }
323353
324354 private void notifyFailure () {
325355 builder = sendNotification (getApplicationContext (), R .drawable .ic_launcher_foreground , "Comrades" ,
326- "File Could not be uploaded.Please try again later." );
356+ "File Could not be uploaded. Please try again later." , null );
327357 builder .setProgress (0 , 0 , false );
328358 builder .setOngoing (false );
329359 notificationManager .notify (notificationId , builder .build ());
330360 }
331361
332362 private void notifySuccess () {
333363 builder = sendNotification (getApplicationContext (), R .drawable .ic_cloud_done_black_24dp , fileName + " uploaded" ,
334- "Thanks for Contributing" );
364+ "Thanks for Contributing" , null );
335365 builder .setProgress (0 , 0 , false );
336366 builder .setOngoing (false );
337367 builder .setSound (RingtoneManager .getDefaultUri (RingtoneManager .TYPE_NOTIFICATION ));
0 commit comments