ContentResolverResourceProvider should handle document Uris correctly (#808)

This commit is contained in:
Emux 2021-03-01 11:09:43 +02:00 committed by GitHub
parent ddf94ae2ca
commit 6850eff26c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,6 +15,7 @@
package org.oscim.android.theme; package org.oscim.android.theme;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
@ -40,6 +41,7 @@ public class ContentResolverResourceProvider implements XmlThemeResourceProvider
private final ContentResolver contentResolver; private final ContentResolver contentResolver;
private final Uri relativeRootUri; private final Uri relativeRootUri;
private final boolean isDocumentUri;
private final Map<String, Uri> resourceUriCache = new HashMap<>(); private final Map<String, Uri> resourceUriCache = new HashMap<>();
@ -55,9 +57,34 @@ public class ContentResolverResourceProvider implements XmlThemeResourceProvider
} }
} }
public ContentResolverResourceProvider(ContentResolver contentResolver, Uri treeUri) { /**
* Creates a new content resolver resource provider.
*
* @param contentResolver content resolver used to read content.
* @param relativeRootUri uri pointing to a directory.
* Uri is assumed to be a pure tree Uri (as e.g. returned by {@link Intent#ACTION_OPEN_DOCUMENT_TREE}).
*/
public ContentResolverResourceProvider(ContentResolver contentResolver, Uri relativeRootUri) {
this(contentResolver, relativeRootUri, false);
}
/**
* Creates a new content resolver resource provider.
*
* @param contentResolver content resolver used to read content.
* @param relativeRootUri uri pointing to a directory.
* @param isDocumentUri Uris as returned e.g. by {@link Intent#ACTION_OPEN_DOCUMENT_TREE}) cannot directly be used to scan directories and read content.
* They must be converted to document uris first using {@link DocumentsContract#buildChildDocumentsUriUsingTree(Uri, String)}.
* However, in some situations this conversion was done previously by caller (e.g. if root dir should be subdirectory of a directory returned by {@link Intent#ACTION_OPEN_DOCUMENT_TREE}).
* In these cases, converted Uri will point to original root directory which is not always the wanted behaviour.
* Thus, this parameter allows caller to control whether conversion should be done or not.
* If set to true, then given Uri is considered to be a document uri already and no conversion is done.
* If set to false, uri is considered to be a pure tree uri as returned e.g. by {@link Intent#ACTION_OPEN_DOCUMENT_TREE}) and it is converted.
*/
public ContentResolverResourceProvider(ContentResolver contentResolver, Uri relativeRootUri, boolean isDocumentUri) {
this.contentResolver = contentResolver; this.contentResolver = contentResolver;
this.relativeRootUri = treeUri; this.relativeRootUri = relativeRootUri;
this.isDocumentUri = isDocumentUri;
refreshCache(); refreshCache();
} }
@ -140,7 +167,11 @@ public class ContentResolverResourceProvider implements XmlThemeResourceProvider
return; return;
} }
Uri dirUri = DocumentsContract.buildDocumentUriUsingTree(relativeRootUri, DocumentsContract.getTreeDocumentId(relativeRootUri)); Uri dirUri = relativeRootUri;
if (!isDocumentUri) {
// Convert "tree uri" to a "document uri"
dirUri = DocumentsContract.buildDocumentUriUsingTree(dirUri, DocumentsContract.getTreeDocumentId(dirUri));
}
buildCacheLevel(CanvasAdapter.PREFIX_FILE, dirUri); buildCacheLevel(CanvasAdapter.PREFIX_FILE, dirUri);
} }
} }