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