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.bedstead.testapp; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.os.PersistableBundle; 22 23 import com.android.bedstead.nene.utils.Poll; 24 import com.android.eventlib.premade.EventLibActivity; 25 26 import java.util.Map; 27 import java.util.WeakHashMap; 28 29 /** 30 * An {@link Activity} which logs events for all lifecycle events and supports TestApp Features. 31 */ 32 public class BaseTestAppActivity extends EventLibActivity { 33 34 public static final String ACTIVITY_RESULT_KEY = "ACTIVITY_RESULT"; 35 36 private static Map<String, BaseTestAppActivity> sActivities = new WeakHashMap<>(); 37 38 /** 39 * Find an activity for the given class name. 40 * 41 * <p>This will throw an {@link IllegalStateException} if there is no existing activity for the 42 * class name. 43 * 44 * <p>This method is thread-safe 45 */ findActivity(String activityClassName)46 public static BaseTestAppActivity findActivity(String activityClassName) { 47 return Poll.forValue("activity for className", () -> { 48 synchronized (BaseTestAppActivity.class) { 49 return sActivities.get(activityClassName); 50 } 51 }).toNotBeNull() 52 .errorOnFail("No existing activity named " + activityClassName) 53 .await(); 54 } 55 56 @Override 57 protected void onCreate(Bundle savedInstanceState) { 58 synchronized (BaseTestAppActivity.class) { 59 sActivities.put(getClassName(), this); 60 } 61 super.onCreate(savedInstanceState); 62 63 forwardIntentResult(); 64 } 65 66 @Override 67 public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) { 68 synchronized (BaseTestAppActivity.class) { 69 sActivities.put(getClassName(), this); 70 } 71 super.onCreate(savedInstanceState, persistentState); 72 73 forwardIntentResult(); 74 } 75 76 @Override 77 protected void onStart() { 78 super.onStart(); 79 } 80 81 @Override 82 protected void onRestart() { 83 super.onRestart(); 84 } 85 86 @Override 87 protected void onResume() { 88 super.onResume(); 89 } 90 91 @Override 92 protected void onPause() { 93 super.onPause(); 94 } 95 96 @Override 97 protected void onStop() { 98 super.onStop(); 99 } 100 101 @Override 102 protected void onDestroy() { 103 super.onDestroy(); 104 } 105 106 // TODO(b/198280332): Remove this temporary solution to set return values for methods 107 private void forwardIntentResult() { 108 int result = getIntent().getIntExtra(ACTIVITY_RESULT_KEY, -1); 109 110 if (result != -1) { 111 setResult(result); 112 finish(); 113 } 114 } 115 } 116