FairEmail/app/src/main/java/eu/faircode/email/StyleHelper.java

457 lines
20 KiB
Java
Raw Normal View History

2019-09-27 17:05:34 +00:00
package eu.faircode.email;
2020-06-28 21:36:31 +00:00
import android.app.Activity;
2020-08-12 08:25:08 +00:00
import android.content.Context;
2020-06-28 21:36:31 +00:00
import android.content.DialogInterface;
2020-08-14 18:15:52 +00:00
import android.content.SharedPreferences;
2019-09-27 17:05:34 +00:00
import android.graphics.Typeface;
2020-08-12 06:18:39 +00:00
import android.os.Build;
2020-10-03 09:37:43 +00:00
import android.text.Layout;
2019-09-27 17:05:34 +00:00
import android.text.SpannableString;
2020-08-13 07:16:53 +00:00
import android.text.SpannableStringBuilder;
2019-09-27 17:05:34 +00:00
import android.text.Spanned;
2020-10-03 09:37:43 +00:00
import android.text.TextUtils;
import android.text.style.AlignmentSpan;
2020-08-11 16:46:26 +00:00
import android.text.style.BulletSpan;
2019-09-27 17:05:34 +00:00
import android.text.style.ForegroundColorSpan;
2019-09-29 10:10:19 +00:00
import android.text.style.ImageSpan;
2020-10-01 08:22:20 +00:00
import android.text.style.QuoteSpan;
2019-09-27 17:05:34 +00:00
import android.text.style.RelativeSizeSpan;
2020-10-01 08:31:58 +00:00
import android.text.style.StrikethroughSpan;
2019-09-27 17:05:34 +00:00
import android.text.style.StyleSpan;
2020-06-28 16:39:27 +00:00
import android.text.style.TypefaceSpan;
2019-09-27 17:05:34 +00:00
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
2020-10-03 12:17:13 +00:00
import android.util.Pair;
2020-06-28 15:18:42 +00:00
import android.view.MenuItem;
2020-08-12 08:25:08 +00:00
import android.view.SubMenu;
2020-06-28 15:18:42 +00:00
import android.view.View;
2020-06-28 21:36:31 +00:00
import android.view.inputmethod.InputMethodManager;
2019-09-27 17:05:34 +00:00
import android.widget.EditText;
2020-06-28 15:18:42 +00:00
import androidx.appcompat.widget.PopupMenu;
2020-11-12 14:08:34 +00:00
import androidx.lifecycle.LifecycleOwner;
2020-08-14 18:15:52 +00:00
import androidx.preference.PreferenceManager;
2020-06-28 15:18:42 +00:00
2020-06-28 21:36:31 +00:00
import com.flask.colorpicker.ColorPickerView;
import com.flask.colorpicker.builder.ColorPickerClickListener;
import com.flask.colorpicker.builder.ColorPickerDialogBuilder;
2019-09-27 17:05:34 +00:00
import java.util.ArrayList;
import java.util.List;
2020-10-03 09:37:43 +00:00
import java.util.Locale;
2019-09-27 17:05:34 +00:00
public class StyleHelper {
2020-11-12 14:08:34 +00:00
static boolean apply(int action, LifecycleOwner owner, View anchor, EditText etBody, Object... args) {
2019-09-27 17:05:34 +00:00
Log.i("Style action=" + action);
try {
int start = etBody.getSelectionStart();
int end = etBody.getSelectionEnd();
if (start < 0)
start = 0;
if (end < 0)
end = 0;
if (start > end) {
int tmp = start;
start = end;
end = tmp;
}
SpannableString ss = new SpannableString(etBody.getText());
switch (action) {
case R.id.menu_bold:
case R.id.menu_italic: {
int style = (action == R.id.menu_bold ? Typeface.BOLD : Typeface.ITALIC);
boolean has = false;
for (StyleSpan span : ss.getSpans(start, end, StyleSpan.class))
if (span.getStyle() == style) {
has = true;
ss.removeSpan(span);
}
if (!has)
ss.setSpan(new StyleSpan(style), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
etBody.setText(ss);
etBody.setSelection(start, end);
return true;
}
case R.id.menu_underline: {
boolean has = false;
for (UnderlineSpan span : ss.getSpans(start, end, UnderlineSpan.class)) {
has = true;
ss.removeSpan(span);
}
if (!has)
ss.setSpan(new UnderlineSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
etBody.setText(ss);
etBody.setSelection(start, end);
return true;
}
2020-06-28 21:36:31 +00:00
case R.id.menu_style: {
2020-06-28 15:18:42 +00:00
final int s = start;
final int e = end;
2020-08-13 07:16:53 +00:00
final SpannableStringBuilder t = new SpannableStringBuilder(ss);
2019-09-27 17:05:34 +00:00
2020-11-12 14:08:34 +00:00
PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(anchor.getContext(), owner, anchor);
2020-06-29 06:00:15 +00:00
popupMenu.inflate(R.menu.popup_style);
2019-09-27 17:05:34 +00:00
2020-06-29 10:55:12 +00:00
String[] fontNames = anchor.getResources().getStringArray(R.array.fontNameNames);
2020-08-12 08:25:08 +00:00
SubMenu smenu = popupMenu.getMenu().findItem(R.id.menu_style_font).getSubMenu();
2020-06-29 10:55:12 +00:00
for (int i = 0; i < fontNames.length; i++)
2020-08-12 08:25:08 +00:00
smenu.add(R.id.group_style_font, i, 0, fontNames[i]);
smenu.add(R.id.group_style_font, fontNames.length, 0, R.string.title_style_font_default);
2020-06-29 10:55:12 +00:00
2020-06-28 15:18:42 +00:00
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
2020-08-12 20:17:54 +00:00
try {
switch (item.getGroupId()) {
case R.id.group_style_size:
return setSize(item);
case R.id.group_style_color:
return setColor(item);
2020-10-03 09:37:43 +00:00
case R.id.group_style_align:
return setAlignment(item);
2020-08-12 20:17:54 +00:00
case R.id.group_style_list:
return setList(item);
case R.id.group_style_font:
return setFont(item);
2020-10-01 08:22:20 +00:00
case R.id.group_style_blockquote:
return setBlockquote(item);
2020-10-01 08:31:58 +00:00
case R.id.group_style_strikethrough:
return setStrikethrough(item);
2020-08-12 20:17:54 +00:00
case R.id.group_style_clear:
return clear(item);
default:
return false;
}
} catch (Throwable ex) {
Log.e(ex);
return false;
2020-06-28 21:36:31 +00:00
}
}
2020-06-28 16:39:27 +00:00
2020-06-28 21:36:31 +00:00
private boolean setSize(MenuItem item) {
RelativeSizeSpan[] spans = t.getSpans(s, e, RelativeSizeSpan.class);
for (RelativeSizeSpan span : spans)
t.removeSpan(span);
Float size;
2020-06-29 06:00:15 +00:00
if (item.getItemId() == R.id.menu_style_size_small)
2020-06-28 21:36:31 +00:00
size = 0.8f;
2020-06-29 06:00:15 +00:00
else if (item.getItemId() == R.id.menu_style_size_large)
2020-06-28 21:36:31 +00:00
size = 1.25f;
else
size = null;
if (size != null)
t.setSpan(new RelativeSizeSpan(size), s, e, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
2020-06-28 16:39:27 +00:00
2020-06-28 21:36:31 +00:00
etBody.setText(t);
etBody.setSelection(s, e);
return true;
}
private boolean setColor(MenuItem item) {
InputMethodManager imm = (InputMethodManager) etBody.getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm != null)
imm.hideSoftInputFromWindow(etBody.getWindowToken(), 0);
2020-10-02 11:11:59 +00:00
Context context = etBody.getContext();
int editTextColor = Helper.resolveColor(context, android.R.attr.editTextColor);
2020-06-28 21:36:31 +00:00
ColorPickerDialogBuilder builder = ColorPickerDialogBuilder
2020-10-02 11:11:59 +00:00
.with(context)
2020-06-28 21:36:31 +00:00
.setTitle(R.string.title_color)
.showColorEdit(true)
2020-10-02 11:11:59 +00:00
.setColorEditTextColor(editTextColor)
2020-06-28 21:36:31 +00:00
.wheelType(ColorPickerView.WHEEL_TYPE.FLOWER)
.density(6)
.lightnessSliderOnly()
.setPositiveButton(android.R.string.ok, new ColorPickerClickListener() {
@Override
public void onClick(DialogInterface dialog, int selectedColor, Integer[] allColors) {
_setColor(selectedColor);
}
})
.setNegativeButton(R.string.title_reset, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
_setColor(null);
}
});
2020-10-02 11:11:59 +00:00
builder.build().show();
2020-06-28 21:36:31 +00:00
return true;
2020-06-28 15:18:42 +00:00
}
2020-06-28 21:36:31 +00:00
private void _setColor(Integer color) {
for (ForegroundColorSpan span : t.getSpans(s, e, ForegroundColorSpan.class))
t.removeSpan(span);
2019-09-27 17:05:34 +00:00
2020-06-28 21:36:31 +00:00
if (color != null)
t.setSpan(new ForegroundColorSpan(color), s, e, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
2019-09-27 17:05:34 +00:00
2020-06-28 21:36:31 +00:00
etBody.setText(t);
etBody.setSelection(s, e);
}
2019-09-27 17:05:34 +00:00
2020-10-03 09:37:43 +00:00
private boolean setAlignment(MenuItem item) {
2020-10-03 12:17:13 +00:00
Pair<Integer, Integer> paragraph = ensureParagraph(t, s, e);
int start = paragraph.first;
int end = paragraph.second;
AlignmentSpan[] spans = t.getSpans(start, end, AlignmentSpan.class);
2020-10-03 09:37:43 +00:00
for (AlignmentSpan span : spans)
t.removeSpan(span);
Layout.Alignment alignment = null;
boolean ltr = (TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()) == View.LAYOUT_DIRECTION_LTR);
switch (item.getItemId()) {
case R.id.menu_style_align_start:
alignment = (ltr ? Layout.Alignment.ALIGN_NORMAL : Layout.Alignment.ALIGN_OPPOSITE);
break;
case R.id.menu_style_align_center:
alignment = Layout.Alignment.ALIGN_CENTER;
break;
case R.id.menu_style_align_end:
alignment = (ltr ? Layout.Alignment.ALIGN_OPPOSITE : Layout.Alignment.ALIGN_NORMAL);
break;
}
if (alignment != null)
2020-10-03 12:26:03 +00:00
t.setSpan(new AlignmentSpan.Standard(alignment),
start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_PARAGRAPH);
2020-10-03 09:37:43 +00:00
etBody.setText(t);
2020-10-03 12:17:13 +00:00
etBody.setSelection(start, end);
2020-10-03 09:37:43 +00:00
return true;
}
2020-08-11 16:46:26 +00:00
private boolean setList(MenuItem item) {
2020-08-12 08:25:08 +00:00
Context context = etBody.getContext();
2020-08-14 18:15:52 +00:00
2020-08-12 08:25:08 +00:00
int colorAccent = Helper.resolveColor(context, R.attr.colorAccent);
int dp3 = Helper.dp2pixels(context, 3);
int dp6 = Helper.dp2pixels(context, 6);
2020-08-14 18:15:52 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int message_zoom = prefs.getInt("message_zoom", 100);
float textSize = Helper.getTextSize(context, 0) * message_zoom / 100f;
2020-08-12 08:25:08 +00:00
2020-10-03 12:17:13 +00:00
Pair<Integer, Integer> paragraph = ensureParagraph(t, s, e);
int start = paragraph.first;
int end = paragraph.second;
2020-08-14 18:32:05 +00:00
2020-08-14 06:46:44 +00:00
// Remove existing bullets
BulletSpan[] spans = t.getSpans(start, end, BulletSpan.class);
for (BulletSpan span : spans)
t.removeSpan(span);
2020-08-12 06:18:39 +00:00
2020-08-13 07:16:53 +00:00
int i = start;
int j = start + 1;
2020-08-12 08:25:08 +00:00
int index = 1;
2020-08-12 08:35:01 +00:00
while (j < end) {
2020-08-11 20:12:00 +00:00
if (i > 0 && t.charAt(i - 1) == '\n' && t.charAt(j) == '\n') {
2020-08-14 18:32:05 +00:00
Log.i("Insert " + i + "..." + (j + 1) + " size=" + end);
2020-08-12 08:25:08 +00:00
if (item.getItemId() == R.id.menu_style_list_bullets)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P)
t.setSpan(new BulletSpan(dp6, colorAccent), i, j + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_PARAGRAPH);
else
t.setSpan(new BulletSpan(dp6, colorAccent, dp3), i, j + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_PARAGRAPH);
2020-08-12 06:18:39 +00:00
else
2020-08-12 08:25:08 +00:00
t.setSpan(new NumberSpan(dp6, colorAccent, textSize, index++), i, j + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_PARAGRAPH);
2020-08-12 06:18:39 +00:00
2020-08-11 16:46:26 +00:00
i = j + 1;
}
j++;
}
etBody.setText(t);
2020-08-13 07:16:53 +00:00
etBody.setSelection(start, end);
2020-08-11 16:46:26 +00:00
return true;
}
2020-06-28 21:36:31 +00:00
private boolean setFont(MenuItem item) {
TypefaceSpan[] spans = t.getSpans(s, e, TypefaceSpan.class);
for (TypefaceSpan span : spans)
t.removeSpan(span);
2020-06-29 10:55:12 +00:00
int id = item.getItemId();
String[] names = anchor.getResources().getStringArray(R.array.fontNameValues);
String face = (id < names.length ? names[id] : null);
2019-09-27 17:05:34 +00:00
2020-06-28 21:36:31 +00:00
if (face != null)
t.setSpan(new TypefaceSpan(face), s, e, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
etBody.setText(t);
etBody.setSelection(s, e);
return true;
}
2020-10-01 08:22:20 +00:00
private boolean setBlockquote(MenuItem item) {
Context context = etBody.getContext();
int colorPrimary = Helper.resolveColor(context, R.attr.colorPrimary);
int dp3 = Helper.dp2pixels(context, 3);
int dp6 = Helper.dp2pixels(context, 6);
QuoteSpan[] spans = t.getSpans(s, e, QuoteSpan.class);
for (QuoteSpan span : spans)
t.removeSpan(span);
QuoteSpan q;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P)
q = new QuoteSpan(colorPrimary);
else
q = new QuoteSpan(colorPrimary, dp3, dp6);
t.setSpan(q, s, e, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
etBody.setText(t);
etBody.setSelection(s, e);
return true;
}
2020-10-01 08:31:58 +00:00
private boolean setStrikethrough(MenuItem item) {
boolean has = false;
for (StrikethroughSpan span : t.getSpans(s, e, StrikethroughSpan.class)) {
has = true;
t.removeSpan(span);
}
if (!has)
t.setSpan(new StrikethroughSpan(), s, e, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
etBody.setText(t);
etBody.setSelection(s, e);
return true;
}
2020-06-28 21:36:31 +00:00
private boolean clear(MenuItem item) {
for (Object span : t.getSpans(s, e, Object.class))
if (!(span instanceof ImageSpan))
t.removeSpan(span);
etBody.setText(t);
etBody.setSelection(s, e);
return true;
}
2020-10-03 12:17:13 +00:00
private Pair<Integer, Integer> ensureParagraph(SpannableStringBuilder t, int s, int e) {
int start = s;
int end = e;
// Expand selection at start
while (start > 0 && t.charAt(start - 1) != '\n')
start--;
// Expand selection at end
while (end > 0 && end < t.length() && t.charAt(end - 1) != '\n')
end++;
// Nothing to do
if (start == end)
return null;
// Create paragraph at start
if (start == 0 && t.charAt(start) != '\n') {
t.insert(0, "\n");
start++;
end++;
}
// Create paragraph at end
if (end == t.length() && t.charAt(end - 1) != '\n') {
t.append("\n");
end++;
}
if (end == t.length())
t.append("\n"); // workaround Android bug
return new Pair(start, end);
}
2020-06-28 21:36:31 +00:00
});
popupMenu.show();
2019-09-27 17:05:34 +00:00
return true;
}
case R.id.menu_link: {
2019-12-29 12:13:52 +00:00
String url = (String) args[0];
2019-09-27 17:05:34 +00:00
List<Object> spans = new ArrayList<>();
for (Object span : ss.getSpans(start, end, Object.class)) {
if (!(span instanceof URLSpan))
spans.add(span);
ss.removeSpan(span);
}
2020-03-26 19:52:03 +00:00
if (url != null) {
if (start == end) {
etBody.getText().insert(start, url);
end += url.length();
ss = new SpannableString(etBody.getText());
}
2019-12-29 12:13:52 +00:00
2020-03-26 19:52:03 +00:00
ss.setSpan(new URLSpan(url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
2019-09-27 17:05:34 +00:00
2020-03-26 19:52:03 +00:00
// Restore other spans
2019-09-27 17:05:34 +00:00
for (Object span : spans)
ss.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
etBody.setText(ss);
etBody.setSelection(end, end);
return true;
}
2019-09-29 10:10:19 +00:00
case R.id.menu_clear: {
boolean selected = (start != end);
if (start == end) {
start = 0;
end = etBody.length();
}
for (Object span : ss.getSpans(start, end, Object.class))
if (!(span instanceof ImageSpan))
ss.removeSpan(span);
etBody.setText(ss);
if (selected)
etBody.setSelection(start, end);
return true;
}
2019-09-27 17:05:34 +00:00
default:
return false;
}
} catch (Throwable ex) {
Log.e(ex);
return false;
}
}
}