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