/
DownloadNotificationHelper.java
261 lines (245 loc) · 9.23 KB
/
DownloadNotificationHelper.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.media3.exoplayer.offline;
import static androidx.core.app.NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import androidx.annotation.DoNotInline;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.StringRes;
import androidx.core.app.NotificationCompat;
import androidx.media3.common.C;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import androidx.media3.exoplayer.R;
import androidx.media3.exoplayer.scheduler.Requirements;
import java.util.List;
/** Helper for creating download notifications. */
@UnstableApi
public final class DownloadNotificationHelper {
private static final @StringRes int NULL_STRING_ID = 0;
private final NotificationCompat.Builder notificationBuilder;
/**
* @param context A context.
* @param channelId The id of the notification channel to use.
*/
public DownloadNotificationHelper(Context context, String channelId) {
this.notificationBuilder =
new NotificationCompat.Builder(context.getApplicationContext(), channelId);
}
/**
* @deprecated Use {@link #buildProgressNotification(Context, int, PendingIntent, String, List,
* int)}.
*/
@Deprecated
public Notification buildProgressNotification(
Context context,
@DrawableRes int smallIcon,
@Nullable PendingIntent contentIntent,
@Nullable String message,
List<Download> downloads) {
return buildProgressNotification(
context, smallIcon, contentIntent, message, downloads, /* notMetRequirements= */ 0);
}
/**
* Returns a progress notification for the given downloads.
*
* @param context A context.
* @param smallIcon A small icon for the notification.
* @param contentIntent An optional content intent to send when the notification is clicked.
* @param message An optional message to display on the notification.
* @param downloads The downloads.
* @param notMetRequirements Any requirements for downloads that are not currently met.
* @return The notification.
*/
public Notification buildProgressNotification(
Context context,
@DrawableRes int smallIcon,
@Nullable PendingIntent contentIntent,
@Nullable String message,
List<Download> downloads,
@Requirements.RequirementFlags int notMetRequirements) {
float totalPercentage = 0;
int downloadTaskCount = 0;
boolean allDownloadPercentagesUnknown = true;
boolean haveDownloadedBytes = false;
boolean haveDownloadingTasks = false;
boolean haveQueuedTasks = false;
boolean haveRemovingTasks = false;
for (int i = 0; i < downloads.size(); i++) {
Download download = downloads.get(i);
switch (download.state) {
case Download.STATE_REMOVING:
haveRemovingTasks = true;
break;
case Download.STATE_QUEUED:
haveQueuedTasks = true;
break;
case Download.STATE_RESTARTING:
case Download.STATE_DOWNLOADING:
haveDownloadingTasks = true;
float downloadPercentage = download.getPercentDownloaded();
if (downloadPercentage != C.PERCENTAGE_UNSET) {
allDownloadPercentagesUnknown = false;
totalPercentage += downloadPercentage;
}
haveDownloadedBytes |= download.getBytesDownloaded() > 0;
downloadTaskCount++;
break;
// Terminal states aren't expected, but if we encounter them we do nothing.
case Download.STATE_STOPPED:
case Download.STATE_COMPLETED:
case Download.STATE_FAILED:
default:
break;
}
}
int titleStringId;
boolean showProgress = true;
if (haveDownloadingTasks) {
titleStringId = R.string.exo_download_downloading;
} else if (haveQueuedTasks && notMetRequirements != 0) {
showProgress = false;
if ((notMetRequirements & Requirements.NETWORK_UNMETERED) != 0) {
// Note: This assumes that "unmetered" == "WiFi", since it provides a clearer message that's
// correct in the majority of cases.
titleStringId = R.string.exo_download_paused_for_wifi;
} else if ((notMetRequirements & Requirements.NETWORK) != 0) {
titleStringId = R.string.exo_download_paused_for_network;
} else {
titleStringId = R.string.exo_download_paused;
}
} else if (haveRemovingTasks) {
titleStringId = R.string.exo_download_removing;
} else {
// There are either no downloads, or all downloads are in terminal states.
titleStringId = NULL_STRING_ID;
}
int maxProgress = 0;
int currentProgress = 0;
boolean indeterminateProgress = false;
if (showProgress) {
maxProgress = 100;
if (haveDownloadingTasks) {
currentProgress = (int) (totalPercentage / downloadTaskCount);
indeterminateProgress = allDownloadPercentagesUnknown && haveDownloadedBytes;
} else {
indeterminateProgress = true;
}
}
return buildNotification(
context,
smallIcon,
contentIntent,
message,
titleStringId,
maxProgress,
currentProgress,
indeterminateProgress,
/* ongoing= */ true,
/* showWhen= */ false);
}
/**
* Returns a notification for a completed download.
*
* @param context A context.
* @param smallIcon A small icon for the notifications.
* @param contentIntent An optional content intent to send when the notification is clicked.
* @param message An optional message to display on the notification.
* @return The notification.
*/
public Notification buildDownloadCompletedNotification(
Context context,
@DrawableRes int smallIcon,
@Nullable PendingIntent contentIntent,
@Nullable String message) {
int titleStringId = R.string.exo_download_completed;
return buildEndStateNotification(context, smallIcon, contentIntent, message, titleStringId);
}
/**
* Returns a notification for a failed download.
*
* @param context A context.
* @param smallIcon A small icon for the notifications.
* @param contentIntent An optional content intent to send when the notification is clicked.
* @param message An optional message to display on the notification.
* @return The notification.
*/
public Notification buildDownloadFailedNotification(
Context context,
@DrawableRes int smallIcon,
@Nullable PendingIntent contentIntent,
@Nullable String message) {
@StringRes int titleStringId = R.string.exo_download_failed;
return buildEndStateNotification(context, smallIcon, contentIntent, message, titleStringId);
}
private Notification buildEndStateNotification(
Context context,
@DrawableRes int smallIcon,
@Nullable PendingIntent contentIntent,
@Nullable String message,
@StringRes int titleStringId) {
return buildNotification(
context,
smallIcon,
contentIntent,
message,
titleStringId,
/* maxProgress= */ 0,
/* currentProgress= */ 0,
/* indeterminateProgress= */ false,
/* ongoing= */ false,
/* showWhen= */ true);
}
private Notification buildNotification(
Context context,
@DrawableRes int smallIcon,
@Nullable PendingIntent contentIntent,
@Nullable String message,
@StringRes int titleStringId,
int maxProgress,
int currentProgress,
boolean indeterminateProgress,
boolean ongoing,
boolean showWhen) {
notificationBuilder.setSmallIcon(smallIcon);
notificationBuilder.setContentTitle(
titleStringId == NULL_STRING_ID ? null : context.getResources().getString(titleStringId));
notificationBuilder.setContentIntent(contentIntent);
notificationBuilder.setStyle(
message == null ? null : new NotificationCompat.BigTextStyle().bigText(message));
notificationBuilder.setProgress(maxProgress, currentProgress, indeterminateProgress);
notificationBuilder.setOngoing(ongoing);
notificationBuilder.setShowWhen(showWhen);
if (Util.SDK_INT >= 31) {
Api31.setForegroundServiceBehavior(notificationBuilder);
}
return notificationBuilder.build();
}
@RequiresApi(31)
private static final class Api31 {
@SuppressLint("WrongConstant") // TODO(b/254277605): remove lint suppression
@DoNotInline
public static void setForegroundServiceBehavior(
NotificationCompat.Builder notificationBuilder) {
notificationBuilder.setForegroundServiceBehavior(FOREGROUND_SERVICE_IMMEDIATE);
}
}
}