blob: 2b7ea66d2054cbeda0613bb51a7104dcf0b5fa55 [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
Mathew Inwood1c77a112018-08-14 14:06:26 +010024import android.annotation.UnsupportedAppUsage;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070025import android.content.res.AssetFileDescriptor;
Dianne Hackborn9f531192010-08-04 17:48:03 -070026import android.graphics.Bitmap;
27import android.net.Uri;
28import android.os.Parcel;
29import android.os.Parcelable;
Jeff Sharkeya14acd22013-04-02 18:27:45 -070030import android.os.StrictMode;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -070031import android.text.Html;
32import android.text.Spannable;
33import android.text.SpannableStringBuilder;
34import android.text.Spanned;
Dianne Hackborn9f531192010-08-04 17:48:03 -070035import android.text.TextUtils;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -070036import android.text.style.URLSpan;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070037import android.util.Log;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080038import android.util.proto.ProtoOutputStream;
Dianne Hackborn9f531192010-08-04 17:48:03 -070039
Vladislav Kaznacheev8e7b9402016-11-23 15:31:13 -080040import com.android.internal.util.ArrayUtils;
41
Kweku Adams85f2fbc2017-12-18 12:04:12 -080042import libcore.io.IoUtils;
43
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070044import java.io.FileInputStream;
45import java.io.FileNotFoundException;
46import java.io.IOException;
47import java.io.InputStreamReader;
Dianne Hackborn9f531192010-08-04 17:48:03 -070048import java.util.ArrayList;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080049import java.util.List;
Dianne Hackborn9f531192010-08-04 17:48:03 -070050
51/**
52 * Representation of a clipped data on the clipboard.
53 *
Vladislav Kaznacheev7e4669c2016-05-02 17:04:08 -070054 * <p>ClipData is a complex type containing one or more Item instances,
Dianne Hackborn9f531192010-08-04 17:48:03 -070055 * each of which can hold one or more representations of an item of data.
Vladislav Kaznacheev7e4669c2016-05-02 17:04:08 -070056 * For display to the user, it also has a label.</p>
Dianne Hackborn9f531192010-08-04 17:48:03 -070057 *
Dianne Hackbornf834dfa2010-10-26 12:43:57 -070058 * <p>A ClipData contains a {@link ClipDescription}, which describes
59 * important meta-data about the clip. In particular, its
60 * {@link ClipDescription#getMimeType(int) getDescription().getMimeType(int)}
Dianne Hackborn1040dc42010-08-26 22:11:06 -070061 * must return correct MIME type(s) describing the data in the clip. For help
62 * in correctly constructing a clip with the correct MIME type, use
Dianne Hackborn327fbd22011-01-17 14:38:50 -080063 * {@link #newPlainText(CharSequence, CharSequence)},
64 * {@link #newUri(ContentResolver, CharSequence, Uri)}, and
65 * {@link #newIntent(CharSequence, Intent)}.
Dianne Hackborn1040dc42010-08-26 22:11:06 -070066 *
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070067 * <p>Each Item instance can be one of three main classes of data: a simple
68 * CharSequence of text, a single Intent object, or a Uri. See {@link Item}
69 * for more details.
Dianne Hackborn9f531192010-08-04 17:48:03 -070070 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080071 * <div class="special reference">
72 * <h3>Developer Guides</h3>
73 * <p>For more information about using the clipboard framework, read the
74 * <a href="{@docRoot}guide/topics/clipboard/copy-paste.html">Copy and Paste</a>
75 * developer guide.</p>
76 * </div>
77 *
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070078 * <a name="ImplementingPaste"></a>
79 * <h3>Implementing Paste or Drop</h3>
80 *
Vladislav Kaznacheev7e4669c2016-05-02 17:04:08 -070081 * <p>To implement a paste or drop of a ClipData object into an application,
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070082 * the application must correctly interpret the data for its use. If the {@link Item}
83 * it contains is simple text or an Intent, there is little to be done: text
84 * can only be interpreted as text, and an Intent will typically be used for
85 * creating shortcuts (such as placing icons on the home screen) or other
86 * actions.
87 *
88 * <p>If all you want is the textual representation of the clipped data, you
89 * can use the convenience method {@link Item#coerceToText Item.coerceToText}.
Dianne Hackborn1040dc42010-08-26 22:11:06 -070090 * In this case there is generally no need to worry about the MIME types
Dianne Hackbornf834dfa2010-10-26 12:43:57 -070091 * reported by {@link ClipDescription#getMimeType(int) getDescription().getMimeType(int)},
Newton Allen4465d1a2013-11-26 10:25:38 -080092 * since any clip item can always be converted to a string.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070093 *
94 * <p>More complicated exchanges will be done through URIs, in particular
Vladislav Kaznacheev7e4669c2016-05-02 17:04:08 -070095 * "content:" URIs. A content URI allows the recipient of a ClipData item
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070096 * to interact closely with the ContentProvider holding the data in order to
Dianne Hackborn1040dc42010-08-26 22:11:06 -070097 * negotiate the transfer of that data. The clip must also be filled in with
Dianne Hackborn327fbd22011-01-17 14:38:50 -080098 * the available MIME types; {@link #newUri(ContentResolver, CharSequence, Uri)}
Dianne Hackborn1040dc42010-08-26 22:11:06 -070099 * will take care of correctly doing this.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700100 *
101 * <p>For example, here is the paste function of a simple NotePad application.
102 * When retrieving the data from the clipboard, it can do either two things:
103 * if the clipboard contains a URI reference to an existing note, it copies
104 * the entire structure of the note into a new note; otherwise, it simply
105 * coerces the clip into text and uses that as the new note's contents.
106 *
107 * {@sample development/samples/NotePad/src/com/example/android/notepad/NoteEditor.java
108 * paste}
109 *
110 * <p>In many cases an application can paste various types of streams of data. For
111 * example, an e-mail application may want to allow the user to paste an image
112 * or other binary data as an attachment. This is accomplished through the
113 * ContentResolver {@link ContentResolver#getStreamTypes(Uri, String)} and
114 * {@link ContentResolver#openTypedAssetFileDescriptor(Uri, String, android.os.Bundle)}
115 * methods. These allow a client to discover the type(s) of data that a particular
116 * content URI can make available as a stream and retrieve the stream of data.
117 *
118 * <p>For example, the implementation of {@link Item#coerceToText Item.coerceToText}
119 * itself uses this to try to retrieve a URI clip as a stream of text:
120 *
Dianne Hackbornf6d952b2010-08-27 15:41:13 -0700121 * {@sample frameworks/base/core/java/android/content/ClipData.java coerceToText}
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700122 *
123 * <a name="ImplementingCopy"></a>
124 * <h3>Implementing Copy or Drag</h3>
125 *
Vladislav Kaznacheev7e4669c2016-05-02 17:04:08 -0700126 * <p>To be the source of a clip, the application must construct a ClipData
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700127 * object that any recipient can interpret best for their context. If the clip
128 * is to contain a simple text, Intent, or URI, this is easy: an {@link Item}
129 * containing the appropriate data type can be constructed and used.
130 *
131 * <p>More complicated data types require the implementation of support in
132 * a ContentProvider for describing and generating the data for the recipient.
133 * A common scenario is one where an application places on the clipboard the
134 * content: URI of an object that the user has copied, with the data at that
135 * URI consisting of a complicated structure that only other applications with
136 * direct knowledge of the structure can use.
137 *
138 * <p>For applications that do not have intrinsic knowledge of the data structure,
139 * the content provider holding it can make the data available as an arbitrary
140 * number of types of data streams. This is done by implementing the
141 * ContentProvider {@link ContentProvider#getStreamTypes(Uri, String)} and
142 * {@link ContentProvider#openTypedAssetFile(Uri, String, android.os.Bundle)}
143 * methods.
144 *
145 * <p>Going back to our simple NotePad application, this is the implementation
146 * it may have to convert a single note URI (consisting of a title and the note
147 * text) into a stream of plain text data.
148 *
149 * {@sample development/samples/NotePad/src/com/example/android/notepad/NotePadProvider.java
150 * stream}
151 *
152 * <p>The copy operation in our NotePad application is now just a simple matter
153 * of making a clip containing the URI of the note being copied:
154 *
155 * {@sample development/samples/NotePad/src/com/example/android/notepad/NotesList.java
156 * copy}
157 *
158 * <p>Note if a paste operation needs this clip as text (for example to paste
159 * into an editor), then {@link Item#coerceToText(Context)} will ask the content
160 * provider for the clip URI as text and successfully paste the entire note.
Dianne Hackborn9f531192010-08-04 17:48:03 -0700161 */
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700162public class ClipData implements Parcelable {
163 static final String[] MIMETYPES_TEXT_PLAIN = new String[] {
164 ClipDescription.MIMETYPE_TEXT_PLAIN };
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700165 static final String[] MIMETYPES_TEXT_HTML = new String[] {
166 ClipDescription.MIMETYPE_TEXT_HTML };
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700167 static final String[] MIMETYPES_TEXT_URILIST = new String[] {
168 ClipDescription.MIMETYPE_TEXT_URILIST };
169 static final String[] MIMETYPES_TEXT_INTENT = new String[] {
170 ClipDescription.MIMETYPE_TEXT_INTENT };
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700171
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700172 final ClipDescription mClipDescription;
Vladislav Kaznacheev7e4669c2016-05-02 17:04:08 -0700173
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700174 final Bitmap mIcon;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700175
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800176 final ArrayList<Item> mItems;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700177
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700178 /**
Vladislav Kaznacheev7e4669c2016-05-02 17:04:08 -0700179 * Description of a single item in a ClipData.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700180 *
181 * <p>The types than an individual item can currently contain are:</p>
182 *
183 * <ul>
184 * <li> Text: a basic string of text. This is actually a CharSequence,
185 * so it can be formatted text supported by corresponding Android built-in
186 * style spans. (Custom application spans are not supported and will be
187 * stripped when transporting through the clipboard.)
188 * <li> Intent: an arbitrary Intent object. A typical use is the shortcut
189 * to create when pasting a clipped item on to the home screen.
190 * <li> Uri: a URI reference. This may be any URI (such as an http: URI
191 * representing a bookmark), however it is often a content: URI. Using
192 * content provider references as clips like this allows an application to
193 * share complex or large clips through the standard content provider
194 * facilities.
195 * </ul>
196 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700197 public static class Item {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700198 final CharSequence mText;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700199 final String mHtmlText;
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700200 final Intent mIntent;
Mathew Inwood1c77a112018-08-14 14:06:26 +0100201 @UnsupportedAppUsage
Nicolas Prevotd85fc722014-04-16 19:52:08 +0100202 Uri mUri;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700203
Nicolas Prevot58926242016-07-14 19:11:10 +0100204 /** @hide */
205 public Item(Item other) {
206 mText = other.mText;
207 mHtmlText = other.mHtmlText;
208 mIntent = other.mIntent;
209 mUri = other.mUri;
210 }
211
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700212 /**
213 * Create an Item consisting of a single block of (possibly styled) text.
214 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700215 public Item(CharSequence text) {
216 mText = text;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700217 mHtmlText = null;
218 mIntent = null;
219 mUri = null;
220 }
221
222 /**
223 * Create an Item consisting of a single block of (possibly styled) text,
224 * with an alternative HTML formatted representation. You <em>must</em>
225 * supply a plain text representation in addition to HTML text; coercion
226 * will not be done from HTML formated text into plain text.
227 */
228 public Item(CharSequence text, String htmlText) {
229 mText = text;
230 mHtmlText = htmlText;
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700231 mIntent = null;
232 mUri = null;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700233 }
234
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700235 /**
236 * Create an Item consisting of an arbitrary Intent.
237 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700238 public Item(Intent intent) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700239 mText = null;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700240 mHtmlText = null;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700241 mIntent = intent;
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700242 mUri = null;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700243 }
244
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700245 /**
246 * Create an Item consisting of an arbitrary URI.
247 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700248 public Item(Uri uri) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700249 mText = null;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700250 mHtmlText = null;
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700251 mIntent = null;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700252 mUri = uri;
253 }
254
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700255 /**
256 * Create a complex Item, containing multiple representations of
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700257 * text, Intent, and/or URI.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700258 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700259 public Item(CharSequence text, Intent intent, Uri uri) {
260 mText = text;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700261 mHtmlText = null;
262 mIntent = intent;
263 mUri = uri;
264 }
265
266 /**
267 * Create a complex Item, containing multiple representations of
268 * text, HTML text, Intent, and/or URI. If providing HTML text, you
269 * <em>must</em> supply a plain text representation as well; coercion
270 * will not be done from HTML formated text into plain text.
271 */
272 public Item(CharSequence text, String htmlText, Intent intent, Uri uri) {
273 if (htmlText != null && text == null) {
274 throw new IllegalArgumentException(
275 "Plain text must be supplied if HTML text is supplied");
276 }
277 mText = text;
278 mHtmlText = htmlText;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700279 mIntent = intent;
280 mUri = uri;
281 }
282
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700283 /**
284 * Retrieve the raw text contained in this Item.
285 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700286 public CharSequence getText() {
287 return mText;
288 }
289
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700290 /**
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700291 * Retrieve the raw HTML text contained in this Item.
292 */
293 public String getHtmlText() {
294 return mHtmlText;
295 }
296
297 /**
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700298 * Retrieve the raw Intent contained in this Item.
299 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700300 public Intent getIntent() {
301 return mIntent;
302 }
303
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700304 /**
305 * Retrieve the raw URI contained in this Item.
306 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700307 public Uri getUri() {
308 return mUri;
309 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700310
311 /**
312 * Turn this item into text, regardless of the type of data it
313 * actually contains.
314 *
315 * <p>The algorithm for deciding what text to return is:
316 * <ul>
317 * <li> If {@link #getText} is non-null, return that.
318 * <li> If {@link #getUri} is non-null, try to retrieve its data
319 * as a text stream from its content provider. If this succeeds, copy
320 * the text into a String and return it. If it is not a content: URI or
321 * the content provider does not supply a text representation, return
322 * the raw URI as a string.
323 * <li> If {@link #getIntent} is non-null, convert that to an intent:
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700324 * URI and return it.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700325 * <li> Otherwise, return an empty string.
326 * </ul>
327 *
328 * @param context The caller's Context, from which its ContentResolver
329 * and other things can be retrieved.
330 * @return Returns the item's textual representation.
331 */
332//BEGIN_INCLUDE(coerceToText)
333 public CharSequence coerceToText(Context context) {
334 // If this Item has an explicit textual value, simply return that.
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700335 CharSequence text = getText();
336 if (text != null) {
337 return text;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700338 }
339
340 // If this Item has a URI value, try using that.
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700341 Uri uri = getUri();
342 if (uri != null) {
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700343 // First see if the URI can be opened as a plain text stream
344 // (of any sub-type). If so, this is the best textual
345 // representation for it.
Daichi Hirono3d9f3fc2017-04-12 14:23:06 +0900346 final ContentResolver resolver = context.getContentResolver();
347 AssetFileDescriptor descr = null;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700348 FileInputStream stream = null;
Daichi Hirono3d9f3fc2017-04-12 14:23:06 +0900349 InputStreamReader reader = null;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700350 try {
Daichi Hirono3d9f3fc2017-04-12 14:23:06 +0900351 try {
352 // Ask for a stream of the desired type.
353 descr = resolver.openTypedAssetFileDescriptor(uri, "text/*", null);
354 } catch (SecurityException e) {
355 Log.w("ClipData", "Failure opening stream", e);
356 } catch (FileNotFoundException|RuntimeException e) {
357 // Unable to open content URI as text... not really an
358 // error, just something to ignore.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700359 }
Daichi Hirono3d9f3fc2017-04-12 14:23:06 +0900360 if (descr != null) {
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700361 try {
Daichi Hirono3d9f3fc2017-04-12 14:23:06 +0900362 stream = descr.createInputStream();
363 reader = new InputStreamReader(stream, "UTF-8");
364
365 // Got it... copy the stream into a local string and return it.
366 final StringBuilder builder = new StringBuilder(128);
367 char[] buffer = new char[8192];
368 int len;
369 while ((len=reader.read(buffer)) > 0) {
370 builder.append(buffer, 0, len);
371 }
372 return builder.toString();
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700373 } catch (IOException e) {
Daichi Hirono3d9f3fc2017-04-12 14:23:06 +0900374 // Something bad has happened.
375 Log.w("ClipData", "Failure loading text", e);
376 return e.toString();
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700377 }
378 }
Daichi Hirono3d9f3fc2017-04-12 14:23:06 +0900379 } finally {
380 IoUtils.closeQuietly(descr);
381 IoUtils.closeQuietly(stream);
382 IoUtils.closeQuietly(reader);
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700383 }
384
Vladislav Kaznacheeve1f5e132017-01-27 16:56:40 -0800385 // If we couldn't open the URI as a stream, use the URI itself as a textual
386 // representation (but not for "content", "android.resource" or "file" schemes).
387 final String scheme = uri.getScheme();
388 if (SCHEME_CONTENT.equals(scheme)
389 || SCHEME_ANDROID_RESOURCE.equals(scheme)
390 || SCHEME_FILE.equals(scheme)) {
391 return "";
392 }
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700393 return uri.toString();
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700394 }
395
396 // Finally, if all we have is an Intent, then we can just turn that
397 // into text. Not the most user-friendly thing, but it's something.
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700398 Intent intent = getIntent();
399 if (intent != null) {
400 return intent.toUri(Intent.URI_INTENT_SCHEME);
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700401 }
402
403 // Shouldn't get here, but just in case...
404 return "";
405 }
406//END_INCLUDE(coerceToText)
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800407
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700408 /**
409 * Like {@link #coerceToHtmlText(Context)}, but any text that would
410 * be returned as HTML formatting will be returned as text with
411 * style spans.
412 * @param context The caller's Context, from which its ContentResolver
413 * and other things can be retrieved.
414 * @return Returns the item's textual representation.
415 */
416 public CharSequence coerceToStyledText(Context context) {
417 CharSequence text = getText();
418 if (text instanceof Spanned) {
419 return text;
420 }
421 String htmlText = getHtmlText();
422 if (htmlText != null) {
423 try {
424 CharSequence newText = Html.fromHtml(htmlText);
425 if (newText != null) {
426 return newText;
427 }
428 } catch (RuntimeException e) {
429 // If anything bad happens, we'll fall back on the plain text.
430 }
431 }
432
433 if (text != null) {
434 return text;
435 }
436 return coerceToHtmlOrStyledText(context, true);
437 }
438
439 /**
440 * Turn this item into HTML text, regardless of the type of data it
441 * actually contains.
442 *
443 * <p>The algorithm for deciding what text to return is:
444 * <ul>
445 * <li> If {@link #getHtmlText} is non-null, return that.
446 * <li> If {@link #getText} is non-null, return that, converting to
447 * valid HTML text. If this text contains style spans,
448 * {@link Html#toHtml(Spanned) Html.toHtml(Spanned)} is used to
449 * convert them to HTML formatting.
450 * <li> If {@link #getUri} is non-null, try to retrieve its data
451 * as a text stream from its content provider. If the provider can
452 * supply text/html data, that will be preferred and returned as-is.
453 * Otherwise, any text/* data will be returned and escaped to HTML.
454 * If it is not a content: URI or the content provider does not supply
455 * a text representation, HTML text containing a link to the URI
456 * will be returned.
457 * <li> If {@link #getIntent} is non-null, convert that to an intent:
458 * URI and return as an HTML link.
459 * <li> Otherwise, return an empty string.
460 * </ul>
461 *
462 * @param context The caller's Context, from which its ContentResolver
463 * and other things can be retrieved.
464 * @return Returns the item's representation as HTML text.
465 */
466 public String coerceToHtmlText(Context context) {
467 // If the item has an explicit HTML value, simply return that.
468 String htmlText = getHtmlText();
469 if (htmlText != null) {
470 return htmlText;
471 }
472
473 // If this Item has a plain text value, return it as HTML.
474 CharSequence text = getText();
475 if (text != null) {
476 if (text instanceof Spanned) {
477 return Html.toHtml((Spanned)text);
478 }
479 return Html.escapeHtml(text);
480 }
481
482 text = coerceToHtmlOrStyledText(context, false);
483 return text != null ? text.toString() : null;
484 }
485
486 private CharSequence coerceToHtmlOrStyledText(Context context, boolean styled) {
487 // If this Item has a URI value, try using that.
488 if (mUri != null) {
489
490 // Check to see what data representations the content
491 // provider supports. We would like HTML text, but if that
492 // is not possible we'll live with plan text.
Vladislav Kaznacheevc14df8e2016-01-22 11:49:13 -0800493 String[] types = null;
494 try {
495 types = context.getContentResolver().getStreamTypes(mUri, "text/*");
496 } catch (SecurityException e) {
497 // No read permission for mUri, assume empty stream types list.
498 }
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700499 boolean hasHtml = false;
500 boolean hasText = false;
501 if (types != null) {
502 for (String type : types) {
503 if ("text/html".equals(type)) {
504 hasHtml = true;
505 } else if (type.startsWith("text/")) {
506 hasText = true;
507 }
508 }
509 }
510
511 // If the provider can serve data we can use, open and load it.
512 if (hasHtml || hasText) {
513 FileInputStream stream = null;
514 try {
515 // Ask for a stream of the desired type.
516 AssetFileDescriptor descr = context.getContentResolver()
517 .openTypedAssetFileDescriptor(mUri,
518 hasHtml ? "text/html" : "text/plain", null);
519 stream = descr.createInputStream();
520 InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
521
522 // Got it... copy the stream into a local string and return it.
523 StringBuilder builder = new StringBuilder(128);
524 char[] buffer = new char[8192];
525 int len;
526 while ((len=reader.read(buffer)) > 0) {
527 builder.append(buffer, 0, len);
528 }
529 String text = builder.toString();
530 if (hasHtml) {
531 if (styled) {
532 // We loaded HTML formatted text and the caller
533 // want styled text, convert it.
534 try {
535 CharSequence newText = Html.fromHtml(text);
536 return newText != null ? newText : text;
537 } catch (RuntimeException e) {
538 return text;
539 }
540 } else {
541 // We loaded HTML formatted text and that is what
542 // the caller wants, just return it.
543 return text.toString();
544 }
545 }
546 if (styled) {
547 // We loaded plain text and the caller wants styled
548 // text, that is all we have so return it.
549 return text;
550 } else {
551 // We loaded plain text and the caller wants HTML
552 // text, escape it for HTML.
553 return Html.escapeHtml(text);
554 }
555
Vladislav Kaznacheevb143ff62017-01-27 16:39:13 -0800556 } catch (SecurityException e) {
557 Log.w("ClipData", "Failure opening stream", e);
558
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700559 } catch (FileNotFoundException e) {
560 // Unable to open content URI as text... not really an
561 // error, just something to ignore.
562
563 } catch (IOException e) {
564 // Something bad has happened.
Vladislav Kaznacheev7e4669c2016-05-02 17:04:08 -0700565 Log.w("ClipData", "Failure loading text", e);
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700566 return Html.escapeHtml(e.toString());
567
568 } finally {
569 if (stream != null) {
570 try {
571 stream.close();
572 } catch (IOException e) {
573 }
574 }
575 }
576 }
577
Vladislav Kaznacheeve1f5e132017-01-27 16:56:40 -0800578 // If we couldn't open the URI as a stream, use the URI itself as a textual
579 // representation (but not for "content", "android.resource" or "file" schemes).
580 final String scheme = mUri.getScheme();
581 if (SCHEME_CONTENT.equals(scheme)
582 || SCHEME_ANDROID_RESOURCE.equals(scheme)
583 || SCHEME_FILE.equals(scheme)) {
584 return "";
585 }
586
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700587 if (styled) {
588 return uriToStyledText(mUri.toString());
589 } else {
590 return uriToHtml(mUri.toString());
591 }
592 }
593
594 // Finally, if all we have is an Intent, then we can just turn that
595 // into text. Not the most user-friendly thing, but it's something.
596 if (mIntent != null) {
597 if (styled) {
598 return uriToStyledText(mIntent.toUri(Intent.URI_INTENT_SCHEME));
599 } else {
600 return uriToHtml(mIntent.toUri(Intent.URI_INTENT_SCHEME));
601 }
602 }
603
604 // Shouldn't get here, but just in case...
605 return "";
606 }
607
608 private String uriToHtml(String uri) {
609 StringBuilder builder = new StringBuilder(256);
610 builder.append("<a href=\"");
Nick Kralevichc92db392012-07-27 13:22:20 -0700611 builder.append(Html.escapeHtml(uri));
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700612 builder.append("\">");
613 builder.append(Html.escapeHtml(uri));
614 builder.append("</a>");
615 return builder.toString();
616 }
617
618 private CharSequence uriToStyledText(String uri) {
619 SpannableStringBuilder builder = new SpannableStringBuilder();
620 builder.append(uri);
621 builder.setSpan(new URLSpan(uri), 0, builder.length(),
622 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
623 return builder;
624 }
625
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800626 @Override
627 public String toString() {
628 StringBuilder b = new StringBuilder(128);
629
630 b.append("ClipData.Item { ");
631 toShortString(b);
632 b.append(" }");
633
634 return b.toString();
635 }
636
637 /** @hide */
638 public void toShortString(StringBuilder b) {
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700639 if (mHtmlText != null) {
640 b.append("H:");
641 b.append(mHtmlText);
642 } else if (mText != null) {
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800643 b.append("T:");
644 b.append(mText);
645 } else if (mUri != null) {
646 b.append("U:");
647 b.append(mUri);
648 } else if (mIntent != null) {
649 b.append("I:");
650 mIntent.toShortString(b, true, true, true, true);
651 } else {
652 b.append("NULL");
653 }
654 }
Dianne Hackbornae498722015-08-14 13:29:47 -0700655
656 /** @hide */
657 public void toShortSummaryString(StringBuilder b) {
658 if (mHtmlText != null) {
659 b.append("HTML");
660 } else if (mText != null) {
661 b.append("TEXT");
662 } else if (mUri != null) {
663 b.append("U:");
664 b.append(mUri);
665 } else if (mIntent != null) {
666 b.append("I:");
667 mIntent.toShortString(b, true, true, true, true);
668 } else {
669 b.append("NULL");
670 }
671 }
Kweku Adams85f2fbc2017-12-18 12:04:12 -0800672
673 /** @hide */
674 public void writeToProto(ProtoOutputStream proto, long fieldId) {
675 final long token = proto.start(fieldId);
676
677 if (mHtmlText != null) {
678 proto.write(ClipDataProto.Item.HTML_TEXT, mHtmlText);
679 } else if (mText != null) {
680 proto.write(ClipDataProto.Item.TEXT, mText.toString());
681 } else if (mUri != null) {
682 proto.write(ClipDataProto.Item.URI, mUri.toString());
683 } else if (mIntent != null) {
684 mIntent.writeToProto(proto, ClipDataProto.Item.INTENT, true, true, true, true);
685 } else {
686 proto.write(ClipDataProto.Item.NOTHING, true);
687 }
688
689 proto.end(token);
690 }
Dianne Hackborn9f531192010-08-04 17:48:03 -0700691 }
692
693 /**
694 * Create a new clip.
695 *
696 * @param label Label to show to the user describing this clip.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700697 * @param mimeTypes An array of MIME types this data is available as.
Dianne Hackborn9f531192010-08-04 17:48:03 -0700698 * @param item The contents of the first item in the clip.
699 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800700 public ClipData(CharSequence label, String[] mimeTypes, Item item) {
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700701 mClipDescription = new ClipDescription(label, mimeTypes);
Dianne Hackborn9f531192010-08-04 17:48:03 -0700702 if (item == null) {
703 throw new NullPointerException("item is null");
704 }
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800705 mIcon = null;
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800706 mItems = new ArrayList<Item>();
Dianne Hackborn9f531192010-08-04 17:48:03 -0700707 mItems.add(item);
708 }
709
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700710 /**
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700711 * Create a new clip.
712 *
713 * @param description The ClipDescription describing the clip contents.
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700714 * @param item The contents of the first item in the clip.
715 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800716 public ClipData(ClipDescription description, Item item) {
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700717 mClipDescription = description;
718 if (item == null) {
719 throw new NullPointerException("item is null");
720 }
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800721 mIcon = null;
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800722 mItems = new ArrayList<Item>();
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700723 mItems.add(item);
724 }
725
726 /**
Steve McKay57eaaf62016-06-13 12:53:31 -0700727 * Create a new clip.
728 *
729 * @param description The ClipDescription describing the clip contents.
730 * @param items The items in the clip. Not that a defensive copy of this
731 * list is not made, so caution should be taken to ensure the
732 * list is not available for further modification.
733 * @hide
734 */
735 public ClipData(ClipDescription description, ArrayList<Item> items) {
736 mClipDescription = description;
737 if (items == null) {
738 throw new NullPointerException("item is null");
739 }
740 mIcon = null;
741 mItems = items;
742 }
743
744 /**
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800745 * Create a new clip that is a copy of another clip. This does a deep-copy
746 * of all items in the clip.
747 *
748 * @param other The existing ClipData that is to be copied.
749 */
750 public ClipData(ClipData other) {
751 mClipDescription = other.mClipDescription;
752 mIcon = other.mIcon;
753 mItems = new ArrayList<Item>(other.mItems);
754 }
755
756 /**
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700757 * Create a new ClipData holding data of the type
758 * {@link ClipDescription#MIMETYPE_TEXT_PLAIN}.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700759 *
760 * @param label User-visible label for the clip data.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700761 * @param text The actual text in the clip.
762 * @return Returns a new ClipData containing the specified data.
763 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800764 static public ClipData newPlainText(CharSequence label, CharSequence text) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700765 Item item = new Item(text);
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800766 return new ClipData(label, MIMETYPES_TEXT_PLAIN, item);
767 }
768
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700769 /**
Dianne Hackbornacb69bb2012-04-13 15:36:06 -0700770 * Create a new ClipData holding data of the type
771 * {@link ClipDescription#MIMETYPE_TEXT_HTML}.
772 *
773 * @param label User-visible label for the clip data.
774 * @param text The text of clip as plain text, for receivers that don't
775 * handle HTML. This is required.
776 * @param htmlText The actual HTML text in the clip.
777 * @return Returns a new ClipData containing the specified data.
778 */
779 static public ClipData newHtmlText(CharSequence label, CharSequence text,
780 String htmlText) {
781 Item item = new Item(text, htmlText);
782 return new ClipData(label, MIMETYPES_TEXT_HTML, item);
783 }
784
785 /**
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700786 * Create a new ClipData holding an Intent with MIME type
787 * {@link ClipDescription#MIMETYPE_TEXT_INTENT}.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700788 *
789 * @param label User-visible label for the clip data.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700790 * @param intent The actual Intent in the clip.
791 * @return Returns a new ClipData containing the specified data.
792 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800793 static public ClipData newIntent(CharSequence label, Intent intent) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700794 Item item = new Item(intent);
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800795 return new ClipData(label, MIMETYPES_TEXT_INTENT, item);
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700796 }
797
798 /**
799 * Create a new ClipData holding a URI. If the URI is a content: URI,
800 * this will query the content provider for the MIME type of its data and
801 * use that as the MIME type. Otherwise, it will use the MIME type
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700802 * {@link ClipDescription#MIMETYPE_TEXT_URILIST}.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700803 *
804 * @param resolver ContentResolver used to get information about the URI.
805 * @param label User-visible label for the clip data.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700806 * @param uri The URI in the clip.
807 * @return Returns a new ClipData containing the specified data.
808 */
809 static public ClipData newUri(ContentResolver resolver, CharSequence label,
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800810 Uri uri) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700811 Item item = new Item(uri);
Vladislav Kaznacheev8e7b9402016-11-23 15:31:13 -0800812 String[] mimeTypes = getMimeTypes(resolver, uri);
813 return new ClipData(label, mimeTypes, item);
814 }
815
816 /**
817 * Finds all applicable MIME types for a given URI.
818 *
819 * @param resolver ContentResolver used to get information about the URI.
820 * @param uri The URI.
821 * @return Returns an array of MIME types.
822 */
823 private static String[] getMimeTypes(ContentResolver resolver, Uri uri) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700824 String[] mimeTypes = null;
Vladislav Kaznacheeve1f5e132017-01-27 16:56:40 -0800825 if (SCHEME_CONTENT.equals(uri.getScheme())) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700826 String realType = resolver.getType(uri);
827 mimeTypes = resolver.getStreamTypes(uri, "*/*");
Vladislav Kaznacheevf67661c2016-05-03 15:02:33 -0700828 if (realType != null) {
829 if (mimeTypes == null) {
830 mimeTypes = new String[] { realType };
Vladislav Kaznacheev8e7b9402016-11-23 15:31:13 -0800831 } else if (!ArrayUtils.contains(mimeTypes, realType)) {
Vladislav Kaznacheevf67661c2016-05-03 15:02:33 -0700832 String[] tmp = new String[mimeTypes.length + 1];
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700833 tmp[0] = realType;
Vladislav Kaznacheevf67661c2016-05-03 15:02:33 -0700834 System.arraycopy(mimeTypes, 0, tmp, 1, mimeTypes.length);
835 mimeTypes = tmp;
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700836 }
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700837 }
838 }
839 if (mimeTypes == null) {
840 mimeTypes = MIMETYPES_TEXT_URILIST;
841 }
Vladislav Kaznacheev8e7b9402016-11-23 15:31:13 -0800842 return mimeTypes;
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700843 }
844
845 /**
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700846 * Create a new ClipData holding an URI with MIME type
847 * {@link ClipDescription#MIMETYPE_TEXT_URILIST}.
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800848 * Unlike {@link #newUri(ContentResolver, CharSequence, Uri)}, nothing
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700849 * is inferred about the URI -- if it is a content: URI holding a bitmap,
850 * the reported type will still be uri-list. Use this with care!
851 *
852 * @param label User-visible label for the clip data.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700853 * @param uri The URI in the clip.
854 * @return Returns a new ClipData containing the specified data.
855 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800856 static public ClipData newRawUri(CharSequence label, Uri uri) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700857 Item item = new Item(uri);
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800858 return new ClipData(label, MIMETYPES_TEXT_URILIST, item);
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700859 }
860
Dianne Hackbornf834dfa2010-10-26 12:43:57 -0700861 /**
862 * Return the {@link ClipDescription} associated with this data, describing
863 * what it contains.
864 */
865 public ClipDescription getDescription() {
866 return mClipDescription;
867 }
Vladislav Kaznacheevf67661c2016-05-03 15:02:33 -0700868
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800869 /**
870 * Add a new Item to the overall ClipData container.
Vladislav Kaznacheevf67661c2016-05-03 15:02:33 -0700871 * <p> This method will <em>not</em> update the list of available MIME types in the
872 * {@link ClipDescription}. It should be used only when adding items which do not add new
Vladislav Kaznacheevc35c02f2017-04-20 08:38:21 -0700873 * MIME types to this clip. If this is not the case, use {@link #addItem(ContentResolver, Item)}
Vladislav Kaznacheev8e7b9402016-11-23 15:31:13 -0800874 * or call {@link #ClipData(CharSequence, String[], Item)} with a complete list of MIME types.
Vladislav Kaznacheevf67661c2016-05-03 15:02:33 -0700875 * @param item Item to be added.
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800876 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700877 public void addItem(Item item) {
878 if (item == null) {
879 throw new NullPointerException("item is null");
880 }
881 mItems.add(item);
882 }
883
Vladislav Kaznacheevc35c02f2017-04-20 08:38:21 -0700884 /** @removed use #addItem(ContentResolver, Item) instead */
885 @Deprecated
886 public void addItem(Item item, ContentResolver resolver) {
887 addItem(resolver, item);
888 }
889
Vladislav Kaznacheev8e7b9402016-11-23 15:31:13 -0800890 /**
891 * Add a new Item to the overall ClipData container.
892 * <p> Unlike {@link #addItem(Item)}, this method will update the list of available MIME types
893 * in the {@link ClipDescription}.
Vladislav Kaznacheev8e7b9402016-11-23 15:31:13 -0800894 * @param resolver ContentResolver used to get information about the URI possibly contained in
895 * the item.
Vladislav Kaznacheevc35c02f2017-04-20 08:38:21 -0700896 * @param item Item to be added.
Vladislav Kaznacheev8e7b9402016-11-23 15:31:13 -0800897 */
Vladislav Kaznacheevc35c02f2017-04-20 08:38:21 -0700898 public void addItem(ContentResolver resolver, Item item) {
Vladislav Kaznacheev8e7b9402016-11-23 15:31:13 -0800899 addItem(item);
900
901 if (item.getHtmlText() != null) {
902 mClipDescription.addMimeTypes(MIMETYPES_TEXT_HTML);
903 } else if (item.getText() != null) {
904 mClipDescription.addMimeTypes(MIMETYPES_TEXT_PLAIN);
905 }
906
907 if (item.getIntent() != null) {
908 mClipDescription.addMimeTypes(MIMETYPES_TEXT_INTENT);
909 }
910
911 if (item.getUri() != null) {
912 mClipDescription.addMimeTypes(getMimeTypes(resolver, item.getUri()));
913 }
914 }
915
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800916 /** @hide */
Mathew Inwood1c77a112018-08-14 14:06:26 +0100917 @UnsupportedAppUsage
Dianne Hackborn9f531192010-08-04 17:48:03 -0700918 public Bitmap getIcon() {
919 return mIcon;
920 }
921
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800922 /**
923 * Return the number of items in the clip data.
924 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700925 public int getItemCount() {
926 return mItems.size();
927 }
928
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800929 /**
930 * Return a single item inside of the clip data. The index can range
931 * from 0 to {@link #getItemCount()}-1.
932 */
933 public Item getItemAt(int index) {
Dianne Hackborn9f531192010-08-04 17:48:03 -0700934 return mItems.get(index);
935 }
936
Nicolas Prevot58926242016-07-14 19:11:10 +0100937 /** @hide */
938 public void setItemAt(int index, Item item) {
939 mItems.set(index, item);
940 }
941
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700942 /**
943 * Prepare this {@link ClipData} to leave an app process.
944 *
945 * @hide
946 */
Jeff Sharkey344744b2016-01-28 19:03:30 -0700947 public void prepareToLeaveProcess(boolean leavingPackage) {
Jeff Sharkeyf361f2f2016-12-21 10:48:42 -0700948 // Assume that callers are going to be granting permissions
949 prepareToLeaveProcess(leavingPackage, Intent.FLAG_GRANT_READ_URI_PERMISSION);
Jeff Sharkeyfb833f32016-12-01 14:59:59 -0700950 }
951
952 /**
953 * Prepare this {@link ClipData} to leave an app process.
954 *
955 * @hide
956 */
957 public void prepareToLeaveProcess(boolean leavingPackage, int intentFlags) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700958 final int size = mItems.size();
959 for (int i = 0; i < size; i++) {
960 final Item item = mItems.get(i);
961 if (item.mIntent != null) {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700962 item.mIntent.prepareToLeaveProcess(leavingPackage);
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700963 }
Jeff Sharkeyfb833f32016-12-01 14:59:59 -0700964 if (item.mUri != null && leavingPackage) {
965 if (StrictMode.vmFileUriExposureEnabled()) {
966 item.mUri.checkFileUriExposed("ClipData.Item.getUri()");
967 }
968 if (StrictMode.vmContentUriWithoutPermissionEnabled()) {
969 item.mUri.checkContentUriWithoutPermission("ClipData.Item.getUri()",
970 intentFlags);
971 }
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700972 }
973 }
974 }
975
Jeff Sharkeyd136e512016-03-09 22:30:56 -0700976 /** {@hide} */
977 public void prepareToEnterProcess() {
978 final int size = mItems.size();
979 for (int i = 0; i < size; i++) {
980 final Item item = mItems.get(i);
981 if (item.mIntent != null) {
982 item.mIntent.prepareToEnterProcess();
983 }
984 }
985 }
986
Nicolas Prevotd1c99b12014-07-04 16:56:17 +0100987 /** @hide */
988 public void fixUris(int contentUserHint) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +0100989 final int size = mItems.size();
990 for (int i = 0; i < size; i++) {
991 final Item item = mItems.get(i);
992 if (item.mIntent != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +0100993 item.mIntent.fixUris(contentUserHint);
Nicolas Prevotd85fc722014-04-16 19:52:08 +0100994 }
995 if (item.mUri != null) {
Nicolas Prevotd1c99b12014-07-04 16:56:17 +0100996 item.mUri = maybeAddUserId(item.mUri, contentUserHint);
Nicolas Prevotd85fc722014-04-16 19:52:08 +0100997 }
998 }
999 }
1000
Nicolas Prevotf1939902014-06-25 09:29:02 +01001001 /**
1002 * Only fixing the data field of the intents
1003 * @hide
1004 */
1005 public void fixUrisLight(int contentUserHint) {
1006 final int size = mItems.size();
1007 for (int i = 0; i < size; i++) {
1008 final Item item = mItems.get(i);
1009 if (item.mIntent != null) {
1010 Uri data = item.mIntent.getData();
1011 if (data != null) {
1012 item.mIntent.setData(maybeAddUserId(data, contentUserHint));
1013 }
1014 }
1015 if (item.mUri != null) {
1016 item.mUri = maybeAddUserId(item.mUri, contentUserHint);
1017 }
1018 }
1019 }
1020
Dianne Hackborn9f531192010-08-04 17:48:03 -07001021 @Override
Dianne Hackborn21c241e2012-03-08 13:57:23 -08001022 public String toString() {
1023 StringBuilder b = new StringBuilder(128);
1024
1025 b.append("ClipData { ");
1026 toShortString(b);
1027 b.append(" }");
1028
1029 return b.toString();
1030 }
1031
1032 /** @hide */
1033 public void toShortString(StringBuilder b) {
1034 boolean first;
1035 if (mClipDescription != null) {
1036 first = !mClipDescription.toShortString(b);
1037 } else {
1038 first = true;
1039 }
1040 if (mIcon != null) {
1041 if (!first) {
1042 b.append(' ');
1043 }
1044 first = false;
1045 b.append("I:");
1046 b.append(mIcon.getWidth());
1047 b.append('x');
1048 b.append(mIcon.getHeight());
1049 }
1050 for (int i=0; i<mItems.size(); i++) {
1051 if (!first) {
1052 b.append(' ');
1053 }
1054 first = false;
1055 b.append('{');
1056 mItems.get(i).toShortString(b);
1057 b.append('}');
1058 }
1059 }
1060
Dianne Hackbornae498722015-08-14 13:29:47 -07001061 /** @hide */
1062 public void toShortStringShortItems(StringBuilder b, boolean first) {
1063 if (mItems.size() > 0) {
1064 if (!first) {
1065 b.append(' ');
1066 }
1067 mItems.get(0).toShortString(b);
1068 if (mItems.size() > 1) {
1069 b.append(" ...");
1070 }
1071 }
1072 }
1073
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -08001074 /** @hide */
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001075 public void writeToProto(ProtoOutputStream proto, long fieldId) {
1076 final long token = proto.start(fieldId);
1077
1078 if (mClipDescription != null) {
1079 mClipDescription.writeToProto(proto, ClipDataProto.DESCRIPTION);
1080 }
1081 if (mIcon != null) {
1082 final long iToken = proto.start(ClipDataProto.ICON);
1083 proto.write(ClipDataProto.Icon.WIDTH, mIcon.getWidth());
1084 proto.write(ClipDataProto.Icon.HEIGHT, mIcon.getHeight());
1085 proto.end(iToken);
1086 }
1087 for (int i = 0; i < mItems.size(); i++) {
1088 mItems.get(i).writeToProto(proto, ClipDataProto.ITEMS);
1089 }
1090
1091 proto.end(token);
1092 }
1093
1094 /** @hide */
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -08001095 public void collectUris(List<Uri> out) {
1096 for (int i = 0; i < mItems.size(); ++i) {
1097 ClipData.Item item = getItemAt(i);
1098
1099 if (item.getUri() != null) {
1100 out.add(item.getUri());
1101 }
1102
1103 Intent intent = item.getIntent();
1104 if (intent != null) {
1105 if (intent.getData() != null) {
1106 out.add(intent.getData());
1107 }
1108 if (intent.getClipData() != null) {
1109 intent.getClipData().collectUris(out);
1110 }
1111 }
1112 }
1113 }
1114
Dianne Hackborn21c241e2012-03-08 13:57:23 -08001115 @Override
Dianne Hackborn9f531192010-08-04 17:48:03 -07001116 public int describeContents() {
1117 return 0;
1118 }
1119
1120 @Override
1121 public void writeToParcel(Parcel dest, int flags) {
Dianne Hackbornf834dfa2010-10-26 12:43:57 -07001122 mClipDescription.writeToParcel(dest, flags);
Dianne Hackborn9f531192010-08-04 17:48:03 -07001123 if (mIcon != null) {
1124 dest.writeInt(1);
1125 mIcon.writeToParcel(dest, flags);
1126 } else {
1127 dest.writeInt(0);
1128 }
1129 final int N = mItems.size();
1130 dest.writeInt(N);
1131 for (int i=0; i<N; i++) {
1132 Item item = mItems.get(i);
1133 TextUtils.writeToParcel(item.mText, dest, flags);
Dianne Hackbornacb69bb2012-04-13 15:36:06 -07001134 dest.writeString(item.mHtmlText);
Dianne Hackborn9f531192010-08-04 17:48:03 -07001135 if (item.mIntent != null) {
1136 dest.writeInt(1);
1137 item.mIntent.writeToParcel(dest, flags);
1138 } else {
1139 dest.writeInt(0);
1140 }
1141 if (item.mUri != null) {
1142 dest.writeInt(1);
1143 item.mUri.writeToParcel(dest, flags);
1144 } else {
1145 dest.writeInt(0);
1146 }
1147 }
1148 }
1149
Dianne Hackborn1040dc42010-08-26 22:11:06 -07001150 ClipData(Parcel in) {
Dianne Hackbornf834dfa2010-10-26 12:43:57 -07001151 mClipDescription = new ClipDescription(in);
Dianne Hackborn9f531192010-08-04 17:48:03 -07001152 if (in.readInt() != 0) {
1153 mIcon = Bitmap.CREATOR.createFromParcel(in);
Dianne Hackborn1040dc42010-08-26 22:11:06 -07001154 } else {
1155 mIcon = null;
Dianne Hackborn9f531192010-08-04 17:48:03 -07001156 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -08001157 mItems = new ArrayList<Item>();
Dianne Hackborn9f531192010-08-04 17:48:03 -07001158 final int N = in.readInt();
1159 for (int i=0; i<N; i++) {
1160 CharSequence text = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
Dianne Hackbornacb69bb2012-04-13 15:36:06 -07001161 String htmlText = in.readString();
Dianne Hackborn9f531192010-08-04 17:48:03 -07001162 Intent intent = in.readInt() != 0 ? Intent.CREATOR.createFromParcel(in) : null;
1163 Uri uri = in.readInt() != 0 ? Uri.CREATOR.createFromParcel(in) : null;
Dianne Hackbornacb69bb2012-04-13 15:36:06 -07001164 mItems.add(new Item(text, htmlText, intent, uri));
Dianne Hackborn9f531192010-08-04 17:48:03 -07001165 }
1166 }
1167
Dianne Hackborn1040dc42010-08-26 22:11:06 -07001168 public static final Parcelable.Creator<ClipData> CREATOR =
1169 new Parcelable.Creator<ClipData>() {
Dianne Hackborn9f531192010-08-04 17:48:03 -07001170
Steve McKay57eaaf62016-06-13 12:53:31 -07001171 @Override
Dianne Hackborn1040dc42010-08-26 22:11:06 -07001172 public ClipData createFromParcel(Parcel source) {
1173 return new ClipData(source);
Dianne Hackborn9f531192010-08-04 17:48:03 -07001174 }
1175
Steve McKay57eaaf62016-06-13 12:53:31 -07001176 @Override
Dianne Hackborn1040dc42010-08-26 22:11:06 -07001177 public ClipData[] newArray(int size) {
1178 return new ClipData[size];
Dianne Hackborn9f531192010-08-04 17:48:03 -07001179 }
1180 };
1181}