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 static org.junit.Assert.assertNotNull; 20 21 import android.service.notification.NotificationListenerService.Ranking; 22 import android.service.notification.NotificationListenerService.RankingMap; 23 import android.service.notification.StatusBarNotification; 24 import android.util.ArrayMap; 25 26 import com.android.systemui.statusbar.NotificationListener.NotificationHandler; 27 28 import java.util.ArrayList; 29 import java.util.List; 30 import java.util.Map; 31 32 /** 33 * Simulates a NotificationManager 34 * 35 * You can post and retract notifications, each with an accompanying Ranking. The simulator will 36 * keep its RankingMap up to date and call appropriate event listeners. 37 */ 38 public class NoManSimulator { 39 private final List<NotificationHandler> mListeners = new ArrayList<>(); 40 private final Map<String, Ranking> mRankings = new ArrayMap<>(); 41 NoManSimulator()42 public NoManSimulator() { 43 } 44 addListener(NotificationHandler listener)45 public void addListener(NotificationHandler listener) { 46 mListeners.add(listener); 47 } 48 postNotif(NotificationEntryBuilder builder)49 public NotifEvent postNotif(NotificationEntryBuilder builder) { 50 final NotificationEntry entry = builder.build(); 51 mRankings.put(entry.getKey(), entry.getRanking()); 52 final RankingMap rankingMap = buildRankingMap(); 53 for (NotificationHandler listener : mListeners) { 54 listener.onNotificationPosted(entry.getSbn(), rankingMap); 55 } 56 return new NotifEvent(entry.getSbn(), entry.getRanking(), rankingMap); 57 } 58 retractNotif(StatusBarNotification sbn, int reason)59 public NotifEvent retractNotif(StatusBarNotification sbn, int reason) { 60 assertNotNull(mRankings.remove(sbn.getKey())); 61 final RankingMap rankingMap = buildRankingMap(); 62 for (NotificationHandler listener : mListeners) { 63 listener.onNotificationRemoved(sbn, rankingMap, reason); 64 } 65 return new NotifEvent(sbn, null, rankingMap); 66 } 67 issueRankingUpdate()68 public void issueRankingUpdate() { 69 final RankingMap rankingMap = buildRankingMap(); 70 for (NotificationHandler listener : mListeners) { 71 listener.onNotificationRankingUpdate(rankingMap); 72 } 73 } 74 setRanking(String key, Ranking ranking)75 public void setRanking(String key, Ranking ranking) { 76 mRankings.put(key, ranking); 77 } 78 buildRankingMap()79 private RankingMap buildRankingMap() { 80 return new RankingMap(mRankings.values().toArray(new Ranking[0])); 81 } 82 83 public static class NotifEvent { 84 public final String key; 85 public final StatusBarNotification sbn; 86 public final Ranking ranking; 87 public final RankingMap rankingMap; 88 NotifEvent( StatusBarNotification sbn, Ranking ranking, RankingMap rankingMap)89 private NotifEvent( 90 StatusBarNotification sbn, 91 Ranking ranking, 92 RankingMap rankingMap) { 93 this.key = sbn.getKey(); 94 this.sbn = sbn; 95 this.ranking = ranking; 96 this.rankingMap = rankingMap; 97 } 98 } 99 } 100