2021-07-24 02:37:17 +00:00
|
|
|
package androidx.core.app;
|
|
|
|
|
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.RemoteInput;
|
2021-09-21 07:33:49 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.drawable.Icon;
|
2021-07-24 02:37:17 +00:00
|
|
|
import android.os.Build;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.widget.RemoteViews;
|
2021-09-21 07:33:49 +00:00
|
|
|
import androidx.annotation.Nullable;
|
2021-07-24 02:37:17 +00:00
|
|
|
import androidx.annotation.RestrictTo;
|
2021-09-21 07:33:49 +00:00
|
|
|
import androidx.collection.ArraySet;
|
2021-07-24 02:37:17 +00:00
|
|
|
import androidx.core.app.NotificationCompat;
|
2021-09-21 07:33:49 +00:00
|
|
|
import androidx.core.content.LocusIdCompat;
|
2021-07-24 02:37:17 +00:00
|
|
|
import androidx.core.graphics.drawable.IconCompat;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP_PREFIX})
|
|
|
|
public class NotificationCompatBuilder implements NotificationBuilderWithBuilderAccessor {
|
|
|
|
private final List<Bundle> mActionExtrasList = new ArrayList();
|
|
|
|
private RemoteViews mBigContentView;
|
|
|
|
private final Notification.Builder mBuilder;
|
|
|
|
private final NotificationCompat.Builder mBuilderCompat;
|
|
|
|
private RemoteViews mContentView;
|
2021-09-21 07:33:49 +00:00
|
|
|
private final Context mContext;
|
2021-07-24 02:37:17 +00:00
|
|
|
private final Bundle mExtras = new Bundle();
|
|
|
|
private int mGroupAlertBehavior;
|
|
|
|
private RemoteViews mHeadsUpContentView;
|
|
|
|
|
|
|
|
public NotificationCompatBuilder(NotificationCompat.Builder builder) {
|
2021-09-21 07:33:49 +00:00
|
|
|
Icon icon;
|
2021-07-24 02:37:17 +00:00
|
|
|
this.mBuilderCompat = builder;
|
2021-09-21 07:33:49 +00:00
|
|
|
this.mContext = builder.mContext;
|
2021-07-24 02:37:17 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= 26) {
|
|
|
|
this.mBuilder = new Notification.Builder(builder.mContext, builder.mChannelId);
|
|
|
|
} else {
|
|
|
|
this.mBuilder = new Notification.Builder(builder.mContext);
|
|
|
|
}
|
|
|
|
Notification notification = builder.mNotification;
|
|
|
|
this.mBuilder.setWhen(notification.when).setSmallIcon(notification.icon, notification.iconLevel).setContent(notification.contentView).setTicker(notification.tickerText, builder.mTickerView).setVibrate(notification.vibrate).setLights(notification.ledARGB, notification.ledOnMS, notification.ledOffMS).setOngoing((notification.flags & 2) != 0).setOnlyAlertOnce((notification.flags & 8) != 0).setAutoCancel((notification.flags & 16) != 0).setDefaults(notification.defaults).setContentTitle(builder.mContentTitle).setContentText(builder.mContentText).setContentInfo(builder.mContentInfo).setContentIntent(builder.mContentIntent).setDeleteIntent(notification.deleteIntent).setFullScreenIntent(builder.mFullScreenIntent, (notification.flags & 128) != 0).setLargeIcon(builder.mLargeIcon).setNumber(builder.mNumber).setProgress(builder.mProgressMax, builder.mProgress, builder.mProgressIndeterminate);
|
|
|
|
this.mBuilder.setSubText(builder.mSubText).setUsesChronometer(builder.mUseChronometer).setPriority(builder.mPriority);
|
|
|
|
Iterator<NotificationCompat.Action> it = builder.mActions.iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
addAction(it.next());
|
|
|
|
}
|
|
|
|
Bundle bundle = builder.mExtras;
|
|
|
|
if (bundle != null) {
|
|
|
|
this.mExtras.putAll(bundle);
|
|
|
|
}
|
2021-09-21 07:33:49 +00:00
|
|
|
int i = Build.VERSION.SDK_INT;
|
2021-07-24 02:37:17 +00:00
|
|
|
this.mContentView = builder.mContentView;
|
|
|
|
this.mBigContentView = builder.mBigContentView;
|
|
|
|
this.mBuilder.setShowWhen(builder.mShowWhen);
|
|
|
|
this.mBuilder.setLocalOnly(builder.mLocalOnly).setGroup(builder.mGroupKey).setGroupSummary(builder.mGroupSummary).setSortKey(builder.mSortKey);
|
|
|
|
this.mGroupAlertBehavior = builder.mGroupAlertBehavior;
|
|
|
|
this.mBuilder.setCategory(builder.mCategory).setColor(builder.mColor).setVisibility(builder.mVisibility).setPublicVersion(builder.mPublicVersion).setSound(notification.sound, notification.audioAttributes);
|
2021-09-21 07:33:49 +00:00
|
|
|
List<String> combineLists = i < 28 ? combineLists(getPeople(builder.mPersonList), builder.mPeople) : builder.mPeople;
|
|
|
|
if (combineLists != null && !combineLists.isEmpty()) {
|
|
|
|
for (String str : combineLists) {
|
|
|
|
this.mBuilder.addPerson(str);
|
|
|
|
}
|
2021-07-24 02:37:17 +00:00
|
|
|
}
|
|
|
|
this.mHeadsUpContentView = builder.mHeadsUpContentView;
|
|
|
|
if (builder.mInvisibleActions.size() > 0) {
|
2021-08-09 22:50:52 +00:00
|
|
|
Bundle bundle2 = builder.getExtras().getBundle(NotificationCompat.CarExtender.EXTRA_CAR_EXTENDER);
|
2021-07-24 02:37:17 +00:00
|
|
|
bundle2 = bundle2 == null ? new Bundle() : bundle2;
|
2021-09-21 07:33:49 +00:00
|
|
|
Bundle bundle3 = new Bundle(bundle2);
|
|
|
|
Bundle bundle4 = new Bundle();
|
|
|
|
for (int i2 = 0; i2 < builder.mInvisibleActions.size(); i2++) {
|
|
|
|
bundle4.putBundle(Integer.toString(i2), NotificationCompatJellybean.getBundleForAction(builder.mInvisibleActions.get(i2)));
|
2021-07-24 02:37:17 +00:00
|
|
|
}
|
2021-09-21 07:33:49 +00:00
|
|
|
bundle2.putBundle(NotificationCompat.CarExtender.EXTRA_INVISIBLE_ACTIONS, bundle4);
|
|
|
|
bundle3.putBundle(NotificationCompat.CarExtender.EXTRA_INVISIBLE_ACTIONS, bundle4);
|
2021-08-09 22:50:52 +00:00
|
|
|
builder.getExtras().putBundle(NotificationCompat.CarExtender.EXTRA_CAR_EXTENDER, bundle2);
|
2021-09-21 07:33:49 +00:00
|
|
|
this.mExtras.putBundle(NotificationCompat.CarExtender.EXTRA_CAR_EXTENDER, bundle3);
|
2021-07-24 02:37:17 +00:00
|
|
|
}
|
2021-09-21 07:33:49 +00:00
|
|
|
int i3 = Build.VERSION.SDK_INT;
|
|
|
|
if (i3 >= 23 && (icon = builder.mSmallIcon) != null) {
|
|
|
|
this.mBuilder.setSmallIcon(icon);
|
|
|
|
}
|
|
|
|
if (i3 >= 24) {
|
2021-07-24 02:37:17 +00:00
|
|
|
this.mBuilder.setExtras(builder.mExtras).setRemoteInputHistory(builder.mRemoteInputHistory);
|
|
|
|
RemoteViews remoteViews = builder.mContentView;
|
|
|
|
if (remoteViews != null) {
|
|
|
|
this.mBuilder.setCustomContentView(remoteViews);
|
|
|
|
}
|
|
|
|
RemoteViews remoteViews2 = builder.mBigContentView;
|
|
|
|
if (remoteViews2 != null) {
|
|
|
|
this.mBuilder.setCustomBigContentView(remoteViews2);
|
|
|
|
}
|
|
|
|
RemoteViews remoteViews3 = builder.mHeadsUpContentView;
|
|
|
|
if (remoteViews3 != null) {
|
|
|
|
this.mBuilder.setCustomHeadsUpContentView(remoteViews3);
|
|
|
|
}
|
|
|
|
}
|
2021-09-21 07:33:49 +00:00
|
|
|
if (i3 >= 26) {
|
|
|
|
this.mBuilder.setBadgeIconType(builder.mBadgeIcon).setSettingsText(builder.mSettingsText).setShortcutId(builder.mShortcutId).setTimeoutAfter(builder.mTimeout).setGroupAlertBehavior(builder.mGroupAlertBehavior);
|
2021-07-24 02:37:17 +00:00
|
|
|
if (builder.mColorizedSet) {
|
|
|
|
this.mBuilder.setColorized(builder.mColorized);
|
|
|
|
}
|
|
|
|
if (!TextUtils.isEmpty(builder.mChannelId)) {
|
|
|
|
this.mBuilder.setSound(null).setDefaults(0).setLights(0, 0, 0).setVibrate(null);
|
|
|
|
}
|
|
|
|
}
|
2021-09-21 07:33:49 +00:00
|
|
|
if (i3 >= 28) {
|
|
|
|
Iterator<Person> it2 = builder.mPersonList.iterator();
|
|
|
|
while (it2.hasNext()) {
|
|
|
|
this.mBuilder.addPerson(it2.next().toAndroidPerson());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int i4 = Build.VERSION.SDK_INT;
|
|
|
|
if (i4 >= 29) {
|
2021-07-24 02:37:17 +00:00
|
|
|
this.mBuilder.setAllowSystemGeneratedContextualActions(builder.mAllowSystemGeneratedContextualActions);
|
|
|
|
this.mBuilder.setBubbleMetadata(NotificationCompat.BubbleMetadata.toPlatform(builder.mBubbleMetadata));
|
2021-09-21 07:33:49 +00:00
|
|
|
LocusIdCompat locusIdCompat = builder.mLocusId;
|
|
|
|
if (locusIdCompat != null) {
|
|
|
|
this.mBuilder.setLocusId(locusIdCompat.toLocusId());
|
|
|
|
}
|
2021-07-24 02:37:17 +00:00
|
|
|
}
|
|
|
|
if (builder.mSilent) {
|
|
|
|
if (this.mBuilderCompat.mGroupSummary) {
|
|
|
|
this.mGroupAlertBehavior = 2;
|
|
|
|
} else {
|
|
|
|
this.mGroupAlertBehavior = 1;
|
|
|
|
}
|
|
|
|
this.mBuilder.setVibrate(null);
|
|
|
|
this.mBuilder.setSound(null);
|
2021-09-21 07:33:49 +00:00
|
|
|
int i5 = notification.defaults & -2;
|
|
|
|
notification.defaults = i5;
|
|
|
|
int i6 = i5 & -3;
|
|
|
|
notification.defaults = i6;
|
|
|
|
this.mBuilder.setDefaults(i6);
|
|
|
|
if (i4 >= 26) {
|
2021-07-24 02:37:17 +00:00
|
|
|
if (TextUtils.isEmpty(this.mBuilderCompat.mGroupKey)) {
|
2021-08-09 22:50:52 +00:00
|
|
|
this.mBuilder.setGroup(NotificationCompat.GROUP_KEY_SILENT);
|
2021-07-24 02:37:17 +00:00
|
|
|
}
|
|
|
|
this.mBuilder.setGroupAlertBehavior(this.mGroupAlertBehavior);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addAction(NotificationCompat.Action action) {
|
|
|
|
int i = Build.VERSION.SDK_INT;
|
|
|
|
IconCompat iconCompat = action.getIconCompat();
|
|
|
|
Notification.Action.Builder builder = i >= 23 ? new Notification.Action.Builder(iconCompat != null ? iconCompat.toIcon() : null, action.getTitle(), action.getActionIntent()) : new Notification.Action.Builder(iconCompat != null ? iconCompat.getResId() : 0, action.getTitle(), action.getActionIntent());
|
|
|
|
if (action.getRemoteInputs() != null) {
|
|
|
|
for (RemoteInput remoteInput : RemoteInput.fromCompat(action.getRemoteInputs())) {
|
|
|
|
builder.addRemoteInput(remoteInput);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Bundle bundle = action.getExtras() != null ? new Bundle(action.getExtras()) : new Bundle();
|
2021-08-09 22:50:52 +00:00
|
|
|
bundle.putBoolean(NotificationCompatJellybean.EXTRA_ALLOW_GENERATED_REPLIES, action.getAllowGeneratedReplies());
|
2021-07-24 02:37:17 +00:00
|
|
|
int i2 = Build.VERSION.SDK_INT;
|
|
|
|
if (i2 >= 24) {
|
|
|
|
builder.setAllowGeneratedReplies(action.getAllowGeneratedReplies());
|
|
|
|
}
|
2021-08-09 22:50:52 +00:00
|
|
|
bundle.putInt(NotificationCompat.Action.EXTRA_SEMANTIC_ACTION, action.getSemanticAction());
|
2021-07-24 02:37:17 +00:00
|
|
|
if (i2 >= 28) {
|
|
|
|
builder.setSemanticAction(action.getSemanticAction());
|
|
|
|
}
|
|
|
|
if (i2 >= 29) {
|
|
|
|
builder.setContextual(action.isContextual());
|
|
|
|
}
|
2021-08-09 22:50:52 +00:00
|
|
|
bundle.putBoolean(NotificationCompat.Action.EXTRA_SHOWS_USER_INTERFACE, action.getShowsUserInterface());
|
2021-07-24 02:37:17 +00:00
|
|
|
builder.addExtras(bundle);
|
|
|
|
this.mBuilder.addAction(builder.build());
|
|
|
|
}
|
|
|
|
|
2021-09-21 07:33:49 +00:00
|
|
|
@Nullable
|
|
|
|
private static List<String> combineLists(@Nullable List<String> list, @Nullable List<String> list2) {
|
|
|
|
if (list == null) {
|
|
|
|
return list2;
|
|
|
|
}
|
|
|
|
if (list2 == null) {
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
ArraySet arraySet = new ArraySet(list2.size() + list.size());
|
|
|
|
arraySet.addAll(list);
|
|
|
|
arraySet.addAll(list2);
|
|
|
|
return new ArrayList(arraySet);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
private static List<String> getPeople(@Nullable List<Person> list) {
|
|
|
|
if (list == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
ArrayList arrayList = new ArrayList(list.size());
|
|
|
|
for (Person person : list) {
|
|
|
|
arrayList.add(person.resolveToLegacyUri());
|
|
|
|
}
|
|
|
|
return arrayList;
|
|
|
|
}
|
|
|
|
|
2021-07-24 02:37:17 +00:00
|
|
|
private void removeSoundAndVibration(Notification notification) {
|
|
|
|
notification.sound = null;
|
|
|
|
notification.vibrate = null;
|
|
|
|
int i = notification.defaults & -2;
|
|
|
|
notification.defaults = i;
|
|
|
|
notification.defaults = i & -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Notification build() {
|
|
|
|
Bundle extras;
|
|
|
|
RemoteViews makeHeadsUpContentView;
|
|
|
|
RemoteViews makeBigContentView;
|
|
|
|
NotificationCompat.Style style = this.mBuilderCompat.mStyle;
|
|
|
|
if (style != null) {
|
|
|
|
style.apply(this);
|
|
|
|
}
|
|
|
|
RemoteViews makeContentView = style != null ? style.makeContentView(this) : null;
|
|
|
|
Notification buildInternal = buildInternal();
|
|
|
|
if (makeContentView != null) {
|
|
|
|
buildInternal.contentView = makeContentView;
|
|
|
|
} else {
|
|
|
|
RemoteViews remoteViews = this.mBuilderCompat.mContentView;
|
|
|
|
if (remoteViews != null) {
|
|
|
|
buildInternal.contentView = remoteViews;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(style == null || (makeBigContentView = style.makeBigContentView(this)) == null)) {
|
|
|
|
buildInternal.bigContentView = makeBigContentView;
|
|
|
|
}
|
|
|
|
if (!(style == null || (makeHeadsUpContentView = this.mBuilderCompat.mStyle.makeHeadsUpContentView(this)) == null)) {
|
|
|
|
buildInternal.headsUpContentView = makeHeadsUpContentView;
|
|
|
|
}
|
|
|
|
if (!(style == null || (extras = NotificationCompat.getExtras(buildInternal)) == null)) {
|
|
|
|
style.addCompatExtras(extras);
|
|
|
|
}
|
|
|
|
return buildInternal;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Notification buildInternal() {
|
|
|
|
int i = Build.VERSION.SDK_INT;
|
|
|
|
if (i >= 26) {
|
|
|
|
return this.mBuilder.build();
|
|
|
|
}
|
|
|
|
if (i >= 24) {
|
|
|
|
Notification build = this.mBuilder.build();
|
|
|
|
if (this.mGroupAlertBehavior != 0) {
|
|
|
|
if (!(build.getGroup() == null || (build.flags & 512) == 0 || this.mGroupAlertBehavior != 2)) {
|
|
|
|
removeSoundAndVibration(build);
|
|
|
|
}
|
|
|
|
if (build.getGroup() != null && (build.flags & 512) == 0 && this.mGroupAlertBehavior == 1) {
|
|
|
|
removeSoundAndVibration(build);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return build;
|
|
|
|
}
|
|
|
|
this.mBuilder.setExtras(this.mExtras);
|
|
|
|
Notification build2 = this.mBuilder.build();
|
|
|
|
RemoteViews remoteViews = this.mContentView;
|
|
|
|
if (remoteViews != null) {
|
|
|
|
build2.contentView = remoteViews;
|
|
|
|
}
|
|
|
|
RemoteViews remoteViews2 = this.mBigContentView;
|
|
|
|
if (remoteViews2 != null) {
|
|
|
|
build2.bigContentView = remoteViews2;
|
|
|
|
}
|
|
|
|
RemoteViews remoteViews3 = this.mHeadsUpContentView;
|
|
|
|
if (remoteViews3 != null) {
|
|
|
|
build2.headsUpContentView = remoteViews3;
|
|
|
|
}
|
|
|
|
if (this.mGroupAlertBehavior != 0) {
|
|
|
|
if (!(build2.getGroup() == null || (build2.flags & 512) == 0 || this.mGroupAlertBehavior != 2)) {
|
|
|
|
removeSoundAndVibration(build2);
|
|
|
|
}
|
|
|
|
if (build2.getGroup() != null && (build2.flags & 512) == 0 && this.mGroupAlertBehavior == 1) {
|
|
|
|
removeSoundAndVibration(build2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return build2;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // androidx.core.app.NotificationBuilderWithBuilderAccessor
|
|
|
|
public Notification.Builder getBuilder() {
|
|
|
|
return this.mBuilder;
|
|
|
|
}
|
2021-09-21 07:33:49 +00:00
|
|
|
|
|
|
|
public Context getContext() {
|
|
|
|
return this.mContext;
|
|
|
|
}
|
2021-07-24 02:37:17 +00:00
|
|
|
}
|