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