Java Code Examples for android.view.KeyCharacterMap#getEvents()

The following examples show how to use android.view.KeyCharacterMap#getEvents() . 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: EventInjectorTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
@Test
public void injectStaleKeyEvent() throws Exception {
  ActivityScenario<SendActivity> scenario = ActivityScenario.launch(SendActivity.class);

  scenario.onActivity(
      sendActivity -> {
        View view = sendActivity.findViewById(R.id.send_data_edit_text);
        assertTrue(view.requestFocus());
        latch.countDown();
      });

  assertTrue("Timed out!", latch.await(10, TimeUnit.SECONDS));
  assertFalse("SecurityException exception was thrown.", injectEventThrewSecurityException.get());

  KeyCharacterMap keyCharacterMap = UiControllerImpl.getKeyCharacterMap();
  KeyEvent[] events = keyCharacterMap.getEvents("a".toCharArray());
  KeyEvent event = KeyEvent.changeTimeRepeat(events[0], 1, 0);

  // Stale event does not fail for API < 13.
  if (Build.VERSION.SDK_INT < 13) {
    assertTrue(injector.injectKeyEvent(event));
  } else {
    assertFalse(injector.injectKeyEvent(event));
  }
}
 
Example 2
Source File: EventInjectorTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
@Test
public void injectKeyEventUpWithNoDown() throws Exception {
  ActivityScenario<SendActivity> scenario = ActivityScenario.launch(SendActivity.class);

  scenario.onActivity(
      sendActivity -> {
        View view = sendActivity.findViewById(R.id.send_data_edit_text);
        assertTrue(view.requestFocus());
        latch.countDown();
      });

  assertTrue("Timed out!", latch.await(10, TimeUnit.SECONDS));
  KeyCharacterMap keyCharacterMap = UiControllerImpl.getKeyCharacterMap();
  KeyEvent[] events = keyCharacterMap.getEvents("a".toCharArray());
  assertTrue(injector.injectKeyEvent(events[1]));
}
 
Example 3
Source File: Instrumentation.java    From AndroidComponentPlugin with Apache License 2.0 6 votes vote down vote up
/**
 * Sends the key events corresponding to the text to the app being
 * instrumented.
 * 
 * @param text The text to be sent. 
 */
public void sendStringSync(String text) {
    if (text == null) {
        return;
    }
    KeyCharacterMap keyCharacterMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);

    KeyEvent[] events = keyCharacterMap.getEvents(text.toCharArray());

    if (events != null) {
        for (int i = 0; i < events.length; i++) {
            // We have to change the time of an event before injecting it because
            // all KeyEvents returned by KeyCharacterMap.getEvents() have the same
            // time stamp and the system rejects too old events. Hence, it is
            // possible for an event to become stale before it is injected if it
            // takes too long to inject the preceding ones.
            sendKeySync(KeyEvent.changeTimeRepeat(events[i], SystemClock.uptimeMillis(), 0));
        }
    }
}
 
Example 4
Source File: Instrumentation.java    From droidel with Apache License 2.0 6 votes vote down vote up
/**
 * Sends the key events corresponding to the text to the app being
 * instrumented.
 * 
 * @param text The text to be sent. 
 */
public void sendStringSync(String text) {
    if (text == null) {
        return;
    }
    KeyCharacterMap keyCharacterMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);

    KeyEvent[] events = keyCharacterMap.getEvents(text.toCharArray());

    if (events != null) {
        for (int i = 0; i < events.length; i++) {
            // We have to change the time of an event before injecting it because
            // all KeyEvents returned by KeyCharacterMap.getEvents() have the same
            // time stamp and the system rejects too old events. Hence, it is
            // possible for an event to become stale before it is injected if it
            // takes too long to inject the preceding ones.
            sendKeySync(KeyEvent.changeTimeRepeat(events[i], SystemClock.uptimeMillis(), 0));
        }
    }
}
 
Example 5
Source File: Instrumentation.java    From AndroidComponentPlugin with Apache License 2.0 6 votes vote down vote up
/**
 * Sends the key events corresponding to the text to the app being
 * instrumented.
 * 
 * @param text The text to be sent. 
 */
