2015-10-23 14:22:43 +02:00
|
|
|
package juloo.keyboard2;
|
|
|
|
|
2015-10-24 16:32:49 +02:00
|
|
|
import android.content.Context;
|
2015-10-26 14:19:46 +01:00
|
|
|
import android.content.SharedPreferences;
|
2015-10-23 14:22:43 +02:00
|
|
|
import android.graphics.Typeface;
|
2015-10-26 14:19:46 +01:00
|
|
|
import android.preference.PreferenceManager;
|
2015-10-24 16:32:49 +02:00
|
|
|
import android.util.AttributeSet;
|
2015-10-23 14:22:43 +02:00
|
|
|
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;
|
2015-10-26 14:19:46 +01:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.HashSet;
|
2015-10-23 14:22:43 +02:00
|
|
|
|
|
|
|
public class EmojiGridView extends GridView
|
|
|
|
implements GridView.OnItemClickListener
|
|
|
|
{
|
2015-10-26 14:19:46 +01:00
|
|
|
public static final int TYPE_LAST_USE = -1;
|
|
|
|
|
2015-10-23 14:22:43 +02:00
|
|
|
public static final int COLUMN_WIDTH = 192;
|
|
|
|
public static final float EMOJI_SIZE = 32.f;
|
|
|
|
|
2015-10-26 14:19:46 +01:00
|
|
|
private static final String LAST_USE_PREF = "emoji_last_use";
|
|
|
|
|
|
|
|
private Emoji[] _emojiArray;
|
|
|
|
private HashMap<Emoji, Integer> _lastUsed;
|
2015-10-24 16:32:49 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
** TODO: adapt column width and emoji size
|
2015-10-26 14:19:46 +01:00
|
|
|
** TODO: use ArraySet instead of Emoji[]
|
2015-10-24 16:32:49 +02:00
|
|
|
*/
|
|
|
|
public EmojiGridView(Context context, AttributeSet attrs)
|
2015-10-23 14:22:43 +02:00
|
|
|
{
|
2015-10-24 16:32:49 +02:00
|
|
|
super(context, attrs);
|
2015-10-23 14:22:43 +02:00
|
|
|
setOnItemClickListener(this);
|
|
|
|
setColumnWidth(COLUMN_WIDTH);
|
2015-10-26 14:19:46 +01:00
|
|
|
loadLastUsed();
|
|
|
|
setEmojiType((_lastUsed.size() == 0) ? Emoji.TYPE_EMOTICONS : TYPE_LAST_USE);
|
2015-10-24 16:32:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setEmojiType(int type)
|
|
|
|
{
|
2015-10-26 14:19:46 +01:00
|
|
|
_emojiArray = (type == TYPE_LAST_USE) ? getLastEmojis() : Emoji.getEmojisByType(type);
|
|
|
|
setAdapter(new EmojiViewAdpater((Keyboard2)getContext(), _emojiArray));
|
2015-10-23 14:22:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onItemClick(AdapterView<?> parent, View v, int pos, long id)
|
|
|
|
{
|
2015-10-24 16:32:49 +02:00
|
|
|
Keyboard2 main = (Keyboard2)getContext();
|
2015-10-26 14:19:46 +01:00
|
|
|
Integer used = _lastUsed.get(_emojiArray[pos]);
|
2015-10-24 16:32:49 +02:00
|
|
|
|
2015-10-26 14:19:46 +01:00
|
|
|
_lastUsed.put(_emojiArray[pos], (used == null) ? 1 : used.intValue() + 1);
|
|
|
|
main.handleKeyUp(_emojiArray[pos], 0);
|
2015-10-23 14:22:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onMeasure(int wSpec, int hSpec)
|
|
|
|
{
|
|
|
|
super.onMeasure(wSpec, hSpec);
|
|
|
|
setNumColumns(getMeasuredWidth() / COLUMN_WIDTH);
|
|
|
|
}
|
|
|
|
|
2015-10-26 14:19:46 +01:00
|
|
|
@Override
|
|
|
|
public void onDetachedFromWindow()
|
|
|
|
{
|
|
|
|
saveLastUsed();
|
|
|
|
}
|
|
|
|
|
|
|
|
private Emoji[] getLastEmojis()
|
|
|
|
{
|
|
|
|
final HashMap<Emoji, Integer> map = _lastUsed;
|
|
|
|
Emoji[] array = new Emoji[map.size()];
|
|
|
|
|
|
|
|
map.keySet().toArray(array);
|
|
|
|
Arrays.sort(array, 0, array.length, new Comparator<Emoji>()
|
|
|
|
{
|
|
|
|
public int compare(Emoji a, Emoji b)
|
|
|
|
{
|
|
|
|
return (map.get(b).intValue() - map.get(a).intValue());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return (array);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void saveLastUsed()
|
|
|
|
{
|
|
|
|
SharedPreferences.Editor edit = PreferenceManager.getDefaultSharedPreferences(getContext()).edit();
|
|
|
|
HashSet<String> set = new HashSet<String>();
|
|
|
|
|
|
|
|
for (Emoji emoji : _lastUsed.keySet())
|
|
|
|
set.add(String.valueOf(_lastUsed.get(emoji)) + "-" + emoji.getName());
|
|
|
|
edit.putStringSet(LAST_USE_PREF, set);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void loadLastUsed()
|
|
|
|
{
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
|
|
|
Set<String> lastUseSet = prefs.getStringSet(LAST_USE_PREF, null);
|
|
|
|
|
|
|
|
_lastUsed = new HashMap<Emoji, Integer>();
|
|
|
|
if (lastUseSet != null)
|
|
|
|
for (String emojiData : lastUseSet)
|
|
|
|
{
|
|
|
|
String[] emoji = emojiData.split("-", 1);
|
|
|
|
|
|
|
|
if (emoji.length != 2)
|
|
|
|
{
|
|
|
|
System.out.println("Warn: Bad emoji data: " + emojiData);
|
|
|
|
continue ;
|
|
|
|
}
|
|
|
|
_lastUsed.put((Emoji)KeyValue.getKeyByName(emoji[1]), Integer.getInteger(emoji[0]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-23 14:22:43 +02:00
|
|
|
private static class EmojiView extends TextView
|
|
|
|
{
|
|
|
|
private static ViewGroup.LayoutParams _layoutParams = null;
|
|
|
|
|
|
|
|
public EmojiView(Keyboard2 context)
|
|
|
|
{
|
|
|
|
super(context);
|
|
|
|
setTextSize(EMOJI_SIZE);
|
|
|
|
setGravity(Gravity.CENTER);
|
2015-10-26 14:19:46 +01:00
|
|
|
setBackgroundColor(getResources().getColor(R.color.emoji_emoji_bg));
|
2015-10-23 14:22:43 +02:00
|
|
|
if (_layoutParams == null)
|
|
|
|
_layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
|
|
|
|
setLayoutParams(_layoutParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setEmoji(Emoji emoji)
|
|
|
|
{
|
2015-10-24 16:32:49 +02:00
|
|
|
setText(emoji.getSymbol(0));
|
2015-10-23 14:22:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class EmojiViewAdpater extends BaseAdapter
|
|
|
|
{
|
2015-10-26 14:19:46 +01:00
|
|
|
private Keyboard2 _main;
|
2015-10-23 14:22:43 +02:00
|
|
|
|
2015-10-26 14:19:46 +01:00
|
|
|
private Emoji[] _emojiArray;
|
2015-10-23 14:22:43 +02:00
|
|
|
|
2015-10-26 14:19:46 +01:00
|
|
|
public EmojiViewAdpater(Keyboard2 main, Emoji[] emojiArray)
|
2015-10-23 14:22:43 +02:00
|
|
|
{
|
|
|
|
_main = main;
|
2015-10-26 14:19:46 +01:00
|
|
|
_emojiArray = emojiArray;
|
2015-10-23 14:22:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getCount()
|
|
|
|
{
|
2015-10-26 14:19:46 +01:00
|
|
|
if (_emojiArray == null)
|
2015-10-23 14:22:43 +02:00
|
|
|
return (0);
|
2015-10-26 14:19:46 +01:00
|
|
|
return (_emojiArray.length);
|
2015-10-23 14:22:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Object getItem(int pos)
|
|
|
|
{
|
2015-10-26 14:19:46 +01:00
|
|
|
return (_emojiArray[pos]);
|
2015-10-23 14:22:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2015-10-26 14:19:46 +01:00
|
|
|
view.setEmoji(_emojiArray[pos]);
|
2015-10-23 14:22:43 +02:00
|
|
|
return (view);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|