|
|
@ -19,57 +19,37 @@ package eu.faircode.email;
|
|
|
|
Copyright 2018-2022 by Marcel Bokhorst (M66B)
|
|
|
|
Copyright 2018-2022 by Marcel Bokhorst (M66B)
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.ClipData;
|
|
|
|
|
|
|
|
import android.content.ClipboardManager;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.content.res.ColorStateList;
|
|
|
|
import android.content.res.ColorStateList;
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.Rect;
|
|
|
|
import android.graphics.Rect;
|
|
|
|
import android.graphics.Typeface;
|
|
|
|
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.provider.ContactsContract;
|
|
|
|
import android.provider.ContactsContract;
|
|
|
|
import android.text.Editable;
|
|
|
|
import android.text.Editable;
|
|
|
|
import android.text.SpannableString;
|
|
|
|
|
|
|
|
import android.text.Spanned;
|
|
|
|
import android.text.Spanned;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.text.TextWatcher;
|
|
|
|
import android.text.TextWatcher;
|
|
|
|
import android.text.style.DynamicDrawableSpan;
|
|
|
|
import android.text.style.DynamicDrawableSpan;
|
|
|
|
import android.text.style.ImageSpan;
|
|
|
|
import android.text.style.ImageSpan;
|
|
|
|
import android.text.style.RelativeSizeSpan;
|
|
|
|
|
|
|
|
import android.text.style.StyleSpan;
|
|
|
|
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.ContextThemeWrapper;
|
|
|
|
import android.view.ContextThemeWrapper;
|
|
|
|
import android.view.KeyEvent;
|
|
|
|
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
|
|
|
|
import android.view.Menu;
|
|
|
|
|
|
|
|
import android.view.MenuItem;
|
|
|
|
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
import android.view.View;
|
|
|
|
|
|
|
|
import android.view.inputmethod.EditorInfo;
|
|
|
|
|
|
|
|
import android.widget.EditText;
|
|
|
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
|
|
|
|
|
import androidx.appcompat.widget.AppCompatMultiAutoCompleteTextView;
|
|
|
|
import androidx.appcompat.widget.AppCompatMultiAutoCompleteTextView;
|
|
|
|
import androidx.appcompat.widget.PopupMenu;
|
|
|
|
|
|
|
|
import androidx.core.graphics.ColorUtils;
|
|
|
|
import androidx.core.graphics.ColorUtils;
|
|
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
|
|
|
|
|
|
|
|
import com.google.android.material.chip.ChipDrawable;
|
|
|
|
import com.google.android.material.chip.ChipDrawable;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.mail.Address;
|
|
|
|
|
|
|
|
import javax.mail.internet.AddressException;
|
|
|
|
import javax.mail.internet.AddressException;
|
|
|
|
import javax.mail.internet.InternetAddress;
|
|
|
|
import javax.mail.internet.InternetAddress;
|
|
|
|
|
|
|
|
|
|
|
@ -181,141 +161,10 @@ public class EditTextMultiAutoComplete extends AppCompatMultiAutoCompleteTextVie
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
|
|
|
|
if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
|
|
|
|
Editable edit = getText();
|
|
|
|
Editable edit = getText();
|
|
|
|
|
|
|
|
|
|
|
|
int off = Helper.getOffset(this, edit, event);
|
|
|
|
int off = Helper.getOffset(this, edit, event);
|
|
|
|
ClipImageSpan[] spans = edit.getSpans(off, off, ClipImageSpan.class);
|
|
|
|
ClipImageSpan[] spans = edit.getSpans(off, off, ClipImageSpan.class);
|
|
|
|
if (spans.length != 1)
|
|
|
|
if (spans.length == 1)
|
|
|
|
return super.onTouchEvent(event);
|
|
|
|
edit.removeSpan(spans[0]);
|
|
|
|
|
|
|
|
|
|
|
|
final Context context = getContext();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int start = edit.getSpanStart(spans[0]);
|
|
|
|
|
|
|
|
int end = edit.getSpanEnd(spans[0]);
|
|
|
|
|
|
|
|
if (start >= end)
|
|
|
|
|
|
|
|
return super.onTouchEvent(event);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
return super.onTouchEvent(event);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public boolean onMenuItemClick(MenuItem item) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
int itemId = item.getItemId();
|
|
|
|
|
|
|
|
if (itemId == R.string.title_edit_contact) {
|
|
|
|
|
|
|
|
return onEdit();
|
|
|
|
|
|
|
|
} else if (itemId == R.string.title_clipboard_copy) {
|
|
|
|
|
|
|
|
return onCopy();
|
|
|
|
|
|
|
|
} else if (itemId == R.string.title_delete) {
|
|
|
|
|
|
|
|
return onDelete();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
|
|
|
|
Log.e(ex);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AlertDialog dialog = 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)
|
|
|
|
|
|
|
|
.create();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TextView.OnEditorActionListener done = new TextView.OnEditorActionListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
|
|
|
|
|
|
|
if (actionId == EditorInfo.IME_ACTION_DONE) {
|
|
|
|
|
|
|
|
dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} else
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
etEmail.setOnEditorActionListener(done);
|
|
|
|
|
|
|
|
etName.setOnEditorActionListener(done);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dialog.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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
popupMenu.show();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
Log.w(ex);
|
|
|
|