Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2010, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.content; |
| 18 | |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 19 | import static android.content.ContentProvider.maybeAddUserId; |
Vladislav Kaznacheev | e1f5e13 | 2017-01-27 16:56:40 -0800 | [diff] [blame] | 20 | import static android.content.ContentResolver.SCHEME_ANDROID_RESOURCE; |
| 21 | import static android.content.ContentResolver.SCHEME_CONTENT; |
| 22 | import static android.content.ContentResolver.SCHEME_FILE; |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 23 | |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame^] | 24 | import android.annotation.UnsupportedAppUsage; |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 25 | import android.content.res.AssetFileDescriptor; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 26 | import android.graphics.Bitmap; |
| 27 | import android.net.Uri; |
| 28 | import android.os.Parcel; |
| 29 | import android.os.Parcelable; |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 30 | import android.os.StrictMode; |
Dianne Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 31 | import android.text.Html; |
| 32 | import android.text.Spannable; |
| 33 | import android.text.SpannableStringBuilder; |
| 34 | import android.text.Spanned; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 35 | import android.text.TextUtils; |
Dianne Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 36 | import android.text.style.URLSpan; |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 37 | import android.util.Log; |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 38 | import android.util.proto.ProtoOutputStream; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 39 | |
Vladislav Kaznacheev | 8e7b940 | 2016-11-23 15:31:13 -0800 | [diff] [blame] | 40 | import com.android.internal.util.ArrayUtils; |
| 41 | |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 42 | import libcore.io.IoUtils; |
| 43 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 44 | import java.io.FileInputStream; |
| 45 | import java.io.FileNotFoundException; |
| 46 | import java.io.IOException; |
| 47 | import java.io.InputStreamReader; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 48 | import java.util.ArrayList; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 49 | import java.util.List; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Representation of a clipped data on the clipboard. |
| 53 | * |
Vladislav Kaznacheev | 7e4669c | 2016-05-02 17:04:08 -0700 | [diff] [blame] | 54 | * <p>ClipData is a complex type containing one or more Item instances, |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 55 | * each of which can hold one or more representations of an item of data. |
Vladislav Kaznacheev | 7e4669c | 2016-05-02 17:04:08 -0700 | [diff] [blame] | 56 | * For display to the user, it also has a label.</p> |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 57 | * |
Dianne Hackborn | f834dfa | 2010-10-26 12:43:57 -0700 | [diff] [blame] | 58 | * <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 Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 61 | * 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 Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 63 | * {@link #newPlainText(CharSequence, CharSequence)}, |
| 64 | * {@link #newUri(ContentResolver, CharSequence, Uri)}, and |
| 65 | * {@link #newIntent(CharSequence, Intent)}. |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 66 | * |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 67 | * <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 Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 70 | * |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 71 | * <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 Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 78 | * <a name="ImplementingPaste"></a> |
| 79 | * <h3>Implementing Paste or Drop</h3> |
| 80 | * |
Vladislav Kaznacheev | 7e4669c | 2016-05-02 17:04:08 -0700 | [diff] [blame] | 81 | * <p>To implement a paste or drop of a ClipData object into an application, |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 82 | * 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 Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 90 | * In this case there is generally no need to worry about the MIME types |
Dianne Hackborn | f834dfa | 2010-10-26 12:43:57 -0700 | [diff] [blame] | 91 | * reported by {@link ClipDescription#getMimeType(int) getDescription().getMimeType(int)}, |
Newton Allen | 4465d1a | 2013-11-26 10:25:38 -0800 | [diff] [blame] | 92 | * since any clip item can always be converted to a string. |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 93 | * |
| 94 | * <p>More complicated exchanges will be done through URIs, in particular |
Vladislav Kaznacheev | 7e4669c | 2016-05-02 17:04:08 -0700 | [diff] [blame] | 95 | * "content:" URIs. A content URI allows the recipient of a ClipData item |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 96 | * to interact closely with the ContentProvider holding the data in order to |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 97 | * negotiate the transfer of that data. The clip must also be filled in with |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 98 | * the available MIME types; {@link #newUri(ContentResolver, CharSequence, Uri)} |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 99 | * will take care of correctly doing this. |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 100 | * |
| 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 Hackborn | f6d952b | 2010-08-27 15:41:13 -0700 | [diff] [blame] | 121 | * {@sample frameworks/base/core/java/android/content/ClipData.java coerceToText} |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 122 | * |
| 123 | * <a name="ImplementingCopy"></a> |
| 124 | * <h3>Implementing Copy or Drag</h3> |
| 125 | * |
Vladislav Kaznacheev | 7e4669c | 2016-05-02 17:04:08 -0700 | [diff] [blame] | 126 | * <p>To be the source of a clip, the application must construct a ClipData |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 127 | * 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 Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 161 | */ |
Dianne Hackborn | f834dfa | 2010-10-26 12:43:57 -0700 | [diff] [blame] | 162 | public class ClipData implements Parcelable { |
| 163 | static final String[] MIMETYPES_TEXT_PLAIN = new String[] { |
| 164 | ClipDescription.MIMETYPE_TEXT_PLAIN }; |
Dianne Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 165 | static final String[] MIMETYPES_TEXT_HTML = new String[] { |
| 166 | ClipDescription.MIMETYPE_TEXT_HTML }; |
Dianne Hackborn | f834dfa | 2010-10-26 12:43:57 -0700 | [diff] [blame] | 167 | 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 Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 171 | |
Dianne Hackborn | f834dfa | 2010-10-26 12:43:57 -0700 | [diff] [blame] | 172 | final ClipDescription mClipDescription; |
Vladislav Kaznacheev | 7e4669c | 2016-05-02 17:04:08 -0700 | [diff] [blame] | 173 | |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 174 | final Bitmap mIcon; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 175 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 176 | final ArrayList<Item> mItems; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 177 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 178 | /** |
Vladislav Kaznacheev | 7e4669c | 2016-05-02 17:04:08 -0700 | [diff] [blame] | 179 | * Description of a single item in a ClipData. |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 180 | * |
| 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 Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 197 | public static class Item { |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 198 | final CharSequence mText; |
Dianne Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 199 | final String mHtmlText; |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 200 | final Intent mIntent; |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame^] | 201 | @UnsupportedAppUsage |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 202 | Uri mUri; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 203 | |
Nicolas Prevot | 5892624 | 2016-07-14 19:11:10 +0100 | [diff] [blame] | 204 | /** @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 Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 212 | /** |
| 213 | * Create an Item consisting of a single block of (possibly styled) text. |
| 214 | */ |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 215 | public Item(CharSequence text) { |
| 216 | mText = text; |
Dianne Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 217 | 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 Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 231 | mIntent = null; |
| 232 | mUri = null; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 235 | /** |
| 236 | * Create an Item consisting of an arbitrary Intent. |
| 237 | */ |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 238 | public Item(Intent intent) { |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 239 | mText = null; |
Dianne Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 240 | mHtmlText = null; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 241 | mIntent = intent; |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 242 | mUri = null; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 245 | /** |
| 246 | * Create an Item consisting of an arbitrary URI. |
| 247 | */ |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 248 | public Item(Uri uri) { |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 249 | mText = null; |
Dianne Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 250 | mHtmlText = null; |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 251 | mIntent = null; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 252 | mUri = uri; |
| 253 | } |
| 254 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 255 | /** |
| 256 | * Create a complex Item, containing multiple representations of |
Dianne Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 257 | * text, Intent, and/or URI. |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 258 | */ |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 259 | public Item(CharSequence text, Intent intent, Uri uri) { |
| 260 | mText = text; |
Dianne Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 261 | 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 Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 279 | mIntent = intent; |
| 280 | mUri = uri; |
| 281 | } |
| 282 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 283 | /** |
| 284 | * Retrieve the raw text contained in this Item. |
| 285 | */ |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 286 | public CharSequence getText() { |
| 287 | return mText; |
| 288 | } |
| 289 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 290 | /** |
Dianne Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 291 | * Retrieve the raw HTML text contained in this Item. |
| 292 | */ |
| 293 | public String getHtmlText() { |
| 294 | return mHtmlText; |
| 295 | } |
| 296 | |
| 297 | /** |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 298 | * Retrieve the raw Intent contained in this Item. |
| 299 | */ |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 300 | public Intent getIntent() { |
| 301 | return mIntent; |
| 302 | } |
| 303 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 304 | /** |
| 305 | * Retrieve the raw URI contained in this Item. |
| 306 | */ |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 307 | public Uri getUri() { |
| 308 | return mUri; |
| 309 | } |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 310 | |
| 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 Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 324 | * URI and return it. |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 325 | * <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 Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 335 | CharSequence text = getText(); |
| 336 | if (text != null) { |
| 337 | return text; |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | // If this Item has a URI value, try using that. |
Dianne Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 341 | Uri uri = getUri(); |
| 342 | if (uri != null) { |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 343 | // 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 Hirono | 3d9f3fc | 2017-04-12 14:23:06 +0900 | [diff] [blame] | 346 | final ContentResolver resolver = context.getContentResolver(); |
| 347 | AssetFileDescriptor descr = null; |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 348 | FileInputStream stream = null; |
Daichi Hirono | 3d9f3fc | 2017-04-12 14:23:06 +0900 | [diff] [blame] | 349 | InputStreamReader reader = null; |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 350 | try { |
Daichi Hirono | 3d9f3fc | 2017-04-12 14:23:06 +0900 | [diff] [blame] | 351 | 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 Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 359 | } |
Daichi Hirono | 3d9f3fc | 2017-04-12 14:23:06 +0900 | [diff] [blame] | 360 | if (descr != null) { |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 361 | try { |
Daichi Hirono | 3d9f3fc | 2017-04-12 14:23:06 +0900 | [diff] [blame] | 362 | 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 Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 373 | } catch (IOException e) { |
Daichi Hirono | 3d9f3fc | 2017-04-12 14:23:06 +0900 | [diff] [blame] | 374 | // Something bad has happened. |
| 375 | Log.w("ClipData", "Failure loading text", e); |
| 376 | return e.toString(); |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
Daichi Hirono | 3d9f3fc | 2017-04-12 14:23:06 +0900 | [diff] [blame] | 379 | } finally { |
| 380 | IoUtils.closeQuietly(descr); |
| 381 | IoUtils.closeQuietly(stream); |
| 382 | IoUtils.closeQuietly(reader); |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Vladislav Kaznacheev | e1f5e13 | 2017-01-27 16:56:40 -0800 | [diff] [blame] | 385 | // 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 Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 393 | return uri.toString(); |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 394 | } |
| 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 Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 398 | Intent intent = getIntent(); |
| 399 | if (intent != null) { |
| 400 | return intent.toUri(Intent.URI_INTENT_SCHEME); |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | // Shouldn't get here, but just in case... |
| 404 | return ""; |
| 405 | } |
| 406 | //END_INCLUDE(coerceToText) |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 407 | |
Dianne Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 408 | /** |
| 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 Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 493 | 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 Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 499 | 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 Kaznacheev | b143ff6 | 2017-01-27 16:39:13 -0800 | [diff] [blame] | 556 | } catch (SecurityException e) { |
| 557 | Log.w("ClipData", "Failure opening stream", e); |
| 558 | |
Dianne Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 559 | } 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 Kaznacheev | 7e4669c | 2016-05-02 17:04:08 -0700 | [diff] [blame] | 565 | Log.w("ClipData", "Failure loading text", e); |
Dianne Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 566 | 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 Kaznacheev | e1f5e13 | 2017-01-27 16:56:40 -0800 | [diff] [blame] | 578 | // 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 Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 587 | 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 Kralevich | c92db39 | 2012-07-27 13:22:20 -0700 | [diff] [blame] | 611 | builder.append(Html.escapeHtml(uri)); |
Dianne Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 612 | 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 Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 626 | @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 Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 639 | if (mHtmlText != null) { |
| 640 | b.append("H:"); |
| 641 | b.append(mHtmlText); |
| 642 | } else if (mText != null) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 643 | 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 Hackborn | ae49872 | 2015-08-14 13:29:47 -0700 | [diff] [blame] | 655 | |
| 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 Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 672 | |
| 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 Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /** |
| 694 | * Create a new clip. |
| 695 | * |
| 696 | * @param label Label to show to the user describing this clip. |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 697 | * @param mimeTypes An array of MIME types this data is available as. |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 698 | * @param item The contents of the first item in the clip. |
| 699 | */ |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 700 | public ClipData(CharSequence label, String[] mimeTypes, Item item) { |
Dianne Hackborn | f834dfa | 2010-10-26 12:43:57 -0700 | [diff] [blame] | 701 | mClipDescription = new ClipDescription(label, mimeTypes); |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 702 | if (item == null) { |
| 703 | throw new NullPointerException("item is null"); |
| 704 | } |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 705 | mIcon = null; |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 706 | mItems = new ArrayList<Item>(); |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 707 | mItems.add(item); |
| 708 | } |
| 709 | |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 710 | /** |
Dianne Hackborn | f834dfa | 2010-10-26 12:43:57 -0700 | [diff] [blame] | 711 | * Create a new clip. |
| 712 | * |
| 713 | * @param description The ClipDescription describing the clip contents. |
Dianne Hackborn | f834dfa | 2010-10-26 12:43:57 -0700 | [diff] [blame] | 714 | * @param item The contents of the first item in the clip. |
| 715 | */ |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 716 | public ClipData(ClipDescription description, Item item) { |
Dianne Hackborn | f834dfa | 2010-10-26 12:43:57 -0700 | [diff] [blame] | 717 | mClipDescription = description; |
| 718 | if (item == null) { |
| 719 | throw new NullPointerException("item is null"); |
| 720 | } |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 721 | mIcon = null; |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 722 | mItems = new ArrayList<Item>(); |
Dianne Hackborn | f834dfa | 2010-10-26 12:43:57 -0700 | [diff] [blame] | 723 | mItems.add(item); |
| 724 | } |
| 725 | |
| 726 | /** |
Steve McKay | 57eaaf6 | 2016-06-13 12:53:31 -0700 | [diff] [blame] | 727 | * 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 Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 745 | * 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 Hackborn | f834dfa | 2010-10-26 12:43:57 -0700 | [diff] [blame] | 757 | * Create a new ClipData holding data of the type |
| 758 | * {@link ClipDescription#MIMETYPE_TEXT_PLAIN}. |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 759 | * |
| 760 | * @param label User-visible label for the clip data. |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 761 | * @param text The actual text in the clip. |
| 762 | * @return Returns a new ClipData containing the specified data. |
| 763 | */ |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 764 | static public ClipData newPlainText(CharSequence label, CharSequence text) { |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 765 | Item item = new Item(text); |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 766 | return new ClipData(label, MIMETYPES_TEXT_PLAIN, item); |
| 767 | } |
| 768 | |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 769 | /** |
Dianne Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 770 | * 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 Hackborn | f834dfa | 2010-10-26 12:43:57 -0700 | [diff] [blame] | 786 | * Create a new ClipData holding an Intent with MIME type |
| 787 | * {@link ClipDescription#MIMETYPE_TEXT_INTENT}. |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 788 | * |
| 789 | * @param label User-visible label for the clip data. |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 790 | * @param intent The actual Intent in the clip. |
| 791 | * @return Returns a new ClipData containing the specified data. |
| 792 | */ |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 793 | static public ClipData newIntent(CharSequence label, Intent intent) { |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 794 | Item item = new Item(intent); |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 795 | return new ClipData(label, MIMETYPES_TEXT_INTENT, item); |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 796 | } |
| 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 Hackborn | f834dfa | 2010-10-26 12:43:57 -0700 | [diff] [blame] | 802 | * {@link ClipDescription#MIMETYPE_TEXT_URILIST}. |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 803 | * |
| 804 | * @param resolver ContentResolver used to get information about the URI. |
| 805 | * @param label User-visible label for the clip data. |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 806 | * @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 Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 810 | Uri uri) { |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 811 | Item item = new Item(uri); |
Vladislav Kaznacheev | 8e7b940 | 2016-11-23 15:31:13 -0800 | [diff] [blame] | 812 | 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 Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 824 | String[] mimeTypes = null; |
Vladislav Kaznacheev | e1f5e13 | 2017-01-27 16:56:40 -0800 | [diff] [blame] | 825 | if (SCHEME_CONTENT.equals(uri.getScheme())) { |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 826 | String realType = resolver.getType(uri); |
| 827 | mimeTypes = resolver.getStreamTypes(uri, "*/*"); |
Vladislav Kaznacheev | f67661c | 2016-05-03 15:02:33 -0700 | [diff] [blame] | 828 | if (realType != null) { |
| 829 | if (mimeTypes == null) { |
| 830 | mimeTypes = new String[] { realType }; |
Vladislav Kaznacheev | 8e7b940 | 2016-11-23 15:31:13 -0800 | [diff] [blame] | 831 | } else if (!ArrayUtils.contains(mimeTypes, realType)) { |
Vladislav Kaznacheev | f67661c | 2016-05-03 15:02:33 -0700 | [diff] [blame] | 832 | String[] tmp = new String[mimeTypes.length + 1]; |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 833 | tmp[0] = realType; |
Vladislav Kaznacheev | f67661c | 2016-05-03 15:02:33 -0700 | [diff] [blame] | 834 | System.arraycopy(mimeTypes, 0, tmp, 1, mimeTypes.length); |
| 835 | mimeTypes = tmp; |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 836 | } |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | if (mimeTypes == null) { |
| 840 | mimeTypes = MIMETYPES_TEXT_URILIST; |
| 841 | } |
Vladislav Kaznacheev | 8e7b940 | 2016-11-23 15:31:13 -0800 | [diff] [blame] | 842 | return mimeTypes; |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | /** |
Dianne Hackborn | f834dfa | 2010-10-26 12:43:57 -0700 | [diff] [blame] | 846 | * Create a new ClipData holding an URI with MIME type |
| 847 | * {@link ClipDescription#MIMETYPE_TEXT_URILIST}. |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 848 | * Unlike {@link #newUri(ContentResolver, CharSequence, Uri)}, nothing |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 849 | * 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 Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 853 | * @param uri The URI in the clip. |
| 854 | * @return Returns a new ClipData containing the specified data. |
| 855 | */ |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 856 | static public ClipData newRawUri(CharSequence label, Uri uri) { |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 857 | Item item = new Item(uri); |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 858 | return new ClipData(label, MIMETYPES_TEXT_URILIST, item); |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Dianne Hackborn | f834dfa | 2010-10-26 12:43:57 -0700 | [diff] [blame] | 861 | /** |
| 862 | * Return the {@link ClipDescription} associated with this data, describing |
| 863 | * what it contains. |
| 864 | */ |
| 865 | public ClipDescription getDescription() { |
| 866 | return mClipDescription; |
| 867 | } |
Vladislav Kaznacheev | f67661c | 2016-05-03 15:02:33 -0700 | [diff] [blame] | 868 | |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 869 | /** |
| 870 | * Add a new Item to the overall ClipData container. |
Vladislav Kaznacheev | f67661c | 2016-05-03 15:02:33 -0700 | [diff] [blame] | 871 | * <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 Kaznacheev | c35c02f | 2017-04-20 08:38:21 -0700 | [diff] [blame] | 873 | * MIME types to this clip. If this is not the case, use {@link #addItem(ContentResolver, Item)} |
Vladislav Kaznacheev | 8e7b940 | 2016-11-23 15:31:13 -0800 | [diff] [blame] | 874 | * or call {@link #ClipData(CharSequence, String[], Item)} with a complete list of MIME types. |
Vladislav Kaznacheev | f67661c | 2016-05-03 15:02:33 -0700 | [diff] [blame] | 875 | * @param item Item to be added. |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 876 | */ |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 877 | public void addItem(Item item) { |
| 878 | if (item == null) { |
| 879 | throw new NullPointerException("item is null"); |
| 880 | } |
| 881 | mItems.add(item); |
| 882 | } |
| 883 | |
Vladislav Kaznacheev | c35c02f | 2017-04-20 08:38:21 -0700 | [diff] [blame] | 884 | /** @removed use #addItem(ContentResolver, Item) instead */ |
| 885 | @Deprecated |
| 886 | public void addItem(Item item, ContentResolver resolver) { |
| 887 | addItem(resolver, item); |
| 888 | } |
| 889 | |
Vladislav Kaznacheev | 8e7b940 | 2016-11-23 15:31:13 -0800 | [diff] [blame] | 890 | /** |
| 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 Kaznacheev | 8e7b940 | 2016-11-23 15:31:13 -0800 | [diff] [blame] | 894 | * @param resolver ContentResolver used to get information about the URI possibly contained in |
| 895 | * the item. |
Vladislav Kaznacheev | c35c02f | 2017-04-20 08:38:21 -0700 | [diff] [blame] | 896 | * @param item Item to be added. |
Vladislav Kaznacheev | 8e7b940 | 2016-11-23 15:31:13 -0800 | [diff] [blame] | 897 | */ |
Vladislav Kaznacheev | c35c02f | 2017-04-20 08:38:21 -0700 | [diff] [blame] | 898 | public void addItem(ContentResolver resolver, Item item) { |
Vladislav Kaznacheev | 8e7b940 | 2016-11-23 15:31:13 -0800 | [diff] [blame] | 899 | 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 Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 916 | /** @hide */ |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame^] | 917 | @UnsupportedAppUsage |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 918 | public Bitmap getIcon() { |
| 919 | return mIcon; |
| 920 | } |
| 921 | |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 922 | /** |
| 923 | * Return the number of items in the clip data. |
| 924 | */ |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 925 | public int getItemCount() { |
| 926 | return mItems.size(); |
| 927 | } |
| 928 | |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 929 | /** |
| 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 Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 934 | return mItems.get(index); |
| 935 | } |
| 936 | |
Nicolas Prevot | 5892624 | 2016-07-14 19:11:10 +0100 | [diff] [blame] | 937 | /** @hide */ |
| 938 | public void setItemAt(int index, Item item) { |
| 939 | mItems.set(index, item); |
| 940 | } |
| 941 | |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 942 | /** |
| 943 | * Prepare this {@link ClipData} to leave an app process. |
| 944 | * |
| 945 | * @hide |
| 946 | */ |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 947 | public void prepareToLeaveProcess(boolean leavingPackage) { |
Jeff Sharkey | f361f2f | 2016-12-21 10:48:42 -0700 | [diff] [blame] | 948 | // Assume that callers are going to be granting permissions |
| 949 | prepareToLeaveProcess(leavingPackage, Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Jeff Sharkey | fb833f3 | 2016-12-01 14:59:59 -0700 | [diff] [blame] | 950 | } |
| 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 Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 958 | 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 Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 962 | item.mIntent.prepareToLeaveProcess(leavingPackage); |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 963 | } |
Jeff Sharkey | fb833f3 | 2016-12-01 14:59:59 -0700 | [diff] [blame] | 964 | 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 Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | } |
| 975 | |
Jeff Sharkey | d136e51 | 2016-03-09 22:30:56 -0700 | [diff] [blame] | 976 | /** {@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 Prevot | d1c99b1 | 2014-07-04 16:56:17 +0100 | [diff] [blame] | 987 | /** @hide */ |
| 988 | public void fixUris(int contentUserHint) { |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 989 | 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 Prevot | d1c99b1 | 2014-07-04 16:56:17 +0100 | [diff] [blame] | 993 | item.mIntent.fixUris(contentUserHint); |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 994 | } |
| 995 | if (item.mUri != null) { |
Nicolas Prevot | d1c99b1 | 2014-07-04 16:56:17 +0100 | [diff] [blame] | 996 | item.mUri = maybeAddUserId(item.mUri, contentUserHint); |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 997 | } |
| 998 | } |
| 999 | } |
| 1000 | |
Nicolas Prevot | f193990 | 2014-06-25 09:29:02 +0100 | [diff] [blame] | 1001 | /** |
| 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 Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 1021 | @Override |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 1022 | 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 Hackborn | ae49872 | 2015-08-14 13:29:47 -0700 | [diff] [blame] | 1061 | /** @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 Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 1074 | /** @hide */ |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1075 | 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 Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 1095 | 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 Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 1115 | @Override |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 1116 | public int describeContents() { |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
| 1120 | @Override |
| 1121 | public void writeToParcel(Parcel dest, int flags) { |
Dianne Hackborn | f834dfa | 2010-10-26 12:43:57 -0700 | [diff] [blame] | 1122 | mClipDescription.writeToParcel(dest, flags); |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 1123 | 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 Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 1134 | dest.writeString(item.mHtmlText); |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 1135 | 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 Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 1150 | ClipData(Parcel in) { |
Dianne Hackborn | f834dfa | 2010-10-26 12:43:57 -0700 | [diff] [blame] | 1151 | mClipDescription = new ClipDescription(in); |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 1152 | if (in.readInt() != 0) { |
| 1153 | mIcon = Bitmap.CREATOR.createFromParcel(in); |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 1154 | } else { |
| 1155 | mIcon = null; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 1156 | } |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 1157 | mItems = new ArrayList<Item>(); |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 1158 | final int N = in.readInt(); |
| 1159 | for (int i=0; i<N; i++) { |
| 1160 | CharSequence text = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); |
Dianne Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 1161 | String htmlText = in.readString(); |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 1162 | Intent intent = in.readInt() != 0 ? Intent.CREATOR.createFromParcel(in) : null; |
| 1163 | Uri uri = in.readInt() != 0 ? Uri.CREATOR.createFromParcel(in) : null; |
Dianne Hackborn | acb69bb | 2012-04-13 15:36:06 -0700 | [diff] [blame] | 1164 | mItems.add(new Item(text, htmlText, intent, uri)); |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 1165 | } |
| 1166 | } |
| 1167 | |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 1168 | public static final Parcelable.Creator<ClipData> CREATOR = |
| 1169 | new Parcelable.Creator<ClipData>() { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 1170 | |
Steve McKay | 57eaaf6 | 2016-06-13 12:53:31 -0700 | [diff] [blame] | 1171 | @Override |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 1172 | public ClipData createFromParcel(Parcel source) { |
| 1173 | return new ClipData(source); |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
Steve McKay | 57eaaf6 | 2016-06-13 12:53:31 -0700 | [diff] [blame] | 1176 | @Override |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 1177 | public ClipData[] newArray(int size) { |
| 1178 | return new ClipData[size]; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 1179 | } |
| 1180 | }; |
| 1181 | } |