ClipData.java revision 3aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45
19f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn/** 29f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * Copyright (c) 2010, The Android Open Source Project 39f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * 49f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License"); 59f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * you may not use this file except in compliance with the License. 69f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * You may obtain a copy of the License at 79f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * 89f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * http://www.apache.org/licenses/LICENSE-2.0 99f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * 109f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * Unless required by applicable law or agreed to in writing, software 119f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS, 129f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * See the License for the specific language governing permissions and 149f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * limitations under the License. 159f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn */ 169f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 179f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackbornpackage android.content; 189f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 1923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackbornimport android.content.res.AssetFileDescriptor; 209f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackbornimport android.graphics.Bitmap; 219f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackbornimport android.net.Uri; 229f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackbornimport android.os.Parcel; 239f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackbornimport android.os.Parcelable; 249f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackbornimport android.text.TextUtils; 2523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackbornimport android.util.Log; 269f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 2723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackbornimport java.io.FileInputStream; 2823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackbornimport java.io.FileNotFoundException; 2923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackbornimport java.io.IOException; 3023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackbornimport java.io.InputStreamReader; 319f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackbornimport java.util.ArrayList; 329f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 339f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn/** 349f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * Representation of a clipped data on the clipboard. 359f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * 369f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * <p>ClippedData is a complex type containing one or Item instances, 379f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * each of which can hold one or more representations of an item of data. 389f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * For display to the user, it also has a label and iconic representation.</p> 399f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * 40f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * <p>A ClipData contains a {@link ClipDescription}, which describes 41f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * important meta-data about the clip. In particular, its 42f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * {@link ClipDescription#getMimeType(int) getDescription().getMimeType(int)} 431040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * must return correct MIME type(s) describing the data in the clip. For help 441040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * in correctly constructing a clip with the correct MIME type, use 45327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn * {@link #newPlainText(CharSequence, CharSequence)}, 46327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn * {@link #newUri(ContentResolver, CharSequence, Uri)}, and 47327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn * {@link #newIntent(CharSequence, Intent)}. 481040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * 4923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>Each Item instance can be one of three main classes of data: a simple 5023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * CharSequence of text, a single Intent object, or a Uri. See {@link Item} 5123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * for more details. 5223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * 533aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <div class="special reference"> 543aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <h3>Developer Guides</h3> 553aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <p>For more information about using the clipboard framework, read the 563aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <a href="{@docRoot}guide/topics/clipboard/copy-paste.html">Copy and Paste</a> 573aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * developer guide.</p> 583aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * </div> 593aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * 6023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <a name="ImplementingPaste"></a> 6123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <h3>Implementing Paste or Drop</h3> 6223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * 6323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>To implement a paste or drop of a ClippedData object into an application, 6423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * the application must correctly interpret the data for its use. If the {@link Item} 6523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * it contains is simple text or an Intent, there is little to be done: text 6623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * can only be interpreted as text, and an Intent will typically be used for 6723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * creating shortcuts (such as placing icons on the home screen) or other 6823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * actions. 6923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * 7023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>If all you want is the textual representation of the clipped data, you 7123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * can use the convenience method {@link Item#coerceToText Item.coerceToText}. 721040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * In this case there is generally no need to worry about the MIME types 73f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * reported by {@link ClipDescription#getMimeType(int) getDescription().getMimeType(int)}, 74f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * since any clip item an always be converted to a string. 7523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * 7623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>More complicated exchanges will be done through URIs, in particular 7723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * "content:" URIs. A content URI allows the recipient of a ClippedData item 7823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * to interact closely with the ContentProvider holding the data in order to 791040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * negotiate the transfer of that data. The clip must also be filled in with 80327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn * the available MIME types; {@link #newUri(ContentResolver, CharSequence, Uri)} 811040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * will take care of correctly doing this. 8223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * 8323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>For example, here is the paste function of a simple NotePad application. 8423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * When retrieving the data from the clipboard, it can do either two things: 8523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * if the clipboard contains a URI reference to an existing note, it copies 8623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * the entire structure of the note into a new note; otherwise, it simply 8723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * coerces the clip into text and uses that as the new note's contents. 8823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * 8923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * {@sample development/samples/NotePad/src/com/example/android/notepad/NoteEditor.java 9023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * paste} 9123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * 9223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>In many cases an application can paste various types of streams of data. For 9323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * example, an e-mail application may want to allow the user to paste an image 9423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * or other binary data as an attachment. This is accomplished through the 9523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * ContentResolver {@link ContentResolver#getStreamTypes(Uri, String)} and 9623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * {@link ContentResolver#openTypedAssetFileDescriptor(Uri, String, android.os.Bundle)} 9723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * methods. These allow a client to discover the type(s) of data that a particular 9823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * content URI can make available as a stream and retrieve the stream of data. 9923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * 10023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>For example, the implementation of {@link Item#coerceToText Item.coerceToText} 10123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * itself uses this to try to retrieve a URI clip as a stream of text: 10223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * 103f6d952bbc85706031e1ad29ec389c1e02cfff433Dianne Hackborn * {@sample frameworks/base/core/java/android/content/ClipData.java coerceToText} 10423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * 10523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <a name="ImplementingCopy"></a> 10623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <h3>Implementing Copy or Drag</h3> 10723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * 10823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>To be the source of a clip, the application must construct a ClippedData 10923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * object that any recipient can interpret best for their context. If the clip 11023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * is to contain a simple text, Intent, or URI, this is easy: an {@link Item} 11123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * containing the appropriate data type can be constructed and used. 11223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * 11323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>More complicated data types require the implementation of support in 11423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * a ContentProvider for describing and generating the data for the recipient. 11523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * A common scenario is one where an application places on the clipboard the 11623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * content: URI of an object that the user has copied, with the data at that 11723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * URI consisting of a complicated structure that only other applications with 11823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * direct knowledge of the structure can use. 11923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * 12023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>For applications that do not have intrinsic knowledge of the data structure, 12123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * the content provider holding it can make the data available as an arbitrary 12223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * number of types of data streams. This is done by implementing the 12323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * ContentProvider {@link ContentProvider#getStreamTypes(Uri, String)} and 12423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * {@link ContentProvider#openTypedAssetFile(Uri, String, android.os.Bundle)} 12523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * methods. 12623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * 12723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>Going back to our simple NotePad application, this is the implementation 12823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * it may have to convert a single note URI (consisting of a title and the note 12923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * text) into a stream of plain text data. 13023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * 13123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * {@sample development/samples/NotePad/src/com/example/android/notepad/NotePadProvider.java 13223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * stream} 13323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * 13423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>The copy operation in our NotePad application is now just a simple matter 13523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * of making a clip containing the URI of the note being copied: 13623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * 13723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * {@sample development/samples/NotePad/src/com/example/android/notepad/NotesList.java 13823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * copy} 13923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * 14023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>Note if a paste operation needs this clip as text (for example to paste 14123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * into an editor), then {@link Item#coerceToText(Context)} will ask the content 14223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * provider for the clip URI as text and successfully paste the entire note. 1439f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn */ 144f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackbornpublic class ClipData implements Parcelable { 145f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn static final String[] MIMETYPES_TEXT_PLAIN = new String[] { 146f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn ClipDescription.MIMETYPE_TEXT_PLAIN }; 147f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn static final String[] MIMETYPES_TEXT_URILIST = new String[] { 148f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn ClipDescription.MIMETYPE_TEXT_URILIST }; 149f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn static final String[] MIMETYPES_TEXT_INTENT = new String[] { 150f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn ClipDescription.MIMETYPE_TEXT_INTENT }; 151f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn 152f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn final ClipDescription mClipDescription; 153f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn 1541040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn final Bitmap mIcon; 1559f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 1569f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn final ArrayList<Item> mItems = new ArrayList<Item>(); 1579f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 15823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn /** 15923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * Description of a single item in a ClippedData. 16023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * 16123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>The types than an individual item can currently contain are:</p> 16223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * 16323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <ul> 16423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <li> Text: a basic string of text. This is actually a CharSequence, 16523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * so it can be formatted text supported by corresponding Android built-in 16623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * style spans. (Custom application spans are not supported and will be 16723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * stripped when transporting through the clipboard.) 16823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <li> Intent: an arbitrary Intent object. A typical use is the shortcut 16923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * to create when pasting a clipped item on to the home screen. 17023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <li> Uri: a URI reference. This may be any URI (such as an http: URI 17123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * representing a bookmark), however it is often a content: URI. Using 17223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * content provider references as clips like this allows an application to 17323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * share complex or large clips through the standard content provider 17423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * facilities. 17523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * </ul> 17623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn */ 1779f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn public static class Item { 1781040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn final CharSequence mText; 1791040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn final Intent mIntent; 1801040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn final Uri mUri; 1819f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 18223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn /** 18323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * Create an Item consisting of a single block of (possibly styled) text. 18423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn */ 1859f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn public Item(CharSequence text) { 1869f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn mText = text; 1871040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn mIntent = null; 1881040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn mUri = null; 1899f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 1909f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 19123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn /** 19223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * Create an Item consisting of an arbitrary Intent. 19323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn */ 1949f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn public Item(Intent intent) { 1951040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn mText = null; 1969f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn mIntent = intent; 1971040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn mUri = null; 1989f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 1999f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 20023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn /** 20123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * Create an Item consisting of an arbitrary URI. 20223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn */ 2039f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn public Item(Uri uri) { 2041040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn mText = null; 2051040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn mIntent = null; 2069f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn mUri = uri; 2079f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 2089f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 20923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn /** 21023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * Create a complex Item, containing multiple representations of 21123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * text, intent, and/or URI. 21223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn */ 2139f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn public Item(CharSequence text, Intent intent, Uri uri) { 2149f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn mText = text; 2159f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn mIntent = intent; 2169f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn mUri = uri; 2179f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 2189f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 21923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn /** 22023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * Retrieve the raw text contained in this Item. 22123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn */ 2229f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn public CharSequence getText() { 2239f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn return mText; 2249f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 2259f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 22623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn /** 22723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * Retrieve the raw Intent contained in this Item. 22823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn */ 2299f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn public Intent getIntent() { 2309f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn return mIntent; 2319f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 2329f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 23323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn /** 23423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * Retrieve the raw URI contained in this Item. 23523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn */ 2369f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn public Uri getUri() { 2379f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn return mUri; 2389f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 23923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn 24023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn /** 24123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * Turn this item into text, regardless of the type of data it 24223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * actually contains. 24323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * 24423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>The algorithm for deciding what text to return is: 24523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <ul> 24623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <li> If {@link #getText} is non-null, return that. 24723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <li> If {@link #getUri} is non-null, try to retrieve its data 24823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * as a text stream from its content provider. If this succeeds, copy 24923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * the text into a String and return it. If it is not a content: URI or 25023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * the content provider does not supply a text representation, return 25123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * the raw URI as a string. 25223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <li> If {@link #getIntent} is non-null, convert that to an intent: 25323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * URI and returnit. 25423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <li> Otherwise, return an empty string. 25523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * </ul> 25623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * 25723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * @param context The caller's Context, from which its ContentResolver 25823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * and other things can be retrieved. 25923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * @return Returns the item's textual representation. 26023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn */ 26123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn//BEGIN_INCLUDE(coerceToText) 26223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn public CharSequence coerceToText(Context context) { 26323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn // If this Item has an explicit textual value, simply return that. 26423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn if (mText != null) { 26523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn return mText; 26623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn } 26723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn 26823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn // If this Item has a URI value, try using that. 26923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn if (mUri != null) { 27023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn 27123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn // First see if the URI can be opened as a plain text stream 27223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn // (of any sub-type). If so, this is the best textual 27323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn // representation for it. 27423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn FileInputStream stream = null; 27523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn try { 27623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn // Ask for a stream of the desired type. 27723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn AssetFileDescriptor descr = context.getContentResolver() 27823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn .openTypedAssetFileDescriptor(mUri, "text/*", null); 27923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn stream = descr.createInputStream(); 28023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn InputStreamReader reader = new InputStreamReader(stream, "UTF-8"); 28123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn 28223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn // Got it... copy the stream into a local string and return it. 28323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn StringBuilder builder = new StringBuilder(128); 28423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn char[] buffer = new char[8192]; 28523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn int len; 28623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn while ((len=reader.read(buffer)) > 0) { 28723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn builder.append(buffer, 0, len); 28823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn } 28923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn return builder.toString(); 29023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn 29123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn } catch (FileNotFoundException e) { 29223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn // Unable to open content URI as text... not really an 29323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn // error, just something to ignore. 29423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn 29523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn } catch (IOException e) { 29623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn // Something bad has happened. 29723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn Log.w("ClippedData", "Failure loading text", e); 29823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn return e.toString(); 29923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn 30023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn } finally { 30123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn if (stream != null) { 30223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn try { 30323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn stream.close(); 30423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn } catch (IOException e) { 30523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn } 30623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn } 30723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn } 30823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn 30923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn // If we couldn't open the URI as a stream, then the URI itself 31023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn // probably serves fairly well as a textual representation. 31123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn return mUri.toString(); 31223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn } 31323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn 31423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn // Finally, if all we have is an Intent, then we can just turn that 31523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn // into text. Not the most user-friendly thing, but it's something. 31623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn if (mIntent != null) { 31723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn return mIntent.toUri(Intent.URI_INTENT_SCHEME); 31823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn } 31923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn 32023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn // Shouldn't get here, but just in case... 32123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn return ""; 32223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn } 32323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn//END_INCLUDE(coerceToText) 3249f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 3259f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 3269f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn /** 3279f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * Create a new clip. 3289f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * 3299f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * @param label Label to show to the user describing this clip. 3301040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * @param mimeTypes An array of MIME types this data is available as. 3319f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * @param item The contents of the first item in the clip. 3329f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn */ 333327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn public ClipData(CharSequence label, String[] mimeTypes, Item item) { 334f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn mClipDescription = new ClipDescription(label, mimeTypes); 335f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn if (item == null) { 336f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn throw new NullPointerException("item is null"); 337f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn } 338327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn mIcon = null; 339f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn mItems.add(item); 340f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn } 341f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn 342f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn /** 343f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * Create a new clip. 344f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * 345f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * @param description The ClipDescription describing the clip contents. 346f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * @param item The contents of the first item in the clip. 347f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn */ 348327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn public ClipData(ClipDescription description, Item item) { 349f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn mClipDescription = description; 3509f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn if (item == null) { 3519f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn throw new NullPointerException("item is null"); 3529f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 353327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn mIcon = null; 3549f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn mItems.add(item); 3559f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 3569f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 3571040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn /** 358f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * Create a new ClipData holding data of the type 359f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * {@link ClipDescription#MIMETYPE_TEXT_PLAIN}. 3601040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * 3611040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * @param label User-visible label for the clip data. 3621040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * @param text The actual text in the clip. 3631040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * @return Returns a new ClipData containing the specified data. 3641040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn */ 365327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn static public ClipData newPlainText(CharSequence label, CharSequence text) { 3661040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn Item item = new Item(text); 367327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn return new ClipData(label, MIMETYPES_TEXT_PLAIN, item); 368327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn } 369327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn 3701040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn /** 371f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * Create a new ClipData holding an Intent with MIME type 372f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * {@link ClipDescription#MIMETYPE_TEXT_INTENT}. 3731040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * 3741040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * @param label User-visible label for the clip data. 3751040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * @param intent The actual Intent in the clip. 3761040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * @return Returns a new ClipData containing the specified data. 3771040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn */ 378327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn static public ClipData newIntent(CharSequence label, Intent intent) { 3791040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn Item item = new Item(intent); 380327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn return new ClipData(label, MIMETYPES_TEXT_INTENT, item); 3811040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn } 3821040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn 3831040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn /** 3841040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * Create a new ClipData holding a URI. If the URI is a content: URI, 3851040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * this will query the content provider for the MIME type of its data and 3861040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * use that as the MIME type. Otherwise, it will use the MIME type 387f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * {@link ClipDescription#MIMETYPE_TEXT_URILIST}. 3881040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * 3891040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * @param resolver ContentResolver used to get information about the URI. 3901040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * @param label User-visible label for the clip data. 3911040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * @param uri The URI in the clip. 3921040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * @return Returns a new ClipData containing the specified data. 3931040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn */ 3941040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn static public ClipData newUri(ContentResolver resolver, CharSequence label, 395327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn Uri uri) { 3961040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn Item item = new Item(uri); 3971040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn String[] mimeTypes = null; 3981040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn if ("content".equals(uri.getScheme())) { 3991040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn String realType = resolver.getType(uri); 4001040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn mimeTypes = resolver.getStreamTypes(uri, "*/*"); 4011040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn if (mimeTypes == null) { 4021040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn if (realType != null) { 403f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn mimeTypes = new String[] { realType, ClipDescription.MIMETYPE_TEXT_URILIST }; 4041040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn } 4051040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn } else { 4061040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn String[] tmp = new String[mimeTypes.length + (realType != null ? 2 : 1)]; 4071040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn int i = 0; 4081040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn if (realType != null) { 4091040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn tmp[0] = realType; 4101040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn i++; 4111040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn } 4121040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn System.arraycopy(mimeTypes, 0, tmp, i, mimeTypes.length); 413f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn tmp[i + mimeTypes.length] = ClipDescription.MIMETYPE_TEXT_URILIST; 4141040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn mimeTypes = tmp; 4151040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn } 4161040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn } 4171040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn if (mimeTypes == null) { 4181040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn mimeTypes = MIMETYPES_TEXT_URILIST; 4191040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn } 420327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn return new ClipData(label, mimeTypes, item); 4211040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn } 4221040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn 4231040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn /** 424f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * Create a new ClipData holding an URI with MIME type 425f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * {@link ClipDescription#MIMETYPE_TEXT_URILIST}. 426327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn * Unlike {@link #newUri(ContentResolver, CharSequence, Uri)}, nothing 4271040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * is inferred about the URI -- if it is a content: URI holding a bitmap, 4281040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * the reported type will still be uri-list. Use this with care! 4291040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * 4301040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * @param label User-visible label for the clip data. 4311040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * @param uri The URI in the clip. 4321040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * @return Returns a new ClipData containing the specified data. 4331040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn */ 434327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn static public ClipData newRawUri(CharSequence label, Uri uri) { 4351040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn Item item = new Item(uri); 436327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn return new ClipData(label, MIMETYPES_TEXT_URILIST, item); 4371040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn } 4381040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn 439f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn /** 440f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * Return the {@link ClipDescription} associated with this data, describing 441f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * what it contains. 442f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn */ 443f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn public ClipDescription getDescription() { 444f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn return mClipDescription; 445f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn } 446f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn 447327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn /** 448327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn * Add a new Item to the overall ClipData container. 449327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn */ 4509f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn public void addItem(Item item) { 4519f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn if (item == null) { 4529f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn throw new NullPointerException("item is null"); 4539f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 4549f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn mItems.add(item); 4559f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 4569f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 457327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn /** @hide */ 4589f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn public Bitmap getIcon() { 4599f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn return mIcon; 4609f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 4619f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 462327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn /** 463327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn * Return the number of items in the clip data. 464327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn */ 4659f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn public int getItemCount() { 4669f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn return mItems.size(); 4679f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 4689f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 469327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn /** 470327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn * Return a single item inside of the clip data. The index can range 471327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn * from 0 to {@link #getItemCount()}-1. 472327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn */ 473327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn public Item getItemAt(int index) { 4749f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn return mItems.get(index); 4759f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 4769f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 4779f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn @Override 4789f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn public int describeContents() { 4799f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn return 0; 4809f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 4819f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 4829f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn @Override 4839f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn public void writeToParcel(Parcel dest, int flags) { 484f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn mClipDescription.writeToParcel(dest, flags); 4859f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn if (mIcon != null) { 4869f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn dest.writeInt(1); 4879f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn mIcon.writeToParcel(dest, flags); 4889f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } else { 4899f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn dest.writeInt(0); 4909f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 4919f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn final int N = mItems.size(); 4929f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn dest.writeInt(N); 4939f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn for (int i=0; i<N; i++) { 4949f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn Item item = mItems.get(i); 4959f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn TextUtils.writeToParcel(item.mText, dest, flags); 4969f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn if (item.mIntent != null) { 4979f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn dest.writeInt(1); 4989f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn item.mIntent.writeToParcel(dest, flags); 4999f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } else { 5009f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn dest.writeInt(0); 5019f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 5029f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn if (item.mUri != null) { 5039f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn dest.writeInt(1); 5049f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn item.mUri.writeToParcel(dest, flags); 5059f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } else { 5069f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn dest.writeInt(0); 5079f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 5089f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 5099f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 5109f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 5111040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn ClipData(Parcel in) { 512f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn mClipDescription = new ClipDescription(in); 5139f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn if (in.readInt() != 0) { 5149f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn mIcon = Bitmap.CREATOR.createFromParcel(in); 5151040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn } else { 5161040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn mIcon = null; 5179f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 5189f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn final int N = in.readInt(); 5199f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn for (int i=0; i<N; i++) { 5209f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn CharSequence text = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); 5219f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn Intent intent = in.readInt() != 0 ? Intent.CREATOR.createFromParcel(in) : null; 5229f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn Uri uri = in.readInt() != 0 ? Uri.CREATOR.createFromParcel(in) : null; 5239f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn mItems.add(new Item(text, intent, uri)); 5249f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 5259f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 5269f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 5271040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn public static final Parcelable.Creator<ClipData> CREATOR = 5281040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn new Parcelable.Creator<ClipData>() { 5299f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 5301040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn public ClipData createFromParcel(Parcel source) { 5311040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn return new ClipData(source); 5329f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 5339f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn 5341040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn public ClipData[] newArray(int size) { 5351040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn return new ClipData[size]; 5369f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn } 5379f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn }; 5389f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn} 539