1 /*
2  * Copyright (C) 2019 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.systemui.statusbar.notification.collection;
18 
19 import android.annotation.Nullable;
20 import android.app.Notification;
21 import android.app.NotificationChannel;
22 import android.app.NotificationManager;
23 import android.content.Context;
24 import android.content.pm.ShortcutInfo;
25 import android.os.UserHandle;
26 import android.service.notification.SnoozeCriterion;
27 import android.service.notification.StatusBarNotification;
28 
29 import com.android.internal.logging.InstanceId;
30 import com.android.systemui.statusbar.RankingBuilder;
31 import com.android.systemui.statusbar.SbnBuilder;
32 import com.android.systemui.util.time.FakeSystemClock;
33 
34 import java.util.ArrayList;
35 
36 /**
37  * Combined builder for constructing a NotificationEntry and its associated StatusBarNotification
38  * and Ranking. Is largely a proxy for the SBN and Ranking builders, but does a little extra magic
39  * to make sure the keys match between the two, etc.
40  *
41  * Has the ability to set ListEntry properties as well.
42  *
43  * Only for use in tests.
44  */
45 public class NotificationEntryBuilder {
46     private final SbnBuilder mSbnBuilder = new SbnBuilder();
47     private final RankingBuilder mRankingBuilder = new RankingBuilder();
48     private final FakeSystemClock mClock = new FakeSystemClock();
49     private StatusBarNotification mSbn = null;
50 
51     /* ListEntry properties */
52     private GroupEntry mParent;
53     private int mSection = -1;
54 
55     /* If set, use this creation time instead of mClock.uptimeMillis */
56     private long mCreationTime = -1;
57 
build()58     public NotificationEntry build() {
59         StatusBarNotification sbn = mSbn != null ? mSbn : mSbnBuilder.build();
60         mRankingBuilder.setKey(sbn.getKey());
61         long creationTime = mCreationTime != -1 ? mCreationTime : mClock.uptimeMillis();
62         final NotificationEntry entry = new NotificationEntry(
63                 sbn, mRankingBuilder.build(), mClock.uptimeMillis());
64 
65         /* ListEntry properties */
66         entry.setParent(mParent);
67         entry.getAttachState().setSectionIndex(mSection);
68         return entry;
69     }
70 
71     /**
72      * Sets the parent.
73      */
setParent(@ullable GroupEntry parent)74     public NotificationEntryBuilder setParent(@Nullable GroupEntry parent) {
75         mParent = parent;
76         return this;
77     }
78 
79     /**
80      * Sets the section.
81      */
setSection(int section)82     public NotificationEntryBuilder setSection(int section) {
83         mSection = section;
84         return this;
85     }
86 
87     /**
88      * Sets the SBN directly. If set, causes all calls to delegated SbnBuilder methods to be
89      * ignored.
90      */
setSbn(@ullable StatusBarNotification sbn)91     public NotificationEntryBuilder setSbn(@Nullable StatusBarNotification sbn) {
92         mSbn = sbn;
93         return this;
94     }
95 
96     /**
97      * Set the creation time
98      */
setCreationTime(long creationTime)99     public NotificationEntryBuilder setCreationTime(long creationTime) {
100         mCreationTime = creationTime;
101         return this;
102     }
103 
104     /* Delegated to SbnBuilder */
105 
setPkg(String pkg)106     public NotificationEntryBuilder setPkg(String pkg) {
107         mSbnBuilder.setPkg(pkg);
108         return this;
109     }
110 
setOpPkg(String opPkg)111     public NotificationEntryBuilder setOpPkg(String opPkg) {
112         mSbnBuilder.setOpPkg(opPkg);
113         return this;
114     }
115 
setId(int id)116     public NotificationEntryBuilder setId(int id) {
117         mSbnBuilder.setId(id);
118         return this;
119     }
120 
setTag(String tag)121     public NotificationEntryBuilder setTag(String tag) {
122         mSbnBuilder.setTag(tag);
123         return this;
124     }
125 
setUid(int uid)126     public NotificationEntryBuilder setUid(int uid) {
127         mSbnBuilder.setUid(uid);
128         return this;
129     }
130 
setInitialPid(int initialPid)131     public NotificationEntryBuilder setInitialPid(int initialPid) {
132         mSbnBuilder.setInitialPid(initialPid);
133         return this;
134     }
135 
setNotification(Notification notification)136     public NotificationEntryBuilder setNotification(Notification notification) {
137         mSbnBuilder.setNotification(notification);
138         return this;
139     }
140 
modifyNotification(Context context)141     public Notification.Builder modifyNotification(Context context) {
142         return mSbnBuilder.modifyNotification(context);
143     }
144 
setUser(UserHandle user)145     public NotificationEntryBuilder setUser(UserHandle user) {
146         mSbnBuilder.setUser(user);
147         return this;
148     }
149 
setOverrideGroupKey(String overrideGroupKey)150     public NotificationEntryBuilder setOverrideGroupKey(String overrideGroupKey) {
151         mSbnBuilder.setOverrideGroupKey(overrideGroupKey);
152         return this;
153     }
154 
setPostTime(long postTime)155     public NotificationEntryBuilder setPostTime(long postTime) {
156         mSbnBuilder.setPostTime(postTime);
157         return this;
158     }
159 
setInstanceId(InstanceId instanceId)160     public NotificationEntryBuilder setInstanceId(InstanceId instanceId) {
161         mSbnBuilder.setInstanceId(instanceId);
162         return this;
163     }
164 
165     /* Delegated to Notification.Builder (via SbnBuilder) */
166 
setContentTitle(Context context, String contentTitle)167     public NotificationEntryBuilder setContentTitle(Context context, String contentTitle) {
168         mSbnBuilder.setContentTitle(context, contentTitle);
169         return this;
170     }
171 
setContentText(Context context, String contentText)172     public NotificationEntryBuilder setContentText(Context context, String contentText) {
173         mSbnBuilder.setContentText(context, contentText);
174         return this;
175     }
176 
setGroup(Context context, String groupKey)177     public NotificationEntryBuilder setGroup(Context context, String groupKey) {
178         mSbnBuilder.setGroup(context, groupKey);
179         return this;
180     }
181 
setGroupSummary(Context context, boolean isGroupSummary)182     public NotificationEntryBuilder setGroupSummary(Context context, boolean isGroupSummary) {
183         mSbnBuilder.setGroupSummary(context, isGroupSummary);
184         return this;
185     }
186 
setFlag(Context context, int mask, boolean value)187     public NotificationEntryBuilder setFlag(Context context, int mask, boolean value) {
188         mSbnBuilder.setFlag(context, mask, value);
189         return this;
190     }
191 
192     /* Delegated to RankingBuilder */
193 
setRank(int rank)194     public NotificationEntryBuilder setRank(int rank) {
195         mRankingBuilder.setRank(rank);
196         return this;
197     }
198 
setMatchesInterruptionFilter( boolean matchesInterruptionFilter)199     public NotificationEntryBuilder setMatchesInterruptionFilter(
200             boolean matchesInterruptionFilter) {
201         mRankingBuilder.setMatchesInterruptionFilter(matchesInterruptionFilter);
202         return this;
203     }
204 
setVisibilityOverride(int visibilityOverride)205     public NotificationEntryBuilder setVisibilityOverride(int visibilityOverride) {
206         mRankingBuilder.setVisibilityOverride(visibilityOverride);
207         return this;
208     }
209 
setSuppressedVisualEffects(int suppressedVisualEffects)210     public NotificationEntryBuilder setSuppressedVisualEffects(int suppressedVisualEffects) {
211         mRankingBuilder.setSuppressedVisualEffects(suppressedVisualEffects);
212         return this;
213     }
214 
setExplanation(CharSequence explanation)215     public NotificationEntryBuilder setExplanation(CharSequence explanation) {
216         mRankingBuilder.setExplanation(explanation);
217         return this;
218     }
219 
setAdditionalPeople(ArrayList<String> additionalPeople)220     public NotificationEntryBuilder setAdditionalPeople(ArrayList<String> additionalPeople) {
221         mRankingBuilder.setAdditionalPeople(additionalPeople);
222         return this;
223     }
224 
setSnoozeCriteria( ArrayList<SnoozeCriterion> snoozeCriteria)225     public NotificationEntryBuilder setSnoozeCriteria(
226             ArrayList<SnoozeCriterion> snoozeCriteria) {
227         mRankingBuilder.setSnoozeCriteria(snoozeCriteria);
228         return this;
229     }
230 
setCanShowBadge(boolean canShowBadge)231     public NotificationEntryBuilder setCanShowBadge(boolean canShowBadge) {
232         mRankingBuilder.setCanShowBadge(canShowBadge);
233         return this;
234     }
235 
setSuspended(boolean suspended)236     public NotificationEntryBuilder setSuspended(boolean suspended) {
237         mRankingBuilder.setSuspended(suspended);
238         return this;
239     }
240 
setLastAudiblyAlertedMs(long lastAudiblyAlertedMs)241     public NotificationEntryBuilder setLastAudiblyAlertedMs(long lastAudiblyAlertedMs) {
242         mRankingBuilder.setLastAudiblyAlertedMs(lastAudiblyAlertedMs);
243         return this;
244     }
245 
setNoisy(boolean noisy)246     public NotificationEntryBuilder setNoisy(boolean noisy) {
247         mRankingBuilder.setNoisy(noisy);
248         return this;
249     }
250 
setCanBubble(boolean canBubble)251     public NotificationEntryBuilder setCanBubble(boolean canBubble) {
252         mRankingBuilder.setCanBubble(canBubble);
253         return this;
254     }
255 
setImportance(@otificationManager.Importance int importance)256     public NotificationEntryBuilder setImportance(@NotificationManager.Importance int importance) {
257         mRankingBuilder.setImportance(importance);
258         return this;
259     }
260 
setUserSentiment(int userSentiment)261     public NotificationEntryBuilder setUserSentiment(int userSentiment) {
262         mRankingBuilder.setUserSentiment(userSentiment);
263         return this;
264     }
265 
setChannel(NotificationChannel channel)266     public NotificationEntryBuilder setChannel(NotificationChannel channel) {
267         mRankingBuilder.setChannel(channel);
268         return this;
269     }
270 
setSmartActions( ArrayList<Notification.Action> smartActions)271     public NotificationEntryBuilder setSmartActions(
272             ArrayList<Notification.Action> smartActions) {
273         mRankingBuilder.setSmartActions(smartActions);
274         return this;
275     }
276 
setSmartActions(Notification.Action... smartActions)277     public NotificationEntryBuilder setSmartActions(Notification.Action... smartActions) {
278         mRankingBuilder.setSmartActions(smartActions);
279         return this;
280     }
281 
setSmartReplies(ArrayList<CharSequence> smartReplies)282     public NotificationEntryBuilder setSmartReplies(ArrayList<CharSequence> smartReplies) {
283         mRankingBuilder.setSmartReplies(smartReplies);
284         return this;
285     }
286 
setSmartReplies(CharSequence... smartReplies)287     public NotificationEntryBuilder setSmartReplies(CharSequence... smartReplies) {
288         mRankingBuilder.setSmartReplies(smartReplies);
289         return this;
290     }
291 
setShortcutInfo(ShortcutInfo shortcutInfo)292     public NotificationEntryBuilder setShortcutInfo(ShortcutInfo shortcutInfo) {
293         mRankingBuilder.setShortcutInfo(shortcutInfo);
294         return this;
295     }
296 }
297