Java Code Examples for android.content.ClipData#Item

The following examples show how to use android.content.ClipData#Item . 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 check out the related API usage on the sidebar.
Example 1
Source File: PassWordInputLayout.java    From AndroidProjects with 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 2
Source File: StandaloneActivity.java    From FireFiles with 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 3
Source File: EditTextPlain.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
@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 4
Source File: ClipboardHelper.java    From ProjectX with 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 5
Source File: FileChooser.java    From AgentWeb with 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 6
Source File: ClipboardReadable.java    From Readily with 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 7
Source File: MainActivity.java    From user-interface-samples with Apache License 2.0 6 votes vote down vote up
@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 8
Source File: ImageDragListener.java    From android-DragAndDropAcrossApps with Apache License 2.0 5 votes vote down vote up
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 9
Source File: DraggableDot.java    From codeexamples-android with Eclipse Public License 1.0 5 votes vote down vote up
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 10
Source File: MentionsEditText.java    From Spyglass with 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 11
Source File: ClipboardModule.java    From clipboard with 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 12
Source File: ClipboardManagerCompatV11.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
@Override
public CharSequence getText() {
    if (clip.getPrimaryClip().getItemCount() >= 1) {
        ClipData.Item item = clip.getPrimaryClip().getItemAt(0);
        return item.getText();
    }
    return "";
}
 
Example 13
Source File: AlbumActivity.java    From Camera-Roll-Android-App with Apache License 2.0 5 votes vote down vote up
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 14
Source File: EditAreaView.java    From 920-text-editor-v2 with 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 15
Source File: L.java    From Android-utils with 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 16
Source File: ClipManager.java    From Android-Keyboard with 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 17
Source File: ClipboardService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
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 18
Source File: QiPick.java    From quickimagepick with 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 19
Source File: ClipboardManagerImpl11.java    From timecat with 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 20
Source File: WebViewActivity.java    From movienow with GNU General Public License v3.0 4 votes vote down vote up
@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);
    }
}