blob: 3c919a17df6ced900342725e42bf16cbb8845c19 [file] [log] [blame]
Selim Cinek6fd06b52017-03-07 15:54:10 -08001/*
2 * Copyright (C) 2017 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
17package com.android.systemui.statusbar;
18
Julia Reynolds0abae112018-06-08 10:36:57 -040019import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
Mady Mellorb4991e62019-01-10 15:14:51 -080020import static android.app.NotificationManager.IMPORTANCE_HIGH;
Julia Reynolds0abae112018-06-08 10:36:57 -040021
Rohan Shah524cf7b2018-03-15 14:40:02 -070022import android.annotation.Nullable;
Selim Cinek6fd06b52017-03-07 15:54:10 -080023import android.app.ActivityManager;
Selim Cinek5ba22542017-04-20 15:16:10 -070024import android.app.Instrumentation;
Selim Cinek6fd06b52017-03-07 15:54:10 -080025import android.app.Notification;
Julia Reynolds0abae112018-06-08 10:36:57 -040026import android.app.NotificationChannel;
Mady Mellorb4991e62019-01-10 15:14:51 -080027import android.app.PendingIntent;
Selim Cinek6fd06b52017-03-07 15:54:10 -080028import android.content.Context;
Mady Mellorb4991e62019-01-10 15:14:51 -080029import android.content.Intent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -080030import android.graphics.drawable.Icon;
Selim Cinek6fd06b52017-03-07 15:54:10 -080031import android.os.UserHandle;
32import android.service.notification.StatusBarNotification;
Selim Cinek5ba22542017-04-20 15:16:10 -070033import android.support.test.InstrumentationRegistry;
Rohan Shah524cf7b2018-03-15 14:40:02 -070034import android.text.TextUtils;
Selim Cinek6fd06b52017-03-07 15:54:10 -080035import android.view.LayoutInflater;
36import android.widget.RemoteViews;
37
38import com.android.systemui.R;
Mark Renoufa9fbb6e2019-02-11 12:48:22 -050039import com.android.systemui.bubbles.BubblesTestActivity;
Ned Burnsf81c4c42019-01-07 14:10:43 -050040import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellorebdbbb92018-11-15 14:36:48 -080041import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
Ned Burns1a5e22f2019-02-14 15:11:52 -050042import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag;
43import com.android.systemui.statusbar.notification.row.NotificationContentInflaterTest;
yoshiki iguchi4e30e762018-02-06 12:09:23 +090044import com.android.systemui.statusbar.phone.HeadsUpManagerPhone;
Selim Cinek6fd06b52017-03-07 15:54:10 -080045import com.android.systemui.statusbar.phone.NotificationGroupManager;
Selim Cinek5cf1d052017-06-01 17:36:46 -070046import com.android.systemui.statusbar.policy.HeadsUpManager;
Selim Cinek6fd06b52017-03-07 15:54:10 -080047
48/**
Rohan Shah524cf7b2018-03-15 14:40:02 -070049 * A helper class to create {@link ExpandableNotificationRow} (for both individual and group
50 * notifications).
Selim Cinek6fd06b52017-03-07 15:54:10 -080051 */
52public class NotificationTestHelper {
53
Rohan Shah20790b82018-07-02 17:21:04 -070054 /** Package name for testing purposes. */
55 public static final String PKG = "com.android.systemui";
56 /** System UI id for testing purposes. */
57 public static final int UID = 1000;
Yohei Yukawa72769462019-01-20 09:28:08 -080058 /** Current {@link UserHandle} of the system. */
59 public static final UserHandle USER_HANDLE = UserHandle.of(ActivityManager.getCurrentUser());
Rohan Shah20790b82018-07-02 17:21:04 -070060
Rohan Shah524cf7b2018-03-15 14:40:02 -070061 private static final String GROUP_KEY = "gruKey";
62
Selim Cinek6fd06b52017-03-07 15:54:10 -080063 private final Context mContext;
Selim Cinek5ba22542017-04-20 15:16:10 -070064 private final Instrumentation mInstrumentation;
Selim Cinek6fd06b52017-03-07 15:54:10 -080065 private int mId;
66 private final NotificationGroupManager mGroupManager = new NotificationGroupManager();
Selim Cinek5ba22542017-04-20 15:16:10 -070067 private ExpandableNotificationRow mRow;
Selim Cinek5cf1d052017-06-01 17:36:46 -070068 private HeadsUpManager mHeadsUpManager;
Selim Cinek6fd06b52017-03-07 15:54:10 -080069
70 public NotificationTestHelper(Context context) {
71 mContext = context;
Selim Cinek5ba22542017-04-20 15:16:10 -070072 mInstrumentation = InstrumentationRegistry.getInstrumentation();
yoshiki iguchi4e30e762018-02-06 12:09:23 +090073 mHeadsUpManager = new HeadsUpManagerPhone(mContext, null, mGroupManager, null, null);
Kevina97ea052018-09-11 13:53:18 -070074 mGroupManager.setHeadsUpManager(mHeadsUpManager);
Selim Cinek6fd06b52017-03-07 15:54:10 -080075 }
76
Kevind4660b22018-09-27 10:57:35 -070077 /**
78 * Creates a generic row.
79 *
80 * @return a generic row with no special properties.
81 * @throws Exception
82 */
Rohan Shah524cf7b2018-03-15 14:40:02 -070083 public ExpandableNotificationRow createRow() throws Exception {
Yohei Yukawa72769462019-01-20 09:28:08 -080084 return createRow(PKG, UID, USER_HANDLE);
Rohan Shah524cf7b2018-03-15 14:40:02 -070085 }
86
Kevind4660b22018-09-27 10:57:35 -070087 /**
88 * Create a row with the package and user id specified.
89 *
90 * @param pkg package
91 * @param uid user id
92 * @return a row with a notification using the package and user id
93 * @throws Exception
94 */
Yohei Yukawa72769462019-01-20 09:28:08 -080095 public ExpandableNotificationRow createRow(String pkg, int uid, UserHandle userHandle)
96 throws Exception {
97 return createRow(pkg, uid, userHandle, false /* isGroupSummary */, null /* groupKey */);
Rohan Shah524cf7b2018-03-15 14:40:02 -070098 }
99
Kevind4660b22018-09-27 10:57:35 -0700100 /**
101 * Creates a row based off the notification given.
102 *
103 * @param notification the notification
104 * @return a row built off the notification
105 * @throws Exception
106 */
Rohan Shah524cf7b2018-03-15 14:40:02 -0700107 public ExpandableNotificationRow createRow(Notification notification) throws Exception {
Yohei Yukawa72769462019-01-20 09:28:08 -0800108 return generateRow(notification, PKG, UID, USER_HANDLE, 0 /* extraInflationFlags */);
Kevind4660b22018-09-27 10:57:35 -0700109 }
110
111 /**
112 * Create a row with the specified content views inflated in addition to the default.
113 *
114 * @param extraInflationFlags the flags corresponding to the additional content views that
115 * should be inflated
116 * @return a row with the specified content views inflated in addition to the default
117 * @throws Exception
118 */
119 public ExpandableNotificationRow createRow(@InflationFlag int extraInflationFlags)
120 throws Exception {
Yohei Yukawa72769462019-01-20 09:28:08 -0800121 return generateRow(createNotification(), PKG, UID, USER_HANDLE, extraInflationFlags);
Rohan Shah524cf7b2018-03-15 14:40:02 -0700122 }
123
124 /**
125 * Returns an {@link ExpandableNotificationRow} group with the given number of child
126 * notifications.
127 */
128 public ExpandableNotificationRow createGroup(int numChildren) throws Exception {
129 ExpandableNotificationRow row = createGroupSummary(GROUP_KEY);
130 for (int i = 0; i < numChildren; i++) {
131 ExpandableNotificationRow childRow = createGroupChild(GROUP_KEY);
132 row.addChildNotification(childRow);
133 }
134 return row;
135 }
136
137 /** Returns a group notification with 2 child notifications. */
138 public ExpandableNotificationRow createGroup() throws Exception {
139 return createGroup(2);
140 }
141
142 private ExpandableNotificationRow createGroupSummary(String groupkey) throws Exception {
Yohei Yukawa72769462019-01-20 09:28:08 -0800143 return createRow(PKG, UID, USER_HANDLE, true /* isGroupSummary */, groupkey);
Rohan Shah524cf7b2018-03-15 14:40:02 -0700144 }
145
146 private ExpandableNotificationRow createGroupChild(String groupkey) throws Exception {
Yohei Yukawa72769462019-01-20 09:28:08 -0800147 return createRow(PKG, UID, USER_HANDLE, false /* isGroupSummary */, groupkey);
Mady Mellorb4991e62019-01-10 15:14:51 -0800148 }
149
150 /**
151 * Returns an {@link ExpandableNotificationRow} that should be shown as a bubble.
152 */
153 public ExpandableNotificationRow createBubble() throws Exception {
154 Notification n = createNotification(false /* isGroupSummary */,
155 null /* groupKey */, true /* isBubble */);
Yohei Yukawa72769462019-01-20 09:28:08 -0800156 return generateRow(n, PKG, UID, USER_HANDLE, 0 /* extraInflationFlags */, IMPORTANCE_HIGH);
Rohan Shah524cf7b2018-03-15 14:40:02 -0700157 }
158
159 /**
160 * Creates a notification row with the given details.
161 *
162 * @param pkg package used for creating a {@link StatusBarNotification}
163 * @param uid uid used for creating a {@link StatusBarNotification}
164 * @param isGroupSummary whether the notification row is a group summary
165 * @param groupKey the group key for the notification group used across notifications
166 * @return a row with that's either a standalone notification or a group notification if the
167 * groupKey is non-null
168 * @throws Exception
169 */
170 private ExpandableNotificationRow createRow(
171 String pkg,
172 int uid,
Yohei Yukawa72769462019-01-20 09:28:08 -0800173 UserHandle userHandle,
Rohan Shah524cf7b2018-03-15 14:40:02 -0700174 boolean isGroupSummary,
Mady Mellorb4991e62019-01-10 15:14:51 -0800175 @Nullable String groupKey)
Rohan Shah524cf7b2018-03-15 14:40:02 -0700176 throws Exception {
Kevind4660b22018-09-27 10:57:35 -0700177 Notification notif = createNotification(isGroupSummary, groupKey);
Yohei Yukawa72769462019-01-20 09:28:08 -0800178 return generateRow(notif, pkg, uid, userHandle, 0 /* inflationFlags */);
Kevind4660b22018-09-27 10:57:35 -0700179 }
180
181 /**
182 * Creates a generic notification.
183 *
184 * @return a notification with no special properties
185 */
186 private Notification createNotification() {
187 return createNotification(false /* isGroupSummary */, null /* groupKey */);
188 }
189
190 /**
191 * Creates a notification with the given parameters.
192 *
193 * @param isGroupSummary whether the notification is a group summary
194 * @param groupKey the group key for the notification group used across notifications
195 * @return a notification that is in the group specified or standalone if unspecified
196 */
Mady Mellorb4991e62019-01-10 15:14:51 -0800197 private Notification createNotification(boolean isGroupSummary, @Nullable String groupKey) {
198 return createNotification(isGroupSummary, groupKey, false /* isBubble */);
199 }
200
201 /**
202 * Creates a notification with the given parameters.
203 *
204 * @param isGroupSummary whether the notification is a group summary
205 * @param groupKey the group key for the notification group used across notifications
206 * @param isBubble whether this notification should bubble
207 * @return a notification that is in the group specified or standalone if unspecified
208 */
Kevind4660b22018-09-27 10:57:35 -0700209 private Notification createNotification(boolean isGroupSummary,
Mady Mellorb4991e62019-01-10 15:14:51 -0800210 @Nullable String groupKey, boolean isBubble) {
Selim Cinek6fd06b52017-03-07 15:54:10 -0800211 Notification publicVersion = new Notification.Builder(mContext).setSmallIcon(
212 R.drawable.ic_person)
213 .setCustomContentView(new RemoteViews(mContext.getPackageName(),
214 R.layout.custom_view_dark))
215 .build();
Kevind4660b22018-09-27 10:57:35 -0700216 Notification.Builder notificationBuilder = new Notification.Builder(mContext, "channelId")
217 .setSmallIcon(R.drawable.ic_person)
218 .setContentTitle("Title")
219 .setContentText("Text")
Kevind5022f92018-10-08 18:30:26 -0700220 .setPublicVersion(publicVersion)
221 .setStyle(new Notification.BigTextStyle().bigText("Big Text"));
Rohan Shah524cf7b2018-03-15 14:40:02 -0700222 if (isGroupSummary) {
223 notificationBuilder.setGroupSummary(true);
224 }
225 if (!TextUtils.isEmpty(groupKey)) {
226 notificationBuilder.setGroup(groupKey);
227 }
Mady Mellorb4991e62019-01-10 15:14:51 -0800228 if (isBubble) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800229 notificationBuilder.setBubbleMetadata(makeBubbleMetadata());
Mady Mellorb4991e62019-01-10 15:14:51 -0800230 }
Kevind4660b22018-09-27 10:57:35 -0700231 return notificationBuilder.build();
Julia Reynoldsfc640012018-02-21 12:25:27 -0500232 }
233
Rohan Shah524cf7b2018-03-15 14:40:02 -0700234 private ExpandableNotificationRow generateRow(
235 Notification notification,
236 String pkg,
237 int uid,
Yohei Yukawa72769462019-01-20 09:28:08 -0800238 UserHandle userHandle,
Mady Mellorb4991e62019-01-10 15:14:51 -0800239 @InflationFlag int extraInflationFlags)
240 throws Exception {
Yohei Yukawa72769462019-01-20 09:28:08 -0800241 return generateRow(notification, pkg, uid, userHandle, extraInflationFlags,
242 IMPORTANCE_DEFAULT);
Mady Mellorb4991e62019-01-10 15:14:51 -0800243 }
244
245 private ExpandableNotificationRow generateRow(
246 Notification notification,
247 String pkg,
248 int uid,
Yohei Yukawa72769462019-01-20 09:28:08 -0800249 UserHandle userHandle,
Mady Mellorb4991e62019-01-10 15:14:51 -0800250 @InflationFlag int extraInflationFlags,
251 int importance)
Julia Reynoldsfc640012018-02-21 12:25:27 -0500252 throws Exception {
Selim Cinek10790672017-03-08 16:33:05 -0800253 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
254 mContext.LAYOUT_INFLATER_SERVICE);
Jason Monk6dceace2018-05-15 20:24:07 -0400255 mRow = (ExpandableNotificationRow) inflater.inflate(
256 R.layout.status_bar_notification_row,
257 null /* root */,
258 false /* attachToRoot */);
Selim Cinek5ba22542017-04-20 15:16:10 -0700259 ExpandableNotificationRow row = mRow;
Selim Cinek10790672017-03-08 16:33:05 -0800260 row.setGroupManager(mGroupManager);
Selim Cinek5cf1d052017-06-01 17:36:46 -0700261 row.setHeadsUpManager(mHeadsUpManager);
262 row.setAboveShelfChangedListener(aboveShelf -> {});
Rohan Shah524cf7b2018-03-15 14:40:02 -0700263 StatusBarNotification sbn = new StatusBarNotification(
264 pkg,
265 pkg,
266 mId++,
267 null /* tag */,
268 uid,
269 2000 /* initialPid */,
270 notification,
Yohei Yukawa72769462019-01-20 09:28:08 -0800271 userHandle,
Rohan Shah524cf7b2018-03-15 14:40:02 -0700272 null /* overrideGroupKey */,
273 System.currentTimeMillis());
Ned Burnsf81c4c42019-01-07 14:10:43 -0500274 NotificationEntry entry = new NotificationEntry(sbn);
Evan Laird94492852018-10-25 13:43:01 -0400275 entry.setRow(row);
Selim Cinek5ba22542017-04-20 15:16:10 -0700276 entry.createIcons(mContext, sbn);
Julia Reynolds0abae112018-06-08 10:36:57 -0400277 entry.channel = new NotificationChannel(
Mady Mellorb4991e62019-01-10 15:14:51 -0800278 notification.getChannelId(), notification.getChannelId(), importance);
Julia Reynolds0abae112018-06-08 10:36:57 -0400279 entry.channel.setBlockableSystem(true);
Kevind4660b22018-09-27 10:57:35 -0700280 row.setEntry(entry);
281 row.getNotificationInflater().addInflationFlags(extraInflationFlags);
Ned Burns1a5e22f2019-02-14 15:11:52 -0500282 NotificationContentInflaterTest.runThenWaitForInflation(
Kevind4660b22018-09-27 10:57:35 -0700283 () -> row.inflateViews(),
Selim Cinek5ba22542017-04-20 15:16:10 -0700284 row.getNotificationInflater());
Selim Cinek6fd06b52017-03-07 15:54:10 -0800285
Rohan Shah524cf7b2018-03-15 14:40:02 -0700286 // This would be done as part of onAsyncInflationFinished, but we skip large amounts of
287 // the callback chain, so we need to make up for not adding it to the group manager
288 // here.
289 mGroupManager.onEntryAdded(entry);
Selim Cinek6fd06b52017-03-07 15:54:10 -0800290 return row;
291 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800292
293 private Notification.BubbleMetadata makeBubbleMetadata() {
Mark Renoufa9fbb6e2019-02-11 12:48:22 -0500294 Intent target = new Intent(mContext, BubblesTestActivity.class);
295 PendingIntent bubbleIntent = PendingIntent.getActivity(mContext, 0, target, 0);
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800296 return new Notification.BubbleMetadata.Builder()
297 .setIntent(bubbleIntent)
298 .setTitle("bubble title")
299 .setIcon(Icon.createWithResource(mContext, 1))
300 .setDesiredHeight(314)
301 .build();
302 }
Selim Cinek6fd06b52017-03-07 15:54:10 -0800303}