66import android .content .Context ;
77import android .content .Intent ;
88import android .content .ServiceConnection ;
9+ import android .content .SharedPreferences ;
910import android .os .IBinder ;
1011import android .util .Log ;
1112
1213import androidx .annotation .Nullable ;
1314
15+ import com .google .gson .Gson ;
16+ import com .google .gson .reflect .TypeToken ;
17+
1418import java .util .ArrayList ;
1519
1620public class DownloadManager implements ServiceConnection {
@@ -20,8 +24,8 @@ public class DownloadManager implements ServiceConnection {
2024 @ Nullable
2125 private DownloaderService downloaderService ;
2226 private final Downloader2 .ClientCallback serviceCallback ;
23- private boolean serviceStarted = false ;
24-
27+ private ArrayList < DownloadGroupInfo > downloadsToStart = new ArrayList <>() ;
28+ private boolean shouldStartAllDownloads = false ;
2529
2630
2731 public DownloadManager (Context context ) {
@@ -58,25 +62,14 @@ public void onError(DownloadGroupInfo downloadGroup, DownloadInfo download, int
5862 }
5963
6064 /**
61- * This method will start the download service and resume all the unfinished downloads
65+ * This method will start the download service (if there are downloads to resume) and resume all the unfinished downloads
6266 */
63- public void startService (){
64- final Intent intent = new Intent (context , DownloaderService .class );
65- //intent.putExtra("notification", notification);
66- context .startService (intent );
67- this .serviceStarted = true ;
68- if (callback != null ) { //if we have previously called subscribe before starting the service we will bind here
69- boolean result = context .bindService (new Intent (context , DownloaderService .class ), this , BIND_ABOVE_CLIENT );
70- Log .d ("bind download" , result ? "success" : "failed" );
71- }
72- }
73-
74- public void subscribe (@ Nullable Callback callback ) {
67+ public void subscribeAndResumeDownload (@ Nullable Callback callback ) {
7568 if (this .callback == null ) {
7669 this .callback = callback ;
77- if ( serviceStarted ) { //if we have not started yet the service, we will bind after starting it, not now (this way the service will not stop when we unbind)
78- boolean result = context . bindService ( new Intent ( context , DownloaderService . class ), this , BIND_ABOVE_CLIENT );
79- Log . d ( "bind download" , result ? "success" : "failed" );
70+ boolean shouldStartService = areDownloadsRunning ( false );
71+ if ( shouldStartService ) {
72+ startAndBindService ( );
8073 }
8174 }
8275 }
@@ -91,12 +84,15 @@ public void unsubscribe() {
9184 }
9285 }
9386
94- public boolean startDownload (DownloadGroupInfo downloadGroup ){
95- if (downloaderService != null ) {
87+ public void startDownload (DownloadGroupInfo downloadGroup ){
88+ if (downloaderService == null && !downloadsToStart .contains (downloadGroup )){
89+ downloadsToStart .add (downloadGroup );
90+ if (!DownloaderService .running ){
91+ startAndBindService ();
92+ }
93+ }else if (downloaderService != null ) {
9694 downloaderService .startDownload (downloadGroup );
97- return true ;
9895 }
99- return false ;
10096 }
10197
10298 public boolean stopDownload (DownloadGroupInfo downloadGroup ){
@@ -107,8 +103,21 @@ public boolean stopDownload(DownloadGroupInfo downloadGroup){
107103 return false ;
108104 }
109105
110- public boolean startAllDownloads () {
106+ public boolean cancelDownload ( DownloadGroupInfo downloadGroup ) {
111107 if (downloaderService != null ) {
108+ downloaderService .cancelDownload (downloadGroup );
109+ return true ;
110+ }
111+ return false ;
112+ }
113+
114+ public boolean startAllDownloads () {
115+ if (downloaderService == null && !shouldStartAllDownloads ){
116+ shouldStartAllDownloads = true ;
117+ if (!DownloaderService .running ){
118+ startAndBindService ();
119+ }
120+ } else if (downloaderService != null ) {
112121 downloaderService .startAllDownloads ();
113122 return true ;
114123 }
@@ -123,18 +132,61 @@ public boolean stopAllDownloads() {
123132 return false ;
124133 }
125134
135+ public boolean cancelAllDownloads () {
136+ if (downloaderService != null ) {
137+ downloaderService .cancelAllDownloads ();
138+ return true ;
139+ }
140+ return false ;
141+ }
142+
126143 public ArrayList <DownloadGroupInfo > getDownloadsStatus () {
127144 if (downloaderService != null ) {
128145 return downloaderService .getDownloadsStatus ();
129146 }
130147 return null ;
131148 }
132149
150+ private void startAndBindService (){
151+ // start the service
152+ final Intent intent = new Intent (context , DownloaderService .class );
153+ context .startService (intent );
154+ //we bind to the service
155+ boolean result = context .bindService (new Intent (context , DownloaderService .class ), this , BIND_ABOVE_CLIENT );
156+ Log .d ("bind download" , result ? "success" : "failed" );
157+ }
158+
159+ private boolean areDownloadsRunning (boolean includePaused ){
160+ SharedPreferences sharedPreferences = context .getSharedPreferences ("default" , Context .MODE_PRIVATE );
161+ String downloadsStatusString = sharedPreferences .getString ("downloadsStatus" , "" );
162+ if (!downloadsStatusString .isEmpty ()) {
163+ //we check if there are unfinished downloads that are not paused (or also paused ones if includePaused is true)
164+ Gson gson = new Gson ();
165+ ArrayList <DownloadGroupInfo > downloadGroupInfos = gson .fromJson (downloadsStatusString , new TypeToken <ArrayList <DownloadGroupInfo >>() {}.getType ());
166+ if (downloadGroupInfos != null ) {
167+ for (DownloadGroupInfo groupInfo : downloadGroupInfos ) {
168+ if (!groupInfo .isAllDownloadCompleted () && (includePaused || groupInfo .getRunningDownloadIndex () != -1 )) {
169+ return true ;
170+ }
171+ }
172+ }
173+ }
174+ return false ;
175+ }
176+
133177 @ Override
134178 public void onServiceConnected (ComponentName componentName , IBinder iBinder ) {
135179 this .downloaderService = ((DownloaderService .LocalBinder ) iBinder ).getService ();
136180 downloaderService .registerClient (serviceCallback );
137181 if (callback != null ) callback .onServiceConnected ();
182+ for (DownloadGroupInfo downloadGroup : downloadsToStart ) {
183+ downloaderService .startDownload (downloadGroup );
184+ }
185+ downloadsToStart .clear ();
186+ if (shouldStartAllDownloads ){
187+ downloaderService .startAllDownloads ();
188+ shouldStartAllDownloads = false ;
189+ }
138190 }
139191
140192 @ Override
0 commit comments