Repository: flutter
Issue: Flutter throws an exception when typing something while holding selection endpoint
Labels: a: text input,c: crash,platform-android,engine,has reproducible steps,P2,found in release: 2.10,found in release: 2.13,customer: chalk (g3),team-android,triaged-android

## Steps to Reproduce

1. Select any text
2. Type any symbol while holding selection endpoint with finger (touch location should be outside the text)

**Expected results:** Nothing special happens.

**Actual results:** Application throws exception.

<details>
<summary>Code sample</summary>

```dart
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Material(
        child: Padding(
          padding: EdgeInsets.all(50.0),
          child: TextField(),
        ),
      ),
    );
  }
}
```

</details>

<details>
  <summary>Logs</summary>

```
E/MethodChannel#flutter/textinput( 9756): Failed to handle method call
E/MethodChannel#flutter/textinput( 9756): java.lang.IndexOutOfBoundsException: invalid selection end: 4
E/MethodChannel#flutter/textinput( 9756): 	at io.flutter.embedding.engine.systemchannels.TextInputChannel$TextEditState.<init>(TextInputChannel.java:787)
E/MethodChannel#flutter/textinput( 9756): 	at io.flutter.embedding.engine.systemchannels.TextInputChannel$TextEditState.fromJson(TextInputChannel.java:738)
E/MethodChannel#flutter/textinput( 9756): 	at io.flutter.embedding.engine.systemchannels.TextInputChannel$1.onMethodCall(TextInputChannel.java:103)
E/MethodChannel#flutter/textinput( 9756): 	at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
E/MethodChannel#flutter/textinput( 9756): 	at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:178)
E/MethodChannel#flutter/textinput( 9756): 	at io.flutter.embedding.engine.dart.DartMessenger.lambda$handleMessageFromDart$0$DartMessenger(DartMessenger.java:206)
E/MethodChannel#flutter/textinput( 9756): 	at io.flutter.embedding.engine.dart.-$$Lambda$DartMessenger$6ZD1MYkhaLxyPjtoFDxe45u43DI.run(Unknown Source:12)
E/MethodChannel#flutter/textinput( 9756): 	at android.os.Handler.handleCallback(Handler.java:900)
E/MethodChannel#flutter/textinput( 9756): 	at android.os.Handler.dispatchMessage(Handler.java:103)
E/MethodChannel#flutter/textinput( 9756): 	at android.os.Looper.loop(Looper.java:219)
E/MethodChannel#flutter/textinput( 9756): 	at android.app.ActivityThread.main(ActivityThread.java:8349)
E/MethodChannel#flutter/textinput( 9756): 	at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#flutter/textinput( 9756): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
E/MethodChannel#flutter/textinput( 9756): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
E/flutter ( 9756): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: PlatformException(error, invalid selection end: 4, null, java.lang.IndexOutOfBoundsException: invalid selection end: 4
E/flutter ( 9756): 	at io.flutter.embedding.engine.systemchannels.TextInputChannel$TextEditState.<init>(TextInputChannel.java:787)
E/flutter ( 9756): 	at io.flutter.embedding.engine.systemchannels.TextInputChannel$TextEditState.fromJson(TextInputChannel.java:738)
E/flutter ( 9756): 	at io.flutter.embedding.engine.systemchannels.TextInputChannel$1.onMethodCall(TextInputChannel.java:103)
E/flutter ( 9756): 	at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
E/flutter ( 9756): 	at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:178)
E/flutter ( 9756): 	at io.flutter.embedding.engine.dart.DartMessenger.lambda$handleMessageFromDart$0$DartMessenger(DartMessenger.java:206)
E/flutter ( 9756): 	at io.flutter.embedding.engine.dart.-$$Lambda$DartMessenger$6ZD1MYkhaLxyPjtoFDxe45u43DI.run(Unknown Source:12)
E/flutter ( 9756): 	at android.os.Handler.handleCallback(Handler.java:900)
E/flutter ( 9756): 	at android.os.Handler.dispatchMessage(Handler.java:103)
E/flutter ( 9756): 	at android.os.Looper.loop(Looper.java:219)
E/flutter ( 9756): 	at android.app.ActivityThread.main(ActivityThread.java:8349)
E/flutter ( 9756): 	at java.lang.reflect.Method.invoke(Native Method)
E/flutter ( 9756): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
E/flutter ( 9756): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
E/flutter ( 9756): )
E/flutter ( 9756): #0      JSONMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:164:7)
E/flutter ( 9756): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:167:18)
E/flutter ( 9756): <asynchronous suspension>
```

```
[√] Flutter (Channel stable, 2.8.1, on Microsoft Windows [Version 10.0.19042.1586], locale ru-RU)
    • Flutter version 2.8.1 at C:\DDD\prog\programs\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 77d935af4d (3 months ago), 2021-12-16 08:37:33 -0800
    • Engine revision 890a5fca2e
    • Dart version 2.15.1

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at C:\DDD\prog\programs\sdks
    • Platform android-31, build-tools 31.0.0
    • Java binary at: C:\DDD\prog\programs\andst\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.11+9-b60-7590822)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 2021.1)
    • Android Studio at C:\DDD\prog\programs\andst
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.11+9-b60-7590822)

[√] Connected device (3 available)
    • PPA LX1 (mobile) • 73XNU21116203122 • android-arm64  • Android 10 (API 29)
    • Chrome (web)     • chrome           • web-javascript • Google Chrome 99.0.4844.51
    • Edge (web)       • edge             • web-javascript • Microsoft Edge 99.0.1150.39

• No issues found!
```