-
Notifications
You must be signed in to change notification settings - Fork 6k
Samsung duplication bug hack-fix #16547
Changes from 5 commits
403d32d
014ff7b
465a8a2
b0ad38f
11d88fd
5dce049
cbfed0e
f01c985
541ebee
c447211
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,8 +4,10 @@ | |||||
|
||||||
package io.flutter.plugin.editing; | ||||||
|
||||||
import android.annotation.SuppressLint; | ||||||
import android.content.Context; | ||||||
import android.os.Build; | ||||||
import android.provider.Settings; | ||||||
import android.text.DynamicLayout; | ||||||
import android.text.Editable; | ||||||
import android.text.Layout; | ||||||
|
@@ -17,6 +19,7 @@ | |||||
import android.view.inputmethod.CursorAnchorInfo; | ||||||
import android.view.inputmethod.EditorInfo; | ||||||
import android.view.inputmethod.InputMethodManager; | ||||||
import android.view.inputmethod.InputMethodSubtype; | ||||||
import io.flutter.Log; | ||||||
import io.flutter.embedding.engine.systemchannels.TextInputChannel; | ||||||
|
||||||
|
@@ -30,6 +33,9 @@ class InputConnectionAdaptor extends BaseInputConnection { | |||||
private InputMethodManager mImm; | ||||||
private final Layout mLayout; | ||||||
|
||||||
// Used to determine if Samsung-specific hacks should be applied. | ||||||
private final boolean mIsSamsung; | ||||||
|
||||||
@SuppressWarnings("deprecation") | ||||||
public InputConnectionAdaptor( | ||||||
View view, | ||||||
|
@@ -56,6 +62,8 @@ public InputConnectionAdaptor( | |||||
0.0f, | ||||||
false); | ||||||
mImm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); | ||||||
|
||||||
mIsSamsung = isSamsung(); | ||||||
} | ||||||
|
||||||
// Send the current state of the editable to Flutter. | ||||||
|
@@ -132,19 +140,59 @@ public boolean setComposingText(CharSequence text, int newCursorPosition) { | |||||
public boolean finishComposingText() { | ||||||
boolean result = super.finishComposingText(); | ||||||
|
||||||
if (Build.VERSION.SDK_INT >= 21) { | ||||||
// Update the keyboard with a reset/empty composing region. Critical on | ||||||
// Samsung keyboards to prevent punctuation duplication. | ||||||
CursorAnchorInfo.Builder builder = new CursorAnchorInfo.Builder(); | ||||||
builder.setComposingText(-1, ""); | ||||||
CursorAnchorInfo anchorInfo = builder.build(); | ||||||
mImm.updateCursorAnchorInfo(mFlutterView, anchorInfo); | ||||||
// Apply Samsung hacks. Samsung caches composing region data strangely, causing text | ||||||
// duplication. | ||||||
if (mIsSamsung) { | ||||||
if (Build.VERSION.SDK_INT >= 21) { | ||||||
// Samsung keyboards don't clear the composing region on finishComposingText. | ||||||
// Update the keyboard with a reset/empty composing region. Critical on | ||||||
// Samsung keyboards to prevent punctuation duplication. | ||||||
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. Maybe lead with "Samsung keyboards don't clear the composing region on |
||||||
CursorAnchorInfo.Builder builder = new CursorAnchorInfo.Builder(); | ||||||
builder.setComposingText(-1, ""); | ||||||
|
builder.setComposingText(-1, ""); | |
builder.setComposingText(/*composingTextStart=*/-1, /*composingText=*/""); |
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.
good idea, much clearer what this is doing!
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.
Hiding and showing the keyboard can restart the IMM. May be related?
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.
Don't have a solid answer here, but in my experimentation, restarting IMM did not seem to have any noticeable effect on this behavior.
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 assume if they fix this bug in a future release, we'll need to change this logic. Is there no way we can narrow this down further?
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.
Overall this hack should be fully compatible with other keyboards. Anything we do here is not 1: telling the keyboard something that it should already know, or 2: performing an action that we are not guaranteed to immediately revert.
It seems unlikely to me that Samsung will release a fix for all versions as well as distribute to the hundreds of millions of devices in the wild.
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.
nit: We actually don't want variables prefixed with
m
, it goes against Google Java style. The other members in the class already have it but I'd avoid matching it going forward. Ideally somebody should come back here later and clean the other names up so they fit the style guide.