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.server.notification; 18 19 import android.annotation.NonNull; 20 import android.app.NotificationHistory; 21 import android.app.NotificationHistory.HistoricalNotification; 22 import android.text.TextUtils; 23 24 import com.android.internal.util.Preconditions; 25 26 public final class NotificationHistoryFilter { 27 private String mPackage; 28 private String mChannel; 29 private int mNotificationCount; 30 NotificationHistoryFilter()31 private NotificationHistoryFilter() {} 32 getPackage()33 public String getPackage() { 34 return mPackage; 35 } 36 getChannel()37 public String getChannel() { 38 return mChannel; 39 } 40 getMaxNotifications()41 public int getMaxNotifications() { 42 return mNotificationCount; 43 } 44 45 /** 46 * Returns whether any of the filtering conditions are set 47 */ isFiltering()48 public boolean isFiltering() { 49 return getPackage() != null || getChannel() != null 50 || mNotificationCount < Integer.MAX_VALUE; 51 } 52 53 /** 54 * Returns true if this notification passes the package and channel name filter, false 55 * otherwise. 56 */ matchesPackageAndChannelFilter(HistoricalNotification notification)57 public boolean matchesPackageAndChannelFilter(HistoricalNotification notification) { 58 if (!TextUtils.isEmpty(getPackage())) { 59 if (!getPackage().equals(notification.getPackage())) { 60 return false; 61 } else { 62 if (!TextUtils.isEmpty(getChannel()) 63 && !getChannel().equals(notification.getChannelId())) { 64 return false; 65 } 66 } 67 } 68 69 return true; 70 } 71 72 /** 73 * Returns true if the NotificationHistory can accept another notification. 74 */ matchesCountFilter(NotificationHistory notifications)75 public boolean matchesCountFilter(NotificationHistory notifications) { 76 return notifications.getHistoryCount() < mNotificationCount; 77 } 78 79 public static final class Builder { 80 private String mPackage = null; 81 private String mChannel = null; 82 private int mNotificationCount = Integer.MAX_VALUE; 83 84 /** 85 * Constructor 86 */ Builder()87 public Builder() {} 88 89 /** 90 * Sets a package name filter 91 */ setPackage(String aPackage)92 public Builder setPackage(String aPackage) { 93 mPackage = aPackage; 94 return this; 95 } 96 97 /** 98 * Sets a channel name filter. Only valid if there is also a package name filter 99 */ setChannel(String pkg, String channel)100 public Builder setChannel(String pkg, String channel) { 101 setPackage(pkg); 102 mChannel = channel; 103 return this; 104 } 105 106 /** 107 * Sets the max historical notifications 108 */ setMaxNotifications(int notificationCount)109 public Builder setMaxNotifications(int notificationCount) { 110 mNotificationCount = notificationCount; 111 return this; 112 } 113 114 /** 115 * Makes a NotificationHistoryFilter 116 */ build()117 public NotificationHistoryFilter build() { 118 NotificationHistoryFilter filter = new NotificationHistoryFilter(); 119 filter.mPackage = mPackage; 120 filter.mChannel = mChannel; 121 filter.mNotificationCount = mNotificationCount; 122 return filter; 123 } 124 } 125 } 126