diff --git a/docs/Changelog.md b/docs/Changelog.md index 861453d0..f00f0e24 100644 --- a/docs/Changelog.md +++ b/docs/Changelog.md @@ -2,6 +2,7 @@ ## New since 0.14.0 +- Render theme from Android content providers [#783](https://github.com/mapsforge/vtm/pull/783) - Many other minor improvements and bug fixes - [Solved issues](https://github.com/mapsforge/vtm/issues?q=is%3Aclosed+milestone%3A0.15.0) @@ -229,6 +230,8 @@ - libGDX layer gestures [#151](https://github.com/mapsforge/vtm/issues/151) - Render theme area tessellation option [#37](https://github.com/mapsforge/vtm/issues/37) - Render theme resources optional location prefixes [#66](https://github.com/mapsforge/vtm/issues/66) +- Render theme from input stream [#161](https://github.com/mapsforge/vtm/issues/161) +- Render theme from Android assets [#162](https://github.com/mapsforge/vtm/issues/162) - Graphics API platform enhancements [#92](https://github.com/mapsforge/vtm/issues/92) - GeoPoint & BoundingBox improvements [#201](https://github.com/mapsforge/vtm/issues/201) [#200](https://github.com/mapsforge/vtm/issues/200) - vtm-jts module [#53](https://github.com/mapsforge/vtm/issues/53) diff --git a/vtm-android/src/org/oscim/android/theme/ContentRenderTheme.java b/vtm-android/src/org/oscim/android/theme/ContentRenderTheme.java new file mode 100644 index 00000000..1acc33d8 --- /dev/null +++ b/vtm-android/src/org/oscim/android/theme/ContentRenderTheme.java @@ -0,0 +1,109 @@ +/* + * Copyright 2020 devemux86 + * + * 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 . + */ +package org.oscim.android.theme; + +import android.content.ContentResolver; +import android.net.Uri; +import org.oscim.theme.IRenderTheme.ThemeException; +import org.oscim.theme.ThemeFile; +import org.oscim.theme.ThemeUtils; +import org.oscim.theme.XmlRenderThemeMenuCallback; +import org.oscim.utils.Utils; + +import java.io.IOException; +import java.io.InputStream; + +/** + * An ContentRenderTheme allows for customizing the rendering style of the map + * via an XML from the Android content providers. + */ +public class ContentRenderTheme implements ThemeFile { + private static final long serialVersionUID = 1L; + + private final ContentResolver mContentResolver; + private XmlRenderThemeMenuCallback mMenuCallback; + private final String mRelativePathPrefix; + 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. + * @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); + } + + /** + * @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. + * @throws ThemeException if an error occurs while reading the render theme XML. + */ + public ContentRenderTheme(ContentResolver contentResolver, String relativePathPrefix, Uri uri, XmlRenderThemeMenuCallback menuCallback) throws ThemeException { + mContentResolver = contentResolver; + mRelativePathPrefix = relativePathPrefix; + mUri = uri; + mMenuCallback = menuCallback; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } else if (!(obj instanceof ContentRenderTheme)) { + return false; + } + ContentRenderTheme other = (ContentRenderTheme) obj; + if (getRenderThemeAsStream() != other.getRenderThemeAsStream()) { + return false; + } + if (!Utils.equals(mRelativePathPrefix, other.mRelativePathPrefix)) { + return false; + } + return true; + } + + @Override + public XmlRenderThemeMenuCallback getMenuCallback() { + return mMenuCallback; + } + + @Override + public String getRelativePathPrefix() { + return mRelativePathPrefix; + } + + @Override + public InputStream getRenderThemeAsStream() throws ThemeException { + try { + return mContentResolver.openInputStream(mUri); + } catch (IOException e) { + throw new ThemeException(e.getMessage()); + } + } + + @Override + public boolean isMapsforgeTheme() { + return ThemeUtils.isMapsforgeTheme(this); + } + + @Override + public void setMenuCallback(XmlRenderThemeMenuCallback menuCallback) { + mMenuCallback = menuCallback; + } +}