1 /* 2 * Copyright (C) 2011 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.telephony.cts; 18 19 import android.app.Instrumentation; 20 import android.os.ParcelFileDescriptor; 21 import android.telephony.TelephonyManager; 22 23 import java.io.BufferedReader; 24 import java.io.FileInputStream; 25 import java.io.InputStream; 26 import java.io.InputStreamReader; 27 import java.nio.charset.StandardCharsets; 28 29 public class TelephonyUtils { 30 private static final String COMMAND_ADD_TEST_EMERGENCY_NUMBER = 31 "cmd phone emergency-number-test-mode -a "; 32 33 private static final String COMMAND_REMOVE_TEST_EMERGENCY_NUMBER = 34 "cmd phone emergency-number-test-mode -r "; 35 36 private static final String COMMAND_END_BLOCK_SUPPRESSION = "cmd phone end-block-suppression"; 37 addTestEmergencyNumber(Instrumentation instr, String testNumber)38 public static void addTestEmergencyNumber(Instrumentation instr, String testNumber) 39 throws Exception { 40 executeShellCommand(instr, COMMAND_ADD_TEST_EMERGENCY_NUMBER + testNumber); 41 } 42 removeTestEmergencyNumber(Instrumentation instr, String testNumber)43 public static void removeTestEmergencyNumber(Instrumentation instr, String testNumber) 44 throws Exception { 45 executeShellCommand(instr, COMMAND_REMOVE_TEST_EMERGENCY_NUMBER + testNumber); 46 } 47 endBlockSuppression(Instrumentation instr)48 public static void endBlockSuppression(Instrumentation instr) throws Exception { 49 executeShellCommand(instr, COMMAND_END_BLOCK_SUPPRESSION); 50 } 51 isSkt(TelephonyManager telephonyManager)52 public static boolean isSkt(TelephonyManager telephonyManager) { 53 return isOperator(telephonyManager, "45005"); 54 } 55 isKt(TelephonyManager telephonyManager)56 public static boolean isKt(TelephonyManager telephonyManager) { 57 return isOperator(telephonyManager, "45002") 58 || isOperator(telephonyManager, "45004") 59 || isOperator(telephonyManager, "45008"); 60 } 61 isOperator(TelephonyManager telephonyManager, String operator)62 private static boolean isOperator(TelephonyManager telephonyManager, String operator) { 63 String simOperator = telephonyManager.getSimOperator(); 64 return simOperator != null && simOperator.equals(operator); 65 } 66 67 /** 68 * Executes the given shell command and returns the output in a string. Note that even 69 * if we don't care about the output, we have to read the stream completely to make the 70 * command execute. 71 */ executeShellCommand(Instrumentation instrumentation, String command)72 public static String executeShellCommand(Instrumentation instrumentation, 73 String command) throws Exception { 74 final ParcelFileDescriptor pfd = 75 instrumentation.getUiAutomation().executeShellCommand(command); 76 BufferedReader br = null; 77 try (InputStream in = new FileInputStream(pfd.getFileDescriptor())) { 78 br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)); 79 String str = null; 80 StringBuilder out = new StringBuilder(); 81 while ((str = br.readLine()) != null) { 82 out.append(str); 83 } 84 return out.toString(); 85 } finally { 86 if (br != null) { 87 closeQuietly(br); 88 } 89 closeQuietly(pfd); 90 } 91 } 92 closeQuietly(AutoCloseable closeable)93 private static void closeQuietly(AutoCloseable closeable) { 94 if (closeable != null) { 95 try { 96 closeable.close(); 97 } catch (RuntimeException rethrown) { 98 throw rethrown; 99 } catch (Exception ignored) { 100 } 101 } 102 } 103 } 104