Java Code Examples for android.provider.DocumentsContract#getTreeDocumentId()

The following examples show how to use android.provider.DocumentsContract#getTreeDocumentId() . 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: DocumentsContractApi21.java    From UniFile with Apache License 2.0 7 votes vote down vote up
public static Uri prepareTreeUri(Uri treeUri) {
    String documentId;
    try {
        documentId = DocumentsContract.getDocumentId(treeUri);
        if (documentId == null) {
            throw new IllegalArgumentException();
        }
    } catch (Exception e) {
        // IllegalArgumentException will be raised
        // if DocumentsContract.getDocumentId() failed.
        // But it isn't mentioned the document,
        // catch all kinds of Exception for safety.
        documentId = DocumentsContract.getTreeDocumentId(treeUri);
    }
    return DocumentsContract.buildDocumentUriUsingTree(treeUri, documentId);
}
 
Example 2
Source File: FileUtil.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
/**
 * WARNING This is a tweak of internal Android code to make it faster by caching calls to queryIntentContentProviders
 * Original (uncached) is DocumentFile.fromTreeUri
 */
@Nullable
private static DocumentFile fromTreeUriCached(@NonNull final Context context, @NonNull final Uri treeUri) {
    String documentId = DocumentsContract.getTreeDocumentId(treeUri);
    if (isDocumentUriCached(context, treeUri)) {
        documentId = DocumentsContract.getDocumentId(treeUri);
    }
    return newTreeDocumentFile(null, context,
            DocumentsContract.buildDocumentUriUsingTree(treeUri,
                    documentId));
}
 
Example 3
Source File: FileUtil.java    From Augendiagnose with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the document path (relative to volume name) for a tree URI (LOLLIPOP).
 *
 * @param treeUri The tree URI.
 * @return the document path.
 */
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private static String getDocumentPathFromTreeUri(final Uri treeUri) {
	final String docId = DocumentsContract.getTreeDocumentId(treeUri);
	final String[] split = docId.split(":");
	if ((split.length >= 2) && (split[1] != null)) {
		return split[1];
	}
	else {
		return File.separator;
	}
}
 
Example 4
Source File: FileUtil.java    From Augendiagnose with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the volume ID from the tree URI.
 *
 * @param treeUri The tree URI.
 * @return The volume ID.
 */
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private static String getVolumeIdFromTreeUri(final Uri treeUri) {
	final String docId = DocumentsContract.getTreeDocumentId(treeUri);
	final String[] split = docId.split(":");

	if (split.length > 0) {
		return split[0];
	}
	else {
		return null;
	}
}
 
Example 5
Source File: FileUtils.java    From syncthing-android with Mozilla Public License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static String getDocumentPathFromTreeUri(final Uri treeUri) {
    final String docId = DocumentsContract.getTreeDocumentId(treeUri);
    final String[] split = docId.split(":");
    if ((split.length >= 2) && (split[1] != null)) return split[1];
    else return File.separator;
}
 
Example 6
Source File: FileUtils.java    From syncthing-android with Mozilla Public License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static String getVolumeIdFromTreeUri(final Uri treeUri) {
    final String docId = DocumentsContract.getTreeDocumentId(treeUri);
    final String[] split = docId.split(":");
    if (split.length > 0) {
        return split[0];
    } else {
        return null;
    }
}
 
Example 7
Source File: TreeUriUtils.java    From fdroidclient with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static String getDocumentPathFromTreeUri(final Uri treeUri) {
    final String docId = DocumentsContract.getTreeDocumentId(treeUri);
    final String[] split = docId.split(":");
    if ((split.length >= 2) && (split[1] != null)) return split[1];
    else return File.separator;
}
 
Example 8
Source File: TreeUriUtils.java    From fdroidclient with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static String getVolumeIdFromTreeUri(final Uri treeUri) {
    final String docId = DocumentsContract.getTreeDocumentId(treeUri);
    final String[] split = docId.split(":");
    if (split.length > 0) return split[0];
    else return null;
}
 
