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 component, boolean prefix)146 public static int toPowerComponent(@Nullable String component, boolean prefix) { 147 if (component == null) { 148 return INVALID_POWER_COMPONENT; 149 } 150 if (prefix) { 151 if (!component.startsWith(POWER_COMPONENT_PREFIX)) { 152 return INVALID_POWER_COMPONENT; 153 } 154 component = component.substring(POWER_COMPONENT_PREFIX.length()); 155 } 156 switch (component) { 157 case POWER_COMPONENT_AUDIO: 158 return PowerComponent.AUDIO; 159 case POWER_COMPONENT_MEDIA: 160 return PowerComponent.MEDIA; 161 case POWER_COMPONENT_DISPLAY: 162 return PowerComponent.DISPLAY; 163 case POWER_COMPONENT_BLUETOOTH: 164 return PowerComponent.BLUETOOTH; 165 case POWER_COMPONENT_WIFI: 166 return PowerComponent.WIFI; 167 case POWER_COMPONENT_CELLULAR: 168 return PowerComponent.CELLULAR; 169 case POWER_COMPONENT_ETHERNET: 170 return PowerComponent.ETHERNET; 171 case POWER_COMPONENT_PROJECTION: 172 return PowerComponent.PROJECTION; 173 case POWER_COMPONENT_NFC: 174 return PowerComponent.NFC; 175 case POWER_COMPONENT_INPUT: 176 return PowerComponent.INPUT; 177 case POWER_COMPONENT_VOICE_INTERACTION: 178 return PowerComponent.VOICE_INTERACTION; 179 case POWER_COMPONENT_VISUAL_INTERACTION: 180 return PowerComponent.VISUAL_INTERACTION; 181 case POWER_COMPONENT_TRUSTED_DEVICE_DETECTION: 182 return PowerComponent.TRUSTED_DEVICE_DETECTION; 183 case POWER_COMPONENT_LOCATION: 184 return PowerComponent.LOCATION; 185 case POWER_COMPONENT_MICROPHONE: 186 return PowerComponent.MICROPHONE; 187 case POWER_COMPONENT_CPU: 188 return PowerComponent.CPU; 189 default: 190 return INVALID_POWER_COMPONENT; 191 } 192 } 193 194 /** 195 * Convert {@link PowerComponent} to string. 196 */ 197 @NonNull 198 @AddedInOrBefore(majorVersion = 33) powerComponentToString(int component)199 public static String powerComponentToString(int component) { 200 switch (component) { 201 case PowerComponent.AUDIO: 202 return POWER_COMPONENT_AUDIO; 203 case PowerComponent.MEDIA: 204 return POWER_COMPONENT_MEDIA; 205 case PowerComponent.DISPLAY: 206 return POWER_COMPONENT_DISPLAY; 207 case PowerComponent.BLUETOOTH: 208 return POWER_COMPONENT_BLUETOOTH; 209 case PowerComponent.WIFI: 210 return POWER_COMPONENT_WIFI; 211 case PowerComponent.CELLULAR: 212 return POWER_COMPONENT_CELLULAR; 213 case PowerComponent.ETHERNET: 214 return POWER_COMPONENT_ETHERNET; 215 case PowerComponent.PROJECTION: 216 return POWER_COMPONENT_PROJECTION; 217 case PowerComponent.NFC: 218 return POWER_COMPONENT_NFC; 219 case PowerComponent.INPUT: 220 return POWER_COMPONENT_INPUT; 221 case PowerComponent.VOICE_INTERACTION: 222 return POWER_COMPONENT_VOICE_INTERACTION; 223 case PowerComponent.VISUAL_INTERACTION: 224 return POWER_COMPONENT_VISUAL_INTERACTION; 225 case PowerComponent.TRUSTED_DEVICE_DETECTION: 226 return POWER_COMPONENT_TRUSTED_DEVICE_DETECTION; 227 case PowerComponent.LOCATION: 228 return POWER_COMPONENT_LOCATION; 229 case PowerComponent.MICROPHONE: 230 return POWER_COMPONENT_MICROPHONE; 231 case PowerComponent.CPU: 232 return POWER_COMPONENT_CPU; 233 default: 234 return "unknown component"; 235 } 236 } 237 } 238