Java Code Examples for android.content.ClipDescription#hasMimeType()

The following examples show how to use android.content.ClipDescription#hasMimeType() . 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: ClipboardManagerUtil.java    From PowerFileExplorer with GNU General Public License v3.0 6 votes vote down vote up
public static CharSequence getText() {
    android.text.ClipboardManager clipboardManager = ServiceUtil.getClipboardManager();
    if (APILevel.require(11)) {
        ClipboardManager cm = (ClipboardManager) clipboardManager;
        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
            return null;
    } else {
        return clipboardManager.getText();
    }
}
 
Example 2
Source File: ClipboardUtils.java    From XposedSmsCode with GNU General Public License v3.0 6 votes vote down vote up
public static void clearClipboard(Context context) {
    ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (cm == null) {
        XLog.e("Clear failed, clipboard manager is null");
        return;
    }
    if(cm.hasPrimaryClip()) {
        ClipDescription cd = cm.getPrimaryClipDescription();
        if (cd != null) {
            if (cd.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                cm.setPrimaryClip(ClipData.newPlainText("Copy text", ""));
                XLog.i("Clear clipboard succeed");
            }
        }
    }
}
 
Example 3
Source File: VerifyActivity.java    From Conversations with GNU General Public License v3.0 6 votes vote down vote up
private void pastePinFromClipboard() {
    final ClipDescription description = clipboardManager != null ? clipboardManager.getPrimaryClipDescription() : null;
    if (description != null && description.hasMimeType(MIMETYPE_TEXT_PLAIN)) {
        final ClipData primaryClip = clipboardManager.getPrimaryClip();
        if (primaryClip != null && primaryClip.getItemCount() > 0) {
            final CharSequence clip = primaryClip.getItemAt(0).getText();
            if (PinEntryWrapper.isValidPin(clip) && !clip.toString().equals(this.pasted)) {
                this.pasted = clip.toString();
                pinEntryWrapper.setPin(clip.toString());
                final Snackbar snackbar = Snackbar.make(binding.coordinator, R.string.possible_pin, Snackbar.LENGTH_LONG);
                snackbar.setAction(R.string.undo, v -> pinEntryWrapper.clear());
                snackbar.show();
            }
        }
    }
}
 
Example 4
Source File: ClipboardUtils.java    From AdBlockedWebView-Android with MIT License 6 votes vote down vote up
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 {
            return "";
        }
    } else {
        //noinspection deprecation
        return cm.getText();
    }
}
 
