package de.thedevstack.piratx.utils; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; import android.widget.Toast; import de.pixart.messenger.R; import de.thedevstack.piratx.PiratXApplication; /** * Util class to work with the Clipboard. */ public final class ClipboardUtil { private static final String CLIPBOARD_LABEL = "piratx+clipboard"; /** * Copies a text to the clipboard. * @param clipboardLabel the label to show to a user to allow identifying the text in clipboard. * @param text the text to copy */ public static void copyToClipboard(String clipboardLabel, String text) { Context context = PiratXApplication.getAppContext(); if (null != text && !text.isEmpty()) { String label = (null == clipboardLabel) ? CLIPBOARD_LABEL : clipboardLabel; ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText(label, text); clipboard.setPrimaryClip(clip); Toast.makeText(context, R.string.piratx_copied_to_clipboard, Toast.LENGTH_LONG).show(); } else { Toast.makeText(context, R.string.piratx_not_copied_to_clipboard_empty, Toast.LENGTH_LONG).show(); } } /** * Copies a text to the clipboard. * @param text the text to copy */ public static void copyToClipboard(String text) { copyToClipboard(CLIPBOARD_LABEL, text); } private ClipboardUtil() { // helper class - avoid instantiation } }