diff options
Diffstat (limited to 'core/java/android/provider/DocumentsContract.java')
-rw-r--r-- | core/java/android/provider/DocumentsContract.java | 46 |
1 files changed, 43 insertions, 3 deletions
diff --git a/core/java/android/provider/DocumentsContract.java b/core/java/android/provider/DocumentsContract.java index acaed73..53f5d58 100644 --- a/core/java/android/provider/DocumentsContract.java +++ b/core/java/android/provider/DocumentsContract.java @@ -19,7 +19,9 @@ package android.provider; import android.content.ContentProvider; import android.content.ContentResolver; import android.content.ContentValues; +import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; import android.content.pm.ProviderInfo; import android.content.res.AssetFileDescriptor; import android.database.Cursor; @@ -30,6 +32,8 @@ import android.net.Uri; import android.os.Bundle; import android.util.Log; +import com.google.android.collect.Lists; + import libcore.io.IoUtils; import java.io.IOException; @@ -201,19 +205,25 @@ public final class DocumentsContract { public static String getRootId(Uri documentUri) { final List<String> paths = documentUri.getPathSegments(); + if (paths.size() < 2) { + throw new IllegalArgumentException("Not a root: " + documentUri); + } if (!PATH_ROOTS.equals(paths.get(0))) { - throw new IllegalArgumentException(); + throw new IllegalArgumentException("Not a root: " + documentUri); } return paths.get(1); } public static String getDocId(Uri documentUri) { final List<String> paths = documentUri.getPathSegments(); + if (paths.size() < 4) { + throw new IllegalArgumentException("Not a document: " + documentUri); + } if (!PATH_ROOTS.equals(paths.get(0))) { - throw new IllegalArgumentException(); + throw new IllegalArgumentException("Not a document: " + documentUri); } if (!PATH_DOCS.equals(paths.get(2))) { - throw new IllegalArgumentException(); + throw new IllegalArgumentException("Not a document: " + documentUri); } return paths.get(3); } @@ -359,6 +369,36 @@ public final class DocumentsContract { } /** + * Return list of all documents that the calling package has "open." These + * are Uris matching {@link DocumentsContract} to which persistent + * read/write access has been granted, usually through + * {@link Intent#ACTION_OPEN_DOCUMENT} or + * {@link Intent#ACTION_CREATE_DOCUMENT}. + * + * @see Context#grantUriPermission(String, Uri, int) + * @see ContentResolver#getIncomingUriPermissionGrants(int, int) + */ + public static Uri[] getOpenDocuments(Context context) { + final int openedFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION + | Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_PERSIST_GRANT_URI_PERMISSION; + final Uri[] uris = context.getContentResolver() + .getIncomingUriPermissionGrants(openedFlags, openedFlags); + + // Filter to only include document providers + final PackageManager pm = context.getPackageManager(); + final List<Uri> result = Lists.newArrayList(); + for (Uri uri : uris) { + final ProviderInfo info = pm.resolveContentProvider( + uri.getAuthority(), PackageManager.GET_META_DATA); + if (info.metaData.containsKey(META_DATA_DOCUMENT_PROVIDER)) { + result.add(uri); + } + } + + return result.toArray(new Uri[result.size()]); + } + + /** * Return thumbnail representing the document at the given URI. Callers are * responsible for their own caching. Given document must have * {@link #FLAG_SUPPORTS_THUMBNAIL} set. |