1 /*
2  * Copyright (C) 2015 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.accessibility.cts.common;
18 
19 import android.app.Instrumentation;
20 import android.app.UiAutomation;
21 import android.os.ParcelFileDescriptor;
22 import android.util.Log;
23 
24 import com.android.compatibility.common.util.SystemUtil;
25 
26 import java.io.BufferedReader;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.nio.charset.StandardCharsets;
32 import java.util.LinkedList;
33 
34 /**
35  * A helper class to execute batch of shell commands.
36  */
37 public class ShellCommandBuilder {
38     private static final String LOG_TAG = "ShellCommandBuilder";
39 
40     private final LinkedList<Runnable> mCommands = new LinkedList<>();
41     private final UiAutomation mUiAutomation;
42 
43     /**
44      * Returns a {@link ShellCommandBuilder} with an {@link UiAutomation} which doesn't suppress
45      * accessibility service.
46      */
create(Instrumentation instrumentation)47     public static ShellCommandBuilder create(Instrumentation instrumentation) {
48         return new ShellCommandBuilder(instrumentation.getUiAutomation(
49                 UiAutomation.FLAG_DONT_SUPPRESS_ACCESSIBILITY_SERVICES));
50     }
51 
52     /**
53      * Returns a {@link ShellCommandBuilder} with {@code uiAutomation}.
54      */
create(UiAutomation uiAutomation)55     public static ShellCommandBuilder create(UiAutomation uiAutomation) {
56         return new ShellCommandBuilder(uiAutomation);
57     }
58 
ShellCommandBuilder(UiAutomation uiAutomation)59     private ShellCommandBuilder(UiAutomation uiAutomation) {
60         mUiAutomation = uiAutomation;
61     }
62 
run()63     public void run() {
64         for (Runnable command : mCommands) {
65             command.run();
66         }
67     }
68 
deleteSecureSetting(String name)69     public ShellCommandBuilder deleteSecureSetting(String name) {
70         addCommand("settings delete secure " + name);
71         return this;
72     }
73 
putSecureSetting(String name, String value)74     public ShellCommandBuilder putSecureSetting(String name, String value) {
75         addCommand("settings put secure " + name + " " + value);
76         return this;
77     }
78 
grantPermission(String packageName, String permission)79     public ShellCommandBuilder grantPermission(String packageName, String permission) {
80         mCommands.add(() -> {
81             mUiAutomation.grantRuntimePermission(packageName, permission);
82         });
83         return this;
84     }
85 
addCommand(String command)86     public ShellCommandBuilder addCommand(String command) {
87         mCommands.add(() -> {
88             execShellCommand(mUiAutomation, command);
89         });
90         return this;
91     }
92 
addCommandPrintOnLogCat(String command)93     public ShellCommandBuilder addCommandPrintOnLogCat(String command) {
94         mCommands.add(() -> {
95             execShellCommandAndPrintOnLogcat(mUiAutomation, command);
96         });
97         return this;
98     }
99 
execShellCommandAndPrintOnLogcat(UiAutomation automation, String command)100     public static void execShellCommandAndPrintOnLogcat(UiAutomation automation, String command) {
101         Log.i(LOG_TAG, "command [" + command + "]");
102         try {
103             final String output = SystemUtil.runShellCommand(automation, command);
104             for (String line : output.split("\\n", -1)) {
105                 Log.i(LOG_TAG, line);
106             }
107         } catch (IOException e) {
108             throw new RuntimeException("Failed to exec shell command [" + command + "]", e);
109         }
110     }
111 
execShellCommand(UiAutomation automation, String command)112     public static void execShellCommand(UiAutomation automation, String command) {
113         Log.i(LOG_TAG, "command [" + command + "]");
114         try (ParcelFileDescriptor fd = automation.executeShellCommand(command)) {
115             try (InputStream inputStream = new FileInputStream(fd.getFileDescriptor())) {
116                 try (BufferedReader reader = new BufferedReader(
117                         new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
118                     while (reader.readLine() != null) {
119                         // Keep reading.
120                     }
121                 }
122             }
123         } catch (IOException e) {
124             throw new RuntimeException("Failed to exec shell command [" + command + "]", e);
125         }
126     }
127 }
128