Example usage for android.content ClipDescription MIMETYPE_TEXT_PLAIN

List of usage examples for android.content ClipDescription MIMETYPE_TEXT_PLAIN

Introduction

In this page you can find the example usage for android.content ClipDescription MIMETYPE_TEXT_PLAIN.

Prototype

String MIMETYPE_TEXT_PLAIN

To view the source code for android.content ClipDescription MIMETYPE_TEXT_PLAIN.

Click Source Link

Document

The MIME type for a clip holding plain text.

Usage

From source file:Main.java

private static boolean hasText(Context c) {
    ClipboardManager clipboard = (ClipboardManager) c.getSystemService(Context.CLIPBOARD_SERVICE);

    return clipboard.hasPrimaryClip()
            && clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
}

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 {/*from www. j a va2 s . com*/
        //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 {/*from w w w  .j  av a  2  s  .  c  o  m*/
            return "";
        }
    } else {
        //noinspection deprecation
        return cm.getText();
    }
}

From source file:Main.java

/**
 * Get text from Clipboard//from  w w w. j  a  va 2 s  . co m
 *
 * @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

/**
 * This is only called when we are on API level > Honeycomb
 * @param context The context, required to get the Cliboard System Service. 
 * @return Returns the latest item added to the clipboard by the user, as long as the {@code MimeType} is {@code MIMETYPE_TEXT_PLAIN}
 *//*from  w  w w .j av a 2s .c  o  m*/
@SuppressLint("NewApi")
public static String getItemFromCliboard(final Context context) {
    ClipboardManager manager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    final ClipData data = manager.getPrimaryClip();

    if (data == null) { //If no data on clipboard, let's call it a day. 
        return EMPTY_STRING;
    }

    final String clipItem;

    if (data.getDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
        ClipData.Item item = data.getItemAt(0);
        clipItem = item.getText().toString();
    }

    else {
        clipItem = EMPTY_STRING;
    }

    return clipItem;

}

From source file:Main.java

/**
 * Return text content of clipboard as individual lines 
 * @param ctx/*from ww  w.j av a2 s  .co m*/
 * @return
 */
@SuppressLint("NewApi")
private static ArrayList<String> getTextLines(Context ctx) {

    String EOL = "\\r?\\n|\\r";

    if (checkForText(ctx)) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);

            // Gets the clipboard as text.
            CharSequence cs = item.getText();
            if (cs == null) { // item might be an URI
                Uri pasteUri = item.getUri();
                if (pasteUri != null) { // FIXME untested
                    try {
                        Log.d("ClipboardUtils", "Clipboard contains an uri");
                        ContentResolver cr = ctx.getContentResolver();
                        String uriMimeType = cr.getType(pasteUri);
                        //               pasteData = resolveUri(pasteUri);
                        // If the return value is not null, the Uri is a content Uri
                        if (uriMimeType != null) {

                            // Does the content provider offer a MIME type that the current application can use?
                            if (uriMimeType.equals(ClipDescription.MIMETYPE_TEXT_PLAIN)) {

                                // Get the data from the content provider.
                                Cursor pasteCursor = cr.query(pasteUri, null, null, null, null);

                                // If the Cursor contains data, move to the first record
                                if (pasteCursor != null) {
                                    if (pasteCursor.moveToFirst()) {
                                        String pasteData = pasteCursor.getString(0);
                                        return new ArrayList<String>(Arrays.asList(pasteData.split(EOL)));
                                    }
                                    // close the Cursor
                                    pasteCursor.close();
                                }
                            }
                        }
                    } catch (Exception e) { // FIXME given that the above is unteted, cath all here
                        Log.e("ClipboardUtils", "Resolving URI failed " + e);
                        e.printStackTrace();
                        return null;
                    }
                }
            } else {
                Log.d("ClipboardUtils", "Clipboard contains text");
                String pasteData = cs.toString();
                return new ArrayList<String>(Arrays.asList(pasteData.split(EOL)));
            }
        } else {
            // Gets the clipboard as text.
            @SuppressWarnings("deprecation")
            CharSequence cs = oldClipboard.getText();
            if (cs != null) {
                String pasteData = cs.toString();
                if (pasteData != null) { // should always be the case
                    return new ArrayList<String>(Arrays.asList(pasteData.split(EOL)));
                }
            }
        }
        Log.e("ClipboardUtils", "Clipboard contains an invalid data type");
    }
    return null;
}

From source file:Main.java

private static boolean hasText(ClipboardManager clipboardManager, ClipData clipData) {
    final ClipDescription description = clipboardManager.getPrimaryClipDescription();
    return clipData != null && description != null
            && description.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
}

From source file:Main.java

/**
 * This is only called when oldAPi is passed as false on {@link #addItemToClipboard(Context, String, String, boolean)}
 * @param context The context, required to get the Cliboard System Service. 
 * @param label The label to show to the user via {@code ClipDescription}
 * @param value The value to store on the clipboard. 
 *//* www .j a  va  2s.c  o m*/
@SuppressLint("NewApi")
private static void addItemToClipboard(final Context context, final String label, final String value) {
    ClipboardManager manager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData.Item item = new Item(value);
    ClipDescription description = new ClipDescription(label,
            new String[] { ClipDescription.MIMETYPE_TEXT_PLAIN });
    ClipData data = new ClipData(description, item);
    manager.setPrimaryClip(data);
}

From source file:me.zchang.onchart.ui.DonateFragment.java

@NonNull
@Override//  ww w  .j  av  a 2s .co m
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setPositiveButton(getString(R.string.button_donate), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = getActivity().getPackageManager()
                    .getLaunchIntentForPackage("com.eg.android.AlipayGphone");
            ClipboardManager clipboardManager = (ClipboardManager) getActivity()
                    .getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData data = ClipData.newPlainText(ClipDescription.MIMETYPE_TEXT_PLAIN,
                    getString(R.string.alipay_account));
            clipboardManager.setPrimaryClip(data);
            if (intent != null) {
                Toast.makeText(getActivity(), getString(R.string.alert_clipboard_complete), Toast.LENGTH_LONG)
                        .show();
                startActivity(intent);
            } else {
                Toast.makeText(getActivity(), getString(R.string.alert_no_alipay), Toast.LENGTH_SHORT).show();
            }
        }
    }).setView(getActivity().getLayoutInflater().inflate(R.layout.fragment_donate, null));
    return builder.create();
}

From source file:com.coloreight.plugin.clipboard.Clipboard.java

public void paste(final CallbackContext callbackContext) {
    cordova.getActivity().runOnUiThread(new Runnable() {
        public void run() {
            ClipboardManager clipboard = (ClipboardManager) cordova.getActivity()
                    .getSystemService(Context.CLIPBOARD_SERVICE);

            if (!clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.NO_RESULT));
            }//from   w ww . java  2 s . co m

            try {
                ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
                String text = item.getText().toString();

                callbackContext.success(text);
            } catch (Exception e) {
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, e.toString()));
            }
        }
    });
}