Render themes: Android scoped storage, zip render theme, custom resource providers (#804)
This commit is contained in:
@@ -30,6 +30,7 @@ import org.oscim.backend.canvas.Canvas;
|
||||
import org.oscim.backend.canvas.Paint;
|
||||
import org.oscim.layers.marker.MarkerSymbol;
|
||||
import org.oscim.layers.marker.MarkerSymbol.HotspotPlace;
|
||||
import org.oscim.theme.XmlThemeResourceProvider;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -69,8 +70,8 @@ public final class AndroidGraphics extends CanvasAdapter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap loadBitmapAssetImpl(String relativePathPrefix, String src, int width, int height, int percent) throws IOException {
|
||||
return createBitmap(relativePathPrefix, src, width, height, percent);
|
||||
public Bitmap loadBitmapAssetImpl(String relativePathPrefix, String src, XmlThemeResourceProvider resourceProvider, int width, int height, int percent) throws IOException {
|
||||
return createBitmap(relativePathPrefix, src, resourceProvider, width, height, percent);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
* Copyright 2010, 2011, 2012 mapsforge.org
|
||||
* Copyright 2016-2021 devemux86
|
||||
* Copyright 2017 Andrey Novikov
|
||||
* Copyright 2021 eddiemuc
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU Lesser General Public License as published by the Free Software
|
||||
@@ -21,6 +22,7 @@ import android.text.TextUtils;
|
||||
import org.oscim.theme.IRenderTheme.ThemeException;
|
||||
import org.oscim.theme.ThemeFile;
|
||||
import org.oscim.theme.XmlRenderThemeMenuCallback;
|
||||
import org.oscim.theme.XmlThemeResourceProvider;
|
||||
import org.oscim.utils.Utils;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -38,6 +40,7 @@ public class AssetsRenderTheme implements ThemeFile {
|
||||
private boolean mMapsforgeTheme;
|
||||
private XmlRenderThemeMenuCallback mMenuCallback;
|
||||
private final String mRelativePathPrefix;
|
||||
private XmlThemeResourceProvider mResourceProvider;
|
||||
|
||||
/**
|
||||
* @param assetManager the Android asset manager.
|
||||
@@ -99,6 +102,11 @@ public class AssetsRenderTheme implements ThemeFile {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public XmlThemeResourceProvider getResourceProvider() {
|
||||
return mResourceProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMapsforgeTheme() {
|
||||
return mMapsforgeTheme;
|
||||
@@ -113,4 +121,9 @@ public class AssetsRenderTheme implements ThemeFile {
|
||||
public void setMenuCallback(XmlRenderThemeMenuCallback menuCallback) {
|
||||
mMenuCallback = menuCallback;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setResourceProvider(XmlThemeResourceProvider resourceProvider) {
|
||||
mResourceProvider = resourceProvider;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Copyright 2020-2021 devemux86
|
||||
* Copyright 2021 eddiemuc
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU Lesser General Public License as published by the Free Software
|
||||
@@ -19,7 +20,7 @@ import android.net.Uri;
|
||||
import org.oscim.theme.IRenderTheme.ThemeException;
|
||||
import org.oscim.theme.ThemeFile;
|
||||
import org.oscim.theme.XmlRenderThemeMenuCallback;
|
||||
import org.oscim.utils.Utils;
|
||||
import org.oscim.theme.XmlThemeResourceProvider;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -34,29 +35,26 @@ public class ContentRenderTheme implements ThemeFile {
|
||||
private final ContentResolver mContentResolver;
|
||||
private boolean mMapsforgeTheme;
|
||||
private XmlRenderThemeMenuCallback mMenuCallback;
|
||||
private final String mRelativePathPrefix;
|
||||
private XmlThemeResourceProvider mResourceProvider;
|
||||
private final Uri mUri;
|
||||
|
||||
/**
|
||||
* @param contentResolver the Android content resolver.
|
||||
* @param relativePathPrefix the prefix for all relative resource paths.
|
||||
* @param uri the XML render theme URI.
|
||||
* @param contentResolver the Android content resolver.
|
||||
* @param uri the XML render theme URI.
|
||||
* @throws ThemeException if an error occurs while reading the render theme XML.
|
||||
*/
|
||||
public ContentRenderTheme(ContentResolver contentResolver, String relativePathPrefix, Uri uri) throws ThemeException {
|
||||
this(contentResolver, relativePathPrefix, uri, null);
|
||||
public ContentRenderTheme(ContentResolver contentResolver, Uri uri) throws ThemeException {
|
||||
this(contentResolver, uri, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param contentResolver the Android content resolver.
|
||||
* @param relativePathPrefix the prefix for all relative resource paths.
|
||||
* @param uri the XML render theme URI.
|
||||
* @param menuCallback the interface callback to create a settings menu on the fly.
|
||||
* @param contentResolver the Android content resolver.
|
||||
* @param uri the XML render theme URI.
|
||||
* @param menuCallback the interface callback to create a settings menu on the fly.
|
||||
* @throws ThemeException if an error occurs while reading the render theme XML.
|
||||
*/
|
||||
public ContentRenderTheme(ContentResolver contentResolver, String relativePathPrefix, Uri uri, XmlRenderThemeMenuCallback menuCallback) throws ThemeException {
|
||||
public ContentRenderTheme(ContentResolver contentResolver, Uri uri, XmlRenderThemeMenuCallback menuCallback) throws ThemeException {
|
||||
mContentResolver = contentResolver;
|
||||
mRelativePathPrefix = relativePathPrefix;
|
||||
mUri = uri;
|
||||
mMenuCallback = menuCallback;
|
||||
}
|
||||
@@ -72,9 +70,6 @@ public class ContentRenderTheme implements ThemeFile {
|
||||
if (getRenderThemeAsStream() != other.getRenderThemeAsStream()) {
|
||||
return false;
|
||||
}
|
||||
if (!Utils.equals(mRelativePathPrefix, other.mRelativePathPrefix)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -85,7 +80,7 @@ public class ContentRenderTheme implements ThemeFile {
|
||||
|
||||
@Override
|
||||
public String getRelativePathPrefix() {
|
||||
return mRelativePathPrefix;
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -97,6 +92,11 @@ public class ContentRenderTheme implements ThemeFile {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public XmlThemeResourceProvider getResourceProvider() {
|
||||
return mResourceProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMapsforgeTheme() {
|
||||
return mMapsforgeTheme;
|
||||
@@ -111,4 +111,9 @@ public class ContentRenderTheme implements ThemeFile {
|
||||
public void setMenuCallback(XmlRenderThemeMenuCallback menuCallback) {
|
||||
mMenuCallback = menuCallback;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setResourceProvider(XmlThemeResourceProvider resourceProvider) {
|
||||
mResourceProvider = resourceProvider;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
* Copyright 2021 eddiemuc
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU Lesser General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.oscim.android.theme;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.provider.DocumentsContract;
|
||||
import org.oscim.backend.CanvasAdapter;
|
||||
import org.oscim.theme.XmlThemeResourceProvider;
|
||||
import org.oscim.utils.IOUtils;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* An xml theme resource provider resolving resources using Android scoped storage (document framework).
|
||||
* <p>
|
||||
* Implementation note: these methods do not use DocumentFile internally,
|
||||
* but query directly for document info due to vastly better performance.
|
||||
* Also for better performance, this implementation caches resource uris.
|
||||
* <p>
|
||||
* Note: this implementation requires minimum Android 5.0 (API 21)
|
||||
*/
|
||||
public class ContentResolverResourceProvider implements XmlThemeResourceProvider {
|
||||
|
||||
private final ContentResolver contentResolver;
|
||||
private final Uri relativeRootUri;
|
||||
|
||||
private final Map<String, Uri> resourceUriCache = new HashMap<>();
|
||||
|
||||
private static class DocumentInfo {
|
||||
private final String name;
|
||||
private final Uri uri;
|
||||
private final boolean isDirectory;
|
||||
|
||||
private DocumentInfo(String name, Uri uri, boolean isDirectory) {
|
||||
this.name = name;
|
||||
this.uri = uri;
|
||||
this.isDirectory = isDirectory;
|
||||
}
|
||||
}
|
||||
|
||||
public ContentResolverResourceProvider(ContentResolver contentResolver, Uri treeUri) {
|
||||
this.contentResolver = contentResolver;
|
||||
this.relativeRootUri = treeUri;
|
||||
|
||||
refreshCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Build uri cache for one dir level (recursive function).
|
||||
*/
|
||||
private void buildCacheLevel(String prefix, Uri dirUri) {
|
||||
List<DocumentInfo> docs = queryDir(dirUri);
|
||||
for (DocumentInfo doc : docs) {
|
||||
if (doc.isDirectory) {
|
||||
buildCacheLevel(prefix + doc.name + "/", doc.uri);
|
||||
} else {
|
||||
resourceUriCache.put(prefix + doc.name, doc.uri);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream createInputStream(String relativePath, String source) throws FileNotFoundException {
|
||||
Uri docUri = resourceUriCache.get(source);
|
||||
if (docUri != null) {
|
||||
return contentResolver.openInputStream(docUri);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Query the content of a directory using scoped storage.
|
||||
*
|
||||
* @return a list of arrays with info [0: name (String), 1: uri (Uri), 2: isDir (boolean)]
|
||||
*/
|
||||
private List<DocumentInfo> queryDir(Uri dirUri) {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
if (dirUri == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<DocumentInfo> result = new ArrayList<>();
|
||||
Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(dirUri, DocumentsContract.getDocumentId(dirUri));
|
||||
|
||||
String[] columns = new String[]{
|
||||
DocumentsContract.Document.COLUMN_DOCUMENT_ID,
|
||||
DocumentsContract.Document.COLUMN_DISPLAY_NAME,
|
||||
DocumentsContract.Document.COLUMN_MIME_TYPE
|
||||
};
|
||||
|
||||
Cursor c = null;
|
||||
try {
|
||||
c = contentResolver.query(childrenUri, columns, null, null, null);
|
||||
|
||||
while (c.moveToNext()) {
|
||||
String documentId = c.getString(0);
|
||||
String name = c.getString(1);
|
||||
String mimeType = c.getString(2);
|
||||
|
||||
Uri uri = DocumentsContract.buildDocumentUriUsingTree(dirUri, documentId);
|
||||
boolean isDir = DocumentsContract.Document.MIME_TYPE_DIR.equals(mimeType);
|
||||
result.add(new DocumentInfo(name, uri, isDir));
|
||||
}
|
||||
|
||||
return result;
|
||||
} finally {
|
||||
IOUtils.closeQuietly(c);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the uri cache by recreating it.
|
||||
*/
|
||||
private void refreshCache() {
|
||||
resourceUriCache.clear();
|
||||
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
||||
return;
|
||||
}
|
||||
if (relativeRootUri == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Uri dirUri = DocumentsContract.buildDocumentUriUsingTree(relativeRootUri, DocumentsContract.getTreeDocumentId(relativeRootUri));
|
||||
buildCacheLevel(CanvasAdapter.PREFIX_FILE, dirUri);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user