Java Code Examples for android.content.ClipData#Item

The following examples show how to use android.content.ClipData#Item . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: FireFiles   File: StandaloneActivity.java    License: Apache License 2.0 6 votes vote down vote up
private void onFinished(Uri... uris) {
    Log.d(TAG, "onFinished() " + Arrays.toString(uris));
    final Intent intent = new Intent();
    if (uris.length == 1) {
        intent.setData(uris[0]);
    } else if (uris.length > 1) {
        final ClipData clipData = new ClipData(
                null, mState.acceptMimes, new ClipData.Item(uris[0]));
        for (int i = 1; i < uris.length; i++) {
            clipData.addItem(new ClipData.Item(uris[i]));
        }
        if(Utils.hasJellyBean()){
            intent.setClipData(clipData);
        }
        else{
            intent.setData(uris[0]);
        }
    }
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
            | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
            | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
    setResult(Activity.RESULT_OK, intent);
    finish();
}
 
Example 2
Source Project: ProjectX   File: ClipboardHelper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 判断是否能粘贴
 *
 * @param context Context
 * @return 是否能粘贴
 */
public boolean canPaste(Context context) {
    if (mMatcher == null)
        return false;
    final ClipboardManager manager = (ClipboardManager)
            context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (manager == null)
        return false;
    final ClipData clip = manager.getPrimaryClip();
    if (clip == null)
        return false;
    if (clip.getItemCount() <= 0)
        return false;
    final ClipData.Item item = clip.getItemAt(0);
    final Uri uri = item.getUri();
    if (uri == null)
        return false;
    final String type = mTypes.get(mMatcher.match(uri));
    return type != null && TextUtils.equals(type, context.getContentResolver().getType(uri));
}
 
Example 3
Source Project: AgentWeb   File: FileChooser.java    License: Apache License 2.0 6 votes vote down vote up
private Uri[] processData(Intent data) {

        Uri[] datas = null;
        if (data == null) {
            return datas;
        }
        String target = data.getDataString();
        if (!TextUtils.isEmpty(target)) {
            return datas = new Uri[]{Uri.parse(target)};
        }
        ClipData mClipData = data.getClipData();
        if (mClipData != null && mClipData.getItemCount() > 0) {
            datas = new Uri[mClipData.getItemCount()];
            for (int i = 0; i < mClipData.getItemCount(); i++) {

                ClipData.Item mItem = mClipData.getItemAt(i);
                datas[i] = mItem.getUri();

            }
        }
        return datas;


    }
 
Example 4
@Override
public boolean onLongClick(View v) {
    TextView thisTextView = (TextView) v;
    String dragContent = "Dragged Text: " + thisTextView.getText();

    //Set the drag content and type.
    ClipData.Item item = new ClipData.Item(dragContent);
    ClipData dragData = new ClipData(dragContent,
            new String[] {ClipDescription.MIMETYPE_TEXT_PLAIN}, item);

    //Set the visual look of the dragged object.
    //Can be extended and customized. Default is used here.
    View.DragShadowBuilder dragShadow = new View.DragShadowBuilder(v);

    // Starts the drag, note: global flag allows for cross-application drag.
    v.startDragAndDrop(dragData, dragShadow, null, DRAG_FLAG_GLOBAL);

    return false;
}
 
Example 5
Source Project: Readily   File: ClipboardReadable.java    License: MIT License 6 votes vote down vote up
private String paste(){
	oncePasted = true;
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
		ClipData clipData = clipboardNew.getPrimaryClip();
		if (clipData != null && clipData.getItemCount() > 0){
			ClipData.Item item = clipboardNew.getPrimaryClip().getItemAt(0);
			CharSequence pasteData = item.getText();
			if (pasteData != null){
				return pasteData.toString();
			}
		}
	} else {
		return clipboardOld.getText().toString();
	}
	return null;
}
 
Example 6
@Override
public boolean onTextContextMenuItem(int id) {
    try {
        if (id == android.R.id.paste) {
            Context context = getContext();
            ClipboardManager cbm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
            if (cbm != null && cbm.hasPrimaryClip()) {
                ClipData data = cbm.getPrimaryClip();
                ClipData.Item item = data.getItemAt(0);

                CharSequence text = item.coerceToText(context);
                data = ClipData.newPlainText("coerced_plain_text", text);
                cbm.setPrimaryClip(data);
            }
        }

        return super.onTextContextMenuItem(id);
    } catch (Throwable ex) {
        Log.w(ex);
        return false;
    }
}
 
