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 
17 package com.android.compatibility.common.util;
18 
19 import static com.android.compatibility.common.util.ShellUtils.runShellCommand;
20 
21 import android.text.TextUtils;
22 import android.util.Log;
23 import android.view.View;
24 
25 import androidx.annotation.NonNull;
26 import androidx.test.InstrumentationRegistry;
27 
28 /**
29  * Provides Shell-based utilities such as running a command.
30  */
31 public final class ShellUtils {
32 
33     private static final String TAG = "ShellHelper";
34 
35     /**
36      * Runs a Shell command, returning a trimmed response.
37      */
38     @NonNull
runShellCommand(@onNull String template, Object...args)39     public static String runShellCommand(@NonNull String template, Object...args) {
40         final String command = String.format(template, args);
41         Log.d(TAG, "runShellCommand(): " + command);
42         try {
43             final String result = SystemUtil
44                     .runShellCommand(InstrumentationRegistry.getInstrumentation(), command);
45             return TextUtils.isEmpty(result) ? "" : result.trim();
46         } catch (Exception e) {
47             throw new RuntimeException("Command '" + command + "' failed: ", e);
48         }
49     }
50 
51     /**
52      * Tap on the view center, it may change window focus.
53      */
tap(View view)54     public static void tap(View view) {
55         final int[] xy = new int[2];
56         view.getLocationOnScreen(xy);
57         final int viewWidth = view.getWidth();
58         final int viewHeight = view.getHeight();
59         final int x = (int) (xy[0] + (viewWidth / 2.0f));
60         final int y = (int) (xy[1] + (viewHeight / 2.0f));
61 
62         runShellCommand("input touchscreen tap %d %d", x, y);
63     }
64 
65 
ShellUtils()66     private ShellUtils() {
67         throw new UnsupportedOperationException("contain static methods only");
68     }
69 
70     /**
71      * Simulates input of key event.
72      *
73      * @param keyCode key event to fire.
74      */
sendKeyEvent(String keyCode)75     public static void sendKeyEvent(String keyCode) {
76         runShellCommand("input keyevent " + keyCode);
77     }
78 
79     /**
80      * Allows an app to draw overlaid windows.
81      */
setOverlayPermissions(@onNull String packageName, boolean allowed)82     public static void setOverlayPermissions(@NonNull String packageName, boolean allowed) {
83         final String action = allowed ? "allow" : "ignore";
84         runShellCommand("appops set %s SYSTEM_ALERT_WINDOW %s", packageName, action);
85     }
86 }
87