public void sendStringSync(String text) {
    if (text == null) {
        return;
    }
    KeyCharacterMap keyCharacterMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);

    KeyEvent[] events = keyCharacterMap.getEvents(text.toCharArray());

    if (events != null) {
        for (int i = 0; i < events.length; i++) {
            // We have to change the time of an event before injecting it because
            // all KeyEvents returned by KeyCharacterMap.getEvents() have the same
            // time stamp and the system rejects too old events. Hence, it is
            // possible for an event to become stale before it is injected if it
            // takes too long to inject the preceding ones.
            sendKeySync(KeyEvent.changeTimeRepeat(events[i], SystemClock.uptimeMillis(), 0));
        }
    }
}
 
Example 6
Source File: TimePickerDialog.java    From MaterialDateTimePicker with Apache License 2.0 5 votes vote down vote up
/**
 * Get the keycode value for AM and PM in the current language.
 */
private int getAmOrPmKeyCode(int amOrPm) {
    // Cache the codes.
    if (mAmKeyCode == -1 || mPmKeyCode == -1) {
        // Find the first character in the AM/PM text that is unique.
        KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
        char amChar;
        char pmChar;
        for (int i = 0; i < Math.max(mAmText.length(), mPmText.length()); i++) {
            amChar = mAmText.toLowerCase(mLocale).charAt(i);
            pmChar = mPmText.toLowerCase(mLocale).charAt(i);
            if (amChar != pmChar) {
                KeyEvent[] events = kcm.getEvents(new char[]{amChar, pmChar});
                // There should be 4 events: a down and up for both AM and PM.
                if (events != null && events.length == 4) {
                    mAmKeyCode = events[0].getKeyCode();
                    mPmKeyCode = events[2].getKeyCode();
                } else {
                    Log.e(TAG, "Unable to find keycodes for AM and PM.");
                }
                break;
            }
        }
    }
    if (amOrPm == AM) {
        return mAmKeyCode;
    } else if (amOrPm == PM) {
        return mPmKeyCode;
    }

    return -1;
}
 
Example 7
Source File: TimePickerDialog.java    From AlarmOn with Apache License 2.0 5 votes vote down vote up
/**
 * Get the keycode value for AM and PM in the current language.
 */
private int getAmOrPmKeyCode(int amOrPm) {
    // Cache the codes.
    if (mAmKeyCode == -1 || mPmKeyCode == -1) {
        // Find the first character in the AM/PM text that is unique.
        KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
        char amChar;
        char pmChar;
        for (int i = 0; i < Math.max(mAmText.length(), mPmText.length()); i++) {
            amChar = mAmText.toLowerCase(Locale.getDefault()).charAt(i);
            pmChar = mPmText.toLowerCase(Locale.getDefault()).charAt(i);
            if (amChar != pmChar) {
                KeyEvent[] events = kcm.getEvents(new char[]{amChar, pmChar});
                // There should be 4 events: a down and up for both AM and PM.
                if (events != null && events.length == 4) {
                    mAmKeyCode = events[0].getKeyCode();
                    mPmKeyCode = events[2].getKeyCode();
                } else {
                    Log.e(TAG, "Unable to find keycodes for AM and PM.");
                }
                break;
            }
        }
    }
    if (amOrPm == AM) {
        return mAmKeyCode;
    } else if (amOrPm == PM) {
        return mPmKeyCode;
    }

    return -1;
}
 
Example 8
Source File: AppCompatTimePickerDelegate.java    From AppCompat-Extension-Library with Apache License 2.0 5 votes vote down vote up
/**
 * Get the keycode value for AM and PM in the current language.
 */
