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

296 lines
11 KiB
Java
Raw Normal View History

2021-01-17 13:47:24 +00:00
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail 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.
FairEmail 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.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2022-01-01 08:46:36 +00:00
Copyright 2018-2022 by Marcel Bokhorst (M66B)
2021-01-17 13:47:24 +00:00
*/
import android.content.Context;
2022-02-21 12:14:51 +00:00
import android.content.SharedPreferences;
2022-02-22 11:36:30 +00:00
import android.content.res.ColorStateList;
2021-12-15 16:51:06 +00:00
import android.graphics.Canvas;
2022-02-23 08:48:26 +00:00
import android.graphics.Rect;
2022-02-20 21:07:10 +00:00
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.ContactsContract;
import android.text.Editable;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ImageSpan;
2021-01-17 13:47:24 +00:00
import android.util.AttributeSet;
2022-02-20 21:07:10 +00:00
import android.view.ContextThemeWrapper;
2021-12-15 16:51:06 +00:00
import android.view.MotionEvent;
2021-01-17 13:47:24 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatMultiAutoCompleteTextView;
2022-02-22 11:36:30 +00:00
import androidx.core.graphics.ColorUtils;
2022-02-21 12:14:51 +00:00
import androidx.preference.PreferenceManager;
2021-01-17 13:47:24 +00:00
2022-02-20 21:07:10 +00:00
import com.google.android.material.chip.ChipDrawable;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
2021-01-17 13:47:24 +00:00
public class EditTextMultiAutoComplete extends AppCompatMultiAutoCompleteTextView {
2022-02-22 22:44:52 +00:00
private SharedPreferences prefs;
private boolean dark;
private int colorAccent;
private ContextThemeWrapper ctx;
2021-01-17 13:47:24 +00:00
public EditTextMultiAutoComplete(@NonNull Context context) {
super(context);
2022-02-20 21:07:10 +00:00
init(context);
2021-01-17 13:47:24 +00:00
}
public EditTextMultiAutoComplete(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
2022-02-20 21:07:10 +00:00
init(context);
2021-01-17 13:47:24 +00:00
}
public EditTextMultiAutoComplete(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
2022-02-20 21:07:10 +00:00
init(context);
}
private void init(Context context) {
2021-01-17 13:47:24 +00:00
Helper.setKeyboardIncognitoMode(this, context);
2022-02-20 21:07:10 +00:00
2022-02-22 22:44:52 +00:00
prefs = PreferenceManager.getDefaultSharedPreferences(context);
dark = Helper.isDarkTheme(context);
colorAccent = Helper.resolveColor(context, R.attr.colorAccent);
colorAccent = ColorUtils.setAlphaComponent(colorAccent, 5 * 255 / 100);
ctx = new ContextThemeWrapper(context, dark ? R.style.ChipDark : R.style.ChipLight);
2022-02-22 07:29:06 +00:00
addTextChangedListener(new TextWatcher() {
2022-02-22 22:44:52 +00:00
private Integer backspace = null;
2022-02-22 18:50:11 +00:00
2022-02-22 07:29:06 +00:00
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
2022-02-22 18:50:11 +00:00
backspace = (count - after == 1 ? start : null);
2022-02-22 07:29:06 +00:00
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Do nothing
}
@Override
public void afterTextChanged(Editable edit) {
2022-02-22 18:50:11 +00:00
if (backspace != null) {
ClipImageSpan[] spans = edit.getSpans(backspace, backspace, ClipImageSpan.class);
if (spans.length == 1) {
int start = edit.getSpanStart(spans[0]);
int end = edit.getSpanEnd(spans[0]);
edit.delete(start, end);
}
}
2022-02-22 07:29:06 +00:00
if (getWidth() == 0)
post(update);
else
update.run();
}
});
2021-01-17 13:47:24 +00:00
}
2021-12-15 16:51:06 +00:00
2022-02-22 22:44:52 +00:00
@Override
2022-02-23 08:48:26 +00:00
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
post(update);
}
2022-02-22 22:44:52 +00:00
2022-02-23 08:48:26 +00:00
@Override
protected void onSelectionChanged(int selStart, int selEnd) {
super.onSelectionChanged(selStart, selEnd);
2022-02-22 22:44:52 +00:00
post(update);
2022-02-21 11:07:13 +00:00
}
2021-12-15 16:51:06 +00:00
@Override
public boolean onPreDraw() {
try {
return super.onPreDraw();
} catch (Throwable ex) {
Log.w(ex);
return true;
}
}
@Override
protected void onDraw(Canvas canvas) {
try {
super.onDraw(canvas);
} catch (Throwable ex) {
Log.w(ex);
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
try {
return super.dispatchTouchEvent(event);
} catch (Throwable ex) {
Log.w(ex);
return false;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
try {
2022-02-22 14:14:40 +00:00
if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
2022-02-22 13:14:12 +00:00
Editable edit = getText();
int off = Helper.getOffset(this, edit, event);
ClipImageSpan[] spans = edit.getSpans(off, off, ClipImageSpan.class);
2022-02-23 13:30:41 +00:00
if (spans.length == 1)
edit.removeSpan(spans[0]);
2022-02-22 13:14:12 +00:00
}
2021-12-15 16:51:06 +00:00
} catch (Throwable ex) {
Log.w(ex);
}
2022-02-22 14:14:40 +00:00
return super.onTouchEvent(event);
2021-12-15 16:51:06 +00:00
}
2022-02-22 22:44:52 +00:00
2022-02-23 12:26:32 +00:00
private final Runnable update = new Runnable() {
2022-02-22 22:44:52 +00:00
@Override
public void run() {
try {
final Context context = getContext();
final Editable edit = getText();
final boolean send_chips = prefs.getBoolean("send_chips", !BuildConfig.PLAY_STORE_RELEASE);
2022-02-22 22:44:52 +00:00
2022-02-23 08:48:26 +00:00
final boolean focus = hasFocus();
final int selStart = getSelectionStart();
final int selEnd = getSelectionEnd();
2022-02-22 22:44:52 +00:00
boolean added = false;
2022-02-23 08:48:26 +00:00
List<ClipImageSpan> tbd = new ArrayList<>();
2022-02-23 14:03:24 +00:00
tbd.addAll(Arrays.asList(edit.getSpans(0, edit.length(), ClipImageSpan.class)));
2022-02-22 22:44:52 +00:00
if (send_chips) {
int start = 0;
2022-02-23 08:48:26 +00:00
boolean space = true;
boolean quote = false;
2022-02-23 14:03:24 +00:00
for (int i = 0; i < edit.length(); i++) {
2022-02-22 22:44:52 +00:00
char kar = edit.charAt(i);
2022-02-23 08:48:26 +00:00
if (space && kar == ' ') {
start++;
continue;
}
space = false;
2022-02-22 22:44:52 +00:00
if (kar == '"')
quote = !quote;
2022-02-23 14:03:24 +00:00
else if (!quote && (kar == ',' || (!focus && i + 1 == edit.length()))) {
2022-02-22 22:44:52 +00:00
boolean found = false;
2022-02-23 08:48:26 +00:00
for (ClipImageSpan span : new ArrayList<>(tbd)) {
2022-02-22 22:44:52 +00:00
int s = edit.getSpanStart(span);
int e = edit.getSpanEnd(span);
if (s == start && e == i + 1) {
found = true;
2022-02-23 08:48:26 +00:00
if (!(focus && overlap(start, i, selStart, selEnd)))
tbd.remove(span);
2022-02-22 22:44:52 +00:00
break;
}
}
if (!found && start < i + 1 &&
2022-02-23 08:48:26 +00:00
!(focus && overlap(start, i, selStart, selEnd))) {
2022-02-22 22:44:52 +00:00
String email = edit.subSequence(start, i + 1).toString();
InternetAddress[] parsed;
try {
parsed = MessageHelper.parseAddresses(context, email);
if (parsed != null)
for (InternetAddress a : parsed)
a.validate();
} catch (AddressException ex) {
parsed = null;
}
if (parsed != null && parsed.length == 1) {
2022-02-23 14:03:24 +00:00
if (kar == ' ')
edit.insert(i++, ",");
else if (kar != ',')
2022-02-23 12:26:32 +00:00
edit.insert(++i, ",");
2022-02-22 22:44:52 +00:00
Drawable avatar = null;
Uri lookupUri = ContactInfo.getLookupUri(parsed);
if (lookupUri != null) {
InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(
context.getContentResolver(), lookupUri, false);
avatar = Drawable.createFromStream(is, email);
}
String e = parsed[0].getAddress();
String p = parsed[0].getPersonal();
String text = (TextUtils.isEmpty(p) ? e : p);
// https://github.com/material-components/material-components-android/blob/master/docs/components/Chip.md
ChipDrawable cd = ChipDrawable.createFromResource(ctx, R.xml.chip);
cd.setChipIcon(avatar);
// cd.setLayoutDirection(View.LAYOUT_DIRECTION_LOCALE);
cd.setText(text);
cd.setChipBackgroundColor(ColorStateList.valueOf(colorAccent));
cd.setMaxWidth(getWidth());
cd.setBounds(0, 0, cd.getIntrinsicWidth(), cd.getIntrinsicHeight());
ClipImageSpan is = new ClipImageSpan(cd);
edit.setSpan(is, start, i + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
2022-02-23 12:26:32 +00:00
2022-02-23 14:03:24 +00:00
if (kar == ',' &&
(i + 1 == edit.length() || edit.charAt(i + 1) != ' '))
2022-02-23 12:26:32 +00:00
edit.insert(++i, " ");
2022-02-22 22:44:52 +00:00
added = true;
}
}
2022-02-23 08:48:26 +00:00
start = i + 1;
space = true;
2022-02-22 22:44:52 +00:00
}
}
}
2022-02-23 08:48:26 +00:00
for (ClipImageSpan span : tbd)
2022-02-22 22:44:52 +00:00
edit.removeSpan(span);
2022-02-23 08:48:26 +00:00
if (tbd.size() > 0 || added)
2022-02-22 22:44:52 +00:00
invalidate();
} catch (Throwable ex) {
Log.e(ex);
}
}
};
2022-02-23 08:48:26 +00:00
private static boolean overlap(int start, int end, int selStart, int selEnd) {
return Math.max(start, selStart) <= Math.min(end, selEnd);
}
2022-02-22 22:44:52 +00:00
private static class ClipImageSpan extends ImageSpan {
public ClipImageSpan(@NonNull Drawable drawable) {
super(drawable, DynamicDrawableSpan.ALIGN_BOTTOM);
}
}
2021-12-15 16:51:06 +00:00
}