| /* |
| * Copyright (C) 2015 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package android.calllog.cts; |
| |
| import android.app.Instrumentation; |
| import android.content.ComponentName; |
| import android.content.Context; |
| import android.content.pm.PackageManager; |
| import android.os.Build; |
| import android.os.ParcelFileDescriptor; |
| import android.telecom.PhoneAccountHandle; |
| |
| import java.io.BufferedInputStream; |
| import java.io.BufferedReader; |
| import java.io.FileInputStream; |
| import java.io.InputStream; |
| import java.io.InputStreamReader; |
| import java.nio.charset.StandardCharsets; |
| |
| public class TestUtils { |
| /** |
| * Executes the given shell command and returns the output in a string. Note that even |
| * if we don't care about the output, we have to read the stream completely to make the |
| * command execute. |
| */ |
| public static String executeShellCommand(Instrumentation instrumentation, |
| String command) throws Exception { |
| BufferedReader br = null; |
| try (InputStream in = executeStreamedShellCommand(instrumentation, command)) { |
| br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)); |
| String str = null; |
| StringBuilder out = new StringBuilder(); |
| while ((str = br.readLine()) != null) { |
| out.append(str); |
| } |
| return out.toString(); |
| } finally { |
| if (br != null) { |
| closeQuietly(br); |
| } |
| } |
| } |
| |
| public static FileInputStream executeStreamedShellCommand(Instrumentation instrumentation, |
| String command) throws Exception { |
| final ParcelFileDescriptor pfd = |
| instrumentation.getUiAutomation().executeShellCommand(command); |
| return new FileInputStream(pfd.getFileDescriptor()); |
| } |
| |
| private static void closeQuietly(AutoCloseable closeable) { |
| if (closeable != null) { |
| try { |
| closeable.close(); |
| } catch (RuntimeException rethrown) { |
| throw rethrown; |
| } catch (Exception ignored) { |
| } |
| } |
| } |
| } |