Example 5
Source File: SelectionPopupController.java    From 365browser with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
public boolean canPasteAsPlainText() {
    // String resource "paste_as_plain_text" only exist in O.
    // Also this is an O feature, we need to make it consistant with TextView.
    if (!BuildInfo.isAtLeastO()) return false;
    if (!mCanEditRichlyForPastePopup) return false;
    ClipboardManager clipMgr =
            (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
    if (!clipMgr.hasPrimaryClip()) return false;

    ClipData clipData = clipMgr.getPrimaryClip();
    ClipDescription description = clipData.getDescription();
    CharSequence text = clipData.getItemAt(0).getText();
    boolean isPlainType = description.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
    // On Android, Spanned could be copied to Clipboard as plain_text MIME type, but in some
    // cases, Spanned could have text format, we need to show "paste as plain text" when
    // that happens.
    if (isPlainType && (text instanceof Spanned)) {
        Spanned spanned = (Spanned) text;
        if (hasStyleSpan(spanned)) return true;
    }
    return description.hasMimeType(ClipDescription.MIMETYPE_TEXT_HTML);
}
 
Example 6
Source File: Clipboard.java    From 365browser with Apache License 2.0 6 votes vote down vote up
public String clipDataToHtmlText(ClipData clipData) {
    ClipDescription description = clipData.getDescription();
    if (description.hasMimeType(ClipDescription.MIMETYPE_TEXT_HTML)) {
        return clipData.getItemAt(0).getHtmlText();
    }

    if (description.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
        CharSequence text = clipData.getItemAt(0).getText();
        if (!(text instanceof Spanned)) return null;
        Spanned spanned = (Spanned) text;
        if (hasStyleSpan(spanned)) {
            return ApiCompatibilityUtils.toHtml(
                    spanned, Html.TO_HTML_PARAGRAPH_LINES_CONSECUTIVE);
        }
    }
    return null;
}
 
Example 7
Source File: ContentFragment.java    From codeexamples-android with Eclipse Public License 1.0 5 votes vote down vote up
boolean processDragStarted(DragEvent event) {
    // Determine whether to continue processing drag and drop based on the
    // plain text mime type.
    ClipDescription clipDesc = event.getClipDescription();
    if (clipDesc != null) {
        return clipDesc.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
    }
    return false;
}
 
Example 8
Source File: RemoteInput.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private static Intent getClipDataIntentFromIntent(Intent intent) {
    ClipData clipData = intent.getClipData();
    if (clipData == null) {
        return null;
    }
    ClipDescription clipDescription = clipData.getDescription();
    if (!clipDescription.hasMimeType(ClipDescription.MIMETYPE_TEXT_INTENT)) {
        return null;
    }
    if (!clipDescription.getLabel().equals(RESULTS_CLIP_LABEL)) {
        return null;
    }
    return clipData.getItemAt(0).getIntent();
}
 
Example 9
Source File: ContentFragment.java    From codeexamples-android with Eclipse Public License 1.0 5 votes vote down vote up
boolean processDragStarted(DragEvent event) {
    // Determine whether to continue processing drag and drop based on the
    // plain text mime type.
    ClipDescription clipDesc = event.getClipDescription();
    if (clipDesc != null) {
        return clipDesc.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
    }
    return false;
}
 
Example 10
Source File: ContentFragment.java    From androidtestdebug with MIT License 5 votes vote down vote up
boolean processDragStarted(DragEvent event) {
    // Determine whether to continue processing drag and drop based on the
    // plain text mime type.
    ClipDescription clipDesc = event.getClipDescription();
    if (clipDesc != null) {
        return clipDesc.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
    }
    return false;
}
 
Example 11
Source File: ContentFragment.java    From androidtestdebug with MIT License 5 votes vote down vote up
boolean processDragStarted(DragEvent event) {
    // Determine whether to continue processing drag and drop based on the
    // plain text mime type.
    ClipDescription clipDesc = event.getClipDescription();
    if (clipDesc != null) {
        return clipDesc.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
    }
    return false;
}
 
Example 12
Source File: ContentFragment.java    From androidtestdebug with MIT License 5 votes vote down vote up
boolean processDragStarted(DragEvent event) {
    // Determine whether to continue processing drag and drop based on the
    // plain text mime type.
    ClipDescription clipDesc = event.getClipDescription();
    if (clipDesc != null) {
        return clipDesc.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
    }
    return false;
}
 
Example 13
Source File: RemoteInputCompatJellybean.java    From adt-leanback-support with Apache License 2.0 5 votes vote down vote up
static Bundle getResultsFromIntent(Intent intent) {
    ClipData clipData = intent.getClipData();
    if (clipData == null) {
        return null;
    }
    ClipDescription clipDescription = clipData.getDescription();
    if (!clipDescription.hasMimeType(ClipDescription.MIMETYPE_TEXT_INTENT)) {
        return null;
    }
    if (clipDescription.getLabel().equals(RESULTS_CLIP_LABEL)) {
        return clipData.getItemAt(0).getIntent().getExtras().getParcelable(EXTRA_RESULTS_DATA);
    }
    return null;
}
 
Example 14
Source File: ClipboardUtils.java    From AdBlockedWebView-Android with MIT License 5 votes vote down vote up
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 {
        //noinspection deprecation
        return cm.hasText();
    }
}
 
Example 15
Source File: DropTargetFragment.java    From android-DragAndDropAcrossApps with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onDrag(View view, DragEvent event) {
    // DragTarget is peeking into the MIME types of the dragged event in order to ignore
    // non-image drags completely.
    // DragSource does not do that but rejects non-image content once a drop has happened.
    ClipDescription clipDescription = event.getClipDescription();
    if (clipDescription != null && !clipDescription.hasMimeType("image/*")) {
        return false;
    }
    // Callback received when image is being dragged.
    return super.onDrag(view, event);
}
 
