Last used emoji

This commit is contained in:
jaguillo 2015-10-26 14:19:46 +01:00
parent 8bd0214e26
commit e95ccbec00
7 changed files with 109 additions and 33 deletions

View File

@ -7,6 +7,15 @@
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
> >
<juloo.keyboard2.EmojiTypeButton
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/emoji_button_bg"
android:padding="0px"
android:text="\uD83D\uDD59"
emoji_type="LAST_USE"
/>
<juloo.keyboard2.EmojiTypeButton <juloo.keyboard2.EmojiTypeButton
android:layout_width="0px" android:layout_width="0px"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View File

@ -7,6 +7,7 @@
<item name="key_label_locked" type="color">#229933</item> <item name="key_label_locked" type="color">#229933</item>
<item name="key_sub_label" type="color">#BDBDBD</item> <item name="key_sub_label" type="color">#BDBDBD</item>
<item name="emoji_button_bg" type="color">#202020</item> <item name="emoji_button_bg" type="color">#202020</item>
<item name="emoji_emoji_bg" type="color">#00FFFFFF</item>
<item name="emoji_key_bg" type="color">@color/emoji_button_bg</item> <item name="emoji_key_bg" type="color">@color/emoji_button_bg</item>
<item name="emoji_key_text" type="color">@color/key_label</item> <item name="emoji_key_text" type="color">@color/key_label</item>
</resources> </resources>

View File

@ -39,6 +39,6 @@
<key width="1.0" key0="alt" key1="page_up" key2="end" key3="home" key4="page_down" /> <key width="1.0" key0="alt" key1="page_up" key2="end" key3="home" key4="page_down" />
<key width="4.4" key0="space" /> <key width="4.4" key0="space" />
<key key1="up" key2="right" key3="left" key4="down" /> <key key1="up" key2="right" key3="left" key4="down" />
<key width="1.8" key0="enter" key1="config" /> <key width="1.8" key0="enter" key1="config" key2="switch_emoji" />
</row> </row>
</keyboard> </keyboard>

View File

@ -10,14 +10,21 @@ public class Emoji extends KeyValue
public static final int TYPE_UNCATEGORIZED = 4; public static final int TYPE_UNCATEGORIZED = 4;
public static final int TYPE_ENCLOSED_CHARACTERS = 5; public static final int TYPE_ENCLOSED_CHARACTERS = 5;
private final String _emojiName;
private final String _desc; private final String _desc;
protected Emoji(String name, String bytecode, String desc) protected Emoji(String name, String bytecode, String desc)
{ {
super(name, bytecode, CHAR_NONE, EVENT_NONE, 0); super(bytecode, bytecode, CHAR_NONE, EVENT_NONE, 0);
_emojiName = name;
_desc = desc; _desc = desc;
} }
public String getEmojiName()
{
return (_emojiName);
}
public String getDescription() public String getDescription()
{ {
return (_desc); return (_desc);

View File

@ -1,7 +1,9 @@
package juloo.keyboard2; package juloo.keyboard2;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.preference.PreferenceManager;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.Gravity; import android.view.Gravity;
import android.view.View; import android.view.View;
@ -10,41 +12,51 @@ import android.widget.AdapterView;
import android.widget.BaseAdapter; import android.widget.BaseAdapter;
import android.widget.GridView; import android.widget.GridView;
import android.widget.TextView; import android.widget.TextView;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
public class EmojiGridView extends GridView public class EmojiGridView extends GridView
implements GridView.OnItemClickListener implements GridView.OnItemClickListener
{ {
public static final int TYPE_LAST_USE = -1;
public static final int COLUMN_WIDTH = 192; public static final int COLUMN_WIDTH = 192;
public static final float EMOJI_SIZE = 32.f; public static final float EMOJI_SIZE = 32.f;
private int _emojiType = Emoji.TYPE_EMOTICONS; private static final String LAST_USE_PREF = "emoji_last_use";
private Emoji[] _emojiArray;
private HashMap<Emoji, Integer> _lastUsed;
/* /*
** TODO: save last emoji type
** TODO: adapt column width and emoji size ** TODO: adapt column width and emoji size
** TODO: use ArraySet instead of Emoji[]
*/ */
public EmojiGridView(Context context, AttributeSet attrs) public EmojiGridView(Context context, AttributeSet attrs)
{ {
super(context, attrs); super(context, attrs);
setOnItemClickListener(this); setOnItemClickListener(this);
setColumnWidth(COLUMN_WIDTH); setColumnWidth(COLUMN_WIDTH);
setEmojiType(Emoji.TYPE_EMOTICONS); loadLastUsed();
setEmojiType((_lastUsed.size() == 0) ? Emoji.TYPE_EMOTICONS : TYPE_LAST_USE);
} }
/*
** TODO: type (-1) for lastest used
*/
public void setEmojiType(int type) public void setEmojiType(int type)
{ {
_emojiType = type; _emojiArray = (type == TYPE_LAST_USE) ? getLastEmojis() : Emoji.getEmojisByType(type);
setAdapter(new EmojiViewAdpater((Keyboard2)getContext(), type)); setAdapter(new EmojiViewAdpater((Keyboard2)getContext(), _emojiArray));
} }
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) public void onItemClick(AdapterView<?> parent, View v, int pos, long id)
{ {
Keyboard2 main = (Keyboard2)getContext(); Keyboard2 main = (Keyboard2)getContext();
Integer used = _lastUsed.get(_emojiArray[pos]);
main.handleKeyUp(Emoji.getEmojisByType(_emojiType)[pos], 0); _lastUsed.put(_emojiArray[pos], (used == null) ? 1 : used.intValue() + 1);
main.handleKeyUp(_emojiArray[pos], 0);
} }
@Override @Override
@ -54,6 +66,58 @@ public class EmojiGridView extends GridView
setNumColumns(getMeasuredWidth() / COLUMN_WIDTH); setNumColumns(getMeasuredWidth() / COLUMN_WIDTH);
} }
@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]));
}
}
private static class EmojiView extends TextView private static class EmojiView extends TextView
{ {
private static ViewGroup.LayoutParams _layoutParams = null; private static ViewGroup.LayoutParams _layoutParams = null;
@ -63,6 +127,7 @@ public class EmojiGridView extends GridView
super(context); super(context);
setTextSize(EMOJI_SIZE); setTextSize(EMOJI_SIZE);
setGravity(Gravity.CENTER); setGravity(Gravity.CENTER);
setBackgroundColor(getResources().getColor(R.color.emoji_emoji_bg));
if (_layoutParams == null) if (_layoutParams == null)
_layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT); _layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
setLayoutParams(_layoutParams); setLayoutParams(_layoutParams);
@ -76,26 +141,26 @@ public class EmojiGridView extends GridView
private static class EmojiViewAdpater extends BaseAdapter private static class EmojiViewAdpater extends BaseAdapter
{ {
private Keyboard2 _main; private Keyboard2 _main;
private Emoji[] _emojiSet = null; private Emoji[] _emojiArray;
public EmojiViewAdpater(Keyboard2 main, int type) public EmojiViewAdpater(Keyboard2 main, Emoji[] emojiArray)
{ {
_main = main; _main = main;
_emojiSet = Emoji.getEmojisByType(type); _emojiArray = emojiArray;
} }
public int getCount() public int getCount()
{ {
if (_emojiSet == null) if (_emojiArray == null)
return (0); return (0);
return (_emojiSet.length); return (_emojiArray.length);
} }
public Object getItem(int pos) public Object getItem(int pos)
{ {
return (_emojiSet[pos]); return (_emojiArray[pos]);
} }
public long getItemId(int pos) public long getItemId(int pos)
@ -109,7 +174,7 @@ public class EmojiGridView extends GridView
if (view == null) if (view == null)
view = new EmojiView(_main); view = new EmojiView(_main);
view.setEmoji(_emojiSet[pos]); view.setEmoji(_emojiArray[pos]);
return (view); return (view);
} }
} }

