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.app.assist.ActivityId; 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 public final class ActivityEvent implements Parcelable { 38 39 /** 40 * The activity resumed. 41 */ 42 public static final int TYPE_ACTIVITY_RESUMED = Event.ACTIVITY_RESUMED; 43 44 /** 45 * The activity paused. 46 */ 47 public static final int TYPE_ACTIVITY_PAUSED = Event.ACTIVITY_PAUSED; 48 49 /** 50 * The activity stopped. 51 */ 52 public static final int TYPE_ACTIVITY_STOPPED = Event.ACTIVITY_STOPPED; 53 54 /** 55 * The activity was destroyed. 56 */ 57 public static final int TYPE_ACTIVITY_DESTROYED = Event.ACTIVITY_DESTROYED; 58 59 /** 60 * TODO: change to public field. 61 * The activity was started. 62 * 63 * <p>There are some reason, ACTIVITY_START cannot be added into UsageStats. We don't depend on 64 * UsageEvents for Activity start. 65 * </p> 66 * 67 * @hide 68 */ 69 public static final int TYPE_ACTIVITY_STARTED = 10000; 70 71 /** @hide */ 72 @IntDef(prefix = { "TYPE_" }, value = { 73 TYPE_ACTIVITY_RESUMED, 74 TYPE_ACTIVITY_PAUSED, 75 TYPE_ACTIVITY_STOPPED, 76 TYPE_ACTIVITY_DESTROYED, 77 TYPE_ACTIVITY_STARTED 78 }) 79 @Retention(RetentionPolicy.SOURCE) 80 public @interface ActivityEventType{} 81 82 private final @NonNull ComponentName mComponentName; 83 private final @ActivityEventType int mType; 84 private final @NonNull ActivityId mActivityId; 85 86 /** @hide */ ActivityEvent(@onNull ActivityId activityId, @NonNull ComponentName componentName, @ActivityEventType int type)87 public ActivityEvent(@NonNull ActivityId activityId, 88 @NonNull ComponentName componentName, @ActivityEventType int type) { 89 mActivityId = activityId; 90 mComponentName = componentName; 91 mType = type; 92 } 93 94 /** 95 * Gets the ActivityId of the activity associated with the event. 96 */ 97 @NonNull getActivityId()98 public ActivityId getActivityId() { 99 return mActivityId; 100 } 101 102 /** 103 * Gests the {@link ComponentName} of the activity associated with the event. 104 */ 105 @NonNull getComponentName()106 public ComponentName getComponentName() { 107 return mComponentName; 108 } 109 110 /** 111 * Gets the event type. 112 * 113 * @return either {@link #TYPE_ACTIVITY_RESUMED}, {@value #TYPE_ACTIVITY_PAUSED}, 114 * {@value #TYPE_ACTIVITY_STOPPED}, {@value #TYPE_ACTIVITY_DESTROYED} or 10000 if the Activity 115 * was started. 116 */ 117 @ActivityEventType getEventType()118 public int getEventType() { 119 return mType; 120 } 121 122 /** @hide */ getTypeAsString(@ctivityEventType int type)123 public static String getTypeAsString(@ActivityEventType int type) { 124 switch (type) { 125 case TYPE_ACTIVITY_RESUMED: 126 return "ACTIVITY_RESUMED"; 127 case TYPE_ACTIVITY_PAUSED: 128 return "ACTIVITY_PAUSED"; 129 case TYPE_ACTIVITY_STOPPED: 130 return "ACTIVITY_STOPPED"; 131 case TYPE_ACTIVITY_DESTROYED: 132 return "ACTIVITY_DESTROYED"; 133 case TYPE_ACTIVITY_STARTED: 134 return "ACTIVITY_STARTED"; 135 default: 136 return "UKNOWN_TYPE: " + type; 137 } 138 } 139 140 @NonNull 141 @Override toString()142 public String toString() { 143 return new StringBuilder("ActivityEvent[").append(mComponentName.toShortString()) 144 .append(", ActivityId: ").append(mActivityId) 145 .append("]:").append(getTypeAsString(mType)).toString(); 146 } 147 148 @Override describeContents()149 public int describeContents() { 150 return 0; 151 } 152 153 @Override writeToParcel(@onNull Parcel parcel, int flags)154 public void writeToParcel(@NonNull Parcel parcel, int flags) { 155 parcel.writeParcelable(mComponentName, flags); 156 parcel.writeInt(mType); 157 parcel.writeParcelable(mActivityId, flags); 158 } 159 160 public static final @android.annotation.NonNull Creator<ActivityEvent> CREATOR = 161 new Creator<ActivityEvent>() { 162 163 @Override 164 @NonNull 165 public ActivityEvent createFromParcel(@NonNull Parcel parcel) { 166 final ComponentName componentName = 167 parcel.readParcelable(null, ComponentName.class); 168 final int eventType = parcel.readInt(); 169 final ActivityId activityId = 170 parcel.readParcelable(null, ActivityId.class); 171 return new ActivityEvent(activityId, componentName, eventType); 172 } 173 174 @Override 175 @NonNull 176 public ActivityEvent[] newArray(int size) { 177 return new ActivityEvent[size]; 178 } 179 }; 180 } 181