Added chip menu

pull/194/merge
M66B 3 years ago
parent 083a7fa49c
commit 2baa9c8128

@ -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);

@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="24dp">
<eu.faircode.email.FixedTextView
android:id="@+id/tvEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableStart="@drawable/twotone_edit_24"
android:drawablePadding="6dp"
android:labelFor="@+id/etName"
android:text="@string/title_edit_contact"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<eu.faircode.email.EditTextPlain
android:id="@+id/etEmail"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:drawableStart="@drawable/twotone_alternate_email_24"
android:drawablePadding="6dp"
android:imeOptions="actionDone"
android:inputType="textEmailAddress"
android:text="Email"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvEdit" />
<eu.faircode.email.EditTextPlain
android:id="@+id/etName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:drawableStart="@drawable/twotone_person_24"
android:drawablePadding="6dp"
android:imeOptions="actionDone"
android:inputType="textPersonName|textCapWords"
android:text="Name"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etEmail" />
</androidx.constraintlayout.widget.ConstraintLayout>
Loading…
Cancel
Save