• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (C) 2014 The Android Open Source Project
3  *
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
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */
16 
17 package android.app.usage;
18 
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 
22 /**
23  * Contains usage statistics for an app package for a specific
24  * time range.
25  */
26 public final class UsageStats implements Parcelable {
27 
28     /**
29      * {@hide}
30      */
31     public String mPackageName;
32 
33     /**
34      * {@hide}
35      */
36     public long mBeginTimeStamp;
37 
38     /**
39      * {@hide}
40      */
41     public long mEndTimeStamp;
42 
43     /**
44      * Last time used by the user with an explicit action (notification, activity launch).
45      * {@hide}
46      */
47     public long mLastTimeUsed;
48 
49     /**
50      * The last time the package was used via implicit, non-user initiated actions (service
51      * was bound, etc).
52      * {@hide}
53      */
54     public long mLastTimeSystemUsed;
55 
56     /**
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.
59      * {@hide}
60      */
61     public long mBeginIdleTime;
62 
63     /**
64      * {@hide}
65      */
66     public long mTotalTimeInForeground;
67 
68     /**
69      * {@hide}
70      */
71     public int mLaunchCount;
72 
73     /**
74      * {@hide}
75      */
76     public int mLastEvent;
77 
78     /**
79      * {@hide}
80      */
UsageStats()81     public UsageStats() {
82     }
83 
UsageStats(UsageStats stats)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;
94     }
95 
getPackageName()96     public String getPackageName() {
97         return mPackageName;
98     }
99 
100     /**
101      * Get the beginning of the time range this {@link android.app.usage.UsageStats} represents,
102      * measured in milliseconds since the epoch.
103      * <p/>
104      * See {@link System#currentTimeMillis()}.
105      */
getFirstTimeStamp()106     public long getFirstTimeStamp() {
107         return mBeginTimeStamp;
108     }
109 
110     /**
111      * Get the end of the time range this {@link android.app.usage.UsageStats} represents,
112      * measured in milliseconds since the epoch.
113      * <p/>
114      * See {@link System#currentTimeMillis()}.
115      */
getLastTimeStamp()116     public long getLastTimeStamp() {
117         return mEndTimeStamp;
118     }
119 
120     /**
121      * Get the last time this package was used, measured in milliseconds since the epoch.
122      * <p/>
123      * See {@link System#currentTimeMillis()}.
124      */
getLastTimeUsed()125     public long getLastTimeUsed() {
126         return mLastTimeUsed;
127     }
128 
129     /**
130      * @hide
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()}.
134      */
getLastTimeSystemUsed()135     public long getLastTimeSystemUsed() {
136         return mLastTimeSystemUsed;
137     }
138 
139     /**
140      * @hide
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.
143      */
getBeginIdleTime()144     public long getBeginIdleTime() {
145         return mBeginIdleTime;
146     }
147 
148     /**
149      * Get the total time this package spent in the foreground, measured in milliseconds.
150      */
getTotalTimeInForeground()151     public long getTotalTimeInForeground() {
152         return mTotalTimeInForeground;
153     }
154 
155     /**
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.
161      */
add(UsageStats right)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 + "'.");
166         }
167 
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;
177         }
178         mBeginTimeStamp = Math.min(mBeginTimeStamp, right.mBeginTimeStamp);
179         mEndTimeStamp = Math.max(mEndTimeStamp, right.mEndTimeStamp);
180         mTotalTimeInForeground += right.mTotalTimeInForeground;
181         mLaunchCount += right.mLaunchCount;
182     }
183 
184     @Override
describeContents()185     public int describeContents() {
186         return 0;
187     }
188 
189     @Override
writeToParcel(Parcel dest, int flags)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);
200     }
201 
202     public static final Creator<UsageStats> CREATOR = new Creator<UsageStats>() {
203         @Override
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();
215             return stats;
216         }
217 
218         @Override
219         public UsageStats[] newArray(int size) {
220             return new UsageStats[size];
221         }
222     };
223 }
224