Home | History | Annotate | Download | only in stubs
      1 /*
      2  * Copyright (C) 2016 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.stubs;
     18 
     19 import android.app.Activity;
     20 import android.view.KeyboardShortcutGroup;
     21 import android.view.Menu;
     22 
     23 import java.util.List;
     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 public class KeyboardShortcutsActivity extends Activity {
     30 
     31     public static final String ITEM_1_NAME = "item 1";
     32     public static final char ITEM_1_SHORTCUT = 'i';
     33     private boolean mOnProvideKeyboardShortcutsCalled;
     34     private final Lock mLock = new ReentrantLock();
     35     private final Condition mSignalOnProvideKeyboardShortcuts  = mLock.newCondition();
     36     private final Condition mSignalOnActivityDeFocused = mLock.newCondition();
     37 
     38     @Override
     39     public boolean onCreateOptionsMenu(Menu menu) {
     40         menu.add(ITEM_1_NAME).setAlphabeticShortcut(ITEM_1_SHORTCUT);
     41         return true;
     42     }
     43 
     44     public void waitForMenuToBeOpen() throws InterruptedException {
     45         boolean stillWaiting = true;
     46         while (hasWindowFocus() && stillWaiting) {
     47             mLock.lock();
     48             try {
     49                 stillWaiting = mSignalOnActivityDeFocused.await(10, TimeUnit.SECONDS);
     50             } finally {
     51                 mLock.unlock();
     52             }
     53         }
     54     }
     55 
     56     @Override
     57     public void onWindowFocusChanged(boolean hasFocus) {
     58         if (!hasFocus) {
     59             mLock.lock();
     60             try {
     61                 mSignalOnActivityDeFocused.signal();
     62             } finally {
     63                 mLock.unlock();
     64             }
     65         }
     66         super.onWindowFocusChanged(hasFocus);
     67     }
     68 
     69     public void waitForKeyboardShortcutsToBeRequested() throws InterruptedException {
     70         boolean stillWaiting = true;
     71         while (!onProvideKeyboardShortcutsCalled() && stillWaiting) {
     72             mLock.lock();
     73             try {
     74                 stillWaiting = mSignalOnProvideKeyboardShortcuts.await(10, TimeUnit.SECONDS);
     75             } finally {
     76                 mLock.unlock();
     77             }
     78         }
     79     }
     80 
     81     @Override
     82     public void onProvideKeyboardShortcuts(
     83             List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
     84         mOnProvideKeyboardShortcutsCalled = true;
     85         mLock.lock();
     86         try {
     87             mSignalOnProvideKeyboardShortcuts.signal();
     88         } finally {
     89             mLock.unlock();
     90         }
     91         super.onProvideKeyboardShortcuts(data, menu, deviceId);
     92     }
     93 
     94     public boolean onProvideKeyboardShortcutsCalled() {
     95         return mOnProvideKeyboardShortcutsCalled;
     96     }
     97 }
     98