下面列出了android.content.ContentProviderClient#release ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Queries a content provider for word list data for some locale and stage the returned files
*
* This will query a content provider for word list data for a given locale, and copy the
* files locally so that they can be mmap'ed. This may overwrite previously cached word lists
* with newer versions if a newer version is made available by the content provider.
* @throw FileNotFoundException if the provider returns non-existent data.
* @throw IOException if the provider-returned data could not be read.
*/
public static void installDictToStagingFromContentProvider(final Locale locale,
final Context context, final boolean hasDefaultWordList) {
final ContentProviderClient providerClient;
try {
providerClient = context.getContentResolver().
acquireContentProviderClient(getProviderUriBuilder("").build());
} catch (final SecurityException e) {
Log.e(TAG, "No permission to communicate with the dictionary provider", e);
return;
}
if (null == providerClient) {
Log.e(TAG, "Can't establish communication with the dictionary provider");
return;
}
try {
final List<WordListInfo> idList = getWordListWordListInfos(locale, context,
hasDefaultWordList);
for (WordListInfo id : idList) {
installWordListToStaging(id.mId, id.mLocale, id.mRawChecksum, providerClient,
context);
}
} finally {
providerClient.release();
}
}
public void reloadAll(){
ContentResolver resolver = ActivityDiaryApplication.getAppContext().getContentResolver();
ContentProviderClient client = resolver.acquireContentProviderClient(ActivityDiaryContract.AUTHORITY);
ActivityDiaryContentProvider provider = (ActivityDiaryContentProvider) client.getLocalContentProvider();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
{
client.close();
}
else
{
client.release();
}
provider.resetDatabase();
startQuery(QUERY_ALL_ACTIVITIES, null, ActivityDiaryContract.DiaryActivity.CONTENT_URI,
ACTIVITIES_PROJ, SELECTION, null,
null);
}
/**
* Queries a content provider for word list data for some locale and stage the returned files
*
* This will query a content provider for word list data for a given locale, and copy the
* files locally so that they can be mmap'ed. This may overwrite previously cached word lists
* with newer versions if a newer version is made available by the content provider.
* @throw FileNotFoundException if the provider returns non-existent data.
* @throw IOException if the provider-returned data could not be read.
*/
public static void installDictToStagingFromContentProvider(final Locale locale,
final Context context, final boolean hasDefaultWordList) {
final ContentProviderClient providerClient;
try {
providerClient = context.getContentResolver().
acquireContentProviderClient(getProviderUriBuilder(context, "").build());
} catch (final SecurityException e) {
Log.e(TAG, "No permission to communicate with the dictionary provider", e);
return;
}
if (null == providerClient) {
Log.e(TAG, "Can't establish communication with the dictionary provider");
return;
}
try {
final List<WordListInfo> idList = getWordListWordListInfos(locale, context,
hasDefaultWordList);
for (WordListInfo id : idList) {
installWordListToStaging(id.mId, id.mLocale, id.mRawChecksum, providerClient,
context);
}
} finally {
providerClient.release();
}
}
private static List<DocumentFile> listDocumentFiles(@NonNull final Context context,
@NonNull final DocumentFile parent,
final String nameFilter,
boolean listFolders,
boolean listFiles) {
ContentProviderClient client = context.getContentResolver().acquireContentProviderClient(parent.getUri());
if (null == client) return Collections.emptyList();
try {
return FileUtil.listDocumentFiles(context, parent, client, FileHelper.createNameFilterEquals(nameFilter), listFolders, listFiles);
} finally {
// ContentProviderClient.close only available on API level 24+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
client.close();
else
client.release();
}
}
/**
* Gets the input stream for a given Uri.
*
* @param uri The Uri for which the InputStream is required.
*/
InputStream getInputStream(Uri uri) throws FileNotFoundException {
checkNotNull(uri);
ContentProviderClient providerClient = null;
try {
providerClient = makeContentProviderClient(contentResolver, uri);
// Assignment to a variable is required. Do not inline.
ParcelFileDescriptor pfd = providerClient.openFile(uri, "r");
// Buffered to improve performance.
return new BufferedInputStream(new ParcelFileDescriptor.AutoCloseInputStream(pfd));
} catch (RemoteException re) {
throw new TestStorageException("Unable to access content provider: " + uri, re);
} finally {
if (providerClient != null) {
// Uses #release() to be compatible with API < 24.
providerClient.release();
}
}
}
/**
* Queries a content provider for word list data for some locale and stage the returned files
*
* This will query a content provider for word list data for a given locale, and copy the
* files locally so that they can be mmap'ed. This may overwrite previously cached word lists
* with newer versions if a newer version is made available by the content provider.
* @throw FileNotFoundException if the provider returns non-existent data.
* @throw IOException if the provider-returned data could not be read.
*/
public static void installDictToStagingFromContentProvider(final Locale locale,
final Context context, final boolean hasDefaultWordList) {
final ContentProviderClient providerClient;
try {
providerClient = context.getContentResolver().
acquireContentProviderClient(getProviderUriBuilder("").build());
} catch (final SecurityException e) {
Log.e(TAG, "No permission to communicate with the dictionary provider", e);
return;
}
if (null == providerClient) {
Log.e(TAG, "Can't establish communication with the dictionary provider");
return;
}
try {
final List<WordListInfo> idList = getWordListWordListInfos(locale, context,
hasDefaultWordList);
for (WordListInfo id : idList) {
installWordListToStaging(id.mId, id.mLocale, id.mRawChecksum, providerClient,
context);
}
} finally {
providerClient.release();
}
}
public static void releaseQuietly(ContentProviderClient client) {
if (client != null) {
try {
client.release();
} catch (Exception ignored) {
}
}
}
public static void releaseQuietly(ContentProviderClient client) {
if (client != null) {
try {
client.release();
} catch (Exception ignored) {
}
}
}
public static void releaseQuietly(ContentProviderClient client) {
if (client != null) {
try {
client.release();
} catch (Exception ignored) {
}
}
}
@Test public void testProvider() {
final ContentResolver resolver = context().getContentResolver();
// Regular provider access
final String android_id = Settings.System.getString(resolver, Settings.System.ANDROID_ID);
assertNotNull(android_id);
final ContentProviderClient client = resolver.acquireContentProviderClient(Settings.AUTHORITY);
assertNotNull(client);
client.release();
sCondomProcessPackageManager.mCondom.mOutboundJudge = mBlockAllJudge;
assertNull(resolver.acquireContentProviderClient("downloads"));
}
public static Bundle call(Uri uri, String method, String arg, Bundle extras) {
ContentResolver resolver = ServiceManager.sApplication.getContentResolver();
if (Build.VERSION.SDK_INT >= 11) {
return resolver.call(uri, method, arg, extras);
} else {
ContentProviderClient client = resolver.acquireContentProviderClient(uri);
if (client == null) {
throw new IllegalArgumentException("Unknown URI " + uri);
}
try {
Object mContentProvider = RefIectUtil.getFieldObject(client, ContentProviderClient.class, "mContentProvider");
if (mContentProvider != null) {
//public Bundle call(String method, String request, Bundle args)
Object result = null;
try {
result = RefIectUtil.invokeMethod(mContentProvider, Class.forName("android.content.IContentProvider"), "call",
new Class[]{String.class, String.class, Bundle.class},
new Object[]{method, arg, extras});
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return (Bundle) result;
}
} finally {
client.release();
}
return null;
}
}
protected static @NonNull <T extends AppListProvider> T getInstance(final Context context) {
final String authority = context.getPackageName() + AUTHORITY_SUFFIX; // Do not use BuildConfig.APPLICATION_ID
final ContentProviderClient client = context.getContentResolver().acquireContentProviderClient(authority);
if (client == null) throw new IllegalStateException("AppListProvider not associated with authority: " + authority);
try {
final ContentProvider provider = client.getLocalContentProvider();
if (provider == null)
throw new IllegalStateException("android:multiprocess=\"true\" is required for this provider.");
if (! (provider instanceof AppListProvider)) throw new IllegalArgumentException("");
@SuppressWarnings("unchecked") final T casted = (T) provider;
return casted;
} finally { //noinspection deprecation
client.release();
}
}
/**
* Updates calendar color
*/
@SuppressWarnings("deprecation")
public static void updateCalendarColor(Context context) {
int color = PreferencesManager.getCustomCalendarColor(context);
ContentResolver contentResolver = context.getContentResolver();
Uri uri = ContentUris.withAppendedId(
CalendarLoader.getBirthdayAdapterUri(context, CalendarContract.Calendars.CONTENT_URI),
getCalendar(context));
Log.d(TAG, "Updating calendar color to " + color + " with uri " + uri.toString());
ContentProviderClient client = contentResolver
.acquireContentProviderClient(CalendarContract.AUTHORITY);
if(client != null) {
ContentValues values = new ContentValues();
values.put(CalendarContract.Calendars.CALENDAR_COLOR, color);
try {
client.update(uri, values, null, null);
} catch (RemoteException e) {
Log.e(TAG, "Error while updating calendar color!", e);
}
if (android.os.Build.VERSION.SDK_INT < 24) {
client.release();
} else {
client.close();
}
}
}
private void notifyLocationUpdated(long id) {
Message msg = handler.obtainMessage();
msg.what = MSG_LOCATION_UPDATED;
msg.obj = (Long) id;
handler.sendMessage(msg);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
prefs.edit().putLong(SettingsFragment.PREF_LIFELINE_LAST, new Date().getTime()).apply();
Intent lifeline = new Intent(mContext, BackgroundService.class);
lifeline.setAction(BackgroundService.ACTION_LIFELINE);
lifeline.putExtra(BackgroundService.EXTRA_ID, id);
mContext.startService(lifeline);
try {
Uri uri = Uri.parse("content://eu.faircode.lifeline/event");
ContentProviderClient cclient = mContext.getContentResolver().acquireContentProviderClient(uri);
if (cclient != null) {
Location location = getLocation(id);
Uri row = cclient.insert(uri, getLifelineLocation(id, location.getProvider(), location));
cclient.release();
Log.i(TAG, "Updated uri=" + row);
}
} catch (Throwable ex) {
Log.e(TAG, "Lifeline: " + ex.toString() + "\n" + Log.getStackTraceString(ex));
}
}
private void stopContentProvider() {
List<ProviderInfo> providers = BackupUtil.findContentProviders(mContext);
for (ProviderInfo providerInfo : providers) {
ContentResolver resolver = mContext.getContentResolver();
ContentProviderClient client = resolver.acquireContentProviderClient(providerInfo.authority);
ContentProvider provider = client.getLocalContentProvider();
if (provider instanceof BackupableContentProvider) {
((BackupableContentProvider) provider).closeDatabase();
}
client.release();
}
}
public static void releaseQuietly(ContentProviderClient client) {
if (client != null) {
try {
if (VERSION.SDK_INT >= Build.VERSION_CODES.N) {
client.close();
} else {
client.release();
}
} catch (Exception ignored) {
}
}
}
public static List<DocumentFile> listFoldersFilter(@NonNull Context context, @NonNull DocumentFile parent, final FileHelper.NameFilter filter) {
ContentProviderClient client = context.getContentResolver().acquireContentProviderClient(parent.getUri());
if (null == client) return Collections.emptyList();
try {
return FileUtil.listDocumentFiles(context, parent, client, filter, true, false);
} finally {
// ContentProviderClient.close only available on API level 24+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
client.close();
else
client.release();
}
}
private boolean logProviderExists() {
// The method below requires API 16
final ContentProviderClient unstableClient = getContentResolver()
.acquireUnstableContentProviderClient(LogContract.AUTHORITY);
if (unstableClient == null)
return false;
unstableClient.release();
return true;
}
public static Bundle call(Uri uri, String method, String arg, Bundle extras) {
ContentResolver resolver = FairyGlobal.getHostApplication().getContentResolver();
if (Build.VERSION.SDK_INT >= 11) {
try {
return resolver.call(uri, method, arg, extras);
} catch (Exception e) {
LogUtil.e("call uri fail", uri, method, arg, extras);
}
return null;
} else {
ContentProviderClient client = resolver.acquireContentProviderClient(uri);
if (client == null) {
throw new IllegalArgumentException("Unknown URI " + uri);
}
try {
HackContentProviderClient hackContentProviderClient = new HackContentProviderClient(client);
Object mContentProvider = hackContentProviderClient.getContentProvider();
if (mContentProvider != null) {
//public Bundle call(String method, String request, Bundle args)
Object result = new HackIContentProvider(mContentProvider).call(method, arg, extras);
return (Bundle) result;
}
} finally {
client.release();
}
return null;
}
}
protected static <T extends LocalContentProvider> T getInstance(final Context context, final String authority) {
final ContentProviderClient client = context.getContentResolver().acquireContentProviderClient(authority);
if (client == null) throw new IllegalStateException("No active provider associated with authority: " + authority);
try {
final ContentProvider provider = client.getLocalContentProvider();
if (provider == null)
throw new IllegalStateException("android:multiprocess=\"true\" is required for this provider.");
if (! (provider instanceof LocalContentProvider))
throw new IllegalArgumentException("Not a LocalContentProvider associated with authority: " + authority);
@SuppressWarnings("unchecked") final T casted = (T) provider;
return casted;
} finally { //noinspection deprecation
client.release();
}
}