1 /* 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 17 package com.android.systemui.statusbar; 18 19 import android.app.ActivityManager; 20 import android.app.Instrumentation; 21 import android.app.Notification; 22 import android.content.Context; 23 import android.os.UserHandle; 24 import android.service.notification.StatusBarNotification; 25 import android.support.test.InstrumentationRegistry; 26 import android.view.LayoutInflater; 27 import android.widget.FrameLayout; 28 import android.widget.RemoteViews; 29 30 import com.android.systemui.R; 31 import com.android.systemui.statusbar.notification.AboveShelfChangedListener; 32 import com.android.systemui.statusbar.notification.AboveShelfObserver; 33 import com.android.systemui.statusbar.notification.InflationException; 34 import com.android.systemui.statusbar.notification.NotificationInflaterTest; 35 import com.android.systemui.statusbar.phone.NotificationGroupManager; 36 import com.android.systemui.statusbar.policy.HeadsUpManager; 37 38 /** 39 * A helper class to create {@link ExpandableNotificationRow} 40 */ 41 public class NotificationTestHelper { 42 43 private final Context mContext; 44 private final Instrumentation mInstrumentation; 45 private int mId; 46 private final NotificationGroupManager mGroupManager = new NotificationGroupManager(); 47 private ExpandableNotificationRow mRow; 48 private InflationException mException; 49 private HeadsUpManager mHeadsUpManager; 50 51 public NotificationTestHelper(Context context) { 52 mContext = context; 53 mInstrumentation = InstrumentationRegistry.getInstrumentation(); 54 mHeadsUpManager = new HeadsUpManager(mContext, null, mGroupManager); 55 } 56 57 public ExpandableNotificationRow createRow() throws Exception { 58 Notification publicVersion = new Notification.Builder(mContext).setSmallIcon( 59 R.drawable.ic_person) 60 .setCustomContentView(new RemoteViews(mContext.getPackageName(), 61 R.layout.custom_view_dark)) 62 .build(); 63 Notification notification = new Notification.Builder(mContext).setSmallIcon( 64 R.drawable.ic_person) 65 .setContentTitle("Title") 66 .setContentText("Text") 67 .setPublicVersion(publicVersion) 68 .build(); 69 return createRow(notification); 70 } 71 72 public ExpandableNotificationRow createRow(Notification notification) throws Exception { 73 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( 74 mContext.LAYOUT_INFLATER_SERVICE); 75 mInstrumentation.runOnMainSync(() -> { 76 mRow = (ExpandableNotificationRow) inflater.inflate( 77 R.layout.status_bar_notification_row, 78 null, false); 79 }); 80 ExpandableNotificationRow row = mRow; 81 row.setGroupManager(mGroupManager); 82 row.setHeadsUpManager(mHeadsUpManager); 83 row.setAboveShelfChangedListener(aboveShelf -> {}); 84 UserHandle mUser = UserHandle.of(ActivityManager.getCurrentUser()); 85 StatusBarNotification sbn = new StatusBarNotification("com.android.systemui", 86 "com.android.systemui", mId++, null, 1000, 87 2000, notification, mUser, null, System.currentTimeMillis()); 88 NotificationData.Entry entry = new NotificationData.Entry(sbn); 89 entry.row = row; 90 entry.createIcons(mContext, sbn); 91 NotificationInflaterTest.runThenWaitForInflation(() -> row.updateNotification(entry), 92 row.getNotificationInflater()); 93 return row; 94 } 95 96 public ExpandableNotificationRow createGroup() throws Exception { 97 ExpandableNotificationRow row = createRow(); 98 row.addChildNotification(createRow()); 99 row.addChildNotification(createRow()); 100 return row; 101 } 102 } 103