• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.car.hardware.power;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.car.annotation.AddedInOrBefore;
23 import android.util.SparseBooleanArray;
24 
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 
28 /**
29  * Utility class used when dealing with PowerComponent.
30  *
31  * @hide
32  */
33 public final class PowerComponentUtil {
34     /**
35      * The component is marked as enabled in the power policy.
36      */
37     @AddedInOrBefore(majorVersion = 33)
38     public static final int COMPONENT_STATE_ENABLED = 1;
39 
40     /**
41      * The component is marked as disabled in the power policy.
42      */
43     @AddedInOrBefore(majorVersion = 33)
44     public static final int COMPONENT_STATE_DISABLED = 2;
45 
46     /**
47      * The component is not specified in the power policy.
48      */
49     @AddedInOrBefore(majorVersion = 33)
50     public static final int COMPONENT_STATE_UNTOUCHED = 3;
51 
52     @IntDef(prefix = { "COMPONENT_STATE_" }, value = {
53             COMPONENT_STATE_ENABLED,
54             COMPONENT_STATE_DISABLED,
55             COMPONENT_STATE_UNTOUCHED
56     })
57     @Retention(RetentionPolicy.SOURCE)
58     public @interface ComponentState { }
59 
60     /**
61      * Represetns an invalid power component.
62      */
63     @AddedInOrBefore(majorVersion = 33)
64     public static final int INVALID_POWER_COMPONENT = -1;
65 
66     /**
67      * The first component in {@link PowerComponent}.
68      */
69     @AddedInOrBefore(majorVersion = 33)
70     public static final int FIRST_POWER_COMPONENT = PowerComponent.AUDIO;
71 
72     /**
73      * The last component in {@link PowerComponent}.
74      *
75      * <p> This should be updated when a new component is added to {@link PowerComponent}.
76      */
77     @AddedInOrBefore(majorVersion = 33)
78     public static final int LAST_POWER_COMPONENT = PowerComponent.CPU;
79 
80     private static final String POWER_COMPONENT_PREFIX = "POWER_COMPONENT_";
81 
82     private static final String POWER_COMPONENT_AUDIO = "AUDIO";
83     private static final String POWER_COMPONENT_MEDIA = "MEDIA";
84     private static final String POWER_COMPONENT_DISPLAY = "DISPLAY";
85     private static final String POWER_COMPONENT_BLUETOOTH = "BLUETOOTH";
86     private static final String POWER_COMPONENT_WIFI = "WIFI";
87     private static final String POWER_COMPONENT_CELLULAR = "CELLULAR";
88     private static final String POWER_COMPONENT_ETHERNET = "ETHERNET";
89     private static final String POWER_COMPONENT_PROJECTION = "PROJECTION";
90     private static final String POWER_COMPONENT_NFC = "NFC";
91     private static final String POWER_COMPONENT_INPUT = "INPUT";
92     private static final String POWER_COMPONENT_VOICE_INTERACTION = "VOICE_INTERACTION";
93     private static final String POWER_COMPONENT_VISUAL_INTERACTION = "VISUAL_INTERACTION";
94     private static final String POWER_COMPONENT_TRUSTED_DEVICE_DETECTION =
95             "TRUSTED_DEVICE_DETECTION";
96     private static final String POWER_COMPONENT_LOCATION = "LOCATION";
97     private static final String POWER_COMPONENT_MICROPHONE = "MICROPHONE";
98     private static final String POWER_COMPONENT_CPU = "CPU";
99 
100     private interface ComponentFilter {
filter(int[] components)101         boolean filter(int[] components);
102     }
103 
104     // PowerComponentUtil is intended to provide static variables and methods.
PowerComponentUtil()105     private PowerComponentUtil() {}
106 
107     /**
108      * Checks whether the given component is valid.
109      */
110     @AddedInOrBefore(majorVersion = 33)
isValidPowerComponent(int component)111     public static boolean isValidPowerComponent(int component) {
112         return component >= FIRST_POWER_COMPONENT && component <= LAST_POWER_COMPONENT;
113     }
114 
115     /**
116      * Checks whether the given policy has one ore more components specified in the given filter.
117      */
118     @AddedInOrBefore(majorVersion = 33)
hasComponents(@onNull CarPowerPolicy policy, @NonNull CarPowerPolicyFilter filter)119     public static boolean hasComponents(@NonNull CarPowerPolicy policy,
120             @NonNull CarPowerPolicyFilter filter) {
121         SparseBooleanArray filterSet = new SparseBooleanArray();
122         int[] components = filter.getComponents();
123         for (int i = 0; i < components.length; i++) {
124             filterSet.put(components[i], true);
125         }
126 
127         ComponentFilter componentFilter = (c) -> {
128             for (int i = 0; i < c.length; i++) {
129                 if (filterSet.get(c[i])) {
130                     return true;
131                 }
132             }
133             return false;
134         };
135 
136         if (componentFilter.filter(policy.getEnabledComponents())) {
137             return true;
138         }
139         return componentFilter.filter(policy.getDisabledComponents());
140     }
141 
142     /**
143      * Matches the given string to {@link PowerComponent}.
144      */
145     @AddedInOrBefore(majorVersion = 33)
toPowerComponent(@ullable String componentArg, boolean prefix)146     public static int toPowerComponent(@Nullable String componentArg, boolean prefix) {
147         String component = componentArg;
148         if (component == null) {
149             return INVALID_POWER_COMPONENT;
150         }
151         if (prefix) {
152             if (!component.startsWith(POWER_COMPONENT_PREFIX)) {
153                 return INVALID_POWER_COMPONENT;
154             }
155             component = component.substring(POWER_COMPONENT_PREFIX.length());
156         }
157         switch (component) {
158             case POWER_COMPONENT_AUDIO:
159                 return PowerComponent.AUDIO;
160             case POWER_COMPONENT_MEDIA:
161                 return PowerComponent.MEDIA;
162             case POWER_COMPONENT_DISPLAY:
163                 return PowerComponent.DISPLAY;
164             case POWER_COMPONENT_BLUETOOTH:
165                 return PowerComponent.BLUETOOTH;
166             case POWER_COMPONENT_WIFI:
167                 return PowerComponent.WIFI;
168             case POWER_COMPONENT_CELLULAR:
169                 return PowerComponent.CELLULAR;
170             case POWER_COMPONENT_ETHERNET:
171                 return PowerComponent.ETHERNET;
172             case POWER_COMPONENT_PROJECTION:
173                 return PowerComponent.PROJECTION;
174             case POWER_COMPONENT_NFC:
175                 return PowerComponent.NFC;
176             case POWER_COMPONENT_INPUT:
177                 return PowerComponent.INPUT;
178             case POWER_COMPONENT_VOICE_INTERACTION:
179                 return PowerComponent.VOICE_INTERACTION;
180             case POWER_COMPONENT_VISUAL_INTERACTION:
181                 return PowerComponent.VISUAL_INTERACTION;
182             case POWER_COMPONENT_TRUSTED_DEVICE_DETECTION:
183                 return PowerComponent.TRUSTED_DEVICE_DETECTION;
184             case POWER_COMPONENT_LOCATION:
185                 return PowerComponent.LOCATION;
186             case POWER_COMPONENT_MICROPHONE:
187                 return PowerComponent.MICROPHONE;
188             case POWER_COMPONENT_CPU:
189                 return PowerComponent.CPU;
190             default:
191                 return INVALID_POWER_COMPONENT;
192         }
193     }
194 
195     /**
196      * Convert {@link PowerComponent} to string.
197      */
198     @NonNull
199     @AddedInOrBefore(majorVersion = 33)
powerComponentToString(int component)200     public static String powerComponentToString(int component) {
201         switch (component) {
202             case PowerComponent.AUDIO:
203                 return POWER_COMPONENT_AUDIO;
204             case PowerComponent.MEDIA:
205                 return POWER_COMPONENT_MEDIA;
206             case PowerComponent.DISPLAY:
207                 return POWER_COMPONENT_DISPLAY;
208             case PowerComponent.BLUETOOTH:
209                 return POWER_COMPONENT_BLUETOOTH;
210             case PowerComponent.WIFI:
211                 return POWER_COMPONENT_WIFI;
212             case PowerComponent.CELLULAR:
213                 return POWER_COMPONENT_CELLULAR;
214             case PowerComponent.ETHERNET:
215                 return POWER_COMPONENT_ETHERNET;
216             case PowerComponent.PROJECTION:
217                 return POWER_COMPONENT_PROJECTION;
218             case PowerComponent.NFC:
219                 return POWER_COMPONENT_NFC;
220             case PowerComponent.INPUT:
221                 return POWER_COMPONENT_INPUT;
222             case PowerComponent.VOICE_INTERACTION:
223                 return POWER_COMPONENT_VOICE_INTERACTION;
224             case PowerComponent.VISUAL_INTERACTION:
225                 return POWER_COMPONENT_VISUAL_INTERACTION;
226             case PowerComponent.TRUSTED_DEVICE_DETECTION:
227                 return POWER_COMPONENT_TRUSTED_DEVICE_DETECTION;
228             case PowerComponent.LOCATION:
229                 return POWER_COMPONENT_LOCATION;
230             case PowerComponent.MICROPHONE:
231                 return POWER_COMPONENT_MICROPHONE;
232             case PowerComponent.CPU:
233                 return POWER_COMPONENT_CPU;
234             default:
235                 if (component >= PowerComponent.MINIMUM_CUSTOM_COMPONENT_VALUE) {
236                     return Integer.toString(component);
237                 }
238                 return "unknown component";
239         }
240     }
241 }
242