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