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.app.Activity; 20 import android.os.Bundle; 21 22 import androidx.annotation.Nullable; 23 24 import java.util.concurrent.TimeUnit; 25 import java.util.concurrent.locks.Condition; 26 import java.util.concurrent.locks.Lock; 27 import java.util.concurrent.locks.ReentrantLock; 28 29 /** 30 * Activity which doesn't extend FragmentActivity, to test ProcessLifecycleOwner because it 31 * should work anyway. 32 */ 33 public class NonSupportActivity extends Activity { 34 35 private static final int TIMEOUT = 1; //secs 36 private final Lock mLock = new ReentrantLock(); 37 private Condition mIsResumedCondition = mLock.newCondition(); 38 private boolean mIsResumed = false; 39 40 @Override 41 protected void onCreate(@Nullable Bundle savedInstanceState) { 42 super.onCreate(savedInstanceState); 43 } 44 45 @Override 46 protected void onResume() { 47 super.onResume(); 48 mLock.lock(); 49 try { 50 mIsResumed = true; 51 mIsResumedCondition.signalAll(); 52 } finally { 53 mLock.unlock(); 54 } 55 } 56 57 @Override 58 protected void onPause() { 59 super.onPause(); 60 mLock.lock(); 61 try { 62 mIsResumed = false; 63 } finally { 64 mLock.unlock(); 65 } 66 } 67 68 /** 69 * awaits resumed state 70 * @return 71 * @throws InterruptedException 72 */ 73 public boolean awaitResumedState() throws InterruptedException { 74 mLock.lock(); 75 try { 76 while (!mIsResumed) { 77 if (!mIsResumedCondition.await(TIMEOUT, TimeUnit.SECONDS)) { 78 return false; 79 } 80 } 81 return true; 82 } finally { 83 mLock.unlock(); 84 } 85 } 86 } 87