269 lines
8.5 KiB
Java
269 lines
8.5 KiB
Java
package androidx.core.app;
|
|
|
|
import android.app.Notification;
|
|
import android.app.NotificationChannel;
|
|
import android.media.AudioAttributes;
|
|
import android.net.Uri;
|
|
import android.os.Build;
|
|
import android.provider.Settings;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.annotation.RequiresApi;
|
|
import androidx.core.util.Preconditions;
|
|
public class NotificationChannelCompat {
|
|
public static final String DEFAULT_CHANNEL_ID = "miscellaneous";
|
|
private static final int DEFAULT_LIGHT_COLOR = 0;
|
|
private static final boolean DEFAULT_SHOW_BADGE = true;
|
|
public AudioAttributes mAudioAttributes;
|
|
private boolean mBypassDnd;
|
|
private boolean mCanBubble;
|
|
public String mConversationId;
|
|
public String mDescription;
|
|
public String mGroupId;
|
|
@NonNull
|
|
public final String mId;
|
|
public int mImportance;
|
|
private boolean mImportantConversation;
|
|
public int mLightColor;
|
|
public boolean mLights;
|
|
private int mLockscreenVisibility;
|
|
public CharSequence mName;
|
|
public String mParentId;
|
|
public boolean mShowBadge;
|
|
public Uri mSound;
|
|
public boolean mVibrationEnabled;
|
|
public long[] mVibrationPattern;
|
|
|
|
public static class Builder {
|
|
private final NotificationChannelCompat mChannel;
|
|
|
|
public Builder(@NonNull String str, int i) {
|
|
this.mChannel = new NotificationChannelCompat(str, i);
|
|
}
|
|
|
|
@NonNull
|
|
public NotificationChannelCompat build() {
|
|
return this.mChannel;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setConversationId(@NonNull String str, @NonNull String str2) {
|
|
if (Build.VERSION.SDK_INT >= 30) {
|
|
NotificationChannelCompat notificationChannelCompat = this.mChannel;
|
|
notificationChannelCompat.mParentId = str;
|
|
notificationChannelCompat.mConversationId = str2;
|
|
}
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setDescription(@Nullable String str) {
|
|
this.mChannel.mDescription = str;
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setGroup(@Nullable String str) {
|
|
this.mChannel.mGroupId = str;
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setImportance(int i) {
|
|
this.mChannel.mImportance = i;
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setLightColor(int i) {
|
|
this.mChannel.mLightColor = i;
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setLightsEnabled(boolean z2) {
|
|
this.mChannel.mLights = z2;
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setName(@Nullable CharSequence charSequence) {
|
|
this.mChannel.mName = charSequence;
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setShowBadge(boolean z2) {
|
|
this.mChannel.mShowBadge = z2;
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setSound(@Nullable Uri uri, @Nullable AudioAttributes audioAttributes) {
|
|
NotificationChannelCompat notificationChannelCompat = this.mChannel;
|
|
notificationChannelCompat.mSound = uri;
|
|
notificationChannelCompat.mAudioAttributes = audioAttributes;
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setVibrationEnabled(boolean z2) {
|
|
this.mChannel.mVibrationEnabled = z2;
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setVibrationPattern(@Nullable long[] jArr) {
|
|
NotificationChannelCompat notificationChannelCompat = this.mChannel;
|
|
notificationChannelCompat.mVibrationEnabled = jArr != null && jArr.length > 0;
|
|
notificationChannelCompat.mVibrationPattern = jArr;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
@RequiresApi(26)
|
|
public NotificationChannelCompat(@NonNull NotificationChannel notificationChannel) {
|
|
this(notificationChannel.getId(), notificationChannel.getImportance());
|
|
this.mName = notificationChannel.getName();
|
|
this.mDescription = notificationChannel.getDescription();
|
|
this.mGroupId = notificationChannel.getGroup();
|
|
this.mShowBadge = notificationChannel.canShowBadge();
|
|
this.mSound = notificationChannel.getSound();
|
|
this.mAudioAttributes = notificationChannel.getAudioAttributes();
|
|
this.mLights = notificationChannel.shouldShowLights();
|
|
this.mLightColor = notificationChannel.getLightColor();
|
|
this.mVibrationEnabled = notificationChannel.shouldVibrate();
|
|
this.mVibrationPattern = notificationChannel.getVibrationPattern();
|
|
int i = Build.VERSION.SDK_INT;
|
|
if (i >= 30) {
|
|
this.mParentId = notificationChannel.getParentChannelId();
|
|
this.mConversationId = notificationChannel.getConversationId();
|
|
}
|
|
this.mBypassDnd = notificationChannel.canBypassDnd();
|
|
this.mLockscreenVisibility = notificationChannel.getLockscreenVisibility();
|
|
if (i >= 29) {
|
|
this.mCanBubble = notificationChannel.canBubble();
|
|
}
|
|
if (i >= 30) {
|
|
this.mImportantConversation = notificationChannel.isImportantConversation();
|
|
}
|
|
}
|
|
|
|
public NotificationChannelCompat(@NonNull String str, int i) {
|
|
this.mShowBadge = true;
|
|
this.mSound = Settings.System.DEFAULT_NOTIFICATION_URI;
|
|
this.mLightColor = 0;
|
|
this.mId = (String) Preconditions.checkNotNull(str);
|
|
this.mImportance = i;
|
|
this.mAudioAttributes = Notification.AUDIO_ATTRIBUTES_DEFAULT;
|
|
}
|
|
|
|
public boolean canBubble() {
|
|
return this.mCanBubble;
|
|
}
|
|
|
|
public boolean canBypassDnd() {
|
|
return this.mBypassDnd;
|
|
}
|
|
|
|
public boolean canShowBadge() {
|
|
return this.mShowBadge;
|
|
}
|
|
|
|
@Nullable
|
|
public AudioAttributes getAudioAttributes() {
|
|
return this.mAudioAttributes;
|
|
}
|
|
|
|
@Nullable
|
|
public String getConversationId() {
|
|
return this.mConversationId;
|
|
}
|
|
|
|
@Nullable
|
|
public String getDescription() {
|
|
return this.mDescription;
|
|
}
|
|
|
|
@Nullable
|
|
public String getGroup() {
|
|
return this.mGroupId;
|
|
}
|
|
|
|
@NonNull
|
|
public String getId() {
|
|
return this.mId;
|
|
}
|
|
|
|
public int getImportance() {
|
|
return this.mImportance;
|
|
}
|
|
|
|
public int getLightColor() {
|
|
return this.mLightColor;
|
|
}
|
|
|
|
public int getLockscreenVisibility() {
|
|
return this.mLockscreenVisibility;
|
|
}
|
|
|
|
@Nullable
|
|
public CharSequence getName() {
|
|
return this.mName;
|
|
}
|
|
|
|
public NotificationChannel getNotificationChannel() {
|
|
String str;
|
|
String str2;
|
|
int i = Build.VERSION.SDK_INT;
|
|
if (i < 26) {
|
|
return null;
|
|
}
|
|
NotificationChannel notificationChannel = new NotificationChannel(this.mId, this.mName, this.mImportance);
|
|
notificationChannel.setDescription(this.mDescription);
|
|
notificationChannel.setGroup(this.mGroupId);
|
|
notificationChannel.setShowBadge(this.mShowBadge);
|
|
notificationChannel.setSound(this.mSound, this.mAudioAttributes);
|
|
notificationChannel.enableLights(this.mLights);
|
|
notificationChannel.setLightColor(this.mLightColor);
|
|
notificationChannel.setVibrationPattern(this.mVibrationPattern);
|
|
notificationChannel.enableVibration(this.mVibrationEnabled);
|
|
if (!(i < 30 || (str = this.mParentId) == null || (str2 = this.mConversationId) == null)) {
|
|
notificationChannel.setConversationId(str, str2);
|
|
}
|
|
return notificationChannel;
|
|
}
|
|
|
|
@Nullable
|
|
public String getParentChannelId() {
|
|
return this.mParentId;
|
|
}
|
|
|
|
@Nullable
|
|
public Uri getSound() {
|
|
return this.mSound;
|
|
}
|
|
|
|
@Nullable
|
|
public long[] getVibrationPattern() {
|
|
return this.mVibrationPattern;
|
|
}
|
|
|
|
public boolean isImportantConversation() {
|
|
return this.mImportantConversation;
|
|
}
|
|
|
|
public boolean shouldShowLights() {
|
|
return this.mLights;
|
|
}
|
|
|
|
public boolean shouldVibrate() {
|
|
return this.mVibrationEnabled;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder toBuilder() {
|
|
return new Builder(this.mId, this.mImportance).setName(this.mName).setDescription(this.mDescription).setGroup(this.mGroupId).setShowBadge(this.mShowBadge).setSound(this.mSound, this.mAudioAttributes).setLightsEnabled(this.mLights).setLightColor(this.mLightColor).setVibrationEnabled(this.mVibrationEnabled).setVibrationPattern(this.mVibrationPattern).setConversationId(this.mParentId, this.mConversationId);
|
|
}
|
|
}
|