1 /*
2  * Copyright (C) 2020 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 
17 package com.android.wm.shell.bubbles;
18 
19 import static android.app.Notification.FLAG_BUBBLE;
20 
21 import android.app.Notification;
22 import android.app.Notification.BubbleMetadata;
23 import android.app.NotificationManager.Policy;
24 import android.content.LocusId;
25 import android.service.notification.NotificationListenerService.Ranking;
26 import android.service.notification.StatusBarNotification;
27 
28 import androidx.annotation.NonNull;
29 import androidx.annotation.Nullable;
30 
31 /**
32  * Represents a notification with needed data and flag for bubbles.
33  *
34  * @see Bubble
35  */
36 public class BubbleEntry {
37 
38     private StatusBarNotification mSbn;
39     private Ranking mRanking;
40 
41     private boolean mIsDismissable;
42     private boolean mShouldSuppressNotificationDot;
43     private boolean mShouldSuppressNotificationList;
44     private boolean mShouldSuppressPeek;
45 
BubbleEntry(@onNull StatusBarNotification sbn, Ranking ranking, boolean isDismissable, boolean shouldSuppressNotificationDot, boolean shouldSuppressNotificationList, boolean shouldSuppressPeek)46     public BubbleEntry(@NonNull StatusBarNotification sbn,
47             Ranking ranking, boolean isDismissable, boolean shouldSuppressNotificationDot,
48             boolean shouldSuppressNotificationList, boolean shouldSuppressPeek) {
49         mSbn = sbn;
50         mRanking = ranking;
51 
52         mIsDismissable = isDismissable;
53         mShouldSuppressNotificationDot = shouldSuppressNotificationDot;
54         mShouldSuppressNotificationList = shouldSuppressNotificationList;
55         mShouldSuppressPeek = shouldSuppressPeek;
56     }
57 
58     /** @return the {@link StatusBarNotification} for this entry. */
59     @NonNull
getStatusBarNotification()60     public StatusBarNotification getStatusBarNotification() {
61         return mSbn;
62     }
63 
64     /** @return the {@link Ranking} for this entry. */
getRanking()65     public Ranking getRanking() {
66         return mRanking;
67     }
68 
69     /** @return the key in the {@link StatusBarNotification}. */
getKey()70     public String getKey() {
71         return mSbn.getKey();
72     }
73 
74     /** @return the group key in the {@link StatusBarNotification}. */
getGroupKey()75     public String getGroupKey() {
76         return mSbn.getGroupKey();
77     }
78 
79     /** @return the {@link LocusId} for this notification, if it exists. */
getLocusId()80     public LocusId getLocusId() {
81         return mSbn.getNotification().getLocusId();
82     }
83 
84     /** @return the {@link BubbleMetadata} in the {@link StatusBarNotification}. */
85     @Nullable
getBubbleMetadata()86     public BubbleMetadata getBubbleMetadata() {
87         return getStatusBarNotification().getNotification().getBubbleMetadata();
88     }
89 
90     /**
91      * Updates the {@link Notification#FLAG_BUBBLE} flag on this notification to indicate
92      * whether it is a bubble or not. If this entry is set to not bubble, or does not have
93      * the required info to bubble, the flag cannot be set to true.
94      *
95      * @param shouldBubble whether this notification should be flagged as a bubble.
96      * @return true if the value changed.
97      */
setFlagBubble(boolean shouldBubble)98     public boolean setFlagBubble(boolean shouldBubble) {
99         boolean wasBubble = isBubble();
100         if (!shouldBubble) {
101             mSbn.getNotification().flags &= ~FLAG_BUBBLE;
102         } else if (getBubbleMetadata() != null && canBubble()) {
103             // wants to be bubble & can bubble, set flag
104             mSbn.getNotification().flags |= FLAG_BUBBLE;
105         }
106         return wasBubble != isBubble();
107     }
108 
isBubble()109     public boolean isBubble() {
110         return (mSbn.getNotification().flags & FLAG_BUBBLE) != 0;
111     }
112 
113     /** @see Ranking#canBubble() */
canBubble()114     public boolean canBubble() {
115         return mRanking.canBubble();
116     }
117 
118     /** @return true if this notification can be dismissed. */
isDismissable()119     public boolean isDismissable() {
120         return mIsDismissable;
121     }
122 
123     /** @return true if {@link Policy#SUPPRESSED_EFFECT_BADGE} set for this notification. */
shouldSuppressNotificationDot()124     public boolean shouldSuppressNotificationDot() {
125         return mShouldSuppressNotificationDot;
126     }
127 
128     /**
129      * @return true if {@link Policy#SUPPRESSED_EFFECT_NOTIFICATION_LIST}
130      * set for this notification.
131      */
shouldSuppressNotificationList()132     public boolean shouldSuppressNotificationList() {
133         return mShouldSuppressNotificationList;
134     }
135 
136     /** @return true if {@link Policy#SUPPRESSED_EFFECT_PEEK} set for this notification. */
shouldSuppressPeek()137     public boolean shouldSuppressPeek() {
138         return mShouldSuppressPeek;
139     }
140 }
141