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