2015-05-06 13:53:55 -07:00
|
|
|
package org.thoughtcrime.securesms.components.emoji;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2015-05-26 15:10:45 -07:00
|
|
|
import android.graphics.drawable.Drawable.Callback;
|
2015-05-06 13:53:55 -07:00
|
|
|
import android.support.v7.widget.AppCompatEditText;
|
|
|
|
import android.util.AttributeSet;
|
2015-05-14 15:54:07 -07:00
|
|
|
|
2015-05-06 13:53:55 -07:00
|
|
|
|
|
|
|
public class EmojiEditText extends AppCompatEditText {
|
2015-05-26 15:10:45 -07:00
|
|
|
private final Callback callback = new PostInvalidateCallback(this);
|
|
|
|
|
2015-05-06 13:53:55 -07:00
|
|
|
public EmojiEditText(Context context) {
|
|
|
|
super(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
public EmojiEditText(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
}
|
|
|
|
|
2015-05-29 10:54:59 -07:00
|
|
|
public EmojiEditText(Context context, AttributeSet attrs, int defStyleAttr) {
|
2015-05-06 13:53:55 -07:00
|
|
|
super(context, attrs, defStyleAttr);
|
|
|
|
}
|
|
|
|
|
2015-05-14 21:08:37 -07:00
|
|
|
@Override public void setText(CharSequence text, BufferType type) {
|
2015-05-21 17:29:23 -07:00
|
|
|
super.setText(EmojiProvider.getInstance(getContext()).emojify(text, EmojiProvider.EMOJI_SMALL, callback),
|
2015-05-14 21:08:37 -07:00
|
|
|
BufferType.SPANNABLE);
|
2015-05-06 13:53:55 -07:00
|
|
|
}
|
|
|
|
|
2015-05-21 17:29:23 -07:00
|
|
|
public void insertEmoji(String emoji) {
|
2015-05-14 15:54:07 -07:00
|
|
|
final int start = getSelectionStart();
|
|
|
|
final int end = getSelectionEnd();
|
2015-05-21 17:29:23 -07:00
|
|
|
final CharSequence text = EmojiProvider.getInstance(getContext()).emojify(emoji,
|
2015-05-14 15:54:07 -07:00
|
|
|
EmojiProvider.EMOJI_SMALL,
|
2015-05-26 15:10:45 -07:00
|
|
|
callback);
|
2015-05-06 13:53:55 -07:00
|
|
|
|
2015-05-14 15:54:07 -07:00
|
|
|
getText().replace(Math.min(start, end), Math.max(start, end), text);
|
2015-05-21 17:29:23 -07:00
|
|
|
setSelection(end + emoji.length());
|
2015-05-06 13:53:55 -07:00
|
|
|
}
|
|
|
|
}
|