parent
570f79d91a
commit
4dce95142f
@ -1,39 +1,42 @@
|
||||
package org.thoughtcrime.securesms.components.emoji;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable.Callback;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.widget.AppCompatEditText;
|
||||
import android.text.InputFilter;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.components.emoji.EmojiProvider.EmojiDrawable;
|
||||
|
||||
|
||||
public class EmojiEditText extends AppCompatEditText {
|
||||
private final Callback callback = new PostInvalidateCallback(this);
|
||||
private static final String TAG = EmojiEditText.class.getSimpleName();
|
||||
|
||||
public EmojiEditText(Context context) {
|
||||
super(context);
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
public EmojiEditText(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
this(context, attrs, R.attr.editTextStyle);
|
||||
}
|
||||
|
||||
public EmojiEditText(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
}
|
||||
|
||||
@Override public void setText(CharSequence text, BufferType type) {
|
||||
super.setText(EmojiProvider.getInstance(getContext()).emojify(text, EmojiProvider.EMOJI_SMALL, callback),
|
||||
BufferType.SPANNABLE);
|
||||
setFilters(new InputFilter[]{ new EmojiFilter(this) });
|
||||
}
|
||||
|
||||
public void insertEmoji(String emoji) {
|
||||
final int start = getSelectionStart();
|
||||
final int end = getSelectionEnd();
|
||||
final CharSequence text = EmojiProvider.getInstance(getContext()).emojify(emoji,
|
||||
EmojiProvider.EMOJI_SMALL,
|
||||
callback);
|
||||
|
||||
getText().replace(Math.min(start, end), Math.max(start, end), text);
|
||||
getText().replace(Math.min(start, end), Math.max(start, end), emoji);
|
||||
setSelection(end + emoji.length());
|
||||
}
|
||||
|
||||
@Override public void invalidateDrawable(@NonNull Drawable drawable) {
|
||||
if (drawable instanceof EmojiDrawable) invalidate();
|
||||
else super.invalidateDrawable(drawable);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,32 @@
|
||||
package org.thoughtcrime.securesms.components.emoji;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.InputFilter;
|
||||
import android.text.Spannable;
|
||||
import android.text.Spanned;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
|
||||
/**
|
||||
* Created by kaonashi on 6/1/15.
|
||||
*/
|
||||
public class EmojiFilter implements InputFilter {
|
||||
private View view;
|
||||
|
||||
public EmojiFilter(TextView view) {
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
@Override public CharSequence filter(CharSequence source, int start, int end,
|
||||
Spanned dest, int dstart, int dend)
|
||||
{
|
||||
char[] v = new char[end - start];
|
||||
TextUtils.getChars(source, start, end, v, 0);
|
||||
Spannable emojified = EmojiProvider.getInstance(view.getContext()).emojify(new String(v), view);
|
||||
if (source instanceof Spanned) {
|
||||
TextUtils.copySpansFrom((Spanned) source, start, end, null, emojified, 0);
|
||||
}
|
||||
return emojified;
|
||||
}
|
||||
}
|
@ -1,29 +1,31 @@
|
||||
package org.thoughtcrime.securesms.components.emoji;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable.Callback;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.widget.AppCompatTextView;
|
||||
import android.text.InputFilter;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
import org.thoughtcrime.securesms.components.emoji.EmojiProvider.EmojiDrawable;
|
||||
|
||||
public class EmojiTextView extends AppCompatTextView {
|
||||
private final Callback callback = new PostInvalidateCallback(this);
|
||||
|
||||
public EmojiTextView(Context context) {
|
||||
super(context);
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
public EmojiTextView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
this(context, attrs, 0);
|
||||
}
|
||||
|
||||
public EmojiTextView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
setFilters(new InputFilter[]{ new EmojiFilter(this) });
|
||||
}
|
||||
|
||||
@Override public void setText(CharSequence text, BufferType type) {
|
||||
super.setText(EmojiProvider.getInstance(getContext()).emojify(text,
|
||||
EmojiProvider.EMOJI_SMALL,
|
||||
callback),
|
||||
BufferType.SPANNABLE);
|
||||
@Override public void invalidateDrawable(@NonNull Drawable drawable) {
|
||||
if (drawable instanceof EmojiDrawable) invalidate();
|
||||
else super.invalidateDrawable(drawable);
|
||||
}
|
||||
}
|
||||
|
@ -1,31 +0,0 @@
|
||||
package org.thoughtcrime.securesms.components.emoji;
|
||||
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.graphics.drawable.Drawable.Callback;
|
||||
import android.view.View;
|
||||
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
|
||||
public class PostInvalidateCallback implements Callback {
|
||||
private final View view;
|
||||
|
||||
public PostInvalidateCallback(View view) {
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
@Override public void invalidateDrawable(Drawable who) {
|
||||
Util.runOnMain(new Runnable() {
|
||||
@Override public void run() {
|
||||
view.invalidate();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override public void scheduleDrawable(Drawable who, Runnable what, long when) {
|
||||
|
||||
}
|
||||
|
||||
@Override public void unscheduleDrawable(Drawable who, Runnable what) {
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue