use of android.content.ClipData in project platform_frameworks_base by android.
the class TextView method paste.
/**
* Paste clipboard content between min and max positions.
*/
private void paste(int min, int max, boolean withFormatting) {
ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = clipboard.getPrimaryClip();
if (clip != null) {
boolean didFirst = false;
for (int i = 0; i < clip.getItemCount(); i++) {
final CharSequence paste;
if (withFormatting) {
paste = clip.getItemAt(i).coerceToStyledText(getContext());
} else {
// Get an item as text and remove all spans by toString().
final CharSequence text = clip.getItemAt(i).coerceToText(getContext());
paste = (text instanceof Spanned) ? text.toString() : text;
}
if (paste != null) {
if (!didFirst) {
Selection.setSelection((Spannable) mText, max);
((Editable) mText).replace(min, max, paste);
didFirst = true;
} else {
((Editable) mText).insert(getSelectionEnd(), "\n");
((Editable) mText).insert(getSelectionEnd(), paste);
}
}
}
sLastCutCopyOrTextChangedTime = 0;
}
}
use of android.content.ClipData in project apps-android-wikipedia by wikimedia.
the class PlainPasteEditText method onTextContextMenuPaste.
private boolean onTextContextMenuPaste() {
// Do not allow pasting of formatted text!
// We do this by intercepting the clipboard and temporarily replacing its
// contents with plain text.
ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
if (clipboard.hasPrimaryClip()) {
ClipData oldClipData = clipboard.getPrimaryClip();
String lastClipText = oldClipData.getItemAt(oldClipData.getItemCount() - 1).coerceToText(getContext()).toString();
// temporarily set the new clip data as the primary
ClipboardUtil.setPlainText(getContext(), null, lastClipText);
// execute the paste!
super.onTextContextMenuItem(android.R.id.paste);
// restore the clip data back to the old one.
clipboard.setPrimaryClip(oldClipData);
}
return true;
}
use of android.content.ClipData in project syncthing-android by syncthing.
the class Util method copyDeviceId.
/**
* Copies the given device ID to the clipboard (and shows a Toast telling about it).
*
* @param id The device ID to copy.
*/
public static void copyDeviceId(Context context, String id) {
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(context.getString(R.string.device_id), id);
clipboard.setPrimaryClip(clip);
Toast.makeText(context, R.string.device_id_copied_to_clipboard, Toast.LENGTH_SHORT).show();
}
use of android.content.ClipData in project android by nextcloud.
the class CopyToClipboardActivity method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
// get the clipboard system service
ClipboardManager clipboardManager = (ClipboardManager) this.getSystemService(CLIPBOARD_SERVICE);
// get the text to copy into the clipboard
Intent intent = getIntent();
CharSequence text = intent.getCharSequenceExtra(Intent.EXTRA_TEXT);
if (text != null && text.length() > 0) {
// minimum API level >= 11 -> only modern Clipboard
ClipData clip = ClipData.newPlainText(getString(R.string.clipboard_label, getString(R.string.app_name)), text);
clipboardManager.setPrimaryClip(clip);
// API level < 11 -> legacy Clipboard - NOT SUPPORTED ANYMORE
// clipboardManager.setText(text);
// alert the user that the text is in the clipboard and we're done
Toast.makeText(this, R.string.clipboard_text_copied, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, R.string.clipboard_no_text_to_copy, Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(this, R.string.clipboard_uxexpected_error, Toast.LENGTH_SHORT).show();
Log_OC.e(TAG, "Exception caught while copying to clipboard", e);
}
finish();
}
use of android.content.ClipData in project android_frameworks_base by crdroidandroid.
the class TextView method paste.
/**
* Paste clipboard content between min and max positions.
*/
private void paste(int min, int max, boolean withFormatting) {
ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = clipboard.getPrimaryClip();
if (clip != null) {
boolean didFirst = false;
for (int i = 0; i < clip.getItemCount(); i++) {
final CharSequence paste;
if (withFormatting) {
paste = clip.getItemAt(i).coerceToStyledText(getContext());
} else {
// Get an item as text and remove all spans by toString().
final CharSequence text = clip.getItemAt(i).coerceToText(getContext());
paste = (text instanceof Spanned) ? text.toString() : text;
}
if (paste != null) {
if (!didFirst) {
Selection.setSelection((Spannable) mText, max);
((Editable) mText).replace(min, max, paste);
didFirst = true;
} else {
((Editable) mText).insert(getSelectionEnd(), "\n");
((Editable) mText).insert(getSelectionEnd(), paste);
}
}
}
sLastCutCopyOrTextChangedTime = 0;
}
}
Aggregations