1 /*
2  * Copyright (C) 2023 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 android.car.ui;
17 
18 import com.android.tradefed.log.LogUtil;
19 
20 import java.util.HashMap;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23 
24 class TaskInfo {
25     private static final String COMPONENT_PATTERN =
26             "([A-Za-z]{1}[A-Za-z\\d_]*\\.)+[A-Za-z][A-Za-z\\d_]*/([A-Za-z]{1}[A-Za-z\\d_]*\\.)"
27                     + "+[A-Za-z][A-Za-z\\d_]*";
28     private static final String TASK_BOUNDS_PATTERN = "bounds=\\[\\d+,\\d+\\]\\[\\d+,\\d+\\]";
29     private static final String TASK_USER_ID = "userId=\\d+";
30     private static final String TASK_VISIBILITY = "visible=[truefalse]+";
31     private static final String TASK_ID_GROUP_PATTERN = "taskId=\\d+:";
32     private static final String TASK_ID_PATTERN = "\\d+";
33     private static final String TASK_PATTERN =
34             TASK_ID_GROUP_PATTERN + " " + COMPONENT_PATTERN + " " + TASK_BOUNDS_PATTERN + " "
35                     + TASK_USER_ID + " " + TASK_VISIBILITY;
36 
37     private final int mTaskId;
38     private final boolean mVisible;
39     private final String mBaseActivity;
40 
TaskInfo(int taskId, String baseActivity, boolean visible)41     TaskInfo(int taskId, String baseActivity, boolean visible) {
42         mBaseActivity = baseActivity;
43         mTaskId = taskId;
44         mVisible = visible;
45     }
46 
unflattenFromString(String string)47     static TaskInfo unflattenFromString(String string) {
48         LogUtil.CLog.d("TaskInfo unflattenFromString %s", string);
49         return new TaskInfo(getTaskId(string), getComponentName(string), getVisibility(string));
50     }
51 
getComponentName(String string)52     private static String getComponentName(String string) {
53         Pattern taskPattern = Pattern.compile(COMPONENT_PATTERN, Pattern.CASE_INSENSITIVE);
54         Matcher matcher = taskPattern.matcher(string);
55         if (matcher.find()) {
56             return matcher.group();
57         }
58         return null;
59     }
60 
getVisibility(String string)61     private static boolean getVisibility(String string) {
62         Pattern taskPattern = Pattern.compile(TASK_VISIBILITY, Pattern.CASE_INSENSITIVE);
63         Matcher matcher = taskPattern.matcher(string);
64         if (matcher.find()) {
65             String result = matcher.group();
66             return result.endsWith("true");
67         }
68         return false;
69     }
70 
getTaskId(String string)71     private static int getTaskId(String string) {
72         Pattern taskIdGroupPattern = Pattern.compile(TASK_ID_GROUP_PATTERN);
73         Matcher matcher = taskIdGroupPattern.matcher(string);
74         if (matcher.find()) {
75             String idGroup = matcher.group();
76             taskIdGroupPattern = Pattern.compile(TASK_ID_PATTERN);
77             Matcher idMather = taskIdGroupPattern.matcher(idGroup);
78             if (idMather.find()) {
79                 String id = idMather.group();
80                 return Integer.parseInt(id);
81             }
82         }
83         return -1;
84     }
85 
getActiveComponents(String stackList)86     static HashMap<String, TaskInfo> getActiveComponents(String stackList) {
87         LogUtil.CLog.d("Try to parse result %s", stackList);
88         HashMap<String, TaskInfo> results = new HashMap<>();
89         Pattern taskPattern = Pattern.compile(TASK_PATTERN, Pattern.CASE_INSENSITIVE);
90         Matcher matcher = taskPattern.matcher(stackList);
91         while (matcher.find()) {
92             TaskInfo taskInfo = TaskInfo.unflattenFromString(matcher.group());
93             results.put(taskInfo.getBaseActivity(), taskInfo);
94             LogUtil.CLog.d("Find activity %s" + taskInfo.getBaseActivity());
95         }
96         return results;
97     }
98 
isVisible()99     boolean isVisible() {
100         return mVisible;
101     }
102 
getBaseActivity()103     String getBaseActivity() {
104         return mBaseActivity;
105     }
106 }
107