1 /* 2 * Copyright (C) 2023 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 android.server.wm.activity.lifecycle; 18 19 import static android.server.wm.activity.lifecycle.LifecycleConstants.EXTRA_ACTIVITY_ON_USER_LEAVE_HINT; 20 import static android.server.wm.activity.lifecycle.LifecycleConstants.EXTRA_FINISH_IN_ON_CREATE; 21 import static android.server.wm.activity.lifecycle.LifecycleConstants.EXTRA_FINISH_IN_ON_PAUSE; 22 import static android.server.wm.activity.lifecycle.LifecycleConstants.EXTRA_FINISH_IN_ON_RESUME; 23 import static android.server.wm.activity.lifecycle.LifecycleConstants.EXTRA_FINISH_IN_ON_START; 24 import static android.server.wm.activity.lifecycle.LifecycleConstants.EXTRA_FINISH_IN_ON_STOP; 25 import static android.server.wm.activity.lifecycle.LifecycleConstants.EXTRA_START_ACTIVITY_IN_ON_CREATE; 26 import static android.server.wm.activity.lifecycle.LifecycleConstants.EXTRA_START_ACTIVITY_WHEN_IDLE; 27 import static android.server.wm.activity.lifecycle.LifecycleConstants.ON_CREATE; 28 import static android.server.wm.activity.lifecycle.LifecycleConstants.ON_DESTROY; 29 import static android.server.wm.activity.lifecycle.LifecycleConstants.ON_PAUSE; 30 import static android.server.wm.activity.lifecycle.LifecycleConstants.ON_RESTART; 31 import static android.server.wm.activity.lifecycle.LifecycleConstants.ON_RESUME; 32 import static android.server.wm.activity.lifecycle.LifecycleConstants.ON_START; 33 import static android.server.wm.activity.lifecycle.LifecycleConstants.ON_STOP; 34 import static android.server.wm.activity.lifecycle.LifecycleConstants.ON_USER_LEAVE_HINT; 35 36 import android.app.Activity; 37 import android.content.Intent; 38 import android.net.Uri; 39 import android.os.Bundle; 40 import android.os.Looper; 41 42 /** Base activity that only tracks fundamental activity lifecycle states. */ 43 public class LifecycleTrackingActivity extends Activity { 44 EventLog.EventLogClient mEventLogClient; 45 46 @Override onCreate(Bundle savedInstanceState)47 protected void onCreate(Bundle savedInstanceState) { 48 super.onCreate(savedInstanceState); 49 mEventLogClient = EventLog.EventLogClient.create(this.getClass().getCanonicalName(), this, 50 Uri.parse("content://android.server.wm.lifecycle.logprovider")); 51 mEventLogClient.onCallback(ON_CREATE); 52 53 final Intent intent = getIntent(); 54 final Intent startOnCreate = 55 intent.getParcelableExtra(EXTRA_START_ACTIVITY_IN_ON_CREATE); 56 if (startOnCreate != null) { 57 startActivity(startOnCreate); 58 } 59 60 final Intent startOnIdle = intent.getParcelableExtra(EXTRA_START_ACTIVITY_WHEN_IDLE); 61 if (startOnIdle != null) { 62 Looper.getMainLooper().getQueue().addIdleHandler(() -> { 63 startActivity(startOnIdle); 64 return false; 65 }); 66 } 67 68 if (intent.getBooleanExtra(EXTRA_FINISH_IN_ON_CREATE, false)) { 69 finish(); 70 } 71 } 72 73 @Override onStart()74 protected void onStart() { 75 super.onStart(); 76 mEventLogClient.onCallback(ON_START); 77 78 if (getIntent().getBooleanExtra(EXTRA_FINISH_IN_ON_START, false)) { 79 finish(); 80 } 81 } 82 83 @Override onResume()84 protected void onResume() { 85 super.onResume(); 86 mEventLogClient.onCallback(ON_RESUME); 87 88 final Intent intent = getIntent(); 89 if (intent.getBooleanExtra(EXTRA_FINISH_IN_ON_RESUME, false)) { 90 finish(); 91 } 92 } 93 94 @Override onPause()95 protected void onPause() { 96 super.onPause(); 97 mEventLogClient.onCallback(ON_PAUSE); 98 99 if (getIntent().getBooleanExtra(EXTRA_FINISH_IN_ON_PAUSE, false)) { 100 finish(); 101 } 102 } 103 104 @Override onStop()105 protected void onStop() { 106 super.onStop(); 107 mEventLogClient.onCallback(ON_STOP); 108 109 if (getIntent().getBooleanExtra(EXTRA_FINISH_IN_ON_STOP, false)) { 110 finish(); 111 } 112 } 113 114 @Override onDestroy()115 protected void onDestroy() { 116 super.onDestroy(); 117 mEventLogClient.onCallback(ON_DESTROY); 118 mEventLogClient.close(); 119 } 120 121 @Override onRestart()122 protected void onRestart() { 123 super.onRestart(); 124 mEventLogClient.onCallback(ON_RESTART); 125 } 126 127 @Override onUserLeaveHint()128 protected void onUserLeaveHint() { 129 super.onUserLeaveHint(); 130 131 if (getIntent().getBooleanExtra(EXTRA_ACTIVITY_ON_USER_LEAVE_HINT, false)) { 132 mEventLogClient.onCallback(ON_USER_LEAVE_HINT); 133 } 134 } 135 } 136