• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.system.helpers;
18 
19 import android.app.KeyguardManager;
20 import android.content.Context;
21 import android.provider.Settings;
22 import android.support.test.InstrumentationRegistry;
23 import android.support.test.uiautomator.By;
24 import android.support.test.uiautomator.UiDevice;
25 import android.support.test.uiautomator.UiObject2;
26 import android.support.test.uiautomator.Until;
27 import android.system.helpers.ActivityHelper;
28 
29 import junit.framework.Assert;
30 
31 import java.io.IOException;
32 /**
33  * Implement common helper methods for Lockscreen.
34  */
35 public class LockscreenHelper {
36     private static final String LOG_TAG = LockscreenHelper.class.getSimpleName();
37     public static final int SHORT_TIMEOUT = 200;
38     public static final int LONG_TIMEOUT = 2000;
39     public static final String EDIT_TEXT_CLASS_NAME = "android.widget.EditText";
40     public static final String CAMERA2_PACKAGE = "com.android.camera2";
41     public static final String CAMERA_PACKAGE = "com.google.android.GoogleCamera";
42     public static final String MODE_PIN = "PIN";
43     private static final int SWIPE_MARGIN = 5;
44     private static final int DEFAULT_FLING_STEPS = 5;
45     private static final int DEFAULT_SCROLL_STEPS = 15;
46 
47     private static final String SET_PIN_COMMAND = "locksettings set-pin %s";
48     private static final String CLEAR_COMMAND = "locksettings clear --old %s";
49 
50     private static LockscreenHelper sInstance = null;
51     private Context mContext = null;
52     private UiDevice mDevice = null;
53     private final ActivityHelper mActivityHelper;
54     private final CommandsHelper mCommandsHelper;
55     private final DeviceHelper mDeviceHelper;
56     private boolean mIsRyuDevice = false;
57 
LockscreenHelper()58     public LockscreenHelper() {
59         mContext = InstrumentationRegistry.getTargetContext();
60         mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
61         mActivityHelper = ActivityHelper.getInstance();
62         mCommandsHelper = CommandsHelper.getInstance(InstrumentationRegistry.getInstrumentation());
63         mDeviceHelper = DeviceHelper.getInstance();
64         mIsRyuDevice = mDeviceHelper.isRyuDevice();
65 
66     }
67 
getInstance()68     public static LockscreenHelper getInstance() {
69         if (sInstance == null) {
70             sInstance = new LockscreenHelper();
71         }
72         return sInstance;
73     }
74 
75     /**
76      * Launch Camera on LockScreen
77      * @return true/false
78      */
launchCameraOnLockScreen()79     public boolean launchCameraOnLockScreen() {
80         String CameraPackage = mIsRyuDevice ? CAMERA2_PACKAGE : CAMERA_PACKAGE;
81         int w = mDevice.getDisplayWidth();
82         int h = mDevice.getDisplayHeight();
83         // Load camera on LockScreen and take a photo
84         mDevice.drag((w - 25), (h - 25), (int) (w * 0.5), (int) (w * 0.5), 40);
85         mDevice.waitForIdle();
86         return mDevice.wait(Until.hasObject(
87                 By.res(CameraPackage, "activity_root_view")),
88                 LONG_TIMEOUT * 2);
89     }
90 
91      /**
92      * Sets the screen lock pin or password
93      * @param pwd text of Password or Pin for lockscreen
94      * @param mode indicate if its password or PIN
95      * @throws InterruptedException
96      */
setScreenLock(String pwd, String mode, boolean mIsNexusDevice)97     public void setScreenLock(String pwd, String mode, boolean mIsNexusDevice) throws InterruptedException {
98         navigateToScreenLock();
99         mDevice.wait(Until.findObject(By.text(mode)), LONG_TIMEOUT * 2).click();
100         // set up Secure start-up page
101         if (!mIsNexusDevice) {
102             mDevice.wait(Until.findObject(By.text("No thanks")), LONG_TIMEOUT).click();
103         }
104         UiObject2 pinField = mDevice.wait(Until.findObject(By.clazz(EDIT_TEXT_CLASS_NAME)),
105                 LONG_TIMEOUT);
106         pinField.setText(pwd);
107         // enter and verify password
108         mDevice.pressEnter();
109         pinField.setText(pwd);
110         mDevice.pressEnter();
111         mDevice.wait(Until.findObject(By.text("DONE")), LONG_TIMEOUT).click();
112     }
113 
114     /**
115      * check if Emergency Call page exists
116      * @throws InterruptedException
117      */
checkEmergencyCallOnLockScreen()118     public void checkEmergencyCallOnLockScreen() throws InterruptedException {
119         mDevice.pressMenu();
120         mDevice.wait(Until.findObject(By.text("EMERGENCY")), LONG_TIMEOUT).click();
121         Thread.sleep(LONG_TIMEOUT);
122         UiObject2 dialButton = mDevice.wait(Until.findObject(By.desc("dial")),
123                 LONG_TIMEOUT);
124         Assert.assertNotNull("Can't reach emergency call page", dialButton);
125         mDevice.pressBack();
126         Thread.sleep(LONG_TIMEOUT);
127     }
128 
129     /**
130      * remove Screen Lock
131      * @throws InterruptedException
132      */
removeScreenLock(String pwd)133     public void removeScreenLock(String pwd)
134             throws InterruptedException {
135         navigateToScreenLock();
136         UiObject2 pinField = mDevice.wait(Until.findObject(By.clazz(EDIT_TEXT_CLASS_NAME)),
137                 LONG_TIMEOUT);
138         pinField.setText(pwd);
139         mDevice.pressEnter();
140         mDevice.wait(Until.findObject(By.text("Swipe")), LONG_TIMEOUT).click();
141         mDevice.waitForIdle();
142         mDevice.wait(Until.findObject(By.text("YES, REMOVE")), LONG_TIMEOUT).click();
143     }
144 
145     /**
146      * unlock screen
147      * @throws InterruptedException, IOException
148      */
unlockScreen(String pwd)149     public void unlockScreen(String pwd)
150             throws InterruptedException, IOException {
151         String command = String.format(" %s %s %s", "input", "keyevent", "82");
152         mDevice.executeShellCommand(command);
153         Thread.sleep(SHORT_TIMEOUT);
154         Thread.sleep(SHORT_TIMEOUT);
155         // enter password to unlock screen
156         command = String.format(" %s %s %s", "input", "text", pwd);
157         mDevice.executeShellCommand(command);
158         mDevice.waitForIdle();
159         Thread.sleep(SHORT_TIMEOUT);
160         mDevice.pressEnter();
161     }
162 
163     /**
164      * navigate to screen lock setting page
165      * @throws InterruptedException
166      */
navigateToScreenLock()167     public void navigateToScreenLock()
168             throws InterruptedException {
169         mActivityHelper.launchIntent(Settings.ACTION_SECURITY_SETTINGS);
170         mDevice.wait(Until.findObject(By.text("Screen lock")), LONG_TIMEOUT).click();
171     }
172 
173     /**
174      * check if Lock Screen is enabled
175      */
isLockScreenEnabled()176     public boolean isLockScreenEnabled() {
177         KeyguardManager km = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
178         return km.isKeyguardSecure();
179     }
180 
181     /**
182      * Sets a screen lock via shell.
183      */
setScreenLockViaShell(String pwd, String mode)184     public void setScreenLockViaShell(String pwd, String mode) throws Exception {
185         switch (mode) {
186             case MODE_PIN:
187                 mCommandsHelper.executeShellCommand(String.format(SET_PIN_COMMAND, pwd));
188                 break;
189             default:
190                 throw new IllegalArgumentException("Unsupported mode: " + mode);
191         }
192     }
193 
194     /**
195      * Removes the screen lock via shell.
196      */
removeScreenLockViaShell(String pwd)197     public void removeScreenLockViaShell(String pwd) throws Exception {
198         mCommandsHelper.executeShellCommand(String.format(CLEAR_COMMAND, pwd));
199     }
200 
201     /**
202      * swipe up to unlock the screen
203      */
unlockScreenSwipeUp()204     public void unlockScreenSwipeUp() throws Exception {
205         mDevice.wakeUp();
206         mDevice.waitForIdle();
207         mDevice.swipe(mDevice.getDisplayWidth() / 2,
208                 mDevice.getDisplayHeight() - SWIPE_MARGIN,
209                 mDevice.getDisplayWidth() / 2,
210                 SWIPE_MARGIN,
211                 DEFAULT_SCROLL_STEPS);
212         mDevice.waitForIdle();
213     }
214 }