Skip to content

Commit

Permalink
Remove Global Layout listener when dismissing the popup. Fixes #22 (#24)
Browse files Browse the repository at this point in the history
Remove Global Layout listener when dismissing the popup. Fixes #22 (#24)
  • Loading branch information
vanniktech committed Apr 28, 2016
1 parent 14d871b commit 0f0b727
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 45 deletions.
89 changes: 44 additions & 45 deletions library/src/main/java/com/vanniktech/emoji/EmojiPopup.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,47 @@ public final class EmojiPopup {
@NonNull private final RecentEmoji recentEmoji;

private final PopupWindow popupWindow;
private final ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
final Rect rect = new Rect();
rootView.getWindowVisibleDisplayFrame(rect);

int heightDifference = getUsableScreenHeight() - (rect.bottom - rect.top);

final Resources resources = context.getResources();
final int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");

if (resourceId > 0) {
heightDifference -= resources.getDimensionPixelSize(resourceId);
}

if (heightDifference > MIN_KEYBOARD_HEIGHT) {
keyBoardHeight = heightDifference;
popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
popupWindow.setHeight(keyBoardHeight);

if (!isKeyboardOpen && onSoftKeyboardOpenListener != null) {
onSoftKeyboardOpenListener.onKeyboardOpen(keyBoardHeight);
}

isKeyboardOpen = true;

if (isPendingOpen) {
showAtBottom();
isPendingOpen = false;
}
} else {
if (isKeyboardOpen) {
isKeyboardOpen = false;

if (onSoftKeyboardCloseListener != null) {
onSoftKeyboardCloseListener.onKeyboardClose();
}
}
}
}
};

private EmojiPopup(final View rootView, final EmojiEditText emojiEditText, @Nullable final RecentEmoji recent) {
this.context = rootView.getContext();
Expand Down Expand Up @@ -90,7 +131,6 @@ public void onDismiss() {
}
}
});
setSizeForSoftKeyboard();
}

private void showAtBottom() {
Expand All @@ -107,6 +147,8 @@ private void showAtBottomPending() {

public void toggle() {
if (!popupWindow.isShowing()) {
rootView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);

if (isKeyboardOpen) {
// If keyboard is visible, simply show the emoji popup
this.showAtBottom();
Expand Down Expand Up @@ -134,54 +176,11 @@ public boolean isShowing() {
}

public void dismiss() {
Utils.removeOnGlobalLayoutListener(rootView, onGlobalLayoutListener);
popupWindow.dismiss();
recentEmoji.persist();
}

private void setSizeForSoftKeyboard() {
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
final Rect rect = new Rect();
rootView.getWindowVisibleDisplayFrame(rect);

int heightDifference = getUsableScreenHeight() - (rect.bottom - rect.top);

final Resources resources = context.getResources();
final int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");

if (resourceId > 0) {
heightDifference -= resources.getDimensionPixelSize(resourceId);
}

if (heightDifference > MIN_KEYBOARD_HEIGHT) {
keyBoardHeight = heightDifference;
popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
popupWindow.setHeight(keyBoardHeight);

if (!isKeyboardOpen && onSoftKeyboardOpenListener != null) {
onSoftKeyboardOpenListener.onKeyboardOpen(keyBoardHeight);
}

isKeyboardOpen = true;

if (isPendingOpen) {
showAtBottom();
isPendingOpen = false;
}
} else {
if (isKeyboardOpen) {
isKeyboardOpen = false;

if (onSoftKeyboardCloseListener != null) {
onSoftKeyboardCloseListener.onKeyboardClose();
}
}
}
}
});
}

private int getUsableScreenHeight() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
final DisplayMetrics metrics = new DisplayMetrics();
Expand Down
22 changes: 22 additions & 0 deletions library/src/main/java/com/vanniktech/emoji/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.vanniktech.emoji;

import android.annotation.TargetApi;
import android.os.Build;
import android.view.View;
import android.view.ViewTreeObserver;

final class Utils {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static void removeOnGlobalLayoutListener(final View v, final ViewTreeObserver.OnGlobalLayoutListener listener){
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
//noinspection deprecation
v.getViewTreeObserver().removeGlobalOnLayoutListener(listener);
} else {
v.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
}
}

private Utils() {
throw new AssertionError("No instances.");
}
}
13 changes: 13 additions & 0 deletions library/src/test/java/com/vanniktech/emoji/UtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.vanniktech.emoji;


import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;

import org.junit.Test;

public class UtilsTest {
@Test
public void constructorShouldBePrivate() {
PrivateConstructorChecker.forClass(Utils.class).expectedTypeOfException(AssertionError.class).expectedExceptionMessage("No instances.").check();
}
}

0 comments on commit 0f0b727

Please sign in to comment.