blob: c53f33be0ae2dab651aca057b816a61948feaeba [file] [log] [blame]
Julien Desprez0e849332017-01-18 12:22:30 +00001/*
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 */
16package com.android.tradefed.suite.checker;
17
18import com.android.ddmlib.Log.LogLevel;
19import com.android.tradefed.device.DeviceNotAvailableException;
20import com.android.tradefed.device.ITestDevice;
21import com.android.tradefed.log.LogUtil.CLog;
Julien Desprez3e4d47d2018-05-23 15:53:40 -070022import com.android.tradefed.suite.checker.StatusCheckerResult.CheckStatus;
Julien Desprez0e849332017-01-18 12:22:30 +000023import com.android.tradefed.util.KeyguardControllerState;
24
Julien Desprez510a4832017-01-27 11:58:17 +000025/** Checks the keyguard status after module execution. */
Julien Desprez0e849332017-01-18 12:22:30 +000026public class KeyguardStatusChecker implements ISystemStatusChecker {
27
28 private boolean mIsKeyguardShowing;
29 private boolean mIsKeyguardOccluded;
30
31 /** {@inheritDoc} */
32 @Override
Julien Desprez3e4d47d2018-05-23 15:53:40 -070033 public StatusCheckerResult postExecutionCheck(ITestDevice device)
34 throws DeviceNotAvailableException {
35 StatusCheckerResult result = new StatusCheckerResult(CheckStatus.SUCCESS);
Julien Desprez0e849332017-01-18 12:22:30 +000036 // We assume keyguard was dismissed before the module.
37 KeyguardControllerState ksc = device.getKeyguardState();
Julien Desprez510a4832017-01-27 11:58:17 +000038 if (ksc == null) {
39 // for compatibility
40 CLog.logAndDisplay(
41 LogLevel.DEBUG,
42 "KeyguardControllerState is not supported by the "
43 + "device. Skipping System Checker.");
Julien Desprez3e4d47d2018-05-23 15:53:40 -070044 return result;
Julien Desprez510a4832017-01-27 11:58:17 +000045 }
Julien Desprez0e849332017-01-18 12:22:30 +000046 mIsKeyguardShowing = ksc.isKeyguardShowing();
47 mIsKeyguardOccluded = ksc.isKeyguardOccluded();
48 if (mIsKeyguardShowing || mIsKeyguardOccluded) {
Julien Desprez3e4d47d2018-05-23 15:53:40 -070049 String message =
50 String.format(
51 "SystemChecker - post-execution:\n" + "\tKeyguard on: %s, occluded: %s",
52 mIsKeyguardShowing, mIsKeyguardOccluded);
53 CLog.logAndDisplay(LogLevel.WARN, message);
Guang Zhu03c985e2017-04-28 14:53:55 -070054 // best effort to dismiss keyguard: will not work if a secured keyguard was left around
55 CLog.w("Also attempting to dismiss keyguard.");
56 device.disableKeyguard();
Julien Desprez3e4d47d2018-05-23 15:53:40 -070057 result.setStatus(CheckStatus.FAILED);
58 result.setErrorMessage(message);
59 return result;
Julien Desprez0e849332017-01-18 12:22:30 +000060 }
Julien Desprez3e4d47d2018-05-23 15:53:40 -070061 return result;
Julien Desprez0e849332017-01-18 12:22:30 +000062 }
63}