1 /*
2  * Copyright (C) 2020 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.appsecurity.cts;
18 
19 import com.android.tradefed.util.RunUtil;
20 import static org.junit.Assert.fail;
21 
22 import com.android.server.wm.KeyguardServiceDelegateProto;
23 import com.android.server.wm.WindowManagerServiceDumpProto;
24 import com.android.tradefed.device.CollectingByteOutputReceiver;
25 import com.android.tradefed.device.ITestDevice;
26 import com.android.tradefed.log.LogUtil.CLog;
27 
28 import com.google.protobuf.MessageLite;
29 import com.google.protobuf.Parser;
30 
31 class LockScreenInspector {
32     private static final String DUMPSYS_WINDOW = "dumpsys window --proto";
33 
34     private Boolean mKeyguardShowingAndNotOccluded;
35     private final ITestDevice mDevice;
36 
LockScreenInspector(ITestDevice device)37     private LockScreenInspector(ITestDevice device) {
38         mDevice = device;
39     }
40 
newInstance(ITestDevice device)41     static LockScreenInspector newInstance(ITestDevice device) {
42         LockScreenInspector inspector = new LockScreenInspector(device);
43         inspector.checkCurrentState();
44         return inspector;
45     }
46 
isDisplayedAndNotOccluded()47     boolean isDisplayedAndNotOccluded() {
48         return mKeyguardShowingAndNotOccluded;
49     }
50 
checkCurrentState()51     private void checkCurrentState() {
52         int retriesLeft = 3;
53         boolean retry = false;
54 
55         do {
56             if (retry) {
57                 CLog.w("***Retrying dump****");
58                 RunUtil.getDefault().sleep(500);
59             }
60             try {
61                 WindowManagerServiceDumpProto dumpProto = getDump(
62                         WindowManagerServiceDumpProto.parser(), DUMPSYS_WINDOW);
63                 KeyguardServiceDelegateProto keyguardProto =
64                         dumpProto.getPolicy().getKeyguardDelegate();
65                 mKeyguardShowingAndNotOccluded = keyguardProto.getShowing()
66                         && !keyguardProto.getOccluded();
67                 retry = false;
68             } catch (Exception e) {
69                 CLog.w(e);
70                 retry = true;
71             }
72         } while (retry && retriesLeft-- > 0);
73 
74         if (retry) {
75             fail("Could not obtain lockscreen state");
76         }
77     }
78 
getDump(Parser<T> parser, String command)79     private <T extends MessageLite> T getDump(Parser<T> parser, String command) throws Exception {
80         final CollectingByteOutputReceiver receiver = new CollectingByteOutputReceiver();
81         mDevice.executeShellCommand(command, receiver);
82         return parser.parseFrom(receiver.getOutput());
83     }
84 }
85