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.NonNull; 20 import android.app.Notification; 21 import android.app.NotificationChannel; 22 import android.app.NotificationManager.Importance; 23 import android.content.pm.ShortcutInfo; 24 import android.service.notification.NotificationListenerService.Ranking; 25 import android.service.notification.SnoozeCriterion; 26 27 import java.util.ArrayList; 28 import java.util.Arrays; 29 import java.util.List; 30 31 /** 32 * Standard builder class for Ranking objects. For use in tests that need to craft the underlying 33 * Ranking object of a NotificationEntry. 34 */ 35 public class RankingBuilder { 36 private String mKey = "test_key"; 37 private int mRank = 0; 38 private boolean mMatchesInterruptionFilter = false; 39 private int mVisibilityOverride = 0; 40 private int mSuppressedVisualEffects = 0; 41 @Importance private int mImportance = 0; 42 private CharSequence mExplanation = "test_explanation"; 43 private String mOverrideGroupKey = null; 44 private NotificationChannel mChannel = null; 45 private ArrayList<String> mAdditionalPeople = null; 46 private ArrayList<SnoozeCriterion> mSnoozeCriteria = null; 47 private boolean mCanShowBadge = false; 48 private int mUserSentiment = 0; 49 private boolean mIsSuspended = false; 50 private long mLastAudiblyAlertedMs = 0; 51 private boolean mNoisy = false; 52 private ArrayList<Notification.Action> mSmartActions = new ArrayList<>(); 53 private ArrayList<CharSequence> mSmartReplies = new ArrayList<>(); 54 private boolean mCanBubble = false; 55 private boolean mIsVisuallyInterruptive = false; 56 private boolean mIsConversation = false; 57 private ShortcutInfo mShortcutInfo = null; 58 private int mRankingAdjustment = 0; 59 private boolean mIsBubble = false; 60 RankingBuilder()61 public RankingBuilder() { 62 } 63 RankingBuilder(Ranking ranking)64 public RankingBuilder(Ranking ranking) { 65 mKey = ranking.getKey(); 66 mRank = ranking.getRank(); 67 mMatchesInterruptionFilter = ranking.matchesInterruptionFilter(); 68 mVisibilityOverride = ranking.getLockscreenVisibilityOverride(); 69 mSuppressedVisualEffects = ranking.getSuppressedVisualEffects(); 70 mImportance = ranking.getImportance(); 71 mExplanation = ranking.getImportanceExplanation(); 72 mOverrideGroupKey = ranking.getOverrideGroupKey(); 73 mChannel = ranking.getChannel(); 74 mAdditionalPeople = copyList(ranking.getAdditionalPeople()); 75 mSnoozeCriteria = copyList(ranking.getSnoozeCriteria()); 76 mCanShowBadge = ranking.canShowBadge(); 77 mUserSentiment = ranking.getUserSentiment(); 78 mIsSuspended = ranking.isSuspended(); 79 mLastAudiblyAlertedMs = ranking.getLastAudiblyAlertedMillis(); 80 mNoisy = ranking.isNoisy(); 81 mSmartActions = copyList(ranking.getSmartActions()); 82 mSmartReplies = copyList(ranking.getSmartReplies()); 83 mCanBubble = ranking.canBubble(); 84 mIsVisuallyInterruptive = ranking.visuallyInterruptive(); 85 mIsConversation = ranking.isConversation(); 86 mShortcutInfo = ranking.getConversationShortcutInfo(); 87 mRankingAdjustment = ranking.getRankingAdjustment(); 88 mIsBubble = ranking.isBubble(); 89 } 90 build()91 public Ranking build() { 92 final Ranking ranking = new Ranking(); 93 ranking.populate( 94 mKey, 95 mRank, 96 mMatchesInterruptionFilter, 97 mVisibilityOverride, 98 mSuppressedVisualEffects, 99 mImportance, 100 mExplanation, 101 mOverrideGroupKey, 102 mChannel, 103 mAdditionalPeople, 104 mSnoozeCriteria, 105 mCanShowBadge, 106 mUserSentiment, 107 mIsSuspended, 108 mLastAudiblyAlertedMs, 109 mNoisy, 110 mSmartActions, 111 mSmartReplies, 112 mCanBubble, 113 mIsVisuallyInterruptive, 114 mIsConversation, 115 mShortcutInfo, 116 mRankingAdjustment, 117 mIsBubble); 118 return ranking; 119 } 120 setKey(String key)121 public RankingBuilder setKey(String key) { 122 mKey = key; 123 return this; 124 } 125 setRank(int rank)126 public RankingBuilder setRank(int rank) { 127 mRank = rank; 128 return this; 129 } 130 setMatchesInterruptionFilter(boolean matchesInterruptionFilter)131 public RankingBuilder setMatchesInterruptionFilter(boolean matchesInterruptionFilter) { 132 mMatchesInterruptionFilter = matchesInterruptionFilter; 133 return this; 134 } 135 setVisibilityOverride(int visibilityOverride)136 public RankingBuilder setVisibilityOverride(int visibilityOverride) { 137 mVisibilityOverride = visibilityOverride; 138 return this; 139 } 140 setSuppressedVisualEffects(int suppressedVisualEffects)141 public RankingBuilder setSuppressedVisualEffects(int suppressedVisualEffects) { 142 mSuppressedVisualEffects = suppressedVisualEffects; 143 return this; 144 } 145 setExplanation(CharSequence explanation)146 public RankingBuilder setExplanation(CharSequence explanation) { 147 mExplanation = explanation; 148 return this; 149 } 150 setOverrideGroupKey(String overrideGroupKey)151 public RankingBuilder setOverrideGroupKey(String overrideGroupKey) { 152 mOverrideGroupKey = overrideGroupKey; 153 return this; 154 } 155 setAdditionalPeople(ArrayList<String> additionalPeople)156 public RankingBuilder setAdditionalPeople(ArrayList<String> additionalPeople) { 157 mAdditionalPeople = additionalPeople; 158 return this; 159 } 160 setSnoozeCriteria( ArrayList<SnoozeCriterion> snoozeCriteria)161 public RankingBuilder setSnoozeCriteria( 162 ArrayList<SnoozeCriterion> snoozeCriteria) { 163 mSnoozeCriteria = snoozeCriteria; 164 return this; 165 } 166 setCanShowBadge(boolean canShowBadge)167 public RankingBuilder setCanShowBadge(boolean canShowBadge) { 168 mCanShowBadge = canShowBadge; 169 return this; 170 } 171 setSuspended(boolean suspended)172 public RankingBuilder setSuspended(boolean suspended) { 173 mIsSuspended = suspended; 174 return this; 175 } 176 setLastAudiblyAlertedMs(long lastAudiblyAlertedMs)177 public RankingBuilder setLastAudiblyAlertedMs(long lastAudiblyAlertedMs) { 178 mLastAudiblyAlertedMs = lastAudiblyAlertedMs; 179 return this; 180 } 181 setNoisy(boolean noisy)182 public RankingBuilder setNoisy(boolean noisy) { 183 mNoisy = noisy; 184 return this; 185 } 186 setCanBubble(boolean canBubble)187 public RankingBuilder setCanBubble(boolean canBubble) { 188 mCanBubble = canBubble; 189 return this; 190 } 191 setVisuallyInterruptive(boolean interruptive)192 public RankingBuilder setVisuallyInterruptive(boolean interruptive) { 193 mIsVisuallyInterruptive = interruptive; 194 return this; 195 } 196 setIsConversation(boolean isConversation)197 public RankingBuilder setIsConversation(boolean isConversation) { 198 mIsConversation = isConversation; 199 return this; 200 } 201 setShortcutInfo(ShortcutInfo shortcutInfo)202 public RankingBuilder setShortcutInfo(ShortcutInfo shortcutInfo) { 203 mShortcutInfo = shortcutInfo; 204 return this; 205 } 206 setRankingAdjustment(int rankingAdjustment)207 public RankingBuilder setRankingAdjustment(int rankingAdjustment) { 208 mRankingAdjustment = rankingAdjustment; 209 return this; 210 } 211 setImportance(@mportance int importance)212 public RankingBuilder setImportance(@Importance int importance) { 213 mImportance = importance; 214 return this; 215 } 216 setUserSentiment(int userSentiment)217 public RankingBuilder setUserSentiment(int userSentiment) { 218 mUserSentiment = userSentiment; 219 return this; 220 } 221 setChannel(NotificationChannel channel)222 public RankingBuilder setChannel(NotificationChannel channel) { 223 mChannel = channel; 224 return this; 225 } 226 setSmartActions(@onNull ArrayList<Notification.Action> smartActions)227 public RankingBuilder setSmartActions(@NonNull ArrayList<Notification.Action> smartActions) { 228 mSmartActions = smartActions; 229 return this; 230 } 231 setSmartActions(Notification.Action... smartActions)232 public RankingBuilder setSmartActions(Notification.Action... smartActions) { 233 mSmartActions = new ArrayList<>(Arrays.asList(smartActions)); 234 return this; 235 } 236 setSmartReplies(@onNull ArrayList<CharSequence> smartReplies)237 public RankingBuilder setSmartReplies(@NonNull ArrayList<CharSequence> smartReplies) { 238 mSmartReplies = smartReplies; 239 return this; 240 } 241 setSmartReplies(CharSequence... smartReplies)242 public RankingBuilder setSmartReplies(CharSequence... smartReplies) { 243 mSmartReplies = new ArrayList<>(Arrays.asList(smartReplies)); 244 return this; 245 } 246 copyList(List<E> list)247 private static <E> ArrayList<E> copyList(List<E> list) { 248 if (list == null) { 249 return null; 250 } else { 251 return new ArrayList<>(list); 252 } 253 } 254 } 255