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

The following examples show how to use android.content.ContentResolver#takePersistableUriPermission() . 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: GalleryPrefsFragment.java    From mimi-reader with Apache License 2.0 6 votes vote down vote up
@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_DIR_CHOOSER) {
//            final int takeFlags = data.getFlags() & Intent.FLAG_GRANT_READ_URI_PERMISSION & Intent.FLAG_GRANT_WRITE_URI_PERMISSION & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION;
            final ContentResolver resolver = getActivity().getContentResolver();
            Uri uriTree = data.getData();
            if (uriTree != null) {
                int flags = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
                try {
                    resolver.releasePersistableUriPermission(MimiUtil.getSaveDir().getUri(), flags);
                } catch (Exception e) {
                    if (BuildConfig.DEBUG) {
                        Log.e(LOG_TAG, "Error releasing previous persistable Uri permissions", e);
                    }
                }
                resolver.takePersistableUriPermission(uriTree, flags);
                MimiUtil.setSaveDir(getActivity(), uriTree.toString());
            }
        }
    }
 
Example 2
Source File: SettingsActivity.java    From ToGoZip with GNU General Public License v3.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static void grandPermission5(Context ctx, Uri data) {
    DocumentFile docPath = DocumentFile.fromTreeUri(ctx, data);
    if (docPath != null) {
        final ContentResolver resolver = ctx.getContentResolver();
        resolver.takePersistableUriPermission(data,
                Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    }
}
 
Example 3
Source File: TreeUriScannerIntentService.java    From fdroidclient with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Now determine if it is External Storage that must be handled by the
 * {@link TreeUriScannerIntentService} or whether it is External Storage
 * like an SD Card that can be directly accessed via the file system.
 */
public static void onActivityResult(Context context, Intent intent) {
    if (intent == null) {
        return;
    }
    Uri uri = intent.getData();
    if (uri != null) {
        if (Build.VERSION.SDK_INT >= 19) {
            ContentResolver contentResolver = context.getContentResolver();
            int perms = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
            contentResolver.takePersistableUriPermission(uri, perms);
        }
        String msg = String.format(context.getString(R.string.swap_toast_using_path), uri.toString());
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        scan(context, uri);
    }
}
 
Example 4
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 5
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;
    }
}