Added copy contact email/name to clipboard

pull/213/head
M66B 1 year ago
parent 7c91e4bacc
commit 9276018795

@ -20,6 +20,8 @@ package eu.faircode.email;
*/
import android.Manifest;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
@ -41,6 +43,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.PopupMenu;
@ -55,11 +58,16 @@ import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.mail.Address;
import javax.mail.internet.InternetAddress;
public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHolder> {
private Fragment parentFragment;
@ -244,6 +252,7 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
contact.type != EntityContact.TYPE_NO_JUNK)
popupMenu.getMenu().add(Menu.NONE, R.string.title_advanced_never_favorite, order++, R.string.title_advanced_never_favorite);
popupMenu.getMenu().add(Menu.NONE, R.string.title_share, order++, R.string.title_share); // should be system whitelisted
popupMenu.getMenu().add(Menu.NONE, R.string.title_clipboard_copy, order++, R.string.title_clipboard_copy); // should be system whitelisted
if (Shortcuts.can(context))
popupMenu.getMenu().add(Menu.NONE, R.string.title_pin, order++, R.string.title_pin);
popupMenu.getMenu().add(Menu.NONE, R.string.title_edit_contact, order++, R.string.title_edit_contact);
@ -260,6 +269,9 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
} else if (itemId == R.string.title_share) {
onActionShare();
return true;
} else if (itemId == R.string.title_clipboard_copy) {
onActionCopy();
return true;
} else if (itemId == R.string.title_pin) {
onActionPin();
return true;
@ -311,6 +323,27 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
}
}
private void onActionCopy() {
ClipboardManager clipboard = Helper.getSystemService(context, ClipboardManager.class);
if (clipboard == null)
return;
String copy;
try {
InternetAddress address = new InternetAddress(contact.email, contact.name, StandardCharsets.UTF_8.name());
copy = MessageHelper.formatAddresses(new Address[]{address});
} catch (UnsupportedEncodingException ex) {
Log.e(ex);
copy = contact.email;
}
ClipData clip = ClipData.newPlainText(context.getString(R.string.app_name), copy);
clipboard.setPrimaryClip(clip);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU)
ToastEx.makeText(context, R.string.title_clipboard_copied, Toast.LENGTH_LONG).show();
}
private void onActionPin() {
ShortcutInfoCompat.Builder builder = Shortcuts.getShortcut(context, contact);
Shortcuts.requestPinShortcut(context, builder.build());

Loading…
Cancel
Save