ContentProviderClient.java revision 3f4c205fd3110345241e690f2a2e7c1b477eac76
16a8d5332f00bdfade6674b312e7166940aa28348Fred Quintana/*
26a8d5332f00bdfade6674b312e7166940aa28348Fred Quintana * Copyright (C) 2009 The Android Open Source Project
36a8d5332f00bdfade6674b312e7166940aa28348Fred Quintana *
46a8d5332f00bdfade6674b312e7166940aa28348Fred Quintana * Licensed under the Apache License, Version 2.0 (the "License");
56a8d5332f00bdfade6674b312e7166940aa28348Fred Quintana * you may not use this file except in compliance with the License.
66a8d5332f00bdfade6674b312e7166940aa28348Fred Quintana * You may obtain a copy of the License at
76a8d5332f00bdfade6674b312e7166940aa28348Fred Quintana *
86a8d5332f00bdfade6674b312e7166940aa28348Fred Quintana *      http://www.apache.org/licenses/LICENSE-2.0
96a8d5332f00bdfade6674b312e7166940aa28348Fred Quintana *
106a8d5332f00bdfade6674b312e7166940aa28348Fred Quintana * Unless required by applicable law or agreed to in writing, software
116a8d5332f00bdfade6674b312e7166940aa28348Fred Quintana * distributed under the License is distributed on an "AS IS" BASIS,
126a8d5332f00bdfade6674b312e7166940aa28348Fred Quintana * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136a8d5332f00bdfade6674b312e7166940aa28348Fred Quintana * See the License for the specific language governing permissions and
146a8d5332f00bdfade6674b312e7166940aa28348Fred Quintana * limitations under the License.
156a8d5332f00bdfade6674b312e7166940aa28348Fred Quintana */
166a8d5332f00bdfade6674b312e7166940aa28348Fred Quintana
17718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintanapackage android.content;
18718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana
19718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintanaimport android.database.Cursor;
20718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintanaimport android.net.Uri;
2123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackbornimport android.os.Bundle;
22a7771df3696954f0e279407e8894a916a7cb26ccJeff Brownimport android.os.CancellationSignal;
236ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackbornimport android.os.DeadObjectException;
24a7771df3696954f0e279407e8894a916a7cb26ccJeff Brownimport android.os.ICancellationSignal;
25718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintanaimport android.os.RemoteException;
26718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintanaimport android.os.ParcelFileDescriptor;
27718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintanaimport android.content.res.AssetFileDescriptor;
28718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana
29718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintanaimport java.io.FileNotFoundException;
3003d9490758c9318cee6d14d3cc5007556dce92d0Fred Quintanaimport java.util.ArrayList;
31718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana
32718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana/**
33718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana * The public interface object used to interact with a {@link ContentProvider}. This is obtained by
34718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana * calling {@link ContentResolver#acquireContentProviderClient}. This object must be released
35718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana * using {@link #release} in order to indicate to the system that the {@link ContentProvider} is
36718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana * no longer needed and can be killed to free up resources.
376ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn *
386ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn * <p>Note that you should generally create a new ContentProviderClient instance
396ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn * for each thread that will be performing operations.  Unlike
406ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn * {@link ContentResolver}, the methods here such as {@link #query} and
416ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn * {@link #openFile} are not thread safe -- you must not call
426ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn * {@link #release()} on the ContentProviderClient those calls are made from
436ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn * until you are finished with the data they have returned.
44718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana */
45718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintanapublic class ContentProviderClient {
46718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    private final IContentProvider mContentProvider;
47718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    private final ContentResolver mContentResolver;
4835654b61e8fe7bc85afcb076ddbb590d51c5865fDianne Hackborn    private final String mPackageName;
49652b6d1e591f6684cda4b93d4712920f287991b4Dianne Hackborn    private final boolean mStable;
506ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn    private boolean mReleased;
51718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana
52718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    /**
53718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana     * @hide
54718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana     */
55652b6d1e591f6684cda4b93d4712920f287991b4Dianne Hackborn    ContentProviderClient(ContentResolver contentResolver,
56652b6d1e591f6684cda4b93d4712920f287991b4Dianne Hackborn            IContentProvider contentProvider, boolean stable) {
57718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana        mContentProvider = contentProvider;
58718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana        mContentResolver = contentResolver;
5935654b61e8fe7bc85afcb076ddbb590d51c5865fDianne Hackborn        mPackageName = contentResolver.mPackageName;
60652b6d1e591f6684cda4b93d4712920f287991b4Dianne Hackborn        mStable = stable;
61718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    }
62718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana
6323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn    /** See {@link ContentProvider#query ContentProvider.query} */
64718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    public Cursor query(Uri url, String[] projection, String selection,
65718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana            String[] selectionArgs, String sortOrder) throws RemoteException {
66bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey        return query(url, projection, selection,  selectionArgs, sortOrder, null);
6775ea64fc54f328d37b115cfb1ded1e45c30380edJeff Brown    }
6875ea64fc54f328d37b115cfb1ded1e45c30380edJeff Brown
6975ea64fc54f328d37b115cfb1ded1e45c30380edJeff Brown    /** See {@link ContentProvider#query ContentProvider.query} */
7075ea64fc54f328d37b115cfb1ded1e45c30380edJeff Brown    public Cursor query(Uri url, String[] projection, String selection,
714c1241df8f8b7fd5ec3dff6c7e0f66271248e76eJeff Brown            String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal)
7275ea64fc54f328d37b115cfb1ded1e45c30380edJeff Brown                    throws RemoteException {
734c1241df8f8b7fd5ec3dff6c7e0f66271248e76eJeff Brown        ICancellationSignal remoteCancellationSignal = null;
744c1241df8f8b7fd5ec3dff6c7e0f66271248e76eJeff Brown        if (cancellationSignal != null) {
754c1241df8f8b7fd5ec3dff6c7e0f66271248e76eJeff Brown            remoteCancellationSignal = mContentProvider.createCancellationSignal();
764c1241df8f8b7fd5ec3dff6c7e0f66271248e76eJeff Brown            cancellationSignal.setRemote(remoteCancellationSignal);
7775ea64fc54f328d37b115cfb1ded1e45c30380edJeff Brown        }
786ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        try {
7935654b61e8fe7bc85afcb076ddbb590d51c5865fDianne Hackborn            return mContentProvider.query(mPackageName, url, projection, selection,  selectionArgs,
8035654b61e8fe7bc85afcb076ddbb590d51c5865fDianne Hackborn                    sortOrder, remoteCancellationSignal);
816ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        } catch (DeadObjectException e) {
826ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            if (!mStable) {
836ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn                mContentResolver.unstableProviderDied(mContentProvider);
846ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            }
856ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            throw e;
866ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        }
87718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    }
88718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana
8923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn    /** See {@link ContentProvider#getType ContentProvider.getType} */
90718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    public String getType(Uri url) throws RemoteException {
916ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        try {
926ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            return mContentProvider.getType(url);
936ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        } catch (DeadObjectException e) {
946ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            if (!mStable) {
956ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn                mContentResolver.unstableProviderDied(mContentProvider);
966ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            }
976ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            throw e;
986ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        }
99718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    }
100718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana
10123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn    /** See {@link ContentProvider#getStreamTypes ContentProvider.getStreamTypes} */
10223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn    public String[] getStreamTypes(Uri url, String mimeTypeFilter) throws RemoteException {
1036ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        try {
1046ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            return mContentProvider.getStreamTypes(url, mimeTypeFilter);
1056ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        } catch (DeadObjectException e) {
1066ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            if (!mStable) {
1076ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn                mContentResolver.unstableProviderDied(mContentProvider);
1086ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            }
1096ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            throw e;
1106ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        }
11123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn    }
11223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
11338ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn    /** See {@link ContentProvider#canonicalize} */
11438ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn    public final Uri canonicalize(Uri url) throws RemoteException {
11538ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn        try {
11638ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn            return mContentProvider.canonicalize(mPackageName, url);
11738ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn        } catch (DeadObjectException e) {
11838ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn            if (!mStable) {
11938ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn                mContentResolver.unstableProviderDied(mContentProvider);
12038ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn            }
12138ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn            throw e;
12238ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn        }
12338ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn    }
12438ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn
12538ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn    /** See {@link ContentProvider#uncanonicalize} */
12638ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn    public final Uri uncanonicalize(Uri url) throws RemoteException {
12738ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn        try {
12838ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn            return mContentProvider.uncanonicalize(mPackageName, url);
12938ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn        } catch (DeadObjectException e) {
13038ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn            if (!mStable) {
13138ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn                mContentResolver.unstableProviderDied(mContentProvider);
13238ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn            }
13338ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn            throw e;
13438ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn        }
13538ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn    }
13638ed2a471a2291383821fb187bfa18450f0581c2Dianne Hackborn
13723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn    /** See {@link ContentProvider#insert ContentProvider.insert} */
138718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    public Uri insert(Uri url, ContentValues initialValues)
139718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana            throws RemoteException {
1406ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        try {
14135654b61e8fe7bc85afcb076ddbb590d51c5865fDianne Hackborn            return mContentProvider.insert(mPackageName, url, initialValues);
1426ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        } catch (DeadObjectException e) {
1436ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            if (!mStable) {
1446ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn                mContentResolver.unstableProviderDied(mContentProvider);
1456ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            }
1466ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            throw e;
1476ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        }
148718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    }
149718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana
15023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn    /** See {@link ContentProvider#bulkInsert ContentProvider.bulkInsert} */
151718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    public int bulkInsert(Uri url, ContentValues[] initialValues) throws RemoteException {
1526ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        try {
15335654b61e8fe7bc85afcb076ddbb590d51c5865fDianne Hackborn            return mContentProvider.bulkInsert(mPackageName, url, initialValues);
1546ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        } catch (DeadObjectException e) {
1556ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            if (!mStable) {
1566ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn                mContentResolver.unstableProviderDied(mContentProvider);
1576ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            }
1586ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            throw e;
1596ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        }
160718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    }
161718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana
16223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn    /** See {@link ContentProvider#delete ContentProvider.delete} */
163718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    public int delete(Uri url, String selection, String[] selectionArgs)
164718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana            throws RemoteException {
1656ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        try {
16635654b61e8fe7bc85afcb076ddbb590d51c5865fDianne Hackborn            return mContentProvider.delete(mPackageName, url, selection, selectionArgs);
1676ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        } catch (DeadObjectException e) {
1686ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            if (!mStable) {
1696ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn                mContentResolver.unstableProviderDied(mContentProvider);
1706ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            }
1716ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            throw e;
1726ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        }
173718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    }
174718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana
17523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn    /** See {@link ContentProvider#update ContentProvider.update} */
176718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    public int update(Uri url, ContentValues values, String selection,
177718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana            String[] selectionArgs) throws RemoteException {
1786ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        try {
17935654b61e8fe7bc85afcb076ddbb590d51c5865fDianne Hackborn            return mContentProvider.update(mPackageName, url, values, selection, selectionArgs);
1806ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        } catch (DeadObjectException e) {
1816ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            if (!mStable) {
1826ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn                mContentResolver.unstableProviderDied(mContentProvider);
1836ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            }
1846ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            throw e;
1856ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        }
186718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    }
187718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana
18823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn    /**
18923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * See {@link ContentProvider#openFile ContentProvider.openFile}.  Note that
19023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * this <em>does not</em>
19123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * take care of non-content: URIs such as file:.  It is strongly recommended
19223fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * you use the {@link ContentResolver#openFileDescriptor
19323fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * ContentResolver.openFileDescriptor} API instead.
19423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     */
195718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    public ParcelFileDescriptor openFile(Uri url, String mode)
196718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana            throws RemoteException, FileNotFoundException {
197bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey        return openFile(url, mode, null);
198bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey    }
199bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey
200bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey    /**
201bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey     * See {@link ContentProvider#openFile ContentProvider.openFile}.  Note that
202bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey     * this <em>does not</em>
203bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey     * take care of non-content: URIs such as file:.  It is strongly recommended
204bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey     * you use the {@link ContentResolver#openFileDescriptor
205bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey     * ContentResolver.openFileDescriptor} API instead.
206bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey     */
207bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey    public ParcelFileDescriptor openFile(Uri url, String mode, CancellationSignal signal)
208bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey            throws RemoteException, FileNotFoundException {
209bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey        ICancellationSignal remoteSignal = null;
210bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey        if (signal != null) {
211bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey            remoteSignal = mContentProvider.createCancellationSignal();
212bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey            signal.setRemote(remoteSignal);
213bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey        }
2146ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        try {
215bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey            return mContentProvider.openFile(mPackageName, url, mode, remoteSignal);
2166ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        } catch (DeadObjectException e) {
2176ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            if (!mStable) {
2186ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn                mContentResolver.unstableProviderDied(mContentProvider);
2196ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            }
2206ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            throw e;
2216ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        }
222718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    }
223718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana
22423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn    /**
22523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * See {@link ContentProvider#openAssetFile ContentProvider.openAssetFile}.
22623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * Note that this <em>does not</em>
22723fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * take care of non-content: URIs such as file:.  It is strongly recommended
22823fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * you use the {@link ContentResolver#openAssetFileDescriptor
22923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     * ContentResolver.openAssetFileDescriptor} API instead.
23023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn     */
231718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    public AssetFileDescriptor openAssetFile(Uri url, String mode)
232718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana            throws RemoteException, FileNotFoundException {
233bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey        return openAssetFile(url, mode, null);
234bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey    }
235bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey
236bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey    /**
237bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey     * See {@link ContentProvider#openAssetFile ContentProvider.openAssetFile}.
238bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey     * Note that this <em>does not</em>
239bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey     * take care of non-content: URIs such as file:.  It is strongly recommended
240bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey     * you use the {@link ContentResolver#openAssetFileDescriptor
241bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey     * ContentResolver.openAssetFileDescriptor} API instead.
242bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey     */
243bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey    public AssetFileDescriptor openAssetFile(Uri url, String mode, CancellationSignal signal)
244bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey            throws RemoteException, FileNotFoundException {
245bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey        ICancellationSignal remoteSignal = null;
246bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey        if (signal != null) {
247bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey            remoteSignal = mContentProvider.createCancellationSignal();
248bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey            signal.setRemote(remoteSignal);
249bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey        }
2506ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        try {
251bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey            return mContentProvider.openAssetFile(mPackageName, url, mode, remoteSignal);
2526ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        } catch (DeadObjectException e) {
2536ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            if (!mStable) {
2546ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn                mContentResolver.unstableProviderDied(mContentProvider);
2556ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            }
2566ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            throw e;
2576ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        }
258718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    }
259718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana
26023fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn    /** See {@link ContentProvider#openTypedAssetFile ContentProvider.openTypedAssetFile} */
26123fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn    public final AssetFileDescriptor openTypedAssetFileDescriptor(Uri uri,
262bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey            String mimeType, Bundle opts) throws RemoteException, FileNotFoundException {
263bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey        return openTypedAssetFileDescriptor(uri, mimeType, opts, null);
264bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey    }
265bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey
266bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey    /** See {@link ContentProvider#openTypedAssetFile ContentProvider.openTypedAssetFile} */
267bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey    public final AssetFileDescriptor openTypedAssetFileDescriptor(Uri uri,
268bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey            String mimeType, Bundle opts, CancellationSignal signal)
26923fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn            throws RemoteException, FileNotFoundException {
270bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey        ICancellationSignal remoteSignal = null;
271bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey        if (signal != null) {
272bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey            remoteSignal = mContentProvider.createCancellationSignal();
273bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey            signal.setRemote(remoteSignal);
274bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey        }
2756ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        try {
276bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey            return mContentProvider.openTypedAssetFile(
277bd3b902567b09379e1b62c60b3319ad82102efadJeff Sharkey                    mPackageName, uri, mimeType, opts, remoteSignal);
2786ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        } catch (DeadObjectException e) {
2796ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            if (!mStable) {
2806ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn                mContentResolver.unstableProviderDied(mContentProvider);
2816ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            }
2826ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            throw e;
2836ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        }
28423fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn    }
28523fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn
28623fdaf6fb62a9b5154b2508916a21c678462c5d0Dianne Hackborn    /** See {@link ContentProvider#applyBatch ContentProvider.applyBatch} */
28703d9490758c9318cee6d14d3cc5007556dce92d0Fred Quintana    public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
2888943737692169f564cd34a9c8d471f3a5d438712Fred Quintana            throws RemoteException, OperationApplicationException {
2896ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        try {
29035654b61e8fe7bc85afcb076ddbb590d51c5865fDianne Hackborn            return mContentProvider.applyBatch(mPackageName, operations);
2916ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        } catch (DeadObjectException e) {
2926ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            if (!mStable) {
2936ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn                mContentResolver.unstableProviderDied(mContentProvider);
2946ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            }
2956ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            throw e;
2966ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        }
2976a8d5332f00bdfade6674b312e7166940aa28348Fred Quintana    }
2986a8d5332f00bdfade6674b312e7166940aa28348Fred Quintana
2997d19e0242faac8017033dabb872cdf1542fa184cDianne Hackborn    /** See {@link ContentProvider#call(String, String, Bundle)} */
3007d19e0242faac8017033dabb872cdf1542fa184cDianne Hackborn    public Bundle call(String method, String arg, Bundle extras)
3017d19e0242faac8017033dabb872cdf1542fa184cDianne Hackborn            throws RemoteException {
3027d19e0242faac8017033dabb872cdf1542fa184cDianne Hackborn        try {
30335654b61e8fe7bc85afcb076ddbb590d51c5865fDianne Hackborn            return mContentProvider.call(mPackageName, method, arg, extras);
3047d19e0242faac8017033dabb872cdf1542fa184cDianne Hackborn        } catch (DeadObjectException e) {
3057d19e0242faac8017033dabb872cdf1542fa184cDianne Hackborn            if (!mStable) {
3067d19e0242faac8017033dabb872cdf1542fa184cDianne Hackborn                mContentResolver.unstableProviderDied(mContentProvider);
3077d19e0242faac8017033dabb872cdf1542fa184cDianne Hackborn            }
3087d19e0242faac8017033dabb872cdf1542fa184cDianne Hackborn            throw e;
3097d19e0242faac8017033dabb872cdf1542fa184cDianne Hackborn        }
3107d19e0242faac8017033dabb872cdf1542fa184cDianne Hackborn    }
3117d19e0242faac8017033dabb872cdf1542fa184cDianne Hackborn
312718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    /**
313718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana     * Call this to indicate to the system that the associated {@link ContentProvider} is no
314718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana     * longer needed by this {@link ContentProviderClient}.
315718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana     * @return true if this was release, false if it was already released
316718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana     */
317718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    public boolean release() {
3186ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn        synchronized (this) {
3196ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            if (mReleased) {
3206ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn                throw new IllegalStateException("Already released");
3216ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            }
3226ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            mReleased = true;
3236ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            if (mStable) {
3246ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn                return mContentResolver.releaseProvider(mContentProvider);
3256ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            } else {
3266ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn                return mContentResolver.releaseUnstableProvider(mContentProvider);
3276ae8d1821822296df0606c9cd1c46708cc21cb58Dianne Hackborn            }
328652b6d1e591f6684cda4b93d4712920f287991b4Dianne Hackborn        }
329718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    }
330718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana
331718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    /**
332718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana     * Get a reference to the {@link ContentProvider} that is associated with this
333718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana     * client. If the {@link ContentProvider} is running in a different process then
334718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana     * null will be returned. This can be used if you know you are running in the same
335718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana     * process as a provider, and want to get direct access to its implementation details.
336718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana     *
337718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana     * @return If the associated {@link ContentProvider} is local, returns it.
338718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana     * Otherwise returns null.
339718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana     */
340718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    public ContentProvider getLocalContentProvider() {
341718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana        return ContentProvider.coerceToLocalContentProvider(mContentProvider);
342718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana    }
343aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey
344aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey    /** {@hide} */
345aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey    public static void closeQuietly(ContentProviderClient client) {
346aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey        if (client != null) {
3473f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            try {
3483f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey                client.release();
3493f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            } catch (Exception ignored) {
3503f4c205fd3110345241e690f2a2e7c1b477eac76Jeff Sharkey            }
351aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey        }
352aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey    }
353718d8a2d7ff3e864a73879eb646f46c14ab74d07Fred Quintana}
354