Home | History | Annotate | Download | only in util
      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 com.android.tradefed.log.LogUtil.CLog;
     19 
     20 import java.util.List;
     21 import java.util.regex.Matcher;
     22 import java.util.regex.Pattern;
     23 
     24 /** A container for the keyguard states. Inspired from ActivityManagerState.java. */
     25 public class KeyguardControllerState {
     26     private static final Pattern NAME_PATTERN = Pattern.compile("KeyguardController:");
     27     private static final Pattern SHOWING_PATTERN = Pattern.compile("mKeyguardShowing=(\\S+)");
     28     private static final Pattern OCCLUDED_PATTERN = Pattern.compile("mOccluded=(\\S+)");
     29 
     30     private boolean mKeyguardShowing;
     31     private boolean mKeyguardOccluded;
     32 
     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      */
     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 
     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                 CLog.v(line);
     63                 final String showingString = matcher.group(1);
     64                 mKeyguardShowing = Boolean.valueOf(showingString);
     65                 CLog.v(showingString);
     66                 continue;
     67             }
     68 
     69             matcher = OCCLUDED_PATTERN.matcher(line);
     70             if (matcher.matches()) {
     71                 CLog.v(line);
     72                 final String occludedString = matcher.group(1);
     73                 mKeyguardOccluded = Boolean.valueOf(occludedString);
     74                 CLog.v(occludedString);
     75                 continue;
     76             }
     77         }
     78     }
     79 
     80     /** Returns True if the keyguard is showing, false otherwise. */
     81     public boolean isKeyguardShowing() {
     82         return mKeyguardShowing;
     83     }
     84 
     85     /** Returns True if the keyguard is occluded, false otherwise. */
     86     public boolean isKeyguardOccluded() {
     87         return mKeyguardOccluded;
     88     }
     89 }
     90