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.util;
17 
18 import java.util.List;
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21 
22 /** A container for the keyguard states. Inspired from ActivityManagerState.java. */
23 public class KeyguardControllerState {
24     private static final Pattern NAME_PATTERN = Pattern.compile(".*KeyguardController:");
25     private static final Pattern SHOWING_PATTERN = Pattern.compile("mKeyguardShowing=(\\S+)");
26     private static final Pattern OCCLUDED_PATTERN = Pattern.compile("mOccluded=(\\S+)");
27     private static final Pattern GOING_AWAY_PATTERN = Pattern.compile("mKeyguardGoingAway=(\\S+)");
28 
29     private boolean mKeyguardShowing;
30     private boolean mKeyguardOccluded;
31     private boolean mKeyguardGoingAway;
32 
KeyguardControllerState()33     private KeyguardControllerState() {}
34 
35     /**
36      * Creates and populate a {@link KeyguardControllerState} based on a dumpsys output from the
37      * KeyguardController.
38      *
39      * @param dump output from the dumpsys activity activities
40      * @return The {@link KeyguardControllerState} representing the output or null if invalid output
41      *     is provided.
42      */
create(List<String> dump)43     public static KeyguardControllerState create(List<String> dump) {
44         final String line = dump.get(0);
45 
46         final Matcher matcher = NAME_PATTERN.matcher(line);
47         if (!matcher.matches()) {
48             // Not KeyguardController
49             return null;
50         }
51 
52         final KeyguardControllerState controller = new KeyguardControllerState();
53         controller.extract(dump);
54         return controller;
55     }
56 
extract(List<String> dump)57     private void extract(List<String> dump) {
58         for (String log : dump) {
59             String line = log.trim();
60             Matcher matcher = SHOWING_PATTERN.matcher(line);
61             if (matcher.matches()) {
62                 final String showingString = matcher.group(1);
63                 mKeyguardShowing = Boolean.valueOf(showingString);
64                 continue;
65             }
66 
67             matcher = OCCLUDED_PATTERN.matcher(line);
68             if (matcher.matches()) {
69                 final String occludedString = matcher.group(1);
70                 mKeyguardOccluded = Boolean.valueOf(occludedString);
71                 continue;
72             }
73 
74             matcher = GOING_AWAY_PATTERN.matcher(line);
75             if (matcher.matches()) {
76                 final String goingAwayString = matcher.group(1);
77                 mKeyguardGoingAway = Boolean.valueOf(goingAwayString);
78                 continue;
79             }
80         }
81     }
82 
83     /** Returns True if the keyguard is showing, false otherwise. */
isKeyguardShowing()84     public boolean isKeyguardShowing() {
85         return mKeyguardShowing;
86     }
87 
88     /** Returns True if the keyguard is occluded, false otherwise. */
isKeyguardOccluded()89     public boolean isKeyguardOccluded() {
90         return mKeyguardOccluded;
91     }
92 
93     /** Returns True if the keyguard is going away, false otherwise. */
isKeyguardGoingAway()94     public boolean isKeyguardGoingAway() {
95         return mKeyguardGoingAway;
96     }
97 }
98