@@ -42,6 +42,7 @@ class DownloadService: Service() {
42
42
companion object {
43
43
const val ONGOING_NOTIFICATION_ID = 1
44
44
const val COMPLETED_NOTIFICATION_ID = 2
45
+ const val FAILED_NOTIFICATION_ID = 3
45
46
46
47
const val ACTION_PAUSE = " pause"
47
48
const val ACTION_CANCEL = " cancel"
@@ -210,6 +211,7 @@ class DownloadService: Service() {
210
211
file.close()
211
212
} catch (error: Exception ) {
212
213
error.printStackTrace()
214
+ processor.onNext(DownloadEvent (DownloadStatus .FAILED ))
213
215
}
214
216
}, {
215
217
error ->
@@ -268,6 +270,10 @@ class DownloadService: Service() {
268
270
mission.bean.save()
269
271
stopForeground(true )
270
272
}
273
+ DownloadStatus .FAILED -> {
274
+ mission.pause = true
275
+ mission.bean.save()
276
+ }
271
277
DownloadStatus .COMPLETED -> {
272
278
mission.bean.save()
273
279
part.durls.find {
@@ -285,6 +291,7 @@ class DownloadService: Service() {
285
291
}
286
292
287
293
private fun sendNotification (event : DownloadEvent , url : String ) {
294
+ " Send Notification ${event.status} ..." .logD()
288
295
when (event.status) {
289
296
DownloadStatus .STARTED , DownloadStatus .READY -> {
290
297
val nfIntent = Intent (this , DownloadActivity ::class .java)
@@ -308,7 +315,7 @@ class DownloadService: Service() {
308
315
val piCancel = PendingIntent .getService(this , 0 , cancelIntent, PendingIntent .FLAG_UPDATE_CURRENT )
309
316
310
317
val builder = NotificationCompat .Builder (this )
311
- .setSmallIcon(R .drawable.ic_file_download_white )
318
+ .setSmallIcon(R .mipmap.ic_launcher )
312
319
.setContentTitle(event.taskName)
313
320
.setContentIntent(pendingIntent)
314
321
.addAction(R .drawable.ic_action_pause, " 暂停" , piPause)
@@ -322,7 +329,7 @@ class DownloadService: Service() {
322
329
builder.setContentText(" 连接中..." )
323
330
}
324
331
val notification = builder.build()
325
- notification.flags = notification.flags or Notification .FLAG_NO_CLEAR or Notification . FLAG_ONGOING_EVENT
332
+ notification.flags = notification.flags or Notification .FLAG_NO_CLEAR
326
333
327
334
startForeground(ONGOING_NOTIFICATION_ID , notification)
328
335
}
@@ -339,7 +346,7 @@ class DownloadService: Service() {
339
346
340
347
val builder = NotificationCompat .Builder (this )
341
348
.setProgress(0 , 0 , false )
342
- .setSmallIcon(R .drawable.ic_file_download_white )
349
+ .setSmallIcon(R .mipmap.ic_launcher )
343
350
.setContentTitle(event.taskName)
344
351
.setContentText(" ${event.totalSize.formatWithUnit()} /已完成" )
345
352
.setContentIntent(pendingIntent)
@@ -352,6 +359,32 @@ class DownloadService: Service() {
352
359
353
360
notifyMgr.notify(COMPLETED_NOTIFICATION_ID , notification)
354
361
}
362
+ DownloadStatus .FAILED -> {
363
+ val nfIntent = Intent (this , DownloadActivity ::class .java)
364
+ nfIntent.flags = Intent .FLAG_ACTIVITY_SINGLE_TOP or Intent .FLAG_ACTIVITY_CLEAR_TOP
365
+ nfIntent.action = DownloadActivity .ACTION_DOWNLOADING
366
+
367
+ val stackBuilder = TaskStackBuilder .create(this )
368
+ stackBuilder.addParentStack(DownloadActivity ::class .java)
369
+ stackBuilder.addNextIntent(nfIntent)
370
+
371
+ val pendingIntent = stackBuilder.getPendingIntent(0 , PendingIntent .FLAG_UPDATE_CURRENT )
372
+
373
+ val builder = NotificationCompat .Builder (this )
374
+ .setProgress(0 , 0 , false )
375
+ .setSmallIcon(R .mipmap.ic_launcher)
376
+ .setContentTitle(event.taskName)
377
+ .setContentText(" 下载失败" )
378
+ .setContentIntent(pendingIntent)
379
+ val notification = builder.build()
380
+ notification.flags = notification.flags or Notification .FLAG_AUTO_CANCEL
381
+
382
+ val notifyMgr = getSystemService(Context .NOTIFICATION_SERVICE ) as NotificationManager
383
+
384
+ stopForeground(true )
385
+
386
+ notifyMgr.notify(FAILED_NOTIFICATION_ID , notification)
387
+ }
355
388
}
356
389
}
357
390
0 commit comments