1 /* 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 */ 16 package com.android.tradefed.suite.checker; 17 18 import com.android.ddmlib.Log.LogLevel; 19 import com.android.tradefed.device.DeviceNotAvailableException; 20 import com.android.tradefed.device.ITestDevice; 21 import com.android.tradefed.log.LogUtil.CLog; 22 import com.android.tradefed.suite.checker.StatusCheckerResult.CheckStatus; 23 import com.android.tradefed.util.KeyguardControllerState; 24 25 /** Checks the keyguard status after module execution. */ 26 public class KeyguardStatusChecker implements ISystemStatusChecker { 27 28 private boolean mIsKeyguardShowing; 29 private boolean mIsKeyguardOccluded; 30 31 /** {@inheritDoc} */ 32 @Override postExecutionCheck(ITestDevice device)33 public StatusCheckerResult postExecutionCheck(ITestDevice device) 34 throws DeviceNotAvailableException { 35 StatusCheckerResult result = new StatusCheckerResult(CheckStatus.SUCCESS); 36 // We assume keyguard was dismissed before the module. 37 KeyguardControllerState ksc = device.getKeyguardState(); 38 if (ksc == null) { 39 // for compatibility 40 CLog.logAndDisplay( 41 LogLevel.DEBUG, 42 "KeyguardControllerState is not supported by the " 43 + "device. Skipping System Checker."); 44 return result; 45 } 46 mIsKeyguardShowing = ksc.isKeyguardShowing(); 47 mIsKeyguardOccluded = ksc.isKeyguardOccluded(); 48 if (mIsKeyguardShowing || mIsKeyguardOccluded) { 49 String message = 50 String.format( 51 "SystemChecker - post-execution:\n" + "\tKeyguard on: %s, occluded: %s", 52 mIsKeyguardShowing, mIsKeyguardOccluded); 53 CLog.logAndDisplay(LogLevel.WARN, message); 54 // 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(); 57 result.setStatus(CheckStatus.FAILED); 58 result.setErrorMessage(message); 59 return result; 60 } 61 return result; 62 } 63 } 64