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