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#onDestroy()} is called. 36 */ 37 public final class ActivityDestroyedEvent extends Event { 38 39 private static final long serialVersionUID = 1; 40 41 /** Begins a query for {@link ActivityDestroyedEvent} events. */ queryPackage(String packageName)42 public static ActivityDestroyedEventQuery queryPackage(String packageName) { 43 return new ActivityDestroyedEventQuery(packageName); 44 } 45 46 /** {@link EventLogsQuery} for {@link ActivityDestroyedEvent}. */ 47 public static final class ActivityDestroyedEventQuery 48 extends EventLogsQuery<ActivityDestroyedEvent, ActivityDestroyedEventQuery> { 49 50 private static final long serialVersionUID = 1; 51 52 ActivityQueryHelper<ActivityDestroyedEventQuery> mActivity = 53 new ActivityQueryHelper<>(this); 54 IntegerQuery<ActivityDestroyedEventQuery> mTaskId = new IntegerQueryHelper<>(this); 55 ActivityDestroyedEventQuery(String packageName)56 private ActivityDestroyedEventQuery(String packageName) { 57 super(ActivityDestroyedEvent.class, packageName); 58 } 59 60 /** Query {@link Activity}. */ 61 @CheckResult whereActivity()62 public ActivityQuery<ActivityDestroyedEventQuery> whereActivity() { 63 return mActivity; 64 } 65 66 /** Query {@code taskId}. */ 67 @CheckResult whereTaskId()68 public IntegerQuery<ActivityDestroyedEventQuery> whereTaskId() { 69 return mTaskId; 70 } 71 72 @Override filter(ActivityDestroyedEvent event)73 protected boolean filter(ActivityDestroyedEvent 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(ActivityDestroyedEvent.class, this) 86 .field("activity", mActivity) 87 .field("taskId", mTaskId) 88 .toString(); 89 } 90 } 91 92 /** Begins logging a {@link ActivityDestroyedEvent}. */ logger(Activity activity, android.content.pm.ActivityInfo activityInfo)93 public static ActivityDestroyedEventLogger logger(Activity activity, android.content.pm.ActivityInfo activityInfo) { 94 return new ActivityDestroyedEventLogger(activity, activityInfo); 95 } 96 97 /** {@link EventLogger} for {@link ActivityDestroyedEvent}. */ 98 public static final class ActivityDestroyedEventLogger 99 extends EventLogger<ActivityDestroyedEvent> { ActivityDestroyedEventLogger(Activity activity, android.content.pm.ActivityInfo activityInfo)100 private ActivityDestroyedEventLogger(Activity activity, android.content.pm.ActivityInfo activityInfo) { 101 super(activity, new ActivityDestroyedEvent()); 102 setActivity(activityInfo); 103 setTaskId(activity.getTaskId()); 104 } 105 106 /** Sets the {@link Activity} being destroyed. */ 107 @CanIgnoreReturnValue setActivity(android.content.pm.ActivityInfo activity)108 public ActivityDestroyedEventLogger 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 ActivityDestroyedEventLogger 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 "ActivityDestroyedEvent{" 137 + ", activity=" + mActivity 138 + ", taskId=" + mTaskId 139 + ", packageName='" + mPackageName + "'" 140 + ", timestamp=" + mTimestamp 141 + "}"; 142 } 143 } 144