Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Emoji keyboard in certain cases #329

Merged
merged 23 commits into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.support.annotation.Px;
import android.support.v7.widget.AppCompatEditText;
import android.util.AttributeSet;

import com.vanniktech.emoji.emoji.Emoji;

/** Reference implementation for an EditText with emoji support. */
Expand Down
39 changes: 25 additions & 14 deletions emoji/src/main/java/com/vanniktech/emoji/EmojiPopup.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.CheckResult;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
Expand All @@ -17,6 +18,7 @@
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.autofill.AutofillManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
Expand All @@ -30,10 +32,12 @@
import com.vanniktech.emoji.listeners.OnSoftKeyboardCloseListener;
import com.vanniktech.emoji.listeners.OnSoftKeyboardOpenListener;

import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.O;
import static com.vanniktech.emoji.Utils.backspace;
import static com.vanniktech.emoji.Utils.checkNotNull;

public final class EmojiPopup {
public final class EmojiPopup implements EmojiResultReceiver.Receiver {
static final String TAG = "EmojiPopup";

static final int MIN_KEYBOARD_HEIGHT = 100;
Expand Down Expand Up @@ -61,17 +65,7 @@ public final class EmojiPopup {

int originalImeOptions = -1;

final ResultReceiver resultReceiver = new ResultReceiver(null) {
@Override protected void onReceiveResult(final int resultCode, final Bundle resultData) {
if (resultCode == 0 || resultCode == 1) {
context.runOnUiThread(new Runnable() {
@Override public void run() {
showAtBottom();
}
});
}
}
};
final EmojiResultReceiver emojiResultReceiver = new EmojiResultReceiver(new Handler(Looper.getMainLooper()));

final ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override @SuppressWarnings("PMD.CyclomaticComplexity") public void onGlobalLayout() {
Expand Down Expand Up @@ -215,7 +209,8 @@ private void showAtBottomPending() {
}

if (inputMethodManager != null) {
inputMethodManager.showSoftInput(editText, InputMethodManager.RESULT_UNCHANGED_SHOWN, resultReceiver);
emojiResultReceiver.setReceiver(this);
inputMethodManager.showSoftInput(editText, InputMethodManager.RESULT_UNCHANGED_SHOWN, emojiResultReceiver);
}
}

Expand All @@ -229,12 +224,22 @@ public void dismiss() {
recentEmoji.persist();
variantEmoji.persist();

emojiResultReceiver.setReceiver(null);

if (originalImeOptions != -1) {
editText.setImeOptions(originalImeOptions);
final InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);

if (inputMethodManager != null) {
inputMethodManager.restartInput(editText);
}

if (SDK_INT >= O) {
AutofillManager autofillManager = context.getSystemService(AutofillManager.class);
if (autofillManager != null) {
autofillManager.cancel();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! I sometimes saw this leak in my app, but always thought that was an Android SDK one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:) It's almost like magic! :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure this is the only available open source lib these days that handles keyboard absolutely correctly btw, which is a great accomplishment - thanks to both @rubengees and @vanniktech for pushing when things didn't work as they were supposed to!

}
}
}
}

Expand All @@ -247,6 +252,12 @@ void showAtBottom() {
}
}

@Override public void onReceiveResult(int resultCode, Bundle data) {
if (resultCode == 0 || resultCode == 1) {
showAtBottom();
}
}

public static final class Builder {
@NonNull private final View rootView;
@StyleRes private int keyboardAnimationStyle;
Expand Down
33 changes: 33 additions & 0 deletions emoji/src/main/java/com/vanniktech/emoji/EmojiResultReceiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.vanniktech.emoji;

import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;

public class EmojiResultReceiver extends ResultReceiver {
public interface Receiver {
public void onReceiveResult(int resultCode, Bundle data);
}

private Receiver receiver;

/**
* Create a new EmojiResultReceiver to receive results. Your
* {@link #onReceiveResult} method will be called from the thread running
* <var>handler</var> if given, or from an arbitrary thread if null.
*/
public EmojiResultReceiver(Handler handler) {
super(handler);
}

public void setReceiver(Receiver receiver) {
this.receiver = receiver;
}

@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
if (receiver != null) {
receiver.onReceiveResult(resultCode, resultData);
}
}
}
2 changes: 1 addition & 1 deletion emoji/src/main/java/com/vanniktech/emoji/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static int getOrientation(final Context context) {
return context.getResources().getConfiguration().orientation;
}

static boolean shouldOverrideRegularCondition(@NonNull final Activity context, final EditText editText) {
static boolean shouldOverrideRegularCondition(@NonNull final Context context, final EditText editText) {
if ((editText.getImeOptions() & EditorInfo.IME_FLAG_NO_EXTRACT_UI) == 0) {
return getOrientation(context) == Configuration.ORIENTATION_LANDSCAPE;
}
Expand Down