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
19d85fc72fb810858f7502e7e7f1bad53e1bf03eddNicolas Prevotimport static android.content.ContentProvider.maybeAddUserId;
2023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackbornimport android.content.res.AssetFileDescriptor;
219f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackbornimport android.graphics.Bitmap;
229f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackbornimport android.net.Uri;
239f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackbornimport android.os.Parcel;
249f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackbornimport android.os.Parcelable;
25a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkeyimport android.os.StrictMode;
26acb69bb909d098cea284df47d794c17171d84c91Dianne Hackbornimport android.text.Html;
27acb69bb909d098cea284df47d794c17171d84c91Dianne Hackbornimport android.text.Spannable;
28acb69bb909d098cea284df47d794c17171d84c91Dianne Hackbornimport android.text.SpannableStringBuilder;
29acb69bb909d098cea284df47d794c17171d84c91Dianne Hackbornimport android.text.Spanned;
309f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackbornimport android.text.TextUtils;
31acb69bb909d098cea284df47d794c17171d84c91Dianne Hackbornimport android.text.style.URLSpan;
3223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackbornimport android.util.Log;
339f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
3423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackbornimport java.io.FileInputStream;
3523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackbornimport java.io.FileNotFoundException;
3623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackbornimport java.io.IOException;
3723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackbornimport java.io.InputStreamReader;
389f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackbornimport java.util.ArrayList;
399f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
409f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn/**
419f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * Representation of a clipped data on the clipboard.
429f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn *
439f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * <p>ClippedData is a complex type containing one or Item instances,
449f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * each of which can hold one or more representations of an item of data.
459f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn * For display to the user, it also has a label and iconic representation.</p>
469f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn *
47f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * <p>A ClipData contains a {@link ClipDescription}, which describes
48f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * important meta-data about the clip.  In particular, its
49f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * {@link ClipDescription#getMimeType(int) getDescription().getMimeType(int)}
501040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * must return correct MIME type(s) describing the data in the clip.  For help
511040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * in correctly constructing a clip with the correct MIME type, use
52327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn * {@link #newPlainText(CharSequence, CharSequence)},
53327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn * {@link #newUri(ContentResolver, CharSequence, Uri)}, and
54327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn * {@link #newIntent(CharSequence, Intent)}.
551040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn *
5623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>Each Item instance can be one of three main classes of data: a simple
5723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * CharSequence of text, a single Intent object, or a Uri.  See {@link Item}
5823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * for more details.
5923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
603aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <div class="special reference">
613aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <h3>Developer Guides</h3>
623aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <p>For more information about using the clipboard framework, read the
633aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <a href="{@docRoot}guide/topics/clipboard/copy-paste.html">Copy and Paste</a>
643aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * developer guide.</p>
653aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * </div>
663aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez *
6723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <a name="ImplementingPaste"></a>
6823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <h3>Implementing Paste or Drop</h3>
6923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
7023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>To implement a paste or drop of a ClippedData object into an application,
7123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * the application must correctly interpret the data for its use.  If the {@link Item}
7223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * it contains is simple text or an Intent, there is little to be done: text
7323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * can only be interpreted as text, and an Intent will typically be used for
7423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * creating shortcuts (such as placing icons on the home screen) or other
7523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * actions.
7623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
7723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>If all you want is the textual representation of the clipped data, you
7823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * can use the convenience method {@link Item#coerceToText Item.coerceToText}.
791040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * In this case there is generally no need to worry about the MIME types
80f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn * reported by {@link ClipDescription#getMimeType(int) getDescription().getMimeType(int)},
818f8a11b7fa26e603519131001ab46596aa30ba1aNewton Allen * since any clip item can always be converted to a string.
8223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
8323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>More complicated exchanges will be done through URIs, in particular
8423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * "content:" URIs.  A content URI allows the recipient of a ClippedData item
8523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * to interact closely with the ContentProvider holding the data in order to
861040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * negotiate the transfer of that data.  The clip must also be filled in with
87327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn * the available MIME types; {@link #newUri(ContentResolver, CharSequence, Uri)}
881040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn * will take care of correctly doing this.
8923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
9023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>For example, here is the paste function of a simple NotePad application.
9123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * When retrieving the data from the clipboard, it can do either two things:
9223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * if the clipboard contains a URI reference to an existing note, it copies
9323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * the entire structure of the note into a new note; otherwise, it simply
9423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * coerces the clip into text and uses that as the new note's contents.
9523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
9623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * {@sample development/samples/NotePad/src/com/example/android/notepad/NoteEditor.java
9723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *      paste}
9823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
9923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>In many cases an application can paste various types of streams of data.  For
10023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * example, an e-mail application may want to allow the user to paste an image
10123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * or other binary data as an attachment.  This is accomplished through the
10223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * ContentResolver {@link ContentResolver#getStreamTypes(Uri, String)} and
10323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * {@link ContentResolver#openTypedAssetFileDescriptor(Uri, String, android.os.Bundle)}
10423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * methods.  These allow a client to discover the type(s) of data that a particular
10523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * content URI can make available as a stream and retrieve the stream of data.
10623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
10723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>For example, the implementation of {@link Item#coerceToText Item.coerceToText}
10823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * itself uses this to try to retrieve a URI clip as a stream of text:
10923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
110f6d952bbc85706031e1ad29ec389c1e02cfff433Dianne Hackborn * {@sample frameworks/base/core/java/android/content/ClipData.java coerceToText}
11123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
11223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <a name="ImplementingCopy"></a>
11323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <h3>Implementing Copy or Drag</h3>
11423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
11523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>To be the source of a clip, the application must construct a ClippedData
11623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * object that any recipient can interpret best for their context.  If the clip
11723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * is to contain a simple text, Intent, or URI, this is easy: an {@link Item}
11823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * containing the appropriate data type can be constructed and used.
11923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
12023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>More complicated data types require the implementation of support in
12123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * a ContentProvider for describing and generating the data for the recipient.
12223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * A common scenario is one where an application places on the clipboard the
12323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * content: URI of an object that the user has copied, with the data at that
12423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * URI consisting of a complicated structure that only other applications with
12523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * direct knowledge of the structure can use.
12623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
12723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>For applications that do not have intrinsic knowledge of the data structure,
12823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * the content provider holding it can make the data available as an arbitrary
12923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * number of types of data streams.  This is done by implementing the
13023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * ContentProvider {@link ContentProvider#getStreamTypes(Uri, String)} and
13123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * {@link ContentProvider#openTypedAssetFile(Uri, String, android.os.Bundle)}
13223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * methods.
13323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
13423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>Going back to our simple NotePad application, this is the implementation
13523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * it may have to convert a single note URI (consisting of a title and the note
13623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * text) into a stream of plain text data.
13723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
13823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * {@sample development/samples/NotePad/src/com/example/android/notepad/NotePadProvider.java
13923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *      stream}
14023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
14123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>The copy operation in our NotePad application is now just a simple matter
14223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * of making a clip containing the URI of the note being copied:
14323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
14423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * {@sample development/samples/NotePad/src/com/example/android/notepad/NotesList.java
14523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *      copy}
14623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn *
14723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * <p>Note if a paste operation needs this clip as text (for example to paste
14823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * into an editor), then {@link Item#coerceToText(Context)} will ask the content
14923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn * provider for the clip URI as text and successfully paste the entire note.
1509f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn */
151f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackbornpublic class ClipData implements Parcelable {
152f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn    static final String[] MIMETYPES_TEXT_PLAIN = new String[] {
153f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn        ClipDescription.MIMETYPE_TEXT_PLAIN };
154acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn    static final String[] MIMETYPES_TEXT_HTML = new String[] {
155acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        ClipDescription.MIMETYPE_TEXT_HTML };
156f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn    static final String[] MIMETYPES_TEXT_URILIST = new String[] {
157f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn        ClipDescription.MIMETYPE_TEXT_URILIST };
158f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn    static final String[] MIMETYPES_TEXT_INTENT = new String[] {
159f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn        ClipDescription.MIMETYPE_TEXT_INTENT };
160f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn
161f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn    final ClipDescription mClipDescription;
162f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn
1631040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    final Bitmap mIcon;
1649f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
16521c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn    final ArrayList<Item> mItems;
1669f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
16723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn    /**
16823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * Description of a single item in a ClippedData.
16923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     *
17023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * <p>The types than an individual item can currently contain are:</p>
17123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     *
17223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * <ul>
17323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * <li> Text: a basic string of text.  This is actually a CharSequence,
17423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * so it can be formatted text supported by corresponding Android built-in
17523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * style spans.  (Custom application spans are not supported and will be
17623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * stripped when transporting through the clipboard.)
17723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * <li> Intent: an arbitrary Intent object.  A typical use is the shortcut
17823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * to create when pasting a clipped item on to the home screen.
17923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * <li> Uri: a URI reference.  This may be any URI (such as an http: URI
18023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * representing a bookmark), however it is often a content: URI.  Using
18123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * content provider references as clips like this allows an application to
18223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * share complex or large clips through the standard content provider
18323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * facilities.
18423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * </ul>
18523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     */
1869f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    public static class Item {
1871040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        final CharSequence mText;
188acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        final String mHtmlText;
1891040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        final Intent mIntent;
190d85fc72fb810858f7502e7e7f1bad53e1bf03eddNicolas Prevot        Uri mUri;
1919f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
19223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
19323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Create an Item consisting of a single block of (possibly styled) text.
19423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
1959f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        public Item(CharSequence text) {
1969f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mText = text;
197acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            mHtmlText = null;
198acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            mIntent = null;
199acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            mUri = null;
200acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        }
201acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn
202acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        /**
203acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * Create an Item consisting of a single block of (possibly styled) text,
204acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * with an alternative HTML formatted representation.  You <em>must</em>
205acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * supply a plain text representation in addition to HTML text; coercion
206acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * will not be done from HTML formated text into plain text.
207acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         */
208acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        public Item(CharSequence text, String htmlText) {
209acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            mText = text;
210acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            mHtmlText = htmlText;
2111040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mIntent = null;
2121040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mUri = null;
2139f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
2149f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
21523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
21623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Create an Item consisting of an arbitrary Intent.
21723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
2189f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        public Item(Intent intent) {
2191040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mText = null;
220acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            mHtmlText = null;
2219f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mIntent = intent;
2221040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mUri = null;
2239f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
2249f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
22523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
22623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Create an Item consisting of an arbitrary URI.
22723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
2289f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        public Item(Uri uri) {
2291040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mText = null;
230acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            mHtmlText = null;
2311040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mIntent = null;
2329f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mUri = uri;
2339f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
2349f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
23523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
23623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Create a complex Item, containing multiple representations of
237acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * text, Intent, and/or URI.
23823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
2399f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        public Item(CharSequence text, Intent intent, Uri uri) {
2409f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mText = text;
241acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            mHtmlText = null;
242acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            mIntent = intent;
243acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            mUri = uri;
244acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        }
245acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn
246acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        /**
247acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * Create a complex Item, containing multiple representations of
248acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * text, HTML text, Intent, and/or URI.  If providing HTML text, you
249acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * <em>must</em> supply a plain text representation as well; coercion
250acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * will not be done from HTML formated text into plain text.
251acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         */
252acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        public Item(CharSequence text, String htmlText, Intent intent, Uri uri) {
253acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            if (htmlText != null && text == null) {
254acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                throw new IllegalArgumentException(
255acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        "Plain text must be supplied if HTML text is supplied");
256acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            }
257acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            mText = text;
258acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            mHtmlText = htmlText;
2599f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mIntent = intent;
2609f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mUri = uri;
2619f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
2629f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
26323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
26423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Retrieve the raw text contained in this Item.
26523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
2669f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        public CharSequence getText() {
2679f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            return mText;
2689f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
2699f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
27023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
271acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * Retrieve the raw HTML text contained in this Item.
272acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         */
273acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        public String getHtmlText() {
274acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            return mHtmlText;
275acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        }
276acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn
277acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        /**
27823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Retrieve the raw Intent contained in this Item.
27923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
2809f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        public Intent getIntent() {
2819f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            return mIntent;
2829f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
2839f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
28423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
28523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Retrieve the raw URI contained in this Item.
28623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
2879f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        public Uri getUri() {
2889f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            return mUri;
2899f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
29023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
29123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        /**
29223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * Turn this item into text, regardless of the type of data it
29323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * actually contains.
29423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         *
29523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * <p>The algorithm for deciding what text to return is:
29623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * <ul>
29723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * <li> If {@link #getText} is non-null, return that.
29823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * <li> If {@link #getUri} is non-null, try to retrieve its data
29923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * as a text stream from its content provider.  If this succeeds, copy
30023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * the text into a String and return it.  If it is not a content: URI or
30123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * the content provider does not supply a text representation, return
30223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * the raw URI as a string.
30323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * <li> If {@link #getIntent} is non-null, convert that to an intent:
304acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * URI and return it.
30523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * <li> Otherwise, return an empty string.
30623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * </ul>
30723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         *
30823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * @param context The caller's Context, from which its ContentResolver
30923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * and other things can be retrieved.
31023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         * @return Returns the item's textual representation.
31123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn         */
31223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn//BEGIN_INCLUDE(coerceToText)
31323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        public CharSequence coerceToText(Context context) {
31423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            // If this Item has an explicit textual value, simply return that.
315acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            CharSequence text = getText();
316acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            if (text != null) {
317acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                return text;
31823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            }
31923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
32023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            // If this Item has a URI value, try using that.
321acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            Uri uri = getUri();
322acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            if (uri != null) {
32323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
32423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                // First see if the URI can be opened as a plain text stream
32523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                // (of any sub-type).  If so, this is the best textual
32623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                // representation for it.
32723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                FileInputStream stream = null;
32823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                try {
32923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    // Ask for a stream of the desired type.
33023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    AssetFileDescriptor descr = context.getContentResolver()
331acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                            .openTypedAssetFileDescriptor(uri, "text/*", null);
33223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    stream = descr.createInputStream();
33323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
33423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
33523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    // Got it...  copy the stream into a local string and return it.
33623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    StringBuilder builder = new StringBuilder(128);
33723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    char[] buffer = new char[8192];
33823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    int len;
33923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    while ((len=reader.read(buffer)) > 0) {
34023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                        builder.append(buffer, 0, len);
34123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    }
34223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    return builder.toString();
34323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
34423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                } catch (FileNotFoundException e) {
34523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    // Unable to open content URI as text...  not really an
34623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    // error, just something to ignore.
34723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
34823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                } catch (IOException e) {
34923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    // Something bad has happened.
35023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    Log.w("ClippedData", "Failure loading text", e);
35123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    return e.toString();
35223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
35323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                } finally {
35423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    if (stream != null) {
35523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                        try {
35623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                            stream.close();
35723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                        } catch (IOException e) {
35823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                        }
35923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                    }
36023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                }
36123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
36223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                // If we couldn't open the URI as a stream, then the URI itself
36323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn                // probably serves fairly well as a textual representation.
364acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                return uri.toString();
36523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            }
36623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
36723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            // Finally, if all we have is an Intent, then we can just turn that
36823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            // into text.  Not the most user-friendly thing, but it's something.
369acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            Intent intent = getIntent();
370acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            if (intent != null) {
371acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                return intent.toUri(Intent.URI_INTENT_SCHEME);
37223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            }
37323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
37423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            // Shouldn't get here, but just in case...
37523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            return "";
37623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn        }
37723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn//END_INCLUDE(coerceToText)
37821c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn
379acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        /**
380acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * Like {@link #coerceToHtmlText(Context)}, but any text that would
381acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * be returned as HTML formatting will be returned as text with
382acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * style spans.
383acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * @param context The caller's Context, from which its ContentResolver
384acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * and other things can be retrieved.
385acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * @return Returns the item's textual representation.
386acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         */
387acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        public CharSequence coerceToStyledText(Context context) {
388acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            CharSequence text = getText();
389acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            if (text instanceof Spanned) {
390acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                return text;
391acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            }
392acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            String htmlText = getHtmlText();
393acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            if (htmlText != null) {
394acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                try {
395acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                    CharSequence newText = Html.fromHtml(htmlText);
396acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                    if (newText != null) {
397acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        return newText;
398acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                    }
399acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                } catch (RuntimeException e) {
400acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                    // If anything bad happens, we'll fall back on the plain text.
401acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                }
402acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            }
403acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn
404acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            if (text != null) {
405acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                return text;
406acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            }
407acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            return coerceToHtmlOrStyledText(context, true);
408acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        }
409acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn
410acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        /**
411acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * Turn this item into HTML text, regardless of the type of data it
412acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * actually contains.
413acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         *
414acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * <p>The algorithm for deciding what text to return is:
415acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * <ul>
416acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * <li> If {@link #getHtmlText} is non-null, return that.
417acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * <li> If {@link #getText} is non-null, return that, converting to
418acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * valid HTML text.  If this text contains style spans,
419acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * {@link Html#toHtml(Spanned) Html.toHtml(Spanned)} is used to
420acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * convert them to HTML formatting.
421acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * <li> If {@link #getUri} is non-null, try to retrieve its data
422acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * as a text stream from its content provider.  If the provider can
423acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * supply text/html data, that will be preferred and returned as-is.
424acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * Otherwise, any text/* data will be returned and escaped to HTML.
425acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * If it is not a content: URI or the content provider does not supply
426acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * a text representation, HTML text containing a link to the URI
427acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * will be returned.
428acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * <li> If {@link #getIntent} is non-null, convert that to an intent:
429acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * URI and return as an HTML link.
430acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * <li> Otherwise, return an empty string.
431acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * </ul>
432acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         *
433acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * @param context The caller's Context, from which its ContentResolver
434acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * and other things can be retrieved.
435acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         * @return Returns the item's representation as HTML text.
436acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn         */
437acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        public String coerceToHtmlText(Context context) {
438acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            // If the item has an explicit HTML value, simply return that.
439acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            String htmlText = getHtmlText();
440acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            if (htmlText != null) {
441acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                return htmlText;
442acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            }
443acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn
444acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            // If this Item has a plain text value, return it as HTML.
445acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            CharSequence text = getText();
446acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            if (text != null) {
447acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                if (text instanceof Spanned) {
448acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                    return Html.toHtml((Spanned)text);
449acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                }
450acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                return Html.escapeHtml(text);
451acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            }
452acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn
453acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            text = coerceToHtmlOrStyledText(context, false);
454acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            return text != null ? text.toString() : null;
455acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        }
456acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn
457acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        private CharSequence coerceToHtmlOrStyledText(Context context, boolean styled) {
458acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            // If this Item has a URI value, try using that.
459acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            if (mUri != null) {
460acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn
461acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                // Check to see what data representations the content
462acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                // provider supports.  We would like HTML text, but if that
463acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                // is not possible we'll live with plan text.
464acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                String[] types = context.getContentResolver().getStreamTypes(mUri, "text/*");
465acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                boolean hasHtml = false;
466acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                boolean hasText = false;
467acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                if (types != null) {
468acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                    for (String type : types) {
469acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        if ("text/html".equals(type)) {
470acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                            hasHtml = true;
471acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        } else if (type.startsWith("text/")) {
472acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                            hasText = true;
473acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        }
474acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                    }
475acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                }
476acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn
477acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                // If the provider can serve data we can use, open and load it.
478acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                if (hasHtml || hasText) {
479acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                    FileInputStream stream = null;
480acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                    try {
481acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        // Ask for a stream of the desired type.
482acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        AssetFileDescriptor descr = context.getContentResolver()
483acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                                .openTypedAssetFileDescriptor(mUri,
484acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                                        hasHtml ? "text/html" : "text/plain", null);
485acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        stream = descr.createInputStream();
486acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
487acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn
488acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        // Got it...  copy the stream into a local string and return it.
489acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        StringBuilder builder = new StringBuilder(128);
490acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        char[] buffer = new char[8192];
491acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        int len;
492acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        while ((len=reader.read(buffer)) > 0) {
493acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                            builder.append(buffer, 0, len);
494acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        }
495acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        String text = builder.toString();
496acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        if (hasHtml) {
497acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                            if (styled) {
498acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                                // We loaded HTML formatted text and the caller
499acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                                // want styled text, convert it.
500acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                                try {
501acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                                    CharSequence newText = Html.fromHtml(text);
502acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                                    return newText != null ? newText : text;
503acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                                } catch (RuntimeException e) {
504acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                                    return text;
505acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                                }
506acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                            } else {
507acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                                // We loaded HTML formatted text and that is what
508acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                                // the caller wants, just return it.
509acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                                return text.toString();
510acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                            }
511acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        }
512acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        if (styled) {
513acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                            // We loaded plain text and the caller wants styled
514acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                            // text, that is all we have so return it.
515acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                            return text;
516acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        } else {
517acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                            // We loaded plain text and the caller wants HTML
518acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                            // text, escape it for HTML.
519acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                            return Html.escapeHtml(text);
520acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        }
521acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn
522acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                    } catch (FileNotFoundException e) {
523acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        // Unable to open content URI as text...  not really an
524acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        // error, just something to ignore.
525acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn
526acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                    } catch (IOException e) {
527acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        // Something bad has happened.
528acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        Log.w("ClippedData", "Failure loading text", e);
529acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        return Html.escapeHtml(e.toString());
530acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn
531acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                    } finally {
532acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        if (stream != null) {
533acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                            try {
534acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                                stream.close();
535acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                            } catch (IOException e) {
536acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                            }
537acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                        }
538acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                    }
539acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                }
540acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn
541acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                // If we couldn't open the URI as a stream, then we can build
542acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                // some HTML text with the URI itself.
543acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                // probably serves fairly well as a textual representation.
544acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                if (styled) {
545acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                    return uriToStyledText(mUri.toString());
546acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                } else {
547acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                    return uriToHtml(mUri.toString());
548acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                }
549acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            }
550acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn
551acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            // Finally, if all we have is an Intent, then we can just turn that
552acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            // into text.  Not the most user-friendly thing, but it's something.
553acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            if (mIntent != null) {
554acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                if (styled) {
555acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                    return uriToStyledText(mIntent.toUri(Intent.URI_INTENT_SCHEME));
556acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                } else {
557acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                    return uriToHtml(mIntent.toUri(Intent.URI_INTENT_SCHEME));
558acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                }
559acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            }
560acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn
561acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            // Shouldn't get here, but just in case...
562acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            return "";
563acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        }
564acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn
565acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        private String uriToHtml(String uri) {
566acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            StringBuilder builder = new StringBuilder(256);
567acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            builder.append("<a href=\"");
568c92db391379cc19738de8bb5008ed619cb049ebeNick Kralevich            builder.append(Html.escapeHtml(uri));
569acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            builder.append("\">");
570acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            builder.append(Html.escapeHtml(uri));
571acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            builder.append("</a>");
572acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            return builder.toString();
573acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        }
574acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn
575acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        private CharSequence uriToStyledText(String uri) {
576acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            SpannableStringBuilder builder = new SpannableStringBuilder();
577acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            builder.append(uri);
578acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            builder.setSpan(new URLSpan(uri), 0, builder.length(),
579acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
580acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            return builder;
581acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        }
582acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn
58321c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        @Override
58421c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        public String toString() {
58521c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            StringBuilder b = new StringBuilder(128);
58621c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn
58721c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            b.append("ClipData.Item { ");
58821c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            toShortString(b);
58921c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            b.append(" }");
59021c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn
59121c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            return b.toString();
59221c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        }
59321c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn
59421c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        /** @hide */
59521c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        public void toShortString(StringBuilder b) {
596acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            if (mHtmlText != null) {
597acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                b.append("H:");
598acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn                b.append(mHtmlText);
599acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            } else if (mText != null) {
60021c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn                b.append("T:");
60121c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn                b.append(mText);
60221c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            } else if (mUri != null) {
60321c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn                b.append("U:");
60421c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn                b.append(mUri);
60521c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            } else if (mIntent != null) {
60621c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn                b.append("I:");
60721c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn                mIntent.toShortString(b, true, true, true, true);
60821c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            } else {
60921c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn                b.append("NULL");
61021c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            }
61121c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        }
6129f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
6139f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
6149f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    /**
6159f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn     * Create a new clip.
6169f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn     *
6179f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn     * @param label Label to show to the user describing this clip.
6181040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param mimeTypes An array of MIME types this data is available as.
6199f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn     * @param item The contents of the first item in the clip.
6209f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn     */
621327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    public ClipData(CharSequence label, String[] mimeTypes, Item item) {
622f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn        mClipDescription = new ClipDescription(label, mimeTypes);
623f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn        if (item == null) {
624f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn            throw new NullPointerException("item is null");
625f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn        }
626327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        mIcon = null;
62721c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        mItems = new ArrayList<Item>();
628f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn        mItems.add(item);
629f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn    }
630f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn
631f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn    /**
632f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * Create a new clip.
633f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     *
634f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * @param description The ClipDescription describing the clip contents.
635f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * @param item The contents of the first item in the clip.
636f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     */
637327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    public ClipData(ClipDescription description, Item item) {
638f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn        mClipDescription = description;
6399f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        if (item == null) {
6409f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            throw new NullPointerException("item is null");
6419f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
642327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        mIcon = null;
64321c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        mItems = new ArrayList<Item>();
6449f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        mItems.add(item);
6459f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
6469f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
6471040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    /**
64821c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn     * Create a new clip that is a copy of another clip.  This does a deep-copy
64921c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn     * of all items in the clip.
65021c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn     *
65121c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn     * @param other The existing ClipData that is to be copied.
65221c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn     */
65321c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn    public ClipData(ClipData other) {
65421c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        mClipDescription = other.mClipDescription;
65521c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        mIcon = other.mIcon;
65621c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        mItems = new ArrayList<Item>(other.mItems);
65721c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn    }
65821c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn
65921c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn    /**
660f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * Create a new ClipData holding data of the type
661f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * {@link ClipDescription#MIMETYPE_TEXT_PLAIN}.
6621040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     *
6631040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param label User-visible label for the clip data.
6641040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param text The actual text in the clip.
6651040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @return Returns a new ClipData containing the specified data.
6661040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     */
667327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    static public ClipData newPlainText(CharSequence label, CharSequence text) {
6681040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        Item item = new Item(text);
669327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        return new ClipData(label, MIMETYPES_TEXT_PLAIN, item);
670327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    }
671327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn
6721040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    /**
673acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn     * Create a new ClipData holding data of the type
674acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn     * {@link ClipDescription#MIMETYPE_TEXT_HTML}.
675acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn     *
676acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn     * @param label User-visible label for the clip data.
677acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn     * @param text The text of clip as plain text, for receivers that don't
678acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn     * handle HTML.  This is required.
679acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn     * @param htmlText The actual HTML text in the clip.
680acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn     * @return Returns a new ClipData containing the specified data.
681acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn     */
682acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn    static public ClipData newHtmlText(CharSequence label, CharSequence text,
683acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            String htmlText) {
684acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        Item item = new Item(text, htmlText);
685acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn        return new ClipData(label, MIMETYPES_TEXT_HTML, item);
686acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn    }
687acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn
688acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn    /**
689f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * Create a new ClipData holding an Intent with MIME type
690f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * {@link ClipDescription#MIMETYPE_TEXT_INTENT}.
6911040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     *
6921040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param label User-visible label for the clip data.
6931040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param intent The actual Intent in the clip.
6941040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @return Returns a new ClipData containing the specified data.
6951040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     */
696327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    static public ClipData newIntent(CharSequence label, Intent intent) {
6971040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        Item item = new Item(intent);
698327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        return new ClipData(label, MIMETYPES_TEXT_INTENT, item);
6991040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    }
7001040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn
7011040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    /**
7021040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * Create a new ClipData holding a URI.  If the URI is a content: URI,
7031040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * this will query the content provider for the MIME type of its data and
7041040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * use that as the MIME type.  Otherwise, it will use the MIME type
705f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * {@link ClipDescription#MIMETYPE_TEXT_URILIST}.
7061040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     *
7071040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param resolver ContentResolver used to get information about the URI.
7081040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param label User-visible label for the clip data.
7091040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param uri The URI in the clip.
7101040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @return Returns a new ClipData containing the specified data.
7111040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     */
7121040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    static public ClipData newUri(ContentResolver resolver, CharSequence label,
713327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn            Uri uri) {
7141040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        Item item = new Item(uri);
7151040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        String[] mimeTypes = null;
7161040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        if ("content".equals(uri.getScheme())) {
7171040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            String realType = resolver.getType(uri);
7181040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mimeTypes = resolver.getStreamTypes(uri, "*/*");
7191040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            if (mimeTypes == null) {
7201040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                if (realType != null) {
721f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn                    mimeTypes = new String[] { realType, ClipDescription.MIMETYPE_TEXT_URILIST };
7221040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                }
7231040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            } else {
7241040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                String[] tmp = new String[mimeTypes.length + (realType != null ? 2 : 1)];
7251040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                int i = 0;
7261040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                if (realType != null) {
7271040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                    tmp[0] = realType;
7281040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                    i++;
7291040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                }
7301040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                System.arraycopy(mimeTypes, 0, tmp, i, mimeTypes.length);
731f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn                tmp[i + mimeTypes.length] = ClipDescription.MIMETYPE_TEXT_URILIST;
7321040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                mimeTypes = tmp;
7331040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            }
7341040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        }
7351040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        if (mimeTypes == null) {
7361040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mimeTypes = MIMETYPES_TEXT_URILIST;
7371040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        }
738327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        return new ClipData(label, mimeTypes, item);
7391040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    }
7401040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn
7411040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    /**
742f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * Create a new ClipData holding an URI with MIME type
743f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * {@link ClipDescription#MIMETYPE_TEXT_URILIST}.
744327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     * Unlike {@link #newUri(ContentResolver, CharSequence, Uri)}, nothing
7451040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * is inferred about the URI -- if it is a content: URI holding a bitmap,
7461040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * the reported type will still be uri-list.  Use this with care!
7471040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     *
7481040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param label User-visible label for the clip data.
7491040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @param uri The URI in the clip.
7501040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     * @return Returns a new ClipData containing the specified data.
7511040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn     */
752327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    static public ClipData newRawUri(CharSequence label, Uri uri) {
7531040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        Item item = new Item(uri);
754327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        return new ClipData(label, MIMETYPES_TEXT_URILIST, item);
7551040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    }
7561040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn
757f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn    /**
758f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * Return the {@link ClipDescription} associated with this data, describing
759f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     * what it contains.
760f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn     */
761f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn    public ClipDescription getDescription() {
762f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn        return mClipDescription;
763f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn    }
764f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn
765327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    /**
766327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     * Add a new Item to the overall ClipData container.
767327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     */
7689f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    public void addItem(Item item) {
7699f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        if (item == null) {
7709f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            throw new NullPointerException("item is null");
7719f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
7729f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        mItems.add(item);
7739f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
7749f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
775327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    /** @hide */
7769f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    public Bitmap getIcon() {
7779f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        return mIcon;
7789f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
7799f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
780327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    /**
781327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     * Return the number of items in the clip data.
782327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     */
7839f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    public int getItemCount() {
7849f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        return mItems.size();
7859f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
7869f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
787327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    /**
788327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     * Return a single item inside of the clip data.  The index can range
789327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     * from 0 to {@link #getItemCount()}-1.
790327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     */
791327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    public Item getItemAt(int index) {
7929f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        return mItems.get(index);
7939f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
7949f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
795a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey    /**
796a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey     * Prepare this {@link ClipData} to leave an app process.
797a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey     *
798a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey     * @hide
799a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey     */
800a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey    public void prepareToLeaveProcess() {
801a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey        final int size = mItems.size();
802a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey        for (int i = 0; i < size; i++) {
803a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey            final Item item = mItems.get(i);
804a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey            if (item.mIntent != null) {
805a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey                item.mIntent.prepareToLeaveProcess();
806a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey            }
807a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey            if (item.mUri != null && StrictMode.vmFileUriExposureEnabled()) {
808a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey                item.mUri.checkFileUriExposed("ClipData.Item.getUri()");
809a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey            }
810a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey        }
811a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey    }
812a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey
813d1c99b1fe85ed03261a77a14ae52e7fa3a6e523aNicolas Prevot    /** @hide */
814d1c99b1fe85ed03261a77a14ae52e7fa3a6e523aNicolas Prevot    public void fixUris(int contentUserHint) {
815d85fc72fb810858f7502e7e7f1bad53e1bf03eddNicolas Prevot        final int size = mItems.size();
816d85fc72fb810858f7502e7e7f1bad53e1bf03eddNicolas Prevot        for (int i = 0; i < size; i++) {
817d85fc72fb810858f7502e7e7f1bad53e1bf03eddNicolas Prevot            final Item item = mItems.get(i);
818d85fc72fb810858f7502e7e7f1bad53e1bf03eddNicolas Prevot            if (item.mIntent != null) {
819d1c99b1fe85ed03261a77a14ae52e7fa3a6e523aNicolas Prevot                item.mIntent.fixUris(contentUserHint);
820d85fc72fb810858f7502e7e7f1bad53e1bf03eddNicolas Prevot            }
821d85fc72fb810858f7502e7e7f1bad53e1bf03eddNicolas Prevot            if (item.mUri != null) {
822d1c99b1fe85ed03261a77a14ae52e7fa3a6e523aNicolas Prevot                item.mUri = maybeAddUserId(item.mUri, contentUserHint);
823d85fc72fb810858f7502e7e7f1bad53e1bf03eddNicolas Prevot            }
824d85fc72fb810858f7502e7e7f1bad53e1bf03eddNicolas Prevot        }
825d85fc72fb810858f7502e7e7f1bad53e1bf03eddNicolas Prevot    }
826d85fc72fb810858f7502e7e7f1bad53e1bf03eddNicolas Prevot
827f1939901d2ed0480069f0b23be64f122fce93995Nicolas Prevot    /**
828f1939901d2ed0480069f0b23be64f122fce93995Nicolas Prevot     * Only fixing the data field of the intents
829f1939901d2ed0480069f0b23be64f122fce93995Nicolas Prevot     * @hide
830f1939901d2ed0480069f0b23be64f122fce93995Nicolas Prevot     */
831f1939901d2ed0480069f0b23be64f122fce93995Nicolas Prevot    public void fixUrisLight(int contentUserHint) {
832f1939901d2ed0480069f0b23be64f122fce93995Nicolas Prevot        final int size = mItems.size();
833f1939901d2ed0480069f0b23be64f122fce93995Nicolas Prevot        for (int i = 0; i < size; i++) {
834f1939901d2ed0480069f0b23be64f122fce93995Nicolas Prevot            final Item item = mItems.get(i);
835f1939901d2ed0480069f0b23be64f122fce93995Nicolas Prevot            if (item.mIntent != null) {
836f1939901d2ed0480069f0b23be64f122fce93995Nicolas Prevot                Uri data = item.mIntent.getData();
837f1939901d2ed0480069f0b23be64f122fce93995Nicolas Prevot                if (data != null) {
838f1939901d2ed0480069f0b23be64f122fce93995Nicolas Prevot                    item.mIntent.setData(maybeAddUserId(data, contentUserHint));
839f1939901d2ed0480069f0b23be64f122fce93995Nicolas Prevot                }
840f1939901d2ed0480069f0b23be64f122fce93995Nicolas Prevot            }
841f1939901d2ed0480069f0b23be64f122fce93995Nicolas Prevot            if (item.mUri != null) {
842f1939901d2ed0480069f0b23be64f122fce93995Nicolas Prevot                item.mUri = maybeAddUserId(item.mUri, contentUserHint);
843f1939901d2ed0480069f0b23be64f122fce93995Nicolas Prevot            }
844f1939901d2ed0480069f0b23be64f122fce93995Nicolas Prevot        }
845f1939901d2ed0480069f0b23be64f122fce93995Nicolas Prevot    }
846f1939901d2ed0480069f0b23be64f122fce93995Nicolas Prevot
8479f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    @Override
84821c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn    public String toString() {
84921c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        StringBuilder b = new StringBuilder(128);
85021c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn
85121c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        b.append("ClipData { ");
85221c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        toShortString(b);
85321c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        b.append(" }");
85421c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn
85521c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        return b.toString();
85621c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn    }
85721c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn
85821c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn    /** @hide */
85921c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn    public void toShortString(StringBuilder b) {
86021c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        boolean first;
86121c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        if (mClipDescription != null) {
86221c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            first = !mClipDescription.toShortString(b);
86321c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        } else {
86421c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            first = true;
86521c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        }
86621c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        if (mIcon != null) {
86721c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            if (!first) {
86821c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn                b.append(' ');
86921c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            }
87021c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            first = false;
87121c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            b.append("I:");
87221c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            b.append(mIcon.getWidth());
87321c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            b.append('x');
87421c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            b.append(mIcon.getHeight());
87521c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        }
87621c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        for (int i=0; i<mItems.size(); i++) {
87721c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            if (!first) {
87821c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn                b.append(' ');
87921c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            }
88021c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            first = false;
88121c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            b.append('{');
88221c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            mItems.get(i).toShortString(b);
88321c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn            b.append('}');
88421c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        }
88521c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn    }
88621c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn
88721c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn    @Override
8889f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    public int describeContents() {
8899f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        return 0;
8909f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
8919f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
8929f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    @Override
8939f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    public void writeToParcel(Parcel dest, int flags) {
894f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn        mClipDescription.writeToParcel(dest, flags);
8959f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        if (mIcon != null) {
8969f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            dest.writeInt(1);
8979f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mIcon.writeToParcel(dest, flags);
8989f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        } else {
8999f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            dest.writeInt(0);
9009f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
9019f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        final int N = mItems.size();
9029f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        dest.writeInt(N);
9039f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        for (int i=0; i<N; i++) {
9049f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            Item item = mItems.get(i);
9059f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            TextUtils.writeToParcel(item.mText, dest, flags);
906acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            dest.writeString(item.mHtmlText);
9079f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            if (item.mIntent != null) {
9089f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn                dest.writeInt(1);
9099f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn                item.mIntent.writeToParcel(dest, flags);
9109f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            } else {
9119f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn                dest.writeInt(0);
9129f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            }
9139f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            if (item.mUri != null) {
9149f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn                dest.writeInt(1);
9159f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn                item.mUri.writeToParcel(dest, flags);
9169f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            } else {
9179f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn                dest.writeInt(0);
9189f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            }
9199f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
9209f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
9219f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
9221040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    ClipData(Parcel in) {
923f834dfabbcbbe1f209682f18c67f2e8b9d3e1dd7Dianne Hackborn        mClipDescription = new ClipDescription(in);
9249f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        if (in.readInt() != 0) {
9259f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            mIcon = Bitmap.CREATOR.createFromParcel(in);
9261040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        } else {
9271040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            mIcon = null;
9289f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
92921c241e061de29a538008ca42df9c878184bcfb8Dianne Hackborn        mItems = new ArrayList<Item>();
9309f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        final int N = in.readInt();
9319f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        for (int i=0; i<N; i++) {
9329f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            CharSequence text = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
933acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            String htmlText = in.readString();
9349f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            Intent intent = in.readInt() != 0 ? Intent.CREATOR.createFromParcel(in) : null;
9359f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            Uri uri = in.readInt() != 0 ? Uri.CREATOR.createFromParcel(in) : null;
936acb69bb909d098cea284df47d794c17171d84c91Dianne Hackborn            mItems.add(new Item(text, htmlText, intent, uri));
9379f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        }
9389f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn    }
9399f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
9401040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn    public static final Parcelable.Creator<ClipData> CREATOR =
9411040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn        new Parcelable.Creator<ClipData>() {
9429f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
9431040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            public ClipData createFromParcel(Parcel source) {
9441040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                return new ClipData(source);
9459f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            }
9469f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn
9471040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn            public ClipData[] newArray(int size) {
9481040dc465cbf5ca8f834a87c949e476abefa3f76Dianne Hackborn                return new ClipData[size];
9499f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn            }
9509f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn        };
9519f53119b72e6da865bcd53173d3dacd1eba01aeeDianne Hackborn}
952