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