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