use of android.content.ClipData.Item in project XobotOS by xamarin.
the class TextView method onTextContextMenuItem.
/**
* Called when a context menu option for the text view is selected. Currently
* this will be {@link android.R.id#copyUrl}, {@link android.R.id#selectTextMode},
* {@link android.R.id#selectAll}, {@link android.R.id#paste}, {@link android.R.id#cut}
* or {@link android.R.id#copy}.
*
* @return true if the context menu item action was performed.
*/
public boolean onTextContextMenuItem(int id) {
int min = 0;
int max = mText.length();
if (isFocused()) {
final int selStart = getSelectionStart();
final int selEnd = getSelectionEnd();
min = Math.max(0, Math.min(selStart, selEnd));
max = Math.max(0, Math.max(selStart, selEnd));
}
switch(id) {
case ID_COPY_URL:
URLSpan[] urls = ((Spanned) mText).getSpans(min, max, URLSpan.class);
if (urls.length >= 1) {
ClipData clip = null;
for (int i = 0; i < urls.length; i++) {
Uri uri = Uri.parse(urls[0].getURL());
if (clip == null) {
clip = ClipData.newRawUri(null, uri);
} else {
clip.addItem(new ClipData.Item(uri));
}
}
if (clip != null) {
setPrimaryClip(clip);
}
}
stopSelectionActionMode();
return true;
case ID_SELECTION_MODE:
if (mSelectionActionMode != null) {
// Selection mode is already started, simply change selected part.
selectCurrentWord();
} else {
startSelectionActionMode();
}
return true;
case ID_SELECT_ALL:
// This does not enter text selection mode. Text is highlighted, so that it can be
// bulk edited, like selectAllOnFocus does. Returns true even if text is empty.
selectAll();
return true;
case ID_PASTE:
paste(min, max);
return true;
case ID_CUT:
setPrimaryClip(ClipData.newPlainText(null, getTransformedText(min, max)));
((Editable) mText).delete(min, max);
stopSelectionActionMode();
return true;
case ID_COPY:
setPrimaryClip(ClipData.newPlainText(null, getTransformedText(min, max)));
stopSelectionActionMode();
return true;
}
return false;
}
use of android.content.ClipData.Item in project XobotOS by xamarin.
the class TextView method onDrop.
private void onDrop(DragEvent event) {
StringBuilder content = new StringBuilder("");
ClipData clipData = event.getClipData();
final int itemCount = clipData.getItemCount();
for (int i = 0; i < itemCount; i++) {
Item item = clipData.getItemAt(i);
content.append(item.coerceToText(TextView.this.mContext));
}
final int offset = getOffsetForPosition(event.getX(), event.getY());
Object localState = event.getLocalState();
DragLocalState dragLocalState = null;
if (localState instanceof DragLocalState) {
dragLocalState = (DragLocalState) localState;
}
boolean dragDropIntoItself = dragLocalState != null && dragLocalState.sourceTextView == this;
if (dragDropIntoItself) {
if (offset >= dragLocalState.start && offset < dragLocalState.end) {
// A drop inside the original selection discards the drop.
return;
}
}
final int originalLength = mText.length();
long minMax = prepareSpacesAroundPaste(offset, offset, content);
int min = extractRangeStartFromLong(minMax);
int max = extractRangeEndFromLong(minMax);
Selection.setSelection((Spannable) mText, max);
((Editable) mText).replace(min, max, content);
if (dragDropIntoItself) {
int dragSourceStart = dragLocalState.start;
int dragSourceEnd = dragLocalState.end;
if (max <= dragSourceStart) {
// Inserting text before selection has shifted positions
final int shift = mText.length() - originalLength;
dragSourceStart += shift;
dragSourceEnd += shift;
}
// Delete original selection
((Editable) mText).delete(dragSourceStart, dragSourceEnd);
// Make sure we do not leave two adjacent spaces.
if ((dragSourceStart == 0 || Character.isSpaceChar(mTransformed.charAt(dragSourceStart - 1))) && (dragSourceStart == mText.length() || Character.isSpaceChar(mTransformed.charAt(dragSourceStart)))) {
final int pos = dragSourceStart == mText.length() ? dragSourceStart - 1 : dragSourceStart;
((Editable) mText).delete(pos, pos + 1);
}
}
}
use of android.content.ClipData.Item in project AisenWeiBo by wangdan.
the class APublishFragment method getPictureFromClipbroad.
/**
* 从剪切板获取链接
*
*/
private void getPictureFromClipbroad() {
ClipboardManager cmb = (ClipboardManager) GlobalContext.getInstance().getSystemService(Context.CLIPBOARD_SERVICE);
ClipData data = cmb.getPrimaryClip();
if (data != null && data.getItemCount() > 0) {
Item item = data.getItemAt(0);
CharSequence image = item.getText();
if (!TextUtils.isEmpty(image)) {
if (image.toString().toLowerCase().endsWith(".gif") || image.toString().toLowerCase().endsWith(".jpg") || image.toString().toLowerCase().endsWith(".jpeg") || image.toString().toLowerCase().endsWith(".bmp") || image.toString().toLowerCase().endsWith(".png")) {
getPublishBean().getParams().addParameter("url", image.toString());
refreshUI();
} else {
showMessage(R.string.publish_clip_faild);
}
} else {
showMessage(R.string.publish_clip_empty);
}
} else {
showMessage(R.string.publish_clip_empty);
}
}
use of android.content.ClipData.Item in project android_frameworks_base by ResurrectionRemix.
the class Editor method onDrop.
void onDrop(DragEvent event) {
StringBuilder content = new StringBuilder("");
final DragAndDropPermissions permissions = DragAndDropPermissions.obtain(event);
if (permissions != null) {
permissions.takeTransient();
}
try {
ClipData clipData = event.getClipData();
final int itemCount = clipData.getItemCount();
for (int i = 0; i < itemCount; i++) {
Item item = clipData.getItemAt(i);
content.append(item.coerceToStyledText(mTextView.getContext()));
}
} finally {
if (permissions != null) {
permissions.release();
}
}
final int offset = mTextView.getOffsetForPosition(event.getX(), event.getY());
Object localState = event.getLocalState();
DragLocalState dragLocalState = null;
if (localState instanceof DragLocalState) {
dragLocalState = (DragLocalState) localState;
}
boolean dragDropIntoItself = dragLocalState != null && dragLocalState.sourceTextView == mTextView;
if (dragDropIntoItself) {
if (offset >= dragLocalState.start && offset < dragLocalState.end) {
// A drop inside the original selection discards the drop.
return;
}
}
final int originalLength = mTextView.getText().length();
int min = offset;
int max = offset;
Selection.setSelection((Spannable) mTextView.getText(), max);
mTextView.replaceText_internal(min, max, content);
if (dragDropIntoItself) {
int dragSourceStart = dragLocalState.start;
int dragSourceEnd = dragLocalState.end;
if (max <= dragSourceStart) {
// Inserting text before selection has shifted positions
final int shift = mTextView.getText().length() - originalLength;
dragSourceStart += shift;
dragSourceEnd += shift;
}
mUndoInputFilter.setForceMerge(true);
try {
// Delete original selection
mTextView.deleteText_internal(dragSourceStart, dragSourceEnd);
// Make sure we do not leave two adjacent spaces.
final int prevCharIdx = Math.max(0, dragSourceStart - 1);
final int nextCharIdx = Math.min(mTextView.getText().length(), dragSourceStart + 1);
if (nextCharIdx > prevCharIdx + 1) {
CharSequence t = mTextView.getTransformedText(prevCharIdx, nextCharIdx);
if (Character.isSpaceChar(t.charAt(0)) && Character.isSpaceChar(t.charAt(1))) {
mTextView.deleteText_internal(prevCharIdx, prevCharIdx + 1);
}
}
} finally {
mUndoInputFilter.setForceMerge(false);
}
}
}
Aggregations