• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
18 
19 import android.annotation.Nullable;
20 import android.app.Notification;
21 import android.content.Context;
22 import android.os.UserHandle;
23 import android.service.notification.StatusBarNotification;
24 
25 import com.android.internal.logging.InstanceId;
26 
27 /**
28  * Convenience builder for {@link StatusBarNotification} since its constructor is terrifying.
29  *
30  * Only for use in tests.
31  */
32 public class SbnBuilder {
33     private String mPkg = "test_pkg";
34     private String mOpPkg;
35     private int mId;
36     private String mTag;
37     private int mUid;
38     private int mInitialPid;
39     @Nullable private Notification mNotification;
40     @Nullable private Notification.Builder mNotificationBuilder;
41     private Notification.BubbleMetadata mBubbleMetadata;
42     private UserHandle mUser = UserHandle.of(0);
43     private String mOverrideGroupKey;
44     private long mPostTime;
45     private InstanceId mInstanceId;
46 
SbnBuilder()47     public SbnBuilder() {
48     }
49 
SbnBuilder(StatusBarNotification source)50     public SbnBuilder(StatusBarNotification source) {
51         mPkg = source.getPackageName();
52         mOpPkg = source.getOpPkg();
53         mId = source.getId();
54         mTag = source.getTag();
55         mUid = source.getUid();
56         mInitialPid = source.getInitialPid();
57         mNotification = source.getNotification();
58         mUser = source.getUser();
59         mOverrideGroupKey = source.getOverrideGroupKey();
60         mPostTime = source.getPostTime();
61         mInstanceId = source.getInstanceId();
62     }
63 
build()64     public StatusBarNotification build() {
65         Notification notification;
66         if (mNotificationBuilder != null) {
67             notification = mNotificationBuilder.build();
68         } else if (mNotification != null) {
69             notification = mNotification;
70         } else {
71             notification = new Notification();
72         }
73 
74         if (mBubbleMetadata != null) {
75             notification.setBubbleMetadata(mBubbleMetadata);
76         }
77 
78         StatusBarNotification result = new StatusBarNotification(
79                 mPkg,
80                 mOpPkg,
81                 mId,
82                 mTag,
83                 mUid,
84                 mInitialPid,
85                 notification,
86                 mUser,
87                 mOverrideGroupKey,
88                 mPostTime);
89         if (mInstanceId != null) {
90             result.setInstanceId(mInstanceId);
91         }
92         return result;
93     }
94 
setPkg(String pkg)95     public SbnBuilder setPkg(String pkg) {
96         mPkg = pkg;
97         return this;
98     }
99 
setOpPkg(String opPkg)100     public SbnBuilder setOpPkg(String opPkg) {
101         mOpPkg = opPkg;
102         return this;
103     }
104 
setId(int id)105     public SbnBuilder setId(int id) {
106         mId = id;
107         return this;
108     }
109 
setTag(String tag)110     public SbnBuilder setTag(String tag) {
111         mTag = tag;
112         return this;
113     }
114 
setUid(int uid)115     public SbnBuilder setUid(int uid) {
116         mUid = uid;
117         return this;
118     }
119 
setInitialPid(int initialPid)120     public SbnBuilder setInitialPid(int initialPid) {
121         mInitialPid = initialPid;
122         return this;
123     }
124 
setNotification(Notification notification)125     public SbnBuilder setNotification(Notification notification) {
126         mNotification = notification;
127         mNotificationBuilder = null;
128         return this;
129     }
130 
setContentTitle(Context context, String contentTitle)131     public SbnBuilder setContentTitle(Context context, String contentTitle) {
132         modifyNotification(context).setContentTitle(contentTitle);
133         return this;
134     }
135 
setContentText(Context context, String contentText)136     public SbnBuilder setContentText(Context context, String contentText) {
137         modifyNotification(context).setContentText(contentText);
138         return this;
139     }
140 
setGroup(Context context, String groupKey)141     public SbnBuilder setGroup(Context context, String groupKey) {
142         modifyNotification(context).setGroup(groupKey);
143         return this;
144     }
145 
setGroupSummary(Context context, boolean isGroupSummary)146     public SbnBuilder setGroupSummary(Context context, boolean isGroupSummary) {
147         modifyNotification(context).setGroupSummary(isGroupSummary);
148         return this;
149     }
150 
setFlag(Context context, int mask, boolean value)151     public SbnBuilder setFlag(Context context, int mask, boolean value) {
152         modifyNotification(context).setFlag(mask, value);
153         return this;
154     }
155 
modifyNotification(Context context)156     public Notification.Builder modifyNotification(Context context) {
157         if (mNotification != null) {
158             mNotificationBuilder = new Notification.Builder(context, mNotification);
159             mNotification = null;
160         } else if (mNotificationBuilder == null) {
161             mNotificationBuilder = new Notification.Builder(context);
162         }
163 
164         return mNotificationBuilder;
165     }
166 
setUser(UserHandle user)167     public SbnBuilder setUser(UserHandle user) {
168         mUser = user;
169         return this;
170     }
171 
setOverrideGroupKey(String overrideGroupKey)172     public SbnBuilder setOverrideGroupKey(String overrideGroupKey) {
173         mOverrideGroupKey = overrideGroupKey;
174         return this;
175     }
176 
setPostTime(long postTime)177     public SbnBuilder setPostTime(long postTime) {
178         mPostTime = postTime;
179         return this;
180     }
181 
setBubbleMetadata(Notification.BubbleMetadata data)182     public SbnBuilder setBubbleMetadata(Notification.BubbleMetadata data) {
183         mBubbleMetadata = data;
184         return this;
185     }
186 
setInstanceId(InstanceId instanceId)187     public SbnBuilder setInstanceId(InstanceId instanceId) {
188         mInstanceId = instanceId;
189         return this;
190     }
191 }
192