ClipData.java revision a14acd20b8d563319ea1a5974dca0e9a29f0aaef
1/** 2 * Copyright (c) 2010, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.content; 18 19import android.content.res.AssetFileDescriptor; 20import android.graphics.Bitmap; 21import android.net.Uri; 22import android.os.Parcel; 23import android.os.Parcelable; 24import android.os.StrictMode; 25import android.text.Html; 26import android.text.Spannable; 27import android.text.SpannableStringBuilder; 28import android.text.Spanned; 29import android.text.TextUtils; 30import android.text.style.URLSpan; 31import android.util.Log; 32 33import java.io.FileInputStream; 34import java.io.FileNotFoundException; 35import java.io.IOException; 36import java.io.InputStreamReader; 37import java.util.ArrayList; 38 39/** 40 * Representation of a clipped data on the clipboard. 41 * 42 * <p>ClippedData is a complex type containing one or Item instances, 43 * each of which can hold one or more representations of an item of data. 44 * For display to the user, it also has a label and iconic representation.</p> 45 * 46 * <p>A ClipData contains a {@link ClipDescription}, which describes 47 * important meta-data about the clip. In particular, its 48 * {@link ClipDescription#getMimeType(int) getDescription().getMimeType(int)} 49 * must return correct MIME type(s) describing the data in the clip. For help 50 * in correctly constructing a clip with the correct MIME type, use 51 * {@link #newPlainText(CharSequence, CharSequence)}, 52 * {@link #newUri(ContentResolver, CharSequence, Uri)}, and 53 * {@link #newIntent(CharSequence, Intent)}. 54 * 55 * <p>Each Item instance can be one of three main classes of data: a simple 56 * CharSequence of text, a single Intent object, or a Uri. See {@link Item} 57 * for more details. 58 * 59 * <div class="special reference"> 60 * <h3>Developer Guides</h3> 61 * <p>For more information about using the clipboard framework, read the 62 * <a href="{@docRoot}guide/topics/clipboard/copy-paste.html">Copy and Paste</a> 63 * developer guide.</p> 64 * </div> 65 * 66 * <a name="ImplementingPaste"></a> 67 * <h3>Implementing Paste or Drop</h3> 68 * 69 * <p>To implement a paste or drop of a ClippedData object into an application, 70 * the application must correctly interpret the data for its use. If the {@link Item} 71 * it contains is simple text or an Intent, there is little to be done: text 72 * can only be interpreted as text, and an Intent will typically be used for 73 * creating shortcuts (such as placing icons on the home screen) or other 74 * actions. 75 * 76 * <p>If all you want is the textual representation of the clipped data, you 77 * can use the convenience method {@link Item#coerceToText Item.coerceToText}. 78 * In this case there is generally no need to worry about the MIME types 79 * reported by {@link ClipDescription#getMimeType(int) getDescription().getMimeType(int)}, 80 * since any clip item an always be converted to a string. 81 * 82 * <p>More complicated exchanges will be done through URIs, in particular 83 * "content:" URIs. A content URI allows the recipient of a ClippedData item 84 * to interact closely with the ContentProvider holding the data in order to 85 * negotiate the transfer of that data. The clip must also be filled in with 86 * the available MIME types; {@link #newUri(ContentResolver, CharSequence, Uri)} 87 * will take care of correctly doing this. 88 * 89 * <p>For example, here is the paste function of a simple NotePad application. 90 * When retrieving the data from the clipboard, it can do either two things: 91 * if the clipboard contains a URI reference to an existing note, it copies 92 * the entire structure of the note into a new note; otherwise, it simply 93 * coerces the clip into text and uses that as the new note's contents. 94 * 95 * {@sample development/samples/NotePad/src/com/example/android/notepad/NoteEditor.java 96 * paste} 97 * 98 * <p>In many cases an application can paste various types of streams of data. For 99 * example, an e-mail application may want to allow the user to paste an image 100 * or other binary data as an attachment. This is accomplished through the 101 * ContentResolver {@link ContentResolver#getStreamTypes(Uri, String)} and 102 * {@link ContentResolver#openTypedAssetFileDescriptor(Uri, String, android.os.Bundle)} 103 * methods. These allow a client to discover the type(s) of data that a particular 104 * content URI can make available as a stream and retrieve the stream of data. 105 * 106 * <p>For example, the implementation of {@link Item#coerceToText Item.coerceToText} 107 * itself uses this to try to retrieve a URI clip as a stream of text: 108 * 109 * {@sample frameworks/base/core/java/android/content/ClipData.java coerceToText} 110 * 111 * <a name="ImplementingCopy"></a> 112 * <h3>Implementing Copy or Drag</h3> 113 * 114 * <p>To be the source of a clip, the application must construct a ClippedData 115 * object that any recipient can interpret best for their context. If the clip 116 * is to contain a simple text, Intent, or URI, this is easy: an {@link Item} 117 * containing the appropriate data type can be constructed and used. 118 * 119 * <p>More complicated data types require the implementation of support in 120 * a ContentProvider for describing and generating the data for the recipient. 121 * A common scenario is one where an application places on the clipboard the 122 * content: URI of an object that the user has copied, with the data at that 123 * URI consisting of a complicated structure that only other applications with 124 * direct knowledge of the structure can use. 125 * 126 * <p>For applications that do not have intrinsic knowledge of the data structure, 127 * the content provider holding it can make the data available as an arbitrary 128 * number of types of data streams. This is done by implementing the 129 * ContentProvider {@link ContentProvider#getStreamTypes(Uri, String)} and 130 * {@link ContentProvider#openTypedAssetFile(Uri, String, android.os.Bundle)} 131 * methods. 132 * 133 * <p>Going back to our simple NotePad application, this is the implementation 134 * it may have to convert a single note URI (consisting of a title and the note 135 * text) into a stream of plain text data. 136 * 137 * {@sample development/samples/NotePad/src/com/example/android/notepad/NotePadProvider.java 138 * stream} 139 * 140 * <p>The copy operation in our NotePad application is now just a simple matter 141 * of making a clip containing the URI of the note being copied: 142 * 143 * {@sample development/samples/NotePad/src/com/example/android/notepad/NotesList.java 144 * copy} 145 * 146 * <p>Note if a paste operation needs this clip as text (for example to paste 147 * into an editor), then {@link Item#coerceToText(Context)} will ask the content 148 * provider for the clip URI as text and successfully paste the entire note. 149 */ 150public class ClipData implements Parcelable { 151 static final String[] MIMETYPES_TEXT_PLAIN = new String[] { 152 ClipDescription.MIMETYPE_TEXT_PLAIN }; 153 static final String[] MIMETYPES_TEXT_HTML = new String[] { 154 ClipDescription.MIMETYPE_TEXT_HTML }; 155 static final String[] MIMETYPES_TEXT_URILIST = new String[] { 156 ClipDescription.MIMETYPE_TEXT_URILIST }; 157 static final String[] MIMETYPES_TEXT_INTENT = new String[] { 158 ClipDescription.MIMETYPE_TEXT_INTENT }; 159 160 final ClipDescription mClipDescription; 161 162 final Bitmap mIcon; 163 164 final ArrayList<Item> mItems; 165 166 /** 167 * Description of a single item in a ClippedData. 168 * 169 * <p>The types than an individual item can currently contain are:</p> 170 * 171 * <ul> 172 * <li> Text: a basic string of text. This is actually a CharSequence, 173 * so it can be formatted text supported by corresponding Android built-in 174 * style spans. (Custom application spans are not supported and will be 175 * stripped when transporting through the clipboard.) 176 * <li> Intent: an arbitrary Intent object. A typical use is the shortcut 177 * to create when pasting a clipped item on to the home screen. 178 * <li> Uri: a URI reference. This may be any URI (such as an http: URI 179 * representing a bookmark), however it is often a content: URI. Using 180 * content provider references as clips like this allows an application to 181 * share complex or large clips through the standard content provider 182 * facilities. 183 * </ul> 184 */ 185 public static class Item { 186 final CharSequence mText; 187 final String mHtmlText; 188 final Intent mIntent; 189 final Uri mUri; 190 191 /** 192 * Create an Item consisting of a single block of (possibly styled) text. 193 */ 194 public Item(CharSequence text) { 195 mText = text; 196 mHtmlText = null; 197 mIntent = null; 198 mUri = null; 199 } 200 201 /** 202 * Create an Item consisting of a single block of (possibly styled) text, 203 * with an alternative HTML formatted representation. You <em>must</em> 204 * supply a plain text representation in addition to HTML text; coercion 205 * will not be done from HTML formated text into plain text. 206 */ 207 public Item(CharSequence text, String htmlText) { 208 mText = text; 209 mHtmlText = htmlText; 210 mIntent = null; 211 mUri = null; 212 } 213 214 /** 215 * Create an Item consisting of an arbitrary Intent. 216 */ 217 public Item(Intent intent) { 218 mText = null; 219 mHtmlText = null; 220 mIntent = intent; 221 mUri = null; 222 } 223 224 /** 225 * Create an Item consisting of an arbitrary URI. 226 */ 227 public Item(Uri uri) { 228 mText = null; 229 mHtmlText = null; 230 mIntent = null; 231 mUri = uri; 232 } 233 234 /** 235 * Create a complex Item, containing multiple representations of 236 * text, Intent, and/or URI. 237 */ 238 public Item(CharSequence text, Intent intent, Uri uri) { 239 mText = text; 240 mHtmlText = null; 241 mIntent = intent; 242 mUri = uri; 243 } 244 245 /** 246 * Create a complex Item, containing multiple representations of 247 * text, HTML text, Intent, and/or URI. If providing HTML text, you 248 * <em>must</em> supply a plain text representation as well; coercion 249 * will not be done from HTML formated text into plain text. 250 */ 251 public Item(CharSequence text, String htmlText, Intent intent, Uri uri) { 252 if (htmlText != null && text == null) { 253 throw new IllegalArgumentException( 254 "Plain text must be supplied if HTML text is supplied"); 255 } 256 mText = text; 257 mHtmlText = htmlText; 258 mIntent = intent; 259 mUri = uri; 260 } 261 262 /** 263 * Retrieve the raw text contained in this Item. 264 */ 265 public CharSequence getText() { 266 return mText; 267 } 268 269 /** 270 * Retrieve the raw HTML text contained in this Item. 271 */ 272 public String getHtmlText() { 273 return mHtmlText; 274 } 275 276 /** 277 * Retrieve the raw Intent contained in this Item. 278 */ 279 public Intent getIntent() { 280 return mIntent; 281 } 282 283 /** 284 * Retrieve the raw URI contained in this Item. 285 */ 286 public Uri getUri() { 287 return mUri; 288 } 289 290 /** 291 * Turn this item into text, regardless of the type of data it 292 * actually contains. 293 * 294 * <p>The algorithm for deciding what text to return is: 295 * <ul> 296 * <li> If {@link #getText} is non-null, return that. 297 * <li> If {@link #getUri} is non-null, try to retrieve its data 298 * as a text stream from its content provider. If this succeeds, copy 299 * the text into a String and return it. If it is not a content: URI or 300 * the content provider does not supply a text representation, return 301 * the raw URI as a string. 302 * <li> If {@link #getIntent} is non-null, convert that to an intent: 303 * URI and return it. 304 * <li> Otherwise, return an empty string. 305 * </ul> 306 * 307 * @param context The caller's Context, from which its ContentResolver 308 * and other things can be retrieved. 309 * @return Returns the item's textual representation. 310 */ 311//BEGIN_INCLUDE(coerceToText) 312 public CharSequence coerceToText(Context context) { 313 // If this Item has an explicit textual value, simply return that. 314 CharSequence text = getText(); 315 if (text != null) { 316 return text; 317 } 318 319 // If this Item has a URI value, try using that. 320 Uri uri = getUri(); 321 if (uri != null) { 322 323 // First see if the URI can be opened as a plain text stream 324 // (of any sub-type). If so, this is the best textual 325 // representation for it. 326 FileInputStream stream = null; 327 try { 328 // Ask for a stream of the desired type. 329 AssetFileDescriptor descr = context.getContentResolver() 330 .openTypedAssetFileDescriptor(uri, "text/*", null); 331 stream = descr.createInputStream(); 332 InputStreamReader reader = new InputStreamReader(stream, "UTF-8"); 333 334 // Got it... copy the stream into a local string and return it. 335 StringBuilder builder = new StringBuilder(128); 336 char[] buffer = new char[8192]; 337 int len; 338 while ((len=reader.read(buffer)) > 0) { 339 builder.append(buffer, 0, len); 340 } 341 return builder.toString(); 342 343 } catch (FileNotFoundException e) { 344 // Unable to open content URI as text... not really an 345 // error, just something to ignore. 346 347 } catch (IOException e) { 348 // Something bad has happened. 349 Log.w("ClippedData", "Failure loading text", e); 350 return e.toString(); 351 352 } finally { 353 if (stream != null) { 354 try { 355 stream.close(); 356 } catch (IOException e) { 357 } 358 } 359 } 360 361 // If we couldn't open the URI as a stream, then the URI itself 362 // probably serves fairly well as a textual representation. 363 return uri.toString(); 364 } 365 366 // Finally, if all we have is an Intent, then we can just turn that 367 // into text. Not the most user-friendly thing, but it's something. 368 Intent intent = getIntent(); 369 if (intent != null) { 370 return intent.toUri(Intent.URI_INTENT_SCHEME); 371 } 372 373 // Shouldn't get here, but just in case... 374 return ""; 375 } 376//END_INCLUDE(coerceToText) 377 378 /** 379 * Like {@link #coerceToHtmlText(Context)}, but any text that would 380 * be returned as HTML formatting will be returned as text with 381 * style spans. 382 * @param context The caller's Context, from which its ContentResolver 383 * and other things can be retrieved. 384 * @return Returns the item's textual representation. 385 */ 386 public CharSequence coerceToStyledText(Context context) { 387 CharSequence text = getText(); 388 if (text instanceof Spanned) { 389 return text; 390 } 391 String htmlText = getHtmlText(); 392 if (htmlText != null) { 393 try { 394 CharSequence newText = Html.fromHtml(htmlText); 395 if (newText != null) { 396 return newText; 397 } 398 } catch (RuntimeException e) { 399 // If anything bad happens, we'll fall back on the plain text. 400 } 401 } 402 403 if (text != null) { 404 return text; 405 } 406 return coerceToHtmlOrStyledText(context, true); 407 } 408 409 /** 410 * Turn this item into HTML text, regardless of the type of data it 411 * actually contains. 412 * 413 * <p>The algorithm for deciding what text to return is: 414 * <ul> 415 * <li> If {@link #getHtmlText} is non-null, return that. 416 * <li> If {@link #getText} is non-null, return that, converting to 417 * valid HTML text. If this text contains style spans, 418 * {@link Html#toHtml(Spanned) Html.toHtml(Spanned)} is used to 419 * convert them to HTML formatting. 420 * <li> If {@link #getUri} is non-null, try to retrieve its data 421 * as a text stream from its content provider. If the provider can 422 * supply text/html data, that will be preferred and returned as-is. 423 * Otherwise, any text/* data will be returned and escaped to HTML. 424 * If it is not a content: URI or the content provider does not supply 425 * a text representation, HTML text containing a link to the URI 426 * will be returned. 427 * <li> If {@link #getIntent} is non-null, convert that to an intent: 428 * URI and return as an HTML link. 429 * <li> Otherwise, return an empty string. 430 * </ul> 431 * 432 * @param context The caller's Context, from which its ContentResolver 433 * and other things can be retrieved. 434 * @return Returns the item's representation as HTML text. 435 */ 436 public String coerceToHtmlText(Context context) { 437 // If the item has an explicit HTML value, simply return that. 438 String htmlText = getHtmlText(); 439 if (htmlText != null) { 440 return htmlText; 441 } 442 443 // If this Item has a plain text value, return it as HTML. 444 CharSequence text = getText(); 445 if (text != null) { 446 if (text instanceof Spanned) { 447 return Html.toHtml((Spanned)text); 448 } 449 return Html.escapeHtml(text); 450 } 451 452 text = coerceToHtmlOrStyledText(context, false); 453 return text != null ? text.toString() : null; 454 } 455 456 private CharSequence coerceToHtmlOrStyledText(Context context, boolean styled) { 457 // If this Item has a URI value, try using that. 458 if (mUri != null) { 459 460 // Check to see what data representations the content 461 // provider supports. We would like HTML text, but if that 462 // is not possible we'll live with plan text. 463 String[] types = context.getContentResolver().getStreamTypes(mUri, "text/*"); 464 boolean hasHtml = false; 465 boolean hasText = false; 466 if (types != null) { 467 for (String type : types) { 468 if ("text/html".equals(type)) { 469 hasHtml = true; 470 } else if (type.startsWith("text/")) { 471 hasText = true; 472 } 473 } 474 } 475 476 // If the provider can serve data we can use, open and load it. 477 if (hasHtml || hasText) { 478 FileInputStream stream = null; 479 try { 480 // Ask for a stream of the desired type. 481 AssetFileDescriptor descr = context.getContentResolver() 482 .openTypedAssetFileDescriptor(mUri, 483 hasHtml ? "text/html" : "text/plain", null); 484 stream = descr.createInputStream(); 485 InputStreamReader reader = new InputStreamReader(stream, "UTF-8"); 486 487 // Got it... copy the stream into a local string and return it. 488 StringBuilder builder = new StringBuilder(128); 489 char[] buffer = new char[8192]; 490 int len; 491 while ((len=reader.read(buffer)) > 0) { 492 builder.append(buffer, 0, len); 493 } 494 String text = builder.toString(); 495 if (hasHtml) { 496 if (styled) { 497 // We loaded HTML formatted text and the caller 498 // want styled text, convert it. 499 try { 500 CharSequence newText = Html.fromHtml(text); 501 return newText != null ? newText : text; 502 } catch (RuntimeException e) { 503 return text; 504 } 505 } else { 506 // We loaded HTML formatted text and that is what 507 // the caller wants, just return it. 508 return text.toString(); 509 } 510 } 511 if (styled) { 512 // We loaded plain text and the caller wants styled 513 // text, that is all we have so return it. 514 return text; 515 } else { 516 // We loaded plain text and the caller wants HTML 517 // text, escape it for HTML. 518 return Html.escapeHtml(text); 519 } 520 521 } catch (FileNotFoundException e) { 522 // Unable to open content URI as text... not really an 523 // error, just something to ignore. 524 525 } catch (IOException e) { 526 // Something bad has happened. 527 Log.w("ClippedData", "Failure loading text", e); 528 return Html.escapeHtml(e.toString()); 529 530 } finally { 531 if (stream != null) { 532 try { 533 stream.close(); 534 } catch (IOException e) { 535 } 536 } 537 } 538 } 539 540 // If we couldn't open the URI as a stream, then we can build 541 // some HTML text with the URI itself. 542 // probably serves fairly well as a textual representation. 543 if (styled) { 544 return uriToStyledText(mUri.toString()); 545 } else { 546 return uriToHtml(mUri.toString()); 547 } 548 } 549 550 // Finally, if all we have is an Intent, then we can just turn that 551 // into text. Not the most user-friendly thing, but it's something. 552 if (mIntent != null) { 553 if (styled) { 554 return uriToStyledText(mIntent.toUri(Intent.URI_INTENT_SCHEME)); 555 } else { 556 return uriToHtml(mIntent.toUri(Intent.URI_INTENT_SCHEME)); 557 } 558 } 559 560 // Shouldn't get here, but just in case... 561 return ""; 562 } 563 564 private String uriToHtml(String uri) { 565 StringBuilder builder = new StringBuilder(256); 566 builder.append("<a href=\""); 567 builder.append(Html.escapeHtml(uri)); 568 builder.append("\">"); 569 builder.append(Html.escapeHtml(uri)); 570 builder.append("</a>"); 571 return builder.toString(); 572 } 573 574 private CharSequence uriToStyledText(String uri) { 575 SpannableStringBuilder builder = new SpannableStringBuilder(); 576 builder.append(uri); 577 builder.setSpan(new URLSpan(uri), 0, builder.length(), 578 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 579 return builder; 580 } 581 582 @Override 583 public String toString() { 584 StringBuilder b = new StringBuilder(128); 585 586 b.append("ClipData.Item { "); 587 toShortString(b); 588 b.append(" }"); 589 590 return b.toString(); 591 } 592 593 /** @hide */ 594 public void toShortString(StringBuilder b) { 595 if (mHtmlText != null) { 596 b.append("H:"); 597 b.append(mHtmlText); 598 } else if (mText != null) { 599 b.append("T:"); 600 b.append(mText); 601 } else if (mUri != null) { 602 b.append("U:"); 603 b.append(mUri); 604 } else if (mIntent != null) { 605 b.append("I:"); 606 mIntent.toShortString(b, true, true, true, true); 607 } else { 608 b.append("NULL"); 609 } 610 } 611 } 612 613 /** 614 * Create a new clip. 615 * 616 * @param label Label to show to the user describing this clip. 617 * @param mimeTypes An array of MIME types this data is available as. 618 * @param item The contents of the first item in the clip. 619 */ 620 public ClipData(CharSequence label, String[] mimeTypes, Item item) { 621 mClipDescription = new ClipDescription(label, mimeTypes); 622 if (item == null) { 623 throw new NullPointerException("item is null"); 624 } 625 mIcon = null; 626 mItems = new ArrayList<Item>(); 627 mItems.add(item); 628 } 629 630 /** 631 * Create a new clip. 632 * 633 * @param description The ClipDescription describing the clip contents. 634 * @param item The contents of the first item in the clip. 635 */ 636 public ClipData(ClipDescription description, Item item) { 637 mClipDescription = description; 638 if (item == null) { 639 throw new NullPointerException("item is null"); 640 } 641 mIcon = null; 642 mItems = new ArrayList<Item>(); 643 mItems.add(item); 644 } 645 646 /** 647 * Create a new clip that is a copy of another clip. This does a deep-copy 648 * of all items in the clip. 649 * 650 * @param other The existing ClipData that is to be copied. 651 */ 652 public ClipData(ClipData other) { 653 mClipDescription = other.mClipDescription; 654 mIcon = other.mIcon; 655 mItems = new ArrayList<Item>(other.mItems); 656 } 657 658 /** 659 * Create a new ClipData holding data of the type 660 * {@link ClipDescription#MIMETYPE_TEXT_PLAIN}. 661 * 662 * @param label User-visible label for the clip data. 663 * @param text The actual text in the clip. 664 * @return Returns a new ClipData containing the specified data. 665 */ 666 static public ClipData newPlainText(CharSequence label, CharSequence text) { 667 Item item = new Item(text); 668 return new ClipData(label, MIMETYPES_TEXT_PLAIN, item); 669 } 670 671 /** 672 * Create a new ClipData holding data of the type 673 * {@link ClipDescription#MIMETYPE_TEXT_HTML}. 674 * 675 * @param label User-visible label for the clip data. 676 * @param text The text of clip as plain text, for receivers that don't 677 * handle HTML. This is required. 678 * @param htmlText The actual HTML text in the clip. 679 * @return Returns a new ClipData containing the specified data. 680 */ 681 static public ClipData newHtmlText(CharSequence label, CharSequence text, 682 String htmlText) { 683 Item item = new Item(text, htmlText); 684 return new ClipData(label, MIMETYPES_TEXT_HTML, item); 685 } 686 687 /** 688 * Create a new ClipData holding an Intent with MIME type 689 * {@link ClipDescription#MIMETYPE_TEXT_INTENT}. 690 * 691 * @param label User-visible label for the clip data. 692 * @param intent The actual Intent in the clip. 693 * @return Returns a new ClipData containing the specified data. 694 */ 695 static public ClipData newIntent(CharSequence label, Intent intent) { 696 Item item = new Item(intent); 697 return new ClipData(label, MIMETYPES_TEXT_INTENT, item); 698 } 699 700 /** 701 * Create a new ClipData holding a URI. If the URI is a content: URI, 702 * this will query the content provider for the MIME type of its data and 703 * use that as the MIME type. Otherwise, it will use the MIME type 704 * {@link ClipDescription#MIMETYPE_TEXT_URILIST}. 705 * 706 * @param resolver ContentResolver used to get information about the URI. 707 * @param label User-visible label for the clip data. 708 * @param uri The URI in the clip. 709 * @return Returns a new ClipData containing the specified data. 710 */ 711 static public ClipData newUri(ContentResolver resolver, CharSequence label, 712 Uri uri) { 713 Item item = new Item(uri); 714 String[] mimeTypes = null; 715 if ("content".equals(uri.getScheme())) { 716 String realType = resolver.getType(uri); 717 mimeTypes = resolver.getStreamTypes(uri, "*/*"); 718 if (mimeTypes == null) { 719 if (realType != null) { 720 mimeTypes = new String[] { realType, ClipDescription.MIMETYPE_TEXT_URILIST }; 721 } 722 } else { 723 String[] tmp = new String[mimeTypes.length + (realType != null ? 2 : 1)]; 724 int i = 0; 725 if (realType != null) { 726 tmp[0] = realType; 727 i++; 728 } 729 System.arraycopy(mimeTypes, 0, tmp, i, mimeTypes.length); 730 tmp[i + mimeTypes.length] = ClipDescription.MIMETYPE_TEXT_URILIST; 731 mimeTypes = tmp; 732 } 733 } 734 if (mimeTypes == null) { 735 mimeTypes = MIMETYPES_TEXT_URILIST; 736 } 737 return new ClipData(label, mimeTypes, item); 738 } 739 740 /** 741 * Create a new ClipData holding an URI with MIME type 742 * {@link ClipDescription#MIMETYPE_TEXT_URILIST}. 743 * Unlike {@link #newUri(ContentResolver, CharSequence, Uri)}, nothing 744 * is inferred about the URI -- if it is a content: URI holding a bitmap, 745 * the reported type will still be uri-list. Use this with care! 746 * 747 * @param label User-visible label for the clip data. 748 * @param uri The URI in the clip. 749 * @return Returns a new ClipData containing the specified data. 750 */ 751 static public ClipData newRawUri(CharSequence label, Uri uri) { 752 Item item = new Item(uri); 753 return new ClipData(label, MIMETYPES_TEXT_URILIST, item); 754 } 755 756 /** 757 * Return the {@link ClipDescription} associated with this data, describing 758 * what it contains. 759 */ 760 public ClipDescription getDescription() { 761 return mClipDescription; 762 } 763 764 /** 765 * Add a new Item to the overall ClipData container. 766 */ 767 public void addItem(Item item) { 768 if (item == null) { 769 throw new NullPointerException("item is null"); 770 } 771 mItems.add(item); 772 } 773 774 /** @hide */ 775 public Bitmap getIcon() { 776 return mIcon; 777 } 778 779 /** 780 * Return the number of items in the clip data. 781 */ 782 public int getItemCount() { 783 return mItems.size(); 784 } 785 786 /** 787 * Return a single item inside of the clip data. The index can range 788 * from 0 to {@link #getItemCount()}-1. 789 */ 790 public Item getItemAt(int index) { 791 return mItems.get(index); 792 } 793 794 /** 795 * Prepare this {@link ClipData} to leave an app process. 796 * 797 * @hide 798 */ 799 public void prepareToLeaveProcess() { 800 final int size = mItems.size(); 801 for (int i = 0; i < size; i++) { 802 final Item item = mItems.get(i); 803 if (item.mIntent != null) { 804 item.mIntent.prepareToLeaveProcess(); 805 } 806 if (item.mUri != null && StrictMode.vmFileUriExposureEnabled()) { 807 item.mUri.checkFileUriExposed("ClipData.Item.getUri()"); 808 } 809 } 810 } 811 812 @Override 813 public String toString() { 814 StringBuilder b = new StringBuilder(128); 815 816 b.append("ClipData { "); 817 toShortString(b); 818 b.append(" }"); 819 820 return b.toString(); 821 } 822 823 /** @hide */ 824 public void toShortString(StringBuilder b) { 825 boolean first; 826 if (mClipDescription != null) { 827 first = !mClipDescription.toShortString(b); 828 } else { 829 first = true; 830 } 831 if (mIcon != null) { 832 if (!first) { 833 b.append(' '); 834 } 835 first = false; 836 b.append("I:"); 837 b.append(mIcon.getWidth()); 838 b.append('x'); 839 b.append(mIcon.getHeight()); 840 } 841 for (int i=0; i<mItems.size(); i++) { 842 if (!first) { 843 b.append(' '); 844 } 845 first = false; 846 b.append('{'); 847 mItems.get(i).toShortString(b); 848 b.append('}'); 849 } 850 } 851 852 @Override 853 public int describeContents() { 854 return 0; 855 } 856 857 @Override 858 public void writeToParcel(Parcel dest, int flags) { 859 mClipDescription.writeToParcel(dest, flags); 860 if (mIcon != null) { 861 dest.writeInt(1); 862 mIcon.writeToParcel(dest, flags); 863 } else { 864 dest.writeInt(0); 865 } 866 final int N = mItems.size(); 867 dest.writeInt(N); 868 for (int i=0; i<N; i++) { 869 Item item = mItems.get(i); 870 TextUtils.writeToParcel(item.mText, dest, flags); 871 dest.writeString(item.mHtmlText); 872 if (item.mIntent != null) { 873 dest.writeInt(1); 874 item.mIntent.writeToParcel(dest, flags); 875 } else { 876 dest.writeInt(0); 877 } 878 if (item.mUri != null) { 879 dest.writeInt(1); 880 item.mUri.writeToParcel(dest, flags); 881 } else { 882 dest.writeInt(0); 883 } 884 } 885 } 886 887 ClipData(Parcel in) { 888 mClipDescription = new ClipDescription(in); 889 if (in.readInt() != 0) { 890 mIcon = Bitmap.CREATOR.createFromParcel(in); 891 } else { 892 mIcon = null; 893 } 894 mItems = new ArrayList<Item>(); 895 final int N = in.readInt(); 896 for (int i=0; i<N; i++) { 897 CharSequence text = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); 898 String htmlText = in.readString(); 899 Intent intent = in.readInt() != 0 ? Intent.CREATOR.createFromParcel(in) : null; 900 Uri uri = in.readInt() != 0 ? Uri.CREATOR.createFromParcel(in) : null; 901 mItems.add(new Item(text, htmlText, intent, uri)); 902 } 903 } 904 905 public static final Parcelable.Creator<ClipData> CREATOR = 906 new Parcelable.Creator<ClipData>() { 907 908 public ClipData createFromParcel(Parcel source) { 909 return new ClipData(source); 910 } 911 912 public ClipData[] newArray(int size) { 913 return new ClipData[size]; 914 } 915 }; 916} 917