aboutsummaryrefslogtreecommitdiffstats
path: root/src/github/ankushsachdeva/emojicon/EmojiconsPopup.java
blob: f6c0fbdfb8ead01fbab98adc5266a88eee082fa0 (plain)
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 * Copyright 2014 Ankush Sachdeva
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package github.ankushsachdeva.emojicon;

import github.ankushsachdeva.emojicon.EmojiconGridView.OnEmojiconClickedListener;
import github.ankushsachdeva.emojicon.emoji.Emojicon;
import github.ankushsachdeva.emojicon.emoji.Nature;
import github.ankushsachdeva.emojicon.emoji.Objects;
import github.ankushsachdeva.emojicon.emoji.People;
import github.ankushsachdeva.emojicon.emoji.Places;
import github.ankushsachdeva.emojicon.emoji.Symbols;

import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.WindowManager.LayoutParams;
import android.widget.EditText;
import android.widget.PopupWindow;

import github.ankushsachdeva.emojicon.R;


/**
 * @author Ankush Sachdeva (sankush@yahoo.co.in).
 */

public class EmojiconsPopup extends PopupWindow implements ViewPager.OnPageChangeListener, EmojiconRecents {
	private int mEmojiTabLastSelectedIndex = -1;
	private View[] mEmojiTabs;
	private PagerAdapter mEmojisAdapter;
	private EmojiconRecentsManager mRecentsManager;
	private int keyBoardHeight = 0;
	private Boolean isOpened = false;
	OnEmojiconClickedListener onEmojiconClickedListener;
	OnEmojiconBackspaceClickedListener onEmojiconBackspaceClickedListener; 
	OnSoftKeyboardOpenCloseListener onSoftKeyboardOpenCloseListener;
	View rootView;
	Context mContext;
	
	public ViewPager emojisPager;
	
	public EmojiconsPopup(View rootView, Context mContext){
		super(mContext);
		this.mContext = mContext;
		this.rootView = rootView;
		View customView = createCustomView();
		setContentView(customView);
		
		setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
		//default size 
		setSize((int) mContext.getResources().getDimension(R.dimen.keyboard_height), LayoutParams.MATCH_PARENT);
	}
	
	public void setOnSoftKeyboardOpenCloseListener(OnSoftKeyboardOpenCloseListener listener){
		this.onSoftKeyboardOpenCloseListener = listener; 
	}
	
	public void setOnEmojiconClickedListener(OnEmojiconClickedListener listener){
		this.onEmojiconClickedListener = listener;
	}
	
	public void setOnEmojiconBackspaceClickedListener(OnEmojiconBackspaceClickedListener listener){
		this.onEmojiconBackspaceClickedListener = listener;
	}
	
	public void showAtBottom(){
		showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
	}
	
	public Boolean isKeyBoardOpen(){
		return isOpened;
	}
	
