Java Code Examples for android.content.ContentProviderClient#release()

The following examples show how to use android.content.ContentProviderClient#release() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ActivityHelper.java    From ActivityDiary with GNU General Public License v3.0 6 votes vote down vote up
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);
}
 
Example 2
Source File: BinaryDictionaryFileDumper.java    From Indic-Keyboard with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
    }
}
 
Example 3
Source File: TestStorage.java    From android-test with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
    }
  }
}
 
Example 4
Source File: FileHelper.java    From Hentoid with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 5
Source File: BinaryDictionaryFileDumper.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
    }
}
 
Example 6
Source File: BinaryDictionaryFileDumper.java    From Android-Keyboard with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
    }
}
 
Example 7
Source File: ContentProviderClientCompat.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
public static void releaseQuietly(ContentProviderClient client) {
	if (client != null) {
		try {
			client.release();
		} catch (Exception ignored) {
		}
	}
}
 
Example 8
Source File: ContentProviderClientCompat.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
public static void releaseQuietly(ContentProviderClient client) {
	if (client != null) {
		try {
			client.release();
		} catch (Exception ignored) {
		}
	}
}
 
Example 9
Source File: CondomProcessTest.java    From MiPushFramework with GNU General Public License v3.0 5 votes vote down vote up
@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"));
}
 
Example 10
Source File: ContentProviderCompat.java    From Android-ServiceManager with MIT License 5 votes vote down vote up
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;
        }
    }
 
Example 11
Source File: AppListProvider.java    From island with Apache License 2.0 5 votes vote down vote up
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();
	}
}
 
Example 12
Source File: CalendarLoader.java    From RememBirthday with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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();
        }
    }
}
 
Example 13
Source File: DatabaseHelper.java    From BackPackTrackII with GNU General Public License v3.0 5 votes vote down vote up
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));
    }
}
 
Example 14
Source File: CarefulDatabaseBackupHelper.java    From android-device-identification with Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example 15
Source File: ContentProviderCompat.java    From DroidPlugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
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) {
        }
    }
}
 
Example 16
Source File: FileHelper.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example 17
Source File: MainActivity.java    From nRF-Logger-API with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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;
}
 
Example 18
Source File: ContentProviderClientCompat.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
public static void releaseQuietly(ContentProviderClient client) {
	if (client != null) {
		try {
			client.release();
		} catch (Exception ignored) {
		}
	}
}
 
Example 19
Source File: CompatForContentProvider.java    From Android-Plugin-Framework with MIT License 5 votes vote down vote up
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;
        }
    }
 
Example 20
Source File: LocalContentProvider.java    From deagle with Apache License 2.0 5 votes vote down vote up
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();
	}
}