Example 7
Source Project: AndroidProjects   File: PassWordInputLayout.java    License: MIT License 6 votes vote down vote up
private boolean handleWrapperLongClicked() {
    final ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clipData = null;

    if (clipboard != null) {
        clipData = clipboard.getPrimaryClip();
    }

    if (clipData == null || clipData.getItemCount() == 0) return false;

    final ClipData.Item clipItem = clipData.getItemAt(0);

    if (clipItem == null || clipItem.getText() == null) return false;

    final List<String> wordList = Arrays.asList(clipItem.getText().toString().split(" "));

    if (wordList.size() > 12) {
        showErrorMessage(getContext().getString(R.string.paste_passphrase_error));
        return false;
    }

    pastePassphrase(wordList);
    return true;
}
 
Example 8
Source Project: 920-text-editor-v2   File: EditAreaView.java    License: Apache License 2.0 5 votes vote down vote up
public boolean paste() {
        ClipboardManager clipboard = (ClipboardManager) getContext().
                getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);

// Gets the clipboard as text.
        CharSequence pasteData = item.getText();
        if (pasteData == null)
            return false;

        execCommand(new EditorCommand.Builder("onPaste").put("text", pasteData.toString()).build());
        return true;
    }
 
Example 9
Source Project: timecat   File: ClipboardManagerImpl11.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public CharSequence getText() {
    if (mClipboardManager.getPrimaryClip().getItemCount() > 0) {
        ClipData.Item item = mClipboardManager.getPrimaryClip().getItemAt(0);
        return item.getText();
    }
    return "";
}
 
Example 10
Source Project: quickimagepick   File: QiPick.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
private static void handleResultFromDocuments(final int pRequestType, @NonNull final PickCallback pCallback, @Nullable final Intent pData) {

    final Uri pictureUri = pData == null ? null : pData.getData();

    if (pictureUri == null) {

        final ClipData clipData = API_18 ? pData != null ? pData.getClipData() : null : null;
        if (clipData != null) {

            final ArrayList<Uri> uris = new ArrayList<>();
            for (int i = 0; i < clipData.getItemCount(); i++) {

                final ClipData.Item item = clipData.getItemAt(i);

                final Uri uri = item.getUri();

                if (uri != null) {
                    uris.add(uri);
                }

            }

            if (uris.isEmpty()) {
                pCallback.onError(PickSource.DOCUMENTS, pRequestType, ERR_DOCS_NULL_RESULT);
            } else {
                pCallback.onMultipleImagesPicked(pRequestType, uris);
            }

        } else {
            pCallback.onError(PickSource.DOCUMENTS, pRequestType, ERR_DOCS_NULL_RESULT);
        }

    } else {
        pCallback.onImagePicked(PickSource.DOCUMENTS, pRequestType, pictureUri);
    }

}
 
Example 11
private final void grantItemLocked(ClipData.Item item, int sourceUid, String targetPkg,
        int targetUserId) {
    if (item.getUri() != null) {
        grantUriLocked(item.getUri(), sourceUid, targetPkg, targetUserId);
    }
    Intent intent = item.getIntent();
    if (intent != null && intent.getData() != null) {
        grantUriLocked(intent.getData(), sourceUid, targetPkg, targetUserId);
    }
}
 
Example 12
Source Project: Android-Keyboard   File: ClipManager.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onPrimaryClipChanged() {
    if (mCbMan == null) {
        return;
    }
    ClipData cd = mCbMan.getPrimaryClip();
    if (cd != null && cd.getItemCount() > 0) {
        ClipData.Item item = cd.getItemAt(0);
        if (item != null) {
            CharSequence s = item.getText();
            addClipString(s);
        }
    }
}
 
Example 13
Source Project: Android-utils   File: L.java    License: Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private static void clipData2String(ClipData clipData, StringBuilder sb) {
    ClipData.Item item = clipData.getItemAt(0);
    if (item == null) {
        sb.append("ClipData.Item {}");
        return;
    }
    sb.append("ClipData.Item { ");
    String mHtmlText = item.getHtmlText();
    if (mHtmlText != null) {
        sb.append("H:");
        sb.append(mHtmlText);
        sb.append("}");
        return;
    }
    CharSequence mText = item.getText();
    if (mText != null) {
        sb.append("T:");
        sb.append(mText);
        sb.append("}");
        return;
    }
    Uri uri = item.getUri();
    if (uri != null) {
        sb.append("U:").append(uri);
        sb.append("}");
        return;
    }
    Intent intent = item.getIntent();
    if (intent != null) {
        sb.append("I:");
        sb.append(intent2String(intent));
        sb.append("}");
        return;
    }
    sb.append("NULL");
    sb.append("}");
}
 
