EventStats.java revision ced54398cc0dfd2f782153560c2ffd0eb8743045
1/** 2 * Copyright (C) 2018 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 event type for a specific 24 * time range. 25 */ 26public final class EventStats implements Parcelable { 27 28 /** 29 * {@hide} 30 */ 31 public int mEventType; 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 * {@hide} 51 */ 52 public long mTotalTime; 53 54 /** 55 * {@hide} 56 */ 57 public int mCount; 58 59 /** 60 * {@hide} 61 */ 62 public EventStats() { 63 } 64 65 public EventStats(EventStats stats) { 66 mEventType = stats.mEventType; 67 mBeginTimeStamp = stats.mBeginTimeStamp; 68 mEndTimeStamp = stats.mEndTimeStamp; 69 mLastTimeUsed = stats.mLastTimeUsed; 70 mTotalTime = stats.mTotalTime; 71 mCount = stats.mCount; 72 } 73 74 /** 75 * Return the type of event this is usage for. May be one of the event 76 * constants in {@link UsageEvents.Event}. 77 */ 78 public int getEventType() { 79 return mEventType; 80 } 81 82 /** 83 * Get the beginning of the time range this {@link android.app.usage.EventStats} represents, 84 * measured in milliseconds since the epoch. 85 * <p/> 86 * See {@link System#currentTimeMillis()}. 87 */ 88 public long getFirstTimeStamp() { 89 return mBeginTimeStamp; 90 } 91 92 /** 93 * Get the end of the time range this {@link android.app.usage.EventStats} represents, 94 * measured in milliseconds since the epoch. 95 * <p/> 96 * See {@link System#currentTimeMillis()}. 97 */ 98 public long getLastTimeStamp() { 99 return mEndTimeStamp; 100 } 101 102 /** 103 * Get the last time this event was used, measured in milliseconds since the epoch. 104 * <p/> 105 * See {@link System#currentTimeMillis()}. 106 */ 107 public long getLastTimeUsed() { 108 return mLastTimeUsed; 109 } 110 111 /** 112 * Return the number of times that this event occurred over the interval. 113 */ 114 public int getCount() { 115 return mCount; 116 } 117 118 /** 119 * Get the total time this event was active, measured in milliseconds. 120 */ 121 public long getTotalTime() { 122 return mTotalTime; 123 } 124 125 /** 126 * Add the statistics from the right {@link EventStats} to the left. The event type for 127 * both {@link UsageStats} objects must be the same. 128 * @param right The {@link EventStats} object to merge into this one. 129 * @throws java.lang.IllegalArgumentException if the event types of the two 130 * {@link UsageStats} objects are different. 131 */ 132 public void add(EventStats right) { 133 if (mEventType != right.mEventType) { 134 throw new IllegalArgumentException("Can't merge EventStats for event #" 135 + mEventType + " with EventStats for event #" + right.mEventType); 136 } 137 138 // We use the mBeginTimeStamp due to a bug where UsageStats files can overlap with 139 // regards to their mEndTimeStamp. 140 if (right.mBeginTimeStamp > mBeginTimeStamp) { 141 mLastTimeUsed = Math.max(mLastTimeUsed, right.mLastTimeUsed); 142 } 143 mBeginTimeStamp = Math.min(mBeginTimeStamp, right.mBeginTimeStamp); 144 mEndTimeStamp = Math.max(mEndTimeStamp, right.mEndTimeStamp); 145 mTotalTime += right.mTotalTime; 146 mCount += right.mCount; 147 } 148 149 @Override 150 public int describeContents() { 151 return 0; 152 } 153 154 @Override 155 public void writeToParcel(Parcel dest, int flags) { 156 dest.writeInt(mEventType); 157 dest.writeLong(mBeginTimeStamp); 158 dest.writeLong(mEndTimeStamp); 159 dest.writeLong(mLastTimeUsed); 160 dest.writeLong(mTotalTime); 161 dest.writeInt(mCount); 162 } 163 164 public static final Creator<EventStats> CREATOR = new Creator<EventStats>() { 165 @Override 166 public EventStats createFromParcel(Parcel in) { 167 EventStats stats = new EventStats(); 168 stats.mEventType = in.readInt(); 169 stats.mBeginTimeStamp = in.readLong(); 170 stats.mEndTimeStamp = in.readLong(); 171 stats.mLastTimeUsed = in.readLong(); 172 stats.mTotalTime = in.readLong(); 173 stats.mCount = in.readInt(); 174 return stats; 175 } 176 177 @Override 178 public EventStats[] newArray(int size) { 179 return new EventStats[size]; 180 } 181 }; 182} 183