diff options
| author | 2017-01-21 01:09:23 +0000 | |
|---|---|---|
| committer | 2017-01-21 01:09:28 +0000 | |
| commit | c49f2e5d88af31b3a20e460c08097dfab4edaa15 (patch) | |
| tree | 143a813407b32c7bb2ec17dfc67cccaf5a01277c | |
| parent | 4cbec883f103b9c6bf553ef4f77df492287d30dc (diff) | |
| parent | 8e7b940f8d08fe6697e274933ba8c06e6363c295 (diff) | |
Merge "Add ClipData.addItem(Item, ContentResolver)"
| -rw-r--r-- | api/current.txt | 1 | ||||
| -rw-r--r-- | api/system-current.txt | 1 | ||||
| -rw-r--r-- | api/test-current.txt | 1 | ||||
| -rw-r--r-- | core/java/android/content/ClipData.java | 48 | ||||
| -rw-r--r-- | core/java/android/content/ClipDescription.java | 51 |
5 files changed, 81 insertions, 21 deletions
diff --git a/api/current.txt b/api/current.txt index 9f1ab1bfc796..25dd3d820db1 100644 --- a/api/current.txt +++ b/api/current.txt @@ -7951,6 +7951,7 @@ package android.content { ctor public ClipData(android.content.ClipDescription, android.content.ClipData.Item); ctor public ClipData(android.content.ClipData); method public void addItem(android.content.ClipData.Item); + method public void addItem(android.content.ClipData.Item, android.content.ContentResolver); method public int describeContents(); method public android.content.ClipDescription getDescription(); method public android.content.ClipData.Item getItemAt(int); diff --git a/api/system-current.txt b/api/system-current.txt index 5c107aec939c..6862762e385b 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -8318,6 +8318,7 @@ package android.content { ctor public ClipData(android.content.ClipDescription, android.content.ClipData.Item); ctor public ClipData(android.content.ClipData); method public void addItem(android.content.ClipData.Item); + method public void addItem(android.content.ClipData.Item, android.content.ContentResolver); method public int describeContents(); method public android.content.ClipDescription getDescription(); method public android.content.ClipData.Item getItemAt(int); diff --git a/api/test-current.txt b/api/test-current.txt index 84aa96c89b03..0839043c2ab4 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -7973,6 +7973,7 @@ package android.content { ctor public ClipData(android.content.ClipDescription, android.content.ClipData.Item); ctor public ClipData(android.content.ClipData); method public void addItem(android.content.ClipData.Item); + method public void addItem(android.content.ClipData.Item, android.content.ContentResolver); method public int describeContents(); method public android.content.ClipDescription getDescription(); method public android.content.ClipData.Item getItemAt(int); diff --git a/core/java/android/content/ClipData.java b/core/java/android/content/ClipData.java index bc5e986d240b..70967712f068 100644 --- a/core/java/android/content/ClipData.java +++ b/core/java/android/content/ClipData.java @@ -32,6 +32,8 @@ import android.text.TextUtils; import android.text.style.URLSpan; import android.util.Log; +import com.android.internal.util.ArrayUtils; + import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; @@ -762,6 +764,18 @@ public class ClipData implements Parcelable { static public ClipData newUri(ContentResolver resolver, CharSequence label, Uri uri) { Item item = new Item(uri); + String[] mimeTypes = getMimeTypes(resolver, uri); + return new ClipData(label, mimeTypes, item); + } + + /** + * Finds all applicable MIME types for a given URI. + * + * @param resolver ContentResolver used to get information about the URI. + * @param uri The URI. + * @return Returns an array of MIME types. + */ + private static String[] getMimeTypes(ContentResolver resolver, Uri uri) { String[] mimeTypes = null; if ("content".equals(uri.getScheme())) { String realType = resolver.getType(uri); @@ -769,7 +783,7 @@ public class ClipData implements Parcelable { if (realType != null) { if (mimeTypes == null) { mimeTypes = new String[] { realType }; - } else { + } else if (!ArrayUtils.contains(mimeTypes, realType)) { String[] tmp = new String[mimeTypes.length + 1]; tmp[0] = realType; System.arraycopy(mimeTypes, 0, tmp, 1, mimeTypes.length); @@ -780,7 +794,7 @@ public class ClipData implements Parcelable { if (mimeTypes == null) { mimeTypes = MIMETYPES_TEXT_URILIST; } - return new ClipData(label, mimeTypes, item); + return mimeTypes; } /** @@ -811,8 +825,8 @@ public class ClipData implements Parcelable { * Add a new Item to the overall ClipData container. * <p> This method will <em>not</em> update the list of available MIME types in the * {@link ClipDescription}. It should be used only when adding items which do not add new - * MIME types to this clip. If this is not the case, {@link #ClipData(CharSequence, String[], - * Item)} should be used with a complete list of MIME types. + * MIME types to this clip. If this is not the case, use {@link #addItem(Item, ContentResolver)} + * or call {@link #ClipData(CharSequence, String[], Item)} with a complete list of MIME types. * @param item Item to be added. */ public void addItem(Item item) { @@ -822,6 +836,32 @@ public class ClipData implements Parcelable { mItems.add(item); } + /** + * Add a new Item to the overall ClipData container. + * <p> Unlike {@link #addItem(Item)}, this method will update the list of available MIME types + * in the {@link ClipDescription}. + * @param item Item to be added. + * @param resolver ContentResolver used to get information about the URI possibly contained in + * the item. + */ + public void addItem(Item item, ContentResolver resolver) { + addItem(item); + + if (item.getHtmlText() != null) { + mClipDescription.addMimeTypes(MIMETYPES_TEXT_HTML); + } else if (item.getText() != null) { + mClipDescription.addMimeTypes(MIMETYPES_TEXT_PLAIN); + } + + if (item.getIntent() != null) { + mClipDescription.addMimeTypes(MIMETYPES_TEXT_INTENT); + } + + if (item.getUri() != null) { + mClipDescription.addMimeTypes(getMimeTypes(resolver, item.getUri())); + } + } + /** @hide */ public Bitmap getIcon() { return mIcon; diff --git a/core/java/android/content/ClipDescription.java b/core/java/android/content/ClipDescription.java index 461d1e04e524..b33a915ad2a5 100644 --- a/core/java/android/content/ClipDescription.java +++ b/core/java/android/content/ClipDescription.java @@ -22,6 +22,7 @@ import android.os.PersistableBundle; import android.text.TextUtils; import java.util.ArrayList; +import java.util.Arrays; /** * Meta-data describing the contents of a {@link ClipData}. Provides enough @@ -89,7 +90,7 @@ public class ClipDescription implements Parcelable { final CharSequence mLabel; - final String[] mMimeTypes; + private final ArrayList<String> mMimeTypes; private PersistableBundle mExtras; /** @@ -103,7 +104,7 @@ public class ClipDescription implements Parcelable { throw new NullPointerException("mimeTypes is null"); } mLabel = label; - mMimeTypes = mimeTypes; + mMimeTypes = new ArrayList<String>(Arrays.asList(mimeTypes)); } /** @@ -111,7 +112,7 @@ public class ClipDescription implements Parcelable { */ public ClipDescription(ClipDescription o) { mLabel = o.mLabel; - mMimeTypes = o.mMimeTypes; + mMimeTypes = new ArrayList<String>(o.mMimeTypes); } /** @@ -155,8 +156,9 @@ public class ClipDescription implements Parcelable { * matches the desired MIME type, else false. */ public boolean hasMimeType(String mimeType) { - for (int i=0; i<mMimeTypes.length; i++) { - if (compareMimeTypes(mMimeTypes[i], mimeType)) { + final int size = mMimeTypes.size(); + for (int i=0; i<size; i++) { + if (compareMimeTypes(mMimeTypes.get(i), mimeType)) { return true; } } @@ -173,12 +175,13 @@ public class ClipDescription implements Parcelable { */ public String[] filterMimeTypes(String mimeType) { ArrayList<String> array = null; - for (int i=0; i<mMimeTypes.length; i++) { - if (compareMimeTypes(mMimeTypes[i], mimeType)) { + final int size = mMimeTypes.size(); + for (int i=0; i<size; i++) { + if (compareMimeTypes(mMimeTypes.get(i), mimeType)) { if (array == null) { array = new ArrayList<String>(); } - array.add(mMimeTypes[i]); + array.add(mMimeTypes.get(i)); } } if (array == null) { @@ -193,14 +196,26 @@ public class ClipDescription implements Parcelable { * Return the number of MIME types the clip is available in. */ public int getMimeTypeCount() { - return mMimeTypes.length; + return mMimeTypes.size(); } /** * Return one of the possible clip MIME types. */ public String getMimeType(int index) { - return mMimeTypes[index]; + return mMimeTypes.get(index); + } + + /** + * Add MIME types to the clip description. + */ + void addMimeTypes(String[] mimeTypes) { + for (int i=0; i!=mimeTypes.length; i++) { + final String mimeType = mimeTypes[i]; + if (!mMimeTypes.contains(mimeType)) { + mMimeTypes.add(mimeType); + } + } } /** @@ -229,11 +244,12 @@ public class ClipDescription implements Parcelable { if (mMimeTypes == null) { throw new NullPointerException("null mime types"); } - if (mMimeTypes.length <= 0) { + final int size = mMimeTypes.size(); + if (size <= 0) { throw new IllegalArgumentException("must have at least 1 mime type"); } - for (int i=0; i<mMimeTypes.length; i++) { - if (mMimeTypes[i] == null) { + for (int i=0; i<size; i++) { + if (mMimeTypes.get(i) == null) { throw new NullPointerException("mime type at " + i + " is null"); } } @@ -275,12 +291,13 @@ public class ClipDescription implements Parcelable { /** @hide */ public boolean toShortStringTypesOnly(StringBuilder b) { boolean first = true; - for (int i=0; i<mMimeTypes.length; i++) { + final int size = mMimeTypes.size(); + for (int i=0; i<size; i++) { if (!first) { b.append(' '); } first = false; - b.append(mMimeTypes[i]); + b.append(mMimeTypes.get(i)); } return !first; } @@ -293,13 +310,13 @@ public class ClipDescription implements Parcelable { @Override public void writeToParcel(Parcel dest, int flags) { TextUtils.writeToParcel(mLabel, dest, flags); - dest.writeStringArray(mMimeTypes); + dest.writeStringList(mMimeTypes); dest.writePersistableBundle(mExtras); } ClipDescription(Parcel in) { mLabel = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); - mMimeTypes = in.createStringArray(); + mMimeTypes = in.createStringArrayList(); mExtras = in.readPersistableBundle(); } |