aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/utils/ClipboardUtil.java
blob: 4d6220e0ad9d61aa8c24f23ad03c388a7e5af2a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package de.thedevstack.conversationsplus.utils;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.widget.Toast;

import de.thedevstack.conversationsplus.ConversationsPlusApplication;

import eu.siacs.conversations.R;

/**
 * Util class to work with the Clipboard.
 */
public final class ClipboardUtil {
    private static final String CLIPBOARD_LABEL = "c+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 = ConversationsPlusApplication.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.cplus_copied_to_clipboard, Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(context, R.string.cplus_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
    }
}