@ TargetApi ( Build . VERSION_CODES . HONEYCOMB ) private void pasteHoneycombImpl ( @ IntRange ( from = 0 ) int min , @ IntRange ( from = 0 ) int max ) { ClipboardManager clipboard = ( ClipboardManager ) getContext ( ) . getSystemService ( Context . CLIPBOARD_SERVICE ) ; ClipData clip = clipboard . getPrimaryClip ( ) ; if ( clip != null ) { for ( int i = 0 ; i < clip . getItemCount ( ) ; i ++ ) { ClipData . Item item = clip . getItemAt ( i ) ; String selectedText = item . coerceToText ( getContext ( ) ) . toString ( ) ; MentionsEditable text = getMentionsText ( ) ; MentionSpan [ ] spans = text . getSpans ( min , max , MentionSpan . class ) ; /* * We need to remove the span between min and max. This is required because in * {@link SpannableStringBuilder#replace(int, int, CharSequence)} existing spans within * the Editable that entirely cover the replaced range are retained, but any that * were strictly within the range that was replaced are removed. In our case the existing * spans are retained if the selection entirely covers the span. So, we just remove * the existing span and replace the new text with that span. */ for ( MentionSpan span : spans ) { if ( text . getSpanEnd ( span ) == min ) { // We do not want to remove the span, when we want to paste anything just next // to the existing span. In this case "text.getSpanEnd(span)" will be equal // to min. continue ; } text . removeSpan ( span ) ; } Intent intent = item . getIntent ( ) ; // Just set the plain text if we do not have mentions data in the intent/bundle if ( intent == null ) { text . replace ( min , max , selectedText ) ; continue ; } Bundle bundle = intent . getExtras ( ) ; if ( bundle == null ) { text . replace ( min , max , selectedText ) ; continue ; } bundle . setClassLoader ( getContext ( ) . getClassLoader ( ) ) ; int [ ] spanStart = bundle . getIntArray ( KEY_MENTION_SPAN_STARTS ) ; Parcelable [ ] parcelables = bundle . getParcelableArray ( KEY_MENTION_SPANS ) ; if ( parcelables == null || parcelables . length <= 0 || spanStart == null || spanStart . length <= 0 ) { text . replace ( min , max , selectedText ) ; continue ; } // Set the MentionSpan in text. SpannableStringBuilder s = new SpannableStringBuilder ( selectedText ) ; for ( int j = 0 ; j < parcelables . length ; j ++ ) { MentionSpan mentionSpan = ( MentionSpan ) parcelables [ j ] ; s . setSpan ( mentionSpan , spanStart [ j ] , spanStart [ j ] + mentionSpan . getDisplayString ( ) . length ( ) , Spanned . SPAN_EXCLUSIVE_EXCLUSIVE ) ; } text . replace ( min , max , s ) ; } } }
Paste clipboard content between min and max positions . This method is supported for all the api above the 10 . If clipboard content contain the MentionSpan set the span in copied text .