Add content intent for download notifications

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=194882787
This commit is contained in:
andrewlewis 2018-04-30 19:09:12 -07:00 committed by Oliver Woodman
parent d412dc97dc
commit 880ce3f59a
2 changed files with 38 additions and 10 deletions

View File

@ -16,6 +16,8 @@
package com.google.android.exoplayer2.demo; package com.google.android.exoplayer2.demo;
import android.app.Notification; import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.util.Pair; import android.util.Pair;
import com.google.android.exoplayer2.offline.DownloadManager; import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloadManager.TaskState; import com.google.android.exoplayer2.offline.DownloadManager.TaskState;
@ -77,7 +79,12 @@ public class DemoDownloadService extends DownloadService {
@Override @Override
protected Notification getForegroundNotification(TaskState[] taskStates) { protected Notification getForegroundNotification(TaskState[] taskStates) {
return DownloadNotificationUtil.createProgressNotification( return DownloadNotificationUtil.createProgressNotification(
taskStates, this, R.drawable.exo_controls_play, CHANNEL_ID, null); taskStates,
/* context= */ this,
R.drawable.exo_controls_play,
CHANNEL_ID,
getContentIntent(),
/* message= */ null);
} }
@Override @Override
@ -86,9 +93,10 @@ public class DemoDownloadService extends DownloadService {
Notification downloadNotification = Notification downloadNotification =
DownloadNotificationUtil.createDownloadFinishedNotification( DownloadNotificationUtil.createDownloadFinishedNotification(
taskState, taskState,
this, /* context= */ this,
R.drawable.exo_controls_play, R.drawable.exo_controls_play,
CHANNEL_ID, CHANNEL_ID,
getContentIntent(),
taskState.action.data, taskState.action.data,
new ErrorMessageProvider<Throwable>() { new ErrorMessageProvider<Throwable>() {
@Override @Override
@ -96,6 +104,12 @@ public class DemoDownloadService extends DownloadService {
return new Pair<>(0, throwable.getLocalizedMessage()); return new Pair<>(0, throwable.getLocalizedMessage());
} }
}); });
NotificationUtil.setNotification(this, notificationId, downloadNotification); NotificationUtil.setNotification(/* context= */ this, notificationId, downloadNotification);
}
private PendingIntent getContentIntent() {
Intent intent = new Intent(/* packageContext= */ this, DownloadActivity.class);
return PendingIntent.getActivity(
/* context= */ this, /* requestCode= */ 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
} }
} }

View File

