package cgeo.geocaching.utils;

import cgeo.geocaching.CgeoApplication;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Clipboard Utilities. Functions to copy data to the Android clipboard.
 * This class uses the deprecated function ClipboardManager.setText(CharSequence).
 * API 11 introduced setPrimaryClip(ClipData)
 */
public final class ClipboardUtils {

    private ClipboardUtils() {
        // utility class
    }

    /**
     * Places the text passed in onto the clipboard as text
     *
     * @param text
     *            The text to place in the clipboard.
     */
    public static void copyToClipboard(@NonNull final CharSequence text) {
        // fully qualified name used here to avoid buggy deprecation warning (of javac) on the import statement
        final ClipboardManager clipboard = (ClipboardManager) CgeoApplication.getInstance().getSystemService(Context.CLIPBOARD_SERVICE);
        if (clipboard != null) {
            final ClipData data = ClipData.newPlainText(null, text);
            clipboard.setPrimaryClip(data);
        }
    }

    /**
     * get clipboard content
     *
     */
    @Nullable
    public static String getText() {
        // fully qualified name used here to avoid buggy deprecation warning (of javac) on the import statement
        final ClipboardManager clipboard = (ClipboardManager) CgeoApplication.getInstance().getSystemService(Context.CLIPBOARD_SERVICE);
        final CharSequence text = clipboard.getText();
        return text != null ? text.toString() : null;
    }

    /**
     * clear clipboard content
     * (up to API level 28: replace with empty string)
     */
    public static void clearClipboard() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
            ((ClipboardManager) CgeoApplication.getInstance().getSystemService(Context.CLIPBOARD_SERVICE)).clearPrimaryClip();
        } else {
            copyToClipboard("");
        }
    }
}

@@ -330,135 +330,9 @@
 .%0A *
- This class uses the deprecated function ClipboardManager.setText(CharSequence).%0A * API 11 introduced setPrimaryClip(ClipData)
 %0A
+
  */%0A
@@ -666,120 +666,8 @@
 ) %7B%0A
-        // fully qualified name used here to avoid buggy deprecation warning (of javac) on the import statement%0A
     
@@ -1069,120 +1069,8 @@
 ) %7B%0A
-        // fully qualified name used here to avoid buggy deprecation warning (of javac) on the import statement%0A
     
@@ -1194,32 +1194,149 @@
 BOARD_SERVICE);%0A
+        final ClipData clip = clipboard.getPrimaryClip();%0A        if (clip != null && clip.getItemCount() %3E 0) %7B%0A    
         final Ch
@@ -1353,29 +1353,37 @@
  text = clip
-board
+.getItemAt(0)
 .getText();%0A
@@ -1378,24 +1378,28 @@
 .getText();%0A
+    
         retu
@@ -1440,16 +1440,47 @@
 : null;%0A
+        %7D%0A        return null;%0A
     %7D%0A%0A 

Migrate deprecated clipboard methods