1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.google.android.exoplayer2.util; 17 18 import android.annotation.SuppressLint; 19 import android.app.Notification; 20 import android.app.NotificationChannel; 21 import android.app.NotificationManager; 22 import android.content.Context; 23 import android.content.Intent; 24 import androidx.annotation.IntDef; 25 import androidx.annotation.Nullable; 26 import androidx.annotation.StringRes; 27 import java.lang.annotation.Documented; 28 import java.lang.annotation.Retention; 29 import java.lang.annotation.RetentionPolicy; 30 31 /** Utility methods for displaying {@link Notification Notifications}. */ 32 @SuppressLint("InlinedApi") 33 public final class NotificationUtil { 34 35 /** 36 * Notification channel importance levels. One of {@link #IMPORTANCE_UNSPECIFIED}, {@link 37 * #IMPORTANCE_NONE}, {@link #IMPORTANCE_MIN}, {@link #IMPORTANCE_LOW}, {@link 38 * #IMPORTANCE_DEFAULT} or {@link #IMPORTANCE_HIGH}. 39 */ 40 @Documented 41 @Retention(RetentionPolicy.SOURCE) 42 @IntDef({ 43 IMPORTANCE_UNSPECIFIED, 44 IMPORTANCE_NONE, 45 IMPORTANCE_MIN, 46 IMPORTANCE_LOW, 47 IMPORTANCE_DEFAULT, 48 IMPORTANCE_HIGH 49 }) 50 public @interface Importance {} 51 /** @see NotificationManager#IMPORTANCE_UNSPECIFIED */ 52 public static final int IMPORTANCE_UNSPECIFIED = NotificationManager.IMPORTANCE_UNSPECIFIED; 53 /** @see NotificationManager#IMPORTANCE_NONE */ 54 public static final int IMPORTANCE_NONE = NotificationManager.IMPORTANCE_NONE; 55 /** @see NotificationManager#IMPORTANCE_MIN */ 56 public static final int IMPORTANCE_MIN = NotificationManager.IMPORTANCE_MIN; 57 /** @see NotificationManager#IMPORTANCE_LOW */ 58 public static final int IMPORTANCE_LOW = NotificationManager.IMPORTANCE_LOW; 59 /** @see NotificationManager#IMPORTANCE_DEFAULT */ 60 public static final int IMPORTANCE_DEFAULT = NotificationManager.IMPORTANCE_DEFAULT; 61 /** @see NotificationManager#IMPORTANCE_HIGH */ 62 public static final int IMPORTANCE_HIGH = NotificationManager.IMPORTANCE_HIGH; 63 64 /** @deprecated Use {@link #createNotificationChannel(Context, String, int, int, int)}. */ 65 @Deprecated createNotificationChannel( Context context, String id, @StringRes int nameResourceId, @Importance int importance)66 public static void createNotificationChannel( 67 Context context, String id, @StringRes int nameResourceId, @Importance int importance) { 68 createNotificationChannel( 69 context, id, nameResourceId, /* descriptionResourceId= */ 0, importance); 70 } 71 72 /** 73 * Creates a notification channel that notifications can be posted to. See {@link 74 * NotificationChannel} and {@link 75 * NotificationManager#createNotificationChannel(NotificationChannel)} for details. 76 * 77 * @param context A {@link Context}. 78 * @param id The id of the channel. Must be unique per package. The value may be truncated if it's 79 * too long. 80 * @param nameResourceId A string resource identifier for the user visible name of the channel. 81 * The recommended maximum length is 40 characters. The string may be truncated if it's too 82 * long. You can rename the channel when the system locale changes by listening for the {@link 83 * Intent#ACTION_LOCALE_CHANGED} broadcast. 84 * @param descriptionResourceId A string resource identifier for the user visible description of 85 * the channel, or 0 if no description is provided. The recommended maximum length is 300 86 * characters. The value may be truncated if it is too long. You can change the description of 87 * the channel when the system locale changes by listening for the {@link 88 * Intent#ACTION_LOCALE_CHANGED} broadcast. 89 * @param importance The importance of the channel. This controls how interruptive notifications 90 * posted to this channel are. One of {@link #IMPORTANCE_UNSPECIFIED}, {@link 91 * #IMPORTANCE_NONE}, {@link #IMPORTANCE_MIN}, {@link #IMPORTANCE_LOW}, {@link 92 * #IMPORTANCE_DEFAULT} and {@link #IMPORTANCE_HIGH}. 93 */ createNotificationChannel( Context context, String id, @StringRes int nameResourceId, @StringRes int descriptionResourceId, @Importance int importance)94 public static void createNotificationChannel( 95 Context context, 96 String id, 97 @StringRes int nameResourceId, 98 @StringRes int descriptionResourceId, 99 @Importance int importance) { 100 if (Util.SDK_INT >= 26) { 101 NotificationManager notificationManager = 102 (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 103 NotificationChannel channel = 104 new NotificationChannel(id, context.getString(nameResourceId), importance); 105 if (descriptionResourceId != 0) { 106 channel.setDescription(context.getString(descriptionResourceId)); 107 } 108 notificationManager.createNotificationChannel(channel); 109 } 110 } 111 112 /** 113 * Post a notification to be shown in the status bar. If a notification with the same id has 114 * already been posted by your application and has not yet been canceled, it will be replaced by 115 * the updated information. If {@code notification} is {@code null} then any notification 116 * previously shown with the specified id will be cancelled. 117 * 118 * @param context A {@link Context}. 119 * @param id The notification id. 120 * @param notification The {@link Notification} to post, or {@code null} to cancel a previously 121 * shown notification. 122 */ setNotification(Context context, int id, @Nullable Notification notification)123 public static void setNotification(Context context, int id, @Nullable Notification notification) { 124 NotificationManager notificationManager = 125 (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 126 if (notification != null) { 127 notificationManager.notify(id, notification); 128 } else { 129 notificationManager.cancel(id); 130 } 131 } 132 NotificationUtil()133 private NotificationUtil() {} 134 } 135