2 * Copyright (C) 2014 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
17 package android.app.usage;
19 import android.os.Parcel;
20 import android.os.Parcelable;
23 * Contains usage statistics for an app package for a specific
26 public final class UsageStats implements Parcelable {
31 public String mPackageName;
36 public long mBeginTimeStamp;
41 public long mEndTimeStamp;
44 * Last time used by the user with an explicit action (notification, activity launch).
47 public long mLastTimeUsed;
50 * The last time the package was used via implicit, non-user initiated actions (service
54 public long mLastTimeSystemUsed;
57 * Last time the package was used and the beginning of the idle countdown.
58 * This uses a different timebase that is about how much the device has been in use in general.
61 public long mBeginIdleTime;
66 public long mTotalTimeInForeground;
71 public int mLaunchCount;
76 public int mLastEvent;
84 public UsageStats(UsageStats stats) {
85 mPackageName = stats.mPackageName;
86 mBeginTimeStamp = stats.mBeginTimeStamp;
87 mEndTimeStamp = stats.mEndTimeStamp;
88 mLastTimeUsed = stats.mLastTimeUsed;
89 mTotalTimeInForeground = stats.mTotalTimeInForeground;
90 mLaunchCount = stats.mLaunchCount;
91 mLastEvent = stats.mLastEvent;
92 mBeginIdleTime = stats.mBeginIdleTime;
93 mLastTimeSystemUsed = stats.mLastTimeSystemUsed;
96 public String getPackageName() {
101 * Get the beginning of the time range this {@link android.app.usage.UsageStats} represents,
102 * measured in milliseconds since the epoch.
104 * See {@link System#currentTimeMillis()}.
106 public long getFirstTimeStamp() {
107 return mBeginTimeStamp;
111 * Get the end of the time range this {@link android.app.usage.UsageStats} represents,
112 * measured in milliseconds since the epoch.
114 * See {@link System#currentTimeMillis()}.
116 public long getLastTimeStamp() {
117 return mEndTimeStamp;
121 * Get the last time this package was used, measured in milliseconds since the epoch.
123 * See {@link System#currentTimeMillis()}.
125 public long getLastTimeUsed() {
126 return mLastTimeUsed;
131 * Get the last time this package was used by the system (not the user). This can be different
132 * from {@link #getLastTimeUsed()} when the system binds to one of this package's services.
133 * See {@link System#currentTimeMillis()}.
135 public long getLastTimeSystemUsed() {
136 return mLastTimeSystemUsed;
141 * Get the last time this package was active, measured in milliseconds. This timestamp
142 * uses a timebase that represents how much the device was used and not wallclock time.
144 public long getBeginIdleTime() {
145 return mBeginIdleTime;
149 * Get the total time this package spent in the foreground, measured in milliseconds.
151 public long getTotalTimeInForeground() {
152 return mTotalTimeInForeground;
156 * Add the statistics from the right {@link UsageStats} to the left. The package name for
157 * both {@link UsageStats} objects must be the same.
158 * @param right The {@link UsageStats} object to merge into this one.
159 * @throws java.lang.IllegalArgumentException if the package names of the two
160 * {@link UsageStats} objects are different.
162 public void add(UsageStats right) {
163 if (!mPackageName.equals(right.mPackageName)) {
164 throw new IllegalArgumentException("Can't merge UsageStats for package '" +
165 mPackageName + "' with UsageStats for package '" + right.mPackageName + "'.");
168 if (right.mBeginTimeStamp > mBeginTimeStamp) {
169 // The incoming UsageStat begins after this one, so use its last time used fields
170 // as the source of truth.
171 // We use the mBeginTimeStamp due to a bug where UsageStats files can overlap with
172 // regards to their mEndTimeStamp.
173 mLastEvent = right.mLastEvent;
174 mLastTimeUsed = right.mLastTimeUsed;
175 mBeginIdleTime = right.mBeginIdleTime;
176 mLastTimeSystemUsed = right.mLastTimeSystemUsed;
178 mBeginTimeStamp = Math.min(mBeginTimeStamp, right.mBeginTimeStamp);
179 mEndTimeStamp = Math.max(mEndTimeStamp, right.mEndTimeStamp);
180 mTotalTimeInForeground += right.mTotalTimeInForeground;
181 mLaunchCount += right.mLaunchCount;
185 public int describeContents() {
190 public void writeToParcel(Parcel dest, int flags) {
191 dest.writeString(mPackageName);
192 dest.writeLong(mBeginTimeStamp);
193 dest.writeLong(mEndTimeStamp);
194 dest.writeLong(mLastTimeUsed);
195 dest.writeLong(mTotalTimeInForeground);
196 dest.writeInt(mLaunchCount);
197 dest.writeInt(mLastEvent);
198 dest.writeLong(mBeginIdleTime);
199 dest.writeLong(mLastTimeSystemUsed);
202 public static final Creator<UsageStats> CREATOR = new Creator<UsageStats>() {
204 public UsageStats createFromParcel(Parcel in) {
205 UsageStats stats = new UsageStats();
206 stats.mPackageName = in.readString();
207 stats.mBeginTimeStamp = in.readLong();
208 stats.mEndTimeStamp = in.readLong();
209 stats.mLastTimeUsed = in.readLong();
210 stats.mTotalTimeInForeground = in.readLong();
211 stats.mLaunchCount = in.readInt();
212 stats.mLastEvent = in.readInt();
213 stats.mBeginIdleTime = in.readLong();
214 stats.mLastTimeSystemUsed = in.readLong();
219 public UsageStats[] newArray(int size) {
220 return new UsageStats[size];