blob: 9323261f052c887777a3d33ac5be81aeff433330 [file] [log] [blame]
Dianne Hackborn9f531192010-08-04 17:48:03 -07001/**
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
17package android.content;
18
Nicolas Prevotd85fc722014-04-16 19:52:08 +010019import static android.content.ContentProvider.maybeAddUserId;
Vladislav Kaznacheeve1f5e132017-01-27 16:56:40 -080020import static android.content.ContentResolver.SCHEME_ANDROID_RESOURCE;
21import static android.content.ContentResolver.SCHEME_CONTENT;
22import static android.content.ContentResolver.SCHEME_FILE;
Vladislav Kaznacheevc14df8e2016-01-22 11:49:13 -080023
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070024import android.content.res.AssetFileDescriptor;
Dianne Hackborn9f531192010-08-04 17:48:03 -070025import android.graphics.Bitmap;
26import android.net.Uri;
27import android.os.Parcel;
28import android.os.Parcelable;
Jeff Sharkeya14acd22013-04-02 18:27:45 -070029import android.os.StrictMode;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -070030import android.text.Html;
31import android.text.Spannable;
32import android.text.SpannableStringBuilder;
33import android.text.Spanned;
Dianne Hackborn9f531192010-08-04 17:48:03 -070034import android.text.TextUtils;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -070035import android.text.style.URLSpan;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070036import android.util.Log;
Dianne Hackborn9f531192010-08-04 17:48:03 -070037
Vladislav Kaznacheev8e7b9402016-11-23 15:31:13 -080038import com.android.internal.util.ArrayUtils;
39
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070040import java.io.FileInputStream;
41import java.io.FileNotFoundException;
42import java.io.IOException;
43import java.io.InputStreamReader;
Dianne Hackborn9f531192010-08-04 17:48:03 -070044import java.util.ArrayList;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080045import java.util.List;
Daichi Hirono3d9f3fc2017-04-12 14:23:06 +090046import libcore.io.IoUtils;
Dianne Hackborn9f531192010-08-04 17:48:03 -070047
48/**
49 * Representation of a clipped data on the clipboard.
50 *
Vladislav Kaznacheev7e4669c2016-05-02 17:04:08 -070051 * <p>ClipData is a complex type containing one or more Item instances,
Dianne Hackborn9f531192010-08-04 17:48:03 -070052 * each of which can hold one or more representations of an item of data.
Vladislav Kaznacheev7e4669c2016-05-02 17:04:08 -070053 * For display to the user, it also has a label.</p>
Dianne Hackborn9f531192010-08-04 17:48:03 -070054 *
Dianne Hackbornf834dfa2010-10-26 12:43:57 -070055 * <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 Hackborn1040dc42010-08-26 22:11:06 -070058 * 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 Hackborn327fbd22011-01-17 14:38:50 -080060 * {@link #newPlainText(CharSequence, CharSequence)},
61 * {@link #newUri(ContentResolver, CharSequence, Uri)}, and
62 * {@link #newIntent(CharSequence, Intent)}.
Dianne Hackborn1040dc42010-08-26 22:11:06 -070063 *
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070064 * <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 Hackborn9f531192010-08-04 17:48:03 -070067 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080068 * <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 Hackborn23fdaf62010-08-06 12:16:55 -070075 * <a name="ImplementingPaste"></a>
76 * <h3>Implementing Paste or Drop</h3>
77 *
Vladislav Kaznacheev7e4669c2016-05-02 17:04:08 -070078 * <p>To implement a paste or drop of a ClipData object into an application,
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070079 * 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 Hackborn1040dc42010-08-26 22:11:06 -070087 * In this case there is generally no need to worry about the MIME types
Dianne Hackbornf834dfa2010-10-26 12:43:57 -070088 * reported by {@link ClipDescription#getMimeType(int) getDescription().getMimeType(int)},
Newton Allen4465d1a2013-11-26 10:25:38 -080089 * since any clip item can always be converted to a string.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070090 *
91 * <p>More complicated exchanges will be done through URIs, in particular
Vladislav Kaznacheev7e4669c2016-05-02 17:04:08 -070092 * "content:" URIs. A content URI allows the recipient of a ClipData item
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070093 * to interact closely with the ContentProvider holding the data in order to
Dianne Hackborn1040dc42010-08-26 22:11:06 -070094 * negotiate the transfer of that data. The clip must also be filled in with
Dianne Hackborn327fbd22011-01-17 14:38:50 -080095 * the available MIME types; {@link #newUri(ContentResolver, CharSequence, Uri)}
Dianne Hackborn1040dc42010-08-26 22:11:06 -070096 * will take care of correctly doing this.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070097 *
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 Hackbornf6d952b2010-08-27 15:41:13 -0700118 * {@sample frameworks/base/core/java/android/content/ClipData.java coerceToText}
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700119 *
120 * <a name="ImplementingCopy"></a>
121 * <h3>Implementing Copy or Drag</h3>
122 *
Vladislav Kaznacheev7e4669c2016-05-02 17:04:08 -0700123 * <p>To be the source of a clip, the application must construct a ClipData
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700124 * 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 Hackborn9f531192010-08-04 17:48:03 -0700158 */
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700159public class ClipData implements Parcelable {
160 static final String[] MIMETYPES_TEXT_PLAIN = new String[] {
161 ClipDescription.MIMETYPE_TEXT_PLAIN };
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700162 static final String[] MIMETYPES_TEXT_HTML = new String[] {
163 ClipDescription.MIMETYPE_TEXT_HTML };
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700164 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 Hackborn1040dc42010-08-26 22:11:06 -0700168
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700169 final ClipDescription mClipDescription;
Vladislav Kaznacheev7e4669c2016-05-02 17:04:08 -0700170
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700171 final Bitmap mIcon;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700172
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800173 final ArrayList<Item> mItems;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700174
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700175 /**
Vladislav Kaznacheev7e4669c2016-05-02 17:04:08 -0700176 * Description of a single item in a ClipData.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700177 *
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 Hackborn9f531192010-08-04 17:48:03 -0700194 public static class Item {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700195 final CharSequence mText;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700196 final String mHtmlText;
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700197 final Intent mIntent;
Nicolas Prevotd85fc722014-04-16 19:52:08 +0100198 Uri mUri;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700199
Nicolas Prevot58926242016-07-14 19:11:10 +0100200 /** @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 Hackborn23fdaf62010-08-06 12:16:55 -0700208 /**
209 * Create an Item consisting of a single block of (possibly styled) text.
210 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700211 public Item(CharSequence text) {
212 mText = text;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700213 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 Hackborn1040dc42010-08-26 22:11:06 -0700227 mIntent = null;
228 mUri = null;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700229 }
230
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700231 /**
232 * Create an Item consisting of an arbitrary Intent.
233 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700234 public Item(Intent intent) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700235 mText = null;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700236 mHtmlText = null;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700237 mIntent = intent;
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700238 mUri = null;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700239 }
240
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700241 /**
242 * Create an Item consisting of an arbitrary URI.
243 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700244 public Item(Uri uri) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700245 mText = null;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700246 mHtmlText = null;
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700247 mIntent = null;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700248 mUri = uri;
249 }
250
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700251 /**
252 * Create a complex Item, containing multiple representations of
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700253 * text, Intent, and/or URI.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700254 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700255 public Item(CharSequence text, Intent intent, Uri uri) {
256 mText = text;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700257 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 Hackborn9f531192010-08-04 17:48:03 -0700275 mIntent = intent;
276 mUri = uri;
277 }
278
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700279 /**
280 * Retrieve the raw text contained in this Item.
281 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700282 public CharSequence getText() {
283 return mText;
284 }
285
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700286 /**
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700287 * Retrieve the raw HTML text contained in this Item.
288 */
289 public String getHtmlText() {
290 return mHtmlText;
291 }
292
293 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700294 * Retrieve the raw Intent contained in this Item.
295 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700296 public Intent getIntent() {
297 return mIntent;
298 }
299
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700300 /**
301 * Retrieve the raw URI contained in this Item.
302 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700303 public Uri getUri() {
304 return mUri;
305 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700306
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 Hackbornacb69bb2012-04-13 15:36:06 -0700320 * URI and return it.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700321 * <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 Hackbornacb69bb2012-04-13 15:36:06 -0700331 CharSequence text = getText();
332 if (text != null) {
333 return text;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700334 }
335
336 // If this Item has a URI value, try using that.
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700337 Uri uri = getUri();
338 if (uri != null) {
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700339 // 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 Hirono3d9f3fc2017-04-12 14:23:06 +0900342 final ContentResolver resolver = context.getContentResolver();
343 AssetFileDescriptor descr = null;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700344 FileInputStream stream = null;
Daichi Hirono3d9f3fc2017-04-12 14:23:06 +0900345 InputStreamReader reader = null;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700346 try {
Daichi Hirono3d9f3fc2017-04-12 14:23:06 +0900347 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 Hackborn23fdaf62010-08-06 12:16:55 -0700355 }
Daichi Hirono3d9f3fc2017-04-12 14:23:06 +0900356 if (descr != null) {
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700357 try {
Daichi Hirono3d9f3fc2017-04-12 14:23:06 +0900358 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 Hackborn23fdaf62010-08-06 12:16:55 -0700369 } catch (IOException e) {
Daichi Hirono3d9f3fc2017-04-12 14:23:06 +0900370 // Something bad has happened.
371 Log.w("ClipData", "Failure loading text", e);
372 return e.toString();
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700373 }
374 }
Daichi Hirono3d9f3fc2017-04-12 14:23:06 +0900375 } finally {
376 IoUtils.closeQuietly(descr);
377 IoUtils.closeQuietly(stream);
378 IoUtils.closeQuietly(reader);
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700379 }
380
Vladislav Kaznacheeve1f5e132017-01-27 16:56:40 -0800381 // 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 Hackbornacb69bb2012-04-13 15:36:06 -0700389 return uri.toString();
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700390 }
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 Hackbornacb69bb2012-04-13 15:36:06 -0700394 Intent intent = getIntent();
395 if (intent != null) {
396 return intent.toUri(Intent.URI_INTENT_SCHEME);
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700397 }
398
399 // Shouldn't get here, but just in case...
400 return "";
401 }
402//END_INCLUDE(coerceToText)
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800403
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700404 /**
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 Kaznacheevc14df8e2016-01-22 11:49:13 -0800489 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 Hackbornacb69bb2012-04-13 15:36:06 -0700495 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 Kaznacheevb143ff62017-01-27 16:39:13 -0800552 } catch (SecurityException e) {
553 Log.w("ClipData", "Failure opening stream", e);
554
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700555 } 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 Kaznacheev7e4669c2016-05-02 17:04:08 -0700561 Log.w("ClipData", "Failure loading text", e);
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700562 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 Kaznacheeve1f5e132017-01-27 16:56:40 -0800574 // 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 Hackbornacb69bb2012-04-13 15:36:06 -0700583 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 Kralevichc92db392012-07-27 13:22:20 -0700607 builder.append(Html.escapeHtml(uri));
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700608 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 Hackborn21c241e2012-03-08 13:57:23 -0800622 @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 Hackbornacb69bb2012-04-13 15:36:06 -0700635 if (mHtmlText != null) {
636 b.append("H:");
637 b.append(mHtmlText);
638 } else if (mText != null) {
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800639 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 Hackbornae498722015-08-14 13:29:47 -0700651
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 Hackborn9f531192010-08-04 17:48:03 -0700668 }
669
670 /**
671 * Create a new clip.
672 *
673 * @param label Label to show to the user describing this clip.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700674 * @param mimeTypes An array of MIME types this data is available as.
Dianne Hackborn9f531192010-08-04 17:48:03 -0700675 * @param item The contents of the first item in the clip.
676 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800677 public ClipData(CharSequence label, String[] mimeTypes, Item item) {
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700678 mClipDescription = new ClipDescription(label, mimeTypes);
Dianne Hackborn9f531192010-08-04 17:48:03 -0700679 if (item == null) {
680 throw new NullPointerException("item is null");
681 }
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800682 mIcon = null;
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800683 mItems = new ArrayList<Item>();
Dianne Hackborn9f531192010-08-04 17:48:03 -0700684 mItems.add(item);
685 }
686
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700687 /**
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700688 * Create a new clip.
689 *
690 * @param description The ClipDescription describing the clip contents.
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700691 * @param item The contents of the first item in the clip.
692 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800693 public ClipData(ClipDescription description, Item item) {
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700694 mClipDescription = description;
695 if (item == null) {
696 throw new NullPointerException("item is null");
697 }
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800698 mIcon = null;
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800699 mItems = new ArrayList<Item>();
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700700 mItems.add(item);
701 }
702
703 /**
Steve McKay57eaaf62016-06-13 12:53:31 -0700704 * 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 Hackborn21c241e2012-03-08 13:57:23 -0800722 * 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 Hackbornf834dfa2010-10-26 12:43:57 -0700734 * Create a new ClipData holding data of the type
735 * {@link ClipDescription#MIMETYPE_TEXT_PLAIN}.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700736 *
737 * @param label User-visible label for the clip data.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700738 * @param text The actual text in the clip.
739 * @return Returns a new ClipData containing the specified data.
740 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800741 static public ClipData newPlainText(CharSequence label, CharSequence text) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700742 Item item = new Item(text);
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800743 return new ClipData(label, MIMETYPES_TEXT_PLAIN, item);
744 }
745
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700746 /**
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700747 * 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 Hackbornf834dfa2010-10-26 12:43:57 -0700763 * Create a new ClipData holding an Intent with MIME type
764 * {@link ClipDescription#MIMETYPE_TEXT_INTENT}.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700765 *
766 * @param label User-visible label for the clip data.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700767 * @param intent The actual Intent in the clip.
768 * @return Returns a new ClipData containing the specified data.
769 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800770 static public ClipData newIntent(CharSequence label, Intent intent) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700771 Item item = new Item(intent);
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800772 return new ClipData(label, MIMETYPES_TEXT_INTENT, item);
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700773 }
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 Hackbornf834dfa2010-10-26 12:43:57 -0700779 * {@link ClipDescription#MIMETYPE_TEXT_URILIST}.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700780 *
781 * @param resolver ContentResolver used to get information about the URI.
782 * @param label User-visible label for the clip data.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700783 * @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 Hackborn327fbd22011-01-17 14:38:50 -0800787 Uri uri) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700788 Item item = new Item(uri);
Vladislav Kaznacheev8e7b9402016-11-23 15:31:13 -0800789 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 Hackborn1040dc42010-08-26 22:11:06 -0700801 String[] mimeTypes = null;
Vladislav Kaznacheeve1f5e132017-01-27 16:56:40 -0800802 if (SCHEME_CONTENT.equals(uri.getScheme())) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700803 String realType = resolver.getType(uri);
804 mimeTypes = resolver.getStreamTypes(uri, "*/*");
Vladislav Kaznacheevf67661c2016-05-03 15:02:33 -0700805 if (realType != null) {
806 if (mimeTypes == null) {
807 mimeTypes = new String[] { realType };
Vladislav Kaznacheev8e7b9402016-11-23 15:31:13 -0800808 } else if (!ArrayUtils.contains(mimeTypes, realType)) {
Vladislav Kaznacheevf67661c2016-05-03 15:02:33 -0700809 String[] tmp = new String[mimeTypes.length + 1];
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700810 tmp[0] = realType;
Vladislav Kaznacheevf67661c2016-05-03 15:02:33 -0700811 System.arraycopy(mimeTypes, 0, tmp, 1, mimeTypes.length);
812 mimeTypes = tmp;
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700813 }
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700814 }
815 }
816 if (mimeTypes == null) {
817 mimeTypes = MIMETYPES_TEXT_URILIST;
818 }
Vladislav Kaznacheev8e7b9402016-11-23 15:31:13 -0800819 return mimeTypes;
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700820 }
821
822 /**
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700823 * Create a new ClipData holding an URI with MIME type
824 * {@link ClipDescription#MIMETYPE_TEXT_URILIST}.
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800825 * Unlike {@link #newUri(ContentResolver, CharSequence, Uri)}, nothing
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700826 * 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 Hackborn1040dc42010-08-26 22:11:06 -0700830 * @param uri The URI in the clip.
831 * @return Returns a new ClipData containing the specified data.
832 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800833 static public ClipData newRawUri(CharSequence label, Uri uri) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700834 Item item = new Item(uri);
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800835 return new ClipData(label, MIMETYPES_TEXT_URILIST, item);
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700836 }
837
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700838 /**
839 * Return the {@link ClipDescription} associated with this data, describing
840 * what it contains.
841 */
842 public ClipDescription getDescription() {
843 return mClipDescription;
844 }
Vladislav Kaznacheevf67661c2016-05-03 15:02:33 -0700845
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800846 /**
847 * Add a new Item to the overall ClipData container.
Vladislav Kaznacheevf67661c2016-05-03 15:02:33 -0700848 * <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 Kaznacheevc35c02f2017-04-20 08:38:21 -0700850 * MIME types to this clip. If this is not the case, use {@link #addItem(ContentResolver, Item)}
Vladislav Kaznacheev8e7b9402016-11-23 15:31:13 -0800851 * or call {@link #ClipData(CharSequence, String[], Item)} with a complete list of MIME types.
Vladislav Kaznacheevf67661c2016-05-03 15:02:33 -0700852 * @param item Item to be added.
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800853 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700854 public void addItem(Item item) {
855 if (item == null) {
856 throw new NullPointerException("item is null");
857 }
858 mItems.add(item);
859 }
860
Vladislav Kaznacheevc35c02f2017-04-20 08:38:21 -0700861 /** @removed use #addItem(ContentResolver, Item) instead */
862 @Deprecated
863 public void addItem(Item item, ContentResolver resolver) {
864 addItem(resolver, item);
865 }
866
Vladislav Kaznacheev8e7b9402016-11-23 15:31:13 -0800867 /**
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 Kaznacheev8e7b9402016-11-23 15:31:13 -0800871 * @param resolver ContentResolver used to get information about the URI possibly contained in
872 * the item.
Vladislav Kaznacheevc35c02f2017-04-20 08:38:21 -0700873 * @param item Item to be added.
Vladislav Kaznacheev8e7b9402016-11-23 15:31:13 -0800874 */
Vladislav Kaznacheevc35c02f2017-04-20 08:38:21 -0700875 public void addItem(ContentResolver resolver, Item item) {
Vladislav Kaznacheev8e7b9402016-11-23 15:31:13 -0800876 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 Hackborn327fbd22011-01-17 14:38:50 -0800893 /** @hide */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700894 public Bitmap getIcon() {
895 return mIcon;
896 }
897
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800898 /**
899 * Return the number of items in the clip data.
900 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700901 public int getItemCount() {
902 return mItems.size();
903 }
904
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800905 /**
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 Hackborn9f531192010-08-04 17:48:03 -0700910 return mItems.get(index);
911 }
912
Nicolas Prevot58926242016-07-14 19:11:10 +0100913 /** @hide */
914 public void setItemAt(int index, Item item) {
915 mItems.set(index, item);
916 }
917
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700918 /**
919 * Prepare this {@link ClipData} to leave an app process.
920 *
921 * @hide
922 */
Jeff Sharkey344744b2016-01-28 19:03:30 -0700923 public void prepareToLeaveProcess(boolean leavingPackage) {
Jeff Sharkeyf361f2f2016-12-21 10:48:42 -0700924 // Assume that callers are going to be granting permissions
925 prepareToLeaveProcess(leavingPackage, Intent.FLAG_GRANT_READ_URI_PERMISSION);
Jeff Sharkeyfb833f32016-12-01 14:59:59 -0700926 }
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 Sharkeya14acd22013-04-02 18:27:45 -0700934 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 Sharkey344744b2016-01-28 19:03:30 -0700938 item.mIntent.prepareToLeaveProcess(leavingPackage);
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700939 }
Jeff Sharkeyfb833f32016-12-01 14:59:59 -0700940 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 Sharkeya14acd22013-04-02 18:27:45 -0700948 }
949 }
950 }
951
Jeff Sharkeyd136e512016-03-09 22:30:56 -0700952 /** {@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 Prevotd1c99b12014-07-04 16:56:17 +0100963 /** @hide */
964 public void fixUris(int contentUserHint) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +0100965 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 Prevotd1c99b12014-07-04 16:56:17 +0100969 item.mIntent.fixUris(contentUserHint);
Nicolas Prevotd85fc722014-04-16 19:52:08 +0100970 }
971 if (item.mUri != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +0100972 item.mUri = maybeAddUserId(item.mUri, contentUserHint);
Nicolas Prevotd85fc722014-04-16 19:52:08 +0100973 }
974 }
975 }
976
Nicolas Prevotf1939902014-06-25 09:29:02 +0100977 /**
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 Hackborn9f531192010-08-04 17:48:03 -0700997 @Override
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800998 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 Hackbornae498722015-08-14 13:29:47 -07001037 /** @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 Kaznacheev9149d2b2015-12-15 12:16:28 -08001050 /** @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 Hackborn21c241e2012-03-08 13:57:23 -08001071 @Override
Dianne Hackborn9f531192010-08-04 17:48:03 -07001072 public int describeContents() {
1073 return 0;
1074 }
1075
1076 @Override
1077 public void writeToParcel(Parcel dest, int flags) {
Dianne Hackbornf834dfa2010-10-26 12:43:57 -07001078 mClipDescription.writeToParcel(dest, flags);
Dianne Hackborn9f531192010-08-04 17:48:03 -07001079 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 Hackbornacb69bb2012-04-13 15:36:06 -07001090 dest.writeString(item.mHtmlText);
Dianne Hackborn9f531192010-08-04 17:48:03 -07001091 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 Hackborn1040dc42010-08-26 22:11:06 -07001106 ClipData(Parcel in) {
Dianne Hackbornf834dfa2010-10-26 12:43:57 -07001107 mClipDescription = new ClipDescription(in);
Dianne Hackborn9f531192010-08-04 17:48:03 -07001108 if (in.readInt() != 0) {
1109 mIcon = Bitmap.CREATOR.createFromParcel(in);
Dianne Hackborn1040dc42010-08-26 22:11:06 -07001110 } else {
1111 mIcon = null;
Dianne Hackborn9f531192010-08-04 17:48:03 -07001112 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -08001113 mItems = new ArrayList<Item>();
Dianne Hackborn9f531192010-08-04 17:48:03 -07001114 final int N = in.readInt();
1115 for (int i=0; i<N; i++) {
1116 CharSequence text = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
Dianne Hackbornacb69bb2012-04-13 15:36:06 -07001117 String htmlText = in.readString();
Dianne Hackborn9f531192010-08-04 17:48:03 -07001118 Intent intent = in.readInt() != 0 ? Intent.CREATOR.createFromParcel(in) : null;
1119 Uri uri = in.readInt() != 0 ? Uri.CREATOR.createFromParcel(in) : null;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -07001120 mItems.add(new Item(text, htmlText, intent, uri));
Dianne Hackborn9f531192010-08-04 17:48:03 -07001121 }
1122 }
1123
Dianne Hackborn1040dc42010-08-26 22:11:06 -07001124 public static final Parcelable.Creator<ClipData> CREATOR =
1125 new Parcelable.Creator<ClipData>() {
Dianne Hackborn9f531192010-08-04 17:48:03 -07001126
Steve McKay57eaaf62016-06-13 12:53:31 -07001127 @Override
Dianne Hackborn1040dc42010-08-26 22:11:06 -07001128 public ClipData createFromParcel(Parcel source) {
1129 return new ClipData(source);
Dianne Hackborn9f531192010-08-04 17:48:03 -07001130 }
1131
Steve McKay57eaaf62016-06-13 12:53:31 -07001132 @Override
Dianne Hackborn1040dc42010-08-26 22:11:06 -07001133 public ClipData[] newArray(int size) {
1134 return new ClipData[size];
Dianne Hackborn9f531192010-08-04 17:48:03 -07001135 }
1136 };
1137}