11package org .amahi .anywhere .service ;
22
3+ import android .app .DownloadManager ;
34import android .app .Notification ;
45import android .app .NotificationManager ;
56import android .app .Service ;
910import android .net .Uri ;
1011import android .os .Build ;
1112import android .os .IBinder ;
13+ import android .support .annotation .NonNull ;
1214import android .support .annotation .Nullable ;
1315import android .util .Log ;
1416
1820import org .amahi .anywhere .R ;
1921import org .amahi .anywhere .bus .BusProvider ;
2022import org .amahi .anywhere .bus .FileMovedEvent ;
23+ import org .amahi .anywhere .bus .OfflineCanceledEvent ;
2124import org .amahi .anywhere .db .entities .OfflineFile ;
2225import org .amahi .anywhere .db .repositories .OfflineFileRepository ;
2326import org .amahi .anywhere .job .NetConnectivityJob ;
3033import org .amahi .anywhere .util .NetworkUtils ;
3134
3235import java .io .File ;
36+ import java .util .List ;
3337
3438import javax .inject .Inject ;
3539
3640import static org .amahi .anywhere .util .Downloader .OFFLINE_PATH ;
3741
3842public class DownloadService extends Service implements Downloader .DownloadCallbacks {
3943
44+ private static final int NOTIFICATION_OFFLINE_ID = 101 ;
45+ private static final int NOTIFICATION_ERROR_ID = 101 ;
46+
4047 @ Inject
4148 ServerClient serverClient ;
4249
@@ -113,7 +120,7 @@ public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
113120 scheduleNetworkConnectivityJob ();
114121 }
115122
116- return START_NOT_STICKY ;
123+ return START_STICKY ;
117124 }
118125
119126 private void startNextDownload () {
@@ -125,6 +132,7 @@ private void startNextDownload() {
125132 isDownloading = true ;
126133 } else {
127134 stopDownloading ();
135+ stopForeground (true );
128136 }
129137 }
130138
@@ -142,12 +150,12 @@ private void resumeDownload(long downloadId, String fileName) {
142150 notificationBuilder
143151 .setOngoing (true )
144152 .setSmallIcon (R .drawable .ic_app_logo )
145- .setContentTitle (getString (R .string .notification_download_title ))
153+ .setContentTitle (getString (R .string .notification_download_offline_title ))
146154 .setContentText (getString (R .string .notification_upload_message , fileName ))
147155 .setProgress (100 , 0 , false )
148156 .build ();
149157 Notification notification = notificationBuilder .build ();
150- startForeground (( int ) downloadId , notification );
158+ startForeground (NOTIFICATION_OFFLINE_ID , notification );
151159 downloader .setDownloadCallbacks (this );
152160 downloader .resumeProgressCount (downloadId );
153161 }
@@ -194,12 +202,12 @@ public void downloadStarted(int id, String fileName) {
194202 notificationBuilder
195203 .setOngoing (true )
196204 .setSmallIcon (R .drawable .ic_app_logo )
197- .setContentTitle (getString (R .string .notification_download_title ))
205+ .setContentTitle (getString (R .string .notification_download_offline_title ))
198206 .setContentText (getString (R .string .notification_upload_message , fileName ))
199207 .setProgress (100 , 0 , false )
200208 .build ();
201209 Notification notification = notificationBuilder .build ();
202- startForeground (id , notification );
210+ startForeground (NOTIFICATION_OFFLINE_ID , notification );
203211 }
204212
205213 @ Override
@@ -209,19 +217,34 @@ public void downloadProgress(int id, int progress) {
209217 notificationBuilder
210218 .setProgress (100 , progress , false );
211219 Notification notification = notificationBuilder .build ();
212- notificationManager .notify (id , notification );
220+ notificationManager .notify (NOTIFICATION_OFFLINE_ID , notification );
213221 }
214222
215223 @ Override
216224 public void downloadSuccess (long id ) {
217225 OfflineFile offlineFile = offlineFileRepository .getFileWithDownloadId (id );
218226 if (offlineFile != null ) {
219227 moveFileInOfflineDirectory (offlineFile .getName ());
228+ showDownloadedNotification (offlineFile );
220229 } else {
221230 stopForeground (true );
222231 }
223232 }
224233
234+ private void showDownloadedNotification (@ NonNull OfflineFile offlineFile ) {
235+ NotificationManager notificationManager = (NotificationManager ) getApplicationContext ()
236+ .getSystemService (Context .NOTIFICATION_SERVICE );
237+
238+ notificationBuilder
239+ .setContentTitle (getString (R .string .notification_offline_download_complete ))
240+ .setContentText (getString (R .string .notification_upload_message , offlineFile .getName ()))
241+ .setOngoing (false );
242+
243+ Notification notification = notificationBuilder .build ();
244+ notificationManager .cancel (NOTIFICATION_OFFLINE_ID );
245+ notificationManager .notify ((int ) offlineFile .getTimeStamp (), notification );
246+ }
247+
225248 @ Subscribe
226249 public void onFileMoved (FileMovedEvent event ) {
227250 OfflineFile offlineFile = offlineFileRepository .getCurrentDownloadingFile ();
@@ -237,7 +260,7 @@ public void onFileMoved(FileMovedEvent event) {
237260 .setProgress (100 , 100 , false );
238261
239262 Notification notification = notificationBuilder .build ();
240- notificationManager .notify (( int ) offlineFile . getDownloadId () , notification );
263+ notificationManager .notify (NOTIFICATION_OFFLINE_ID , notification );
241264
242265 offlineFile .setState (OfflineFile .DOWNLOADED );
243266 offlineFile .setDownloadId (-1 );
@@ -261,7 +284,38 @@ private void moveFileInOfflineDirectory(String fileName) {
261284
262285 @ Override
263286 public void downloadError (long id ) {
287+ removeFromDownloader (id );
288+ stopNotification ();
289+ showErrorNotification (id );
264290 removeOfflineFile (id );
291+ startNextDownload ();
292+ }
293+
294+ private void removeFromDownloader (long id ) {
295+ DownloadManager dm = (DownloadManager ) getSystemService (Context .DOWNLOAD_SERVICE );
296+ if (dm != null ) {
297+ dm .remove (id );
298+ }
299+ }
300+
301+ private void showErrorNotification (long id ) {
302+ OfflineFile offlineFile = offlineFileRepository .getFileWithDownloadId (id );
303+ if (offlineFile != null ) {
304+ NotificationManager notificationManager = (NotificationManager ) getApplicationContext ()
305+ .getSystemService (Context .NOTIFICATION_SERVICE );
306+
307+ notificationBuilder
308+ .setContentTitle (getString (R .string .notification_offline_download_error ))
309+ .setContentText (getString (R .string .notification_upload_message , offlineFile .getName ()))
310+ .setOngoing (false );
311+
312+ Notification notification = notificationBuilder .build ();
313+ notificationManager .notify ((int ) offlineFile .getTimeStamp (), notification );
314+ }
315+ }
316+
317+ private void stopNotification () {
318+ stopForeground (true );
265319 }
266320
267321 @ Override
@@ -276,19 +330,32 @@ public void downloadPaused(long downloadId, int progress) {
276330 .setProgress (100 , progress , false );
277331
278332 Notification notification = notificationBuilder .build ();
279- notificationManager .notify (( int ) downloadId , notification );
333+ notificationManager .notify (NOTIFICATION_OFFLINE_ID , notification );
280334
281335 stopDownloading ();
282336 }
283337
284338 private void removeOfflineFile (long id ) {
285339 OfflineFile offlineFile = offlineFileRepository .getFileWithDownloadId (id );
286- offlineFileRepository .delete (offlineFile );
340+ if (offlineFile != null ) {
341+ offlineFileRepository .delete (offlineFile );
342+ }
287343 }
288344
289345 private void scheduleNetworkConnectivityJob () {
290346 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
291347 NetConnectivityJob .scheduleJob (this );
292348 }
293349 }
350+
351+ @ Subscribe
352+ public void onDownloadCanceled (OfflineCanceledEvent event ) {
353+ stopForeground (true );
354+ List <OfflineFile > offlineFiles = offlineFileRepository .getAllOfflineFiles ();
355+ if (offlineFiles .isEmpty ()) {
356+ stopDownloading ();
357+ } else if (event .getDownloadId () != -1 ) {
358+ startNextDownload ();
359+ }
360+ }
294361}
0 commit comments