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