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