2018-05-02 17:29:58 +02:00
|
|
|
/*
|
|
|
|
* Copyright © 2018 Samuel Holland <samuel@sholland.org>
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
package com.wireguard.android.util;
|
|
|
|
|
|
|
|
import android.content.ClipData;
|
|
|
|
import android.content.ClipboardManager;
|
|
|
|
import android.content.Context;
|
2018-04-29 07:11:08 +02:00
|
|
|
import android.support.design.widget.Snackbar;
|
2018-01-01 09:06:37 +01:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
/**
|
2018-01-09 03:42:58 +01:00
|
|
|
* Standalone utilities for interacting with the system clipboard.
|
2018-01-01 09:06:37 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
public final class ClipboardUtils {
|
|
|
|
private ClipboardUtils() {
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|