1 /* 2 * Copyright (C) 2015 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.widget; 18 19 import com.android.frameworks.coretests.R; 20 21 import android.app.Activity; 22 import android.os.Bundle; 23 import android.os.SystemClock; 24 import android.util.Log; 25 26 /** 27 * An activity for testing the TextView widget. 28 * 29 * This class is copied from {@link android.text.method.cts.KeyListenerCtsActivity} in 30 * CtsTextTestCase. The original class is located at 31 * cts/tests/tests/text/src/android/text/method/cts/KeyListenerCtsActivity.java 32 */ 33 public class TextViewActivity extends Activity { 34 private boolean mHasWindowFocus = false; 35 private Object mHasWindowFocusLock = new Object(); 36 37 @Override onCreate(Bundle savedInstanceState)38 protected void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 setContentView(R.layout.activity_text_view); 41 } 42 43 @Override onWindowFocusChanged(boolean hasFocus)44 public void onWindowFocusChanged(boolean hasFocus) { 45 super.onWindowFocusChanged(hasFocus); 46 if (!hasFocus) { 47 Log.w("TextViewActivity", "TextViewActivity lost window focus"); 48 } 49 synchronized(mHasWindowFocusLock) { 50 mHasWindowFocus = hasFocus; 51 mHasWindowFocusLock.notify(); 52 } 53 } 54 55 /** 56 * Blocks the calling thread until the {@link KeyListenerCtsActivity} has window focus or the 57 * specified duration (in milliseconds) has passed. 58 */ waitForWindowFocus(long durationMillis)59 public boolean waitForWindowFocus(long durationMillis) { 60 long elapsedMillis = SystemClock.elapsedRealtime(); 61 synchronized(mHasWindowFocusLock) { 62 mHasWindowFocus = hasWindowFocus(); 63 while (!mHasWindowFocus && durationMillis > 0) { 64 long newElapsedMillis = SystemClock.elapsedRealtime(); 65 durationMillis -= (newElapsedMillis - elapsedMillis); 66 elapsedMillis = newElapsedMillis; 67 if (durationMillis > 0) { 68 try { 69 mHasWindowFocusLock.wait(durationMillis); 70 } catch (InterruptedException e) { 71 } 72 } 73 } 74 return mHasWindowFocus; 75 } 76 } 77 } 78