	public void setSizeForSoftKeyboard(){
		rootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
	        @Override
	        public void onGlobalLayout() {
        	     Rect r = new Rect();
        	     rootView.getWindowVisibleDisplayFrame(r);

                 int screenHeight = rootView.getRootView()
                         .getHeight();
                 int heightDifference = screenHeight
                         - (r.bottom - r.top);
                 int resourceId = mContext.getResources()
                         .getIdentifier("status_bar_height",
                                 "dimen", "android");
                 if (resourceId > 0) {
                     heightDifference -= mContext.getResources()
                             .getDimensionPixelSize(resourceId);
                 }
                 if (heightDifference > 100) {
                	 keyBoardHeight = heightDifference;
                	 setSize(LayoutParams.MATCH_PARENT, keyBoardHeight);
                	 if(isOpened == false){
 	                    if(onSoftKeyboardOpenCloseListener!=null)
 	                    	onSoftKeyboardOpenCloseListener.onKeyboardOpen(keyBoardHeight);
 	                }
                	isOpened = true;
                 }
                 else{
                	 isOpened = false;
                	 if(onSoftKeyboardOpenCloseListener!=null)
	                    	onSoftKeyboardOpenCloseListener.onKeyboardClose();
                 }
	         }
	    });
	}
	
	public void setSize(int width, int height){
		setWidth(width);
		setHeight(height);
	}
	
	public View createCustomView() {
		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.emojicons, null, false);
		emojisPager = (ViewPager) view.findViewById(R.id.emojis_pager);
		emojisPager.setOnPageChangeListener(this);
		EmojiconRecents recents = this;
		mEmojisAdapter = new EmojisPagerAdapter(
		Arrays.asList(
				new EmojiconRecentsGridView(mContext, null, null, this),
				new EmojiconGridView(mContext, People.DATA, recents, this),
				new EmojiconGridView(mContext, Nature.DATA, recents, this),
				new EmojiconGridView(mContext, Objects.DATA, recents, this),
				new EmojiconGridView(mContext, Places.DATA, recents, this),
				new EmojiconGridView(mContext, Symbols.DATA, recents, this)
				)
			);
		emojisPager.setAdapter(mEmojisAdapter);
		mEmojiTabs = new View[6];
		mEmojiTabs[0] = view.findViewById(R.id.emojis_tab_0_recents);
		mEmojiTabs[1] = view.findViewById(R.id.emojis_tab_1_people);
		mEmojiTabs[2] = view.findViewById(R.id.emojis_tab_2_nature);
		mEmojiTabs[3] = view.findViewById(R.id.emojis_tab_3_objects);
		mEmojiTabs[4] = view.findViewById(R.id.emojis_tab_4_cars);
		mEmojiTabs[5] = view.findViewById(R.id.emojis_tab_5_punctuation);
		for (int i = 0; i < mEmojiTabs.length; i++) {
			final int position = i;
			mEmojiTabs[i].setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					emojisPager.setCurrentItem(position);
				}
			});
		}
		view.findViewById(R.id.emojis_backspace).setOnTouchListener(new RepeatListener(1000, 50, new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(onEmojiconBackspaceClickedListener != null)
					onEmojiconBackspaceClickedListener.onEmojiconBackspaceClicked(v);
			}
		}));

		// get last selected page
		mRecentsManager = EmojiconRecentsManager.getInstance(view.getContext());
		int page = mRecentsManager.getRecentPage();
		// last page was recents, check if there are recents to use
		// if none was found, go to page 1
		if (page == 0 && mRecentsManager.size() == 0) {
			page = 1;
		}

		if (page == 0) {
			onPageSelected(page);
		}
		else {
			emojisPager.setCurrentItem(page, false);
		}
		return view;
	}

	public static void input(EditText editText, Emojicon emojicon) {
		if (editText == null || emojicon == null) {
			return;
		}

		int start = editText.getSelectionStart();
		int end = editText.getSelectionEnd();
		if (start < 0) {
			editText.append(emojicon.getEmoji());
		} else {
			editText.getText().replace(Math.min(start, end), Math.max(start, end), emojicon.getEmoji(), 0, emojicon.getEmoji().length());
		}
	}

	@Override
	public void addRecentEmoji(Context context, Emojicon emojicon) {
		EmojiconRecentsGridView fragment = ((EmojisPagerAdapter)emojisPager.getAdapter()).getRecentFragment();
		fragment.addRecentEmoji(context, emojicon);
	}

	public static void backspace(EditText editText) {
		KeyEvent event = new KeyEvent(0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL);
		editText.dispatchKeyEvent(event);
	}

	@Override
	public void onPageScrolled(int i, float v, int i2) {
	}

	@Override
	public void onPageSelected(int i) {
		if (mEmojiTabLastSelectedIndex == i) {
			return;
		}
		switch (i) {
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
			if (mEmojiTabLastSelectedIndex >= 0 && mEmojiTabLastSelectedIndex < mEmojiTabs.length) {
				mEmojiTabs[mEmojiTabLastSelectedIndex].setSelected(false);
			}
			mEmojiTabs[i].setSelected(true);
			mEmojiTabLastSelectedIndex = i;
			mRecentsManager.setRecentPage(i);
			break;
		}
	}

	@Override
	public void onPageScrollStateChanged(int i) {
	}

	private static class EmojisPagerAdapter extends PagerAdapter {
		private List<EmojiconGridView> views;
		public EmojiconRecentsGridView getRecentFragment(){
			for (EmojiconGridView it : views) {
				if(it instanceof EmojiconRecentsGridView)
					return (EmojiconRecentsGridView)it;
			}
			return null;
		}
		public EmojisPagerAdapter(List<EmojiconGridView> views) {
			super();
			this.views = views;
		}

		@Override
		public int getCount() {
			return views.size();
		}
		
		
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			View v = views.get(position).rootView;
			((ViewPager)container).addView(v, 0);
			return v;
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object view) {
			((ViewPager)container).removeView((View)view);
		}
		
		@Override
		public boolean isViewFromObject(View view, Object key) {
			return key == view;
		}
	}

	/**
	 * A class, that can be used as a TouchListener on any view (e.g. a Button).
	 * It cyclically runs a clickListener, emulating keyboard-like behaviour. First
	 * click is fired immediately, next before initialInterval, and subsequent before
	 * normalInterval.
	 * <p/>
	 * <p>Interval is scheduled before the onClick completes, so it has to run fast.
	 * If it runs slow, it does not generate skipped onClicks.
	 */
	public static class RepeatListener implements View.OnTouchListener {

		private Handler handler = new Handler();

		private int initialInterval;
		private final int normalInterval;
		private final View.OnClickListener clickListener;

		private Runnable handlerRunnable = new Runnable() {
			@Override
			public void run() {
				if (downView == null) {
					return;
				}
				handler.removeCallbacksAndMessages(downView);
				handler.postAtTime(this, downView, SystemClock.uptimeMillis() + normalInterval);
				clickListener.onClick(downView);
			}
		};

		private View downView;

		/**
		 * @param initialInterval The interval before first click event
		 * @param normalInterval  The interval before second and subsequent click
		 *                        events
		 * @param clickListener   The OnClickListener, that will be called
		 *                        periodically
		 */
		public RepeatListener(int initialInterval, int normalInterval, View.OnClickListener clickListener) {
			if (clickListener == null)
				throw new IllegalArgumentException("null runnable");
			if (initialInterval < 0 || normalInterval < 0)
				throw new IllegalArgumentException("negative interval");

			this.initialInterval = initialInterval;
			this.normalInterval = normalInterval;
			this.clickListener = clickListener;
		}

		public boolean onTouch(View view, MotionEvent motionEvent) {
			switch (motionEvent.getAction()) {
			case MotionEvent.ACTION_DOWN:
				downView = view;
				handler.removeCallbacks(handlerRunnable);
				handler.postAtTime(handlerRunnable, downView, SystemClock.uptimeMillis() + initialInterval);
				clickListener.onClick(view);
				return true;
			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_CANCEL:
			case MotionEvent.ACTION_OUTSIDE:
				handler.removeCallbacksAndMessages(downView);
				downView = null;
				return true;
			}
			return false;
		}
	}

	public interface OnEmojiconBackspaceClickedListener {
		void onEmojiconBackspaceClicked(View v);
	}
	
	public interface OnSoftKeyboardOpenCloseListener{
		void onKeyboardOpen(int keyBoardHeight);
		void onKeyboardClose();
	}
}