11package ca .pkay .rcloneexplorer .Services ;
22
3+ import android .annotation .SuppressLint ;
34import android .app .IntentService ;
45import android .app .NotificationChannel ;
56import android .app .NotificationManager ;
67import android .app .PendingIntent ;
78import android .content .Context ;
89import android .content .Intent ;
10+ import android .os .AsyncTask ;
911import android .os .Build ;
1012import android .support .annotation .Nullable ;
1113import android .support .v4 .app .NotificationCompat ;
1214
1315import java .util .List ;
16+ import java .util .concurrent .ExecutionException ;
1417
1518import ca .pkay .rcloneexplorer .BroadcastReceivers .DownloadCancelAction ;
1619import ca .pkay .rcloneexplorer .Items .FileItem ;
@@ -28,6 +31,9 @@ public class DownloadService extends IntentService {
2831 private final int PERSISTENT_NOTIFICATION_ID = 167 ;
2932 private final int FAILED_DOWNLOAD_NOTIFICATION_ID = 138 ;
3033 private final int DOWNLOAD_FINISHED_NOTIFICATION_ID = 80 ;
34+ private int numOfRunningProcesses ;
35+ private int numOfFinishedDownloads ;
36+ private int numOfFailedDownloads ;
3137 private Rclone rclone ;
3238 private List <Process > runningProcesses ;
3339
@@ -71,19 +77,19 @@ protected void onHandleIntent(@Nullable Intent intent) {
7177 final String remote = intent .getStringExtra (REMOTE_ARG );
7278
7379 runningProcesses = rclone .downloadItems (remote , downloadList , downloadPath );
74- int numOfRunningProcesses = runningProcesses .size ();
75- int numOfFinishedDownloads = 0 ;
76- int numOfFailedDownloads = 0 ;
80+ numOfRunningProcesses = runningProcesses .size ();
81+ numOfFinishedDownloads = 0 ;
82+ numOfFailedDownloads = 0 ;
83+ AsyncTask [] asyncTasks = new AsyncTask [numOfRunningProcesses ];
84+ int i = 0 ;
7785 for (Process process : runningProcesses ) {
78- try {
79- process .waitFor ();
80- if (process .exitValue () != 0 ) {
81- showDownloadFailedNotification (++numOfFailedDownloads , numOfRunningProcesses );
82- } else {
83- showDownloadFinishedNotification (++numOfFinishedDownloads , numOfRunningProcesses );
84- }
86+ asyncTasks [i ++] = new MonitorDownload ().execute (process );
87+ }
8588
86- } catch (InterruptedException e ) {
89+ for (AsyncTask asyncTask : asyncTasks ) {
90+ try {
91+ asyncTask .get ();
92+ } catch (InterruptedException | ExecutionException e ) {
8793 e .printStackTrace ();
8894 }
8995 }
@@ -140,4 +146,29 @@ private void setNotificationChannel() {
140146 }
141147 }
142148 }
149+
150+ @ SuppressLint ("StaticFieldLeak" )
151+ private class MonitorDownload extends AsyncTask <Process , Void , Boolean > {
152+
153+ @ Override
154+ protected Boolean doInBackground (Process ... processes ) {
155+ Process process = processes [0 ];
156+ try {
157+ process .waitFor ();
158+ } catch (InterruptedException e ) {
159+ e .printStackTrace ();
160+ }
161+ return process .exitValue () == 0 ;
162+ }
163+
164+ @ Override
165+ protected void onPostExecute (Boolean result ) {
166+ super .onPostExecute (result );
167+ if (result ) {
168+ showDownloadFinishedNotification (++numOfFinishedDownloads , numOfRunningProcesses );
169+ } else {
170+ showDownloadFailedNotification (++numOfFailedDownloads , numOfRunningProcesses );
171+ }
172+ }
173+ }
143174}
0 commit comments