private int getAmOrPmKeyCode(int amOrPm) {
    // Cache the codes.
    if (mAmKeyCode == -1 || mPmKeyCode == -1) {
        // Find the first character in the AM/PM text that is unique.
        final KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
        final CharSequence amText = mAmText.toLowerCase(mCurrentLocale);
        final CharSequence pmText = mPmText.toLowerCase(mCurrentLocale);
        final int N = Math.min(amText.length(), pmText.length());
        for (int i = 0; i < N; i++) {
            final char amChar = amText.charAt(i);
            final char pmChar = pmText.charAt(i);
            if (amChar != pmChar) {
                // There should be 4 events: a down and up for both AM and PM.
                final KeyEvent[] events = kcm.getEvents(new char[] { amChar, pmChar });
                if (events != null && events.length == 4) {
                    mAmKeyCode = events[0].getKeyCode();
                    mPmKeyCode = events[2].getKeyCode();
                } else {
                    Log.e(TAG, "Unable to find keycodes for AM and PM.");
                }
                break;
            }
        }
    }

    if (amOrPm == AM) {
        return mAmKeyCode;
    } else if (amOrPm == PM) {
        return mPmKeyCode;
    }

    return -1;
}
 
Example 9
Source File: TimePickerDialog.java    From Conquer with Apache License 2.0 5 votes vote down vote up
/**
 * Get the keycode value for AM and PM in the current language.
 */
private int getAmOrPmKeyCode(int amOrPm) {
    // Cache the codes.
    if (mAmKeyCode == -1 || mPmKeyCode == -1) {
        // Find the first character in the AM/PM text that is unique.
        KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
        char amChar;
        char pmChar;
        for (int i = 0; i < Math.max(mAmText.length(), mPmText.length()); i++) {
            amChar = mAmText.toLowerCase(Locale.getDefault()).charAt(i);
            pmChar = mPmText.toLowerCase(Locale.getDefault()).charAt(i);
            if (amChar != pmChar) {
                KeyEvent[] events = kcm.getEvents(new char[]{amChar, pmChar});
                // There should be 4 events: a down and up for both AM and PM.
                if (events != null && events.length == 4) {
                    mAmKeyCode = events[0].getKeyCode();
                    mPmKeyCode = events[2].getKeyCode();
                } else {
                    Log.e(TAG, "Unable to find keycodes for AM and PM.");
                }
                break;
            }
        }
    }
    if (amOrPm == AM) {
        return mAmKeyCode;
    } else if (amOrPm == PM) {
        return mPmKeyCode;
    }

    return -1;
}
 
Example 10
Source File: SublimeTimePicker.java    From SublimePicker with Apache License 2.0 5 votes vote down vote up
/**
 * Get the keycode value for AM and PM in the current language.
 */
private int getAmOrPmKeyCode(int amOrPm) {
    // Cache the codes.
    if (mAmKeyCode == -1 || mPmKeyCode == -1) {
        // Find the first character in the AM/PM text that is unique.
        final KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
        final CharSequence amText = mAmText.toLowerCase(mCurrentLocale);
        final CharSequence pmText = mPmText.toLowerCase(mCurrentLocale);
        final int N = Math.min(amText.length(), pmText.length());
        for (int i = 0; i < N; i++) {
            final char amChar = amText.charAt(i);
            final char pmChar = pmText.charAt(i);
            if (amChar != pmChar) {
                // There should be 4 events: a down and up for both AM and PM.
                final KeyEvent[] events = kcm.getEvents(new char[]{amChar, pmChar});
                if (events != null && events.length == 4) {
                    mAmKeyCode = events[0].getKeyCode();
                    mPmKeyCode = events[2].getKeyCode();
                } else {
                    Log.e(TAG, "Unable to find keycodes for AM and PM.");
                }
                break;
            }
        }
    }

    if (amOrPm == AM) {
        return mAmKeyCode;
    } else if (amOrPm == PM) {
        return mPmKeyCode;
    }

    return -1;
}
 
Example 11
Source File: TimePickerDialog.java    From DateTimepicker with Apache License 2.0 5 votes vote down vote up
/**
 * Get the keycode value for AM and PM in the current language.
 */
