fedilab-Android-App/app/src/playstore/java/app/fedilab/android/helper/CustomTextView.java

121 lines
4.5 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.helper;
/* Copyright 2018 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
2019-09-06 17:55:14 +02:00
import android.annotation.SuppressLint;
import android.content.Context;
2018-10-28 11:32:23 +01:00
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.graphics.Paint;
2019-11-15 16:32:25 +01:00
import android.text.SpannableStringBuilder;
import android.util.AttributeSet;
2019-09-06 17:55:14 +02:00
2019-06-11 19:38:26 +02:00
import androidx.annotation.DimenRes;
import androidx.annotation.Px;
import androidx.appcompat.widget.AppCompatTextView;
2019-09-06 17:55:14 +02:00
2018-10-28 11:32:23 +01:00
import com.vanniktech.emoji.EmojiManager;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.R;
/**
* Created by Thomas on 12/05/2018.
* Allows to fix crashes with selection see: https://stackoverflow.com/a/36740247
*/
2018-10-28 11:32:23 +01:00
public class CustomTextView extends AppCompatTextView {
private float emojiSize;
2018-10-28 11:32:23 +01:00
private boolean emoji;
2019-09-06 17:55:14 +02:00
public CustomTextView(Context context) {
super(context);
2020-03-15 17:12:53 +01:00
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
emoji = sharedpreferences.getBoolean(Helper.SET_DISPLAY_EMOJI, false);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
2018-10-28 11:32:23 +01:00
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
2020-03-15 17:12:53 +01:00
emoji = sharedpreferences.getBoolean(Helper.SET_DISPLAY_EMOJI, false);
2018-10-28 11:32:23 +01:00
final Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
final float defaultEmojiSize = fontMetrics.descent - fontMetrics.ascent;
if (attrs == null) {
emojiSize = defaultEmojiSize;
} else {
2018-10-28 11:32:23 +01:00
@SuppressLint("CustomViewStyleable") final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.EmojiTextView);
try {
2018-10-28 11:32:23 +01:00
emojiSize = a.getDimension(R.styleable.EmojiTextView_emojiSize, defaultEmojiSize);
} finally {
a.recycle();
}
}
setText(getText());
}
@Override
2018-10-28 11:32:23 +01:00
public void setText(final CharSequence rawText, final BufferType type) {
2019-09-06 17:55:14 +02:00
if (emoji) {
2018-10-28 11:32:23 +01:00
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);
2019-09-06 17:55:14 +02:00
} else {
2018-10-28 11:32:23 +01:00
super.setText(rawText, type);
}
2018-10-28 11:32:23 +01:00
}
2019-09-06 17:55:14 +02:00
/**
* 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);
}
2019-09-06 17:55:14 +02:00
/**
* 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());
}
}
2019-09-06 17:55:14 +02:00
/**
* 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);
}
2019-09-06 17:55:14 +02:00
/**
* 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);
}
}