1 /*
2  * Copyright (C) 2009 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.text.method.cts;
18 
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.os.SystemClock;
22 import android.text.cts.R;
23 import android.text.method.BaseKeyListener;
24 import android.text.method.DateKeyListener;
25 import android.text.method.DateTimeKeyListener;
26 import android.text.method.DigitsKeyListener;
27 import android.text.method.MultiTapKeyListener;
28 import android.text.method.NumberKeyListener;
29 import android.text.method.QwertyKeyListener;
30 import android.text.method.TextKeyListener;
31 import android.text.method.TimeKeyListener;
32 import android.util.Log;
33 
34 /**
35  * This Activity is used for testing:
36  * {@link DigitsKeyListener}
37  * {@link BaseKeyListener}
38  * {@link MultiTapKeyListener}
39  * {@link NumberKeyListener}
40  * {@link QwertyKeyListener}
41  * {@link TextKeyListener}
42  * {@link DateKeyListener}
43  * {@link DateTimeKeyListener}
44  * {@link TimeKeyListener}
45  *
46  * @see DigitsKeyListener
47  * @see BaseKeyListener
48  * @see MultiTapKeyListener
49  * @see NumberKeyListener
50  * @see QwertyKeyListener
51  * @see TextKeyListener
52  * @see DateKeyListener
53  * @see DateTimeKeyListener
54  * @see TimeKeyListener
55  */
56 
57 public class KeyListenerCtsActivity extends Activity {
58     private boolean mHasWindowFocus = false;
59     private final Object mHasWindowFocusLock = new Object();
60 
61     @Override
onCreate(Bundle savedInstanceState)62     protected void onCreate(Bundle savedInstanceState) {
63         super.onCreate(savedInstanceState);
64         setContentView(R.layout.keylistener_layout);
65     }
66 
67     @Override
onWindowFocusChanged(boolean hasFocus)68     public void onWindowFocusChanged(boolean hasFocus) {
69         super.onWindowFocusChanged(hasFocus);
70         if (!hasFocus) {
71             Log.w("KeyListenerCtsActivity", "KeyListenerCtsActivity lost window focus");
72         }
73         synchronized(mHasWindowFocusLock) {
74             mHasWindowFocus = hasFocus;
75             mHasWindowFocusLock.notify();
76         }
77     }
78 
79     /**
80      * Blocks the calling thread until the {@link KeyListenerCtsActivity} has window focus or the
81      * specified duration (in milliseconds) has passed.
82      */
waitForWindowFocus(long durationMillis)83     public boolean waitForWindowFocus(long durationMillis) {
84         long elapsedMillis = SystemClock.elapsedRealtime();
85         synchronized(mHasWindowFocusLock) {
86             mHasWindowFocus = hasWindowFocus();
87             while (!mHasWindowFocus && durationMillis > 0) {
88                 long newElapsedMillis = SystemClock.elapsedRealtime();
89                 durationMillis -= (newElapsedMillis - elapsedMillis);
90                 elapsedMillis = newElapsedMillis;
91                 if (durationMillis > 0) {
92                     try {
93                         mHasWindowFocusLock.wait(durationMillis);
94                     } catch (InterruptedException e) {
95                     }
96                 }
97             }
98             return mHasWindowFocus;
99         }
100     }
101 }
102