forked from vanniktech/Emoji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmojiPopup.java
274 lines (224 loc) · 10.5 KB
/
EmojiPopup.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package com.vanniktech.emoji;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.PopupWindow;
import com.vanniktech.emoji.emoji.Emoji;
import com.vanniktech.emoji.listeners.OnEmojiBackspaceClickListener;
import com.vanniktech.emoji.listeners.OnEmojiClickedListener;
import com.vanniktech.emoji.listeners.OnEmojiPopupDismissListener;
import com.vanniktech.emoji.listeners.OnEmojiPopupShownListener;
import com.vanniktech.emoji.listeners.OnSoftKeyboardCloseListener;
import com.vanniktech.emoji.listeners.OnSoftKeyboardOpenListener;
public final class EmojiPopup {
private static final int MIN_KEYBOARD_HEIGHT = 100;
private final EmojiEditText emojiEditText;
private final View rootView;
private final Context context;
private int keyBoardHeight;
private boolean isPendingOpen;
private boolean isKeyboardOpen;
@Nullable private OnEmojiPopupShownListener onEmojiPopupShownListener;
@Nullable private OnSoftKeyboardCloseListener onSoftKeyboardCloseListener;
@Nullable private OnSoftKeyboardOpenListener onSoftKeyboardOpenListener;
@Nullable private OnEmojiBackspaceClickListener onEmojiBackspaceClickListener;
@Nullable private OnEmojiClickedListener onEmojiClickedListener;
@Nullable private OnEmojiPopupDismissListener onEmojiPopupDismissListener;
@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();
this.rootView = rootView;
this.emojiEditText = emojiEditText;
this.recentEmoji = recent != null ? recent : new RecentEmojiManager(context);
popupWindow = new PopupWindow(context);
popupWindow.setBackgroundDrawable(new BitmapDrawable(context.getResources(), (Bitmap) null)); // To avoid borders & overdraw
final EmojiView emojiView = new EmojiView(context, new OnEmojiClickedListener() {
@Override
public void onEmojiClicked(final Emoji emoji) {
emojiEditText.input(emoji);
recentEmoji.addEmoji(emoji);
if (onEmojiClickedListener != null) {
onEmojiClickedListener.onEmojiClicked(emoji);
}
}
}, this.recentEmoji);
emojiView.setOnEmojiBackspaceClickListener(new OnEmojiBackspaceClickListener() {
@Override
public void onEmojiBackspaceClicked(final View v) {
emojiEditText.backspace();
if (onEmojiBackspaceClickListener != null) {
onEmojiBackspaceClickListener.onEmojiBackspaceClicked(v);
}
}
});
popupWindow.setContentView(emojiView);
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
popupWindow.setHeight((int) context.getResources().getDimension(R.dimen.emoji_keyboard_height));
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
if (onEmojiPopupDismissListener != null) {
onEmojiPopupDismissListener.onEmojiPopupDismiss();
}
}
});
}
private void showAtBottom() {
popupWindow.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
}
private void showAtBottomPending() {
if (isKeyboardOpen) {
showAtBottom();
} else {
isPendingOpen = true;
}
}
public void toggle() {
if (!popupWindow.isShowing()) {
rootView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
if (isKeyboardOpen) {
// If keyboard is visible, simply show the emoji popup
this.showAtBottom();
} else {
// Open the text keyboard first and immediately after that show the emoji popup
emojiEditText.setFocusableInTouchMode(true);
emojiEditText.requestFocus();
this.showAtBottomPending();
final InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(emojiEditText, InputMethodManager.SHOW_IMPLICIT);
}
if (onEmojiPopupShownListener != null) {
onEmojiPopupShownListener.onEmojiPopupShown();
}
} else {
dismiss();
}
}
public boolean isShowing() {
return popupWindow.isShowing();
}
public void dismiss() {
Utils.removeOnGlobalLayoutListener(rootView, onGlobalLayoutListener);
popupWindow.dismiss();
recentEmoji.persist();
}
private int getUsableScreenHeight() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
final DisplayMetrics metrics = new DisplayMetrics();
final WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(metrics);
return metrics.heightPixels;
} else {
return rootView.getRootView().getHeight();
}
}
public static final class Builder {
/**
* @param rootView the rootView of your layout.xml which will be used for calculating the height of the keyboard
* @return builder for building {@link EmojiPopup}
*/
public static Builder fromRootView(final View rootView) {
return new Builder(rootView);
}
private final View rootView;
@Nullable private OnEmojiPopupShownListener onEmojiPopupShownListener;
@Nullable private OnSoftKeyboardCloseListener onSoftKeyboardCloseListener;
@Nullable private OnSoftKeyboardOpenListener onSoftKeyboardOpenListener;
@Nullable private OnEmojiBackspaceClickListener onEmojiBackspaceClickListener;
@Nullable private OnEmojiClickedListener onEmojiClickedListener;
@Nullable private OnEmojiPopupDismissListener onEmojiPopupDismissListener;
@Nullable private RecentEmoji recentEmoji;
private Builder(final View rootView) {
this.rootView = rootView;
}
public Builder setOnSoftKeyboardCloseListener(@Nullable final OnSoftKeyboardCloseListener listener) {
this.onSoftKeyboardCloseListener = listener;
return this;
}
public Builder setOnEmojiClickedListener(@Nullable final OnEmojiClickedListener listener) {
this.onEmojiClickedListener = listener;
return this;
}
public Builder setOnSoftKeyboardOpenListener(@Nullable final OnSoftKeyboardOpenListener listener) {
this.onSoftKeyboardOpenListener = listener;
return this;
}
public Builder setOnEmojiPopupShownListener(@Nullable final OnEmojiPopupShownListener listener) {
this.onEmojiPopupShownListener = listener;
return this;
}
public Builder setOnEmojiPopupDismissListener(@Nullable final OnEmojiPopupDismissListener listener) {
this.onEmojiPopupDismissListener = listener;
return this;
}
public Builder setOnEmojiBackspaceClickListener(@Nullable final OnEmojiBackspaceClickListener listener) {
this.onEmojiBackspaceClickListener = listener;
return this;
}
/**
* allows you to pass your own implementation of recent emojis. If not provided the default one ({@link RecentEmojiManager} will be used
*
* @since 0.2.0
*/
public Builder setRecentEmoji(@Nullable final RecentEmoji recent) {
this.recentEmoji = recent;
return this;
}
public EmojiPopup build(final EmojiEditText emojiEditText) {
final EmojiPopup emojiPopup = new EmojiPopup(rootView, emojiEditText, recentEmoji);
emojiPopup.onSoftKeyboardCloseListener = onSoftKeyboardCloseListener;
emojiPopup.onEmojiClickedListener = onEmojiClickedListener;
emojiPopup.onSoftKeyboardOpenListener = onSoftKeyboardOpenListener;
emojiPopup.onEmojiPopupShownListener = onEmojiPopupShownListener;
emojiPopup.onEmojiPopupDismissListener = onEmojiPopupDismissListener;
emojiPopup.onEmojiBackspaceClickListener = onEmojiBackspaceClickListener;
return emojiPopup;
}
}
}