1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.service.contentcapture; 18 19 import android.annotation.IntDef; 20 import android.annotation.NonNull; 21 import android.annotation.SystemApi; 22 import android.annotation.TestApi; 23 import android.app.usage.UsageEvents.Event; 24 import android.content.ComponentName; 25 import android.os.Parcel; 26 import android.os.Parcelable; 27 28 import java.lang.annotation.Retention; 29 import java.lang.annotation.RetentionPolicy; 30 31 /** 32 * Represents an activity-level event that is not associated with a session. 33 * 34 * @hide 35 */ 36 @SystemApi 37 @TestApi 38 public final class ActivityEvent implements Parcelable { 39 40 /** 41 * The activity resumed. 42 */ 43 public static final int TYPE_ACTIVITY_RESUMED = Event.ACTIVITY_RESUMED; 44 45 /** 46 * The activity paused. 47 */ 48 public static final int TYPE_ACTIVITY_PAUSED = Event.ACTIVITY_PAUSED; 49 50 /** 51 * The activity stopped. 52 */ 53 public static final int TYPE_ACTIVITY_STOPPED = Event.ACTIVITY_STOPPED; 54 55 /** 56 * The activity was destroyed. 57 */ 58 public static final int TYPE_ACTIVITY_DESTROYED = Event.ACTIVITY_DESTROYED; 59 60 /** @hide */ 61 @IntDef(prefix = { "TYPE_" }, value = { 62 TYPE_ACTIVITY_RESUMED, 63 TYPE_ACTIVITY_PAUSED, 64 TYPE_ACTIVITY_STOPPED, 65 TYPE_ACTIVITY_DESTROYED 66 }) 67 @Retention(RetentionPolicy.SOURCE) 68 public @interface ActivityEventType{} 69 70 private final @NonNull ComponentName mComponentName; 71 private final @ActivityEventType int mType; 72 73 /** @hide */ ActivityEvent(@onNull ComponentName componentName, @ActivityEventType int type)74 public ActivityEvent(@NonNull ComponentName componentName, @ActivityEventType int type) { 75 mComponentName = componentName; 76 mType = type; 77 } 78 79 /** 80 * Gests the {@link ComponentName} of the activity associated with the event. 81 */ 82 @NonNull getComponentName()83 public ComponentName getComponentName() { 84 return mComponentName; 85 } 86 87 /** 88 * Gets the event type. 89 * 90 * @return either {@link #TYPE_ACTIVITY_RESUMED}, {@value #TYPE_ACTIVITY_PAUSED}, 91 * {@value #TYPE_ACTIVITY_STOPPED}, or {@value #TYPE_ACTIVITY_DESTROYED}. 92 */ 93 @ActivityEventType getEventType()94 public int getEventType() { 95 return mType; 96 } 97 98 /** @hide */ getTypeAsString(@ctivityEventType int type)99 public static String getTypeAsString(@ActivityEventType int type) { 100 switch (type) { 101 case TYPE_ACTIVITY_RESUMED: 102 return "ACTIVITY_RESUMED"; 103 case TYPE_ACTIVITY_PAUSED: 104 return "ACTIVITY_PAUSED"; 105 case TYPE_ACTIVITY_STOPPED: 106 return "ACTIVITY_STOPPED"; 107 case TYPE_ACTIVITY_DESTROYED: 108 return "ACTIVITY_DESTROYED"; 109 default: 110 return "UKNOWN_TYPE: " + type; 111 } 112 } 113 114 @Override toString()115 public String toString() { 116 return new StringBuilder("ActivityEvent[").append(mComponentName.toShortString()) 117 .append("]:").append(getTypeAsString(mType)).toString(); 118 } 119 120 @Override describeContents()121 public int describeContents() { 122 return 0; 123 } 124 125 @Override writeToParcel(@onNull Parcel parcel, int flags)126 public void writeToParcel(@NonNull Parcel parcel, int flags) { 127 parcel.writeParcelable(mComponentName, flags); 128 parcel.writeInt(mType); 129 } 130 131 public static final @android.annotation.NonNull Creator<ActivityEvent> CREATOR = 132 new Creator<ActivityEvent>() { 133 134 @Override 135 @NonNull 136 public ActivityEvent createFromParcel(@NonNull Parcel parcel) { 137 final ComponentName componentName = parcel.readParcelable(null); 138 final int eventType = parcel.readInt(); 139 return new ActivityEvent(componentName, eventType); 140 } 141 142 @Override 143 @NonNull 144 public ActivityEvent[] newArray(int size) { 145 return new ActivityEvent[size]; 146 } 147 }; 148 } 149