• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.text.TextUtils;
20 import android.util.Log;
21 import android.view.View;
22 
23 import androidx.annotation.NonNull;
24 import androidx.test.InstrumentationRegistry;
25 
26 /**
27  * Provides Shell-based utilities such as running a command.
28  */
29 public final class ShellUtils {
30 
31     private static final String TAG = "ShellHelper";
32 
33     private static final UserHelper sUserHelper = new UserHelper();
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("%s tap %d %d", sUserHelper.getInputCmd("touchscreen"), x, y);
63     }
64 
ShellUtils()65     private ShellUtils() {
66         throw new UnsupportedOperationException("contain static methods only");
67     }
68 
69     /**
70      * Simulates input of key event.
71      *
72      * @param keyCode key event to fire.
73      */
sendKeyEvent(String keyCode)74     public static void sendKeyEvent(String keyCode) {
75         runShellCommand("%s %s", sUserHelper.getInputCmd("keyevent"), keyCode);
76     }
77 
78     /**
79      * Allows an app to draw overlaid windows.
80      */
setOverlayPermissions(@onNull String packageName, boolean allowed)81     public static void setOverlayPermissions(@NonNull String packageName, boolean allowed) {
82         final String action = allowed ? "allow" : "ignore";
83         runShellCommand("%s %s SYSTEM_ALERT_WINDOW %s",
84                 sUserHelper.getAppopsCmd("set"), packageName, action);
85     }
86 }
87