This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Custom unicode handling for Android backspace via JNI to ICU #17960
Merged
fluttergithubbot
merged 4 commits into
flutter:master
from
DeMonkeyCoder:android-backspace
May 6, 2020
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f811df2
Handle android backspace by handling the last Unicode characters
DeMonkeyCoder 7615795
Handle android backspace with icu library by JNI
DeMonkeyCoder 9bf671b
InputConnectionAdaptor cleanup and test
DeMonkeyCoder d853f9f
Merge branch 'master' into android-backspace
DeMonkeyCoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
shell/platform/android/io/flutter/plugin/editing/FlutterTextUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| package io.flutter.plugin.editing; | ||
|
|
||
| import io.flutter.embedding.engine.FlutterJNI; | ||
|
|
||
| class FlutterTextUtils { | ||
| public static final int LINE_FEED = 0x0A; | ||
| public static final int CARRIAGE_RETURN = 0x0D; | ||
| public static final int COMBINING_ENCLOSING_KEYCAP = 0x20E3; | ||
| public static final int CANCEL_TAG = 0xE007F; | ||
| public static final int ZERO_WIDTH_JOINER = 0x200D; | ||
| private final FlutterJNI flutterJNI; | ||
|
|
||
| public FlutterTextUtils(FlutterJNI flutterJNI) { | ||
| this.flutterJNI = flutterJNI; | ||
| } | ||
|
|
||
| public boolean isEmoji(int codePoint) { | ||
| return flutterJNI.nativeFlutterTextUtilsIsEmoji(codePoint); | ||
| } | ||
|
|
||
| public boolean isEmojiModifier(int codePoint) { | ||
| return flutterJNI.nativeFlutterTextUtilsIsEmojiModifier(codePoint); | ||
| } | ||
|
|
||
| public boolean isEmojiModifierBase(int codePoint) { | ||
| return flutterJNI.nativeFlutterTextUtilsIsEmojiModifierBase(codePoint); | ||
| } | ||
|
|
||
| public boolean isVariationSelector(int codePoint) { | ||
| return flutterJNI.nativeFlutterTextUtilsIsVariationSelector(codePoint); | ||
| } | ||
|
|
||
| public boolean isRegionalIndicatorSymbol(int codePoint) { | ||
| return flutterJNI.nativeFlutterTextUtilsIsRegionalIndicator(codePoint); | ||
| } | ||
|
|
||
| public boolean isTagSpecChar(int codePoint) { | ||
| return 0xE0020 <= codePoint && codePoint <= 0xE007E; | ||
| } | ||
|
|
||
| public boolean isKeycapBase(int codePoint) { | ||
| return ('0' <= codePoint && codePoint <= '9') || codePoint == '#' || codePoint == '*'; | ||
| } | ||
|
|
||
| /** | ||
| * Start offset for backspace key or moving left from the current offset. Same methods are also | ||
| * included in Android APIs but they don't work as expected in API Levels lower than 24. Reference | ||
| * for the logic in this code is the Android source code. | ||
| * | ||
| * @see <a target="_new" | ||
| * href="https://android.googlesource.com/platform/frameworks/base/+/refs/heads/android10-s3-release/core/java/android/text/method/BaseKeyListener.java#111">https://android.googlesource.com/platform/frameworks/base/+/refs/heads/android10-s3-release/core/java/android/text/method/BaseKeyListener.java#111</a> | ||
| */ | ||
| public int getOffsetBefore(CharSequence text, int offset) { | ||
| if (offset <= 1) { | ||
| return 0; | ||
| } | ||
|
|
||
| int codePoint = Character.codePointBefore(text, offset); | ||
| int deleteCharCount = Character.charCount(codePoint); | ||
| int lastOffset = offset - deleteCharCount; | ||
|
|
||
| if (lastOffset == 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| // Line Feed | ||
| if (codePoint == LINE_FEED) { | ||
| codePoint = Character.codePointBefore(text, lastOffset); | ||
| if (codePoint == CARRIAGE_RETURN) { | ||
| ++deleteCharCount; | ||
| } | ||
| return offset - deleteCharCount; | ||
| } | ||
|
|
||
| // Flags | ||
| if (isRegionalIndicatorSymbol(codePoint)) { | ||
| codePoint = Character.codePointBefore(text, lastOffset); | ||
| lastOffset -= Character.charCount(codePoint); | ||
| int regionalIndicatorSymbolCount = 1; | ||
| while (lastOffset > 0 && isRegionalIndicatorSymbol(codePoint)) { | ||
| codePoint = Character.codePointBefore(text, lastOffset); | ||
| lastOffset -= Character.charCount(codePoint); | ||
| regionalIndicatorSymbolCount++; | ||
| } | ||
| if (regionalIndicatorSymbolCount % 2 == 0) { | ||
| deleteCharCount += 2; | ||
| } | ||
| return offset - deleteCharCount; | ||
| } | ||
|
|
||
| // Keycaps | ||
| if (codePoint == COMBINING_ENCLOSING_KEYCAP) { | ||
| codePoint = Character.codePointBefore(text, lastOffset); | ||
| lastOffset -= Character.charCount(codePoint); | ||
| if (lastOffset > 0 && isVariationSelector(codePoint)) { | ||
| int tmpCodePoint = Character.codePointBefore(text, lastOffset); | ||
| if (isKeycapBase(tmpCodePoint)) { | ||
| deleteCharCount += Character.charCount(codePoint) + Character.charCount(tmpCodePoint); | ||
| } | ||
| } else if (isKeycapBase(codePoint)) { | ||
| deleteCharCount += Character.charCount(codePoint); | ||
| } | ||
| return offset - deleteCharCount; | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you have unrolled the state machine, can you add some notation here indicating that the following few if statements are meant to act like a fall through and do not always return like the if statements above? The separation will make the code much more parse-able.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| /** | ||
| * Following if statements for Emoji tag sequence and Variation selector are skipping these | ||
| * modifiers for going through the last statement that is for handling emojis. They return the | ||
| * offset if they don't find proper base characters | ||
| */ | ||
| // Emoji Tag Sequence | ||
| if (codePoint == CANCEL_TAG) { // tag_end | ||
| codePoint = Character.codePointBefore(text, lastOffset); | ||
| lastOffset -= Character.charCount(codePoint); | ||
| while (lastOffset > 0 && isTagSpecChar(codePoint)) { // tag_spec | ||
| deleteCharCount += Character.charCount(codePoint); | ||
| codePoint = Character.codePointBefore(text, lastOffset); | ||
| lastOffset -= Character.charCount(codePoint); | ||
| } | ||
| if (!isEmoji(codePoint)) { // tag_base not found. Just delete the end. | ||
| return offset - 2; | ||
| } | ||
| deleteCharCount += Character.charCount(codePoint); | ||
| } | ||
|
|
||
| if (isVariationSelector(codePoint)) { | ||
| codePoint = Character.codePointBefore(text, lastOffset); | ||
| if (!isEmoji(codePoint)) { | ||
| return offset - deleteCharCount; | ||
| } | ||
| deleteCharCount += Character.charCount(codePoint); | ||
|
|
||
| lastOffset -= deleteCharCount; | ||
| } | ||
|
|
||
| if (isEmoji(codePoint)) { | ||
| boolean isZwj = false; | ||
| int lastSeenVariantSelectorCharCount = 0; | ||
| do { | ||
| if (isZwj) { | ||
| deleteCharCount += Character.charCount(codePoint) + lastSeenVariantSelectorCharCount + 1; | ||
| isZwj = false; | ||
| } | ||
| lastSeenVariantSelectorCharCount = 0; | ||
| if (isEmojiModifier(codePoint)) { | ||
| codePoint = Character.codePointBefore(text, lastOffset); | ||
| lastOffset -= Character.charCount(codePoint); | ||
| if (lastOffset > 0 && isVariationSelector(codePoint)) { | ||
| codePoint = Character.codePointBefore(text, lastOffset); | ||
| if (!isEmoji(codePoint)) { | ||
| return offset - deleteCharCount; | ||
| } | ||
| lastSeenVariantSelectorCharCount = Character.charCount(codePoint); | ||
| lastOffset -= Character.charCount(codePoint); | ||
| } | ||
| if (isEmojiModifierBase(codePoint)) { | ||
| deleteCharCount += lastSeenVariantSelectorCharCount + Character.charCount(codePoint); | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| if (lastOffset > 0) { | ||
| codePoint = Character.codePointBefore(text, lastOffset); | ||
| lastOffset -= Character.charCount(codePoint); | ||
| if (codePoint == ZERO_WIDTH_JOINER) { | ||
| isZwj = true; | ||
| codePoint = Character.codePointBefore(text, lastOffset); | ||
| lastOffset -= Character.charCount(codePoint); | ||
| if (lastOffset > 0 && isVariationSelector(codePoint)) { | ||
| codePoint = Character.codePointBefore(text, lastOffset); | ||
| lastSeenVariantSelectorCharCount = Character.charCount(codePoint); | ||
| lastOffset -= Character.charCount(codePoint); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (lastOffset == 0) { | ||
| break; | ||
| } | ||
| } while (isZwj && isEmoji(codePoint)); | ||
| } | ||
|
|
||
| return offset - deleteCharCount; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the Unicode spec refers to this as
TAG_END, should probably stick to same naming schemeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
http://unicode.org/reports/tr51/#def_emoji_tag_sequence
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jk, it looks like they do call this
CANCEL_TAGin Android, this is fine, but I still think TAG_END may be a more appropriate name. Up to you if you want to change it. Just depends on if you want to follow Android or Unicode's lead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to follow the Android guideline because we are using it as the reference.