ClipData.java revision 327fbd2c8fa294b919475feb4c74a74ee1981e02
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 *
5323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <a name="ImplementingPaste"></a>
5423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <h3>Implementing Paste or Drop</h3>
5523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
5623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>To implement a paste or drop of a ClippedData object into an application,
5723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * the application must correctly interpret the data for its use.  If the {@link Item}
5823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * it contains is simple text or an Intent, there is little to be done: text
5923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * can only be interpreted as text, and an Intent will typically be used for
6023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * creating shortcuts (such as placing icons on the home screen) or other
6123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * actions.
6223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
6323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>If all you want is the textual representation of the clipped data, you
6423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * can use the convenience method {@link Item#coerceToText Item.coerceToText}.
651040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * In this case there is generally no need to worry about the MIME types
66f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * reported by {@link ClipDescription#getMimeType(int) getDescription().getMimeType(int)},
67f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * since any clip item an always be converted to a string.
6823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
6923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>More complicated exchanges will be done through URIs, in particular
7023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * "content:" URIs.  A content URI allows the recipient of a ClippedData item
7123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * to interact closely with the ContentProvider holding the data in order to
721040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * negotiate the transfer of that data.  The clip must also be filled in with
73327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn * the available MIME types; {@link #newUri(ContentResolver, CharSequence, Uri)}
741040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * will take care of correctly doing this.
7523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
7623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>For example, here is the paste function of a simple NotePad application.
7723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * When retrieving the data from the clipboard, it can do either two things:
7823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * if the clipboard contains a URI reference to an existing note, it copies
7923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * the entire structure of the note into a new note; otherwise, it simply
8023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * coerces the clip into text and uses that as the new note's contents.
8123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
8223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * {@sample development/samples/NotePad/src/com/example/android/notepad/NoteEditor.java
8323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *      paste}
8423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
8523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>In many cases an application can paste various types of streams of data.  For
8623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * example, an e-mail application may want to allow the user to paste an image
8723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * or other binary data as an attachment.  This is accomplished through the
8823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * ContentResolver {@link ContentResolver#getStreamTypes(Uri, String)} and
8923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * {@link ContentResolver#openTypedAssetFileDescriptor(Uri, String, android.os.Bundle)}
9023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * methods.  These allow a client to discover the type(s) of data that a particular
9123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * content URI can make available as a stream and retrieve the stream of data.
9223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
9323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>For example, the implementation of {@link Item#coerceToText Item.coerceToText}
9423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * itself uses this to try to retrieve a URI clip as a stream of text:
9523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
96f6d952bbc85706031e1ad29ec389c1e02cfff433Dianne Hackborn * {@sample frameworks/base/core/java/android/content/ClipData.java coerceToText}
9723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
9823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <a name="ImplementingCopy"></a>
9923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <h3>Implementing Copy or Drag</h3>
10023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
10123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>To be the source of a clip, the application must construct a ClippedData
10223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * object that any recipient can interpret best for their context.  If the clip
10323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * is to contain a simple text, Intent, or URI, this is easy: an {@link Item}
10423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * containing the appropriate data type can be constructed and used.
10523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
10623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>More complicated data types require the implementation of support in
10723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * a ContentProvider for describing and generating the data for the recipient.
10823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * A common scenario is one where an application places on the clipboard the
10923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * content: URI of an object that the user has copied, with the data at that
11023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * URI consisting of a complicated structure that only other applications with
11123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * direct knowledge of the structure can use.
11223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
11323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>For applications that do not have intrinsic knowledge of the data structure,
11423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * the content provider holding it can make the data available as an arbitrary
11523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * number of types of data streams.  This is done by implementing the
11623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * ContentProvider {@link ContentProvider#getStreamTypes(Uri, String)} and
11723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * {@link ContentProvider#openTypedAssetFile(Uri, String, android.os.Bundle)}
11823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * methods.
11923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
12023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>Going back to our simple NotePad application, this is the implementation
12123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * it may have to convert a single note URI (consisting of a title and the note
12223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * text) into a stream of plain text data.
12323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
12423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * {@sample development/samples/NotePad/src/com/example/android/notepad/NotePadProvider.java
12523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *      stream}
12623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
12723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>The copy operation in our NotePad application is now just a simple matter
12823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * of making a clip containing the URI of the note being copied:
12923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
13023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * {@sample development/samples/NotePad/src/com/example/android/notepad/NotesList.java
13123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *      copy}
13223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
13323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>Note if a paste operation needs this clip as text (for example to paste
13423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * into an editor), then {@link Item#coerceToText(Context)} will ask the content
13523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * provider for the clip URI as text and successfully paste the entire note.
1369f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn */
137f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackbornpublic class ClipData implements Parcelable {
138f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn    static final String[] MIMETYPES_TEXT_PLAIN = new String[] {
139f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn        ClipDescription.MIMETYPE_TEXT_PLAIN };
140f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn    static final String[] MIMETYPES_TEXT_URILIST = new String[] {
141f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn        ClipDescription.MIMETYPE_TEXT_URILIST };
142f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn    static final String[] MIMETYPES_TEXT_INTENT = new String[] {
143f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn        ClipDescription.MIMETYPE_TEXT_INTENT };
144f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn
145f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn    final ClipDescription mClipDescription;
146f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn
1471040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    final Bitmap mIcon;
1489f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
1499f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    final ArrayList<Item> mItems = new ArrayList<Item>();
1509f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
15123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn    /**
15223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * Description of a single item in a ClippedData.
15323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     *
15423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * <p>The types than an individual item can currently contain are:</p>
15523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     *
15623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * <ul>
15723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * <li> Text: a basic string of text.  This is actually a CharSequence,
15823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * so it can be formatted text supported by corresponding Android built-in
15923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * style spans.  (Custom application spans are not supported and will be
16023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * stripped when transporting through the clipboard.)
16123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * <li> Intent: an arbitrary Intent object.  A typical use is the shortcut
16223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * to create when pasting a clipped item on to the home screen.
16323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * <li> Uri: a URI reference.  This may be any URI (such as an http: URI
16423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * representing a bookmark), however it is often a content: URI.  Using
16523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * content provider references as clips like this allows an application to
16623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * share complex or large clips through the standard content provider
16723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * facilities.
16823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * </ul>
16923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     */
1709f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    public static class Item {
1711040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        final CharSequence mText;
1721040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        final Intent mIntent;
1731040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        final Uri mUri;
1749f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
17523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
17623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Create an Item consisting of a single block of (possibly styled) text.
17723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
1789f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        public Item(CharSequence text) {
1799f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mText = text;
1801040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mIntent = null;
1811040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mUri = null;
1829f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
1839f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
18423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
18523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Create an Item consisting of an arbitrary Intent.
18623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
1879f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        public Item(Intent intent) {
1881040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mText = null;
1899f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mIntent = intent;
1901040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mUri = null;
1919f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
1929f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
19323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
19423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Create an Item consisting of an arbitrary URI.
19523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
1969f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        public Item(Uri uri) {
1971040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mText = null;
1981040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mIntent = null;
1999f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mUri = uri;
2009f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
2019f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
20223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
20323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Create a complex Item, containing multiple representations of
20423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * text, intent, and/or URI.
20523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
2069f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        public Item(CharSequence text, Intent intent, Uri uri) {
2079f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mText = text;
2089f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mIntent = intent;
2099f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mUri = uri;
2109f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
2119f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
21223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
21323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Retrieve the raw text contained in this Item.
21423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
2159f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        public CharSequence getText() {
2169f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            return mText;
2179f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
2189f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
21923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
22023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Retrieve the raw Intent contained in this Item.
22123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
2229f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        public Intent getIntent() {
2239f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            return mIntent;
2249f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
2259f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
22623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
22723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Retrieve the raw URI contained in this Item.
22823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
2299f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        public Uri getUri() {
2309f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            return mUri;
2319f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
23223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
23323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
23423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Turn this item into text, regardless of the type of data it
23523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * actually contains.
23623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         *
23723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * <p>The algorithm for deciding what text to return is:
23823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * <ul>
23923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * <li> If {@link #getText} is non-null, return that.
24023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * <li> If {@link #getUri} is non-null, try to retrieve its data
24123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * as a text stream from its content provider.  If this succeeds, copy
24223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * the text into a String and return it.  If it is not a content: URI or
24323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * the content provider does not supply a text representation, return
24423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * the raw URI as a string.
24523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * <li> If {@link #getIntent} is non-null, convert that to an intent:
24623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * URI and returnit.
24723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * <li> Otherwise, return an empty string.
24823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * </ul>
24923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         *
25023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * @param context The caller's Context, from which its ContentResolver
25123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * and other things can be retrieved.
25223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * @return Returns the item's textual representation.
25323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
25423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn//BEGIN_INCLUDE(coerceToText)
25523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        public CharSequence coerceToText(Context context) {
25623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            // If this Item has an explicit textual value, simply return that.
25723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            if (mText != null) {
25823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                return mText;
25923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            }
26023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
26123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            // If this Item has a URI value, try using that.
26223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            if (mUri != null) {
26323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
26423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                // First see if the URI can be opened as a plain text stream
26523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                // (of any sub-type).  If so, this is the best textual
26623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                // representation for it.
26723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                FileInputStream stream = null;
26823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                try {
26923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    // Ask for a stream of the desired type.
27023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    AssetFileDescriptor descr = context.getContentResolver()
27123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                            .openTypedAssetFileDescriptor(mUri, "text/*", null);
27223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    stream = descr.createInputStream();
27323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
27423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
27523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    // Got it...  copy the stream into a local string and return it.
27623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    StringBuilder builder = new StringBuilder(128);
27723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    char[] buffer = new char[8192];
27823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    int len;
27923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    while ((len=reader.read(buffer)) > 0) {
28023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                        builder.append(buffer, 0, len);
28123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    }
28223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    return builder.toString();
28323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
28423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                } catch (FileNotFoundException e) {
28523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    // Unable to open content URI as text...  not really an
28623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    // error, just something to ignore.
28723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
28823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                } catch (IOException e) {
28923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    // Something bad has happened.
29023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    Log.w("ClippedData", "Failure loading text", e);
29123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    return e.toString();
29223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
29323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                } finally {
29423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    if (stream != null) {
29523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                        try {
29623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                            stream.close();
29723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                        } catch (IOException e) {
29823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                        }
29923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    }
30023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                }
30123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
30223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                // If we couldn't open the URI as a stream, then the URI itself
30323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                // probably serves fairly well as a textual representation.
30423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                return mUri.toString();
30523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            }
30623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
30723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            // Finally, if all we have is an Intent, then we can just turn that
30823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            // into text.  Not the most user-friendly thing, but it's something.
30923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            if (mIntent != null) {
31023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                return mIntent.toUri(Intent.URI_INTENT_SCHEME);
31123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            }
31223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
31323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            // Shouldn't get here, but just in case...
31423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            return "";
31523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        }
31623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn//END_INCLUDE(coerceToText)
3179f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
3189f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
3199f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    /**
3209f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn     * Create a new clip.
3219f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn     *
3229f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn     * @param label Label to show to the user describing this clip.
3231040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param mimeTypes An array of MIME types this data is available as.
3249f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn     * @param item The contents of the first item in the clip.
3259f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn     */
326327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    public ClipData(CharSequence label, String[] mimeTypes, Item item) {
327f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn        mClipDescription = new ClipDescription(label, mimeTypes);
328f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn        if (item == null) {
329f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn            throw new NullPointerException("item is null");
330f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn        }
331327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        mIcon = null;
332f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn        mItems.add(item);
333f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn    }
334f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn
335f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn    /**
336f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * Create a new clip.
337f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     *
338f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * @param description The ClipDescription describing the clip contents.
339f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * @param item The contents of the first item in the clip.
340f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     */
341327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    public ClipData(ClipDescription description, Item item) {
342f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn        mClipDescription = description;
3439f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        if (item == null) {
3449f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            throw new NullPointerException("item is null");
3459f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
346327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        mIcon = null;
3479f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        mItems.add(item);
3489f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
3499f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
3501040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    /**
351f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * Create a new ClipData holding data of the type
352f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * {@link ClipDescription#MIMETYPE_TEXT_PLAIN}.
3531040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     *
3541040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param label User-visible label for the clip data.
3551040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param text The actual text in the clip.
3561040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @return Returns a new ClipData containing the specified data.
3571040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     */
358327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    static public ClipData newPlainText(CharSequence label, CharSequence text) {
3591040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        Item item = new Item(text);
360327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        return new ClipData(label, MIMETYPES_TEXT_PLAIN, item);
361327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    }
362327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn
363327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    @Deprecated
364327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    static public ClipData newPlainText(CharSequence label, Bitmap icon, CharSequence text) {
365327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        return newPlainText(label, text);
3661040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    }
3671040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn
3681040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    /**
369f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * Create a new ClipData holding an Intent with MIME type
370f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * {@link ClipDescription#MIMETYPE_TEXT_INTENT}.
3711040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     *
3721040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param label User-visible label for the clip data.
3731040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param intent The actual Intent in the clip.
3741040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @return Returns a new ClipData containing the specified data.
3751040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     */
376327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    static public ClipData newIntent(CharSequence label, Intent intent) {
3771040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        Item item = new Item(intent);
378327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        return new ClipData(label, MIMETYPES_TEXT_INTENT, item);
3791040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    }
3801040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn
381327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    @Deprecated
382327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    static public ClipData newIntent(CharSequence label, Bitmap icon, Intent intent) {
383327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        return newIntent(label, intent);
384327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    }
385327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn
3861040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    /**
3871040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * Create a new ClipData holding a URI.  If the URI is a content: URI,
3881040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * this will query the content provider for the MIME type of its data and
3891040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * use that as the MIME type.  Otherwise, it will use the MIME type
390f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * {@link ClipDescription#MIMETYPE_TEXT_URILIST}.
3911040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     *
3921040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param resolver ContentResolver used to get information about the URI.
3931040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param label User-visible label for the clip data.
3941040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param uri The URI in the clip.
3951040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @return Returns a new ClipData containing the specified data.
3961040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     */
3971040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    static public ClipData newUri(ContentResolver resolver, CharSequence label,
398327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn            Uri uri) {
3991040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        Item item = new Item(uri);
4001040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        String[] mimeTypes = null;
4011040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        if ("content".equals(uri.getScheme())) {
4021040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            String realType = resolver.getType(uri);
4031040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mimeTypes = resolver.getStreamTypes(uri, "*/*");
4041040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            if (mimeTypes == null) {
4051040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                if (realType != null) {
406f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn                    mimeTypes = new String[] { realType, ClipDescription.MIMETYPE_TEXT_URILIST };
4071040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                }
4081040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            } else {
4091040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                String[] tmp = new String[mimeTypes.length + (realType != null ? 2 : 1)];
4101040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                int i = 0;
4111040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                if (realType != null) {
4121040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                    tmp[0] = realType;
4131040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                    i++;
4141040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                }
4151040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                System.arraycopy(mimeTypes, 0, tmp, i, mimeTypes.length);
416f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn                tmp[i + mimeTypes.length] = ClipDescription.MIMETYPE_TEXT_URILIST;
4171040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                mimeTypes = tmp;
4181040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            }
4191040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        }
4201040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        if (mimeTypes == null) {
4211040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mimeTypes = MIMETYPES_TEXT_URILIST;
4221040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        }
423327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        return new ClipData(label, mimeTypes, item);
4241040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    }
4251040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn
426327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    @Deprecated
427327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    static public ClipData newUri(ContentResolver resolver, CharSequence label,
428327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn            Bitmap icon, Uri uri) {
429327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        return newUri(resolver, label, uri);
430327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    }
431327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn
4321040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    /**
433f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * Create a new ClipData holding an URI with MIME type
434f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * {@link ClipDescription#MIMETYPE_TEXT_URILIST}.
435327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     * Unlike {@link #newUri(ContentResolver, CharSequence, Uri)}, nothing
4361040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * is inferred about the URI -- if it is a content: URI holding a bitmap,
4371040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * the reported type will still be uri-list.  Use this with care!
4381040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     *
4391040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param label User-visible label for the clip data.
4401040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param uri The URI in the clip.
4411040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @return Returns a new ClipData containing the specified data.
4421040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     */
443327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    static public ClipData newRawUri(CharSequence label, Uri uri) {
4441040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        Item item = new Item(uri);
445327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        return new ClipData(label, MIMETYPES_TEXT_URILIST, item);
4461040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    }
4471040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn
448327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    @Deprecated
449327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    static public ClipData newRawUri(CharSequence label, Bitmap icon, Uri uri) {
450327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        return newRawUri(label, uri);
451327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    }
452327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn
453f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn    /**
454f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * Return the {@link ClipDescription} associated with this data, describing
455f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * what it contains.
456f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     */
457f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn    public ClipDescription getDescription() {
458f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn        return mClipDescription;
459f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn    }
460f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn
461327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    /**
462327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     * Add a new Item to the overall ClipData container.
463327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     */
4649f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    public void addItem(Item item) {
4659f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        if (item == null) {
4669f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            throw new NullPointerException("item is null");
4679f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
4689f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        mItems.add(item);
4699f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
4709f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
471327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    /** @hide */
4729f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    public Bitmap getIcon() {
4739f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        return mIcon;
4749f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
4759f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
476327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    /**
477327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     * Return the number of items in the clip data.
478327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     */
4799f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    public int getItemCount() {
4809f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        return mItems.size();
4819f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
4829f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
483327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    /**
484327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     * Return a single item inside of the clip data.  The index can range
485327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     * from 0 to {@link #getItemCount()}-1.
486327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     */
487327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    public Item getItemAt(int index) {
4889f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        return mItems.get(index);
4899f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
4909f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
491327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    @Deprecated
492327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    public Item getItem(int index) {
493327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        return getItemAt(index);
494327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    }
495327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn
4969f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    @Override
4979f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    public int describeContents() {
4989f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        return 0;
4999f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
5009f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
5019f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    @Override
5029f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    public void writeToParcel(Parcel dest, int flags) {
503f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn        mClipDescription.writeToParcel(dest, flags);
5049f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        if (mIcon != null) {
5059f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            dest.writeInt(1);
5069f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mIcon.writeToParcel(dest, flags);
5079f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        } else {
5089f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            dest.writeInt(0);
5099f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
5109f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        final int N = mItems.size();
5119f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        dest.writeInt(N);
5129f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        for (int i=0; i<N; i++) {
5139f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            Item item = mItems.get(i);
5149f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            TextUtils.writeToParcel(item.mText, dest, flags);
5159f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            if (item.mIntent != null) {
5169f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn                dest.writeInt(1);
5179f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn                item.mIntent.writeToParcel(dest, flags);
5189f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            } else {
5199f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn                dest.writeInt(0);
5209f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            }
5219f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            if (item.mUri != null) {
5229f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn                dest.writeInt(1);
5239f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn                item.mUri.writeToParcel(dest, flags);
5249f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            } else {
5259f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn                dest.writeInt(0);
5269f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            }
5279f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
5289f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
5299f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
5301040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    ClipData(Parcel in) {
531f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn        mClipDescription = new ClipDescription(in);
5329f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        if (in.readInt() != 0) {
5339f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mIcon = Bitmap.CREATOR.createFromParcel(in);
5341040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        } else {
5351040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mIcon = null;
5369f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
5379f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        final int N = in.readInt();
5389f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        for (int i=0; i<N; i++) {
5399f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            CharSequence text = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
5409f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            Intent intent = in.readInt() != 0 ? Intent.CREATOR.createFromParcel(in) : null;
5419f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            Uri uri = in.readInt() != 0 ? Uri.CREATOR.createFromParcel(in) : null;
5429f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mItems.add(new Item(text, intent, uri));
5439f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
5449f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
5459f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
5461040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    public static final Parcelable.Creator<ClipData> CREATOR =
5471040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        new Parcelable.Creator<ClipData>() {
5489f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
5491040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            public ClipData createFromParcel(Parcel source) {
5501040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                return new ClipData(source);
5519f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            }
5529f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
5531040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            public ClipData[] newArray(int size) {
5541040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                return new ClipData[size];
5559f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            }
5569f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        };
5579f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn}
558