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 * {@hide} 45 */ 46 public long mLastTimeUsed; 47 48 /** 49 * {@hide} 50 */ 51 public long mTotalTimeInForeground; 52 53 /** 54 * {@hide} 55 */ 56 public int mLaunchCount; 57 58 /** 59 * {@hide} 60 */ 61 public int mLastEvent; 62 63 /** 64 * {@hide} 65 */ UsageStats()66 public UsageStats() { 67 } 68 UsageStats(UsageStats stats)69 public UsageStats(UsageStats stats) { 70 mPackageName = stats.mPackageName; 71 mBeginTimeStamp = stats.mBeginTimeStamp; 72 mEndTimeStamp = stats.mEndTimeStamp; 73 mLastTimeUsed = stats.mLastTimeUsed; 74 mTotalTimeInForeground = stats.mTotalTimeInForeground; 75 mLaunchCount = stats.mLaunchCount; 76 mLastEvent = stats.mLastEvent; 77 } 78 getPackageName()79 public String getPackageName() { 80 return mPackageName; 81 } 82 83 /** 84 * Get the beginning of the time range this {@link android.app.usage.UsageStats} represents, 85 * measured in milliseconds since the epoch. 86 * <p/> 87 * See {@link System#currentTimeMillis()}. 88 */ getFirstTimeStamp()89 public long getFirstTimeStamp() { 90 return mBeginTimeStamp; 91 } 92 93 /** 94 * Get the end of the time range this {@link android.app.usage.UsageStats} represents, 95 * measured in milliseconds since the epoch. 96 * <p/> 97 * See {@link System#currentTimeMillis()}. 98 */ getLastTimeStamp()99 public long getLastTimeStamp() { 100 return mEndTimeStamp; 101 } 102 103 /** 104 * Get the last time this package was used, measured in milliseconds since the epoch. 105 * <p/> 106 * See {@link System#currentTimeMillis()}. 107 */ getLastTimeUsed()108 public long getLastTimeUsed() { 109 return mLastTimeUsed; 110 } 111 112 /** 113 * Get the total time this package spent in the foreground, measured in milliseconds. 114 */ getTotalTimeInForeground()115 public long getTotalTimeInForeground() { 116 return mTotalTimeInForeground; 117 } 118 119 /** 120 * Add the statistics from the right {@link UsageStats} to the left. The package name for 121 * both {@link UsageStats} objects must be the same. 122 * @param right The {@link UsageStats} object to merge into this one. 123 * @throws java.lang.IllegalArgumentException if the package names of the two 124 * {@link UsageStats} objects are different. 125 */ add(UsageStats right)126 public void add(UsageStats right) { 127 if (!mPackageName.equals(right.mPackageName)) { 128 throw new IllegalArgumentException("Can't merge UsageStats for package '" + 129 mPackageName + "' with UsageStats for package '" + right.mPackageName + "'."); 130 } 131 132 if (right.mEndTimeStamp > mEndTimeStamp) { 133 mLastEvent = right.mLastEvent; 134 mEndTimeStamp = right.mEndTimeStamp; 135 mLastTimeUsed = right.mLastTimeUsed; 136 } 137 mBeginTimeStamp = Math.min(mBeginTimeStamp, right.mBeginTimeStamp); 138 mTotalTimeInForeground += right.mTotalTimeInForeground; 139 mLaunchCount += right.mLaunchCount; 140 } 141 142 @Override describeContents()143 public int describeContents() { 144 return 0; 145 } 146 147 @Override writeToParcel(Parcel dest, int flags)148 public void writeToParcel(Parcel dest, int flags) { 149 dest.writeString(mPackageName); 150 dest.writeLong(mBeginTimeStamp); 151 dest.writeLong(mEndTimeStamp); 152 dest.writeLong(mLastTimeUsed); 153 dest.writeLong(mTotalTimeInForeground); 154 dest.writeInt(mLaunchCount); 155 dest.writeInt(mLastEvent); 156 } 157 158 public static final Creator<UsageStats> CREATOR = new Creator<UsageStats>() { 159 @Override 160 public UsageStats createFromParcel(Parcel in) { 161 UsageStats stats = new UsageStats(); 162 stats.mPackageName = in.readString(); 163 stats.mBeginTimeStamp = in.readLong(); 164 stats.mEndTimeStamp = in.readLong(); 165 stats.mLastTimeUsed = in.readLong(); 166 stats.mTotalTimeInForeground = in.readLong(); 167 stats.mLaunchCount = in.readInt(); 168 stats.mLastEvent = in.readInt(); 169 return stats; 170 } 171 172 @Override 173 public UsageStats[] newArray(int size) { 174 return new UsageStats[size]; 175 } 176 }; 177 } 178