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
17package androidx.lifecycle.testapp;
18
19import android.os.Bundle;
20import android.util.Pair;
21
22import androidx.fragment.app.FragmentActivity;
23import androidx.lifecycle.Lifecycle;
24import androidx.lifecycle.LifecycleObserver;
25import androidx.lifecycle.LifecycleOwner;
26import androidx.lifecycle.OnLifecycleEvent;
27import androidx.lifecycle.ProcessLifecycleOwner;
28
29import java.util.ArrayList;
30import java.util.List;
31import java.util.concurrent.CountDownLatch;
32import java.util.concurrent.TimeUnit;
33
34/**
35 * Activity for SimpleAppFullLifecycleTest
36 */
37public 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
50        TestObserver(TestEventType type) {
51            mType = type;
52        }
53
54        @SuppressWarnings("unused")
55        @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
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     */
68    public static void startProcessObserver() {
69        ProcessLifecycleOwner.get().getLifecycle().addObserver(sProcessObserver);
70    }
71
72    /**
73     * stop process observer
74     */
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
82    protected void onCreate(Bundle savedInstanceState) {
83        super.onCreate(savedInstanceState);
84        getLifecycle().addObserver(new TestObserver(TestEventType.ACTIVITY_EVENT));
85    }
86
87    @Override
88    protected void onResume() {
89        super.onResume();
90        finish();
91    }
92
93    /**
94     * returns collected events
95     */
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