• 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.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 public final class BatteryUsageStatsQuery implements Parcelable {
32 
33     @NonNull
34     public static final BatteryUsageStatsQuery DEFAULT =
35             new BatteryUsageStatsQuery.Builder().build();
36 
37     /**
38      * Flags for the {@link BatteryStatsManager#getBatteryUsageStats()} method.
39      * @hide
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     })
45     @Retention(RetentionPolicy.SOURCE)
46     public @interface BatteryUsageStatsFlags {}
47 
48     /**
49      * Indicates that power estimations should be based on the usage time and
50      * average power constants provided in the PowerProfile, even if on-device power monitoring
51      * is available.
52      *
53      * @hide
54      */
55     public static final int FLAG_BATTERY_USAGE_STATS_POWER_PROFILE_MODEL = 1;
56 
57     /**
58      * Indicates that battery history should be included in the BatteryUsageStats.
59      * @hide
60      */
61     public static final int FLAG_BATTERY_USAGE_STATS_INCLUDE_HISTORY = 2;
62 
63     /**
64      * Indicates that identifiers of power models used for computations of power
65      * consumption should be included in the BatteryUsageStats.
66      */
67     public static final int FLAG_BATTERY_USAGE_STATS_INCLUDE_POWER_MODELS = 4;
68 
69     private static final long DEFAULT_MAX_STATS_AGE_MS = 5 * 60 * 1000;
70 
71     private final int mFlags;
72     @NonNull
73     private final int[] mUserIds;
74     private final long mMaxStatsAgeMs;
75     private long mFromTimestamp;
76     private long mToTimestamp;
77 
BatteryUsageStatsQuery(@onNull Builder builder)78     private BatteryUsageStatsQuery(@NonNull Builder builder) {
79         mFlags = builder.mFlags;
80         mUserIds = builder.mUserIds != null ? builder.mUserIds.toArray()
81                 : new int[]{UserHandle.USER_ALL};
82         mMaxStatsAgeMs = builder.mMaxStatsAgeMs;
83         mFromTimestamp = builder.mFromTimestamp;
84         mToTimestamp = builder.mToTimestamp;
85     }
86 
87     @BatteryUsageStatsFlags
getFlags()88     public int getFlags() {
89         return mFlags;
90     }
91 
92     /**
93      * Returns an array of users for which the attribution is requested.  It may
94      * contain {@link UserHandle#USER_ALL} to indicate that the attribution
95      * should be performed for all users. Battery consumed by users <b>not</b> included
96      * in this array will be returned in the aggregated form as {@link UserBatteryConsumer}'s.
97      */
98     @NonNull
getUserIds()99     public int[] getUserIds() {
100         return mUserIds;
101     }
102 
103     /**
104      * Returns true if the power calculations must be based on the PowerProfile constants,
105      * even if measured energy data is available.
106      */
shouldForceUsePowerProfileModel()107     public boolean shouldForceUsePowerProfileModel() {
108         return (mFlags & FLAG_BATTERY_USAGE_STATS_POWER_PROFILE_MODEL) != 0;
109     }
110 
111     /**
112      * Returns the client's tolerance for stale battery stats. The data is allowed to be up to
113      * this many milliseconds out-of-date.
114      */
getMaxStatsAge()115     public long getMaxStatsAge() {
116         return mMaxStatsAgeMs;
117     }
118 
119     /**
120      * Returns the exclusive lower bound of the stored snapshot timestamps that should be included
121      * in the aggregation.  Ignored if {@link #getToTimestamp()} is zero.
122      */
getFromTimestamp()123     public long getFromTimestamp() {
124         return mFromTimestamp;
125     }
126 
127     /**
128      * Returns the inclusive upper bound of the stored snapshot timestamps that should
129      * be included in the aggregation.  The default is to include only the current stats
130      * accumulated since the latest battery reset.
131      */
getToTimestamp()132     public long getToTimestamp() {
133         return mToTimestamp;
134     }
135 
BatteryUsageStatsQuery(Parcel in)136     private BatteryUsageStatsQuery(Parcel in) {
137         mFlags = in.readInt();
138         mUserIds = new int[in.readInt()];
139         in.readIntArray(mUserIds);
140         mMaxStatsAgeMs = in.readLong();
141         mFromTimestamp = in.readLong();
142         mToTimestamp = in.readLong();
143     }
144 
145     @Override
writeToParcel(Parcel dest, int flags)146     public void writeToParcel(Parcel dest, int flags) {
147         dest.writeInt(mFlags);
148         dest.writeInt(mUserIds.length);
149         dest.writeIntArray(mUserIds);
150         dest.writeLong(mMaxStatsAgeMs);
151         dest.writeLong(mFromTimestamp);
152         dest.writeLong(mToTimestamp);
153     }
154 
155     @Override
describeContents()156     public int describeContents() {
157         return 0;
158     }
159 
160     @NonNull
161     public static final Creator<BatteryUsageStatsQuery> CREATOR =
162             new Creator<BatteryUsageStatsQuery>() {
163                 @Override
164                 public BatteryUsageStatsQuery createFromParcel(Parcel in) {
165                     return new BatteryUsageStatsQuery(in);
166                 }
167 
168                 @Override
169                 public BatteryUsageStatsQuery[] newArray(int size) {
170                     return new BatteryUsageStatsQuery[size];
171                 }
172             };
173 
174     /**
175      * Builder for BatteryUsageStatsQuery.
176      */
177     public static final class Builder {
178         private int mFlags;
179         private IntArray mUserIds;
180         private long mMaxStatsAgeMs = DEFAULT_MAX_STATS_AGE_MS;
181         private long mFromTimestamp;
182         private long mToTimestamp;
183 
184         /**
185          * Builds a read-only BatteryUsageStatsQuery object.
186          */
build()187         public BatteryUsageStatsQuery build() {
188             return new BatteryUsageStatsQuery(this);
189         }
190 
191         /**
192          * Add a user whose battery stats should be included in the battery usage stats.
193          * {@link UserHandle#USER_ALL} will be used by default if no users are added explicitly.
194          */
addUser(@onNull UserHandle userHandle)195         public Builder addUser(@NonNull UserHandle userHandle) {
196             if (mUserIds == null) {
197                 mUserIds = new IntArray(1);
198             }
199             mUserIds.add(userHandle.getIdentifier());
200             return this;
201         }
202 
203         /**
204          * Requests that battery history be included in the BatteryUsageStats.
205          */
includeBatteryHistory()206         public Builder includeBatteryHistory() {
207             mFlags |= BatteryUsageStatsQuery.FLAG_BATTERY_USAGE_STATS_INCLUDE_HISTORY;
208             return this;
209         }
210 
211         /**
212          * Requests to return modeled battery usage stats only, even if on-device
213          * power monitoring data is available.
214          *
215          * Should only be used for testing and debugging.
216          */
powerProfileModeledOnly()217         public Builder powerProfileModeledOnly() {
218             mFlags |= BatteryUsageStatsQuery.FLAG_BATTERY_USAGE_STATS_POWER_PROFILE_MODEL;
219             return this;
220         }
221 
222         /**
223          * Requests to return identifiers of models that were used for estimation
224          * of power consumption.
225          *
226          * Should only be used for testing and debugging.
227          */
includePowerModels()228         public Builder includePowerModels() {
229             mFlags |= BatteryUsageStatsQuery.FLAG_BATTERY_USAGE_STATS_INCLUDE_POWER_MODELS;
230             return this;
231         }
232 
233         /**
234          * Requests to aggregate stored snapshots between the two supplied timestamps
235          * @param fromTimestamp Exclusive starting timestamp, as per System.currentTimeMillis()
236          * @param toTimestamp Inclusive ending timestamp, as per System.currentTimeMillis()
237          */
aggregateSnapshots(long fromTimestamp, long toTimestamp)238         public Builder aggregateSnapshots(long fromTimestamp, long toTimestamp) {
239             mFromTimestamp = fromTimestamp;
240             mToTimestamp = toTimestamp;
241             return this;
242         }
243 
244         /**
245          * Set the client's tolerance for stale battery stats. The data may be up to
246          * this many milliseconds out-of-date.
247          */
setMaxStatsAgeMs(long maxStatsAgeMs)248         public Builder setMaxStatsAgeMs(long maxStatsAgeMs) {
249             mMaxStatsAgeMs = maxStatsAgeMs;
250             return this;
251         }
252     }
253 }
254