summaryrefslogtreecommitdiffstats
path: root/app/src/main/java/fr/gouv/etalab/mastodon/helper/CustomTextView.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/fr/gouv/etalab/mastodon/helper/CustomTextView.java')
-rw-r--r--app/src/main/java/fr/gouv/etalab/mastodon/helper/CustomTextView.java92
1 files changed, 69 insertions, 23 deletions
diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/helper/CustomTextView.java b/app/src/main/java/fr/gouv/etalab/mastodon/helper/CustomTextView.java
index e3891a364..2def0f090 100644
--- a/app/src/main/java/fr/gouv/etalab/mastodon/helper/CustomTextView.java
+++ b/app/src/main/java/fr/gouv/etalab/mastodon/helper/CustomTextView.java
@@ -14,11 +14,20 @@ package fr.gouv.etalab.mastodon.helper;
*
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
* see <http://www.gnu.org/licenses>. */
+import android.annotation.SuppressLint;
import android.content.Context;
-import android.text.Selection;
-import android.text.Spannable;
+import android.content.SharedPreferences;
+import android.content.res.TypedArray;
+import android.graphics.Paint;
+import android.support.annotation.DimenRes;
+import android.support.annotation.Px;
+import android.support.v7.widget.AppCompatTextView;
+import android.text.SpannableStringBuilder;
import android.util.AttributeSet;
-import android.view.MotionEvent;
+
+import com.vanniktech.emoji.EmojiManager;
+
+import fr.gouv.etalab.mastodon.R;
/**
@@ -26,36 +35,73 @@ import android.view.MotionEvent;
* Allows to fix crashes with selection see: https://stackoverflow.com/a/36740247
*/
-public class CustomTextView extends android.support.v7.widget.AppCompatTextView {
-
+public class CustomTextView extends AppCompatTextView {
+ private float emojiSize;
+ private boolean emoji;
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
+ final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
+ emoji = sharedpreferences.getBoolean(Helper.SET_DISPLAY_EMOJI, true);
+
+ final Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
+ final float defaultEmojiSize = fontMetrics.descent - fontMetrics.ascent;
+
+ if (attrs == null) {
+ emojiSize = defaultEmojiSize;
+ } else {
+ @SuppressLint("CustomViewStyleable") final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.EmojiTextView);
+
+ try {
+ emojiSize = a.getDimension(R.styleable.EmojiTextView_emojiSize, defaultEmojiSize);
+ } finally {
+ a.recycle();
+ }
+ }
+ setText(getText());
+ }
+
+ @Override
+ public void setText(final CharSequence rawText, final BufferType type) {
+ if( emoji) {
+ final CharSequence text = rawText == null ? "" : rawText;
+ final SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text);
+ final Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
+ final float defaultEmojiSize = fontMetrics.descent - fontMetrics.ascent;
+ EmojiManager.getInstance().replaceWithImages(getContext(), spannableStringBuilder, emojiSize, defaultEmojiSize);
+ super.setText(spannableStringBuilder, type);
+ }else {
+ super.setText(rawText, type);
+ }
+
}
- public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
+ /** sets the emoji size in pixels and automatically invalidates the text and renders it with the new size */
+ public final void setEmojiSize(@Px final int pixels) {
+ setEmojiSize(pixels, true);
}
- //TODO: sounds no longer needed, commented but might be removed in a next release
- /*@Override
- public boolean dispatchTouchEvent(final MotionEvent event) {
- // FIXME simple workaround to https://code.google.com/p/android/issues/detail?id=191430
- int startSelection = getSelectionStart();
- int endSelection = getSelectionEnd();
- if (startSelection < 0 || endSelection < 0){
- Selection.setSelection((Spannable) getText(), getText().length());
- } else if (startSelection != endSelection) {
- if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
- final CharSequence text = getText();
- setText(null);
- setText(text);
- }
+ /** sets the emoji size in pixels and automatically invalidates the text and renders it with the new size when {@code shouldInvalidate} is true */
+ public final void setEmojiSize(@Px final int pixels, final boolean shouldInvalidate) {
+ emojiSize = pixels;
+
+ if (shouldInvalidate) {
+ setText(getText());
}
- return super.dispatchTouchEvent(event);
- }*/
+ }
+
+ /** sets the emoji size in pixels with the provided resource and automatically invalidates the text and renders it with the new size */
+ public final void setEmojiSizeRes(@DimenRes final int res) {
+ setEmojiSizeRes(res, true);
+ }
+
+ /** sets the emoji size in pixels with the provided resource and invalidates the text and renders it with the new size when {@code shouldInvalidate} is true */
+ public final void setEmojiSizeRes(@DimenRes final int res, final boolean shouldInvalidate) {
+ setEmojiSize(getResources().getDimensionPixelSize(res), shouldInvalidate);
+ }
+
}