Skip to content

Commit

Permalink
Fix memory leaks
Browse files Browse the repository at this point in the history
Signed-off-by: Mario Danic <[email protected]>
  • Loading branch information
mario committed Feb 12, 2019
1 parent 896555e commit ac1d9ed
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
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();
}
}
}
}

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 ResultReceive 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

0 comments on commit ac1d9ed

Please sign in to comment.