blob: 5407c1d9b832c001f2045b4e37f4f45477f673db [file] [log] [blame]
Felipe Leme141864d2019-02-27 17:01:51 -08001/*
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
17package android.service.contentcapture;
18
19import android.annotation.IntDef;
20import android.annotation.NonNull;
21import android.annotation.SystemApi;
22import android.annotation.TestApi;
23import android.app.usage.UsageEvents.Event;
24import android.content.ComponentName;
25import android.os.Parcel;
26import android.os.Parcelable;
27
28import java.lang.annotation.Retention;
29import 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
38public 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
Felipe Leme77670592019-03-14 09:03:29 -070050 /**
51 * The activity stopped.
52 */
53 public static final int TYPE_ACTIVITY_STOPPED = Event.ACTIVITY_STOPPED;
54
Felipe Leme141864d2019-02-27 17:01:51 -080055 /** @hide */
56 @IntDef(prefix = { "TYPE_" }, value = {
57 TYPE_ACTIVITY_RESUMED,
Felipe Leme77670592019-03-14 09:03:29 -070058 TYPE_ACTIVITY_PAUSED,
59 TYPE_ACTIVITY_STOPPED
Felipe Leme141864d2019-02-27 17:01:51 -080060 })
61 @Retention(RetentionPolicy.SOURCE)
62 public @interface ActivityEventType{}
63
64 private final @NonNull ComponentName mComponentName;
65 private final @ActivityEventType int mType;
66
67 /** @hide */
68 public ActivityEvent(@NonNull ComponentName componentName, @ActivityEventType int type) {
69 mComponentName = componentName;
70 mType = type;
71 }
72
73 /**
74 * Gests the {@link ComponentName} of the activity associated with the event.
75 */
76 @NonNull
77 public ComponentName getComponentName() {
78 return mComponentName;
79 }
80
81 /**
82 * Gets the event type.
83 *
84 * @return either {@link #TYPE_ACTIVITY_RESUMED} or {@value #TYPE_ACTIVITY_PAUSED}.
85 */
86 @ActivityEventType
87 public int getEventType() {
88 return mType;
89 }
90
91 /** @hide */
92 public static String getTypeAsString(@ActivityEventType int type) {
93 switch (type) {
94 case TYPE_ACTIVITY_RESUMED:
95 return "ACTIVITY_RESUMED";
96 case TYPE_ACTIVITY_PAUSED:
97 return "ACTIVITY_PAUSED";
Felipe Leme77670592019-03-14 09:03:29 -070098 case TYPE_ACTIVITY_STOPPED:
99 return "ACTIVITY_STOPPED";
Felipe Leme141864d2019-02-27 17:01:51 -0800100 default:
101 return "UKNOWN_TYPE: " + type;
102 }
103 }
104
105 @Override
106 public String toString() {
107 return new StringBuilder("ActivityEvent[").append(mComponentName.toShortString())
108 .append("]:").append(getTypeAsString(mType)).toString();
109 }
110
111 @Override
112 public int describeContents() {
113 return 0;
114 }
115
116 @Override
117 public void writeToParcel(@NonNull Parcel parcel, int flags) {
118 parcel.writeParcelable(mComponentName, flags);
119 parcel.writeInt(mType);
120 }
121
122 public static final @android.annotation.NonNull Creator<ActivityEvent> CREATOR =
123 new Creator<ActivityEvent>() {
124
125 @Override
126 @NonNull
127 public ActivityEvent createFromParcel(@NonNull Parcel parcel) {
128 final ComponentName componentName = parcel.readParcelable(null);
129 final int eventType = parcel.readInt();
130 return new ActivityEvent(componentName, eventType);
131 }
132
133 @Override
134 @NonNull
135 public ActivityEvent[] newArray(int size) {
136 return new ActivityEvent[size];
137 }
138 };
139}