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.premade; 18 19 import android.app.Activity; 20 import android.content.ComponentName; 21 import android.content.pm.ActivityInfo; 22 import android.content.pm.PackageManager; 23 import android.os.Bundle; 24 import android.os.PersistableBundle; 25 26 import com.android.eventlib.events.activities.ActivityCreatedEvent; 27 import com.android.eventlib.events.activities.ActivityDestroyedEvent; 28 import com.android.eventlib.events.activities.ActivityPausedEvent; 29 import com.android.eventlib.events.activities.ActivityRestartedEvent; 30 import com.android.eventlib.events.activities.ActivityResumedEvent; 31 import com.android.eventlib.events.activities.ActivityStartedEvent; 32 import com.android.eventlib.events.activities.ActivityStoppedEvent; 33 34 /** 35 * An {@link Activity} which logs events for all lifecycle events. 36 */ 37 public class EventLibActivity extends Activity { 38 private String mOverrideActivityClassName; 39 setOverrideActivityClassName(String overrideActivityClassName)40 public void setOverrideActivityClassName(String overrideActivityClassName) { 41 mOverrideActivityClassName = overrideActivityClassName; 42 } 43 44 /** 45 * Gets the class name of this activity. 46 * 47 * <p>If the class name has been overridden, that will be returned instead. 48 */ getClassName()49 public String getClassName() { 50 if (mOverrideActivityClassName != null) { 51 return mOverrideActivityClassName; 52 } 53 54 return EventLibActivity.class.getName(); 55 } 56 getComponentName()57 public ComponentName getComponentName() { 58 return new ComponentName(getApplication().getPackageName(), getClassName()); 59 } 60 61 /** Log a {@link ActivityCreatedEvent}. */ 62 @Override onCreate(Bundle savedInstanceState)63 protected void onCreate(Bundle savedInstanceState) { 64 super.onCreate(savedInstanceState); 65 logOnCreate(savedInstanceState, /* persistentState= */ null); 66 } 67 68 /** Log a {@link ActivityCreatedEvent}. */ 69 @Override onCreate(Bundle savedInstanceState, PersistableBundle persistentState)70 public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) { 71 super.onCreate(savedInstanceState, persistentState); 72 logOnCreate(savedInstanceState, persistentState); 73 } 74 75 private ActivityInfo mActivityInfo = null; 76 activityInfo()77 private ActivityInfo activityInfo() { 78 if (mActivityInfo != null) { 79 return mActivityInfo; 80 } 81 82 PackageManager packageManager = getPackageManager(); 83 try { 84 mActivityInfo = packageManager.getActivityInfo(getComponentName(), /* flags= */ 0); 85 } catch (PackageManager.NameNotFoundException e) { 86 throw new AssertionError("Cannot find activity", e); 87 } 88 89 return mActivityInfo; 90 } 91 logOnCreate(Bundle savedInstanceState, PersistableBundle persistentState)92 private void logOnCreate(Bundle savedInstanceState, PersistableBundle persistentState) { 93 ActivityCreatedEvent.logger(this, activityInfo(), savedInstanceState) 94 .setPersistentState(persistentState) 95 .log(); 96 } 97 98 /** Log a {@link ActivityStartedEvent}. */ 99 @Override onStart()100 protected void onStart() { 101 super.onStart(); 102 ActivityStartedEvent.logger(this, activityInfo()).log(); 103 } 104 105 /** Log a {@link ActivityRestartedEvent}. */ 106 @Override onRestart()107 protected void onRestart() { 108 super.onRestart(); 109 ActivityRestartedEvent.logger(this, activityInfo()).log(); 110 } 111 112 /** Log a {@link ActivityResumedEvent}. */ 113 @Override onResume()114 protected void onResume() { 115 super.onResume(); 116 ActivityResumedEvent.logger(this, activityInfo()).log(); 117 } 118 119 /** Log a {@link ActivityPausedEvent}. */ 120 @Override onPause()121 protected void onPause() { 122 super.onPause(); 123 ActivityPausedEvent.logger(this, activityInfo()).log(); 124 } 125 126 /** Log a {@link ActivityStoppedEvent}. */ 127 @Override onStop()128 protected void onStop() { 129 super.onStop(); 130 ActivityStoppedEvent.logger(this, activityInfo()).log(); 131 } 132 133 /** Log a {@link ActivityDestroyedEvent}. */ 134 @Override onDestroy()135 protected void onDestroy() { 136 super.onDestroy(); 137 ActivityDestroyedEvent.logger(this, activityInfo()) 138 .log(); 139 } 140 } 141