~singpolyma/cheogram-android

166794e3907e0986fec50eea74b5889669240fe5 — Stephen Paul Weber 1 year, 7 months ago 4725daf
Refactor command UI clipboard to use ShareUtil
M src/main/java/eu/siacs/conversations/entities/Conversation.java => src/main/java/eu/siacs/conversations/entities/Conversation.java +5 -7
@@ 1,9 1,6 @@
package eu.siacs.conversations.entities;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.graphics.Rect;


@@ 82,6 79,8 @@ import eu.siacs.conversations.persistance.DatabaseBackend;
import eu.siacs.conversations.services.AvatarService;
import eu.siacs.conversations.services.QuickConversationsService;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.ui.text.FixedURLSpan;
import eu.siacs.conversations.ui.util.ShareUtil;
import eu.siacs.conversations.utils.JidHelper;
import eu.siacs.conversations.utils.MessageUtils;
import eu.siacs.conversations.utils.UIHelper;


@@ 1500,11 1499,10 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
                    }
                    binding.values.setAdapter(values);

                    ClipboardManager clipboard = (ClipboardManager) binding.getRoot().getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                    binding.values.setOnItemLongClickListener((arg0, arg1, pos, id) -> {
                        ClipData myClip = ClipData.newPlainText("text", values.getItem(pos));
                        clipboard.setPrimaryClip(myClip);
                        Toast.makeText(binding.getRoot().getContext(), R.string.message_copied_to_clipboard, Toast.LENGTH_SHORT).show();
                        if (ShareUtil.copyTextToClipboard(binding.getRoot().getContext(), values.getItem(pos), R.string.message)) {
                            Toast.makeText(binding.getRoot().getContext(), R.string.message_copied_to_clipboard, Toast.LENGTH_SHORT).show();
                        }
                        return true;
                    });
                }

M src/main/java/eu/siacs/conversations/ui/text/FixedURLSpan.java => src/main/java/eu/siacs/conversations/ui/text/FixedURLSpan.java +1 -1
@@ 50,7 50,7 @@ import eu.siacs.conversations.ui.ConversationsActivity;
@SuppressLint("ParcelCreator")
public class FixedURLSpan extends URLSpan {

	private FixedURLSpan(String url) {
	public FixedURLSpan(String url) {
		super(url);
	}


M src/main/java/eu/siacs/conversations/ui/util/ShareUtil.java => src/main/java/eu/siacs/conversations/ui/util/ShareUtil.java +19 -5
@@ 30,6 30,9 @@
package eu.siacs.conversations.ui.util;

import android.content.ActivityNotFoundException;
import android.content.ClipboardManager;
import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.SpannableStringBuilder;


@@ 108,18 111,18 @@ public class ShareUtil {
		}
	}

	public static void copyLinkToClipboard(final XmppActivity activity, final String url) {
	public static void copyLinkToClipboard(final Context context, final String url) {
		final Uri uri = Uri.parse(url);
		if ("xmpp".equals(uri.getScheme())) {
			try {
				final Jid jid = new XmppUri(uri).getJid();
				if (activity.copyTextToClipboard(jid.asBareJid().toString(), R.string.account_settings_jabber_id)) {
					Toast.makeText(activity, R.string.jabber_id_copied_to_clipboard, Toast.LENGTH_SHORT).show();
				if (copyTextToClipboard(context, jid.asBareJid().toString(), R.string.account_settings_jabber_id)) {
					Toast.makeText(context, R.string.jabber_id_copied_to_clipboard, Toast.LENGTH_SHORT).show();
				}
			} catch (final Exception e) { }
		} else {
			if (activity.copyTextToClipboard(url, R.string.web_address)) {
				Toast.makeText(activity, R.string.url_copied_to_clipboard, Toast.LENGTH_SHORT).show();
			if (copyTextToClipboard(context, url, R.string.web_address)) {
				Toast.makeText(context, R.string.url_copied_to_clipboard, Toast.LENGTH_SHORT).show();
			}
		}
	}


@@ 144,4 147,15 @@ public class ShareUtil {
		}
		return false;
	}

	public static boolean copyTextToClipboard(Context context, String text, int labelResId) {
		ClipboardManager mClipBoardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
		String label = context.getResources().getString(labelResId);
		if (mClipBoardManager != null) {
			ClipData mClipData = ClipData.newPlainText(label, text);
			mClipBoardManager.setPrimaryClip(mClipData);
			return true;
		}
		return false;
	}
}