@@ -168,10 +168,21 @@ public boolean sendKeyEvent(KeyEvent event) {
168
168
// isRTLCharAt() call below is returning blanket direction assumption
169
169
// based on the first character in the line.
170
170
boolean isRtl = mLayout .isRtlCharAt (mLayout .getLineForOffset (selStart ));
171
- if (isRtl ) {
172
- Selection .extendRight (mEditable , mLayout );
173
- } else {
174
- Selection .extendLeft (mEditable , mLayout );
171
+ try {
172
+ if (isRtl ) {
173
+ Selection .extendRight (mEditable , mLayout );
174
+ } else {
175
+ Selection .extendLeft (mEditable , mLayout );
176
+ }
177
+ } catch (IndexOutOfBoundsException e ) {
178
+ // On some Chinese devices (primarily Huawei, some Xiaomi),
179
+ // on initial app startup before focus is lost, the
180
+ // Selection.extendLeft and extendRight calls always extend
181
+ // from the index of the initial contents of mEditable. This
182
+ // try-catch will prevent crashing on Huawei devices by falling
183
+ // back to a simple way of deletion, although this a hack and
184
+ // will not handle emojis.
185
+ Selection .setSelection (mEditable , selStart , selStart - 1 );
175
186
}
176
187
int newStart = clampIndexToEditable (Selection .getSelectionStart (mEditable ), mEditable );
177
188
int newEnd = clampIndexToEditable (Selection .getSelectionEnd (mEditable ), mEditable );
0 commit comments