• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 static android.app.Notification.CATEGORY_CALL;
20 import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
21 import static android.app.NotificationManager.Policy.CONVERSATION_SENDERS_ANYONE;
22 import static android.app.NotificationManager.Policy.CONVERSATION_SENDERS_IMPORTANT;
23 import static android.app.NotificationManager.Policy.CONVERSATION_SENDERS_NONE;
24 import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_CALLS;
25 import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_CONVERSATIONS;
26 import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES;
27 import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_REPEAT_CALLERS;
28 import static android.app.NotificationManager.Policy.PRIORITY_SENDERS_ANY;
29 import static android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
30 import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS;
31 import static android.provider.Settings.Global.ZEN_MODE_OFF;
32 
33 import static junit.framework.Assert.assertFalse;
34 import static junit.framework.Assert.assertTrue;
35 
36 import static org.mockito.ArgumentMatchers.any;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.when;
39 
40 import android.app.Notification;
41 import android.app.NotificationChannel;
42 import android.app.NotificationManager.Policy;
43 import android.media.AudioAttributes;
44 import android.os.Bundle;
45 import android.os.UserHandle;
46 import android.platform.test.flag.junit.SetFlagsRule;
47 import android.service.notification.StatusBarNotification;
48 import android.service.notification.ZenModeConfig;
49 import android.service.notification.ZenPolicy;
50 import android.telephony.TelephonyManager;
51 import android.testing.AndroidTestingRunner;
52 import android.testing.TestableLooper;
53 import android.util.ArraySet;
54 
55 import androidx.test.filters.SmallTest;
56 
57 import com.android.internal.util.NotificationMessagingUtil;
58 import com.android.server.UiServiceTestCase;
59 
60 import org.junit.After;
61 import org.junit.Before;
62 import org.junit.Rule;
63 import org.junit.Test;
64 import org.junit.runner.RunWith;
65 import org.mockito.Mock;
66 import org.mockito.MockitoAnnotations;
67 
68 @SmallTest
69 @RunWith(AndroidTestingRunner.class)
70 @TestableLooper.RunWithLooper
71 public class ZenModeFilteringTest extends UiServiceTestCase {
72 
73     @Mock
74     private NotificationMessagingUtil mMessagingUtil;
75     private ZenModeFiltering mZenModeFiltering;
76 
77     @Mock private TelephonyManager mTelephonyManager;
78 
79     private long mTestStartTime;
80 
81     @Rule
82     public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
83 
84     @Before
setUp()85     public void setUp() {
86         MockitoAnnotations.initMocks(this);
87         mZenModeFiltering = new ZenModeFiltering(mContext, mMessagingUtil);
88 
89         // for repeat callers / matchesCallFilter
90         mContext.addMockSystemService(TelephonyManager.class, mTelephonyManager);
91         mTestStartTime = System.currentTimeMillis();
92     }
93 
94     @After
tearDown()95     public void tearDown() {
96         // make sure to get rid of any data stored in repeat callers
97         mZenModeFiltering.cleanUpCallersAfter(mTestStartTime);
98     }
99 
getNotificationRecord()100     private NotificationRecord getNotificationRecord() {
101         return getNotificationRecord(mock(NotificationChannel.class));
102     }
103 
getNotificationRecord(NotificationChannel c)104     private NotificationRecord getNotificationRecord(NotificationChannel c) {
105         StatusBarNotification sbn = mock(StatusBarNotification.class);
106         Notification notification = mock(Notification.class);
107         when(sbn.getNotification()).thenReturn(notification);
108         return new NotificationRecord(mContext, sbn, c);
109     }
110 
getConversationRecord(NotificationChannel c, StatusBarNotification sbn)111     private NotificationRecord getConversationRecord(NotificationChannel c,
112             StatusBarNotification sbn) {
113         NotificationRecord r = mock(NotificationRecord.class);
114         when(r.getCriticality()).thenReturn(CriticalNotificationExtractor.NORMAL);
115         when(r.getSbn()).thenReturn(sbn);
116         when(r.getChannel()).thenReturn(c);
117         when(r.isConversation()).thenReturn(true);
118         return r;
119     }
120 
makeExtrasBundleWithPeople(String[] people)121     private Bundle makeExtrasBundleWithPeople(String[] people) {
122         Bundle extras = new Bundle();
123         extras.putObject(Notification.EXTRA_PEOPLE_LIST, people);
124         return extras;
125     }
126 
127     // Create a notification record with the people String array as the
128     // bundled extras, and the numbers ArraySet as additional phone numbers.
getCallRecordWithPeopleInfo(String[] people, ArraySet<String> numbers)129     private NotificationRecord getCallRecordWithPeopleInfo(String[] people,
130             ArraySet<String> numbers) {
131         // set up notification record
132         NotificationRecord r = mock(NotificationRecord.class);
133         StatusBarNotification sbn = mock(StatusBarNotification.class);
134         Notification notification = mock(Notification.class);
135         notification.extras = makeExtrasBundleWithPeople(people);
136         when(sbn.getNotification()).thenReturn(notification);
137         when(r.getSbn()).thenReturn(sbn);
138         when(r.getPhoneNumbers()).thenReturn(numbers);
139         when(r.getCriticality()).thenReturn(CriticalNotificationExtractor.NORMAL);
140         when(r.isCategory(CATEGORY_CALL)).thenReturn(true);
141         return r;
142     }
143 
144     @Test
testIsMessage()145     public void testIsMessage() {
146         NotificationRecord r = getNotificationRecord();
147 
148         when(mMessagingUtil.isMessaging(any())).thenReturn(true);
149         assertTrue(mZenModeFiltering.isMessage(r));
150 
151         when(mMessagingUtil.isMessaging(any())).thenReturn(false);
152         assertFalse(mZenModeFiltering.isMessage(r));
153     }
154 
155     @Test
testIsAlarm()156     public void testIsAlarm() {
157         NotificationChannel c = mock(NotificationChannel.class);
158         when(c.getAudioAttributes()).thenReturn(new AudioAttributes.Builder()
159                 .setUsage(AudioAttributes.USAGE_ALARM)
160                 .build());
161         NotificationRecord r = getNotificationRecord(c);
162         assertTrue(mZenModeFiltering.isAlarm(r));
163 
164         r = getNotificationRecord();
165         r.getSbn().getNotification().category = Notification.CATEGORY_ALARM;
166         assertTrue(mZenModeFiltering.isAlarm(r));
167     }
168 
169     @Test
testIsAlarm_wrongCategory()170     public void testIsAlarm_wrongCategory() {
171         NotificationRecord r = getNotificationRecord();
172         r.getSbn().getNotification().category = CATEGORY_CALL;
173         assertFalse(mZenModeFiltering.isAlarm(r));
174     }
175 
176     @Test
testIsAlarm_wrongUsage()177     public void testIsAlarm_wrongUsage() {
178         NotificationChannel c = mock(NotificationChannel.class);
179         when(c.getAudioAttributes()).thenReturn(new AudioAttributes.Builder()
180                 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
181                 .build());
182         NotificationRecord r = getNotificationRecord(c);
183         assertFalse(mZenModeFiltering.isAlarm(r));
184     }
185 
186     @Test
testSuppressAnything_yes_ZenModeOff()187     public void testSuppressAnything_yes_ZenModeOff() {
188         NotificationRecord r = getNotificationRecord();
189         when(r.getSbn().getPackageName()).thenReturn("bananas");
190         Policy policy = new Policy(0, 0, 0, Policy.getAllSuppressedVisualEffects());
191 
192         assertFalse(mZenModeFiltering.shouldIntercept(ZEN_MODE_OFF, policy, r));
193     }
194 
195     @Test
testSuppressAnything_bypass_ZenModeOn()196     public void testSuppressAnything_bypass_ZenModeOn() {
197         NotificationRecord r = getNotificationRecord();
198         r.setCriticality(CriticalNotificationExtractor.CRITICAL);
199         when(r.getSbn().getPackageName()).thenReturn("bananas");
200         Policy policy = new Policy(0, 0, 0, Policy.getAllSuppressedVisualEffects(), 0);
201 
202         assertFalse(mZenModeFiltering.shouldIntercept(ZEN_MODE_NO_INTERRUPTIONS, policy, r));
203 
204         r.setCriticality(CriticalNotificationExtractor.CRITICAL_LOW);
205         assertFalse(mZenModeFiltering.shouldIntercept(ZEN_MODE_NO_INTERRUPTIONS, policy, r));
206     }
207 
208     @Test
testConversation_allAllowed()209     public void testConversation_allAllowed() {
210         Notification n = new Notification.Builder(mContext, "a").build();
211         StatusBarNotification sbn = new StatusBarNotification("pkg", "pkg", 0, "tag", 0, 0, n,
212                 UserHandle.SYSTEM, null, 0);
213 
214         NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT);
215         channel.setConversationId("parent", "me, work");
216 
217         NotificationRecord r = getConversationRecord(channel, sbn);
218         when(r.isConversation()).thenReturn(true);
219 
220         Policy policy = new Policy(
221                 PRIORITY_CATEGORY_CONVERSATIONS, 0, 0, 0, CONVERSATION_SENDERS_ANYONE);
222 
223         assertFalse(mZenModeFiltering.shouldIntercept(ZEN_MODE_IMPORTANT_INTERRUPTIONS, policy, r));
224     }
225 
226     @Test
testConversation_importantAllowed_isImportant()227     public void testConversation_importantAllowed_isImportant() {
228         Notification n = new Notification.Builder(mContext, "a").build();
229         StatusBarNotification sbn = new StatusBarNotification("pkg", "pkg", 0, "tag", 0, 0, n,
230                 UserHandle.SYSTEM, null, 0);
231 
232         NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT);
233         channel.setConversationId("parent", "me, work");
234         channel.setImportantConversation(true);
235 
236         NotificationRecord r = getConversationRecord(channel, sbn);
237 
238         Policy policy = new Policy(
239                 PRIORITY_CATEGORY_CONVERSATIONS, 0, 0, 0, CONVERSATION_SENDERS_IMPORTANT);
240 
241         assertFalse(mZenModeFiltering.shouldIntercept(ZEN_MODE_IMPORTANT_INTERRUPTIONS, policy, r));
242     }
243 
244     @Test
testConversation_importantAllowed_isNotImportant()245     public void testConversation_importantAllowed_isNotImportant() {
246         Notification n = new Notification.Builder(mContext, "a").build();
247         StatusBarNotification sbn = new StatusBarNotification("pkg", "pkg", 0, "tag", 0, 0, n,
248                 UserHandle.SYSTEM, null, 0);
249 
250         NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT);
251         channel.setConversationId("parent", "me, work");
252 
253         NotificationRecord r = getConversationRecord(channel, sbn);
254 
255         Policy policy = new Policy(
256                 PRIORITY_CATEGORY_CONVERSATIONS, 0, 0, 0, CONVERSATION_SENDERS_IMPORTANT);
257 
258         assertTrue(mZenModeFiltering.shouldIntercept(ZEN_MODE_IMPORTANT_INTERRUPTIONS, policy, r));
259     }
260 
261     @Test
testConversation_noneAllowed_notCallOrMsg()262     public void testConversation_noneAllowed_notCallOrMsg() {
263         Notification n = new Notification.Builder(mContext, "a").build();
264         StatusBarNotification sbn = new StatusBarNotification("pkg", "pkg", 0, "tag", 0, 0, n,
265                 UserHandle.SYSTEM, null, 0);
266 
267         NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT);
268         channel.setConversationId("parent", "me, work");
269 
270         NotificationRecord r = getConversationRecord(channel, sbn);
271 
272         Policy policy =
273                 new Policy(PRIORITY_CATEGORY_CONVERSATIONS, 0, 0, 0, CONVERSATION_SENDERS_NONE);
274 
275         assertTrue(mZenModeFiltering.shouldIntercept(ZEN_MODE_IMPORTANT_INTERRUPTIONS, policy, r));
276     }
277 
278     @Test
testConversation_noneAllowed_callAllowed()279     public void testConversation_noneAllowed_callAllowed() {
280         Notification n = new Notification.Builder(mContext, "a").build();
281         StatusBarNotification sbn = new StatusBarNotification("pkg", "pkg", 0, "tag", 0, 0, n,
282                 UserHandle.SYSTEM, null, 0);
283 
284         NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT);
285         channel.setConversationId("parent", "me, work");
286 
287         NotificationRecord r = getConversationRecord(channel, sbn);
288         when(r.isCategory(CATEGORY_CALL)).thenReturn(true);
289 
290         Policy policy =
291                 new Policy(PRIORITY_CATEGORY_CALLS,
292                         PRIORITY_SENDERS_ANY, 0, 0, CONVERSATION_SENDERS_NONE);
293 
294         assertFalse(mZenModeFiltering.shouldIntercept(ZEN_MODE_IMPORTANT_INTERRUPTIONS, policy, r));
295     }
296 
297     @Test
testConversation_noneAllowed_msgAllowed()298     public void testConversation_noneAllowed_msgAllowed() {
299         when(mMessagingUtil.isMessaging(any())).thenReturn(true);
300         Notification n = new Notification.Builder(mContext, "a").build();
301         StatusBarNotification sbn = new StatusBarNotification("pkg", "pkg", 0, "tag", 0, 0, n,
302                 UserHandle.SYSTEM, null, 0);
303 
304         NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT);
305         channel.setConversationId("parent", "me, work");
306 
307         NotificationRecord r = getConversationRecord(channel, sbn);
308 
309         Policy policy =
310                 new Policy(PRIORITY_CATEGORY_MESSAGES,
311                         0, PRIORITY_SENDERS_ANY, 0, CONVERSATION_SENDERS_NONE);
312 
313         assertFalse(mZenModeFiltering.shouldIntercept(ZEN_MODE_IMPORTANT_INTERRUPTIONS, policy, r));
314     }
315 
316     @Test
testRepeatCallers_checksPhoneNumbers()317     public void testRepeatCallers_checksPhoneNumbers() {
318         // set up telephony manager behavior
319         when(mTelephonyManager.getNetworkCountryIso()).thenReturn("us");
320 
321         // first, record a phone call from a telephone number
322         String[] callNumber = new String[]{"tel:12345678910"};
323         mZenModeFiltering.recordCall(getCallRecordWithPeopleInfo(callNumber, null));
324 
325         // set up policy to only allow repeat callers
326         Policy policy = new Policy(
327                 PRIORITY_CATEGORY_REPEAT_CALLERS, 0, 0, 0, CONVERSATION_SENDERS_NONE);
328 
329         // make sure that a record with the phone number in extras is correctly allowed through
330         NotificationRecord r = getCallRecordWithPeopleInfo(callNumber, null);
331         assertFalse(mZenModeFiltering.shouldIntercept(ZEN_MODE_IMPORTANT_INTERRUPTIONS, policy, r));
332 
333         // make sure that a record with the phone number in the phone numbers array is also
334         // allowed through
335         NotificationRecord r2 = getCallRecordWithPeopleInfo(new String[]{"some_contact_uri"},
336                 new ArraySet<>(new String[]{"12345678910"}));
337         assertFalse(mZenModeFiltering.shouldIntercept(
338                 ZEN_MODE_IMPORTANT_INTERRUPTIONS, policy, r2));
339 
340         // A record with the phone number in neither of the above should be intercepted
341         NotificationRecord r3 = getCallRecordWithPeopleInfo(new String[]{"tel:10987654321"},
342                 new ArraySet<>(new String[]{"15555555555"}));
343         assertTrue(mZenModeFiltering.shouldIntercept(ZEN_MODE_IMPORTANT_INTERRUPTIONS, policy, r3));
344     }
345 
346     @Test
testMatchesCallFilter_repeatCallers_directMatch()347     public void testMatchesCallFilter_repeatCallers_directMatch() {
348         // after calls given an email with an exact string match, make sure that
349         // matchesCallFilter returns the right thing
350         String[] mailSource = new String[]{"mailto:hello.world"};
351         mZenModeFiltering.recordCall(getCallRecordWithPeopleInfo(mailSource, null));
352 
353         // set up policy to only allow repeat callers
354         Policy policy = new Policy(
355                 PRIORITY_CATEGORY_REPEAT_CALLERS, 0, 0, 0, CONVERSATION_SENDERS_NONE);
356 
357         // check whether matchesCallFilter returns the right thing
358         Bundle inputMatches = makeExtrasBundleWithPeople(new String[]{"mailto:hello.world"});
359         Bundle inputWrong = makeExtrasBundleWithPeople(new String[]{"mailto:nope"});
360         assertTrue(ZenModeFiltering.matchesCallFilter(mContext, ZEN_MODE_IMPORTANT_INTERRUPTIONS,
361                 policy, UserHandle.SYSTEM,
362                 inputMatches, null, 0, 0, 0));
363         assertFalse(ZenModeFiltering.matchesCallFilter(mContext, ZEN_MODE_IMPORTANT_INTERRUPTIONS,
364                 policy, UserHandle.SYSTEM,
365                 inputWrong, null, 0, 0, 0));
366     }
367 
368     @Test
testMatchesCallFilter_repeatCallers_telephoneVariants()369     public void testMatchesCallFilter_repeatCallers_telephoneVariants() {
370         // set up telephony manager behavior
371         when(mTelephonyManager.getNetworkCountryIso()).thenReturn("us");
372 
373         String[] telSource = new String[]{"tel:+1-617-555-1212"};
374         mZenModeFiltering.recordCall(getCallRecordWithPeopleInfo(telSource, null));
375 
376         // set up policy to only allow repeat callers
377         Policy policy = new Policy(
378                 PRIORITY_CATEGORY_REPEAT_CALLERS, 0, 0, 0, CONVERSATION_SENDERS_NONE);
379 
380         // cases to test:
381         //   - identical number
382         //   - same number, different formatting
383         //   - different number
384         //   - garbage
385         Bundle identical = makeExtrasBundleWithPeople(new String[]{"tel:+1-617-555-1212"});
386         Bundle same = makeExtrasBundleWithPeople(new String[]{"tel:16175551212"});
387         Bundle different = makeExtrasBundleWithPeople(new String[]{"tel:123-456-7890"});
388         Bundle garbage = makeExtrasBundleWithPeople(new String[]{"asdfghjkl;"});
389 
390         assertTrue("identical numbers should match",
391                 ZenModeFiltering.matchesCallFilter(mContext, ZEN_MODE_IMPORTANT_INTERRUPTIONS,
392                 policy, UserHandle.SYSTEM,
393                 identical, null, 0, 0, 0));
394         assertTrue("equivalent but non-identical numbers should match",
395                 ZenModeFiltering.matchesCallFilter(mContext, ZEN_MODE_IMPORTANT_INTERRUPTIONS,
396                 policy, UserHandle.SYSTEM,
397                 same, null, 0, 0, 0));
398         assertFalse("non-equivalent numbers should not match",
399                 ZenModeFiltering.matchesCallFilter(mContext, ZEN_MODE_IMPORTANT_INTERRUPTIONS,
400                 policy, UserHandle.SYSTEM,
401                 different, null, 0, 0, 0));
402         assertFalse("non-tel strings should not match",
403                 ZenModeFiltering.matchesCallFilter(mContext, ZEN_MODE_IMPORTANT_INTERRUPTIONS,
404                 policy, UserHandle.SYSTEM,
405                 garbage, null, 0, 0, 0));
406     }
407 
408     @Test
testMatchesCallFilter_repeatCallers_urlEncodedTels()409     public void testMatchesCallFilter_repeatCallers_urlEncodedTels() {
410         // this is not intended to be a supported case but is one that we have seen
411         // sometimes in the wild, so make sure we handle url-encoded telephone numbers correctly
412         // when somebody provides one.
413 
414         // set up telephony manager behavior
415         when(mTelephonyManager.getNetworkCountryIso()).thenReturn("us");
416 
417         String[] telSource = new String[]{"tel:%2B16175551212"};
418         mZenModeFiltering.recordCall(getCallRecordWithPeopleInfo(telSource, null));
419 
420         // set up policy to only allow repeat callers
421         Policy policy = new Policy(
422                 PRIORITY_CATEGORY_REPEAT_CALLERS, 0, 0, 0, CONVERSATION_SENDERS_NONE);
423 
424         // test cases for various forms of the same phone number and different ones
425         Bundle same1 = makeExtrasBundleWithPeople(new String[]{"tel:+1-617-555-1212"});
426         Bundle same2 = makeExtrasBundleWithPeople(new String[]{"tel:%2B1-617-555-1212"});
427         Bundle same3 = makeExtrasBundleWithPeople(new String[]{"tel:6175551212"});
428         Bundle different1 = makeExtrasBundleWithPeople(new String[]{"tel:%2B16175553434"});
429         Bundle different2 = makeExtrasBundleWithPeople(new String[]{"tel:+16175553434"});
430 
431         assertTrue("same number 1 should match",
432                 ZenModeFiltering.matchesCallFilter(mContext, ZEN_MODE_IMPORTANT_INTERRUPTIONS,
433                         policy, UserHandle.SYSTEM,
434                         same1, null, 0, 0, 0));
435         assertTrue("same number 2 should match",
436                 ZenModeFiltering.matchesCallFilter(mContext, ZEN_MODE_IMPORTANT_INTERRUPTIONS,
437                         policy, UserHandle.SYSTEM,
438                         same2, null, 0, 0, 0));
439         assertTrue("same number 3 should match",
440                 ZenModeFiltering.matchesCallFilter(mContext, ZEN_MODE_IMPORTANT_INTERRUPTIONS,
441                         policy, UserHandle.SYSTEM,
442                         same3, null, 0, 0, 0));
443         assertFalse("different number 1 should not match",
444                 ZenModeFiltering.matchesCallFilter(mContext, ZEN_MODE_IMPORTANT_INTERRUPTIONS,
445                         policy, UserHandle.SYSTEM,
446                         different1, null, 0, 0, 0));
447         assertFalse("different number 2 should not match",
448                 ZenModeFiltering.matchesCallFilter(mContext, ZEN_MODE_IMPORTANT_INTERRUPTIONS,
449                         policy, UserHandle.SYSTEM,
450                         different2, null, 0, 0, 0));
451     }
452 
453     @Test
testMatchesCallFilter_repeatCallers_viaRecordPhoneNumbers()454     public void testMatchesCallFilter_repeatCallers_viaRecordPhoneNumbers() {
455         // make sure that phone numbers that are passed in via the NotificationRecord's
456         // cached phone numbers field (from a contact lookup if the record is provided a contact
457         // uri) also get recorded in the repeat callers list.
458 
459         // set up telephony manager behavior
460         when(mTelephonyManager.getNetworkCountryIso()).thenReturn("us");
461 
462         String[] contactSource = new String[]{"content://contacts/lookup/uri-here"};
463         ArraySet<String> contactNumbers = new ArraySet<>(
464                 new String[]{"1-617-555-1212", "1-617-555-3434"});
465         NotificationRecord record = getCallRecordWithPeopleInfo(contactSource, contactNumbers);
466         record.mergePhoneNumbers(contactNumbers);
467         mZenModeFiltering.recordCall(record);
468 
469         // set up policy to only allow repeat callers
470         Policy policy = new Policy(
471                 PRIORITY_CATEGORY_REPEAT_CALLERS, 0, 0, 0, CONVERSATION_SENDERS_NONE);
472 
473         // both phone numbers should register here
474         Bundle tel1 = makeExtrasBundleWithPeople(new String[]{"tel:+1-617-555-1212"});
475         Bundle tel2 = makeExtrasBundleWithPeople(new String[]{"tel:16175553434"});
476         Bundle different = makeExtrasBundleWithPeople(new String[]{"tel:16175555656"});
477 
478         assertTrue("contact number 1 should match",
479                 ZenModeFiltering.matchesCallFilter(mContext, ZEN_MODE_IMPORTANT_INTERRUPTIONS,
480                         policy, UserHandle.SYSTEM,
481                         tel1, null, 0, 0, 0));
482         assertTrue("contact number 2 should match",
483                 ZenModeFiltering.matchesCallFilter(mContext, ZEN_MODE_IMPORTANT_INTERRUPTIONS,
484                         policy, UserHandle.SYSTEM,
485                         tel2, null, 0, 0, 0));
486         assertFalse("different number should not match",
487                 ZenModeFiltering.matchesCallFilter(mContext, ZEN_MODE_IMPORTANT_INTERRUPTIONS,
488                         policy, UserHandle.SYSTEM,
489                         different, null, 0, 0, 0));
490     }
491 
492     @Test
testAllowChannels_priorityPackage()493     public void testAllowChannels_priorityPackage() {
494         // Notification with package priority = PRIORITY_MAX (assigned to indicate canBypassDnd)
495         NotificationRecord r = getNotificationRecord();
496         r.setPackagePriority(Notification.PRIORITY_MAX);
497 
498         // Create a policy to allow channels through, which means shouldIntercept is false
499         ZenModeConfig config = new ZenModeConfig();
500         Policy policy = config.toNotificationPolicy(new ZenPolicy.Builder()
501                 .allowPriorityChannels(true)
502                 .build());
503         assertFalse(mZenModeFiltering.shouldIntercept(ZEN_MODE_IMPORTANT_INTERRUPTIONS, policy, r));
504 
505         // Now create a policy which does not allow priority channels:
506         policy = config.toNotificationPolicy(new ZenPolicy.Builder()
507                 .allowPriorityChannels(false)
508                 .build());
509         assertTrue(mZenModeFiltering.shouldIntercept(ZEN_MODE_IMPORTANT_INTERRUPTIONS, policy, r));
510     }
511 }
512