Example usage for android.content ClipboardManager getPrimaryClip

List of usage examples for android.content ClipboardManager getPrimaryClip

Introduction

In this page you can find the example usage for android.content ClipboardManager getPrimaryClip.

Prototype

public @Nullable ClipData getPrimaryClip() 

Source Link

Document

Returns the current primary clip on the clipboard.

Usage

From source file:Main.java

@SuppressWarnings("deprecation")
public static boolean hasText(Context context) {
    ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ClipData clipData = clipboardManager.getPrimaryClip();
        return hasText(clipboardManager, clipData);
    } else {//from w w w . j a  va  2  s  .c  o m
        return clipboardManager.hasText();
    }
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static String getText(Context context) {
    ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ClipData clipData = clipboardManager.getPrimaryClip();
        if (hasText(clipboardManager, clipData)) {
            return clipData.getItemAt(0).getText().toString();
        } else {/*from  w w w. ja  v a  2s  . com*/
            return null;
        }
    } else {
        return clipboardManager.getText().toString();
    }
}

From source file:Main.java

public static boolean hasText(Context context) {
    ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ClipDescription description = cm.getPrimaryClipDescription();
        ClipData clipData = cm.getPrimaryClip();
        return clipData != null && description != null
                && (description.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN));
    } else {/*  w  ww .  j  a  va  2 s .  c  o  m*/
        //noinspection deprecation
        return cm.hasText();
    }
}

From source file:Main.java

public static CharSequence getText(Context context) {
    ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ClipDescription description = cm.getPrimaryClipDescription();
        ClipData clipData = cm.getPrimaryClip();
        if (clipData != null && description != null
                && description.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
            return clipData.getItemAt(0).getText();
        } else {/* w ww.  j  a va2  s  . co  m*/
            return "";
        }
    } else {
        //noinspection deprecation
        return cm.getText();
    }
}

From source file:Main.java

@SuppressWarnings("ConstantConditions")
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private static String getClipboardTextNew(Context ctx) {
    String txt = null;/*from w  w w . j a v  a 2 s .co m*/
    android.content.ClipboardManager clipboard = (android.content.ClipboardManager) ctx
            .getSystemService(Context.CLIPBOARD_SERVICE);
    if (clipboard != null) {
        if (clipboard.hasPrimaryClip()) {
            txt = clipboard.getPrimaryClip().getItemAt(0).getText().toString();
        }
    }
    return txt;
}

From source file:Main.java

/**
 * Get text from Clipboard//from  ww w.  j  a v a 2  s.  c  om
 *
 * @param context
 * @return
 */
public static String getText(Context context) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (clipboard.hasPrimaryClip()
            && clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
        ClipData clip = clipboard.getPrimaryClip();
        if (clip != null && clip.getItemCount() > 0) {
            return String.valueOf(clip.getItemAt(0).coerceToText(context));
        }
    }
    return null;
}

From source file:Main.java

/**
 * Get intent from Clipboard//w ww .  jav  a2  s  . c om
 *
 * @param context
 * @return
 */
public static Intent getIntent(Context context) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (clipboard.hasPrimaryClip()
            && clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_INTENT)) {
        ClipData clip = clipboard.getPrimaryClip();
        if (clip != null && clip.getItemCount() > 0) {
            return clip.getItemAt(0).getIntent();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Get uri from Clipboard/*from   w  w  w  . jav a2s  .  c  om*/
 *
 * @param context
 * @return
 */
public static Uri getUri(Context context) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (clipboard.hasPrimaryClip()
            && clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_URILIST)) {
        ClipData clip = clipboard.getPrimaryClip();
        if (clip != null && clip.getItemCount() > 0) {
            return clip.getItemAt(0).getUri();
        }
    }
    return null;
}

From source file:org.proninyaroslav.libretorrent.core.utils.Utils.java

@Nullable
public static String getClipboard(Context context) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);

    if (!clipboard.hasPrimaryClip()) {
        return null;
    }/*from   w w w.  j a  v a  2  s . c  o m*/

    ClipData clip = clipboard.getPrimaryClip();

    if (clip == null || clip.getItemCount() == 0) {
        return null;
    }

    CharSequence text = clip.getItemAt(0).getText();
    if (text == null) {
        return null;
    }

    return text.toString();
}

From source file:org.ttrssreader.utils.Utils.java

public static String getTextFromClipboard(Context context) {
    // New Clipboard API
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (clipboard.hasPrimaryClip()) {

        if (!clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN))
            return null;

        ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
        CharSequence chars = item.getText();
        if (chars != null && chars.length() > 0) {
            return chars.toString();
        } else {/*from   ww  w  .j ava 2s  .co m*/
            Uri pasteUri = item.getUri();
            if (pasteUri != null) {
                return pasteUri.toString();
            }
        }
    }
    return null;
}