1 /** 2 * Copyright (c) 2020, The Android Open Source Project 3 * 4 * Licensed under the Apache License, 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 package android.service.notification; 17 18 import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_ALERTING; 19 import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_CONVERSATIONS; 20 import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_ONGOING; 21 import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_SILENT; 22 23 import android.content.pm.VersionedPackage; 24 import android.os.Parcel; 25 import android.os.Parcelable; 26 import android.util.ArraySet; 27 28 /** 29 * Specifies a filter for what types of notifications should be bridged to notification listeners. 30 * Each requested listener will have their own filter instance. 31 * @hide 32 */ 33 public class NotificationListenerFilter implements Parcelable { 34 35 private static final int DEFAULT_TYPES = FLAG_FILTER_TYPE_CONVERSATIONS 36 | FLAG_FILTER_TYPE_ALERTING 37 | FLAG_FILTER_TYPE_SILENT 38 | FLAG_FILTER_TYPE_ONGOING; 39 private int mAllowedNotificationTypes; 40 // VersionedPackage is holding the pkg name and pkg uid 41 private ArraySet<VersionedPackage> mDisallowedPackages; 42 NotificationListenerFilter()43 public NotificationListenerFilter() { 44 mAllowedNotificationTypes = DEFAULT_TYPES; 45 mDisallowedPackages = new ArraySet<>(); 46 } 47 NotificationListenerFilter(int types, ArraySet<VersionedPackage> pkgs)48 public NotificationListenerFilter(int types, ArraySet<VersionedPackage> pkgs) { 49 mAllowedNotificationTypes = types; 50 mDisallowedPackages = pkgs; 51 } 52 53 /** 54 * @hide 55 */ NotificationListenerFilter(Parcel in)56 protected NotificationListenerFilter(Parcel in) { 57 mAllowedNotificationTypes = in.readInt(); 58 mDisallowedPackages = (ArraySet<VersionedPackage>) in.readArraySet( 59 VersionedPackage.class.getClassLoader()); 60 } 61 62 @Override writeToParcel(Parcel dest, int flags)63 public void writeToParcel(Parcel dest, int flags) { 64 dest.writeInt(mAllowedNotificationTypes); 65 dest.writeArraySet(mDisallowedPackages); 66 } 67 68 public static final Creator<NotificationListenerFilter> CREATOR = 69 new Creator<NotificationListenerFilter>() { 70 @Override 71 public NotificationListenerFilter createFromParcel(Parcel in) { 72 return new NotificationListenerFilter(in); 73 } 74 75 @Override 76 public NotificationListenerFilter[] newArray(int size) { 77 return new NotificationListenerFilter[size]; 78 } 79 }; 80 isTypeAllowed(int type)81 public boolean isTypeAllowed(int type) { 82 return (mAllowedNotificationTypes & type) != 0; 83 } 84 areAllTypesAllowed()85 public boolean areAllTypesAllowed() { 86 return DEFAULT_TYPES == mAllowedNotificationTypes; 87 } 88 isPackageAllowed(VersionedPackage pkg)89 public boolean isPackageAllowed(VersionedPackage pkg) { 90 return !mDisallowedPackages.contains(pkg); 91 } 92 getTypes()93 public int getTypes() { 94 return mAllowedNotificationTypes; 95 } 96 getDisallowedPackages()97 public ArraySet<VersionedPackage> getDisallowedPackages() { 98 return mDisallowedPackages; 99 } 100 setTypes(int types)101 public void setTypes(int types) { 102 mAllowedNotificationTypes = types; 103 } 104 setDisallowedPackages(ArraySet<VersionedPackage> pkgs)105 public void setDisallowedPackages(ArraySet<VersionedPackage> pkgs) { 106 mDisallowedPackages = pkgs; 107 } 108 removePackage(VersionedPackage pkg)109 public void removePackage(VersionedPackage pkg) { 110 mDisallowedPackages.remove(pkg); 111 } 112 addPackage(VersionedPackage pkg)113 public void addPackage(VersionedPackage pkg) { 114 mDisallowedPackages.add(pkg); 115 } 116 117 @Override describeContents()118 public int describeContents() { 119 return 0; 120 } 121 } 122