ContentProviderClient.java revision 03d9490758c9318cee6d14d3cc5007556dce92d0
1/*
2 * Copyright (C) 2009 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
19import android.database.Cursor;
20import android.net.Uri;
21import android.os.RemoteException;
22import android.os.ParcelFileDescriptor;
23import android.content.res.AssetFileDescriptor;
24
25import java.io.FileNotFoundException;
26import java.util.ArrayList;
27
28/**
29 * The public interface object used to interact with a {@link ContentProvider}. This is obtained by
30 * calling {@link ContentResolver#acquireContentProviderClient}. This object must be released
31 * using {@link #release} in order to indicate to the system that the {@link ContentProvider} is
32 * no longer needed and can be killed to free up resources.
33 */
34public class ContentProviderClient {
35    private final IContentProvider mContentProvider;
36    private final ContentResolver mContentResolver;
37
38    /**
39     * @hide
40     */
41    ContentProviderClient(ContentResolver contentResolver, IContentProvider contentProvider) {
42        mContentProvider = contentProvider;
43        mContentResolver = contentResolver;
44    }
45
46    /** see {@link ContentProvider#query} */
47    public Cursor query(Uri url, String[] projection, String selection,
48            String[] selectionArgs, String sortOrder) throws RemoteException {
49        return mContentProvider.query(url, projection, selection,  selectionArgs, sortOrder);
50    }
51
52    /** see {@link ContentProvider#getType} */
53    public String getType(Uri url) throws RemoteException {
54        return mContentProvider.getType(url);
55    }
56
57    /** see {@link ContentProvider#insert} */
58    public Uri insert(Uri url, ContentValues initialValues)
59            throws RemoteException {
60        return mContentProvider.insert(url, initialValues);
61    }
62
63    /** see {@link ContentProvider#bulkInsert} */
64    public int bulkInsert(Uri url, ContentValues[] initialValues) throws RemoteException {
65        return mContentProvider.bulkInsert(url, initialValues);
66    }
67
68    /** see {@link ContentProvider#delete} */
69    public int delete(Uri url, String selection, String[] selectionArgs)
70            throws RemoteException {
71        return mContentProvider.delete(url, selection, selectionArgs);
72    }
73
74    /** see {@link ContentProvider#update} */
75    public int update(Uri url, ContentValues values, String selection,
76            String[] selectionArgs) throws RemoteException {
77        return mContentProvider.update(url, values, selection, selectionArgs);
78    }
79
80    /** see {@link ContentProvider#openFile} */
81    public ParcelFileDescriptor openFile(Uri url, String mode)
82            throws RemoteException, FileNotFoundException {
83        return mContentProvider.openFile(url, mode);
84    }
85
86    /** see {@link ContentProvider#openAssetFile} */
87    public AssetFileDescriptor openAssetFile(Uri url, String mode)
88            throws RemoteException, FileNotFoundException {
89        return mContentProvider.openAssetFile(url, mode);
90    }
91
92    /** see {@link ContentProvider#queryEntities} */
93    public EntityIterator queryEntities(Uri uri, String selection, String[] selectionArgs,
94            String sortOrder) throws RemoteException {
95        return mContentProvider.queryEntities(uri, selection, selectionArgs, sortOrder);
96    }
97
98    /** see {@link ContentProvider#insertEntity} */
99    public Uri insertEntity(Uri uri, Entity entity) throws RemoteException {
100        return mContentProvider.insertEntity(uri, entity);
101    }
102
103    /** see {@link ContentProvider#updateEntity} */
104    public int updateEntity(Uri uri, Entity entity) throws RemoteException {
105        return mContentProvider.updateEntity(uri, entity);
106    }
107
108    /** see {@link ContentProvider#applyBatch} */
109    public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
110            throws RemoteException, OperationApplicationException {
111        return mContentProvider.applyBatch(operations);
112    }
113
114    /**
115     * Call this to indicate to the system that the associated {@link ContentProvider} is no
116     * longer needed by this {@link ContentProviderClient}.
117     * @return true if this was release, false if it was already released
118     */
119    public boolean release() {
120        return mContentResolver.releaseProvider(mContentProvider);
121    }
122
123    /**
124     * Get a reference to the {@link ContentProvider} that is associated with this
125     * client. If the {@link ContentProvider} is running in a different process then
126     * null will be returned. This can be used if you know you are running in the same
127     * process as a provider, and want to get direct access to its implementation details.
128     *
129     * @return If the associated {@link ContentProvider} is local, returns it.
130     * Otherwise returns null.
131     */
132    public ContentProvider getLocalContentProvider() {
133        return ContentProvider.coerceToLocalContentProvider(mContentProvider);
134    }
135}
136