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 android.os.Bundle; 20 import android.util.Pair; 21 22 import androidx.fragment.app.FragmentActivity; 23 import androidx.lifecycle.Lifecycle; 24 import androidx.lifecycle.LifecycleObserver; 25 import androidx.lifecycle.LifecycleOwner; 26 import androidx.lifecycle.OnLifecycleEvent; 27 import androidx.lifecycle.ProcessLifecycleOwner; 28 29 import java.util.ArrayList; 30 import java.util.List; 31 import java.util.concurrent.CountDownLatch; 32 import java.util.concurrent.TimeUnit; 33 34 /** 35 * Activity for SimpleAppFullLifecycleTest 36 */ 37 public class SimpleAppLifecycleTestActivity extends FragmentActivity { 38 39 public enum TestEventType { 40 PROCESS_EVENT, 41 ACTIVITY_EVENT 42 } 43 44 private static final long TIMEOUT_SECS = 10; // secs 45 46 static class TestObserver implements LifecycleObserver { 47 48 private TestEventType mType; 49 TestObserver(TestEventType type)50 TestObserver(TestEventType type) { 51 mType = type; 52 } 53 54 @SuppressWarnings("unused") 55 @OnLifecycleEvent(Lifecycle.Event.ON_ANY) onEvent(LifecycleOwner provider, Lifecycle.Event event)56 void onEvent(LifecycleOwner provider, Lifecycle.Event event) { 57 sCollectedEvents.add(new Pair<>(mType, event)); 58 sLatch.countDown(); 59 } 60 } 61 62 static List<Pair<TestEventType, Lifecycle.Event>> sCollectedEvents = new ArrayList<>(); 63 static CountDownLatch sLatch = new CountDownLatch(11); 64 65 /** 66 * start process observer 67 */ startProcessObserver()68 public static void startProcessObserver() { 69 ProcessLifecycleOwner.get().getLifecycle().addObserver(sProcessObserver); 70 } 71 72 /** 73 * stop process observer 74 */ stopProcessObserver()75 public static void stopProcessObserver() { 76 ProcessLifecycleOwner.get().getLifecycle().removeObserver(sProcessObserver); 77 } 78 79 private static TestObserver sProcessObserver = new TestObserver(TestEventType.PROCESS_EVENT); 80 81 @Override onCreate(Bundle savedInstanceState)82 protected void onCreate(Bundle savedInstanceState) { 83 super.onCreate(savedInstanceState); 84 getLifecycle().addObserver(new TestObserver(TestEventType.ACTIVITY_EVENT)); 85 } 86 87 @Override onResume()88 protected void onResume() { 89 super.onResume(); 90 finish(); 91 } 92 93 /** 94 * returns collected events 95 */ awaitForEvents()96 public static List<Pair<TestEventType, Lifecycle.Event>> awaitForEvents() 97 throws InterruptedException { 98 boolean success = sLatch.await(TIMEOUT_SECS, TimeUnit.SECONDS); 99 return success ? sCollectedEvents : null; 100 } 101 } 102