Java Code Examples for android.content.ContentResolver#getPersistedUriPermissions()

The following examples show how to use android.content.ContentResolver#getPersistedUriPermissions() . 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: FileHelper.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
public static void revokePreviousPermissions(@NonNull final ContentResolver resolver, @NonNull final Uri newUri) {
    // Unfortunately, the content Uri of the selected resource is not exactly the same as the one stored by ContentResolver
    // -> solution is to compare their TreeDocumentId instead
    String treeUriId = DocumentsContract.getTreeDocumentId(newUri);

    for (UriPermission p : resolver.getPersistedUriPermissions())
        if (!DocumentsContract.getTreeDocumentId(p.getUri()).equals(treeUriId))
            resolver.releasePersistableUriPermission(p.getUri(),
                    Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    if (resolver.getPersistedUriPermissions().isEmpty()) {
        Timber.d("Permissions revoked successfully.");
    } else {
        Timber.d("Permissions failed to be revoked.");
    }
}
 
Example 2
Source File: Api29MigrationActivity.java    From Hentoid with Apache License 2.0 4 votes vote down vote up
public void onSelectSAFRootFolder(@NonNull final Uri treeUri) {

        boolean isUriPermissionPeristed = false;
        ContentResolver contentResolver = getContentResolver();
        String treeUriId = DocumentsContract.getTreeDocumentId(treeUri);

        for (UriPermission p : contentResolver.getPersistedUriPermissions()) {
            if (DocumentsContract.getTreeDocumentId(p.getUri()).equals(treeUriId)) {
                isUriPermissionPeristed = true;
                Timber.d("Uri permission already persisted for %s", treeUri);
                break;
            }
        }

        if (!isUriPermissionPeristed) {
            Timber.d("Persisting Uri permission for %s", treeUri);
            // Release previous access permissions, if different than the new one
            FileHelper.revokePreviousPermissions(contentResolver, treeUri);
            // Persist new access permission
            contentResolver.takePersistableUriPermission(treeUri,
                    Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        }

        DocumentFile selectedFolder = DocumentFile.fromTreeUri(this, treeUri);
        if (selectedFolder != null) {
            String folderName = selectedFolder.getName();
            if (null == folderName) folderName = "";

            // Make sure we detect the Hentoid folder if it's a child of the selected folder
            if (!ImportHelper.isHentoidFolderName(folderName))
                selectedFolder = ImportHelper.getExistingHentoidDirFrom(this, selectedFolder);
        }

        // If no existing hentoid folder is detected, tell the user to select it again
        if (null == selectedFolder || null == selectedFolder.getName() || !ImportHelper.isHentoidFolderName(selectedFolder.getName()))
        {
            ToastUtil.toast("Please select an existing Hentoid folder. Its location is displayed on screen.");
            return;
        }
        scanLibrary(selectedFolder);
    }
 
Example 3
Source File: ImportHelper.java    From Hentoid with Apache License 2.0 4 votes vote down vote up
public static @Result
int setAndScanFolder(
        @NonNull final Context context,
        @NonNull final Uri treeUri,
        boolean askScanExisting,
        @Nullable final ImportOptions options) {

    boolean isUriPermissionPeristed = false;
    ContentResolver contentResolver = context.getContentResolver();
    String treeUriId = DocumentsContract.getTreeDocumentId(treeUri);

    for (UriPermission p : contentResolver.getPersistedUriPermissions()) {
        if (DocumentsContract.getTreeDocumentId(p.getUri()).equals(treeUriId)) {
            isUriPermissionPeristed = true;
            Timber.d("Uri permission already persisted for %s", treeUri);
            break;
        }
    }

    if (!isUriPermissionPeristed) {
        Timber.d("Persisting Uri permission for %s", treeUri);
        // Release previous access permissions, if different than the new one
        FileHelper.revokePreviousPermissions(contentResolver, treeUri);
        // Persist new access permission
        contentResolver.takePersistableUriPermission(treeUri,
                Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    }

    DocumentFile docFile = DocumentFile.fromTreeUri(context, treeUri);
    if (null == docFile || !docFile.exists()) {
        Timber.e("Could not find the selected file %s", treeUri.toString());
        return Result.INVALID_FOLDER;
    }
    DocumentFile hentoidFolder = addHentoidFolder(context, docFile);
    if (null == hentoidFolder) {
        Timber.e("Could not create Hentoid folder in root %s", docFile.getUri().toString());
        return Result.CREATE_FAIL;
    }
    if (!FileHelper.checkAndSetRootFolder(context, hentoidFolder, true)) {
        Timber.e("Could not set the selected root folder %s", hentoidFolder.getUri().toString());
        return Result.INVALID_FOLDER;
    }

    if (hasBooks(context)) {
        if (!askScanExisting) {
            runImport(context, options);
            return Result.OK_LIBRARY_DETECTED;
        } else return Result.OK_LIBRARY_DETECTED_ASK;
    } else {
        // New library created - drop and recreate db (in case user is re-importing)
        new ObjectBoxDAO(context).deleteAllLibraryBooks(true);
        return Result.OK_EMPTY_FOLDER;
    }
}
 
Example 4
Source File: MediaStore.java    From android_9.0.0_r45 with Apache License 2.0 3 votes vote down vote up
/**
 * Gets a URI backed by a {@link DocumentsProvider} that points to the same media
 * file as the specified mediaUri. This allows apps who have permissions to access
 * media files in Storage Access Framework to perform file operations through that
 * on media files.
 * <p>
 * Note: this method doesn't grant any URI permission. Callers need to obtain
 * permission before calling this method. One way to obtain permission is through
 * a 3-step process:
 * <ol>
 *     <li>Call {@link android.os.storage.StorageManager#getStorageVolume(File)} to
 *     obtain the {@link android.os.storage.StorageVolume} of a media file;</li>
 *
 *     <li>Invoke the intent returned by
 *     {@link android.os.storage.StorageVolume#createAccessIntent(String)} to
 *     obtain the access of the volume or one of its specific subdirectories;</li>
 *
 *     <li>Check whether permission is granted and take persistent permission.</li>
 * </ol>
 * @param mediaUri the media URI which document URI is requested
 * @return the document URI
 */
public static Uri getDocumentUri(Context context, Uri mediaUri) {

    try {
        final ContentResolver resolver = context.getContentResolver();

        final String path = getFilePath(resolver, mediaUri);
        final List<UriPermission> uriPermissions = resolver.getPersistedUriPermissions();

        return getDocumentUri(resolver, path, uriPermissions);
    } catch (RemoteException e) {
        throw e.rethrowAsRuntimeException();
    }
}