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-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
|
2021-12-19 19:44:27 +01:00
|
|
|
implements GridView.OnItemClickListener
|
2015-10-23 14:22:43 +02:00
|
|
|
{
|
2021-12-19 19:44:27 +01:00
|
|
|
public static final int GROUP_LAST_USE = -1;
|
2015-10-26 14:19:46 +01:00
|
|
|
|
2021-12-19 19:44:27 +01:00
|
|
|
public static final int COLUMN_WIDTH = 192;
|
2015-10-23 14:22:43 +02:00
|
|
|
|
2021-12-19 19:44:27 +01:00
|
|
|
private static final String LAST_USE_PREF = "emoji_last_use";
|
2015-10-26 14:19:46 +01:00
|
|
|
|
2021-12-19 19:44:27 +01:00
|
|
|
private Emoji[] _emojiArray;
|
|
|
|
private HashMap<Emoji, Integer> _lastUsed;
|
2015-10-24 16:32:49 +02:00
|
|
|
|
2021-12-19 19:44:27 +01:00
|
|
|
/*
|
|
|
|
** TODO: adapt column width and emoji size
|
|
|
|
** TODO: use ArraySet instead of Emoji[]
|
|
|
|
*/
|
|
|
|
public EmojiGridView(Context context, AttributeSet attrs)
|
|
|
|
{
|
|
|
|
super(context, attrs);
|
2021-01-16 21:57:51 +01:00
|
|
|
Emoji.init(context.getResources());
|
2021-12-19 19:44:27 +01:00
|
|
|
setOnItemClickListener(this);
|
|
|
|
setColumnWidth(COLUMN_WIDTH);
|
|
|
|
loadLastUsed();
|
|
|
|
setEmojiGroup((_lastUsed.size() == 0) ? 0 : GROUP_LAST_USE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setEmojiGroup(int group)
|
|
|
|
{
|
|
|
|
_emojiArray = (group == GROUP_LAST_USE) ? getLastEmojis() : Emoji.getEmojisByGroup(group);
|
2021-12-28 17:47:18 +01:00
|
|
|
setAdapter(new EmojiViewAdpater(getContext(), _emojiArray));
|
2021-12-19 19:44:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onItemClick(AdapterView<?> parent, View v, int pos, long id)
|
|
|
|
{
|
2021-12-28 17:47:18 +01:00
|
|
|
Config config = Config.globalConfig();
|
2021-12-19 19:44:27 +01:00
|
|
|
Integer used = _lastUsed.get(_emojiArray[pos]);
|
|
|
|
_lastUsed.put(_emojiArray[pos], (used == null) ? 1 : used.intValue() + 1);
|
2022-06-05 20:14:16 +02:00
|
|
|
config.handler.handleKeyUp(_emojiArray[pos].kv(), Pointers.Modifiers.EMPTY);
|
2021-12-19 19:44:27 +01:00
|
|
|
saveLastUsed(); // TODO: opti
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onMeasure(int wSpec, int hSpec)
|
|
|
|
{
|
|
|
|
super.onMeasure(wSpec, hSpec);
|
|
|
|
setNumColumns(getMeasuredWidth() / COLUMN_WIDTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2022-11-11 14:27:02 +01:00
|
|
|
SharedPreferences.Editor edit = emojiSharedPreferences().edit();
|
2021-12-19 19:44:27 +01:00
|
|
|
HashSet<String> set = new HashSet<String>();
|
|
|
|
for (Emoji emoji : _lastUsed.keySet())
|
2022-06-05 20:14:16 +02:00
|
|
|
set.add(String.valueOf(_lastUsed.get(emoji)) + "-" + emoji.name());
|
2021-12-19 19:44:27 +01:00
|
|
|
edit.putStringSet(LAST_USE_PREF, set);
|
|
|
|
edit.apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void loadLastUsed()
|
|
|
|
{
|
2022-11-11 14:27:02 +01:00
|
|
|
SharedPreferences prefs = emojiSharedPreferences();
|
2021-12-19 19:44:27 +01:00
|
|
|
Set<String> lastUseSet = prefs.getStringSet(LAST_USE_PREF, null);
|
|
|
|
_lastUsed = new HashMap<Emoji, Integer>();
|
|
|
|
if (lastUseSet != null)
|
|
|
|
for (String emojiData : lastUseSet)
|
|
|
|
{
|
|
|
|
String[] data = emojiData.split("-", 2);
|
|
|
|
Emoji emoji;
|
|
|
|
|
|
|
|
if (data.length != 2)
|
|
|
|
continue ;
|
|
|
|
emoji = Emoji.getEmojiByName(data[1]);
|
|
|
|
if (emoji == null)
|
|
|
|
continue ;
|
|
|
|
_lastUsed.put(emoji, Integer.valueOf(data[0]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-11 14:27:02 +01:00
|
|
|
SharedPreferences emojiSharedPreferences()
|
|
|
|
{
|
|
|
|
return getContext().getSharedPreferences("emoji_last_use", Context.MODE_PRIVATE);
|
|
|
|
}
|
|
|
|
|
2021-12-19 19:44:27 +01:00
|
|
|
private static class EmojiView extends TextView
|
|
|
|
{
|
2021-12-28 17:47:18 +01:00
|
|
|
public EmojiView(Context context)
|
2021-12-19 19:44:27 +01:00
|
|
|
{
|
|
|
|
super(context);
|
2021-12-28 19:12:50 +01:00
|
|
|
setTextAppearance(context, R.style.emojiGridButton);
|
2021-12-19 19:44:27 +01:00
|
|
|
setGravity(Gravity.CENTER);
|
|
|
|
setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, GridView.LayoutParams.WRAP_CONTENT));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setEmoji(Emoji emoji)
|
|
|
|
{
|
2022-06-05 20:14:16 +02:00
|
|
|
setText(emoji.kv().getString());
|
2021-12-19 19:44:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class EmojiViewAdpater extends BaseAdapter
|
|
|
|
{
|
2021-12-28 17:47:18 +01:00
|
|
|
private Context _context;
|
2021-12-19 19:44:27 +01:00
|
|
|
|
|
|
|
private Emoji[] _emojiArray;
|
|
|
|
|
2021-12-28 17:47:18 +01:00
|
|
|
public EmojiViewAdpater(Context context, Emoji[] emojiArray)
|
2021-12-19 19:44:27 +01:00
|
|
|
{
|
2021-12-28 17:47:18 +01:00
|
|
|
_context = context;
|
2021-12-19 19:44:27 +01:00
|
|
|
_emojiArray = emojiArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getCount()
|
|
|
|
{
|
|
|
|
if (_emojiArray == null)
|
|
|
|
return (0);
|
|
|
|
return (_emojiArray.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Object getItem(int pos)
|
|
|
|
{
|
|
|
|
return (_emojiArray[pos]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getItemId(int pos)
|
|
|
|
{
|
|
|
|
return (pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
public View getView(int pos, View convertView, ViewGroup parent)
|
|
|
|
{
|
|
|
|
EmojiView view = (EmojiView)convertView;
|
|
|
|
|
|
|
|
if (view == null)
|
2021-12-28 17:47:18 +01:00
|
|
|
view = new EmojiView(_context);
|
2021-12-19 19:44:27 +01:00
|
|
|
view.setEmoji(_emojiArray[pos]);
|
|
|
|
return (view);
|
|
|
|
}
|
|
|
|
}
|
2015-10-23 14:22:43 +02:00
|
|
|
}
|