blob: ea95a0557f2b6e3e58f484813dfd0ebee15291f7 [file] [log] [blame]
Dianne Hackbornced54392018-02-26 13:07:42 -08001/**
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 /**
Dianne Hackbornced54392018-02-26 13:07:42 -080044 * {@hide}
45 */
Dianne Hackborn3378aa92018-03-30 17:43:49 -070046 public long mLastEventTime;
Dianne Hackbornced54392018-02-26 13:07:42 -080047
48 /**
49 * {@hide}
50 */
51 public long mTotalTime;
52
53 /**
54 * {@hide}
55 */
56 public int mCount;
57
58 /**
59 * {@hide}
60 */
61 public EventStats() {
62 }
63
64 public EventStats(EventStats stats) {
65 mEventType = stats.mEventType;
66 mBeginTimeStamp = stats.mBeginTimeStamp;
67 mEndTimeStamp = stats.mEndTimeStamp;
Dianne Hackborn3378aa92018-03-30 17:43:49 -070068 mLastEventTime = stats.mLastEventTime;
Dianne Hackbornced54392018-02-26 13:07:42 -080069 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 */
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 */
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 */
97 public long getLastTimeStamp() {
98 return mEndTimeStamp;
99 }
100
101 /**
Dianne Hackborn3378aa92018-03-30 17:43:49 -0700102 * Get the last time this event triggered, measured in milliseconds since the epoch.
Dianne Hackbornced54392018-02-26 13:07:42 -0800103 * <p/>
104 * See {@link System#currentTimeMillis()}.
105 */
Dianne Hackborn3378aa92018-03-30 17:43:49 -0700106 public long getLastEventTime() {
107 return mLastEventTime;
Dianne Hackbornced54392018-02-26 13:07:42 -0800108 }
109
110 /**
111 * Return the number of times that this event occurred over the interval.
112 */
113 public int getCount() {
114 return mCount;
115 }
116
117 /**
118 * Get the total time this event was active, measured in milliseconds.
119 */
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 */
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) {
Dianne Hackborn3378aa92018-03-30 17:43:49 -0700140 mLastEventTime = Math.max(mLastEventTime, right.mLastEventTime);
Dianne Hackbornced54392018-02-26 13:07:42 -0800141 }
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
149 public int describeContents() {
150 return 0;
151 }
152
153 @Override
154 public void writeToParcel(Parcel dest, int flags) {
155 dest.writeInt(mEventType);
156 dest.writeLong(mBeginTimeStamp);
157 dest.writeLong(mEndTimeStamp);
Dianne Hackborn3378aa92018-03-30 17:43:49 -0700158 dest.writeLong(mLastEventTime);
Dianne Hackbornced54392018-02-26 13:07:42 -0800159 dest.writeLong(mTotalTime);
160 dest.writeInt(mCount);
161 }
162
163 public static final 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();
Dianne Hackborn3378aa92018-03-30 17:43:49 -0700170 stats.mLastEventTime = in.readLong();
Dianne Hackbornced54392018-02-26 13:07:42 -0800171 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}