Example usage for android.content ClipData getItemAt

List of usage examples for android.content ClipData getItemAt

Introduction

In this page you can find the example usage for android.content ClipData getItemAt.

Prototype

public Item getItemAt(int index) 

Source Link

Document

Return a single item inside of the clip data.

Usage

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 ww  w . j  a va 2  s.  co  m*/
            return null;
        }
    } else {
        return clipboardManager.getText().toString();
    }
}

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 a va  2 s .  c  o  m*/
            return "";
        }
    } else {
        //noinspection deprecation
        return cm.getText();
    }
}

From source file:Main.java

public static CharSequence getText(Context context) {
    StringBuilder sb = new StringBuilder();
    if (isNew()) {
        instance(context);/*from   w  w w. j ava 2 s .c om*/
        if (!mNewCliboardManager.hasPrimaryClip()) {
            return sb.toString();
        } else {
            ClipData clipData = (mNewCliboardManager).getPrimaryClip();
            int count = clipData.getItemCount();

            for (int i = 0; i < count; ++i) {

                ClipData.Item item = clipData.getItemAt(i);
                CharSequence str = item.coerceToText(context);
                sb.append(str);
            }
        }

    } else {
        instance(context);
        sb.append(mClipboardManager.getText());
    }
    return sb.toString();
}

From source file:Main.java

/**
 * /*from w w  w . j  a v  a 2  s  . c  o  m*/
 * @param context
 * @return
 */
public static CharSequence pasteToResult(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ClipData clipData = ((android.content.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE)).getPrimaryClip();
        if ((clipData == null) || (clipData.getItemCount() <= 0)) {
            return null;
        }

        ClipData.Item item = clipData.getItemAt(0);
        if (item == null) {
            return null;
        }
        return item.getText();
    }

    return ((android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE)).getText();
}

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}
 */// w w  w  .ja v a 2 s. 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

/**
 * Convert {@link ClipData} to String for storing.
 * @param clip//from  w w w.ja  v  a2s  .c  om
 * @return String for specifed clipdata
 */
public static CharSequence stringFromClipData(ClipData clip) {
    StringBuilder sb = new StringBuilder();
    if (clip != null) {
        append(sb, "clip");
        appendClipDescription(sb, clip.getDescription());
        int n = clip.getItemCount();
        appendInt(sb, n);
        for (int i = 0; i < n; i++) {
            appendItem(sb, clip.getItemAt(i));
        }
    } else {
        append(sb, (String) null);
    }
    return sb;
}

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;
    }// 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:com.pavlospt.rxfile.RxFile.java

public static Observable<List<File>> createFilesFromClipData(final Context context, final ClipData clipData) {

    return Observable.defer(new Func0<Observable<List<File>>>() {
        @Override/*from   w w  w. j a va 2 s. co  m*/
        public Observable<List<File>> call() {
            int numOfUris = clipData.getItemCount();
            List<File> filesRetrieved = new ArrayList<>(numOfUris);

            for (int i = 0; i < numOfUris; i++) {
                Uri data = clipData.getItemAt(i).getUri();
                if (data != null) {
                    try {
                        filesRetrieved.add(fileFromUri(context, data));
                    } catch (Exception e) {
                        logError("Exception:" + e.getMessage() + " line: 117");
                        e.printStackTrace();
                        return Observable.error(e);
                    }
                }
            }
            return Observable.just(filesRetrieved);
        }
    });

}

From source file:com.xxxifan.devbox.library.rxfile.RxFile.java

public static Observable<List<File>> createFilesFromClipData(final Context context, final ClipData clipData) {

    return Observable.defer(new Func0<Observable<List<File>>>() {
        @Override/*  www. j  a v a 2 s .  c  om*/
        public Observable<List<File>> call() {
            int numOfUris = clipData.getItemCount();
            List<File> filesRetrieved = new ArrayList<>(numOfUris);

            for (int i = 0; i < numOfUris; i++) {
                Uri data = clipData.getItemAt(i).getUri();
                if (data != null) {
                    try {
                        filesRetrieved.add(fileFromUri(context, data));
                    } catch (Exception e) {
                        Log.e(TAG, "Exception: " + e.getMessage());
                        e.printStackTrace();
                        return Observable.error(e);
                    }
                }
            }
            return Observable.just(filesRetrieved);
        }
    });

}

From source file:com.commonsware.android.tte.MainActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case REQUEST_OPEN:
        if (resultCode == Activity.RESULT_OK) {
            if (data.getData() == null) {
                ClipData clip = data.getClipData();

                for (int i = 0; i < clip.getItemCount(); i++) {
                    openEditor(clip.getItemAt(i).getUri());
                }//  ww  w  . ja v a 2 s . co m
            } else {
                openEditor(data.getData());
            }
        }
        break;

    case REQUEST_CREATE:
        if (resultCode == Activity.RESULT_OK) {
            openEditor(data.getData());
        }
        break;
    }
}