2019-09-27 17:05:34 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
2020-06-28 21:36:31 +00:00
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.Dialog;
|
|
|
|
import android.content.DialogInterface;
|
2019-09-27 17:05:34 +00:00
|
|
|
import android.graphics.Typeface;
|
|
|
|
import android.text.SpannableString;
|
|
|
|
import android.text.Spanned;
|
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;
|
2019-09-27 17:05:34 +00:00
|
|
|
import android.text.style.RelativeSizeSpan;
|
|
|
|
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-06-28 15:18:42 +00:00
|
|
|
import android.view.MenuItem;
|
|
|
|
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-06-28 21:36:31 +00:00
|
|
|
import com.flask.colorpicker.ColorPickerView;
|
|
|
|
import com.flask.colorpicker.builder.ColorPickerClickListener;
|
|
|
|
import com.flask.colorpicker.builder.ColorPickerDialogBuilder;
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
2019-09-27 17:05:34 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class StyleHelper {
|
2020-06-28 15:18:42 +00:00
|
|
|
static boolean apply(int action, 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;
|
|
|
|
final SpannableString t = ss;
|
2019-09-27 17:05:34 +00:00
|
|
|
|
2020-06-28 15:18:42 +00:00
|
|
|
PopupMenu popupMenu = new PopupMenu(anchor.getContext(), 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);
|
|
|
|
for (int i = 0; i < fontNames.length; i++)
|
2020-08-11 16:46:26 +00:00
|
|
|
popupMenu.getMenu().add(R.id.group_style_font, i, 4, fontNames[i]);
|
|
|
|
popupMenu.getMenu().add(R.id.group_style_font, fontNames.length, 4, 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-06-28 21:36:31 +00:00
|
|
|
switch (item.getGroupId()) {
|
2020-06-29 06:00:15 +00:00
|
|
|
case R.id.group_style_size:
|
2020-06-28 21:36:31 +00:00
|
|
|
return setSize(item);
|
2020-06-29 06:00:15 +00:00
|
|
|
case R.id.group_style_color:
|
2020-06-28 21:36:31 +00:00
|
|
|
return setColor(item);
|
2020-08-11 16:46:26 +00:00
|
|
|
case R.id.group_style_list:
|
|
|
|
return setList(item);
|
2020-06-29 06:00:15 +00:00
|
|
|
case R.id.group_style_font:
|
2020-06-28 21:36:31 +00:00
|
|
|
return setFont(item);
|
2020-06-29 06:00:15 +00:00
|
|
|
case R.id.group_style_clear:
|
2020-06-28 21:36:31 +00:00
|
|
|
return clear(item);
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
|
|
|
|
ColorPickerDialogBuilder builder = ColorPickerDialogBuilder
|
|
|
|
.with(etBody.getContext())
|
|
|
|
.setTitle(R.string.title_color)
|
|
|
|
.showColorEdit(true)
|
|
|
|
.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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Dialog dialog = builder.build();
|
|
|
|
|
|
|
|
try {
|
|
|
|
Field fColorEdit = builder.getClass().getDeclaredField("colorEdit");
|
|
|
|
fColorEdit.setAccessible(true);
|
|
|
|
EditText colorEdit = (EditText) fColorEdit.get(builder);
|
|
|
|
colorEdit.setTextColor(Helper.resolveColor(etBody.getContext(), android.R.attr.textColorPrimary));
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
2020-06-28 16:39:27 +00:00
|
|
|
}
|
2020-06-28 15:18:42 +00:00
|
|
|
|
2020-06-28 21:36:31 +00:00
|
|
|
dialog.show();
|
|
|
|
|
|
|
|
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-08-11 16:46:26 +00:00
|
|
|
private boolean setList(MenuItem item) {
|
|
|
|
BulletSpan[] spans = t.getSpans(s, e, BulletSpan.class);
|
|
|
|
for (BulletSpan span : spans)
|
|
|
|
t.removeSpan(span);
|
|
|
|
|
|
|
|
int i = s;
|
|
|
|
int j = s + 1;
|
|
|
|
while (j < e) {
|
|
|
|
if (t.charAt(j) == '\n') {
|
|
|
|
t.setSpan(new BulletSpan(), i, j + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_PARAGRAPH);
|
|
|
|
i = j + 1;
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
|
|
|
|
etBody.setText(t);
|
|
|
|
etBody.setSelection(s, e);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|