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