private int getAmOrPmKeyCode(int amOrPm) {
    // Cache the codes.
    if (mAmKeyCode == -1 || mPmKeyCode == -1) {
        // Find the first character in the AM/PM text that is unique.
        KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
        char amChar;
        char pmChar;
        for (int i = 0; i < Math.max(mAmText.length(), mPmText.length()); i++) {
            amChar = mAmText.toLowerCase(Locale.getDefault()).charAt(i);
            pmChar = mPmText.toLowerCase(Locale.getDefault()).charAt(i);
            if (amChar != pmChar) {
                KeyEvent[] events = kcm.getEvents(new char[]{amChar, pmChar});
                // There should be 4 events: a down and up for both AM and PM.
                if (events != null && events.length == 4) {
                    mAmKeyCode = events[0].getKeyCode();
                    mPmKeyCode = events[2].getKeyCode();
                } else {
                    Log.e(TAG, "Unable to find keycodes for AM and PM.");
                }
                break;
            }
        }
    }
    if (amOrPm == AM) {
        return mAmKeyCode;
    } else if (amOrPm == PM) {
        return mPmKeyCode;
    }

    return -1;
}
 
Example 12
Source File: SwitchTimePicker.java    From Android-SwitchDateTimePicker with Apache License 2.0 5 votes vote down vote up
/**
 * Get the keycode value for AM and PM in the current language.
 */
private int getAmOrPmKeyCode(int amOrPm) {
    // Cache the codes.
    if (mAmKeyCode == -1 || mPmKeyCode == -1) {
        // Find the first character in the AM/PM text that is unique.
        KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
        char amChar;
        char pmChar;
        for (int i = 0; i < Math.max(mAmText.length(), mPmText.length()); i++) {
            amChar = mAmText.toLowerCase(Locale.getDefault()).charAt(i);
            pmChar = mPmText.toLowerCase(Locale.getDefault()).charAt(i);
            if (amChar != pmChar) {
                KeyEvent[] events = kcm.getEvents(new char[]{amChar, pmChar});
                // There should be 4 events: a down and up for both AM and PM.
                if (events != null && events.length == 4) {
                    mAmKeyCode = events[0].getKeyCode();
                    mPmKeyCode = events[2].getKeyCode();
                } else {
                    Log.e(TAG, "Unable to find keycodes for AM and PM.");
                }
                break;
            }
        }
    }
    if (amOrPm == AM) {
        return mAmKeyCode;
    } else if (amOrPm == PM) {
        return mPmKeyCode;
    }

    return -1;
}
 
Example 13
Source File: GridTimePickerDialog.java    From BottomSheetPickers with Apache License 2.0 5 votes vote down vote up
/**
 * Get the keycode value for AM and PM in the current language.
 */
private int getAmOrPmKeyCode(int amOrPm) {
    // Cache the codes.
    if (mAmKeyCode == -1 || mPmKeyCode == -1) {
        // Find the first character in the AM/PM text that is unique.
        KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
        char amChar;
        char pmChar;
        for (int i = 0; i < Math.max(mAmText.length(), mPmText.length()); i++) {
            amChar = mAmText.toLowerCase(Locale.getDefault()).charAt(i);
            pmChar = mPmText.toLowerCase(Locale.getDefault()).charAt(i);
            if (amChar != pmChar) {
                KeyEvent[] events = kcm.getEvents(new char[]{amChar, pmChar});
                // There should be 4 events: a down and up for both AM and PM.
                if (events != null && events.length == 4) {
                    mAmKeyCode = events[0].getKeyCode();
                    mPmKeyCode = events[2].getKeyCode();
                } else {
                    Log.e(TAG, "Unable to find keycodes for AM and PM.");
                }
                break;
            }
        }
    }
    if (amOrPm == HALF_DAY_1) {
        return mAmKeyCode;
    } else if (amOrPm == HALF_DAY_2) {
        return mPmKeyCode;
    }

    return -1;
}
 
Example 14
Source File: TimePickerDialog.java    From narrate-android with Apache License 2.0 5 votes vote down vote up
/**
 * Get the keycode value for AM and PM in the current language.
 */
