diff --git a/vtm-android/src/org/oscim/android/theme/ContentResolverResourceProvider.java b/vtm-android/src/org/oscim/android/theme/ContentResolverResourceProvider.java index d0bc3b20..9ae1b7a2 100644 --- a/vtm-android/src/org/oscim/android/theme/ContentResolverResourceProvider.java +++ b/vtm-android/src/org/oscim/android/theme/ContentResolverResourceProvider.java @@ -15,6 +15,7 @@ package org.oscim.android.theme; import android.content.ContentResolver; +import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Build; @@ -40,6 +41,7 @@ public class ContentResolverResourceProvider implements XmlThemeResourceProvider private final ContentResolver contentResolver; private final Uri relativeRootUri; + private final boolean isDocumentUri; private final Map 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.relativeRootUri = treeUri; + this.relativeRootUri = relativeRootUri; + this.isDocumentUri = isDocumentUri; refreshCache(); } @@ -140,7 +167,11 @@ public class ContentResolverResourceProvider implements XmlThemeResourceProvider 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); } }