10debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski/** 20debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * Copyright (C) 2014 The Android Open Source Project 30debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * 40debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * Licensed under the Apache License, Version 2.0 (the "License"); you may not 50debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * use this file except in compliance with the License. You may obtain a copy 60debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * of the License at 70debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * 80debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * http://www.apache.org/licenses/LICENSE-2.0 90debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * 100debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * Unless required by applicable law or agreed to in writing, software 110debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 120debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 130debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * License for the specific language governing permissions and limitations 140debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * under the License. 150debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski */ 160debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 170debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinskipackage android.app.usage; 180debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 190debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinskiimport android.os.Parcel; 200debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinskiimport android.os.Parcelable; 210debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 223516800b611a79339a3c188332d13a26e9086b09Adam Lesinski/** 233516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * Contains usage statistics for an app package for a specific 243516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * time range. 253516800b611a79339a3c188332d13a26e9086b09Adam Lesinski */ 260debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinskipublic final class UsageStats implements Parcelable { 270debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 280debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski /** 290debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * {@hide} 300debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski */ 313516800b611a79339a3c188332d13a26e9086b09Adam Lesinski public String mPackageName; 320debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 330debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski /** 340debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * {@hide} 350debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski */ 360debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski public long mBeginTimeStamp; 370debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 380debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski /** 390debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * {@hide} 400debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski */ 410debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski public long mEndTimeStamp; 420debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 430debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski /** 44c8e8729244d75584ce71a74d29c452fe538a22c5Adam Lesinski * Last time used by the user with an explicit action (notification, activity launch). 450debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * {@hide} 460debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski */ 473516800b611a79339a3c188332d13a26e9086b09Adam Lesinski public long mLastTimeUsed; 480debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 493516800b611a79339a3c188332d13a26e9086b09Adam Lesinski /** 50c8e8729244d75584ce71a74d29c452fe538a22c5Adam Lesinski * The last time the package was used via implicit, non-user initiated actions (service 51c8e8729244d75584ce71a74d29c452fe538a22c5Adam Lesinski * was bound, etc). 52c8e8729244d75584ce71a74d29c452fe538a22c5Adam Lesinski * {@hide} 53c8e8729244d75584ce71a74d29c452fe538a22c5Adam Lesinski */ 54c8e8729244d75584ce71a74d29c452fe538a22c5Adam Lesinski public long mLastTimeSystemUsed; 55c8e8729244d75584ce71a74d29c452fe538a22c5Adam Lesinski 56c8e8729244d75584ce71a74d29c452fe538a22c5Adam Lesinski /** 5706bf824628c118fbd5ad6756913d7fd63a6f4ce5Amith Yamasani * Last time the package was used and the beginning of the idle countdown. 5806bf824628c118fbd5ad6756913d7fd63a6f4ce5Amith Yamasani * This uses a different timebase that is about how much the device has been in use in general. 5906bf824628c118fbd5ad6756913d7fd63a6f4ce5Amith Yamasani * {@hide} 6006bf824628c118fbd5ad6756913d7fd63a6f4ce5Amith Yamasani */ 6106bf824628c118fbd5ad6756913d7fd63a6f4ce5Amith Yamasani public long mBeginIdleTime; 6206bf824628c118fbd5ad6756913d7fd63a6f4ce5Amith Yamasani 6306bf824628c118fbd5ad6756913d7fd63a6f4ce5Amith Yamasani /** 643516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * {@hide} 653516800b611a79339a3c188332d13a26e9086b09Adam Lesinski */ 663516800b611a79339a3c188332d13a26e9086b09Adam Lesinski public long mTotalTimeInForeground; 670debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 680debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski /** 690debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * {@hide} 700debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski */ 713516800b611a79339a3c188332d13a26e9086b09Adam Lesinski public int mLaunchCount; 720debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 730debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski /** 740debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * {@hide} 750debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski */ 763516800b611a79339a3c188332d13a26e9086b09Adam Lesinski public int mLastEvent; 770debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 780debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski /** 790debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski * {@hide} 800debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski */ 810debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski public UsageStats() { 820debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } 830debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 840debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski public UsageStats(UsageStats stats) { 853516800b611a79339a3c188332d13a26e9086b09Adam Lesinski mPackageName = stats.mPackageName; 860debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski mBeginTimeStamp = stats.mBeginTimeStamp; 870debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski mEndTimeStamp = stats.mEndTimeStamp; 883516800b611a79339a3c188332d13a26e9086b09Adam Lesinski mLastTimeUsed = stats.mLastTimeUsed; 893516800b611a79339a3c188332d13a26e9086b09Adam Lesinski mTotalTimeInForeground = stats.mTotalTimeInForeground; 903516800b611a79339a3c188332d13a26e9086b09Adam Lesinski mLaunchCount = stats.mLaunchCount; 913516800b611a79339a3c188332d13a26e9086b09Adam Lesinski mLastEvent = stats.mLastEvent; 9206bf824628c118fbd5ad6756913d7fd63a6f4ce5Amith Yamasani mBeginIdleTime = stats.mBeginIdleTime; 93c8e8729244d75584ce71a74d29c452fe538a22c5Adam Lesinski mLastTimeSystemUsed = stats.mLastTimeSystemUsed; 943516800b611a79339a3c188332d13a26e9086b09Adam Lesinski } 950debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 963516800b611a79339a3c188332d13a26e9086b09Adam Lesinski public String getPackageName() { 973516800b611a79339a3c188332d13a26e9086b09Adam Lesinski return mPackageName; 980debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } 990debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 1003516800b611a79339a3c188332d13a26e9086b09Adam Lesinski /** 101cc562a811da508b275254f275d6e0c1758a47d07Adam Lesinski * Get the beginning of the time range this {@link android.app.usage.UsageStats} represents, 102cc562a811da508b275254f275d6e0c1758a47d07Adam Lesinski * measured in milliseconds since the epoch. 103cc562a811da508b275254f275d6e0c1758a47d07Adam Lesinski * <p/> 104cc562a811da508b275254f275d6e0c1758a47d07Adam Lesinski * See {@link System#currentTimeMillis()}. 1053516800b611a79339a3c188332d13a26e9086b09Adam Lesinski */ 1060debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski public long getFirstTimeStamp() { 1070debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski return mBeginTimeStamp; 1080debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } 1090debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 1103516800b611a79339a3c188332d13a26e9086b09Adam Lesinski /** 111cc562a811da508b275254f275d6e0c1758a47d07Adam Lesinski * Get the end of the time range this {@link android.app.usage.UsageStats} represents, 112cc562a811da508b275254f275d6e0c1758a47d07Adam Lesinski * measured in milliseconds since the epoch. 113cc562a811da508b275254f275d6e0c1758a47d07Adam Lesinski * <p/> 114cc562a811da508b275254f275d6e0c1758a47d07Adam Lesinski * See {@link System#currentTimeMillis()}. 1153516800b611a79339a3c188332d13a26e9086b09Adam Lesinski */ 1160debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski public long getLastTimeStamp() { 1170debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski return mEndTimeStamp; 1180debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } 1190debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 1203516800b611a79339a3c188332d13a26e9086b09Adam Lesinski /** 121cc562a811da508b275254f275d6e0c1758a47d07Adam Lesinski * Get the last time this package was used, measured in milliseconds since the epoch. 122cc562a811da508b275254f275d6e0c1758a47d07Adam Lesinski * <p/> 123cc562a811da508b275254f275d6e0c1758a47d07Adam Lesinski * See {@link System#currentTimeMillis()}. 1243516800b611a79339a3c188332d13a26e9086b09Adam Lesinski */ 1253516800b611a79339a3c188332d13a26e9086b09Adam Lesinski public long getLastTimeUsed() { 1263516800b611a79339a3c188332d13a26e9086b09Adam Lesinski return mLastTimeUsed; 1270debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } 1280debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 1293516800b611a79339a3c188332d13a26e9086b09Adam Lesinski /** 13006bf824628c118fbd5ad6756913d7fd63a6f4ce5Amith Yamasani * @hide 131c8e8729244d75584ce71a74d29c452fe538a22c5Adam Lesinski * Get the last time this package was used by the system (not the user). This can be different 132c8e8729244d75584ce71a74d29c452fe538a22c5Adam Lesinski * from {@link #getLastTimeUsed()} when the system binds to one of this package's services. 133c8e8729244d75584ce71a74d29c452fe538a22c5Adam Lesinski * See {@link System#currentTimeMillis()}. 134c8e8729244d75584ce71a74d29c452fe538a22c5Adam Lesinski */ 135c8e8729244d75584ce71a74d29c452fe538a22c5Adam Lesinski public long getLastTimeSystemUsed() { 136c8e8729244d75584ce71a74d29c452fe538a22c5Adam Lesinski return mLastTimeSystemUsed; 137c8e8729244d75584ce71a74d29c452fe538a22c5Adam Lesinski } 138c8e8729244d75584ce71a74d29c452fe538a22c5Adam Lesinski 139c8e8729244d75584ce71a74d29c452fe538a22c5Adam Lesinski /** 140c8e8729244d75584ce71a74d29c452fe538a22c5Adam Lesinski * @hide 14106bf824628c118fbd5ad6756913d7fd63a6f4ce5Amith Yamasani * Get the last time this package was active, measured in milliseconds. This timestamp 14206bf824628c118fbd5ad6756913d7fd63a6f4ce5Amith Yamasani * uses a timebase that represents how much the device was used and not wallclock time. 14306bf824628c118fbd5ad6756913d7fd63a6f4ce5Amith Yamasani */ 14406bf824628c118fbd5ad6756913d7fd63a6f4ce5Amith Yamasani public long getBeginIdleTime() { 14506bf824628c118fbd5ad6756913d7fd63a6f4ce5Amith Yamasani return mBeginIdleTime; 14606bf824628c118fbd5ad6756913d7fd63a6f4ce5Amith Yamasani } 14706bf824628c118fbd5ad6756913d7fd63a6f4ce5Amith Yamasani 14806bf824628c118fbd5ad6756913d7fd63a6f4ce5Amith Yamasani /** 149cc562a811da508b275254f275d6e0c1758a47d07Adam Lesinski * Get the total time this package spent in the foreground, measured in milliseconds. 1503516800b611a79339a3c188332d13a26e9086b09Adam Lesinski */ 1513516800b611a79339a3c188332d13a26e9086b09Adam Lesinski public long getTotalTimeInForeground() { 1523516800b611a79339a3c188332d13a26e9086b09Adam Lesinski return mTotalTimeInForeground; 1530debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } 1540debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 1550debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski /** 1563516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * Add the statistics from the right {@link UsageStats} to the left. The package name for 1573516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * both {@link UsageStats} objects must be the same. 1583516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * @param right The {@link UsageStats} object to merge into this one. 1593516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * @throws java.lang.IllegalArgumentException if the package names of the two 1603516800b611a79339a3c188332d13a26e9086b09Adam Lesinski * {@link UsageStats} objects are different. 1610debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski */ 1623516800b611a79339a3c188332d13a26e9086b09Adam Lesinski public void add(UsageStats right) { 1633516800b611a79339a3c188332d13a26e9086b09Adam Lesinski if (!mPackageName.equals(right.mPackageName)) { 1643516800b611a79339a3c188332d13a26e9086b09Adam Lesinski throw new IllegalArgumentException("Can't merge UsageStats for package '" + 1653516800b611a79339a3c188332d13a26e9086b09Adam Lesinski mPackageName + "' with UsageStats for package '" + right.mPackageName + "'."); 1660debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } 1673516800b611a79339a3c188332d13a26e9086b09Adam Lesinski 1683516800b611a79339a3c188332d13a26e9086b09Adam Lesinski if (right.mEndTimeStamp > mEndTimeStamp) { 1693516800b611a79339a3c188332d13a26e9086b09Adam Lesinski mLastEvent = right.mLastEvent; 1703516800b611a79339a3c188332d13a26e9086b09Adam Lesinski mEndTimeStamp = right.mEndTimeStamp; 1713516800b611a79339a3c188332d13a26e9086b09Adam Lesinski mLastTimeUsed = right.mLastTimeUsed; 17206bf824628c118fbd5ad6756913d7fd63a6f4ce5Amith Yamasani mBeginIdleTime = right.mBeginIdleTime; 173c8e8729244d75584ce71a74d29c452fe538a22c5Adam Lesinski mLastTimeSystemUsed = right.mLastTimeSystemUsed; 1743516800b611a79339a3c188332d13a26e9086b09Adam Lesinski } 1753516800b611a79339a3c188332d13a26e9086b09Adam Lesinski mBeginTimeStamp = Math.min(mBeginTimeStamp, right.mBeginTimeStamp); 1763516800b611a79339a3c188332d13a26e9086b09Adam Lesinski mTotalTimeInForeground += right.mTotalTimeInForeground; 1773516800b611a79339a3c188332d13a26e9086b09Adam Lesinski mLaunchCount += right.mLaunchCount; 1780debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } 1790debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 1800debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski @Override 1810debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski public int describeContents() { 1820debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski return 0; 1830debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } 1840debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 1850debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski @Override 1860debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski public void writeToParcel(Parcel dest, int flags) { 1873516800b611a79339a3c188332d13a26e9086b09Adam Lesinski dest.writeString(mPackageName); 1880debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski dest.writeLong(mBeginTimeStamp); 1890debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski dest.writeLong(mEndTimeStamp); 1903516800b611a79339a3c188332d13a26e9086b09Adam Lesinski dest.writeLong(mLastTimeUsed); 1913516800b611a79339a3c188332d13a26e9086b09Adam Lesinski dest.writeLong(mTotalTimeInForeground); 1923516800b611a79339a3c188332d13a26e9086b09Adam Lesinski dest.writeInt(mLaunchCount); 1933516800b611a79339a3c188332d13a26e9086b09Adam Lesinski dest.writeInt(mLastEvent); 19406bf824628c118fbd5ad6756913d7fd63a6f4ce5Amith Yamasani dest.writeLong(mBeginIdleTime); 195c8e8729244d75584ce71a74d29c452fe538a22c5Adam Lesinski dest.writeLong(mLastTimeSystemUsed); 1960debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } 1970debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 1980debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski public static final Creator<UsageStats> CREATOR = new Creator<UsageStats>() { 1990debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski @Override 2000debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski public UsageStats createFromParcel(Parcel in) { 2010debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski UsageStats stats = new UsageStats(); 2023516800b611a79339a3c188332d13a26e9086b09Adam Lesinski stats.mPackageName = in.readString(); 2030debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski stats.mBeginTimeStamp = in.readLong(); 2040debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski stats.mEndTimeStamp = in.readLong(); 2053516800b611a79339a3c188332d13a26e9086b09Adam Lesinski stats.mLastTimeUsed = in.readLong(); 2063516800b611a79339a3c188332d13a26e9086b09Adam Lesinski stats.mTotalTimeInForeground = in.readLong(); 2073516800b611a79339a3c188332d13a26e9086b09Adam Lesinski stats.mLaunchCount = in.readInt(); 2083516800b611a79339a3c188332d13a26e9086b09Adam Lesinski stats.mLastEvent = in.readInt(); 20906bf824628c118fbd5ad6756913d7fd63a6f4ce5Amith Yamasani stats.mBeginIdleTime = in.readLong(); 210c8e8729244d75584ce71a74d29c452fe538a22c5Adam Lesinski stats.mLastTimeSystemUsed = in.readLong(); 2110debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski return stats; 2120debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } 2130debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski 2140debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski @Override 2150debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski public UsageStats[] newArray(int size) { 2160debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski return new UsageStats[size]; 2170debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski } 2180debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski }; 2190debc9aff4c0cbc28e083a948081d91b0f171319Adam Lesinski} 220