View File

@ -33,6 +33,8 @@ public class EmojiTypeButton extends Button
public static int getTypeByString(String str) public static int getTypeByString(String str)
{ {
// caca // caca
if (str.equals("LAST_USE"))
return (EmojiGridView.TYPE_LAST_USE);
if (str.equals("EMOTICONS")) if (str.equals("EMOTICONS"))
return (Emoji.TYPE_EMOTICONS); return (Emoji.TYPE_EMOTICONS);
if (str.equals("DINGBATS")) if (str.equals("DINGBATS"))

View File

@ -65,11 +65,6 @@ public class Keyboard2 extends InputMethodService
_keyboardView.reset_prefs(); _keyboardView.reset_prefs();
} }
@Override
public void onAppPrivateCommand(String command, Bundle data)
{
}
@Override @Override
public void onConfigurationChanged(Configuration newConfig) public void onConfigurationChanged(Configuration newConfig)
{ {
@ -109,7 +104,11 @@ public class Keyboard2 extends InputMethodService
else if (eventCode == KeyValue.EVENT_SWITCH_NUMERIC) else if (eventCode == KeyValue.EVENT_SWITCH_NUMERIC)
_keyboardView.setKeyboard(_numericKeyboard); _keyboardView.setKeyboard(_numericKeyboard);
else if (eventCode == KeyValue.EVENT_SWITCH_EMOJI) else if (eventCode == KeyValue.EVENT_SWITCH_EMOJI)
setInputView(getEmojiPane()); {
if (_emojiPane == null)
_emojiPane = (ViewGroup)getLayoutInflater().inflate(R.layout.emoji_pane, null);
setInputView(_emojiPane);
}
else if (eventCode == KeyValue.EVENT_SWITCH_BACK_EMOJI) else if (eventCode == KeyValue.EVENT_SWITCH_BACK_EMOJI)
setInputView(_keyboardView); setInputView(_keyboardView);
else if ((flags & (KeyValue.FLAG_CTRL | KeyValue.FLAG_ALT)) != 0) else if ((flags & (KeyValue.FLAG_CTRL | KeyValue.FLAG_ALT)) != 0)
@ -125,17 +124,10 @@ public class Keyboard2 extends InputMethodService
else else
getCurrentInputConnection().commitText(key.getSymbol(flags), 1); getCurrentInputConnection().commitText(key.getSymbol(flags), 1);
} }
else if (keyChar != KeyValue.CHAR_NONE) else
sendKeyChar(keyChar); sendKeyChar(keyChar);
} }
private ViewGroup getEmojiPane()
{
if (_emojiPane == null)
_emojiPane = (ViewGroup)getLayoutInflater().inflate(R.layout.emoji_pane, null);
return (_emojiPane);
}
// private void handleDelKey(int before, int after) // private void handleDelKey(int before, int after)
// { // {
// CharSequence selection = getCurrentInputConnection().getSelectedText(0); // CharSequence selection = getCurrentInputConnection().getSelectedText(0);