Home | History | Annotate | Download | only in am
      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.am;
     18 
     19 import static android.server.am.StateLogger.logE;
     20 import static android.support.test.InstrumentationRegistry.getContext;
     21 import static android.view.KeyEvent.KEYCODE_APP_SWITCH;
     22 import static android.view.KeyEvent.KEYCODE_MENU;
     23 import static android.view.KeyEvent.KEYCODE_SLEEP;
     24 import static android.view.KeyEvent.KEYCODE_WAKEUP;
     25 import static android.view.KeyEvent.KEYCODE_WINDOW;
     26 
     27 import android.app.KeyguardManager;
     28 import android.graphics.Point;
     29 import android.os.PowerManager;
     30 import android.os.RemoteException;
     31 import android.os.SystemClock;
     32 import android.support.test.InstrumentationRegistry;
     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     static void pressHomeButton() {
     70         if (DEBUG) Log.d(TAG, "pressHomeButton");
     71         getDevice().pressHome();
     72     }
     73 
     74     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 = getContext().getSystemService(PowerManager.class);
     87         retryPressKeyCode(KEYCODE_SLEEP, () -> pm != null && !pm.isInteractive(),
     88                 "***Waiting for device sleep...");
     89     }
     90 
     91     static void pressWakeupButton() {
     92         if (DEBUG) Log.d(TAG, "pressWakeupButton");
     93         final PowerManager pm = getContext().getSystemService(PowerManager.class);
     94         retryPressKeyCode(KEYCODE_WAKEUP, () -> pm != null && pm.isInteractive(),
     95                 "***Waiting for device wakeup...");
     96     }
     97 
     98     static void pressUnlockButton() {
     99         if (DEBUG) Log.d(TAG, "pressUnlockButton");
    100         final KeyguardManager kgm = getContext().getSystemService(KeyguardManager.class);
    101         retryPressKeyCode(KEYCODE_MENU, () -> kgm != null && !kgm.isKeyguardLocked(),
    102                 "***Waiting for device unlock...");
    103     }
    104 
    105     static void pressAppSwitchButton() {
    106         if (DEBUG) Log.d(TAG, "pressAppSwitchButton");
    107         pressKeyCode(KEYCODE_APP_SWITCH);
    108     }
    109 
    110     private static void retryPressKeyCode(int keyCode, BooleanSupplier waitFor, String msg) {
    111         int retry = 1;
    112         do {
    113             pressKeyCode(keyCode);
    114             if (waitFor.getAsBoolean()) {
    115                 return;
    116             }
    117             Log.d(TAG, msg + " retry=" + retry);
    118             SystemClock.sleep(50);
    119         } while (retry++ < 5);
    120         if (!waitFor.getAsBoolean()) {
    121             logE(msg + " FAILED");
    122         }
    123     }
    124 
    125     private static void pressKeyCode(int keyCode) {
    126         getDevice().pressKeyCode(keyCode);
    127     }
    128 
    129     private static UiDevice getDevice() {
    130         return UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    131     }
    132 }
    133