下面列出了android.provider.DocumentsContract#getTreeDocumentId ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
@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;
}
@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;
}
/**
* 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;
}
}
/**
* 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;
}
}
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);
}
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);
}
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;
}
}
/**
* 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));
}
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;
}
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;
}
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.");
}
}
@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;
}
@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;
}
@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;
}
}
@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;
}
/**
* 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;
}
}
/**
* 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;
}
}
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);
}
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;
}
}