mirror of
https://github.com/M66B/FairEmail.git
synced 2026-04-02 07:06:18 +02:00
Added chip menu
This commit is contained in:
@@ -21,36 +21,49 @@ package eu.faircode.email;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.res.ColorStateList;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Typeface;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.provider.ContactsContract;
|
||||
import android.text.Editable;
|
||||
import android.text.SpannableString;
|
||||
import android.text.Spanned;
|
||||
import android.text.TextUtils;
|
||||
import android.text.TextWatcher;
|
||||
import android.text.style.DynamicDrawableSpan;
|
||||
import android.text.style.ImageSpan;
|
||||
import android.text.style.RelativeSizeSpan;
|
||||
import android.text.style.StyleSpan;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.view.ContextThemeWrapper;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.widget.EditText;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.widget.AppCompatMultiAutoCompleteTextView;
|
||||
import androidx.appcompat.widget.PopupMenu;
|
||||
import androidx.core.graphics.ColorUtils;
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import com.google.android.material.chip.ChipDrawable;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.mail.Address;
|
||||
import javax.mail.internet.AddressException;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
|
||||
@@ -229,6 +242,94 @@ public class EditTextMultiAutoComplete extends AppCompatMultiAutoCompleteTextVie
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent event) {
|
||||
try {
|
||||
int action = event.getActionMasked();
|
||||
if (action == MotionEvent.ACTION_DOWN) {
|
||||
Editable edit = getText();
|
||||
|
||||
int off = Helper.getOffset(this, edit, event);
|
||||
ClipImageSpan[] spans = edit.getSpans(off, off, ClipImageSpan.class);
|
||||
if (spans.length != 1)
|
||||
return false;
|
||||
|
||||
final Context context = getContext();
|
||||
|
||||
int start = edit.getSpanStart(spans[0]);
|
||||
int end = edit.getSpanEnd(spans[0]);
|
||||
if (start >= end)
|
||||
return false;
|
||||
|
||||
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 false;
|
||||
|
||||
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);
|
||||
|
||||
PopupMenu popupMenu = new PopupMenu(context, this);
|
||||
popupMenu.getMenu().add(Menu.NONE, 0, 1, ss).setEnabled(false);
|
||||
popupMenu.getMenu().add(Menu.NONE, R.string.title_edit_contact, 2, R.string.title_edit_contact);
|
||||
popupMenu.getMenu().add(Menu.NONE, R.string.title_delete, 3, R.string.title_delete);
|
||||
|
||||
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
|
||||
@Override
|
||||
public boolean onMenuItemClick(MenuItem item) {
|
||||
try {
|
||||
int itemId = item.getItemId();
|
||||
if (itemId == R.string.title_edit_contact) {
|
||||
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;
|
||||
} else if (itemId == R.string.title_delete) {
|
||||
edit.delete(start, end);
|
||||
setSelection(start);
|
||||
return true;
|
||||
}
|
||||
} catch (Throwable ex) {
|
||||
Log.e(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
popupMenu.show();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return super.onTouchEvent(event);
|
||||
} catch (Throwable ex) {
|
||||
Log.w(ex);
|
||||
|
||||
Reference in New Issue
Block a user