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.mEndTimeStamp > mEndTimeStamp) {
169 mLastEvent = right.mLastEvent;
170 mEndTimeStamp = right.mEndTimeStamp;
171 mLastTimeUsed = right.mLastTimeUsed;
172 mBeginIdleTime = right.mBeginIdleTime;
173 mLastTimeSystemUsed = right.mLastTimeSystemUsed;
175 mBeginTimeStamp = Math.min(mBeginTimeStamp, right.mBeginTimeStamp);
176 mTotalTimeInForeground += right.mTotalTimeInForeground;
177 mLaunchCount += right.mLaunchCount;
181 public int describeContents() {
186 public void writeToParcel(Parcel dest, int flags) {
187 dest.writeString(mPackageName);
188 dest.writeLong(mBeginTimeStamp);
189 dest.writeLong(mEndTimeStamp);
190 dest.writeLong(mLastTimeUsed);
191 dest.writeLong(mTotalTimeInForeground);
192 dest.writeInt(mLaunchCount);
193 dest.writeInt(mLastEvent);
194 dest.writeLong(mBeginIdleTime);
195 dest.writeLong(mLastTimeSystemUsed);
198 public static final Creator<UsageStats> CREATOR = new Creator<UsageStats>() {
200 public UsageStats createFromParcel(Parcel in) {
201 UsageStats stats = new UsageStats();
202 stats.mPackageName = in.readString();
203 stats.mBeginTimeStamp = in.readLong();
204 stats.mEndTimeStamp = in.readLong();
205 stats.mLastTimeUsed = in.readLong();
206 stats.mTotalTimeInForeground = in.readLong();
207 stats.mLaunchCount = in.readInt();
208 stats.mLastEvent = in.readInt();
209 stats.mBeginIdleTime = in.readLong();
210 stats.mLastTimeSystemUsed = in.readLong();
215 public UsageStats[] newArray(int size) {
216 return new UsageStats[size];