@ -16,8 +16,11 @@
package com.google.android.exoplayer2.ui; package com.google.android.exoplayer2.ui;
import android.app.Notification; import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.offline.DownloadManager; import com.google.android.exoplayer2.offline.DownloadManager;
@ -27,7 +30,7 @@ import com.google.android.exoplayer2.util.ErrorMessageProvider;
/** Helper class to create notifications for downloads using {@link DownloadManager}. */ /** Helper class to create notifications for downloads using {@link DownloadManager}. */
public final class DownloadNotificationUtil { public final class DownloadNotificationUtil {
private static final int NULL_STRING_ID = 0; private static final @StringRes int NULL_STRING_ID = 0;
private DownloadNotificationUtil() {} private DownloadNotificationUtil() {}
@ -39,14 +42,16 @@ public final class DownloadNotificationUtil {
* @param smallIcon A small icon for the notification. * @param smallIcon A small icon for the notification.
* @param channelId The id of the notification channel to use. Only required for API level 26 and * @param channelId The id of the notification channel to use. Only required for API level 26 and
* above. * above.
* @param contentIntent An optional content intent to send when the notification is clicked.
* @param message An optional message to display on the notification. * @param message An optional message to display on the notification.
* @return A progress notification for the given {@link TaskState}s. * @return A progress notification for the given {@link TaskState}s.
*/ */
public static @Nullable Notification createProgressNotification( public static @Nullable Notification createProgressNotification(
TaskState[] taskStates, TaskState[] taskStates,
Context context, Context context,
int smallIcon, @DrawableRes int smallIcon,
String channelId, String channelId,
@Nullable PendingIntent contentIntent,
@Nullable String message) { @Nullable String message) {
float totalPercentage = 0; float totalPercentage = 0;
int downloadTaskCount = 0; int downloadTaskCount = 0;
@ -70,7 +75,8 @@ public final class DownloadNotificationUtil {
? R.string.exo_download_downloading ? R.string.exo_download_downloading
: (taskStates.length > 0 ? R.string.exo_download_removing : NULL_STRING_ID); : (taskStates.length > 0 ? R.string.exo_download_removing : NULL_STRING_ID);
NotificationCompat.Builder notificationBuilder = NotificationCompat.Builder notificationBuilder =
createNotificationBuilder(context, smallIcon, channelId, message, titleStringId); createNotificationBuilder(
context, smallIcon, channelId, contentIntent, message, titleStringId);
int progress = haveDownloadTasks ? (int) (totalPercentage / downloadTaskCount) : 0; int progress = haveDownloadTasks ? (int) (totalPercentage / downloadTaskCount) : 0;
boolean indeterminate = boolean indeterminate =
@ -91,6 +97,7 @@ public final class DownloadNotificationUtil {
* @param smallIcon A small icon for the notifications. * @param smallIcon A small icon for the notifications.
* @param channelId The id of the notification channel to use. Only required for API level 26 and * @param channelId The id of the notification channel to use. Only required for API level 26 and
* above. * above.
* @param contentIntent An optional content intent to send when the notification is clicked.
* @param message An optional message to display on the notification. * @param message An optional message to display on the notification.
* @param errorMessageProvider An optional {@link ErrorMessageProvider} for translating download * @param errorMessageProvider An optional {@link ErrorMessageProvider} for translating download
* errors into readable error messages. If not null and there is a download error then the * errors into readable error messages. If not null and there is a download error then the
@ -102,8 +109,9 @@ public final class DownloadNotificationUtil {
public static @Nullable Notification createDownloadFinishedNotification( public static @Nullable Notification createDownloadFinishedNotification(
TaskState taskState, TaskState taskState,
Context context, Context context,
int smallIcon, @DrawableRes int smallIcon,
String channelId, String channelId,
@Nullable PendingIntent contentIntent,
@Nullable String message, @Nullable String message,
@Nullable ErrorMessageProvider<Throwable> errorMessageProvider) { @Nullable ErrorMessageProvider<Throwable> errorMessageProvider) {
if (taskState.action.isRemoveAction if (taskState.action.isRemoveAction
@ -113,26 +121,32 @@ public final class DownloadNotificationUtil {
if (taskState.error != null && errorMessageProvider != null) { if (taskState.error != null && errorMessageProvider != null) {
message = errorMessageProvider.getErrorMessage(taskState.error).second; message = errorMessageProvider.getErrorMessage(taskState.error).second;
} }
@StringRes
int titleStringId = int titleStringId =
taskState.state == TaskState.STATE_ENDED taskState.state == TaskState.STATE_ENDED
? R.string.exo_download_completed ? R.string.exo_download_completed
: R.string.exo_download_failed; : R.string.exo_download_failed;
NotificationCompat.Builder notificationBuilder = NotificationCompat.Builder notificationBuilder =
createNotificationBuilder(context, smallIcon, channelId, message, titleStringId); createNotificationBuilder(
context, smallIcon, channelId, contentIntent, message, titleStringId);
return notificationBuilder.build(); return notificationBuilder.build();
} }
private static NotificationCompat.Builder createNotificationBuilder( private static NotificationCompat.Builder createNotificationBuilder(
Context context, Context context,
int smallIcon, @DrawableRes int smallIcon,
String channelId, String channelId,
@Nullable PendingIntent contentIntent,
@Nullable String message, @Nullable String message,
int titleStringId) { @StringRes int titleStringId) {
NotificationCompat.Builder notificationBuilder = NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context, channelId).setSmallIcon(smallIcon); new NotificationCompat.Builder(context, channelId).setSmallIcon(smallIcon);
if (titleStringId != NULL_STRING_ID) { if (titleStringId != NULL_STRING_ID) {
notificationBuilder.setContentTitle(context.getResources().getString(titleStringId)); notificationBuilder.setContentTitle(context.getResources().getString(titleStringId));
} }
if (contentIntent != null) {
notificationBuilder.setContentIntent(contentIntent);
}
if (message != null) { if (message != null) {
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message)); notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
} }