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 import android.os.Bundle; 21 import android.os.PersistableBundle; 22 23 import androidx.annotation.CheckResult; 24 25 import com.android.eventlib.Event; 26 import com.android.eventlib.EventLogger; 27 import com.android.eventlib.EventLogsQuery; 28 import com.android.queryable.info.ActivityInfo; 29 import com.android.queryable.queries.ActivityQuery; 30 import com.android.queryable.queries.ActivityQueryHelper; 31 import com.android.queryable.queries.BundleQueryHelper; 32 import com.android.queryable.queries.IntegerQuery; 33 import com.android.queryable.queries.IntegerQueryHelper; 34 import com.android.queryable.queries.PersistableBundleQuery; 35 import com.android.queryable.queries.PersistableBundleQueryHelper; 36 import com.android.queryable.util.SerializableParcelWrapper; 37 38 import com.google.errorprone.annotations.CanIgnoreReturnValue; 39 40 /** 41 * Event logged when {@link Activity#onCreate(Bundle)} or 42 * {@link Activity#onCreate(Bundle, PersistableBundle)} is called. 43 */ 44 public final class ActivityCreatedEvent extends Event { 45 46 private static final long serialVersionUID = 1; 47 48 /** Begins a query for {@link ActivityCreatedEvent} events. */ queryPackage(String packageName)49 public static ActivityCreatedEventQuery queryPackage(String packageName) { 50 return new ActivityCreatedEventQuery(packageName); 51 } 52 53 /** {@link EventLogsQuery} for {@link ActivityCreatedEvent}. */ 54 public static final class ActivityCreatedEventQuery 55 extends EventLogsQuery<ActivityCreatedEvent, ActivityCreatedEventQuery> { 56 57 private static final long serialVersionUID = 1; 58 59 ActivityQueryHelper<ActivityCreatedEventQuery> mActivity = new ActivityQueryHelper<>(this); 60 BundleQueryHelper<ActivityCreatedEventQuery> mSavedInstanceState = 61 new BundleQueryHelper<>(this); 62 PersistableBundleQueryHelper<ActivityCreatedEventQuery> mPersistentState = 63 new PersistableBundleQueryHelper<>(this); 64 IntegerQuery<ActivityCreatedEventQuery> mTaskId = new IntegerQueryHelper<>(this); 65 ActivityCreatedEventQuery(String packageName)66 private ActivityCreatedEventQuery(String packageName) { 67 super(ActivityCreatedEvent.class, packageName); 68 } 69 70 /** 71 * Query {@code savedInstanceState} {@link Bundle} passed into 72 * {@link Activity#onCreate(Bundle)} or 73 * {@link Activity#onCreate(Bundle, PersistableBundle)}. 74 */ 75 @CheckResult whereSavedInstanceState()76 public BundleQueryHelper<ActivityCreatedEventQuery> whereSavedInstanceState() { 77 return mSavedInstanceState; 78 } 79 80 /** 81 * Query {@code persistentState} {@link PersistableBundle} passed into 82 * {@link Activity#onCreate(Bundle, PersistableBundle)}. 83 */ 84 @CheckResult wherePersistentState()85 public PersistableBundleQuery<ActivityCreatedEventQuery> wherePersistentState() { 86 return mPersistentState; 87 } 88 89 /** Query {@link Activity}. */ 90 @CheckResult whereActivity()91 public ActivityQuery<ActivityCreatedEventQuery> whereActivity() { 92 return mActivity; 93 } 94 95 /** Query {@code taskId}. */ 96 @CheckResult whereTaskId()97 public IntegerQuery<ActivityCreatedEventQuery> whereTaskId() { 98 return mTaskId; 99 } 100 101 @Override filter(ActivityCreatedEvent event)102 protected boolean filter(ActivityCreatedEvent event) { 103 if (!mSavedInstanceState.matches(event.mSavedInstanceState)) { 104 return false; 105 } 106 if (!mPersistentState.matches(event.mPersistentState)) { 107 return false; 108 } 109 if (!mActivity.matches(event.mActivity)) { 110 return false; 111 } 112 if (!mTaskId.matches(event.mTaskId)) { 113 return false; 114 } 115 return true; 116 } 117 118 @Override describeQuery(String fieldName)119 public String describeQuery(String fieldName) { 120 return toStringBuilder(ActivityCreatedEvent.class, this) 121 .field("savedInstanceState", mSavedInstanceState) 122 .field("persistentState", mPersistentState) 123 .field("activity", mActivity) 124 .field("taskId", mTaskId) 125 .toString(); 126 } 127 } 128 129 /** Begins logging a {@link ActivityCreatedEvent}. */ logger(Activity activity, android.content.pm.ActivityInfo activityInfo, Bundle savedInstanceState)130 public static ActivityCreatedEventLogger logger(Activity activity, android.content.pm.ActivityInfo activityInfo, Bundle savedInstanceState) { 131 return new ActivityCreatedEventLogger(activity, activityInfo, savedInstanceState); 132 } 133 134 /** {@link EventLogger} for {@link ActivityCreatedEvent}. */ 135 public static final class ActivityCreatedEventLogger extends EventLogger<ActivityCreatedEvent> { ActivityCreatedEventLogger(Activity activity, android.content.pm.ActivityInfo activityInfo, Bundle savedInstanceState)136 private ActivityCreatedEventLogger(Activity activity, android.content.pm.ActivityInfo activityInfo, Bundle savedInstanceState) { 137 super(activity, new ActivityCreatedEvent()); 138 setSavedInstanceState(savedInstanceState); 139 setTaskId(activity.getTaskId()); 140 setActivity(activityInfo); 141 } 142 143 @CanIgnoreReturnValue setActivity(android.content.pm.ActivityInfo activityInfo)144 public ActivityCreatedEventLogger setActivity(android.content.pm.ActivityInfo activityInfo) { 145 mEvent.mActivity = ActivityInfo.builder() 146 .activityClass(activityInfo.name) 147 .exported(activityInfo.exported) 148 .build(); 149 return this; 150 } 151 152 @CanIgnoreReturnValue setSavedInstanceState(Bundle savedInstanceState)153 public ActivityCreatedEventLogger setSavedInstanceState(Bundle savedInstanceState) { 154 mEvent.mSavedInstanceState = new SerializableParcelWrapper<>(savedInstanceState); 155 return this; 156 } 157 setPersistentState(PersistableBundle persistentState)158 public ActivityCreatedEventLogger setPersistentState(PersistableBundle persistentState) { 159 mEvent.mPersistentState = new SerializableParcelWrapper<>(persistentState); 160 return this; 161 } 162 163 /** Sets the task ID for the activity. */ 164 @CanIgnoreReturnValue setTaskId(int taskId)165 public ActivityCreatedEventLogger setTaskId(int taskId) { 166 mEvent.mTaskId = taskId; 167 return this; 168 } 169 } 170 171 protected SerializableParcelWrapper<Bundle> mSavedInstanceState; 172 protected SerializableParcelWrapper<PersistableBundle> mPersistentState; 173 protected ActivityInfo mActivity; 174 protected int mTaskId; 175 176 /** 177 * The {@code savedInstanceState} {@link Bundle} passed into 178 * {@link Activity#onCreate(Bundle)} or 179 * {@link Activity#onCreate(Bundle, PersistableBundle)}. 180 */ savedInstanceState()181 public Bundle savedInstanceState() { 182 if (mSavedInstanceState == null) { 183 return null; 184 } 185 return mSavedInstanceState.get(); 186 } 187 188 /** 189 * The {@code persistentState} {@link PersistableBundle} passed into 190 * {@link Activity#onCreate(Bundle, PersistableBundle)}. 191 */ persistentState()192 public PersistableBundle persistentState() { 193 if (mPersistentState == null) { 194 return null; 195 } 196 return mPersistentState.get(); 197 } 198 199 /** Information about the {@link Activity} started. */ activity()200 public ActivityInfo activity() { 201 return mActivity; 202 } 203 204 /** The Task ID of the Activity. */ taskId()205 public int taskId() { 206 return mTaskId; 207 } 208 209 @Override toString()210 public String toString() { 211 return "ActivityCreatedEvent{" 212 + " savedInstanceState=" + savedInstanceState() 213 + ", persistentState=" + persistentState() 214 + ", activity=" + mActivity 215 + ", taskId=" + mTaskId 216 + ", packageName='" + mPackageName + "'" 217 + ", timestamp=" + mTimestamp 218 + "}"; 219 } 220 } 221