1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
/**
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package android.app.usage;
import android.content.res.Configuration;
import android.os.Parcel;
import android.os.Parcelable;
import java.util.Arrays;
import java.util.List;
/**
* A result returned from {@link android.app.usage.UsageStatsManager#queryEvents(long, long)}
* from which to read {@link android.app.usage.UsageEvents.Event} objects.
*/
public final class UsageEvents implements Parcelable {
/**
* An event representing a state change for a component.
*/
public static final class Event {
/**
* No event type.
*/
public static final int NONE = 0;
/**
* An event type denoting that a component moved to the foreground.
*/
public static final int MOVE_TO_FOREGROUND = 1;
/**
* An event type denoting that a component moved to the background.
*/
public static final int MOVE_TO_BACKGROUND = 2;
/**
* An event type denoting that a component was in the foreground when the stats
* rolled-over. This is effectively treated as a {@link #MOVE_TO_BACKGROUND}.
* {@hide}
*/
public static final int END_OF_DAY = 3;
/**
* An event type denoting that a component was in the foreground the previous day.
* This is effectively treated as a {@link #MOVE_TO_FOREGROUND}.
* {@hide}
*/
public static final int CONTINUE_PREVIOUS_DAY = 4;
/**
* An event type denoting that the device configuration has changed.
*/
public static final int CONFIGURATION_CHANGE = 5;
/**
* An event type denoting that a package was interacted with in some way by the system.
* @hide
*/
public static final int SYSTEM_INTERACTION = 6;
/**
* An event type denoting that a package was interacted with in some way by the user.
*/
public static final int USER_INTERACTION = 7;
/**
* {@hide}
*/
public String mPackage;
/**
* {@hide}
*/
public String mClass;
/**
* {@hide}
*/
public long mTimeStamp;
/**
* {@hide}
*/
public int mEventType;
/**
* Only present for {@link #CONFIGURATION_CHANGE} event types.
* {@hide}
*/
public Configuration mConfiguration;
/**
* The package name of the source of this event.
*/
public String getPackageName() {
return mPackage;
}
/**
* The class name of the source of this event. This may be null for
* certain events.
*/
public String getClassName() {
return mClass;
}
/**
* The time at which this event occurred, measured in milliseconds since the epoch.
* <p/>
* See {@link System#currentTimeMillis()}.
*/
public long getTimeStamp() {
return mTimeStamp;
}
/**
* The event type.
*
* See {@link #MOVE_TO_BACKGROUND}
* See {@link #MOVE_TO_FOREGROUND}
*/
public int getEventType() {
return mEventType;
}
/**
* Returns a {@link Configuration} for this event if the event is of type
* {@link #CONFIGURATION_CHANGE}, otherwise it returns null.
*/
public Configuration getConfiguration() {
return mConfiguration;
}
}
// Only used when creating the resulting events. Not used for reading/unparceling.
private List<Event> mEventsToWrite = null;
// Only used for reading/unparceling events.
private Parcel mParcel = null;
private final int mEventCount;
private int mIndex = 0;
/*
* In order to save space, since ComponentNames will be duplicated everywhere,
* we use a map and index into it.
*/
private String[] mStringPool;
/**
* Construct the iterator from a parcel.
* {@hide}
*/
public UsageEvents(Parcel in) {
mEventCount = in.readInt();
mIndex = in.readInt();
if (mEventCount > 0) {
mStringPool = in.createStringArray();
final int listByteLength = in.readInt();
final int positionInParcel = in.readInt();
mParcel = Parcel.obtain();
mParcel.setDataPosition(0);
mParcel.appendFrom(in, in.dataPosition(), listByteLength);
mParcel.setDataSize(mParcel.dataPosition());
mParcel.setDataPosition(positionInParcel);
}
}
/**
* Create an empty iterator.
* {@hide}
*/
UsageEvents() {
mEventCount = 0;
}
/**
* Construct the iterator in preparation for writing it to a parcel.
* {@hide}
*/
public UsageEvents(List<Event> events, String[] stringPool) {
mStringPool = stringPool;
mEventCount = events.size();
mEventsToWrite = events;
}
/**
* Returns whether or not there are more events to read using
* {@link #getNextEvent(android.app.usage.UsageEvents.Event)}.
*
* @return true if there are more events, false otherwise.
*/
public boolean hasNextEvent() {
return mIndex < mEventCount;
}
/**
* Retrieve the next {@link android.app.usage.UsageEvents.Event} from the collection and put the
* resulting data into {@code eventOut}.
*
* @param eventOut The {@link android.app.usage.UsageEvents.Event} object that will receive the
* next event data.
* @return true if an event was available, false if there are no more events.
*/
public boolean getNextEvent(Event eventOut) {
if (mIndex >= mEventCount) {
return false;
}
readEventFromParcel(mParcel, eventOut);
mIndex++;
if (mIndex >= mEventCount) {
mParcel.recycle();
mParcel = null;
}
return true;
}
/**
* Resets the collection so that it can be iterated over from the beginning.
*
* @hide When this object is iterated to completion, the parcel is destroyed and
* so resetToStart doesn't work.
*/
public void resetToStart() {
mIndex = 0;
if (mParcel != null) {
mParcel.setDataPosition(0);
}
}
private int findStringIndex(String str) {
final int index = Arrays.binarySearch(mStringPool, str);
if (index < 0) {
throw new IllegalStateException("String '" + str + "' is not in the string pool");
}
return index;
}
/**
* Writes a single event to the parcel. Modify this when updating {@link Event}.
*/
private void writeEventToParcel(Event event, Parcel p, int flags) {
final int packageIndex;
if (event.mPackage != null) {
packageIndex = findStringIndex(event.mPackage);
} else {
packageIndex = -1;
}
final int classIndex;
if (event.mClass != null) {
classIndex = findStringIndex(event.mClass);
} else {
classIndex = -1;
}
p.writeInt(packageIndex);
p.writeInt(classIndex);
p.writeInt(event.mEventType);
p.writeLong(event.mTimeStamp);
if (event.mEventType == Event.CONFIGURATION_CHANGE) {
event.mConfiguration.writeToParcel(p, flags);
}
}
/**
* Reads a single event from the parcel. Modify this when updating {@link Event}.
*/
private void readEventFromParcel(Parcel p, Event eventOut) {
final int packageIndex = p.readInt();
if (packageIndex >= 0) {
eventOut.mPackage = mStringPool[packageIndex];
} else {
eventOut.mPackage = null;
}
final int classIndex = p.readInt();
if (classIndex >= 0) {
eventOut.mClass = mStringPool[classIndex];
} else {
eventOut.mClass = null;
}
eventOut.mEventType = p.readInt();
eventOut.mTimeStamp = p.readLong();
// Extract the configuration for configuration change events.
if (eventOut.mEventType == Event.CONFIGURATION_CHANGE) {
eventOut.mConfiguration = Configuration.CREATOR.createFromParcel(p);
} else {
eventOut.mConfiguration = null;
}
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mEventCount);
dest.writeInt(mIndex);
if (mEventCount > 0) {
dest.writeStringArray(mStringPool);
if (mEventsToWrite != null) {
// Write out the events
Parcel p = Parcel.obtain();
try {
p.setDataPosition(0);
for (int i = 0; i < mEventCount; i++) {
final Event event = mEventsToWrite.get(i);
writeEventToParcel(event, p, flags);
}
final int listByteLength = p.dataPosition();
// Write the total length of the data.
dest.writeInt(listByteLength);
// Write our current position into the data.
dest.writeInt(0);
// Write the data.
dest.appendFrom(p, 0, listByteLength);
} finally {
p.recycle();
}
} else if (mParcel != null) {
// Write the total length of the data.
dest.writeInt(mParcel.dataSize());
// Write out current position into the data.
dest.writeInt(mParcel.dataPosition());
// Write the data.
dest.appendFrom(mParcel, 0, mParcel.dataSize());
} else {
throw new IllegalStateException(
"Either mParcel or mEventsToWrite must not be null");
}
}
}
public static final Creator<UsageEvents> CREATOR = new Creator<UsageEvents>() {
@Override
public UsageEvents createFromParcel(Parcel source) {
return new UsageEvents(source);
}
@Override
public UsageEvents[] newArray(int size) {
return new UsageEvents[size];
}
};
}
|