1 /* 2 * Copyright (C) 2008 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.view.cts; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.os.SystemClock; 22 import android.util.Log; 23 import android.view.cts.R; 24 25 public class ViewTestCtsActivity extends Activity { 26 private boolean mHasWindowFocus = false; 27 private Object mHasWindowFocusLock = new Object(); 28 29 @Override onCreate(Bundle icicle)30 protected void onCreate(Bundle icicle) { 31 super.onCreate(icicle); 32 setContentView(R.layout.view_layout); 33 } 34 35 @Override onWindowFocusChanged(boolean hasFocus)36 public void onWindowFocusChanged(boolean hasFocus) { 37 super.onWindowFocusChanged(hasFocus); 38 if (!hasFocus) { 39 Log.w("ViewTestCtsActivity", "ViewTestCtsActivity lost window focus"); 40 } 41 synchronized(mHasWindowFocusLock) { 42 mHasWindowFocus = hasFocus; 43 mHasWindowFocusLock.notify(); 44 } 45 } 46 47 /** 48 * Blocks the calling thread until the {@link ViewTestCtsActivity} has window focus or the 49 * specified duration (in milliseconds) has passed. 50 */ waitForWindowFocus(long durationMillis)51 public boolean waitForWindowFocus(long durationMillis) { 52 long elapsedMillis = SystemClock.elapsedRealtime(); 53 synchronized(mHasWindowFocusLock) { 54 mHasWindowFocus = hasWindowFocus(); 55 while (!mHasWindowFocus && durationMillis > 0) { 56 long newElapsedMillis = SystemClock.elapsedRealtime(); 57 durationMillis -= (newElapsedMillis - elapsedMillis); 58 elapsedMillis = newElapsedMillis; 59 if (durationMillis > 0) { 60 try { 61 mHasWindowFocusLock.wait(durationMillis); 62 } catch (InterruptedException e) { 63 } 64 } 65 } 66 return mHasWindowFocus; 67 } 68 } 69 } 70