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