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
|
|
|
*/
|
|
|
|
|
2022-02-22 14:14:40 +00:00
|
|
|
import android.content.ClipData;
|
|
|
|
import android.content.ClipboardManager;
|
2022-02-20 21:07:10 +00:00
|
|
|
import android.content.ContentResolver;
|
2021-01-17 13:47:24 +00:00
|
|
|
import android.content.Context;
|
2022-02-22 13:14:12 +00:00
|
|
|
import android.content.DialogInterface;
|
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-22 13:14:12 +00:00
|
|
|
import android.graphics.Typeface;
|
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;
|
2022-02-22 13:14:12 +00:00
|
|
|
import android.text.SpannableString;
|
2022-02-20 21:07:10 +00:00
|
|
|
import android.text.Spanned;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.text.TextWatcher;
|
|
|
|
import android.text.style.DynamicDrawableSpan;
|
|
|
|
import android.text.style.ImageSpan;
|
2022-02-22 13:14:12 +00:00
|
|
|
import android.text.style.RelativeSizeSpan;
|
|
|
|
import android.text.style.StyleSpan;
|
2021-01-17 13:47:24 +00:00
|
|
|
import android.util.AttributeSet;
|
2022-02-20 21:07:10 +00:00
|
|
|
import android.view.ContextThemeWrapper;
|
2022-02-22 13:14:12 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2021-12-15 16:51:06 +00:00
|
|
|
import android.view.MotionEvent;
|
2022-02-22 13:14:12 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.EditText;
|
2022-02-22 14:14:40 +00:00
|
|
|
import android.widget.Toast;
|
2021-01-17 13:47:24 +00:00
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2022-02-22 13:14:12 +00:00
|
|
|
import androidx.appcompat.app.AlertDialog;
|
2021-01-17 13:47:24 +00:00
|
|
|
import androidx.appcompat.widget.AppCompatMultiAutoCompleteTextView;
|
2022-02-22 13:14:12 +00:00
|
|
|
import androidx.appcompat.widget.PopupMenu;
|
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;
|
2022-02-22 13:14:12 +00:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2022-02-20 21:07:10 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
|
2022-02-22 13:14:12 +00:00
|
|
|
import javax.mail.Address;
|
2022-02-20 21:07:10 +00:00
|
|
|
import javax.mail.internet.AddressException;
|
|
|
|
import javax.mail.internet.InternetAddress;
|
|
|
|
|
2021-01-17 13:47:24 +00:00
|
|
|
public class EditTextMultiAutoComplete extends AppCompatMultiAutoCompleteTextView {
|
|
|
|
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-21 12:14:51 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2022-02-22 07:29:06 +00:00
|
|
|
boolean dark = Helper.isDarkTheme(context);
|
2022-02-22 11:36:30 +00:00
|
|
|
int colorAccent = ColorUtils.setAlphaComponent(
|
|
|
|
Helper.resolveColor(context, R.attr.colorAccent), 5 * 255 / 100);
|
2022-02-22 07:29:06 +00:00
|
|
|
ContextThemeWrapper ctx = new ContextThemeWrapper(context,
|
|
|
|
dark ? R.style.ChipDark : R.style.ChipLight);
|
|
|
|
ContentResolver resolver = context.getContentResolver();
|
|
|
|
|
|
|
|
Runnable update = new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
Editable edit = getText();
|
|
|
|
boolean send_chips = prefs.getBoolean("send_chips", false);
|
|
|
|
|
|
|
|
boolean added = false;
|
|
|
|
List<ClipImageSpan> spans = new ArrayList<>();
|
|
|
|
spans.addAll(Arrays.asList(edit.getSpans(0, edit.length(), ClipImageSpan.class)));
|
|
|
|
|
|
|
|
if (send_chips) {
|
2022-02-21 19:16:43 +00:00
|
|
|
boolean quote = false;
|
|
|
|
int start = 0;
|
|
|
|
for (int i = 0; i < edit.length(); i++) {
|
|
|
|
char kar = edit.charAt(i);
|
|
|
|
if (kar == '"')
|
|
|
|
quote = !quote;
|
|
|
|
else if (kar == ',' && !quote) {
|
|
|
|
boolean found = false;
|
|
|
|
for (ClipImageSpan span : new ArrayList<>(spans)) {
|
|
|
|
int s = edit.getSpanStart(span);
|
|
|
|
int e = edit.getSpanEnd(span);
|
|
|
|
if (s == start && e == i + 1) {
|
|
|
|
found = true;
|
|
|
|
spans.remove(span);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found && start < i + 1) {
|
|
|
|
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) {
|
|
|
|
Drawable avatar = null;
|
|
|
|
Uri lookupUri = ContactInfo.getLookupUri(parsed);
|
|
|
|
if (lookupUri != null) {
|
|
|
|
InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(
|
|
|
|
resolver, 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);
|
2022-02-22 11:36:30 +00:00
|
|
|
cd.setChipBackgroundColor(ColorStateList.valueOf(colorAccent));
|
2022-02-21 19:16:43 +00:00
|
|
|
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);
|
|
|
|
added = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i + 1 < edit.length() && edit.charAt(i + 1) == ' ')
|
|
|
|
start = i + 2;
|
|
|
|
else
|
|
|
|
start = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-20 21:07:10 +00:00
|
|
|
|
2022-02-22 07:29:06 +00:00
|
|
|
for (ClipImageSpan span : spans)
|
|
|
|
edit.removeSpan(span);
|
2022-02-20 21:07:10 +00:00
|
|
|
|
2022-02-22 07:29:06 +00:00
|
|
|
if (spans.size() > 0 || added)
|
|
|
|
invalidate();
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
2022-02-20 21:07:10 +00:00
|
|
|
}
|
2022-02-22 07:29:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
addTextChangedListener(new TextWatcher() {
|
|
|
|
@Override
|
|
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void afterTextChanged(Editable edit) {
|
|
|
|
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-21 11:07:13 +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
|
|
|
@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);
|
|
|
|
if (spans.length != 1)
|
2022-02-22 14:14:40 +00:00
|
|
|
return super.onTouchEvent(event);
|
2022-02-22 13:14:12 +00:00
|
|
|
|
|
|
|
final Context context = getContext();
|
|
|
|
|
|
|
|
int start = edit.getSpanStart(spans[0]);
|
|
|
|
int end = edit.getSpanEnd(spans[0]);
|
|
|
|
if (start >= end)
|
2022-02-22 14:14:40 +00:00
|
|
|
return super.onTouchEvent(event);
|
2022-02-22 13:14:12 +00:00
|
|
|
|
|
|
|
String email = edit.subSequence(start, end).toString().trim();
|
|
|
|
if (email.endsWith(","))
|
|
|
|
email = email.substring(0, email.length() - 1);
|
|
|
|
|
|
|
|
Address[] parsed = MessageHelper.parseAddresses(context, email);
|
|
|
|
if (parsed == null && parsed.length != 1)
|
2022-02-22 14:14:40 +00:00
|
|
|
return super.onTouchEvent(event);
|
2022-02-22 13:14:12 +00:00
|
|
|
|
|
|
|
String e = ((InternetAddress) parsed[0]).getAddress();
|
|
|
|
String p = ((InternetAddress) parsed[0]).getPersonal();
|
|
|
|
|
|
|
|
SpannableString ss = new SpannableString(TextUtils.isEmpty(e) ? p : e);
|
|
|
|
ss.setSpan(new StyleSpan(Typeface.ITALIC), 0, ss.length(), 0);
|
|
|
|
ss.setSpan(new RelativeSizeSpan(0.9f), 0, ss.length(), 0);
|
|
|
|
|
2022-02-22 14:29:15 +00:00
|
|
|
TwoStateOwner owner = new TwoStateOwner("Chip");
|
|
|
|
PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(context, owner, this);
|
|
|
|
popupMenu.getMenu().add(Menu.NONE, 0, 1, ss)
|
|
|
|
.setEnabled(false)
|
|
|
|
.setIcon(R.drawable.twotone_alternate_email_24);
|
|
|
|
popupMenu.getMenu().add(Menu.NONE, R.string.title_edit_contact, 2, R.string.title_edit_contact)
|
|
|
|
.setIcon(R.drawable.twotone_edit_24);
|
|
|
|
popupMenu.getMenu().add(Menu.NONE, R.string.title_clipboard_copy, 3, R.string.title_clipboard_copy)
|
|
|
|
.setIcon(R.drawable.twotone_file_copy_24);
|
|
|
|
popupMenu.getMenu().add(Menu.NONE, R.string.title_delete, 4, R.string.title_delete)
|
|
|
|
.setIcon(R.drawable.twotone_delete_24);
|
|
|
|
|
|
|
|
popupMenu.insertIcons(context);
|
2022-02-22 13:14:12 +00:00
|
|
|
|
|
|
|
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onMenuItemClick(MenuItem item) {
|
|
|
|
try {
|
|
|
|
int itemId = item.getItemId();
|
|
|
|
if (itemId == R.string.title_edit_contact) {
|
2022-02-22 14:14:40 +00:00
|
|
|
return onEdit();
|
|
|
|
} else if (itemId == R.string.title_clipboard_copy) {
|
|
|
|
return onCopy();
|
2022-02-22 13:14:12 +00:00
|
|
|
} else if (itemId == R.string.title_delete) {
|
2022-02-22 14:14:40 +00:00
|
|
|
return onDelete();
|
2022-02-22 13:14:12 +00:00
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2022-02-22 14:14:40 +00:00
|
|
|
|
|
|
|
private boolean onEdit() {
|
|
|
|
View dview = LayoutInflater.from(context).inflate(R.layout.dialog_edit_email, null);
|
|
|
|
EditText etEmail = dview.findViewById(R.id.etEmail);
|
|
|
|
EditText etName = dview.findViewById(R.id.etName);
|
|
|
|
|
|
|
|
etEmail.setText(e);
|
|
|
|
etName.setText(p);
|
|
|
|
|
|
|
|
new AlertDialog.Builder(context)
|
|
|
|
.setView(dview)
|
|
|
|
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
try {
|
|
|
|
String email = etEmail.getText().toString();
|
|
|
|
String name = etName.getText().toString();
|
|
|
|
|
|
|
|
InternetAddress a = new InternetAddress(email, name, StandardCharsets.UTF_8.name());
|
|
|
|
String formatted = MessageHelper.formatAddressesCompose(new Address[]{a});
|
|
|
|
|
|
|
|
edit.delete(start, end);
|
|
|
|
edit.insert(start, formatted);
|
|
|
|
setSelection(start + formatted.length());
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.setNegativeButton(android.R.string.cancel, null)
|
|
|
|
.show();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean onCopy() {
|
|
|
|
ClipboardManager clipboard =
|
|
|
|
(ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
|
|
|
|
if (clipboard == null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
String formatted = MessageHelper.formatAddressesCompose(parsed);
|
|
|
|
ClipData clip = ClipData.newPlainText(context.getString(R.string.app_name), formatted);
|
|
|
|
clipboard.setPrimaryClip(clip);
|
|
|
|
ToastEx.makeText(context, R.string.title_clipboard_copied, Toast.LENGTH_LONG).show();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean onDelete() {
|
|
|
|
edit.delete(start, end);
|
|
|
|
setSelection(start);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-02-22 13:14:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
popupMenu.show();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|