1 /* 2 * Copyright (C) 2017 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 androidx.lifecycle.testapp; 18 19 import static androidx.lifecycle.testapp.TestEvent.OWNER_CALLBACK; 20 21 import android.app.Activity; 22 import android.os.Bundle; 23 24 import androidx.annotation.NonNull; 25 import androidx.core.util.Pair; 26 import androidx.lifecycle.Lifecycle; 27 import androidx.lifecycle.LifecycleRegistry; 28 import androidx.lifecycle.LifecycleRegistryOwner; 29 30 import java.util.ArrayList; 31 import java.util.List; 32 import java.util.concurrent.CountDownLatch; 33 34 /** 35 * LifecycleRegistryOwner that extends framework activity. 36 */ 37 @SuppressWarnings("deprecation") 38 public class FrameworkLifecycleRegistryActivity extends Activity implements 39 LifecycleRegistryOwner, CollectingLifecycleOwner { 40 private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this); 41 42 @NonNull 43 @Override getLifecycle()44 public LifecycleRegistry getLifecycle() { 45 return mLifecycleRegistry; 46 } 47 48 private List<Pair<TestEvent, Lifecycle.Event>> mCollectedEvents = new ArrayList<>(); 49 private TestObserver mTestObserver = new TestObserver(mCollectedEvents); 50 private CountDownLatch mLatch = new CountDownLatch(1); 51 52 @Override onCreate(Bundle savedInstanceState)53 protected void onCreate(Bundle savedInstanceState) { 54 super.onCreate(savedInstanceState); 55 mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Lifecycle.Event.ON_CREATE)); 56 getLifecycle().addObserver(mTestObserver); 57 } 58 59 @Override onStart()60 protected void onStart() { 61 super.onStart(); 62 mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Lifecycle.Event.ON_START)); 63 } 64 65 @Override onResume()66 protected void onResume() { 67 super.onResume(); 68 mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Lifecycle.Event.ON_RESUME)); 69 } 70 71 @Override onDestroy()72 protected void onDestroy() { 73 super.onDestroy(); 74 mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Lifecycle.Event.ON_DESTROY)); 75 mLatch.countDown(); 76 } 77 78 @Override onStop()79 protected void onStop() { 80 super.onStop(); 81 mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Lifecycle.Event.ON_STOP)); 82 } 83 84 @Override onPause()85 protected void onPause() { 86 super.onPause(); 87 mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Lifecycle.Event.ON_PAUSE)); 88 } 89 90 @Override copyCollectedEvents()91 public List<Pair<TestEvent, Lifecycle.Event>> copyCollectedEvents() { 92 return new ArrayList<>(mCollectedEvents); 93 } 94 } 95