1 /*
2  * Copyright (C) 2021 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 com.android.eventlib.events.activities;
18 
19 import android.app.Activity;
20 
21 import androidx.annotation.CheckResult;
22 
23 import com.android.eventlib.Event;
24 import com.android.eventlib.EventLogger;
25 import com.android.eventlib.EventLogsQuery;
26 import com.android.queryable.info.ActivityInfo;
27 import com.android.queryable.queries.ActivityQuery;
28 import com.android.queryable.queries.ActivityQueryHelper;
29 import com.android.queryable.queries.IntegerQuery;
30 import com.android.queryable.queries.IntegerQueryHelper;
31 
32 import com.google.errorprone.annotations.CanIgnoreReturnValue;
33 
34 /**
35  * Event logged when {@link Activity#onStop()} is called.
36  */
37 public final class ActivityStoppedEvent extends Event {
38 
39     private static final long serialVersionUID = 1;
40 
41     /** Begins a query for {@link ActivityStoppedEvent} events. */
queryPackage(String packageName)42     public static ActivityStoppedEventQuery queryPackage(String packageName) {
43         return new ActivityStoppedEventQuery(packageName);
44     }
45 
46     /** {@link EventLogsQuery} for {@link ActivityStoppedEvent}. */
47     public static final class ActivityStoppedEventQuery
48             extends EventLogsQuery<ActivityStoppedEvent, ActivityStoppedEventQuery> {
49 
50         private static final long serialVersionUID = 1;
51 
52         ActivityQueryHelper<ActivityStoppedEventQuery> mActivity = new ActivityQueryHelper<>(this);
53         IntegerQuery<ActivityStoppedEventQuery> mTaskId = new IntegerQueryHelper<>(this);
54 
ActivityStoppedEventQuery(String packageName)55         private ActivityStoppedEventQuery(String packageName) {
56             super(ActivityStoppedEvent.class, packageName);
57         }
58 
59         /** Query {@link Activity}. */
60         @CheckResult
whereActivity()61         public ActivityQuery<ActivityStoppedEventQuery> whereActivity() {
62             return mActivity;
63         }
64 
65         /** Query {@code taskId}. */
66         @CheckResult
whereTaskId()67         public IntegerQuery<ActivityStoppedEventQuery> whereTaskId() {
68             return mTaskId;
69         }
70 
71         @Override
filter(ActivityStoppedEvent event)72         protected boolean filter(ActivityStoppedEvent event) {
73             if (!mActivity.matches(event.mActivity)) {
74                 return false;
75             }
76             if (!mTaskId.matches(event.mTaskId)) {
77                 return false;
78             }
79             return true;
80         }
81 
82         @Override
describeQuery(String fieldName)83         public String describeQuery(String fieldName) {
84             return toStringBuilder(ActivityStoppedEvent.class, this)
85                     .field("activity", mActivity)
86                     .field("taskId", mTaskId)
87                     .toString();
88         }
89     }
90 
91     /** Begins logging a {@link ActivityStoppedEvent}. */
logger(Activity activity, android.content.pm.ActivityInfo activityInfo)92     public static ActivityStoppedEventLogger logger(Activity activity, android.content.pm.ActivityInfo activityInfo) {
93         return new ActivityStoppedEventLogger(activity, activityInfo);
94     }
95 
96     /** {@link EventLogger} for {@link ActivityStoppedEvent}. */
97     public static final class ActivityStoppedEventLogger extends EventLogger<ActivityStoppedEvent> {
ActivityStoppedEventLogger(Activity activity, android.content.pm.ActivityInfo activityInfo)98         private ActivityStoppedEventLogger(Activity activity, android.content.pm.ActivityInfo activityInfo) {
99             super(activity, new ActivityStoppedEvent());
100             setActivity(activityInfo);
101             setTaskId(activity.getTaskId());
102         }
103 
104         /** Sets the {@link Activity} being stopped. */
105         @CanIgnoreReturnValue
setActivity(android.content.pm.ActivityInfo activity)106         public ActivityStoppedEventLogger setActivity(android.content.pm.ActivityInfo activity) {
107             mEvent.mActivity = ActivityInfo.builder(activity).build();
108             return this;
109         }
110 
111         /** Sets the task ID for the activity. */
112         @CanIgnoreReturnValue
setTaskId(int taskId)113         public ActivityStoppedEventLogger setTaskId(int taskId) {
114             mEvent.mTaskId = taskId;
115             return this;
116         }
117     }
118 
119     protected ActivityInfo mActivity;
120     protected int mTaskId;
121 
122     /** Information about the {@link Activity} stopped. */
activity()123     public ActivityInfo activity() {
124         return mActivity;
125     }
126 
127     /** The Task ID of the Activity. */
taskId()128     public int taskId() {
129         return mTaskId;
130     }
131 
132     @Override
toString()133     public String toString() {
134         return "ActivityStoppedEvent{"
135                 + ", activity=" + mActivity
136                 + ", taskId=" + mTaskId
137                 + ", packageName='" + mPackageName + "'"
138                 + ", timestamp=" + mTimestamp
139                 + "}";
140     }
141 }
142