blob: 58630b0204913d90847894136421897ba584a3b2 [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 Kaznacheevc14df8e2016-01-22 11:49:13 -080020
21import android.content.pm.PackageManager;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070022import android.content.res.AssetFileDescriptor;
Dianne Hackborn9f531192010-08-04 17:48:03 -070023import android.graphics.Bitmap;
24import android.net.Uri;
25import android.os.Parcel;
26import android.os.Parcelable;
Vladislav Kaznacheevc14df8e2016-01-22 11:49:13 -080027import android.os.Process;
Jeff Sharkeya14acd22013-04-02 18:27:45 -070028import android.os.StrictMode;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -070029import android.text.Html;
30import android.text.Spannable;
31import android.text.SpannableStringBuilder;
32import android.text.Spanned;
Dianne Hackborn9f531192010-08-04 17:48:03 -070033import android.text.TextUtils;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -070034import android.text.style.URLSpan;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070035import android.util.Log;
Dianne Hackborn9f531192010-08-04 17:48:03 -070036
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070037import java.io.FileInputStream;
38import java.io.FileNotFoundException;
39import java.io.IOException;
40import java.io.InputStreamReader;
Dianne Hackborn9f531192010-08-04 17:48:03 -070041import java.util.ArrayList;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080042import java.util.List;
Dianne Hackborn9f531192010-08-04 17:48:03 -070043
44/**
45 * Representation of a clipped data on the clipboard.
46 *
47 * <p>ClippedData is a complex type containing one or Item instances,
48 * each of which can hold one or more representations of an item of data.
49 * For display to the user, it also has a label and iconic representation.</p>
50 *
Dianne Hackbornf834dfa2010-10-26 12:43:57 -070051 * <p>A ClipData contains a {@link ClipDescription}, which describes
52 * important meta-data about the clip. In particular, its
53 * {@link ClipDescription#getMimeType(int) getDescription().getMimeType(int)}
Dianne Hackborn1040dc42010-08-26 22:11:06 -070054 * must return correct MIME type(s) describing the data in the clip. For help
55 * in correctly constructing a clip with the correct MIME type, use
Dianne Hackborn327fbd22011-01-17 14:38:50 -080056 * {@link #newPlainText(CharSequence, CharSequence)},
57 * {@link #newUri(ContentResolver, CharSequence, Uri)}, and
58 * {@link #newIntent(CharSequence, Intent)}.
Dianne Hackborn1040dc42010-08-26 22:11:06 -070059 *
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070060 * <p>Each Item instance can be one of three main classes of data: a simple
61 * CharSequence of text, a single Intent object, or a Uri. See {@link Item}
62 * for more details.
Dianne Hackborn9f531192010-08-04 17:48:03 -070063 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080064 * <div class="special reference">
65 * <h3>Developer Guides</h3>
66 * <p>For more information about using the clipboard framework, read the
67 * <a href="{@docRoot}guide/topics/clipboard/copy-paste.html">Copy and Paste</a>
68 * developer guide.</p>
69 * </div>
70 *
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070071 * <a name="ImplementingPaste"></a>
72 * <h3>Implementing Paste or Drop</h3>
73 *
74 * <p>To implement a paste or drop of a ClippedData object into an application,
75 * the application must correctly interpret the data for its use. If the {@link Item}
76 * it contains is simple text or an Intent, there is little to be done: text
77 * can only be interpreted as text, and an Intent will typically be used for
78 * creating shortcuts (such as placing icons on the home screen) or other
79 * actions.
80 *
81 * <p>If all you want is the textual representation of the clipped data, you
82 * can use the convenience method {@link Item#coerceToText Item.coerceToText}.
Dianne Hackborn1040dc42010-08-26 22:11:06 -070083 * In this case there is generally no need to worry about the MIME types
Dianne Hackbornf834dfa2010-10-26 12:43:57 -070084 * reported by {@link ClipDescription#getMimeType(int) getDescription().getMimeType(int)},
Newton Allen4465d1a2013-11-26 10:25:38 -080085 * since any clip item can always be converted to a string.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070086 *
87 * <p>More complicated exchanges will be done through URIs, in particular
88 * "content:" URIs. A content URI allows the recipient of a ClippedData item
89 * to interact closely with the ContentProvider holding the data in order to
Dianne Hackborn1040dc42010-08-26 22:11:06 -070090 * negotiate the transfer of that data. The clip must also be filled in with
Dianne Hackborn327fbd22011-01-17 14:38:50 -080091 * the available MIME types; {@link #newUri(ContentResolver, CharSequence, Uri)}
Dianne Hackborn1040dc42010-08-26 22:11:06 -070092 * will take care of correctly doing this.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070093 *
94 * <p>For example, here is the paste function of a simple NotePad application.
95 * When retrieving the data from the clipboard, it can do either two things:
96 * if the clipboard contains a URI reference to an existing note, it copies
97 * the entire structure of the note into a new note; otherwise, it simply
98 * coerces the clip into text and uses that as the new note's contents.
99 *
100 * {@sample development/samples/NotePad/src/com/example/android/notepad/NoteEditor.java
101 * paste}
102 *
103 * <p>In many cases an application can paste various types of streams of data. For
104 * example, an e-mail application may want to allow the user to paste an image
105 * or other binary data as an attachment. This is accomplished through the
106 * ContentResolver {@link ContentResolver#getStreamTypes(Uri, String)} and
107 * {@link ContentResolver#openTypedAssetFileDescriptor(Uri, String, android.os.Bundle)}
108 * methods. These allow a client to discover the type(s) of data that a particular
109 * content URI can make available as a stream and retrieve the stream of data.
110 *
111 * <p>For example, the implementation of {@link Item#coerceToText Item.coerceToText}
112 * itself uses this to try to retrieve a URI clip as a stream of text:
113 *
Dianne Hackbornf6d952b2010-08-27 15:41:13 -0700114 * {@sample frameworks/base/core/java/android/content/ClipData.java coerceToText}
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700115 *
116 * <a name="ImplementingCopy"></a>
117 * <h3>Implementing Copy or Drag</h3>
118 *
119 * <p>To be the source of a clip, the application must construct a ClippedData
120 * object that any recipient can interpret best for their context. If the clip
121 * is to contain a simple text, Intent, or URI, this is easy: an {@link Item}
122 * containing the appropriate data type can be constructed and used.
123 *
124 * <p>More complicated data types require the implementation of support in
125 * a ContentProvider for describing and generating the data for the recipient.
126 * A common scenario is one where an application places on the clipboard the
127 * content: URI of an object that the user has copied, with the data at that
128 * URI consisting of a complicated structure that only other applications with
129 * direct knowledge of the structure can use.
130 *
131 * <p>For applications that do not have intrinsic knowledge of the data structure,
132 * the content provider holding it can make the data available as an arbitrary
133 * number of types of data streams. This is done by implementing the
134 * ContentProvider {@link ContentProvider#getStreamTypes(Uri, String)} and
135 * {@link ContentProvider#openTypedAssetFile(Uri, String, android.os.Bundle)}
136 * methods.
137 *
138 * <p>Going back to our simple NotePad application, this is the implementation
139 * it may have to convert a single note URI (consisting of a title and the note
140 * text) into a stream of plain text data.
141 *
142 * {@sample development/samples/NotePad/src/com/example/android/notepad/NotePadProvider.java
143 * stream}
144 *
145 * <p>The copy operation in our NotePad application is now just a simple matter
146 * of making a clip containing the URI of the note being copied:
147 *
148 * {@sample development/samples/NotePad/src/com/example/android/notepad/NotesList.java
149 * copy}
150 *
151 * <p>Note if a paste operation needs this clip as text (for example to paste
152 * into an editor), then {@link Item#coerceToText(Context)} will ask the content
153 * provider for the clip URI as text and successfully paste the entire note.
Dianne Hackborn9f531192010-08-04 17:48:03 -0700154 */
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700155public class ClipData implements Parcelable {
156 static final String[] MIMETYPES_TEXT_PLAIN = new String[] {
157 ClipDescription.MIMETYPE_TEXT_PLAIN };
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700158 static final String[] MIMETYPES_TEXT_HTML = new String[] {
159 ClipDescription.MIMETYPE_TEXT_HTML };
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700160 static final String[] MIMETYPES_TEXT_URILIST = new String[] {
161 ClipDescription.MIMETYPE_TEXT_URILIST };
162 static final String[] MIMETYPES_TEXT_INTENT = new String[] {
163 ClipDescription.MIMETYPE_TEXT_INTENT };
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700164
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700165 final ClipDescription mClipDescription;
166
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700167 final Bitmap mIcon;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700168
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800169 final ArrayList<Item> mItems;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700170
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700171 /**
172 * Description of a single item in a ClippedData.
173 *
174 * <p>The types than an individual item can currently contain are:</p>
175 *
176 * <ul>
177 * <li> Text: a basic string of text. This is actually a CharSequence,
178 * so it can be formatted text supported by corresponding Android built-in
179 * style spans. (Custom application spans are not supported and will be
180 * stripped when transporting through the clipboard.)
181 * <li> Intent: an arbitrary Intent object. A typical use is the shortcut
182 * to create when pasting a clipped item on to the home screen.
183 * <li> Uri: a URI reference. This may be any URI (such as an http: URI
184 * representing a bookmark), however it is often a content: URI. Using
185 * content provider references as clips like this allows an application to
186 * share complex or large clips through the standard content provider
187 * facilities.
188 * </ul>
189 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700190 public static class Item {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700191 final CharSequence mText;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700192 final String mHtmlText;
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700193 final Intent mIntent;
Nicolas Prevotd85fc722014-04-16 19:52:08 +0100194 Uri mUri;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700195
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700196 /**
197 * Create an Item consisting of a single block of (possibly styled) text.
198 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700199 public Item(CharSequence text) {
200 mText = text;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700201 mHtmlText = null;
202 mIntent = null;
203 mUri = null;
204 }
205
206 /**
207 * Create an Item consisting of a single block of (possibly styled) text,
208 * with an alternative HTML formatted representation. You <em>must</em>
209 * supply a plain text representation in addition to HTML text; coercion
210 * will not be done from HTML formated text into plain text.
211 */
212 public Item(CharSequence text, String htmlText) {
213 mText = text;
214 mHtmlText = htmlText;
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700215 mIntent = null;
216 mUri = null;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700217 }
218
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700219 /**
220 * Create an Item consisting of an arbitrary Intent.
221 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700222 public Item(Intent intent) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700223 mText = null;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700224 mHtmlText = null;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700225 mIntent = intent;
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700226 mUri = null;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700227 }
228
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700229 /**
230 * Create an Item consisting of an arbitrary URI.
231 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700232 public Item(Uri uri) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700233 mText = null;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700234 mHtmlText = null;
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700235 mIntent = null;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700236 mUri = uri;
237 }
238
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700239 /**
240 * Create a complex Item, containing multiple representations of
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700241 * text, Intent, and/or URI.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700242 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700243 public Item(CharSequence text, Intent intent, Uri uri) {
244 mText = text;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700245 mHtmlText = null;
246 mIntent = intent;
247 mUri = uri;
248 }
249
250 /**
251 * Create a complex Item, containing multiple representations of
252 * text, HTML text, Intent, and/or URI. If providing HTML text, you
253 * <em>must</em> supply a plain text representation as well; coercion
254 * will not be done from HTML formated text into plain text.
255 */
256 public Item(CharSequence text, String htmlText, Intent intent, Uri uri) {
257 if (htmlText != null && text == null) {
258 throw new IllegalArgumentException(
259 "Plain text must be supplied if HTML text is supplied");
260 }
261 mText = text;
262 mHtmlText = htmlText;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700263 mIntent = intent;
264 mUri = uri;
265 }
266
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700267 /**
268 * Retrieve the raw text contained in this Item.
269 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700270 public CharSequence getText() {
271 return mText;
272 }
273
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700274 /**
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700275 * Retrieve the raw HTML text contained in this Item.
276 */
277 public String getHtmlText() {
278 return mHtmlText;
279 }
280
281 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700282 * Retrieve the raw Intent contained in this Item.
283 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700284 public Intent getIntent() {
285 return mIntent;
286 }
287
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700288 /**
289 * Retrieve the raw URI contained in this Item.
290 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700291 public Uri getUri() {
292 return mUri;
293 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700294
295 /**
296 * Turn this item into text, regardless of the type of data it
297 * actually contains.
298 *
299 * <p>The algorithm for deciding what text to return is:
300 * <ul>
301 * <li> If {@link #getText} is non-null, return that.
302 * <li> If {@link #getUri} is non-null, try to retrieve its data
303 * as a text stream from its content provider. If this succeeds, copy
304 * the text into a String and return it. If it is not a content: URI or
305 * the content provider does not supply a text representation, return
306 * the raw URI as a string.
307 * <li> If {@link #getIntent} is non-null, convert that to an intent:
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700308 * URI and return it.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700309 * <li> Otherwise, return an empty string.
310 * </ul>
311 *
312 * @param context The caller's Context, from which its ContentResolver
313 * and other things can be retrieved.
314 * @return Returns the item's textual representation.
315 */
316//BEGIN_INCLUDE(coerceToText)
317 public CharSequence coerceToText(Context context) {
318 // If this Item has an explicit textual value, simply return that.
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700319 CharSequence text = getText();
320 if (text != null) {
321 return text;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700322 }
323
324 // If this Item has a URI value, try using that.
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700325 Uri uri = getUri();
326 if (uri != null) {
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700327
328 // First see if the URI can be opened as a plain text stream
329 // (of any sub-type). If so, this is the best textual
330 // representation for it.
331 FileInputStream stream = null;
332 try {
333 // Ask for a stream of the desired type.
334 AssetFileDescriptor descr = context.getContentResolver()
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700335 .openTypedAssetFileDescriptor(uri, "text/*", null);
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700336 stream = descr.createInputStream();
337 InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
338
339 // Got it... copy the stream into a local string and return it.
340 StringBuilder builder = new StringBuilder(128);
341 char[] buffer = new char[8192];
342 int len;
343 while ((len=reader.read(buffer)) > 0) {
344 builder.append(buffer, 0, len);
345 }
346 return builder.toString();
347
348 } catch (FileNotFoundException e) {
349 // Unable to open content URI as text... not really an
350 // error, just something to ignore.
351
352 } catch (IOException e) {
353 // Something bad has happened.
354 Log.w("ClippedData", "Failure loading text", e);
355 return e.toString();
356
357 } finally {
358 if (stream != null) {
359 try {
360 stream.close();
361 } catch (IOException e) {
362 }
363 }
364 }
365
366 // If we couldn't open the URI as a stream, then the URI itself
367 // probably serves fairly well as a textual representation.
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700368 return uri.toString();
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700369 }
370
371 // Finally, if all we have is an Intent, then we can just turn that
372 // into text. Not the most user-friendly thing, but it's something.
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700373 Intent intent = getIntent();
374 if (intent != null) {
375 return intent.toUri(Intent.URI_INTENT_SCHEME);
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700376 }
377
378 // Shouldn't get here, but just in case...
379 return "";
380 }
381//END_INCLUDE(coerceToText)
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800382
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700383 /**
384 * Like {@link #coerceToHtmlText(Context)}, but any text that would
385 * be returned as HTML formatting will be returned as text with
386 * style spans.
387 * @param context The caller's Context, from which its ContentResolver
388 * and other things can be retrieved.
389 * @return Returns the item's textual representation.
390 */
391 public CharSequence coerceToStyledText(Context context) {
392 CharSequence text = getText();
393 if (text instanceof Spanned) {
394 return text;
395 }
396 String htmlText = getHtmlText();
397 if (htmlText != null) {
398 try {
399 CharSequence newText = Html.fromHtml(htmlText);
400 if (newText != null) {
401 return newText;
402 }
403 } catch (RuntimeException e) {
404 // If anything bad happens, we'll fall back on the plain text.
405 }
406 }
407
408 if (text != null) {
409 return text;
410 }
411 return coerceToHtmlOrStyledText(context, true);
412 }
413
414 /**
415 * Turn this item into HTML text, regardless of the type of data it
416 * actually contains.
417 *
418 * <p>The algorithm for deciding what text to return is:
419 * <ul>
420 * <li> If {@link #getHtmlText} is non-null, return that.
421 * <li> If {@link #getText} is non-null, return that, converting to
422 * valid HTML text. If this text contains style spans,
423 * {@link Html#toHtml(Spanned) Html.toHtml(Spanned)} is used to
424 * convert them to HTML formatting.
425 * <li> If {@link #getUri} is non-null, try to retrieve its data
426 * as a text stream from its content provider. If the provider can
427 * supply text/html data, that will be preferred and returned as-is.
428 * Otherwise, any text/* data will be returned and escaped to HTML.
429 * If it is not a content: URI or the content provider does not supply
430 * a text representation, HTML text containing a link to the URI
431 * will be returned.
432 * <li> If {@link #getIntent} is non-null, convert that to an intent:
433 * URI and return as an HTML link.
434 * <li> Otherwise, return an empty string.
435 * </ul>
436 *
437 * @param context The caller's Context, from which its ContentResolver
438 * and other things can be retrieved.
439 * @return Returns the item's representation as HTML text.
440 */
441 public String coerceToHtmlText(Context context) {
442 // If the item has an explicit HTML value, simply return that.
443 String htmlText = getHtmlText();
444 if (htmlText != null) {
445 return htmlText;
446 }
447
448 // If this Item has a plain text value, return it as HTML.
449 CharSequence text = getText();
450 if (text != null) {
451 if (text instanceof Spanned) {
452 return Html.toHtml((Spanned)text);
453 }
454 return Html.escapeHtml(text);
455 }
456
457 text = coerceToHtmlOrStyledText(context, false);
458 return text != null ? text.toString() : null;
459 }
460
461 private CharSequence coerceToHtmlOrStyledText(Context context, boolean styled) {
462 // If this Item has a URI value, try using that.
463 if (mUri != null) {
464
465 // Check to see what data representations the content
466 // provider supports. We would like HTML text, but if that
467 // is not possible we'll live with plan text.
Vladislav Kaznacheevc14df8e2016-01-22 11:49:13 -0800468 String[] types = null;
469 try {
470 types = context.getContentResolver().getStreamTypes(mUri, "text/*");
471 } catch (SecurityException e) {
472 // No read permission for mUri, assume empty stream types list.
473 }
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700474 boolean hasHtml = false;
475 boolean hasText = false;
476 if (types != null) {
477 for (String type : types) {
478 if ("text/html".equals(type)) {
479 hasHtml = true;
480 } else if (type.startsWith("text/")) {
481 hasText = true;
482 }
483 }
484 }
485
486 // If the provider can serve data we can use, open and load it.
487 if (hasHtml || hasText) {
488 FileInputStream stream = null;
489 try {
490 // Ask for a stream of the desired type.
491 AssetFileDescriptor descr = context.getContentResolver()
492 .openTypedAssetFileDescriptor(mUri,
493 hasHtml ? "text/html" : "text/plain", null);
494 stream = descr.createInputStream();
495 InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
496
497 // Got it... copy the stream into a local string and return it.
498 StringBuilder builder = new StringBuilder(128);
499 char[] buffer = new char[8192];
500 int len;
501 while ((len=reader.read(buffer)) > 0) {
502 builder.append(buffer, 0, len);
503 }
504 String text = builder.toString();
505 if (hasHtml) {
506 if (styled) {
507 // We loaded HTML formatted text and the caller
508 // want styled text, convert it.
509 try {
510 CharSequence newText = Html.fromHtml(text);
511 return newText != null ? newText : text;
512 } catch (RuntimeException e) {
513 return text;
514 }
515 } else {
516 // We loaded HTML formatted text and that is what
517 // the caller wants, just return it.
518 return text.toString();
519 }
520 }
521 if (styled) {
522 // We loaded plain text and the caller wants styled
523 // text, that is all we have so return it.
524 return text;
525 } else {
526 // We loaded plain text and the caller wants HTML
527 // text, escape it for HTML.
528 return Html.escapeHtml(text);
529 }
530
531 } catch (FileNotFoundException e) {
532 // Unable to open content URI as text... not really an
533 // error, just something to ignore.
534
535 } catch (IOException e) {
536 // Something bad has happened.
537 Log.w("ClippedData", "Failure loading text", e);
538 return Html.escapeHtml(e.toString());
539
540 } finally {
541 if (stream != null) {
542 try {
543 stream.close();
544 } catch (IOException e) {
545 }
546 }
547 }
548 }
549
550 // If we couldn't open the URI as a stream, then we can build
551 // some HTML text with the URI itself.
552 // probably serves fairly well as a textual representation.
553 if (styled) {
554 return uriToStyledText(mUri.toString());
555 } else {
556 return uriToHtml(mUri.toString());
557 }
558 }
559
560 // Finally, if all we have is an Intent, then we can just turn that
561 // into text. Not the most user-friendly thing, but it's something.
562 if (mIntent != null) {
563 if (styled) {
564 return uriToStyledText(mIntent.toUri(Intent.URI_INTENT_SCHEME));
565 } else {
566 return uriToHtml(mIntent.toUri(Intent.URI_INTENT_SCHEME));
567 }
568 }
569
570 // Shouldn't get here, but just in case...
571 return "";
572 }
573
574 private String uriToHtml(String uri) {
575 StringBuilder builder = new StringBuilder(256);
576 builder.append("<a href=\"");
Nick Kralevichc92db392012-07-27 13:22:20 -0700577 builder.append(Html.escapeHtml(uri));
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700578 builder.append("\">");
579 builder.append(Html.escapeHtml(uri));
580 builder.append("</a>");
581 return builder.toString();
582 }
583
584 private CharSequence uriToStyledText(String uri) {
585 SpannableStringBuilder builder = new SpannableStringBuilder();
586 builder.append(uri);
587 builder.setSpan(new URLSpan(uri), 0, builder.length(),
588 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
589 return builder;
590 }
591
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800592 @Override
593 public String toString() {
594 StringBuilder b = new StringBuilder(128);
595
596 b.append("ClipData.Item { ");
597 toShortString(b);
598 b.append(" }");
599
600 return b.toString();
601 }
602
603 /** @hide */
604 public void toShortString(StringBuilder b) {
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700605 if (mHtmlText != null) {
606 b.append("H:");
607 b.append(mHtmlText);
608 } else if (mText != null) {
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800609 b.append("T:");
610 b.append(mText);
611 } else if (mUri != null) {
612 b.append("U:");
613 b.append(mUri);
614 } else if (mIntent != null) {
615 b.append("I:");
616 mIntent.toShortString(b, true, true, true, true);
617 } else {
618 b.append("NULL");
619 }
620 }
Dianne Hackbornae498722015-08-14 13:29:47 -0700621
622 /** @hide */
623 public void toShortSummaryString(StringBuilder b) {
624 if (mHtmlText != null) {
625 b.append("HTML");
626 } else if (mText != null) {
627 b.append("TEXT");
628 } else if (mUri != null) {
629 b.append("U:");
630 b.append(mUri);
631 } else if (mIntent != null) {
632 b.append("I:");
633 mIntent.toShortString(b, true, true, true, true);
634 } else {
635 b.append("NULL");
636 }
637 }
Dianne Hackborn9f531192010-08-04 17:48:03 -0700638 }
639
640 /**
641 * Create a new clip.
642 *
643 * @param label Label to show to the user describing this clip.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700644 * @param mimeTypes An array of MIME types this data is available as.
Dianne Hackborn9f531192010-08-04 17:48:03 -0700645 * @param item The contents of the first item in the clip.
646 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800647 public ClipData(CharSequence label, String[] mimeTypes, Item item) {
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700648 mClipDescription = new ClipDescription(label, mimeTypes);
Dianne Hackborn9f531192010-08-04 17:48:03 -0700649 if (item == null) {
650 throw new NullPointerException("item is null");
651 }
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800652 mIcon = null;
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800653 mItems = new ArrayList<Item>();
Dianne Hackborn9f531192010-08-04 17:48:03 -0700654 mItems.add(item);
655 }
656
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700657 /**
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700658 * Create a new clip.
659 *
660 * @param description The ClipDescription describing the clip contents.
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700661 * @param item The contents of the first item in the clip.
662 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800663 public ClipData(ClipDescription description, Item item) {
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700664 mClipDescription = description;
665 if (item == null) {
666 throw new NullPointerException("item is null");
667 }
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800668 mIcon = null;
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800669 mItems = new ArrayList<Item>();
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700670 mItems.add(item);
671 }
672
673 /**
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800674 * Create a new clip that is a copy of another clip. This does a deep-copy
675 * of all items in the clip.
676 *
677 * @param other The existing ClipData that is to be copied.
678 */
679 public ClipData(ClipData other) {
680 mClipDescription = other.mClipDescription;
681 mIcon = other.mIcon;
682 mItems = new ArrayList<Item>(other.mItems);
683 }
684
685 /**
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700686 * Create a new ClipData holding data of the type
687 * {@link ClipDescription#MIMETYPE_TEXT_PLAIN}.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700688 *
689 * @param label User-visible label for the clip data.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700690 * @param text The actual text in the clip.
691 * @return Returns a new ClipData containing the specified data.
692 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800693 static public ClipData newPlainText(CharSequence label, CharSequence text) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700694 Item item = new Item(text);
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800695 return new ClipData(label, MIMETYPES_TEXT_PLAIN, item);
696 }
697
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700698 /**
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700699 * Create a new ClipData holding data of the type
700 * {@link ClipDescription#MIMETYPE_TEXT_HTML}.
701 *
702 * @param label User-visible label for the clip data.
703 * @param text The text of clip as plain text, for receivers that don't
704 * handle HTML. This is required.
705 * @param htmlText The actual HTML text in the clip.
706 * @return Returns a new ClipData containing the specified data.
707 */
708 static public ClipData newHtmlText(CharSequence label, CharSequence text,
709 String htmlText) {
710 Item item = new Item(text, htmlText);
711 return new ClipData(label, MIMETYPES_TEXT_HTML, item);
712 }
713
714 /**
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700715 * Create a new ClipData holding an Intent with MIME type
716 * {@link ClipDescription#MIMETYPE_TEXT_INTENT}.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700717 *
718 * @param label User-visible label for the clip data.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700719 * @param intent The actual Intent in the clip.
720 * @return Returns a new ClipData containing the specified data.
721 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800722 static public ClipData newIntent(CharSequence label, Intent intent) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700723 Item item = new Item(intent);
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800724 return new ClipData(label, MIMETYPES_TEXT_INTENT, item);
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700725 }
726
727 /**
728 * Create a new ClipData holding a URI. If the URI is a content: URI,
729 * this will query the content provider for the MIME type of its data and
730 * use that as the MIME type. Otherwise, it will use the MIME type
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700731 * {@link ClipDescription#MIMETYPE_TEXT_URILIST}.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700732 *
733 * @param resolver ContentResolver used to get information about the URI.
734 * @param label User-visible label for the clip data.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700735 * @param uri The URI in the clip.
736 * @return Returns a new ClipData containing the specified data.
737 */
738 static public ClipData newUri(ContentResolver resolver, CharSequence label,
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800739 Uri uri) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700740 Item item = new Item(uri);
741 String[] mimeTypes = null;
742 if ("content".equals(uri.getScheme())) {
743 String realType = resolver.getType(uri);
744 mimeTypes = resolver.getStreamTypes(uri, "*/*");
745 if (mimeTypes == null) {
746 if (realType != null) {
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700747 mimeTypes = new String[] { realType, ClipDescription.MIMETYPE_TEXT_URILIST };
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700748 }
749 } else {
750 String[] tmp = new String[mimeTypes.length + (realType != null ? 2 : 1)];
751 int i = 0;
752 if (realType != null) {
753 tmp[0] = realType;
754 i++;
755 }
756 System.arraycopy(mimeTypes, 0, tmp, i, mimeTypes.length);
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700757 tmp[i + mimeTypes.length] = ClipDescription.MIMETYPE_TEXT_URILIST;
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700758 mimeTypes = tmp;
759 }
760 }
761 if (mimeTypes == null) {
762 mimeTypes = MIMETYPES_TEXT_URILIST;
763 }
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800764 return new ClipData(label, mimeTypes, item);
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700765 }
766
767 /**
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700768 * Create a new ClipData holding an URI with MIME type
769 * {@link ClipDescription#MIMETYPE_TEXT_URILIST}.
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800770 * Unlike {@link #newUri(ContentResolver, CharSequence, Uri)}, nothing
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700771 * is inferred about the URI -- if it is a content: URI holding a bitmap,
772 * the reported type will still be uri-list. Use this with care!
773 *
774 * @param label User-visible label for the clip data.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700775 * @param uri The URI in the clip.
776 * @return Returns a new ClipData containing the specified data.
777 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800778 static public ClipData newRawUri(CharSequence label, Uri uri) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700779 Item item = new Item(uri);
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800780 return new ClipData(label, MIMETYPES_TEXT_URILIST, item);
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700781 }
782
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700783 /**
784 * Return the {@link ClipDescription} associated with this data, describing
785 * what it contains.
786 */
787 public ClipDescription getDescription() {
788 return mClipDescription;
789 }
790
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800791 /**
792 * Add a new Item to the overall ClipData container.
793 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700794 public void addItem(Item item) {
795 if (item == null) {
796 throw new NullPointerException("item is null");
797 }
798 mItems.add(item);
799 }
800
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800801 /** @hide */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700802 public Bitmap getIcon() {
803 return mIcon;
804 }
805
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800806 /**
807 * Return the number of items in the clip data.
808 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700809 public int getItemCount() {
810 return mItems.size();
811 }
812
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800813 /**
814 * Return a single item inside of the clip data. The index can range
815 * from 0 to {@link #getItemCount()}-1.
816 */
817 public Item getItemAt(int index) {
Dianne Hackborn9f531192010-08-04 17:48:03 -0700818 return mItems.get(index);
819 }
820
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700821 /**
822 * Prepare this {@link ClipData} to leave an app process.
823 *
824 * @hide
825 */
Jeff Sharkey344744b2016-01-28 19:03:30 -0700826 public void prepareToLeaveProcess(boolean leavingPackage) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700827 final int size = mItems.size();
828 for (int i = 0; i < size; i++) {
829 final Item item = mItems.get(i);
830 if (item.mIntent != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700831 item.mIntent.prepareToLeaveProcess(leavingPackage);
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700832 }
Jeff Sharkey344744b2016-01-28 19:03:30 -0700833 if (item.mUri != null && StrictMode.vmFileUriExposureEnabled() && leavingPackage) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700834 item.mUri.checkFileUriExposed("ClipData.Item.getUri()");
835 }
836 }
837 }
838
Jeff Sharkeyd136e512016-03-09 22:30:56 -0700839 /** {@hide} */
840 public void prepareToEnterProcess() {
841 final int size = mItems.size();
842 for (int i = 0; i < size; i++) {
843 final Item item = mItems.get(i);
844 if (item.mIntent != null) {
845 item.mIntent.prepareToEnterProcess();
846 }
847 }
848 }
849
Nicolas Prevotd1c99b12014-07-04 16:56:17 +0100850 /** @hide */
851 public void fixUris(int contentUserHint) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +0100852 final int size = mItems.size();
853 for (int i = 0; i < size; i++) {
854 final Item item = mItems.get(i);
855 if (item.mIntent != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +0100856 item.mIntent.fixUris(contentUserHint);
Nicolas Prevotd85fc722014-04-16 19:52:08 +0100857 }
858 if (item.mUri != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +0100859 item.mUri = maybeAddUserId(item.mUri, contentUserHint);
Nicolas Prevotd85fc722014-04-16 19:52:08 +0100860 }
861 }
862 }
863
Nicolas Prevotf1939902014-06-25 09:29:02 +0100864 /**
865 * Only fixing the data field of the intents
866 * @hide
867 */
868 public void fixUrisLight(int contentUserHint) {
869 final int size = mItems.size();
870 for (int i = 0; i < size; i++) {
871 final Item item = mItems.get(i);
872 if (item.mIntent != null) {
873 Uri data = item.mIntent.getData();
874 if (data != null) {
875 item.mIntent.setData(maybeAddUserId(data, contentUserHint));
876 }
877 }
878 if (item.mUri != null) {
879 item.mUri = maybeAddUserId(item.mUri, contentUserHint);
880 }
881 }
882 }
883
Dianne Hackborn9f531192010-08-04 17:48:03 -0700884 @Override
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800885 public String toString() {
886 StringBuilder b = new StringBuilder(128);
887
888 b.append("ClipData { ");
889 toShortString(b);
890 b.append(" }");
891
892 return b.toString();
893 }
894
895 /** @hide */
896 public void toShortString(StringBuilder b) {
897 boolean first;
898 if (mClipDescription != null) {
899 first = !mClipDescription.toShortString(b);
900 } else {
901 first = true;
902 }
903 if (mIcon != null) {
904 if (!first) {
905 b.append(' ');
906 }
907 first = false;
908 b.append("I:");
909 b.append(mIcon.getWidth());
910 b.append('x');
911 b.append(mIcon.getHeight());
912 }
913 for (int i=0; i<mItems.size(); i++) {
914 if (!first) {
915 b.append(' ');
916 }
917 first = false;
918 b.append('{');
919 mItems.get(i).toShortString(b);
920 b.append('}');
921 }
922 }
923
Dianne Hackbornae498722015-08-14 13:29:47 -0700924 /** @hide */
925 public void toShortStringShortItems(StringBuilder b, boolean first) {
926 if (mItems.size() > 0) {
927 if (!first) {
928 b.append(' ');
929 }
930 mItems.get(0).toShortString(b);
931 if (mItems.size() > 1) {
932 b.append(" ...");
933 }
934 }
935 }
936
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -0800937 /** @hide */
938 public void collectUris(List<Uri> out) {
939 for (int i = 0; i < mItems.size(); ++i) {
940 ClipData.Item item = getItemAt(i);
941
942 if (item.getUri() != null) {
943 out.add(item.getUri());
944 }
945
946 Intent intent = item.getIntent();
947 if (intent != null) {
948 if (intent.getData() != null) {
949 out.add(intent.getData());
950 }
951 if (intent.getClipData() != null) {
952 intent.getClipData().collectUris(out);
953 }
954 }
955 }
956 }
957
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800958 @Override
Dianne Hackborn9f531192010-08-04 17:48:03 -0700959 public int describeContents() {
960 return 0;
961 }
962
963 @Override
964 public void writeToParcel(Parcel dest, int flags) {
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700965 mClipDescription.writeToParcel(dest, flags);
Dianne Hackborn9f531192010-08-04 17:48:03 -0700966 if (mIcon != null) {
967 dest.writeInt(1);
968 mIcon.writeToParcel(dest, flags);
969 } else {
970 dest.writeInt(0);
971 }
972 final int N = mItems.size();
973 dest.writeInt(N);
974 for (int i=0; i<N; i++) {
975 Item item = mItems.get(i);
976 TextUtils.writeToParcel(item.mText, dest, flags);
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700977 dest.writeString(item.mHtmlText);
Dianne Hackborn9f531192010-08-04 17:48:03 -0700978 if (item.mIntent != null) {
979 dest.writeInt(1);
980 item.mIntent.writeToParcel(dest, flags);
981 } else {
982 dest.writeInt(0);
983 }
984 if (item.mUri != null) {
985 dest.writeInt(1);
986 item.mUri.writeToParcel(dest, flags);
987 } else {
988 dest.writeInt(0);
989 }
990 }
991 }
992
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700993 ClipData(Parcel in) {
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700994 mClipDescription = new ClipDescription(in);
Dianne Hackborn9f531192010-08-04 17:48:03 -0700995 if (in.readInt() != 0) {
996 mIcon = Bitmap.CREATOR.createFromParcel(in);
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700997 } else {
998 mIcon = null;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700999 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -08001000 mItems = new ArrayList<Item>();
Dianne Hackborn9f531192010-08-04 17:48:03 -07001001 final int N = in.readInt();
1002 for (int i=0; i<N; i++) {
1003 CharSequence text = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
Dianne Hackbornacb69bb2012-04-13 15:36:06 -07001004 String htmlText = in.readString();
Dianne Hackborn9f531192010-08-04 17:48:03 -07001005 Intent intent = in.readInt() != 0 ? Intent.CREATOR.createFromParcel(in) : null;
1006 Uri uri = in.readInt() != 0 ? Uri.CREATOR.createFromParcel(in) : null;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -07001007 mItems.add(new Item(text, htmlText, intent, uri));
Dianne Hackborn9f531192010-08-04 17:48:03 -07001008 }
1009 }
1010
Dianne Hackborn1040dc42010-08-26 22:11:06 -07001011 public static final Parcelable.Creator<ClipData> CREATOR =
1012 new Parcelable.Creator<ClipData>() {
Dianne Hackborn9f531192010-08-04 17:48:03 -07001013
Dianne Hackborn1040dc42010-08-26 22:11:06 -07001014 public ClipData createFromParcel(Parcel source) {
1015 return new ClipData(source);
Dianne Hackborn9f531192010-08-04 17:48:03 -07001016 }
1017
Dianne Hackborn1040dc42010-08-26 22:11:06 -07001018 public ClipData[] newArray(int size) {
1019 return new ClipData[size];
Dianne Hackborn9f531192010-08-04 17:48:03 -07001020 }
1021 };
1022}