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