Example 16
Source File: ClipboardUtils.java    From SmsCode with GNU General Public License v3.0 5 votes vote down vote up
public static void clearClipboard(Context context) {
    ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (cm == null) {
        return;
    }
    if (cm.hasPrimaryClip()) {
        ClipDescription cd = cm.getPrimaryClipDescription();
        if (cd != null) {
            if (cd.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                cm.setPrimaryClip(ClipData.newPlainText("Copy text", ""));
            }
        }
    }
}
 
Example 17
Source File: ClipboardManagerUtil.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
public static boolean hasText() {
    android.text.ClipboardManager clipboardManager = ServiceUtil.getClipboardManager();
    if (APILevel.require(11)) {
        ClipboardManager cm = (ClipboardManager) clipboardManager;
        ClipDescription description = cm.getPrimaryClipDescription();
        ClipData clipData = cm.getPrimaryClip();
        return clipData != null
                && description != null
                && (description.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN));
    } else {
        return clipboardManager.hasText();
    }
}
 
Example 18
Source File: DropTargetFragment.java    From user-interface-samples with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onDrag(View view, DragEvent event) {
    // DragTarget is peeking into the MIME types of the dragged event in order to ignore
    // non-image drags completely.
    // DragSource does not do that but rejects non-image content once a drop has happened.
    ClipDescription clipDescription = event.getClipDescription();
    if (clipDescription != null && !clipDescription.hasMimeType("image/*")) {
        return false;
    }
    // Callback received when image is being dragged.
    return super.onDrag(view, event);
}
 
Example 19
Source File: PinItemDragListener.java    From LaunchEnr with GNU General Public License v3.0 4 votes vote down vote up
private boolean onDragStart(DragEvent event) {
    if (!mRequest.isValid()) {
        return false;
    }
    ClipDescription desc =  event.getClipDescription();
    if (desc == null || !desc.hasMimeType(getMimeType())) {
        return false;
    }

    final PendingAddItemInfo item;
    if (mRequest.getRequestType() == PinItemRequestCompat.REQUEST_TYPE_SHORTCUT) {
        item = new PendingAddShortcutInfo(
                new PinShortcutRequestActivityInfo(mRequest, mLauncher));
    } else {
        // mRequest.getRequestType() == PinItemRequestCompat.REQUEST_TYPE_APPWIDGET
        LauncherAppWidgetProviderInfo providerInfo =
                LauncherAppWidgetProviderInfo.fromProviderInfo(
                        mLauncher, mRequest.getAppWidgetProviderInfo(mLauncher));
        final PinWidgetFlowHandler flowHandler =
                new PinWidgetFlowHandler(providerInfo, mRequest);
        item = new PendingAddWidgetInfo(providerInfo) {
            @Override
            public WidgetAddFlowHandler getHandler() {
                return flowHandler;
            }
        };
    }
    View view = new View(mLauncher);
    view.setTag(item);

    Point downPos = new Point((int) event.getX(), (int) event.getY());
    DragOptions options = new DragOptions();
    options.systemDndStartPoint = downPos;
    options.preDragCondition = this;

    // We use drag event position as the screenPos for the preview image. Since mPreviewRect
    // already includes the view position relative to the drag event on the source window,
    // and the absolute position (position relative to the screen) of drag event is same
    // across windows, using drag position here give a good estimate for relative position
    // to source window.
    PendingItemDragHelper dragHelper = new PendingItemDragHelper(view);
    if (mRequest.getRequestType() == PinItemRequestCompat.REQUEST_TYPE_APPWIDGET) {
        dragHelper.setPreview(getPreview(mRequest));
    }

    dragHelper.startDrag(new Rect(mPreviewRect),
            mPreviewBitmapWidth, mPreviewViewWidth, downPos,  this, options);
    mDragStartTime = SystemClock.uptimeMillis();
    return true;
}