• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.platform.spectatio.configs;
18 
19 import android.platform.spectatio.constants.JsonConfigConstants;
20 
21 import com.google.common.collect.Sets;
22 import com.google.gson.annotations.SerializedName;
23 
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.stream.Collectors;
28 
29 public class SpectatioConfig {
30     @SerializedName("COMMANDS")
31     private Map<String, String> mCommands = new HashMap<String, String>();
32 
33     @SerializedName("PACKAGES")
34     private Map<String, String> mPackages = new HashMap<String, String>();
35 
36     @SerializedName("ACTIONS")
37     private Map<String, String> mActions = new HashMap<String, String>();
38 
39     @SerializedName("UI_ELEMENTS")
40     private Map<String, UiElement> mUiElements = new HashMap<String, UiElement>();
41 
42     @SerializedName("WORKFLOWS")
43     private Map<String, List<WorkflowTask>> mWorkflows = new HashMap<String, List<WorkflowTask>>();
44 
getCommands()45     public Map<String, String> getCommands() {
46         return mCommands;
47     }
48 
getPackages()49     public Map<String, String> getPackages() {
50         return mPackages;
51     }
52 
getActions()53     public Map<String, String> getActions() {
54         return mActions;
55     }
56 
getUiElements()57     public Map<String, UiElement> getUiElements() {
58         return mUiElements;
59     }
60 
getWorkflows()61     public Map<String, List<WorkflowTask>> getWorkflows() {
62         return mWorkflows;
63     }
64 
65     /**
66      * Update config values for runtime and gas jsons. ThrowErrorForNewKeys should be true for
67      * runtime jsons
68      */
updateConfig(SpectatioConfig newSpectatioConfig, boolean throwErrorForNewKeys)69     public void updateConfig(SpectatioConfig newSpectatioConfig, boolean throwErrorForNewKeys) {
70         // Update Config with values from Runtime/GAS Json Config
71         updateConfigValues(mCommands, newSpectatioConfig.getCommands(), throwErrorForNewKeys);
72 
73         // Update Config with values from Runtime/GAS Json Config
74         updateConfigValues(mPackages, newSpectatioConfig.getPackages(), throwErrorForNewKeys);
75 
76         // Update Config with values from Runtime/GAS Json Config
77         updateConfigValues(mActions, newSpectatioConfig.getActions(), throwErrorForNewKeys);
78 
79         // Update Config with values from Runtime/GAS Json Config
80         updateConfigValues(mUiElements, newSpectatioConfig.getUiElements(), throwErrorForNewKeys);
81 
82         // Update Config with values from Runtime/GAS Json Config
83         updateConfigValues(mWorkflows, newSpectatioConfig.getWorkflows(), throwErrorForNewKeys);
84     }
85 
updateConfigValues( Map<String, T> currentConfigMap, Map<String, T> newConfigMap, boolean throwErrorForNewKeys)86     private <T> void updateConfigValues(
87             Map<String, T> currentConfigMap,
88             Map<String, T> newConfigMap,
89             boolean throwErrorForNewKeys) {
90         if (currentConfigMap == null || newConfigMap == null) {
91             throw new RuntimeException(
92                     "Validate Spectatio Json Config as it does not allow null values.");
93         }
94         if (throwErrorForNewKeys) {
95             Sets.SetView<String> newKeys =
96                     Sets.difference(newConfigMap.keySet(), currentConfigMap.keySet());
97             if (!newKeys.isEmpty()) {
98                 throw new RuntimeException(
99                         String.format(
100                                 "Unknown keys in runtime config: %s.",
101                                 newKeys.stream().collect(Collectors.joining(", "))));
102             }
103         }
104         currentConfigMap.putAll(newConfigMap);
105     }
106 
getValueFromConfig(Map<String, T> config, String name, String type)107     private <T> T getValueFromConfig(Map<String, T> config, String name, String type) {
108         if (config == null || !config.containsKey(name) || config.get(name) == null) {
109             throw new RuntimeException(
110                     String.format(
111                             "Validate Spectatio JSON config as it does not have %s with key %s.",
112                             type, name));
113         }
114         return config.get(name);
115     }
116 
getActionFromConfig(String actionName)117     public String getActionFromConfig(String actionName) {
118         return getValueFromConfig(mActions, actionName, JsonConfigConstants.ACTIONS);
119     }
120 
getCommandFromConfig(String commandName)121     public String getCommandFromConfig(String commandName) {
122         return getValueFromConfig(mCommands, commandName, JsonConfigConstants.COMMANDS);
123     }
124 
getPackageFromConfig(String packageName)125     public String getPackageFromConfig(String packageName) {
126         return getValueFromConfig(mPackages, packageName, JsonConfigConstants.PACKAGES);
127     }
128 
129     /** Instantiate a UI element specifier from the config values at the specified key */
getUiElementFromConfig(String uiElementName)130     public UiElement getUiElementFromConfig(String uiElementName) {
131         return getValueFromConfig(mUiElements, uiElementName, JsonConfigConstants.UI_ELEMENTS);
132     }
133 
getWorkflowFromConfig(String workflowName)134     public List<WorkflowTask> getWorkflowFromConfig(String workflowName) {
135         return getValueFromConfig(mWorkflows, workflowName, JsonConfigConstants.WORKFLOWS);
136     }
137 }
138