1 /* 2 * Copyright (C) 2015 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.media.cts; 18 19 import android.app.Instrumentation; 20 import android.app.UiAutomation; 21 import android.content.ContentResolver; 22 import android.content.Context; 23 import android.os.ParcelFileDescriptor; 24 import android.provider.Settings; 25 26 import java.io.FileInputStream; 27 import java.io.IOException; 28 import java.io.InputStream; 29 import java.util.Scanner; 30 31 public class Utils { enableAppOps(String packageName, String operation, Instrumentation instrumentation)32 public static void enableAppOps(String packageName, String operation, 33 Instrumentation instrumentation) { 34 setAppOps(packageName, operation, instrumentation, true); 35 } 36 disableAppOps(String packageName, String operation, Instrumentation instrumentation)37 public static void disableAppOps(String packageName, String operation, 38 Instrumentation instrumentation) { 39 setAppOps(packageName, operation, instrumentation, false); 40 } 41 convertStreamToString(InputStream is)42 public static String convertStreamToString(InputStream is) { 43 try (Scanner scanner = new Scanner(is).useDelimiter("\\A")) { 44 return scanner.hasNext() ? scanner.next() : ""; 45 } 46 } 47 setAppOps(String packageName, String operation, Instrumentation instrumentation, boolean enable)48 private static void setAppOps(String packageName, String operation, 49 Instrumentation instrumentation, boolean enable) { 50 StringBuilder cmd = new StringBuilder(); 51 cmd.append("appops set "); 52 cmd.append(packageName); 53 cmd.append(" "); 54 cmd.append(operation); 55 cmd.append(enable ? " allow" : " deny"); 56 instrumentation.getUiAutomation().executeShellCommand(cmd.toString()); 57 58 StringBuilder query = new StringBuilder(); 59 query.append("appops get "); 60 query.append(packageName); 61 query.append(" "); 62 query.append(operation); 63 String queryStr = query.toString(); 64 65 String expectedResult = enable ? "allow" : "deny"; 66 String result = ""; 67 while(!result.contains(expectedResult)) { 68 ParcelFileDescriptor pfd = instrumentation.getUiAutomation().executeShellCommand( 69 queryStr); 70 InputStream inputStream = new FileInputStream(pfd.getFileDescriptor()); 71 result = convertStreamToString(inputStream); 72 } 73 } 74 toggleNotificationPolicyAccess(String packageName, Instrumentation instrumentation, boolean on)75 protected static void toggleNotificationPolicyAccess(String packageName, 76 Instrumentation instrumentation, boolean on) throws IOException { 77 Context context = instrumentation.getContext(); 78 79 // Get permission to enable accessibility 80 UiAutomation uiAutomation = instrumentation.getUiAutomation(); 81 82 ContentResolver cr = context.getContentResolver(); 83 String alreadyEnabledServices = Settings.Secure.getString( 84 cr, Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES); 85 ParcelFileDescriptor fd = null; 86 if (on) { 87 if ((alreadyEnabledServices == null) || !alreadyEnabledServices.contains(packageName)) { 88 // Change the settings to enable the media cts package 89 final String newEnabledServices = (alreadyEnabledServices == null) ? packageName 90 : alreadyEnabledServices + ":" + packageName; 91 fd = uiAutomation.executeShellCommand( 92 "settings --user cur put secure " 93 + Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES + " " 94 + newEnabledServices); 95 } 96 } else if (alreadyEnabledServices != null) { 97 int index = alreadyEnabledServices.indexOf(":" + packageName); 98 if (index >= 0) { 99 fd = uiAutomation.executeShellCommand( 100 "settings --user cur put secure " 101 + Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES + " " 102 + alreadyEnabledServices.substring(0, index)); 103 } else if (alreadyEnabledServices.equals(packageName)) { 104 // "packageName" is the only enabled service 105 fd = uiAutomation.executeShellCommand("settings --user cur put secure " 106 + Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES + " null"); 107 } 108 } 109 if (fd != null) { 110 InputStream in = new FileInputStream(fd.getFileDescriptor()); 111 byte[] buffer = new byte[4096]; 112 while (in.read(buffer) > 0) ; 113 } 114 uiAutomation.destroy(); 115 } 116 } 117