private int getAmOrPmKeyCode(int amOrPm) {
    // Cache the codes.
    if (mAmKeyCode == -1 || mPmKeyCode == -1) {
        // Find the first character in the AM/PM text that is unique.
        KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
        char amChar;
        char pmChar;
        for (int i = 0; i < Math.max(mAmText.length(), mPmText.length()); i++) {
            amChar = mAmText.toLowerCase(Locale.getDefault()).charAt(i);
            pmChar = mPmText.toLowerCase(Locale.getDefault()).charAt(i);
            if (amChar != pmChar) {
                KeyEvent[] events = kcm.getEvents(new char[]{amChar, pmChar});
                // There should be 4 events: a down and up for both AM and PM.
                if (events != null && events.length == 4) {
                    mAmKeyCode = events[0].getKeyCode();
                    mPmKeyCode = events[2].getKeyCode();
                } else {
                    Log.e(TAG, "Unable to find keycodes for AM and PM.");
                }
                break;
            }
        }
    }
    if (amOrPm == AM) {
        return mAmKeyCode;
    } else if (amOrPm == PM) {
        return mPmKeyCode;
    }

    return -1;
}
 
Example 15
Source File: InputHelper.java    From XposedSmsCode with GNU General Public License v3.0 5 votes vote down vote up
/**
 * refer: com.android.commands.input.Input#sendText()
 *
 * @throws Throwable throwable throws if the caller has no android.permission.INJECT_EVENTS permission
 */
public static void sendText(String text) throws Throwable {
    int source = InputDevice.SOURCE_KEYBOARD;

    StringBuilder sb = new StringBuilder(text);

    boolean escapeFlag = false;
    for (int i = 0; i < sb.length(); i++) {
        if (escapeFlag) {
            escapeFlag = false;
            if (sb.charAt(i) == 's') {
                sb.setCharAt(i, ' ');
                sb.deleteCharAt(--i);
            }
        }
        if (sb.charAt(i) == '%') {
            escapeFlag = true;
        }
    }

    char[] chars = sb.toString().toCharArray();

    KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
    KeyEvent[] events = kcm.getEvents(chars);
    for (KeyEvent keyEvent : events) {
        if (source != keyEvent.getSource()) {
            keyEvent.setSource(source);
        }
        injectKeyEvent(keyEvent);
    }
}
 
Example 16
Source File: TimePickerDialog.java    From MaterialDateRangePicker with Apache License 2.0 5 votes vote down vote up
/**
 * Get the keycode value for AM and PM in the current language.
 */
private int getAmOrPmKeyCode(int amOrPm) {
    // Cache the codes.
    if (mAmKeyCode == -1 || mPmKeyCode == -1) {
        // Find the first character in the AM/PM text that is unique.
        KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
        char amChar;
        char pmChar;
        for (int i = 0; i < Math.max(mAmText.length(), mPmText.length()); i++) {
            amChar = mAmText.toLowerCase(Locale.getDefault()).charAt(i);
            pmChar = mPmText.toLowerCase(Locale.getDefault()).charAt(i);
            if (amChar != pmChar) {
                KeyEvent[] events = kcm.getEvents(new char[]{amChar, pmChar});
                // There should be 4 events: a down and up for both AM and PM.
                if (events != null && events.length == 4) {
                    mAmKeyCode = events[0].getKeyCode();
                    mPmKeyCode = events[2].getKeyCode();
                } else {
                    Log.e(TAG, "Unable to find keycodes for AM and PM.");
                }
                break;
            }
        }
    }
    if (amOrPm == AM) {
        return mAmKeyCode;
    } else if (amOrPm == PM) {
        return mPmKeyCode;
    }

    return -1;
}
 
Example 17
Source File: TimePickerDialog.java    From cathode with Apache License 2.0 5 votes vote down vote up
/**
 * Get the keycode value for AM and PM in the current language.
 */
