Home | History | Annotate | Download | only in cts
      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.accessibilityservice.cts;
     18 
     19 import android.app.Instrumentation;
     20 import android.app.UiAutomation;
     21 import android.os.ParcelFileDescriptor;
     22 
     23 import java.io.BufferedReader;
     24 import java.io.FileInputStream;
     25 import java.io.IOException;
     26 import java.io.InputStream;
     27 import java.io.InputStreamReader;
     28 import java.nio.charset.StandardCharsets;
     29 import java.util.LinkedList;
     30 
     31 public class ShellCommandBuilder {
     32     private final LinkedList<Runnable> mCommands = new LinkedList<>();
     33 
     34     private final Instrumentation mInstrumentation;
     35     private final UiAutomation mUiAutomation;
     36 
     37     public static ShellCommandBuilder create(Instrumentation instrumentation) {
     38         return new ShellCommandBuilder(instrumentation);
     39     }
     40 
     41     private ShellCommandBuilder(Instrumentation instrumentation) {
     42         mInstrumentation = instrumentation;
     43         mUiAutomation = mInstrumentation.getUiAutomation(
     44                 UiAutomation.FLAG_DONT_SUPPRESS_ACCESSIBILITY_SERVICES);
     45     }
     46 
     47     public void run() {
     48         for (Runnable command : mCommands) {
     49             command.run();
     50         }
     51     }
     52 
     53     public ShellCommandBuilder deleteSecureSetting(String name) {
     54         addCommand("settings delete secure " + name);
     55         return this;
     56     }
     57 
     58     public ShellCommandBuilder putSecureSetting(String name, String value) {
     59         addCommand("settings put secure " + name + " " + value);
     60         return this;
     61     }
     62 
     63     public ShellCommandBuilder grantPermission(String packageName, String permission) {
     64         mCommands.add(() -> {
     65             mUiAutomation.grantRuntimePermission(packageName, permission);
     66         });
     67         return this;
     68     }
     69 
     70     public ShellCommandBuilder addCommand(String command) {
     71         mCommands.add(() -> {
     72             execShellCommand(mUiAutomation, command);
     73         });
     74         return this;
     75     }
     76 
     77     public static void execShellCommand(UiAutomation automation, String command) {
     78         try (ParcelFileDescriptor fd = automation.executeShellCommand(command)) {
     79             try (InputStream inputStream = new FileInputStream(fd.getFileDescriptor())) {
     80                 try (BufferedReader reader = new BufferedReader(
     81                         new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
     82                     while (reader.readLine() != null) {
     83                         // Keep reading.
     84                     }
     85                 }
     86             }
     87         } catch (IOException e) {
     88             throw new RuntimeException("Failed to exec shell command", e);
     89         }
     90     }
     91 }
     92