Home | History | Annotate | Download | only in wm
      1 /*
      2  * Copyright (C) 2018 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.server.wm;
     18 
     19 import static android.server.wm.StateLogger.logE;
     20 import static android.view.KeyEvent.KEYCODE_APP_SWITCH;
     21 import static android.view.KeyEvent.KEYCODE_MENU;
     22 import static android.view.KeyEvent.KEYCODE_SLEEP;
     23 import static android.view.KeyEvent.KEYCODE_WAKEUP;
     24 import static android.view.KeyEvent.KEYCODE_WINDOW;
     25 
     26 import static androidx.test.InstrumentationRegistry.getInstrumentation;
     27 
     28 import android.app.KeyguardManager;
     29 import android.graphics.Point;
     30 import android.os.PowerManager;
     31 import android.os.RemoteException;
     32 import android.os.SystemClock;
     33 import android.support.test.uiautomator.UiDevice;
     34 import android.util.Log;
     35 import android.view.KeyEvent;
     36 
     37 import java.util.function.BooleanSupplier;
     38 
     39 /**
     40  * Helper class to interact with {@link UiDevice}.
     41  *
     42  * All references to {@link UiDevice} and {@link KeyEvent} should be here for easy debugging.
     43  */
     44 public class UiDeviceUtils {
     45 
     46     private static final String TAG = "UiDeviceUtils";
     47     private static final boolean DEBUG = false;
     48 
     49     static void waitForDeviceIdle(long timeout) {
     50         if (DEBUG) Log.d(TAG, "waitForDeviceIdle: timeout=" + timeout);
     51         getDevice().waitForIdle(timeout);
     52     }
     53 
     54     public static void wakeUpDevice() throws RemoteException {
     55         if (DEBUG) Log.d(TAG, "wakeUpDevice");
     56         getDevice().wakeUp();
     57     }
     58 
     59     public static void dragPointer(Point from, Point to, int steps) {
     60         if (DEBUG) Log.d(TAG, "dragPointer: from=" + from + " to=" + to + " steps=" + steps);
     61         getDevice().drag(from.x, from.y, to.x, to.y, steps);
     62     }
     63 
     64     static void pressEnterButton() {
     65         if (DEBUG) Log.d(TAG, "pressEnterButton");
     66         getDevice().pressEnter();
     67     }
     68 
     69     public static void pressHomeButton() {
     70         if (DEBUG) Log.d(TAG, "pressHomeButton");
     71         getDevice().pressHome();
     72     }
     73 
     74     public static void pressBackButton() {
     75         if (DEBUG) Log.d(TAG, "pressBackButton");
     76         getDevice().pressBack();
     77     }
     78 
     79     public static void pressMenuButton() {
     80         if (DEBUG) Log.d(TAG, "pressMenuButton");
     81         getDevice().pressMenu();
     82     }
     83 
     84     static void pressSleepButton() {
     85         if (DEBUG) Log.d(TAG, "pressSleepButton");
     86         final PowerManager pm = getInstrumentation()
     87                 .getContext().getSystemService(PowerManager.class);
     88         retryPressKeyCode(KEYCODE_SLEEP, () -> pm != null && !pm.isInteractive(),
     89                 "***Waiting for device sleep...");
     90     }
     91 
     92     public static void pressWakeupButton() {
     93         if (DEBUG) Log.d(TAG, "pressWakeupButton");
     94         final PowerManager pm = getInstrumentation()
     95                 .getContext().getSystemService(PowerManager.class);
     96         retryPressKeyCode(KEYCODE_WAKEUP, () -> pm != null && pm.isInteractive(),
     97                 "***Waiting for device wakeup...");
     98     }
     99 
    100     public static void pressUnlockButton() {
    101         if (DEBUG) Log.d(TAG, "pressUnlockButton");
    102         final KeyguardManager kgm = getInstrumentation()
    103                 .getContext().getSystemService(KeyguardManager.class);
    104         retryPressKeyCode(KEYCODE_MENU, () -> kgm != null && !kgm.isKeyguardLocked(),
    105                 "***Waiting for device unlock...");
    106     }
    107 
    108     static void pressWindowButton() {
    109         if (DEBUG) Log.d(TAG, "pressWindowButton");
    110         pressKeyCode(KEYCODE_WINDOW);
    111     }
    112 
    113     static void pressAppSwitchButton() {
    114         if (DEBUG) Log.d(TAG, "pressAppSwitchButton");
    115         pressKeyCode(KEYCODE_APP_SWITCH);
    116     }
    117 
    118     private static void retryPressKeyCode(int keyCode, BooleanSupplier waitFor, String msg) {
    119         int retry = 1;
    120         do {
    121             pressKeyCode(keyCode);
    122             if (waitFor.getAsBoolean()) {
    123                 return;
    124             }
    125             Log.d(TAG, msg + " retry=" + retry);
    126             SystemClock.sleep(50);
    127         } while (retry++ < 5);
    128         if (!waitFor.getAsBoolean()) {
    129             logE(msg + " FAILED");
    130         }
    131     }
    132 
    133     private static void pressKeyCode(int keyCode) {
    134         getDevice().pressKeyCode(keyCode);
    135     }
    136 
    137     private static UiDevice getDevice() {
    138         return UiDevice.getInstance(getInstrumentation());
    139     }
    140 }
    141