1 /* 2 * Copyright (C) 2017 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 package android.system.helpers; 17 18 import android.app.Instrumentation; 19 import android.support.test.InstrumentationRegistry; 20 import android.support.test.uiautomator.UiDevice; 21 import android.util.Log; 22 import java.io.IOException; 23 import java.util.Arrays; 24 import java.util.List; 25 26 /** 27 * Implement common helper for executing shell commands on device 28 */ 29 public class CommandsHelper { 30 private static final String TAG = CommandsHelper.class.getSimpleName(); 31 private static CommandsHelper sInstance = null; 32 private Instrumentation mInstrumentation = null; 33 34 private static final String LINE_SEPARATORS = "\\r?\\n"; 35 36 37 private CommandsHelper(Instrumentation instrumentation) { 38 mInstrumentation = instrumentation; 39 } 40 41 /** 42 * @deprecated Should use {@link CommandsHelper#getInstance(Instrumentation)} instead. 43 */ 44 @Deprecated 45 public static CommandsHelper getInstance() { 46 if (sInstance == null) { 47 sInstance = new CommandsHelper(InstrumentationRegistry.getInstrumentation()); 48 } 49 return sInstance; 50 } 51 52 public static CommandsHelper getInstance(Instrumentation instrumentation) { 53 if (sInstance == null) { 54 sInstance = new CommandsHelper(instrumentation); 55 } else { 56 sInstance.injectInstrumentation(instrumentation); 57 } 58 return sInstance; 59 } 60 61 /** 62 * Injects instrumentation into this helper. 63 * 64 * @param instrumentation the instrumentation to use with this instance 65 */ 66 public void injectInstrumentation(Instrumentation instrumentation) { 67 mInstrumentation = instrumentation; 68 } 69 70 /** 71 * Executes a shell command on device, and return the standard output in string. 72 * @param command the command to run 73 * @return the standard output of the command, or empty string if 74 * failed without throwing an IOException 75 */ 76 public String executeShellCommand(String command) { 77 try { 78 return UiDevice.getInstance(mInstrumentation).executeShellCommand(command); 79 } catch (IOException e) { 80 // ignore 81 Log.e(TAG, String.format("The shell command failed to run: %s exception: %s", 82 command, e.getMessage())); 83 return ""; 84 } 85 } 86 87 /** 88 * Executes a shell command on device, and split the multi-line output into collection 89 * @param command the command to run 90 * @param separatorChars the line separator 91 * @return the List of strings from the standard output of the command 92 */ 93 public List<String> executeShellCommandAndSplitOutput(String command, 94 final String separatorChars) { 95 return Arrays.asList(executeShellCommand(command).split(separatorChars)); 96 } 97 98 /** 99 * Convenience version of {@link #executeShellCommand} for use without having a reference to 100 * CommandsHelper. 101 * @param command the command to run 102 */ 103 @Deprecated 104 public static String execute(String command) { 105 return getInstance().executeShellCommand(command); 106 } 107 108 /** 109 * Convenience version of {@link #executeShellCommandAndSplitOutput} for use 110 * without having a reference to CommandsHelper. 111 * @param command the command to run 112 */ 113 @Deprecated 114 public static List<String> executeAndSplitLines(String command) { 115 return getInstance().executeShellCommandAndSplitOutput(command, LINE_SEPARATORS); 116 } 117 } 118