• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.platform.helpers;
18 
19 import static android.content.Context.KEYGUARD_SERVICE;
20 import static android.os.SystemClock.sleep;
21 import static android.platform.helpers.CommonUtils.executeShellCommand;
22 import static android.platform.helpers.Constants.SHORT_WAIT_TIME_IN_SECONDS;
23 import static android.platform.helpers.ui.UiAutomatorUtils.getUiDevice;
24 import static android.platform.uiautomatorhelpers.DeviceHelpers.getContext;
25 import static android.platform.uiautomatorhelpers.WaitUtils.ensureThat;
26 import static android.view.KeyEvent.KEYCODE_ENTER;
27 
28 import static com.google.common.truth.Truth.assertThat;
29 
30 import static org.junit.Assert.assertEquals;
31 
32 import static java.lang.String.format;
33 import static java.lang.System.currentTimeMillis;
34 import static java.util.concurrent.TimeUnit.SECONDS;
35 
36 import android.app.KeyguardManager;
37 import android.content.ContentResolver;
38 import android.os.RemoteException;
39 import android.platform.helpers.features.common.HomeLockscreenPage;
40 import android.platform.test.util.HealthTestingUtils;
41 import android.provider.Settings;
42 import android.util.Log;
43 
44 import androidx.test.platform.app.InstrumentationRegistry;
45 
46 /**
47  * All required util for Lockscreen.
48  * @deprecated use classes from the "systemui-tapl" library instead
49  */
50 @Deprecated
51 public class LockscreenUtils {
52     private static final String TAG = "LockscreenUtils";
53     private static final String RESET_LOCKSCREEN_SHELL_COMMAND = "locksettings clear --old";
54     private static final String INPUT_KEYEVENT_COMMAND = "input keyevent";
55     private static final String INPUT_TEXT_COMMAND = "input keyboard text";
56     private static final String SET_PASSWORD_COMMAND = "locksettings set-password";
57     private static final String SET_PIN_COMMAND = "locksettings set-pin";
58     private static final String SET_PATTERN_COMMAND = "locksettings set-pattern";
59     private static final String SET_SWIPE_COMMAND = "locksettings set-disabled false";
60     private static final String SET_LOCK_AS_NONE_COMMAND = "locksettings set-disabled true";
61     // The SysUI/WM goingAway timeout is 10s. Give that time to process before timing out.
62     private static final int MAX_LOCKSCREEN_TIMEOUT_IN_SEC = 15;
63 
64     public static int sPreviousAodSetting;
65 
LockscreenUtils()66     private LockscreenUtils() {
67     }
68 
69     /**
70      * To get an instance of class that can be used to lock and unlock the keygaurd.
71      *
72      * @return an instance of class that can be used to lock and unlock the screen.
73      */
getKeyguardManager()74     public static final KeyguardManager getKeyguardManager() {
75         return (KeyguardManager) getContext().getSystemService(KEYGUARD_SERVICE);
76     }
77 
78     /**
79      * Different way to set the Lockscreen for Android device. Currently we only support PIN,
80      * PATTERN and PASSWORD
81      *
82      * @param lockscreenType it enum with list of supported lockscreen type
83      * @param lockscreenCode code[PIN or PATTERN or PASSWORD] which needs to be set.
84      * @param expectedResult expected result after setting the lockscreen because for lock type
85      *                       Swipe and None Keygaurd#isKeyguardSecure remain unlocked i.e. false.
86      */
setLockscreen(LockscreenType lockscreenType, String lockscreenCode, boolean expectedResult)87     public static void setLockscreen(LockscreenType lockscreenType, String lockscreenCode,
88             boolean expectedResult) {
89         Log.d(TAG, format("Setting Lockscreen [%s(%s)]", lockscreenType, lockscreenCode));
90         switch (lockscreenType) {
91             case PIN:
92                 executeShellCommand(format("%s %s", SET_PIN_COMMAND, lockscreenCode));
93                 break;
94             case PASSWORD:
95                 executeShellCommand(format("%s %s", SET_PASSWORD_COMMAND, lockscreenCode));
96                 break;
97             case PATTERN:
98                 executeShellCommand(format("%s %s", SET_PATTERN_COMMAND, lockscreenCode));
99                 break;
100             case SWIPE:
101                 executeShellCommand(SET_SWIPE_COMMAND);
102                 break;
103             case NONE:
104                 executeShellCommand(SET_LOCK_AS_NONE_COMMAND);
105                 break;
106             default:
107                 throw new AssertionError("Non-supported Lockscreen Type: " + lockscreenType);
108         }
109         assertKeyguardSecure(expectedResult);
110     }
111 
assertKeyguardSecure(boolean expectedSecure)112     private static void assertKeyguardSecure(boolean expectedSecure) {
113         HealthTestingUtils.waitForCondition(
114                 () -> String.format("Assert that keyguard %s secure, but failed.",
115                         expectedSecure ? "is" : "isn't"),
116                 () -> getKeyguardManager().isKeyguardSecure() == expectedSecure);
117     }
118 
119     /**
120      * Resets the give lockscreen.
121      *
122      * @param lockscreenCode old code which is currently set.
123      */
resetLockscreen(String lockscreenCode)124     public static void resetLockscreen(String lockscreenCode) {
125         Log.d(TAG, String.format("Re-Setting Lockscreen %s", lockscreenCode));
126         executeShellCommand(
127                 format("%s %s", RESET_LOCKSCREEN_SHELL_COMMAND, lockscreenCode));
128         assertKeyguardSecure(/* expectedSecure= */ false);
129     }
130 
131     /**
132      * Entering the given code on the lockscreen
133      *
134      * @param lockscreenType type of lockscreen set.
135      * @param lockscreenCode valid lockscreen code.
136      */
enterCodeOnLockscreen(LockscreenType lockscreenType, String lockscreenCode)137     public static void enterCodeOnLockscreen(LockscreenType lockscreenType,
138             String lockscreenCode) {
139         Log.d(TAG,
140                 format("Entering Lockscreen code: %s(%s)", lockscreenType, lockscreenCode));
141         assertEquals("Lockscreen was not set", true,
142                 getKeyguardManager().isKeyguardSecure());
143         switch (lockscreenType) {
144             case PIN:
145             case PASSWORD:
146                 // Entering the lockscreen code in text box.
147                 executeShellCommand(format("%s %s", INPUT_TEXT_COMMAND, lockscreenCode));
148                 // Pressing the ENTER button after entering the code.
149                 executeShellCommand(format("%s %s", INPUT_KEYEVENT_COMMAND, KEYCODE_ENTER));
150                 break;
151             default:
152                 throw new AssertionError("Non-supported Lockscreen Type: " + lockscreenType);
153         }
154     }
155 
156     /**
157      * Check if the device is locked as per the user expectation.
158      *
159      * @param expectedLockStatus expected device lock status.
160      */
checkDeviceLock(boolean expectedLockStatus)161     public static void checkDeviceLock(boolean expectedLockStatus) {
162         Log.d(TAG, format("Checking device lock status: %s", expectedLockStatus));
163         long endTime = currentTimeMillis() + SECONDS.toMillis(MAX_LOCKSCREEN_TIMEOUT_IN_SEC);
164         while (currentTimeMillis() <= endTime) {
165             if (getKeyguardManager().isDeviceLocked() == expectedLockStatus) {
166                 break;
167             }
168             try {
169                 Thread.sleep(100);
170             } catch (InterruptedException e) {
171                 e.printStackTrace();
172             }
173         }
174         assertThat(getKeyguardManager().isDeviceLocked()).isEqualTo(expectedLockStatus);
175     }
176 
177     /**
178      * Goes to the Locked screen page
179      *
180      * @deprecated use Root.goToLockscreen() to improve validation b/322870306
181      */
goToLockScreen()182     public static void goToLockScreen() {
183         try {
184             getUiDevice().sleep();
185             sleep(SHORT_WAIT_TIME_IN_SECONDS * 1000);
186             getUiDevice().wakeUp();
187         } catch (RemoteException e) {
188             throw new RuntimeException(e);
189         }
190     }
191 
192     /** Ensures that the lockscreen is visible. */
ensureLockscreen()193     public static void ensureLockscreen() {
194         HomeLockscreenPage page = new HomeLockscreenPage();
195         HealthTestingUtils.waitForCondition(() -> "Lock screen is not visible", page::isVisible);
196     }
197 
198     /** Ensures that the lockscreen is not visible. */
ensureNoLockscreen()199     public static void ensureNoLockscreen() {
200         HomeLockscreenPage page = new HomeLockscreenPage();
201         ensureThat("Lock screen is invisible", () -> !page.isVisible());
202     }
203 
204     /**
205      * Dismisses the lock screen, by swiping up, if it's visible.
206      * The device shouldn't have a password set.
207      */
dismissLockScreen()208     public static void dismissLockScreen() {
209         checkDeviceLock(false /* expectedLockStatus */);
210 
211         HomeLockscreenPage page = new HomeLockscreenPage();
212         if (page.isVisible()) {
213             page.swipeUp();
214         }
215     }
216 
ensureAoD(boolean enabled)217     public static void ensureAoD(boolean enabled) {
218         final ContentResolver contentResolver =
219                 InstrumentationRegistry.getInstrumentation().getContext().getContentResolver();
220         sPreviousAodSetting = Settings.Secure.getInt(
221                 contentResolver, Settings.Secure.DOZE_ALWAYS_ON, 0);
222         final boolean isAodEnabled = sPreviousAodSetting != 0;
223         if (isAodEnabled != enabled) {
224             Settings.Secure.putInt(
225                     contentResolver, Settings.Secure.DOZE_ALWAYS_ON, enabled ? 1 : 0);
226         }
227     }
228 
recoverAoD()229     public static void recoverAoD() {
230         final ContentResolver contentResolver =
231                 InstrumentationRegistry.getInstrumentation().getContext().getContentResolver();
232         Settings.Secure.putInt(
233                 contentResolver, Settings.Secure.DOZE_ALWAYS_ON, sPreviousAodSetting);
234     }
235 
236     /**
237      * Enum for different types of Lockscreen, PIN, PATTERN and PASSWORD.
238      */
239     public enum LockscreenType {
240         PIN,
241         PASSWORD,
242         PATTERN,
243         SWIPE,
244         NONE
245     }
246 }
247