1 /*
2  * Copyright (C) 2019 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 android.app.appops.cts
18 
19 import android.app.Activity
20 import android.os.Bundle
21 import java.util.concurrent.locks.ReentrantLock
22 import kotlin.concurrent.withLock
23 
24 class UidStateForceActivity : Activity() {
25     companion object {
26         private val lock = ReentrantLock()
27         private val condition = lock.newCondition()
28         private var isActivityResumed = false
29         private var isActivityDestroyed = false;
30 
31         var instance: UidStateForceActivity? = null
32 
waitForResumednull33         fun waitForResumed() {
34             lock.withLock {
35                 while (!isActivityResumed) {
36                     condition.await()
37                 }
38             }
39         }
40 
waitForDestroyednull41         fun waitForDestroyed() {
42             lock.withLock {
43                 while (!isActivityDestroyed) {
44                     condition.await()
45                 }
46             }
47         }
48     }
49 
onCreatenull50     override fun onCreate(savedInstanceState: Bundle?) {
51         super.onCreate(savedInstanceState)
52 
53         instance = this
54     }
55 
onResumenull56     override fun onResume() {
57         super.onResume()
58 
59         lock.withLock {
60             isActivityResumed = true
61             condition.signalAll()
62         }
63     }
64 
onPausenull65     override fun onPause() {
66         super.onPause()
67 
68         lock.withLock {
69             isActivityResumed = false
70         }
71     }
72 
onDestroynull73     override fun onDestroy() {
74         super.onDestroy()
75 
76         lock.withLock {
77             isActivityDestroyed = true
78             condition.signalAll()
79         }
80     }
81 }
82