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