Java Code Examples for android.app.Instrumentation#sendKeyDownUpSync()

The following examples show how to use android.app.Instrumentation#sendKeyDownUpSync() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainNativeActivity.java    From android-openslmediaplayer with Apache License 2.0 6 votes vote down vote up
private void startFakeTouchTask() {
    // Solution/Hack #3 - "Send fake touches!"
    // https://www.youtube.com/watch?v=F2ZDp-eNrh4

    mTimer = new Timer();
    mSendFakeTouchTask = new TimerTask() {
        @Override
        public void run() {
            try {
                Instrumentation instrumentation = new Instrumentation();
                instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_BACKSLASH);
            } catch (SecurityException e) {
                // ignore SecurityException
                // "Injecting to another application requires INJECT_EVENTS permission"
            }
        }
    };
    mTimer.schedule(mSendFakeTouchTask, 1000, 1000);
}
 
Example 2
Source File: TestUtilities.java    From google-authenticator-android with Apache License 2.0 5 votes vote down vote up
/**
 * Taps the negative button of a currently displayed dialog. This method assumes that a button of
 * the dialog is currently selected.
 *
 * @see #tapDialogPositiveButton(Instrumentation)
 */
public static void tapDialogNegativeButton(Instrumentation instrumentation) {
  selectDialogButton(instrumentation);

  instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_LEFT);
  instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
}
 
Example 3
Source File: TestUtilities.java    From google-authenticator-android with Apache License 2.0 5 votes vote down vote up
/**
 * Taps the positive button of a currently displayed dialog. This method assumes that a button of
 * the dialog is currently selected.
 *
 * @see #tapDialogNegativeButton(Instrumentation)
 */
@FixWhenMinSdkVersion(14)
public static void tapDialogPositiveButton(Instrumentation instrumentation) {
  selectDialogButton(instrumentation);

  instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_RIGHT);
  instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
}
 
Example 4
Source File: TestUtilities.java    From google-authenticator-android with Apache License 2.0 5 votes vote down vote up
/**
 * Selects a button at the bottom of a dialog. This assumes that a dialog is currently displayed
 * in the foreground.
 */
private static void selectDialogButton(Instrumentation instrumentation) {
  // If the dialog contains too much text it will scroll and the buttons at the bottom will only
  // get selected once it scrolls to the very bottom.
  // So far, 6 x DPAD_DOWN seems to do the trick for our app...
  instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
  instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
  instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
  instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
  instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
  instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
}
 
Example 5
Source File: TestUtilities.java    From google-authenticator-android with Apache License 2.0 5 votes vote down vote up
/**
 * Taps the negative button of a currently displayed 3 button dialog. This method assumes that a
 * button of the dialog is currently selected.
 */
public static void tapNegativeButtonIn3ButtonDialog(Instrumentation instrumentation) {
  selectDialogButton(instrumentation);

  instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_LEFT);
  instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_LEFT);
  instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
}
 
Example 6
Source File: TestUtilities.java    From google-authenticator-android with Apache License 2.0 5 votes vote down vote up
/**
 * Taps the neutral button of a currently displayed 3 button dialog. This method assumes that a
 * button of the dialog is currently selected.
 */
public static void tapNeutralButtonIn3ButtonDialog(Instrumentation instrumentation) {
  selectDialogButton(instrumentation);

  instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_RIGHT);
  instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
}
 
Example 7
Source File: TestUtilities.java    From google-authenticator-android with Apache License 2.0 5 votes vote down vote up
/**
 * Taps the positive button of a currently displayed 3 button dialog. This method assumes that a
 * button of the dialog is currently selected.
 */
public static void tapPositiveButtonIn3ButtonDialog(Instrumentation instrumentation) {
  selectDialogButton(instrumentation);

  instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_RIGHT);
  instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_RIGHT);
  instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
}
 
Example 8
Source File: ActivityNavigator.java    From relight with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
    Instrumentation inst = new Instrumentation();
    inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
}
 
Example 9
Source File: AndroidSystemHandler.java    From AlexaAndroid with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Force the device to think that a hardware button has been pressed, this is used for Play/Pause/Previous/Next Media commands
 * @param keyCode keycode for the hardware button we're emulating
 */
private static void sendMediaButton(int keyCode) {
    Instrumentation inst = new Instrumentation();
    inst.sendKeyDownUpSync(keyCode);
}
 
Example 10
Source File: BaseActivity.java    From AlexaAndroid with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Force the device to think that a hardware button has been pressed, this is used for Play/Pause/Previous/Next Media commands
 * @param context
 * @param keyCode keycode for the hardware button we're emulating
 */
private static void sendMediaButton(Context context, int keyCode) {
    Instrumentation inst = new Instrumentation();
    inst.sendKeyDownUpSync(keyCode);
}