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