• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 android.app;
18 
19 import static org.junit.Assert.assertEquals;
20 
21 import android.Manifest;
22 import android.content.Context;
23 import android.os.ParcelFileDescriptor;
24 
25 import androidx.annotation.NonNull;
26 import androidx.test.platform.app.InstrumentationRegistry;
27 
28 import com.android.compatibility.common.util.AmUtils;
29 import com.android.compatibility.common.util.FileUtils;
30 import com.android.compatibility.common.util.SystemUtil;
31 import com.android.compatibility.common.util.ThrowingRunnable;
32 
33 import java.io.FileInputStream;
34 import java.io.IOException;
35 
36 public class NotificationSystemUtil {
37 
38     /**
39      * Runs a {@link ThrowingRunnable} as the Shell, while adopting SystemUI's permission (as
40      * checked by {@code NotificationManagerService#isCallerSystemOrSystemUi}).
41      */
runAsSystemUi(@onNull ThrowingRunnable runnable)42     protected static void runAsSystemUi(@NonNull ThrowingRunnable runnable) {
43         SystemUtil.runWithShellPermissionIdentity(
44                 InstrumentationRegistry.getInstrumentation().getUiAutomation(),
45                 runnable, Manifest.permission.STATUS_BAR_SERVICE);
46     }
47 
toggleNotificationPolicyAccess(Context context, String packageName, boolean on)48     static void toggleNotificationPolicyAccess(Context context, String packageName,
49             boolean on) throws IOException {
50 
51         String command = " cmd notification " + (on ? "allow_dnd " : "disallow_dnd ") + packageName
52                 + " " + context.getUserId();
53 
54         runCommand(command, InstrumentationRegistry.getInstrumentation());
55         AmUtils.waitForBroadcastBarrier();
56 
57         NotificationManager nm = context.getSystemService(NotificationManager.class);
58         assertEquals("Notification Policy Access Grant is "
59                 + nm.isNotificationPolicyAccessGranted() + " not " + on + " for "
60                 + packageName, on, nm.isNotificationPolicyAccessGranted());
61     }
62 
runCommand(String command, Instrumentation instrumentation)63     private static void runCommand(String command, Instrumentation instrumentation)
64             throws IOException {
65         UiAutomation uiAutomation = instrumentation.getUiAutomation();
66         try (FileInputStream fis = new ParcelFileDescriptor.AutoCloseInputStream(
67                 uiAutomation.executeShellCommand(command))) {
68             FileUtils.readInputStreamFully(fis);
69         }
70     }
71 }
72