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 android.app.cts.android.app.cts.tools; 18 19 import android.app.NotificationManager; 20 import android.app.PendingIntent.CanceledException; 21 import android.app.stubs.TestNotificationListener; 22 import android.content.Context; 23 import android.service.notification.StatusBarNotification; 24 25 import java.util.function.Supplier; 26 27 public class NotificationHelper { 28 public static final long SHORT_WAIT_TIME = 100; 29 public static final long MAX_WAIT_TIME = 2000; 30 31 private final Context mContext; 32 private final NotificationManager mNotificationManager; 33 private Supplier<TestNotificationListener> mNotificationListener; 34 NotificationHelper(Context context, Supplier<TestNotificationListener> listener)35 public NotificationHelper(Context context, Supplier<TestNotificationListener> listener) { 36 mContext = context; 37 mNotificationManager = mContext.getSystemService(NotificationManager.class); 38 mNotificationListener = listener; 39 } 40 clickNotification(int notificationId, boolean searchAll)41 public void clickNotification(int notificationId, boolean searchAll) throws CanceledException { 42 findPostedNotification(notificationId, searchAll).getNotification().contentIntent.send(); 43 } 44 findPostedNotification(int id, boolean all)45 public StatusBarNotification findPostedNotification(int id, boolean all) { 46 // notification is a bit asynchronous so it may take a few ms to appear in 47 // getActiveNotifications() 48 // we will check for it for up to 1000ms before giving up 49 for (long totalWait = 0; totalWait < MAX_WAIT_TIME; totalWait += SHORT_WAIT_TIME) { 50 StatusBarNotification n = findNotificationNoWait(id, all); 51 if (n != null) { 52 return n; 53 } 54 try { 55 Thread.sleep(SHORT_WAIT_TIME); 56 } catch (InterruptedException ex) { 57 // pass 58 } 59 } 60 return findNotificationNoWait(id, all); 61 } 62 findNotificationNoWait(int id, boolean all)63 public StatusBarNotification findNotificationNoWait(int id, boolean all) { 64 for (StatusBarNotification sbn : getActiveNotifications(all)) { 65 if (sbn.getId() == id) { 66 return sbn; 67 } 68 } 69 return null; 70 } 71 getActiveNotifications(boolean all)72 public StatusBarNotification[] getActiveNotifications(boolean all) { 73 if (all) { 74 return mNotificationListener.get().getActiveNotifications(); 75 } else { 76 return mNotificationManager.getActiveNotifications(); 77 } 78 } 79 } 80