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 
30 /**
31  * Event logged when {@link Activity#onRestart()} is called.
32  */
33 public final class ActivityRestartedEvent extends Event {
34 
35     private static final long serialVersionUID = 1;
36 
37     /** Begins a query for {@link ActivityRestartedEvent} events. */
queryPackage(String packageName)38     public static ActivityRestartedEventQuery queryPackage(String packageName) {
39         return new ActivityRestartedEventQuery(packageName);
40     }
41 
42     /** {@link EventLogsQuery} for {@link ActivityRestartedEvent}. */
43     public static final class ActivityRestartedEventQuery
44             extends EventLogsQuery<ActivityRestartedEvent, ActivityRestartedEventQuery> {
45 
46         private static final long serialVersionUID = 1;
47 
48         ActivityQueryHelper<ActivityRestartedEventQuery> mActivity =
49                 new ActivityQueryHelper<>(this);
50 
ActivityRestartedEventQuery(String packageName)51         private ActivityRestartedEventQuery(String packageName) {
52             super(ActivityRestartedEvent.class, packageName);
53         }
54 
55         /** Query {@link Activity}. */
56         @CheckResult
whereActivity()57         public ActivityQuery<ActivityRestartedEventQuery> whereActivity() {
58             return mActivity;
59         }
60 
61         @Override
filter(ActivityRestartedEvent event)62         protected boolean filter(ActivityRestartedEvent event) {
63             if (!mActivity.matches(event.mActivity)) {
64                 return false;
65             }
66             return true;
67         }
68 
69         @Override
describeQuery(String fieldName)70         public String describeQuery(String fieldName) {
71             return toStringBuilder(ActivityRestartedEvent.class, this)
72                     .field("activity", mActivity)
73                     .toString();
74         }
75     }
76 
77     /** Begins logging a {@link ActivityRestartedEvent}. */
logger(Activity activity, android.content.pm.ActivityInfo activityInfo)78     public static ActivityRestartedEventLogger logger(Activity activity, android.content.pm.ActivityInfo activityInfo) {
79         return new ActivityRestartedEventLogger(activity, activityInfo);
80     }
81 
82     /** {@link EventLogger} for {@link ActivityRestartedEvent}. */
83     public static final class ActivityRestartedEventLogger
84             extends EventLogger<ActivityRestartedEvent> {
ActivityRestartedEventLogger(Activity activity, android.content.pm.ActivityInfo activityInfo)85         private ActivityRestartedEventLogger(Activity activity, android.content.pm.ActivityInfo activityInfo) {
86             super(activity, new ActivityRestartedEvent());
87             setActivity(activityInfo);
88         }
89 
90         /** Sets the {@link Activity} being destroyed. */
setActivity(android.content.pm.ActivityInfo activity)91         public ActivityRestartedEventLogger setActivity(android.content.pm.ActivityInfo activity) {
92             mEvent.mActivity = ActivityInfo.builder(activity).build();
93             return this;
94         }
95     }
96 
97     protected ActivityInfo mActivity;
98 
99     /** Information about the {@link Activity} destroyed. */
activity()100     public ActivityInfo activity() {
101         return mActivity;
102     }
103 
104     @Override
toString()105     public String toString() {
106         return "ActivityRestartedEvent{"
107                 + ", activity=" + mActivity
108                 + ", packageName='" + mPackageName + "'"
109                 + ", timestamp=" + mTimestamp
110                 + "}";
111     }
112 }
113