Example 14
private boolean processDrop(View view, DragEvent event) {
    ClipData clipData = event.getClipData();
    if (clipData == null || clipData.getItemCount() == 0) {
        return false;
    }
    ClipData.Item item = clipData.getItemAt(0);
    if (item == null) {
        return false;
    }
    Uri uri = item.getUri();
    if (uri == null) {
        return false;
    }
    return setImageUri(view, event, uri);
}
 
Example 15
private ClipData createClipData(AlbumItem[] items) {
    String[] mimeTypes = new String[items.length];
    for (int i = 0; i < items.length; i++) {
        mimeTypes[i] = MediaType.getMimeType(this, items[i].getUri(this));
    }

    ClipData clipData =
            new ClipData("Images", mimeTypes,
                    new ClipData.Item(items[0].getUri(this)));
    for (int i = 1; i < items.length; i++) {
        clipData.addItem(new ClipData.Item(items[i].getUri(this)));
    }
    return clipData;
}
 
Example 16
@Override
public CharSequence getText() {
    if (clip.getPrimaryClip().getItemCount() >= 1) {
        ClipData.Item item = clip.getPrimaryClip().getItemAt(0);
        return item.getText();
    }
    return "";
}
 
Example 17
Source Project: clipboard   File: ClipboardModule.java    License: MIT License 5 votes vote down vote up
@ReactMethod
public void getString(Promise promise) {
  try {
    ClipboardManager clipboard = getClipboardService();
    ClipData clipData = clipboard.getPrimaryClip();
    if (clipData != null && clipData.getItemCount() >= 1) {
      ClipData.Item firstItem = clipboard.getPrimaryClip().getItemAt(0);
      promise.resolve("" + firstItem.getText());
    } else {
      promise.resolve("");
    }
  } catch (Exception e) {
    promise.reject(e);
  }
}
 
Example 18
Source Project: Spyglass   File: MentionsEditText.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Save the selected text and intent in ClipboardManager
 */
private void saveToClipboard(@NonNull CharSequence selectedText, @Nullable Intent intent) {
    ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData.Item item = new ClipData.Item(selectedText, intent, null);
    ClipData clip = new ClipData(null, new String[]{ClipDescription.MIMETYPE_TEXT_PLAIN}, item);
    clipboard.setPrimaryClip(clip);
}
 
Example 19
private void processDrop(DragEvent event) {
    final ClipData data = event.getClipData();
    final int N = data.getItemCount();
    for (int i = 0; i < N; i++) {
        ClipData.Item item = data.getItemAt(i);
        Log.i(TAG, "Dropped item " + i + " : " + item);
        if (mReportView != null) {
            String text = item.coerceToText(getContext()).toString();
            if (event.getLocalState() == (Object) this) {
                text += " : Dropped on self!";
            }
            mReportView.setText(text);
        }
    }
}
 
Example 20
@Override
protected void processLogic(Bundle savedInstanceState) {
    if (!EventBus.getDefault().isRegistered(this)) {
        EventBus.getDefault().register(this);
    }
    //初始化监听剪贴板
    final ClipboardManager manager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    hasPrimaryClip = new ClipboardManager.OnPrimaryClipChangedListener() {
        @Override
        public void onPrimaryClipChanged() {
            long now = System.currentTimeMillis();
            if (now - previousTime < 5000) {
                return;
            }
            previousTime = now;
            if (manager == null || !manager.hasPrimaryClip()) {
                return;
            }
            //如果是文本信息
            if (manager.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)
                    || manager.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_HTML)) {
                ClipData cdText = manager.getPrimaryClip();
                ClipData.Item item = cdText.getItemAt(0);
                //此处是TEXT文本信息
                if (item.getText() != null) {
                    final String t = item.getText().toString();
                    Snackbar.make(webViewT, "剪贴板被使用,是否撤销内容", Snackbar.LENGTH_LONG)
                            .setAction("撤销", new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Helper.copyToClipboard(getContext(), text);
                                }
                            }).addCallback(new Snackbar.Callback() {
                        @Override
                        public void onDismissed(Snackbar transientBottomBar, int event) {
                            text = t;
                            super.onDismissed(transientBottomBar, event);
                        }
                    }).show();
                }
            }
        }
    };
    if (manager != null) {
        manager.addPrimaryClipChangedListener(hasPrimaryClip);
    }
}