Example 9
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 10
Source File: FileHelper.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
private static String getDocumentPathFromUri(final Uri uri, boolean isFolder) {
    final String docId;
    if (isFolder) docId = DocumentsContract.getTreeDocumentId(uri);
    else docId = DocumentsContract.getDocumentId(uri);

    final String[] split = docId.split(":");
    if ((split.length >= 2) && (split[1] != null)) return split[1];
    else return File.separator;
}
 
Example 11
Source File: FileHelper.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
private static String getVolumeIdFromUri(final Uri uri, boolean isFolder) {
    final String docId;
    if (isFolder) docId = DocumentsContract.getTreeDocumentId(uri);
    else docId = DocumentsContract.getDocumentId(uri);

    final String[] split = docId.split(":");
    if (split.length > 0) return split[0];
    else return null;
}
 
Example 12
Source File: FileUtils.java    From CommonUtils with Apache License 2.0 5 votes vote down vote up
@Nullable
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static String getVolumeIdFromTreeUri(@NonNull Uri treeUri) {
    final String docId = DocumentsContract.getTreeDocumentId(treeUri);
    final String[] split = docId.split(":");
    if (split.length > 0) return split[0];
    else return null;
}
 
Example 13
Source File: DocumentTreeLocation.java    From edslite with GNU General Public License v2.0 5 votes vote down vote up
public static boolean isDocumentTreeUri(Context context, Uri uri)
{
	try
	{
		//noinspection ConstantConditions
		return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP &&
				DocumentsContract.getTreeDocumentId(uri) != null && DocumentFile.isDocumentUri(context, uri);
	}
	catch (IllegalArgumentException e)
	{
		return false;
	}
}
 
Example 14
Source File: SafUtils.java    From SAI with GNU General Public License v3.0 5 votes vote down vote up
public static String getPathWithoutRoot(Uri docUri) {
    String path = DocumentsContract.getTreeDocumentId(docUri);

    int indexOfLastColon = path.lastIndexOf(':');
    if (indexOfLastColon == -1)
        throw new IllegalArgumentException("Given uri does not contain a colon: " + docUri);

    return path.substring(indexOfLastColon + 1);
}
 
Example 15
Source File: SafUtils.java    From SAI with GNU General Public License v3.0 5 votes vote down vote up
public static String getRootForPath(Uri docUri) {
    String path = DocumentsContract.getTreeDocumentId(docUri);

    int indexOfLastColon = path.lastIndexOf(':');
    if (indexOfLastColon == -1)
        throw new IllegalArgumentException("Given uri does not contain a colon: " + docUri);

    return path.substring(0, indexOfLastColon);
}
 
Example 16
Source File: AndroidPathUtils.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get the document path (relative to volume name) for a tree URI (LOLLIPOP).
 *
 * @param treeUri The tree URI.
 * @return the document path.
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static String getDocumentPathFromTreeUri(final Uri treeUri) {
	final String docId = DocumentsContract.getTreeDocumentId(treeUri);
	final String[] split = docId.split(":");
	if ((split.length >= 2) && (split[1] != null)) {
		return split[1];
	} else {
		return File.separator;
	}
}
 
Example 17
Source File: AndroidPathUtils.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get the volume ID from the tree URI.
 *
 * @param treeUri The tree URI.
 * @return The volume ID.
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static String getVolumeIdFromTreeUri(final Uri treeUri) {
	final String docId = DocumentsContract.getTreeDocumentId(treeUri);
	final String[] split = docId.split(":");

	if (split.length > 0) {
		return split[0];
	} else {
		return null;
	}
}
 
Example 18
Source File: FileUtils.java    From CommonUtils with Apache License 2.0 5 votes vote down vote up
@NonNull
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static String getDocumentPathFromTreeUri(@NonNull Uri treeUri) {
    final String docId = DocumentsContract.getTreeDocumentId(treeUri);
    final String[] split = docId.split(":");
    if ((split.length >= 2) && (split[1] != null)) return split[1];
    else return File.separator;
}
 
Example 19
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 20
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);
    }