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.os; 18 19 import android.annotation.IntDef; 20 import android.annotation.NonNull; 21 import android.util.IntArray; 22 23 import java.lang.annotation.Retention; 24 import java.lang.annotation.RetentionPolicy; 25 26 /** 27 * Query parameters for the {@link BatteryStatsManager#getBatteryUsageStats()} call. 28 * 29 * @hide 30 */ 31 @android.ravenwood.annotation.RavenwoodKeepWholeClass 32 public final class BatteryUsageStatsQuery implements Parcelable { 33 34 @NonNull 35 public static final BatteryUsageStatsQuery DEFAULT = 36 new BatteryUsageStatsQuery.Builder().build(); 37 38 /** 39 * Flags for the {@link BatteryStatsManager#getBatteryUsageStats()} method. 40 * @hide 41 */ 42 @IntDef(flag = true, prefix = { "FLAG_BATTERY_USAGE_STATS_" }, value = { 43 FLAG_BATTERY_USAGE_STATS_POWER_PROFILE_MODEL, 44 FLAG_BATTERY_USAGE_STATS_INCLUDE_HISTORY, 45 FLAG_BATTERY_USAGE_STATS_INCLUDE_PROCESS_STATE_DATA, 46 FLAG_BATTERY_USAGE_STATS_INCLUDE_VIRTUAL_UIDS, 47 }) 48 @Retention(RetentionPolicy.SOURCE) 49 public @interface BatteryUsageStatsFlags {} 50 51 /** 52 * Indicates that power estimations should be based on the usage time and 53 * average power constants provided in the PowerProfile, even if on-device power monitoring 54 * is available. 55 * 56 * @hide 57 */ 58 public static final int FLAG_BATTERY_USAGE_STATS_POWER_PROFILE_MODEL = 0x0001; 59 60 /** 61 * Indicates that battery history should be included in the BatteryUsageStats. 62 * @hide 63 */ 64 public static final int FLAG_BATTERY_USAGE_STATS_INCLUDE_HISTORY = 0x0002; 65 66 /** 67 * Indicates that identifiers of power models used for computations of power 68 * consumption should be included in the BatteryUsageStats. 69 */ 70 public static final int FLAG_BATTERY_USAGE_STATS_INCLUDE_POWER_MODELS = 0x0004; 71 72 public static final int FLAG_BATTERY_USAGE_STATS_INCLUDE_PROCESS_STATE_DATA = 0x0008; 73 74 public static final int FLAG_BATTERY_USAGE_STATS_INCLUDE_VIRTUAL_UIDS = 0x0010; 75 76 private static final long DEFAULT_MAX_STATS_AGE_MS = 5 * 60 * 1000; 77 78 private final int mFlags; 79 @NonNull 80 private final int[] mUserIds; 81 private final long mMaxStatsAgeMs; 82 private final long mFromTimestamp; 83 private final long mToTimestamp; 84 private final double mMinConsumedPowerThreshold; 85 private final @BatteryConsumer.PowerComponent int[] mPowerComponents; 86 BatteryUsageStatsQuery(@onNull Builder builder)87 private BatteryUsageStatsQuery(@NonNull Builder builder) { 88 mFlags = builder.mFlags; 89 mUserIds = builder.mUserIds != null ? builder.mUserIds.toArray() 90 : new int[]{UserHandle.USER_ALL}; 91 mMaxStatsAgeMs = builder.mMaxStatsAgeMs; 92 mMinConsumedPowerThreshold = builder.mMinConsumedPowerThreshold; 93 mFromTimestamp = builder.mFromTimestamp; 94 mToTimestamp = builder.mToTimestamp; 95 mPowerComponents = builder.mPowerComponents; 96 } 97 98 @BatteryUsageStatsFlags getFlags()99 public int getFlags() { 100 return mFlags; 101 } 102 103 /** 104 * Returns an array of users for which the attribution is requested. It may 105 * contain {@link UserHandle#USER_ALL} to indicate that the attribution 106 * should be performed for all users. Battery consumed by users <b>not</b> included 107 * in this array will be returned in the aggregated form as {@link UserBatteryConsumer}'s. 108 */ 109 @NonNull getUserIds()110 public int[] getUserIds() { 111 return mUserIds; 112 } 113 114 /** 115 * Returns true if the power calculations must be based on the PowerProfile constants, 116 * even if measured energy data is available. 117 */ shouldForceUsePowerProfileModel()118 public boolean shouldForceUsePowerProfileModel() { 119 return (mFlags & FLAG_BATTERY_USAGE_STATS_POWER_PROFILE_MODEL) != 0; 120 } 121 isProcessStateDataNeeded()122 public boolean isProcessStateDataNeeded() { 123 return (mFlags & FLAG_BATTERY_USAGE_STATS_INCLUDE_PROCESS_STATE_DATA) != 0; 124 } 125 126 /** 127 * Returns the power components that should be estimated or null if all power components 128 * are being requested. 129 */ getPowerComponents()130 public int[] getPowerComponents() { 131 return mPowerComponents; 132 } 133 134 /** 135 * Returns the client's tolerance for stale battery stats. The data is allowed to be up to 136 * this many milliseconds out-of-date. 137 */ getMaxStatsAge()138 public long getMaxStatsAge() { 139 return mMaxStatsAgeMs; 140 } 141 142 /** 143 * Returns the minimal power component consumed power threshold. The small power consuming 144 * components will be reported as zero. 145 */ getMinConsumedPowerThreshold()146 public double getMinConsumedPowerThreshold() { 147 return mMinConsumedPowerThreshold; 148 } 149 150 /** 151 * Returns the exclusive lower bound of the stored snapshot timestamps that should be included 152 * in the aggregation. Ignored if {@link #getToTimestamp()} is zero. 153 */ getFromTimestamp()154 public long getFromTimestamp() { 155 return mFromTimestamp; 156 } 157 158 /** 159 * Returns the inclusive upper bound of the stored snapshot timestamps that should 160 * be included in the aggregation. The default is to include only the current stats 161 * accumulated since the latest battery reset. 162 */ getToTimestamp()163 public long getToTimestamp() { 164 return mToTimestamp; 165 } 166 BatteryUsageStatsQuery(Parcel in)167 private BatteryUsageStatsQuery(Parcel in) { 168 mFlags = in.readInt(); 169 mUserIds = new int[in.readInt()]; 170 in.readIntArray(mUserIds); 171 mMaxStatsAgeMs = in.readLong(); 172 mMinConsumedPowerThreshold = in.readDouble(); 173 mFromTimestamp = in.readLong(); 174 mToTimestamp = in.readLong(); 175 mPowerComponents = in.createIntArray(); 176 } 177 178 @Override writeToParcel(Parcel dest, int flags)179 public void writeToParcel(Parcel dest, int flags) { 180 dest.writeInt(mFlags); 181 dest.writeInt(mUserIds.length); 182 dest.writeIntArray(mUserIds); 183 dest.writeLong(mMaxStatsAgeMs); 184 dest.writeDouble(mMinConsumedPowerThreshold); 185 dest.writeLong(mFromTimestamp); 186 dest.writeLong(mToTimestamp); 187 dest.writeIntArray(mPowerComponents); 188 } 189 190 @Override describeContents()191 public int describeContents() { 192 return 0; 193 } 194 195 @NonNull 196 public static final Creator<BatteryUsageStatsQuery> CREATOR = 197 new Creator<BatteryUsageStatsQuery>() { 198 @Override 199 public BatteryUsageStatsQuery createFromParcel(Parcel in) { 200 return new BatteryUsageStatsQuery(in); 201 } 202 203 @Override 204 public BatteryUsageStatsQuery[] newArray(int size) { 205 return new BatteryUsageStatsQuery[size]; 206 } 207 }; 208 209 /** 210 * Builder for BatteryUsageStatsQuery. 211 */ 212 public static final class Builder { 213 private int mFlags; 214 private IntArray mUserIds; 215 private long mMaxStatsAgeMs = DEFAULT_MAX_STATS_AGE_MS; 216 private long mFromTimestamp; 217 private long mToTimestamp; 218 private double mMinConsumedPowerThreshold = 0; 219 private @BatteryConsumer.PowerComponent int[] mPowerComponents; 220 221 /** 222 * Builds a read-only BatteryUsageStatsQuery object. 223 */ build()224 public BatteryUsageStatsQuery build() { 225 return new BatteryUsageStatsQuery(this); 226 } 227 228 /** 229 * Add a user whose battery stats should be included in the battery usage stats. 230 * {@link UserHandle#USER_ALL} will be used by default if no users are added explicitly. 231 */ addUser(@onNull UserHandle userHandle)232 public Builder addUser(@NonNull UserHandle userHandle) { 233 if (mUserIds == null) { 234 mUserIds = new IntArray(1); 235 } 236 mUserIds.add(userHandle.getIdentifier()); 237 return this; 238 } 239 240 /** 241 * Requests that battery history be included in the BatteryUsageStats. 242 */ includeBatteryHistory()243 public Builder includeBatteryHistory() { 244 mFlags |= BatteryUsageStatsQuery.FLAG_BATTERY_USAGE_STATS_INCLUDE_HISTORY; 245 return this; 246 } 247 248 /** 249 * Requests that per-process state data be included in the BatteryUsageStats, if 250 * available. Check {@link BatteryUsageStats#isProcessStateDataIncluded()} on the result 251 * to see if the data is available. 252 */ includeProcessStateData()253 public Builder includeProcessStateData() { 254 mFlags |= BatteryUsageStatsQuery.FLAG_BATTERY_USAGE_STATS_INCLUDE_PROCESS_STATE_DATA; 255 return this; 256 } 257 258 /** 259 * Requests to return modeled battery usage stats only, even if on-device 260 * power monitoring data is available. 261 * 262 * Should only be used for testing and debugging. 263 */ powerProfileModeledOnly()264 public Builder powerProfileModeledOnly() { 265 mFlags |= BatteryUsageStatsQuery.FLAG_BATTERY_USAGE_STATS_POWER_PROFILE_MODEL; 266 return this; 267 } 268 269 /** 270 * Requests to return identifiers of models that were used for estimation 271 * of power consumption. 272 * 273 * Should only be used for testing and debugging. 274 */ includePowerModels()275 public Builder includePowerModels() { 276 mFlags |= BatteryUsageStatsQuery.FLAG_BATTERY_USAGE_STATS_INCLUDE_POWER_MODELS; 277 return this; 278 } 279 280 /** 281 * Requests to return only statistics for the specified power components. The default 282 * is all power components. 283 */ includePowerComponents( @atteryConsumer.PowerComponent int[] powerComponents)284 public Builder includePowerComponents( 285 @BatteryConsumer.PowerComponent int[] powerComponents) { 286 mPowerComponents = powerComponents; 287 return this; 288 } 289 290 /** 291 * Requests to return attribution data for virtual UIDs such as 292 * {@link Process#SDK_SANDBOX_VIRTUAL_UID}. 293 */ includeVirtualUids()294 public Builder includeVirtualUids() { 295 mFlags |= BatteryUsageStatsQuery.FLAG_BATTERY_USAGE_STATS_INCLUDE_VIRTUAL_UIDS; 296 return this; 297 } 298 299 /** 300 * Requests to aggregate stored snapshots between the two supplied timestamps 301 * @param fromTimestamp Exclusive starting timestamp, as per System.currentTimeMillis() 302 * @param toTimestamp Inclusive ending timestamp, as per System.currentTimeMillis() 303 */ 304 // TODO(b/298459065): switch to monotonic clock aggregateSnapshots(long fromTimestamp, long toTimestamp)305 public Builder aggregateSnapshots(long fromTimestamp, long toTimestamp) { 306 mFromTimestamp = fromTimestamp; 307 mToTimestamp = toTimestamp; 308 return this; 309 } 310 311 /** 312 * Set the client's tolerance for stale battery stats. The data may be up to 313 * this many milliseconds out-of-date. 314 */ setMaxStatsAgeMs(long maxStatsAgeMs)315 public Builder setMaxStatsAgeMs(long maxStatsAgeMs) { 316 mMaxStatsAgeMs = maxStatsAgeMs; 317 return this; 318 } 319 320 /** 321 * Set the minimal power component consumed power threshold. The small power consuming 322 * components will be reported as zero. 323 */ setMinConsumedPowerThreshold(double minConsumedPowerThreshold)324 public Builder setMinConsumedPowerThreshold(double minConsumedPowerThreshold) { 325 mMinConsumedPowerThreshold = minConsumedPowerThreshold; 326 return this; 327 } 328 } 329 } 330