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