-
Notifications
You must be signed in to change notification settings - Fork 6
/
AxDownloadService.java
122 lines (104 loc) · 4.76 KB
/
AxDownloadService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.axinom.drm.sample.offline;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import androidx.annotation.NonNull;
import com.axinom.drm.sample.R;
import com.google.android.exoplayer2.offline.Download;
import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloadService;
import com.google.android.exoplayer2.scheduler.PlatformScheduler;
import com.google.android.exoplayer2.ui.DownloadNotificationHelper;
import com.google.android.exoplayer2.util.NotificationUtil;
import com.google.android.exoplayer2.util.Util;
import java.util.List;
/**
* A class that extends Exoplayer's DownloadService class.
* Defines a service that enables the downloads to continue even when the
* app is in background.
*/
public class AxDownloadService extends DownloadService {
private static final String CHANNEL_ID = "download_channel";
private static final int JOB_ID = 1;
private static final int FOREGROUND_NOTIFICATION_ID = 1;
public static final String NOTIFICATION = "com.axinom.drm.sample.offline.AxDownloadService";
public static final String PROGRESS = "progress";
// Helper for creating a download notifications
private DownloadNotificationHelper notificationHelper;
public AxDownloadService() {
super(FOREGROUND_NOTIFICATION_ID,
DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL,
CHANNEL_ID,
R.string.exo_download_notification_channel_name,
0);
}
@Override
public void onCreate() {
super.onCreate();
notificationHelper = new DownloadNotificationHelper(this, CHANNEL_ID);
DownloadManager downloadManager = AxOfflineManager.getInstance().getDownloadManager();
if (downloadManager != null) {
downloadManager.addListener(
new TerminalStateNotificationHelper(
this, notificationHelper, FOREGROUND_NOTIFICATION_ID + 1));
}
}
@NonNull
@Override
protected DownloadManager getDownloadManager() {
return AxOfflineManager.getInstance().getDownloadManager();
}
@Override
protected PlatformScheduler getScheduler() {
return Util.SDK_INT >= 21 ? new PlatformScheduler(this, JOB_ID) : null;
}
// Returns a notification to be displayed
@Override
protected Notification getForegroundNotification(List<Download> downloads, int i) {
Notification notification = notificationHelper.buildProgressNotification(this,
R.drawable.ic_download, null, null, downloads, i);
if (notification.extras != null) {
// Notification about download progress is sent here
sendNotification(notification.extras.getInt(Notification.EXTRA_PROGRESS));
}
return notification;
}
// A method that sends a notification
private void sendNotification(int progress) {
Intent intent = new Intent(NOTIFICATION);
intent.putExtra(PROGRESS, progress);
sendBroadcast(intent);
}
// For listening download changes and sending notifications about it
private static final class TerminalStateNotificationHelper implements DownloadManager.Listener {
private final Context context;
private final DownloadNotificationHelper notificationHelper;
private int nextNotificationId;
public TerminalStateNotificationHelper(
Context context, DownloadNotificationHelper notificationHelper, int firstNotificationId) {
this.context = context.getApplicationContext();
this.notificationHelper = notificationHelper;
nextNotificationId = firstNotificationId;
}
@Override
public void onDownloadChanged(DownloadManager manager, Download download, Exception exception) {
Notification notification;
if (download.state == Download.STATE_COMPLETED) {
notification =
notificationHelper.buildDownloadCompletedNotification(context,
R.drawable.ic_download_done,
/* contentIntent= */ null,
Util.fromUtf8Bytes(download.request.data));
} else if (download.state == Download.STATE_FAILED) {
notification =
notificationHelper.buildDownloadFailedNotification(context,
R.drawable.ic_download_done,
/* contentIntent= */ null,
Util.fromUtf8Bytes(download.request.data));
} else {
return;
}
NotificationUtil.setNotification(context, nextNotificationId++, notification);
}
}
}