private int getAmOrPmKeyCode(int amOrPm) {
  // Cache the codes.
  if (mAmKeyCode == -1 || mPmKeyCode == -1) {
    // Find the first character in the AM/PM text that is unique.
    KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
    char amChar;
    char pmChar;
    for (int i = 0; i < Math.max(mAmText.length(), mPmText.length()); i++) {
      amChar = mAmText.toLowerCase(Locale.getDefault()).charAt(i);
      pmChar = mPmText.toLowerCase(Locale.getDefault()).charAt(i);
      if (amChar != pmChar) {
        KeyEvent[] events = kcm.getEvents(new char[] { amChar, pmChar });
        // There should be 4 events: a down and up for both AM and PM.
        if (events != null && events.length == 4) {
          mAmKeyCode = events[0].getKeyCode();
          mPmKeyCode = events[2].getKeyCode();
        } else {
          Log.e(TAG, "Unable to find keycodes for AM and PM.");
        }
        break;
      }
    }
  }
  if (amOrPm == AM) {
    return mAmKeyCode;
  } else if (amOrPm == PM) {
    return mPmKeyCode;
  }

  return -1;
}
 
Example 18
Source File: EventInjectorTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void injectKeyEvent_securityException() throws InjectEventSecurityException {
  KeyCharacterMap keyCharacterMap = UiControllerImpl.getKeyCharacterMap();
  KeyEvent[] events = keyCharacterMap.getEvents("a".toCharArray());
  expectedException.expect(InjectEventSecurityException.class);
  injector.injectKeyEvent(events[0]);
}
 
Example 19
Source File: Instrumentation.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
/**
 * Sends the key events corresponding to the text to the app being
 * instrumented.
 * 
 * @param text The text to be sent. 
 */
public void sendStringSync(String text) {
    if (text == null) {
        return;
    }
    KeyCharacterMap keyCharacterMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
    
    KeyEvent[] events = keyCharacterMap.getEvents(text.toCharArray());
    
    if (events != null) {
        for (int i = 0; i < events.length; i++) {
            sendKeySync(events[i]);
        }
    }        
}
 
Example 20
Source File: UiControllerImpl.java    From android-test with Apache License 2.0 4 votes vote down vote up
@Override
public boolean injectString(String str) throws InjectEventSecurityException {
  checkNotNull(str);
  checkState(Looper.myLooper() == mainLooper, "Expecting to be on main thread!");
  initialize();

  // No-op if string is empty.
  if (str.isEmpty()) {
    Log.w(TAG, "Supplied string is empty resulting in no-op (nothing is typed).");
    return true;
  }

  boolean eventInjected = false;
  KeyCharacterMap keyCharacterMap = getKeyCharacterMap();

  // TODO(b/80130875): Investigate why not use (as suggested in javadoc of
  // keyCharacterMap.getEvents):
  // http://developer.android.com/reference/android/view/KeyEvent.html#KeyEvent(long,
  // java.lang.String, int, int)
  KeyEvent[] events = keyCharacterMap.getEvents(str.toCharArray());
  if (events == null) {
    throw new RuntimeException(
        String.format(
            Locale.ROOT,
            "Failed to get key events for string %s (i.e. current IME does not understand how to"
                + " translate the string into key events). As a workaround, you can use"
                + " replaceText action to set the text directly in the EditText field.",
            str));
  }

  Log.d(TAG, String.format(Locale.ROOT, "Injecting string: \"%s\"", str));

  for (KeyEvent event : events) {
    checkNotNull(
        event,
        String.format(
            Locale.ROOT,
            "Failed to get event for character (%c) with key code (%s)",
            event.getKeyCode(),
            event.getUnicodeChar()));

    eventInjected = false;
    for (int attempts = 0; !eventInjected && attempts < 4; attempts++) {
      // We have to change the time of an event before injecting it because
      // all KeyEvents returned by KeyCharacterMap.getEvents() have the same
      // time stamp and the system rejects too old events. Hence, it is
      // possible for an event to become stale before it is injected if it
      // takes too long to inject the preceding ones.
      event = KeyEvent.changeTimeRepeat(event, SystemClock.uptimeMillis(), 0);
      eventInjected = injectKeyEvent(event);
    }

    if (!eventInjected) {
      Log.e(
          TAG,
          String.format(
              Locale.ROOT,
              "Failed to inject event for character (%c) with key code (%s)",
              event.getUnicodeChar(),
              event.getKeyCode()));
      break;
    }
  }

  return eventInjected;
}