ClipData.java revision f6d952bbc85706031e1ad29ec389c1e02cfff433
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 *
401040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * <p>A ClipData is a sub-class of {@link ClipDescription}, which describes
411040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * important meta-data about the clip.  In particular, its {@link #getMimeType(int)}
421040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * must return correct MIME type(s) describing the data in the clip.  For help
431040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * in correctly constructing a clip with the correct MIME type, use
441040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * {@link #newPlainText(CharSequence, Bitmap, CharSequence)},
451040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * {@link #newUri(ContentResolver, CharSequence, Bitmap, Uri)}, and
461040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * {@link #newIntent(CharSequence, Bitmap, Intent)}.
471040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn *
4823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>Each Item instance can be one of three main classes of data: a simple
4923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * CharSequence of text, a single Intent object, or a Uri.  See {@link Item}
5023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * for more details.
5123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
5223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <a name="ImplementingPaste"></a>
5323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <h3>Implementing Paste or Drop</h3>
5423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
5523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>To implement a paste or drop of a ClippedData object into an application,
5623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * the application must correctly interpret the data for its use.  If the {@link Item}
5723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * it contains is simple text or an Intent, there is little to be done: text
5823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * can only be interpreted as text, and an Intent will typically be used for
5923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * creating shortcuts (such as placing icons on the home screen) or other
6023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * actions.
6123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
6223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>If all you want is the textual representation of the clipped data, you
6323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * can use the convenience method {@link Item#coerceToText Item.coerceToText}.
641040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * In this case there is generally no need to worry about the MIME types
651040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * reported by {@link #getMimeType(int)}, since any clip item an always be
661040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * converted to a string.
6723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
6823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>More complicated exchanges will be done through URIs, in particular
6923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * "content:" URIs.  A content URI allows the recipient of a ClippedData item
7023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * to interact closely with the ContentProvider holding the data in order to
711040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * negotiate the transfer of that data.  The clip must also be filled in with
721040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * the available MIME types; {@link #newUri(ContentResolver, CharSequence, Bitmap, Uri)}
731040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * will take care of correctly doing this.
7423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
7523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>For example, here is the paste function of a simple NotePad application.
7623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * When retrieving the data from the clipboard, it can do either two things:
7723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * if the clipboard contains a URI reference to an existing note, it copies
7823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * the entire structure of the note into a new note; otherwise, it simply
7923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * coerces the clip into text and uses that as the new note's contents.
8023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
8123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * {@sample development/samples/NotePad/src/com/example/android/notepad/NoteEditor.java
8223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *      paste}
8323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
8423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>In many cases an application can paste various types of streams of data.  For
8523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * example, an e-mail application may want to allow the user to paste an image
8623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * or other binary data as an attachment.  This is accomplished through the
8723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * ContentResolver {@link ContentResolver#getStreamTypes(Uri, String)} and
8823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * {@link ContentResolver#openTypedAssetFileDescriptor(Uri, String, android.os.Bundle)}
8923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * methods.  These allow a client to discover the type(s) of data that a particular
9023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * content URI can make available as a stream and retrieve the stream of data.
9123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
9223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>For example, the implementation of {@link Item#coerceToText Item.coerceToText}
9323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * itself uses this to try to retrieve a URI clip as a stream of text:
9423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
95f6d952bbc85706031e1ad29ec389c1e02cfff433Dianne Hackborn * {@sample frameworks/base/core/java/android/content/ClipData.java coerceToText}
9623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
9723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <a name="ImplementingCopy"></a>
9823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <h3>Implementing Copy or Drag</h3>
9923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
10023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>To be the source of a clip, the application must construct a ClippedData
10123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * object that any recipient can interpret best for their context.  If the clip
10223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * is to contain a simple text, Intent, or URI, this is easy: an {@link Item}
10323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * containing the appropriate data type can be constructed and used.
10423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
10523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>More complicated data types require the implementation of support in
10623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * a ContentProvider for describing and generating the data for the recipient.
10723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * A common scenario is one where an application places on the clipboard the
10823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * content: URI of an object that the user has copied, with the data at that
10923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * URI consisting of a complicated structure that only other applications with
11023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * direct knowledge of the structure can use.
11123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
11223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>For applications that do not have intrinsic knowledge of the data structure,
11323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * the content provider holding it can make the data available as an arbitrary
11423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * number of types of data streams.  This is done by implementing the
11523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * ContentProvider {@link ContentProvider#getStreamTypes(Uri, String)} and
11623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * {@link ContentProvider#openTypedAssetFile(Uri, String, android.os.Bundle)}
11723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * methods.
11823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
11923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>Going back to our simple NotePad application, this is the implementation
12023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * it may have to convert a single note URI (consisting of a title and the note
12123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * text) into a stream of plain text data.
12223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
12323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * {@sample development/samples/NotePad/src/com/example/android/notepad/NotePadProvider.java
12423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *      stream}
12523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
12623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>The copy operation in our NotePad application is now just a simple matter
12723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * of making a clip containing the URI of the note being copied:
12823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
12923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * {@sample development/samples/NotePad/src/com/example/android/notepad/NotesList.java
13023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *      copy}
13123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
13223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>Note if a paste operation needs this clip as text (for example to paste
13323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * into an editor), then {@link Item#coerceToText(Context)} will ask the content
13423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * provider for the clip URI as text and successfully paste the entire note.
1359f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn */
1361040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackbornpublic class ClipData extends ClipDescription {
1371040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    static final String[] MIMETYPES_TEXT_PLAIN = new String[] { MIMETYPE_TEXT_PLAIN };
1381040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    static final String[] MIMETYPES_TEXT_URILIST = new String[] { MIMETYPE_TEXT_URILIST };
1391040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    static final String[] MIMETYPES_TEXT_INTENT = new String[] { MIMETYPE_TEXT_INTENT };
1401040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn
1411040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    final Bitmap mIcon;
1429f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
1439f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    final ArrayList<Item> mItems = new ArrayList<Item>();
1449f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
14523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn    /**
14623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * Description of a single item in a ClippedData.
14723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     *
14823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * <p>The types than an individual item can currently contain are:</p>
14923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     *
15023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * <ul>
15123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * <li> Text: a basic string of text.  This is actually a CharSequence,
15223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * so it can be formatted text supported by corresponding Android built-in
15323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * style spans.  (Custom application spans are not supported and will be
15423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * stripped when transporting through the clipboard.)
15523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * <li> Intent: an arbitrary Intent object.  A typical use is the shortcut
15623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * to create when pasting a clipped item on to the home screen.
15723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * <li> Uri: a URI reference.  This may be any URI (such as an http: URI
15823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * representing a bookmark), however it is often a content: URI.  Using
15923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * content provider references as clips like this allows an application to
16023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * share complex or large clips through the standard content provider
16123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * facilities.
16223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * </ul>
16323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     */
1649f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    public static class Item {
1651040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        final CharSequence mText;
1661040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        final Intent mIntent;
1671040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        final Uri mUri;
1689f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
16923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
17023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Create an Item consisting of a single block of (possibly styled) text.
17123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
1729f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        public Item(CharSequence text) {
1739f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mText = text;
1741040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mIntent = null;
1751040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mUri = null;
1769f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
1779f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
17823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
17923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Create an Item consisting of an arbitrary Intent.
18023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
1819f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        public Item(Intent intent) {
1821040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mText = null;
1839f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mIntent = intent;
1841040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mUri = null;
1859f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
1869f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
18723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
18823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Create an Item consisting of an arbitrary URI.
18923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
1909f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        public Item(Uri uri) {
1911040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mText = null;
1921040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mIntent = null;
1939f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mUri = uri;
1949f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
1959f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
19623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
19723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Create a complex Item, containing multiple representations of
19823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * text, intent, and/or URI.
19923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
2009f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        public Item(CharSequence text, Intent intent, Uri uri) {
2019f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mText = text;
2029f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mIntent = intent;
2039f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mUri = uri;
2049f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
2059f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
20623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
20723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Retrieve the raw text contained in this Item.
20823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
2099f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        public CharSequence getText() {
2109f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            return mText;
2119f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
2129f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
21323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
21423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Retrieve the raw Intent contained in this Item.
21523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
2169f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        public Intent getIntent() {
2179f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            return mIntent;
2189f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
2199f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
22023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
22123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Retrieve the raw URI contained in this Item.
22223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
2239f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        public Uri getUri() {
2249f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            return mUri;
2259f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
22623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
22723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
22823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Turn this item into text, regardless of the type of data it
22923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * actually contains.
23023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         *
23123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * <p>The algorithm for deciding what text to return is:
23223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * <ul>
23323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * <li> If {@link #getText} is non-null, return that.
23423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * <li> If {@link #getUri} is non-null, try to retrieve its data
23523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * as a text stream from its content provider.  If this succeeds, copy
23623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * the text into a String and return it.  If it is not a content: URI or
23723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * the content provider does not supply a text representation, return
23823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * the raw URI as a string.
23923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * <li> If {@link #getIntent} is non-null, convert that to an intent:
24023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * URI and returnit.
24123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * <li> Otherwise, return an empty string.
24223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * </ul>
24323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         *
24423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * @param context The caller's Context, from which its ContentResolver
24523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * and other things can be retrieved.
24623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * @return Returns the item's textual representation.
24723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
24823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn//BEGIN_INCLUDE(coerceToText)
24923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        public CharSequence coerceToText(Context context) {
25023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            // If this Item has an explicit textual value, simply return that.
25123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            if (mText != null) {
25223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                return mText;
25323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            }
25423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
25523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            // If this Item has a URI value, try using that.
25623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            if (mUri != null) {
25723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
25823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                // First see if the URI can be opened as a plain text stream
25923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                // (of any sub-type).  If so, this is the best textual
26023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                // representation for it.
26123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                FileInputStream stream = null;
26223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                try {
26323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    // Ask for a stream of the desired type.
26423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    AssetFileDescriptor descr = context.getContentResolver()
26523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                            .openTypedAssetFileDescriptor(mUri, "text/*", null);
26623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    stream = descr.createInputStream();
26723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
26823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
26923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    // Got it...  copy the stream into a local string and return it.
27023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    StringBuilder builder = new StringBuilder(128);
27123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    char[] buffer = new char[8192];
27223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    int len;
27323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    while ((len=reader.read(buffer)) > 0) {
27423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                        builder.append(buffer, 0, len);
27523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    }
27623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    return builder.toString();
27723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
27823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                } catch (FileNotFoundException e) {
27923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    // Unable to open content URI as text...  not really an
28023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    // error, just something to ignore.
28123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
28223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                } catch (IOException e) {
28323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    // Something bad has happened.
28423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    Log.w("ClippedData", "Failure loading text", e);
28523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    return e.toString();
28623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
28723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                } finally {
28823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    if (stream != null) {
28923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                        try {
29023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                            stream.close();
29123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                        } catch (IOException e) {
29223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                        }
29323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    }
29423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                }
29523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
29623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                // If we couldn't open the URI as a stream, then the URI itself
29723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                // probably serves fairly well as a textual representation.
29823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                return mUri.toString();
29923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            }
30023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
30123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            // Finally, if all we have is an Intent, then we can just turn that
30223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            // into text.  Not the most user-friendly thing, but it's something.
30323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            if (mIntent != null) {
30423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                return mIntent.toUri(Intent.URI_INTENT_SCHEME);
30523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            }
30623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
30723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            // Shouldn't get here, but just in case...
30823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            return "";
30923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        }
31023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn//END_INCLUDE(coerceToText)
3119f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
3129f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
3139f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    /**
3149f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn     * Create a new clip.
3159f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn     *
3169f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn     * @param label Label to show to the user describing this clip.
3171040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param mimeTypes An array of MIME types this data is available as.
3189f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn     * @param icon Bitmap providing the user with an iconing representation of
3199f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn     * the clip.
3209f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn     * @param item The contents of the first item in the clip.
3219f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn     */
3221040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    public ClipData(CharSequence label, String[] mimeTypes, Bitmap icon, Item item) {
3231040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        super(label, mimeTypes);
3249f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        if (item == null) {
3259f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            throw new NullPointerException("item is null");
3269f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
3279f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        mIcon = icon;
3289f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        mItems.add(item);
3299f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
3309f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
3311040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    /**
3321040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * Create a new ClipData holding data of the type {@link #MIMETYPE_TEXT_PLAIN}.
3331040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     *
3341040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param label User-visible label for the clip data.
3351040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param icon Iconic representation of the clip data.
3361040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param text The actual text in the clip.
3371040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @return Returns a new ClipData containing the specified data.
3381040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     */
3391040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    static public ClipData newPlainText(CharSequence label, Bitmap icon, CharSequence text) {
3401040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        Item item = new Item(text);
3411040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        return new ClipData(label, MIMETYPES_TEXT_PLAIN, icon, item);
3421040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    }
3431040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn
3441040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    /**
3451040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * Create a new ClipData holding an Intent with MIME type {@link #MIMETYPE_TEXT_INTENT}.
3461040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     *
3471040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param label User-visible label for the clip data.
3481040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param icon Iconic representation of the clip data.
3491040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param intent The actual Intent in the clip.
3501040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @return Returns a new ClipData containing the specified data.
3511040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     */
3521040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    static public ClipData newIntent(CharSequence label, Bitmap icon, Intent intent) {
3531040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        Item item = new Item(intent);
3541040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        return new ClipData(label, MIMETYPES_TEXT_INTENT, icon, item);
3551040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    }
3561040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn
3571040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    /**
3581040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * Create a new ClipData holding a URI.  If the URI is a content: URI,
3591040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * this will query the content provider for the MIME type of its data and
3601040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * use that as the MIME type.  Otherwise, it will use the MIME type
3611040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * {@link #MIMETYPE_TEXT_URILIST}.
3621040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     *
3631040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param resolver ContentResolver used to get information about the URI.
3641040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param label User-visible label for the clip data.
3651040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param icon Iconic representation of the clip data.
3661040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param uri The URI in the clip.
3671040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @return Returns a new ClipData containing the specified data.
3681040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     */
3691040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    static public ClipData newUri(ContentResolver resolver, CharSequence label,
3701040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            Bitmap icon, Uri uri) {
3711040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        Item item = new Item(uri);
3721040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        String[] mimeTypes = null;
3731040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        if ("content".equals(uri.getScheme())) {
3741040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            String realType = resolver.getType(uri);
3751040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mimeTypes = resolver.getStreamTypes(uri, "*/*");
3761040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            if (mimeTypes == null) {
3771040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                if (realType != null) {
3781040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                    mimeTypes = new String[] { realType, MIMETYPE_TEXT_URILIST };
3791040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                }
3801040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            } else {
3811040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                String[] tmp = new String[mimeTypes.length + (realType != null ? 2 : 1)];
3821040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                int i = 0;
3831040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                if (realType != null) {
3841040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                    tmp[0] = realType;
3851040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                    i++;
3861040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                }
3871040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                System.arraycopy(mimeTypes, 0, tmp, i, mimeTypes.length);
3881040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                tmp[i + mimeTypes.length] = MIMETYPE_TEXT_URILIST;
3891040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                mimeTypes = tmp;
3901040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            }
3911040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        }
3921040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        if (mimeTypes == null) {
3931040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mimeTypes = MIMETYPES_TEXT_URILIST;
3941040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        }
3951040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        return new ClipData(label, mimeTypes, icon, item);
3961040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    }
3971040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn
3981040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    /**
3991040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * Create a new ClipData holding an URI with MIME type {@link #MIMETYPE_TEXT_URILIST}.
4001040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * Unlike {@link #newUri(ContentResolver, CharSequence, Bitmap, Uri)}, nothing
4011040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * is inferred about the URI -- if it is a content: URI holding a bitmap,
4021040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * the reported type will still be uri-list.  Use this with care!
4031040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     *
4041040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param label User-visible label for the clip data.
4051040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param icon Iconic representation of the clip data.
4061040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param uri The URI in the clip.
4071040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @return Returns a new ClipData containing the specified data.
4081040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     */
4091040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    static public ClipData newRawUri(CharSequence label, Bitmap icon, Uri uri) {
4101040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        Item item = new Item(uri);
4111040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        return new ClipData(label, MIMETYPES_TEXT_URILIST, icon, item);
4121040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    }
4131040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn
4149f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    public void addItem(Item item) {
4159f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        if (item == null) {
4169f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            throw new NullPointerException("item is null");
4179f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
4189f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        mItems.add(item);
4199f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
4209f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
4219f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    public Bitmap getIcon() {
4229f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        return mIcon;
4239f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
4249f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
4259f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    public int getItemCount() {
4269f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        return mItems.size();
4279f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
4289f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
4299f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    public Item getItem(int index) {
4309f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        return mItems.get(index);
4319f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
4329f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
4339f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    @Override
4349f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    public int describeContents() {
4359f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        return 0;
4369f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
4379f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
4389f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    @Override
4399f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    public void writeToParcel(Parcel dest, int flags) {
4401040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        super.writeToParcel(dest, flags);
4419f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        if (mIcon != null) {
4429f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            dest.writeInt(1);
4439f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mIcon.writeToParcel(dest, flags);
4449f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        } else {
4459f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            dest.writeInt(0);
4469f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
4479f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        final int N = mItems.size();
4489f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        dest.writeInt(N);
4499f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        for (int i=0; i<N; i++) {
4509f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            Item item = mItems.get(i);
4519f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            TextUtils.writeToParcel(item.mText, dest, flags);
4529f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            if (item.mIntent != null) {
4539f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn                dest.writeInt(1);
4549f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn                item.mIntent.writeToParcel(dest, flags);
4559f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            } else {
4569f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn                dest.writeInt(0);
4579f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            }
4589f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            if (item.mUri != null) {
4599f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn                dest.writeInt(1);
4609f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn                item.mUri.writeToParcel(dest, flags);
4619f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            } else {
4629f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn                dest.writeInt(0);
4639f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            }
4649f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
4659f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
4669f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
4671040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    ClipData(Parcel in) {
4681040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        super(in);
4699f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        if (in.readInt() != 0) {
4709f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mIcon = Bitmap.CREATOR.createFromParcel(in);
4711040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        } else {
4721040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mIcon = null;
4739f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
4749f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        final int N = in.readInt();
4759f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        for (int i=0; i<N; i++) {
4769f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            CharSequence text = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
4779f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            Intent intent = in.readInt() != 0 ? Intent.CREATOR.createFromParcel(in) : null;
4789f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            Uri uri = in.readInt() != 0 ? Uri.CREATOR.createFromParcel(in) : null;
4799f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mItems.add(new Item(text, intent, uri));
4809f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
4819f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
4829f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
4831040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    public static final Parcelable.Creator<ClipData> CREATOR =
4841040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        new Parcelable.Creator<ClipData>() {
4859f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
4861040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            public ClipData createFromParcel(Parcel source) {
4871040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                return new ClipData(source);
4889f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            }
4899f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
4901040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            public ClipData[] newArray(int size) {
4911040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                return new ClipData[size];
4929f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            }
4939f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        };
4949f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn}
495