1 /* 2 * Copyright 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.car.settings.testutils; 18 19 import android.app.AutomaticZenRule; 20 import android.app.NotificationManager; 21 import android.content.ComponentName; 22 import android.util.ArrayMap; 23 import android.util.ArraySet; 24 25 import org.robolectric.annotation.Implementation; 26 import org.robolectric.annotation.Implements; 27 28 import java.util.Map; 29 import java.util.Set; 30 31 @Implements(NotificationManager.class) 32 public class ShadowNotificationManager extends org.robolectric.shadows.ShadowNotificationManager { 33 34 private Set<String> mNotificationPolicyGrantedPackages = new ArraySet<>(); 35 private Set<ComponentName> mNotificationListenerAccessGrantedComponents = new ArraySet<>(); 36 private Map<String, AutomaticZenRule> mAutomaticZenRules = new ArrayMap<>(); 37 private int mZenRuleIdCounter = 0; 38 39 @Implementation setNotificationPolicyAccessGranted(String pkg, boolean granted)40 public void setNotificationPolicyAccessGranted(String pkg, boolean granted) { 41 if (granted) { 42 mNotificationPolicyGrantedPackages.add(pkg); 43 } else { 44 mNotificationPolicyGrantedPackages.remove(pkg); 45 } 46 } 47 48 @Implementation isNotificationPolicyAccessGrantedForPackage(String pkg)49 protected boolean isNotificationPolicyAccessGrantedForPackage(String pkg) { 50 return mNotificationPolicyGrantedPackages.contains(pkg); 51 } 52 53 @Implementation isNotificationListenerAccessGranted(ComponentName listener)54 protected boolean isNotificationListenerAccessGranted(ComponentName listener) { 55 return mNotificationListenerAccessGrantedComponents.contains(listener); 56 } 57 58 @Implementation setNotificationListenerAccessGranted(ComponentName listener, boolean granted)59 protected void setNotificationListenerAccessGranted(ComponentName listener, boolean granted) { 60 if (granted) { 61 mNotificationListenerAccessGrantedComponents.add(listener); 62 } else { 63 mNotificationListenerAccessGrantedComponents.remove(listener); 64 } 65 } 66 67 @Implementation getAutomaticZenRules()68 protected Map<String, AutomaticZenRule> getAutomaticZenRules() { 69 return mAutomaticZenRules; 70 } 71 72 @Implementation addAutomaticZenRule(AutomaticZenRule automaticZenRule)73 protected String addAutomaticZenRule(AutomaticZenRule automaticZenRule) { 74 String id = String.valueOf(mZenRuleIdCounter++); 75 mAutomaticZenRules.put(id, automaticZenRule); 76 return id; 77 } 78 79 @Implementation removeAutomaticZenRules(String packageName)80 protected void removeAutomaticZenRules(String packageName) { 81 mAutomaticZenRules.clear(); 82 } 83 } 84