abouttreesummaryrefslogcommitdiff
path: root/srcs/juloo.keyboard2/EmojiGridView.java
blob: fd94aee519e809dfb525f3e7240196786aa33718 (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
package juloo.keyboard2;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

public class EmojiGridView extends GridView
	implements GridView.OnItemClickListener
{
	public static final int			COLUMN_WIDTH = 192;
	public static final float		EMOJI_SIZE = 32.f;

	private int				_emojiType = Emoji.TYPE_EMOTICONS;

	/*
	** TODO: save last emoji type
	** TODO: adapt column width and emoji size
	*/
	public EmojiGridView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		setOnItemClickListener(this);
		setColumnWidth(COLUMN_WIDTH);
		setEmojiType(Emoji.TYPE_EMOTICONS);
	}

	/*
	** TODO: type (-1) for lastest used
	*/
	public void			setEmojiType(int type)
	{
		_emojiType = type;
		setAdapter(new EmojiViewAdpater((Keyboard2)getContext(), type));
	}

	public void			onItemClick(AdapterView<?> parent, View v, int pos, long id)
	{
		Keyboard2			main = (Keyboard2)getContext();

		main.handleKeyUp(Emoji.getEmojisByType(_emojiType)[pos], 0);
	}

	@Override
	public void			onMeasure(int wSpec, int hSpec)
	{
		super.onMeasure(wSpec, hSpec);
		setNumColumns(getMeasuredWidth() / COLUMN_WIDTH);
	}

	private static class EmojiView extends TextView
	{
		private static ViewGroup.LayoutParams	_layoutParams = null;

		public EmojiView(Keyboard2 context)
		{
			super(context);
			setTextSize(EMOJI_SIZE);
			setGravity(Gravity.CENTER);
			if (_layoutParams == null)
				_layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
			setLayoutParams(_layoutParams);
		}

		public void			setEmoji(Emoji emoji)
		{
			setText(emoji.getSymbol(0));
		}
	}

	private static class EmojiViewAdpater extends BaseAdapter
	{
		private Keyboard2	_main;

		private Emoji[]		_emojiSet = null;

		public EmojiViewAdpater(Keyboard2 main, int type)
		{
			_main = main;
			_emojiSet = Emoji.getEmojisByType(type);
		}

		public int			getCount()
		{
			if (_emojiSet == null)
				return (0);
			return (_emojiSet.length);
		}

		public Object		getItem(int pos)
		{
			return (_emojiSet[pos]);
		}

		public long			getItemId(int pos)
		{
			return (pos);
		}

		public View			getView(int pos, View convertView, ViewGroup parent)
		{
			EmojiView			view = (EmojiView)convertView;

			if (view == null)
				view = new EmojiView(_main);
			view.setEmoji(_emojiSet[pos]);
			return (view);
		}
	}
}