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.TimeUnit 22 import java.util.concurrent.locks.ReentrantLock 23 import kotlin.concurrent.withLock 24 25 class UidStateForceActivity : Activity() { 26 companion object { 27 private val lock = ReentrantLock() 28 private val condition = lock.newCondition() 29 private var isActivityResumed = false 30 private var isActivityDestroyed = false; 31 32 var instance: UidStateForceActivity? = null 33 waitForResumednull34 fun waitForResumed() { 35 lock.withLock { 36 if (!isActivityResumed) { 37 if (!condition.await(10, TimeUnit.SECONDS)) { 38 throw RuntimeException("Activity never resumed") 39 } 40 } 41 } 42 } 43 waitForDestroyednull44 fun waitForDestroyed() { 45 lock.withLock { 46 while (!isActivityDestroyed) { 47 condition.await() 48 } 49 } 50 } 51 } 52 onCreatenull53 override fun onCreate(savedInstanceState: Bundle?) { 54 super.onCreate(savedInstanceState) 55 56 instance = this 57 } 58 onResumenull59 override fun onResume() { 60 super.onResume() 61 62 lock.withLock { 63 isActivityResumed = true 64 condition.signalAll() 65 } 66 } 67 onPausenull68 override fun onPause() { 69 super.onPause() 70 71 lock.withLock { 72 isActivityResumed = false 73 } 74 } 75 onDestroynull76 override fun onDestroy() { 77 super.onDestroy() 78 79 lock.withLock { 80 isActivityDestroyed = true 81 condition.signalAll() 82 } 83 } 84 } 85