| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public final class BatteryUsageStatsQuery implements Parcelable { |
| 32 |
|
| 33 |
@NonNull |
| 34 |
public static final BatteryUsageStatsQuery DEFAULT = |
| 35 |
new BatteryUsageStatsQuery.Builder().build(); |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
@IntDef(flag = true, prefix = { "FLAG_BATTERY_USAGE_STATS_" }, value = { |
| 42 |
FLAG_BATTERY_USAGE_STATS_POWER_PROFILE_MODEL, |
| 43 |
FLAG_BATTERY_USAGE_STATS_INCLUDE_HISTORY, |
| 44 |
FLAG_BATTERY_USAGE_STATS_INCLUDE_PROCESS_STATE_DATA, |
| 45 |
FLAG_BATTERY_USAGE_STATS_INCLUDE_VIRTUAL_UIDS, |
| 46 |
}) |
| 47 |
@Retention(RetentionPolicy.SOURCE) |
| 48 |
public @interface BatteryUsageStatsFlags {} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public static final int FLAG_BATTERY_USAGE_STATS_POWER_PROFILE_MODEL = 0x0001; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public static final int FLAG_BATTERY_USAGE_STATS_INCLUDE_HISTORY = 0x0002; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public static final int FLAG_BATTERY_USAGE_STATS_INCLUDE_POWER_MODELS = 0x0004; |
| 70 |
|
| 71 |
public static final int FLAG_BATTERY_USAGE_STATS_INCLUDE_PROCESS_STATE_DATA = 0x0008; |
| 72 |
|
| 73 |
public static final int FLAG_BATTERY_USAGE_STATS_INCLUDE_VIRTUAL_UIDS = 0x0010; |
| 74 |
|
| 75 |
private static final long DEFAULT_MAX_STATS_AGE_MS = 5 * 60 * 1000; |
| 76 |
|
| 77 |
private final int mFlags; |
| 78 |
@NonNull |
| 79 |
private final int[] mUserIds; |
| 80 |
private final long mMaxStatsAgeMs; |
| 81 |
private final long mFromTimestamp; |
| 82 |
private final long mToTimestamp; |
| 83 |
private final @BatteryConsumer.PowerComponent int[] mPowerComponents; |
| 84 |
|
| 85 |
private BatteryUsageStatsQuery(@NonNull Builder builder) { |
| 86 |
mFlags = builder.mFlags; |
| 87 |
mUserIds = builder.mUserIds != null ? builder.mUserIds.toArray() |
| 88 |
: new int[]{UserHandle.USER_ALL}; |
| 89 |
mMaxStatsAgeMs = builder.mMaxStatsAgeMs; |
| 90 |
mFromTimestamp = builder.mFromTimestamp; |
| 91 |
mToTimestamp = builder.mToTimestamp; |
| 92 |
mPowerComponents = builder.mPowerComponents; |
| 93 |
} |
| 94 |
|
| 95 |
@BatteryUsageStatsFlags |
| 96 |
public int getFlags() { |
| 97 |
return mFlags; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
@NonNull |
| 107 |
public int[] getUserIds() { |
| 108 |
return mUserIds; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public boolean shouldForceUsePowerProfileModel() { |
| 116 |
return (mFlags & FLAG_BATTERY_USAGE_STATS_POWER_PROFILE_MODEL) != 0; |
| 117 |
} |
| 118 |
|
| 119 |
public boolean isProcessStateDataNeeded() { |
| 120 |
return (mFlags & FLAG_BATTERY_USAGE_STATS_INCLUDE_PROCESS_STATE_DATA) != 0; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public int[] getPowerComponents() { |
| 128 |
return mPowerComponents; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public long getMaxStatsAge() { |
| 136 |
return mMaxStatsAgeMs; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public long getFromTimestamp() { |
| 144 |
return mFromTimestamp; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public long getToTimestamp() { |
| 153 |
return mToTimestamp; |
| 154 |
} |
| 155 |
|
| 156 |
private BatteryUsageStatsQuery(Parcel in) { |
| 157 |
mFlags = in.readInt(); |
| 158 |
mUserIds = new int[in.readInt()]; |
| 159 |
in.readIntArray(mUserIds); |
| 160 |
mMaxStatsAgeMs = in.readLong(); |
| 161 |
mFromTimestamp = in.readLong(); |
| 162 |
mToTimestamp = in.readLong(); |
| 163 |
mPowerComponents = in.createIntArray(); |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public void writeToParcel(Parcel dest, int flags) { |
| 168 |
dest.writeInt(mFlags); |
| 169 |
dest.writeInt(mUserIds.length); |
| 170 |
dest.writeIntArray(mUserIds); |
| 171 |
dest.writeLong(mMaxStatsAgeMs); |
| 172 |
dest.writeLong(mFromTimestamp); |
| 173 |
dest.writeLong(mToTimestamp); |
| 174 |
dest.writeIntArray(mPowerComponents); |
| 175 |
} |
| 176 |
|
| 177 |
@Override |
| 178 |
public int describeContents() { |
| 179 |
return 0; |
| 180 |
} |
| 181 |
|
| 182 |
@NonNull |
| 183 |
public static final Creator<BatteryUsageStatsQuery> CREATOR = |
| 184 |
new Creator<BatteryUsageStatsQuery>() { |
| 185 |
@Override |
| 186 |
public BatteryUsageStatsQuery createFromParcel(Parcel in) { |
| 187 |
return new BatteryUsageStatsQuery(in); |
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 191 |
public BatteryUsageStatsQuery[] newArray(int size) { |
| 192 |
return new BatteryUsageStatsQuery[size]; |
| 193 |
} |
| 194 |
}; |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public static final class Builder { |
| 200 |
private int mFlags; |
| 201 |
private IntArray mUserIds; |
| 202 |
private long mMaxStatsAgeMs = DEFAULT_MAX_STATS_AGE_MS; |
| 203 |
private long mFromTimestamp; |
| 204 |
private long mToTimestamp; |
| 205 |
private @BatteryConsumer.PowerComponent int[] mPowerComponents; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public BatteryUsageStatsQuery build() { |
| 211 |
return new BatteryUsageStatsQuery(this); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public Builder addUser(@NonNull UserHandle userHandle) { |
| 219 |
if (mUserIds == null) { |
| 220 |
mUserIds = new IntArray(1); |
| 221 |
} |
| 222 |
mUserIds.add(userHandle.getIdentifier()); |
| 223 |
return this; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public Builder includeBatteryHistory() { |
| 230 |
mFlags |= BatteryUsageStatsQuery.FLAG_BATTERY_USAGE_STATS_INCLUDE_HISTORY; |
| 231 |
return this; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public Builder includeProcessStateData() { |
| 240 |
mFlags |= BatteryUsageStatsQuery.FLAG_BATTERY_USAGE_STATS_INCLUDE_PROCESS_STATE_DATA; |
| 241 |
return this; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public Builder powerProfileModeledOnly() { |
| 251 |
mFlags |= BatteryUsageStatsQuery.FLAG_BATTERY_USAGE_STATS_POWER_PROFILE_MODEL; |
| 252 |
return this; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public Builder includePowerModels() { |
| 262 |
mFlags |= BatteryUsageStatsQuery.FLAG_BATTERY_USAGE_STATS_INCLUDE_POWER_MODELS; |
| 263 |
return this; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public Builder includePowerComponents( |
| 271 |
@BatteryConsumer.PowerComponent int[] powerComponents) { |
| 272 |
mPowerComponents = powerComponents; |
| 273 |
return this; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
public Builder includeVirtualUids() { |
| 281 |
mFlags |= BatteryUsageStatsQuery.FLAG_BATTERY_USAGE_STATS_INCLUDE_VIRTUAL_UIDS; |
| 282 |
return this; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
public Builder aggregateSnapshots(long fromTimestamp, long toTimestamp) { |
| 291 |
mFromTimestamp = fromTimestamp; |
| 292 |
mToTimestamp = toTimestamp; |
| 293 |
return this; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
public Builder setMaxStatsAgeMs(long maxStatsAgeMs) { |
| 301 |
mMaxStatsAgeMs = maxStatsAgeMs; |
| 302 |
return this; |
| 303 |
} |
| 304 |
} |
| 305 |
} |