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 17package android.app.usage; 18 19import android.os.Parcel; 20import android.os.Parcelable; 21 22/** 23 * Contains usage statistics for an app package for a specific 24 * time range. 25 */ 26public 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 */ 81 public UsageStats() { 82 } 83 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 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 */ 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 */ 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 */ 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 */ 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 */ 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 */ 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 */ 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.mEndTimeStamp > mEndTimeStamp) { 169 mLastEvent = right.mLastEvent; 170 mEndTimeStamp = right.mEndTimeStamp; 171 mLastTimeUsed = right.mLastTimeUsed; 172 mBeginIdleTime = right.mBeginIdleTime; 173 mLastTimeSystemUsed = right.mLastTimeSystemUsed; 174 } 175 mBeginTimeStamp = Math.min(mBeginTimeStamp, right.mBeginTimeStamp); 176 mTotalTimeInForeground += right.mTotalTimeInForeground; 177 mLaunchCount += right.mLaunchCount; 178 } 179 180 @Override 181 public int describeContents() { 182 return 0; 183 } 184 185 @Override 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); 196 } 197 198 public static final Creator<UsageStats> CREATOR = new Creator<UsageStats>() { 199 @Override 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(); 211 return stats; 212 } 213 214 @Override 215 public UsageStats[] newArray(int size) { 216 return new UsageStats[size]; 217 } 218 }; 219} 220