summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/util/ClipboardUtils.java
blob: 0df5e96aeb81043080e4550d99e166bf2f64a411 (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
/*
 * Copyright © 2017-2019 WireGuard LLC. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package com.wireguard.android.util;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import com.google.android.material.snackbar.Snackbar;
import android.view.View;
import android.widget.TextView;

/**
 * Standalone utilities for interacting with the system clipboard.
 */

public final class ClipboardUtils {
    private ClipboardUtils() {
        // Prevent instantiation
    }

    public static void copyTextView(final View view) {
        if (!(view instanceof TextView))
            return;
        final CharSequence text = ((TextView) view).getText();
        if (text == null || text.length() == 0)
            return;
        final Object service = view.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
        if (!(service instanceof ClipboardManager))
            return;
        final CharSequence description = view.getContentDescription();
        ((ClipboardManager) service).setPrimaryClip(ClipData.newPlainText(description, text));
        Snackbar.make(view, description + " copied to clipboard", Snackbar.LENGTH_LONG).show();
    }
}