ContentProviderClient.java revision 5bba632d877c2878384ff21566c8eb6a1a22f37b
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    /**
93     * see {@link ContentProvider#queryEntities}
94     * @hide
95     */
96    public EntityIterator queryEntities(Uri uri, String selection, String[] selectionArgs,
97            String sortOrder) throws RemoteException {
98        return mContentProvider.queryEntities(uri, selection, selectionArgs, sortOrder);
99    }
100
101    /** see {@link ContentProvider#applyBatch} */
102    public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
103            throws RemoteException, OperationApplicationException {
104        return mContentProvider.applyBatch(operations);
105    }
106
107    /**
108     * Call this to indicate to the system that the associated {@link ContentProvider} is no
109     * longer needed by this {@link ContentProviderClient}.
110     * @return true if this was release, false if it was already released
111     */
112    public boolean release() {
113        return mContentResolver.releaseProvider(mContentProvider);
114    }
115
116    /**
117     * Get a reference to the {@link ContentProvider} that is associated with this
118     * client. If the {@link ContentProvider} is running in a different process then
119     * null will be returned. This can be used if you know you are running in the same
120     * process as a provider, and want to get direct access to its implementation details.
121     *
122     * @return If the associated {@link ContentProvider} is local, returns it.
123     * Otherwise returns null.
124     */
125    public ContentProvider getLocalContentProvider() {
126        return ContentProvider.coerceToLocalContentProvider(mContentProvider);
127    }
128}
129