blob: a19b13299ee740efbc0c2bc781898cb93713e0ec [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
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070019import android.content.res.AssetFileDescriptor;
Dianne Hackborn9f531192010-08-04 17:48:03 -070020import android.graphics.Bitmap;
21import android.net.Uri;
22import android.os.Parcel;
23import android.os.Parcelable;
24import android.text.TextUtils;
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070025import android.util.Log;
Dianne Hackborn9f531192010-08-04 17:48:03 -070026
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070027import java.io.FileInputStream;
28import java.io.FileNotFoundException;
29import java.io.IOException;
30import java.io.InputStreamReader;
Dianne Hackborn9f531192010-08-04 17:48:03 -070031import java.util.ArrayList;
32
33/**
34 * Representation of a clipped data on the clipboard.
35 *
36 * <p>ClippedData is a complex type containing one or Item instances,
37 * each of which can hold one or more representations of an item of data.
38 * For display to the user, it also has a label and iconic representation.</p>
39 *
Dianne Hackborn1040dc42010-08-26 22:11:06 -070040 * <p>A ClipData is a sub-class of {@link ClipDescription}, which describes
41 * important meta-data about the clip. In particular, its {@link #getMimeType(int)}
42 * must return correct MIME type(s) describing the data in the clip. For help
43 * in correctly constructing a clip with the correct MIME type, use
44 * {@link #newPlainText(CharSequence, Bitmap, CharSequence)},
45 * {@link #newUri(ContentResolver, CharSequence, Bitmap, Uri)}, and
46 * {@link #newIntent(CharSequence, Bitmap, Intent)}.
47 *
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070048 * <p>Each Item instance can be one of three main classes of data: a simple
49 * CharSequence of text, a single Intent object, or a Uri. See {@link Item}
50 * for more details.
Dianne Hackborn9f531192010-08-04 17:48:03 -070051 *
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070052 * <a name="ImplementingPaste"></a>
53 * <h3>Implementing Paste or Drop</h3>
54 *
55 * <p>To implement a paste or drop of a ClippedData object into an application,
56 * the application must correctly interpret the data for its use. If the {@link Item}
57 * it contains is simple text or an Intent, there is little to be done: text
58 * can only be interpreted as text, and an Intent will typically be used for
59 * creating shortcuts (such as placing icons on the home screen) or other
60 * actions.
61 *
62 * <p>If all you want is the textual representation of the clipped data, you
63 * can use the convenience method {@link Item#coerceToText Item.coerceToText}.
Dianne Hackborn1040dc42010-08-26 22:11:06 -070064 * In this case there is generally no need to worry about the MIME types
65 * reported by {@link #getMimeType(int)}, since any clip item an always be
66 * converted to a string.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070067 *
68 * <p>More complicated exchanges will be done through URIs, in particular
69 * "content:" URIs. A content URI allows the recipient of a ClippedData item
70 * to interact closely with the ContentProvider holding the data in order to
Dianne Hackborn1040dc42010-08-26 22:11:06 -070071 * negotiate the transfer of that data. The clip must also be filled in with
72 * the available MIME types; {@link #newUri(ContentResolver, CharSequence, Bitmap, Uri)}
73 * will take care of correctly doing this.
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070074 *
75 * <p>For example, here is the paste function of a simple NotePad application.
76 * When retrieving the data from the clipboard, it can do either two things:
77 * if the clipboard contains a URI reference to an existing note, it copies
78 * the entire structure of the note into a new note; otherwise, it simply
79 * coerces the clip into text and uses that as the new note's contents.
80 *
81 * {@sample development/samples/NotePad/src/com/example/android/notepad/NoteEditor.java
82 * paste}
83 *
84 * <p>In many cases an application can paste various types of streams of data. For
85 * example, an e-mail application may want to allow the user to paste an image
86 * or other binary data as an attachment. This is accomplished through the
87 * ContentResolver {@link ContentResolver#getStreamTypes(Uri, String)} and
88 * {@link ContentResolver#openTypedAssetFileDescriptor(Uri, String, android.os.Bundle)}
89 * methods. These allow a client to discover the type(s) of data that a particular
90 * content URI can make available as a stream and retrieve the stream of data.
91 *
92 * <p>For example, the implementation of {@link Item#coerceToText Item.coerceToText}
93 * itself uses this to try to retrieve a URI clip as a stream of text:
94 *
Dianne Hackbornf6d952b2010-08-27 15:41:13 -070095 * {@sample frameworks/base/core/java/android/content/ClipData.java coerceToText}
Dianne Hackborn23fdaf62010-08-06 12:16:55 -070096 *
97 * <a name="ImplementingCopy"></a>
98 * <h3>Implementing Copy or Drag</h3>
99 *
100 * <p>To be the source of a clip, the application must construct a ClippedData
101 * object that any recipient can interpret best for their context. If the clip
102 * is to contain a simple text, Intent, or URI, this is easy: an {@link Item}
103 * containing the appropriate data type can be constructed and used.
104 *
105 * <p>More complicated data types require the implementation of support in
106 * a ContentProvider for describing and generating the data for the recipient.
107 * A common scenario is one where an application places on the clipboard the
108 * content: URI of an object that the user has copied, with the data at that
109 * URI consisting of a complicated structure that only other applications with
110 * direct knowledge of the structure can use.
111 *
112 * <p>For applications that do not have intrinsic knowledge of the data structure,
113 * the content provider holding it can make the data available as an arbitrary
114 * number of types of data streams. This is done by implementing the
115 * ContentProvider {@link ContentProvider#getStreamTypes(Uri, String)} and
116 * {@link ContentProvider#openTypedAssetFile(Uri, String, android.os.Bundle)}
117 * methods.
118 *
119 * <p>Going back to our simple NotePad application, this is the implementation
120 * it may have to convert a single note URI (consisting of a title and the note
121 * text) into a stream of plain text data.
122 *
123 * {@sample development/samples/NotePad/src/com/example/android/notepad/NotePadProvider.java
124 * stream}
125 *
126 * <p>The copy operation in our NotePad application is now just a simple matter
127 * of making a clip containing the URI of the note being copied:
128 *
129 * {@sample development/samples/NotePad/src/com/example/android/notepad/NotesList.java
130 * copy}
131 *
132 * <p>Note if a paste operation needs this clip as text (for example to paste
133 * into an editor), then {@link Item#coerceToText(Context)} will ask the content
134 * provider for the clip URI as text and successfully paste the entire note.
Dianne Hackborn9f531192010-08-04 17:48:03 -0700135 */
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700136public class ClipData extends ClipDescription {
137 static final String[] MIMETYPES_TEXT_PLAIN = new String[] { MIMETYPE_TEXT_PLAIN };
138 static final String[] MIMETYPES_TEXT_URILIST = new String[] { MIMETYPE_TEXT_URILIST };
139 static final String[] MIMETYPES_TEXT_INTENT = new String[] { MIMETYPE_TEXT_INTENT };
140
141 final Bitmap mIcon;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700142
143 final ArrayList<Item> mItems = new ArrayList<Item>();
144
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700145 /**
146 * Description of a single item in a ClippedData.
147 *
148 * <p>The types than an individual item can currently contain are:</p>
149 *
150 * <ul>
151 * <li> Text: a basic string of text. This is actually a CharSequence,
152 * so it can be formatted text supported by corresponding Android built-in
153 * style spans. (Custom application spans are not supported and will be
154 * stripped when transporting through the clipboard.)
155 * <li> Intent: an arbitrary Intent object. A typical use is the shortcut
156 * to create when pasting a clipped item on to the home screen.
157 * <li> Uri: a URI reference. This may be any URI (such as an http: URI
158 * representing a bookmark), however it is often a content: URI. Using
159 * content provider references as clips like this allows an application to
160 * share complex or large clips through the standard content provider
161 * facilities.
162 * </ul>
163 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700164 public static class Item {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700165 final CharSequence mText;
166 final Intent mIntent;
167 final Uri mUri;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700168
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700169 /**
170 * Create an Item consisting of a single block of (possibly styled) text.
171 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700172 public Item(CharSequence text) {
173 mText = text;
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700174 mIntent = null;
175 mUri = null;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700176 }
177
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700178 /**
179 * Create an Item consisting of an arbitrary Intent.
180 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700181 public Item(Intent intent) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700182 mText = null;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700183 mIntent = intent;
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700184 mUri = null;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700185 }
186
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700187 /**
188 * Create an Item consisting of an arbitrary URI.
189 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700190 public Item(Uri uri) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700191 mText = null;
192 mIntent = null;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700193 mUri = uri;
194 }
195
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700196 /**
197 * Create a complex Item, containing multiple representations of
198 * text, intent, and/or URI.
199 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700200 public Item(CharSequence text, Intent intent, Uri uri) {
201 mText = text;
202 mIntent = intent;
203 mUri = uri;
204 }
205
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700206 /**
207 * Retrieve the raw text contained in this Item.
208 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700209 public CharSequence getText() {
210 return mText;
211 }
212
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700213 /**
214 * Retrieve the raw Intent contained in this Item.
215 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700216 public Intent getIntent() {
217 return mIntent;
218 }
219
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700220 /**
221 * Retrieve the raw URI contained in this Item.
222 */
Dianne Hackborn9f531192010-08-04 17:48:03 -0700223 public Uri getUri() {
224 return mUri;
225 }
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700226
227 /**
228 * Turn this item into text, regardless of the type of data it
229 * actually contains.
230 *
231 * <p>The algorithm for deciding what text to return is:
232 * <ul>
233 * <li> If {@link #getText} is non-null, return that.
234 * <li> If {@link #getUri} is non-null, try to retrieve its data
235 * as a text stream from its content provider. If this succeeds, copy
236 * the text into a String and return it. If it is not a content: URI or
237 * the content provider does not supply a text representation, return
238 * the raw URI as a string.
239 * <li> If {@link #getIntent} is non-null, convert that to an intent:
240 * URI and returnit.
241 * <li> Otherwise, return an empty string.
242 * </ul>
243 *
244 * @param context The caller's Context, from which its ContentResolver
245 * and other things can be retrieved.
246 * @return Returns the item's textual representation.
247 */
248//BEGIN_INCLUDE(coerceToText)
249 public CharSequence coerceToText(Context context) {
250 // If this Item has an explicit textual value, simply return that.
251 if (mText != null) {
252 return mText;
253 }
254
255 // If this Item has a URI value, try using that.
256 if (mUri != null) {
257
258 // First see if the URI can be opened as a plain text stream
259 // (of any sub-type). If so, this is the best textual
260 // representation for it.
261 FileInputStream stream = null;
262 try {
263 // Ask for a stream of the desired type.
264 AssetFileDescriptor descr = context.getContentResolver()
265 .openTypedAssetFileDescriptor(mUri, "text/*", null);
266 stream = descr.createInputStream();
267 InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
268
269 // Got it... copy the stream into a local string and return it.
270 StringBuilder builder = new StringBuilder(128);
271 char[] buffer = new char[8192];
272 int len;
273 while ((len=reader.read(buffer)) > 0) {
274 builder.append(buffer, 0, len);
275 }
276 return builder.toString();
277
278 } catch (FileNotFoundException e) {
279 // Unable to open content URI as text... not really an
280 // error, just something to ignore.
281
282 } catch (IOException e) {
283 // Something bad has happened.
284 Log.w("ClippedData", "Failure loading text", e);
285 return e.toString();
286
287 } finally {
288 if (stream != null) {
289 try {
290 stream.close();
291 } catch (IOException e) {
292 }
293 }
294 }
295
296 // If we couldn't open the URI as a stream, then the URI itself
297 // probably serves fairly well as a textual representation.
298 return mUri.toString();
299 }
300
301 // Finally, if all we have is an Intent, then we can just turn that
302 // into text. Not the most user-friendly thing, but it's something.
303 if (mIntent != null) {
304 return mIntent.toUri(Intent.URI_INTENT_SCHEME);
305 }
306
307 // Shouldn't get here, but just in case...
308 return "";
309 }
310//END_INCLUDE(coerceToText)
Dianne Hackborn9f531192010-08-04 17:48:03 -0700311 }
312
313 /**
314 * Create a new clip.
315 *
316 * @param label Label to show to the user describing this clip.
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700317 * @param mimeTypes An array of MIME types this data is available as.
Dianne Hackborn9f531192010-08-04 17:48:03 -0700318 * @param icon Bitmap providing the user with an iconing representation of
319 * the clip.
320 * @param item The contents of the first item in the clip.
321 */
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700322 public ClipData(CharSequence label, String[] mimeTypes, Bitmap icon, Item item) {
323 super(label, mimeTypes);
Dianne Hackborn9f531192010-08-04 17:48:03 -0700324 if (item == null) {
325 throw new NullPointerException("item is null");
326 }
Dianne Hackborn9f531192010-08-04 17:48:03 -0700327 mIcon = icon;
328 mItems.add(item);
329 }
330
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700331 /**
332 * Create a new ClipData holding data of the type {@link #MIMETYPE_TEXT_PLAIN}.
333 *
334 * @param label User-visible label for the clip data.
335 * @param icon Iconic representation of the clip data.
336 * @param text The actual text in the clip.
337 * @return Returns a new ClipData containing the specified data.
338 */
339 static public ClipData newPlainText(CharSequence label, Bitmap icon, CharSequence text) {
340 Item item = new Item(text);
341 return new ClipData(label, MIMETYPES_TEXT_PLAIN, icon, item);
342 }
343
344 /**
345 * Create a new ClipData holding an Intent with MIME type {@link #MIMETYPE_TEXT_INTENT}.
346 *
347 * @param label User-visible label for the clip data.
348 * @param icon Iconic representation of the clip data.
349 * @param intent The actual Intent in the clip.
350 * @return Returns a new ClipData containing the specified data.
351 */
352 static public ClipData newIntent(CharSequence label, Bitmap icon, Intent intent) {
353 Item item = new Item(intent);
354 return new ClipData(label, MIMETYPES_TEXT_INTENT, icon, item);
355 }
356
357 /**
358 * Create a new ClipData holding a URI. If the URI is a content: URI,
359 * this will query the content provider for the MIME type of its data and
360 * use that as the MIME type. Otherwise, it will use the MIME type
361 * {@link #MIMETYPE_TEXT_URILIST}.
362 *
363 * @param resolver ContentResolver used to get information about the URI.
364 * @param label User-visible label for the clip data.
365 * @param icon Iconic representation of the clip data.
366 * @param uri The URI in the clip.
367 * @return Returns a new ClipData containing the specified data.
368 */
369 static public ClipData newUri(ContentResolver resolver, CharSequence label,
370 Bitmap icon, Uri uri) {
371 Item item = new Item(uri);
372 String[] mimeTypes = null;
373 if ("content".equals(uri.getScheme())) {
374 String realType = resolver.getType(uri);
375 mimeTypes = resolver.getStreamTypes(uri, "*/*");
376 if (mimeTypes == null) {
377 if (realType != null) {
378 mimeTypes = new String[] { realType, MIMETYPE_TEXT_URILIST };
379 }
380 } else {
381 String[] tmp = new String[mimeTypes.length + (realType != null ? 2 : 1)];
382 int i = 0;
383 if (realType != null) {
384 tmp[0] = realType;
385 i++;
386 }
387 System.arraycopy(mimeTypes, 0, tmp, i, mimeTypes.length);
388 tmp[i + mimeTypes.length] = MIMETYPE_TEXT_URILIST;
389 mimeTypes = tmp;
390 }
391 }
392 if (mimeTypes == null) {
393 mimeTypes = MIMETYPES_TEXT_URILIST;
394 }
395 return new ClipData(label, mimeTypes, icon, item);
396 }
397
398 /**
399 * Create a new ClipData holding an URI with MIME type {@link #MIMETYPE_TEXT_URILIST}.
400 * Unlike {@link #newUri(ContentResolver, CharSequence, Bitmap, Uri)}, nothing
401 * is inferred about the URI -- if it is a content: URI holding a bitmap,
402 * the reported type will still be uri-list. Use this with care!
403 *
404 * @param label User-visible label for the clip data.
405 * @param icon Iconic representation of the clip data.
406 * @param uri The URI in the clip.
407 * @return Returns a new ClipData containing the specified data.
408 */
409 static public ClipData newRawUri(CharSequence label, Bitmap icon, Uri uri) {
410 Item item = new Item(uri);
411 return new ClipData(label, MIMETYPES_TEXT_URILIST, icon, item);
412 }
413
Dianne Hackborn9f531192010-08-04 17:48:03 -0700414 public void addItem(Item item) {
415 if (item == null) {
416 throw new NullPointerException("item is null");
417 }
418 mItems.add(item);
419 }
420
Dianne Hackborn9f531192010-08-04 17:48:03 -0700421 public Bitmap getIcon() {
422 return mIcon;
423 }
424
425 public int getItemCount() {
426 return mItems.size();
427 }
428
429 public Item getItem(int index) {
430 return mItems.get(index);
431 }
432
433 @Override
434 public int describeContents() {
435 return 0;
436 }
437
438 @Override
439 public void writeToParcel(Parcel dest, int flags) {
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700440 super.writeToParcel(dest, flags);
Dianne Hackborn9f531192010-08-04 17:48:03 -0700441 if (mIcon != null) {
442 dest.writeInt(1);
443 mIcon.writeToParcel(dest, flags);
444 } else {
445 dest.writeInt(0);
446 }
447 final int N = mItems.size();
448 dest.writeInt(N);
449 for (int i=0; i<N; i++) {
450 Item item = mItems.get(i);
451 TextUtils.writeToParcel(item.mText, dest, flags);
452 if (item.mIntent != null) {
453 dest.writeInt(1);
454 item.mIntent.writeToParcel(dest, flags);
455 } else {
456 dest.writeInt(0);
457 }
458 if (item.mUri != null) {
459 dest.writeInt(1);
460 item.mUri.writeToParcel(dest, flags);
461 } else {
462 dest.writeInt(0);
463 }
464 }
465 }
466
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700467 ClipData(Parcel in) {
468 super(in);
Dianne Hackborn9f531192010-08-04 17:48:03 -0700469 if (in.readInt() != 0) {
470 mIcon = Bitmap.CREATOR.createFromParcel(in);
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700471 } else {
472 mIcon = null;
Dianne Hackborn9f531192010-08-04 17:48:03 -0700473 }
474 final int N = in.readInt();
475 for (int i=0; i<N; i++) {
476 CharSequence text = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
477 Intent intent = in.readInt() != 0 ? Intent.CREATOR.createFromParcel(in) : null;
478 Uri uri = in.readInt() != 0 ? Uri.CREATOR.createFromParcel(in) : null;
479 mItems.add(new Item(text, intent, uri));
480 }
481 }
482
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700483 public static final Parcelable.Creator<ClipData> CREATOR =
484 new Parcelable.Creator<ClipData>() {
Dianne Hackborn9f531192010-08-04 17:48:03 -0700485
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700486 public ClipData createFromParcel(Parcel source) {
487 return new ClipData(source);
Dianne Hackborn9f531192010-08-04 17:48:03 -0700488 }
489
Dianne Hackborn1040dc42010-08-26 22:11:06 -0700490 public ClipData[] newArray(int size) {
491 return new ClipData[size];
Dianne Hackborn9f531192010-08-04 17:48:03 -0700492 }
493 };
494}