/* * Copyright (C) 2013 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.provider; import static android.net.TrafficStats.KB_IN_BYTES; import static libcore.io.OsConstants.SEEK_SET; import android.content.ContentProviderClient; import android.content.ContentResolver; 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; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.os.Parcel; import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor.OnCloseListener; import android.os.Parcelable; import android.util.Log; import com.android.internal.util.Preconditions; import com.google.android.collect.Lists; import libcore.io.ErrnoException; import libcore.io.IoBridge; import libcore.io.IoUtils; import libcore.io.Libcore; import java.io.FileDescriptor; import java.io.IOException; import java.util.List; /** * Defines the contract between a documents provider and the platform. *
* To create a document provider, extend {@link DocumentsProvider}, which
* provides a foundational implementation of this contract.
*
* @see DocumentsProvider
*/
public final class DocumentsContract {
private static final String TAG = "Documents";
// content://com.example/docs/12/
// content://com.example/docs/12/children/
// content://com.example/docs/12/search/?query=pony
private DocumentsContract() {
}
/** {@hide} */
public static final String META_DATA_DOCUMENT_PROVIDER = "android.content.DOCUMENT_PROVIDER";
/** {@hide} */
public static final String ACTION_MANAGE_DOCUMENTS = "android.provider.action.MANAGE_DOCUMENTS";
/** {@hide} */
public static final String
ACTION_DOCUMENT_ROOT_CHANGED = "android.provider.action.DOCUMENT_ROOT_CHANGED";
/**
* Constants for individual documents.
*/
public final static class Documents {
private Documents() {
}
/**
* MIME type of a document which is a directory that may contain additional
* documents.
*/
public static final String MIME_TYPE_DIR = "vnd.android.doc/dir";
/**
* Flag indicating that a document is a directory that supports creation of
* new files within it.
*
* @see DocumentColumns#FLAGS
*/
public static final int FLAG_SUPPORTS_CREATE = 1;
/**
* Flag indicating that a document is renamable.
*
* @see DocumentColumns#FLAGS
*/
public static final int FLAG_SUPPORTS_RENAME = 1 << 1;
/**
* Flag indicating that a document is deletable.
*
* @see DocumentColumns#FLAGS
*/
public static final int FLAG_SUPPORTS_DELETE = 1 << 2;
/**
* Flag indicating that a document can be represented as a thumbnail.
*
* @see DocumentColumns#FLAGS
*/
public static final int FLAG_SUPPORTS_THUMBNAIL = 1 << 3;
/**
* Flag indicating that a document is a directory that supports search.
*
* @see DocumentColumns#FLAGS
*/
public static final int FLAG_SUPPORTS_SEARCH = 1 << 4;
/**
* Flag indicating that a document supports writing.
*
* @see DocumentColumns#FLAGS
*/
public static final int FLAG_SUPPORTS_WRITE = 1 << 5;
/**
* Flag indicating that a document is a directory that prefers its contents
* be shown in a larger format grid. Usually suitable when a directory
* contains mostly pictures.
*
* @see DocumentColumns#FLAGS
*/
public static final int FLAG_PREFERS_GRID = 1 << 6;
}
/**
* Extra boolean flag included in a directory {@link Cursor#getExtras()}
* indicating that a document provider is still loading data. For example, a
* provider has returned some results, but is still waiting on an
* outstanding network request.
*
* @see ContentResolver#notifyChange(Uri, android.database.ContentObserver,
* boolean)
*/
public static final String EXTRA_LOADING = "loading";
/**
* Extra string included in a directory {@link Cursor#getExtras()}
* providing an informational message that should be shown to a user. For
* example, a provider may wish to indicate that not all documents are
* available.
*/
public static final String EXTRA_INFO = "info";
/**
* Extra string included in a directory {@link Cursor#getExtras()} providing
* an error message that should be shown to a user. For example, a provider
* may wish to indicate that a network error occurred. The user may choose
* to retry, resulting in a new query.
*/
public static final String EXTRA_ERROR = "error";
/** {@hide} */
public static final String METHOD_GET_ROOTS = "android:getRoots";
/** {@hide} */
public static final String METHOD_CREATE_DOCUMENT = "android:createDocument";
/** {@hide} */
public static final String METHOD_RENAME_DOCUMENT = "android:renameDocument";
/** {@hide} */
public static final String METHOD_DELETE_DOCUMENT = "android:deleteDocument";
/** {@hide} */
public static final String EXTRA_AUTHORITY = "authority";
/** {@hide} */
public static final String EXTRA_PACKAGE_NAME = "packageName";
/** {@hide} */
public static final String EXTRA_URI = "uri";
/** {@hide} */
public static final String EXTRA_ROOTS = "roots";
/** {@hide} */
public static final String EXTRA_THUMBNAIL_SIZE = "thumbnail_size";
private static final String PATH_DOCS = "docs";
private static final String PATH_CHILDREN = "children";
private static final String PATH_SEARCH = "search";
private static final String PARAM_QUERY = "query";
/**
* Build Uri representing the given {@link DocumentColumns#DOC_ID} in a
* document provider.
*/
public static Uri buildDocumentUri(String authority, String docId) {
return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
.authority(authority).appendPath(PATH_DOCS).appendPath(docId).build();
}
/**
* Build Uri representing the contents of the given directory in a document
* provider. The given document must be {@link Documents#MIME_TYPE_DIR}.
*
* @hide
*/
public static Uri buildChildrenUri(String authority, String docId) {
return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority)
.appendPath(PATH_DOCS).appendPath(docId).appendPath(PATH_CHILDREN).build();
}
/**
* Build Uri representing a search for matching documents under a specific
* directory in a document provider. The given document must have
* {@link Documents#FLAG_SUPPORTS_SEARCH}.
*
* @hide
*/
public static Uri buildSearchUri(String authority, String docId, String query) {
return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority)
.appendPath(PATH_DOCS).appendPath(docId).appendPath(PATH_SEARCH)
.appendQueryParameter(PARAM_QUERY, query).build();
}
/**
* Extract the {@link DocumentColumns#DOC_ID} from the given Uri.
*/
public static String getDocId(Uri documentUri) {
final List
* Type: STRING
*/
public static final String DOC_ID = "doc_id";
/**
* MIME type of a document.
*
* Type: STRING
*
* @see Documents#MIME_TYPE_DIR
*/
public static final String MIME_TYPE = "mime_type";
/**
* Timestamp when a document was last modified, in milliseconds since
* January 1, 1970 00:00:00.0 UTC, or {@code null} if unknown. Document
* providers can update this field using events from
* {@link OnCloseListener} or other reliable
* {@link ParcelFileDescriptor} transports.
*
* Type: INTEGER (long)
*
* @see System#currentTimeMillis()
*/
public static final String LAST_MODIFIED = "last_modified";
/**
* Specific icon resource for a document, or {@code null} to resolve
* default using {@link #MIME_TYPE}.
*
* Type: INTEGER (int)
*/
public static final String ICON = "icon";
/**
* Summary for a document, or {@code null} to omit.
*
* Type: STRING
*/
public static final String SUMMARY = "summary";
/**
* Flags that apply to a specific document.
*
* Type: INTEGER (int)
*/
public static final String FLAGS = "flags";
}
/**
* Metadata about a specific root of documents.
*/
public final static class DocumentRoot implements Parcelable {
/**
* Root that represents a storage service, such as a cloud-based
* service.
*
* @see #rootType
*/
public static final int ROOT_TYPE_SERVICE = 1;
/**
* Root that represents a shortcut to content that may be available
* elsewhere through another storage root.
*
* @see #rootType
*/
public static final int ROOT_TYPE_SHORTCUT = 2;
/**
* Root that represents a physical storage device.
*
* @see #rootType
*/
public static final int ROOT_TYPE_DEVICE = 3;
/**
* Root that represents a physical storage device that should only be
* displayed to advanced users.
*
* @see #rootType
*/
public static final int ROOT_TYPE_DEVICE_ADVANCED = 4;
/**
* Flag indicating that at least one directory under this root supports
* creating content.
*
* @see #flags
*/
public static final int FLAG_SUPPORTS_CREATE = 1;
/**
* Flag indicating that this root offers content that is strictly local
* on the device. That is, no network requests are made for the content.
*
* @see #flags
*/
public static final int FLAG_LOCAL_ONLY = 1 << 1;
/** {@hide} */
public String authority;
/**
* Root type, use for clustering.
*
* @see #ROOT_TYPE_SERVICE
* @see #ROOT_TYPE_DEVICE
*/
public int rootType;
/**
* Flags for this root.
*
* @see #FLAG_LOCAL_ONLY
*/
public int flags;
/**
* Icon resource ID for this root.
*/
public int icon;
/**
* Title for this root.
*/
public String title;
/**
* Summary for this root. May be {@code null}.
*/
public String summary;
/**
* Document which is a directory that represents the top of this root.
* Must not be {@code null}.
*
* @see DocumentColumns#DOC_ID
*/
public String docId;
/**
* Document which is a directory representing recently modified
* documents under this root. This directory should return at most two
* dozen documents modified within the last 90 days. May be {@code null}
* if this root doesn't support recents.
*
* @see DocumentColumns#DOC_ID
*/
public String recentDocId;
/**
* Number of free bytes of available in this root, or -1 if unknown or
* unbounded.
*/
public long availableBytes;
/**
* Set of MIME type filters describing the content offered by this root,
* or {@code null} to indicate that all MIME types are supported. For
* example, a provider only supporting audio and video might set this to
* {@code ["audio/*", "video/*"]}.
*/
public String[] mimeTypes;
public DocumentRoot() {
}
/** {@hide} */
public DocumentRoot(Parcel in) {
rootType = in.readInt();
flags = in.readInt();
icon = in.readInt();
title = in.readString();
summary = in.readString();
docId = in.readString();
recentDocId = in.readString();
availableBytes = in.readLong();
mimeTypes = in.readStringArray();
}
/** {@hide} */
public Drawable loadIcon(Context context) {
if (icon != 0) {
if (authority != null) {
final PackageManager pm = context.getPackageManager();
final ProviderInfo info = pm.resolveContentProvider(authority, 0);
if (info != null) {
return pm.getDrawable(info.packageName, icon, info.applicationInfo);
}
} else {
return context.getResources().getDrawable(icon);
}
}
return null;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
Preconditions.checkNotNull(docId);
dest.writeInt(rootType);
dest.writeInt(flags);
dest.writeInt(icon);
dest.writeString(title);
dest.writeString(summary);
dest.writeString(docId);
dest.writeString(recentDocId);
dest.writeLong(availableBytes);
dest.writeStringArray(mimeTypes);
}
public static final Creator