This commit is contained in:
Alexey Pelykh 2014-12-18 19:39:50 +02:00
parent 8484c9dbf5
commit 904c2bf342
5 changed files with 101 additions and 44 deletions

View file

@ -166,7 +166,7 @@ public class RenderingRuleProperty {
try { try {
return parseColor(value); return parseColor(value);
} catch (RuntimeException e) { } catch (RuntimeException e) {
log.error("Rendering parse " + e.getMessage()); log.error("Rendering parse " + e.getMessage() + " in " + attrName);
} }
return -1; return -1;
} else if(type == FLOAT_TYPE){ } else if(type == FLOAT_TYPE){
@ -178,7 +178,7 @@ public class RenderingRuleProperty {
} }
return 0; return 0;
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
log.error("Rendering parse " + value); log.error("Rendering parse " + value + " in " + attrName);
} }
return -1; return -1;
} else { } else {
@ -198,7 +198,7 @@ public class RenderingRuleProperty {
} }
return Float.parseFloat(value); return Float.parseFloat(value);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
log.error("Rendering parse " + value); log.error("Rendering parse " + value + " in " + attrName);
} }
return -1; return -1;
} else { } else {

View file

@ -1,7 +1,11 @@
package net.osmand.core.android; package net.osmand.core.android;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import net.osmand.core.jni.IMapTiledSymbolsProvider; import net.osmand.core.jni.IMapTiledSymbolsProvider;
@ -17,6 +21,7 @@ import net.osmand.core.jni.MapStylesCollection;
import net.osmand.core.jni.ObfMapObjectsProvider; import net.osmand.core.jni.ObfMapObjectsProvider;
import net.osmand.core.jni.QStringStringHash; import net.osmand.core.jni.QStringStringHash;
import net.osmand.core.jni.ResolvedMapStyle; import net.osmand.core.jni.ResolvedMapStyle;
import net.osmand.core.jni.SwigUtilities;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
import net.osmand.plus.OsmandSettings.CommonPreference; import net.osmand.plus.OsmandSettings.CommonPreference;
@ -30,7 +35,8 @@ import net.osmand.util.Algorithms;
* @author Alexey Pelykh * @author Alexey Pelykh
* *
*/ */
public class MapRendererContext { public class MapRendererContext implements RendererRegistry.IRendererLoadedEventListener {
private static final String TAG = "MapRendererContext";
private static final int OBF_RASTER_LAYER = 0; private static final int OBF_RASTER_LAYER = 0;
private OsmandApplication app; private OsmandApplication app;
@ -101,7 +107,7 @@ public class MapRendererContext {
} }
protected float getDisplayDensityFactor() { protected float getDisplayDensityFactor() {
return (float) (app.getSettings().MAP_DENSITY.get()) * Math.max(1, density); return app.getSettings().MAP_DENSITY.get() * Math.max(1, density);
} }
protected int getRasterTileSize() { protected int getRasterTileSize() {
@ -126,7 +132,28 @@ public class MapRendererContext {
rendName = "default"; rendName = "default";
} }
if (!mapStyles.containsKey(rendName)) { if (!mapStyles.containsKey(rendName)) {
mapStyles.put(rendName, mapStylesCollection.getResolvedStyleByName(rendName)); Log.d(TAG, "Style '" + rendName + "' not in cache");
if (mapStylesCollection.getStyleByName(rendName) == null) {
Log.d(TAG, "Unknown '" + rendName + "' style, need to load");
// Ensure parents are loaded (this may also trigger load)
app.getRendererRegistry().getRenderer(rendName);
if (mapStylesCollection.getStyleByName(rendName) == null) {
try {
loadStyleFromStream(rendName, app.getRendererRegistry().getInputStream(rendName));
} catch (IOException e) {
Log.e(TAG, "Failed to load '" + rendName + "'", e);
}
}
}
ResolvedMapStyle mapStyle = mapStylesCollection.getResolvedStyleByName(rendName);
if (mapStyle != null) {
mapStyles.put(rendName, mapStyle);
} else {
Log.d(TAG, "Failed to resolve '" + rendName + "', will use 'default'");
rendName = "default";
}
} }
ResolvedMapStyle mapStyle = mapStyles.get(rendName); ResolvedMapStyle mapStyle = mapStyles.get(rendName);
CachedMapPresentation pres = new CachedMapPresentation(langId, langPref, mapStyle, displayDensityFactor); CachedMapPresentation pres = new CachedMapPresentation(langId, langPref, mapStyle, displayDensityFactor);
@ -163,8 +190,7 @@ public class MapRendererContext {
} }
QStringStringHash convertedStyleSettings = new QStringStringHash(); QStringStringHash convertedStyleSettings = new QStringStringHash();
for (Iterator<Map.Entry<String, String>> itSetting = props.entrySet().iterator(); itSetting.hasNext();) { for (Map.Entry<String, String> setting : props.entrySet()) {
Map.Entry<String, String> setting = itSetting.next();
convertedStyleSettings.set(setting.getKey(), setting.getValue()); convertedStyleSettings.set(setting.getKey(), setting.getValue());
} }
if (nightMode) { if (nightMode) {
@ -222,7 +248,6 @@ public class MapRendererContext {
} }
} }
private class CachedMapPresentation { private class CachedMapPresentation {
String langId ; String langId ;
LanguagePreference langPref; LanguagePreference langPref;
@ -257,7 +282,45 @@ public class MapRendererContext {
return false; return false;
return true; return true;
} }
} }
public void onRendererLoaded(String name, RenderingRulesStorage rules, InputStream source) {
loadStyleFromStream(name, source);
}
private void loadStyleFromStream(String name, InputStream source) {
if (RendererRegistry.DEFAULT_RENDER.equals(name)) {
if (source != null) {
try {
source.close();
} catch(IOException e) {}
}
return;
}
Log.d(TAG, "Going to pass '" + name + "' style content to native");
byte[] content;
try {
ByteArrayOutputStream intermediateBuffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = source.read(data, 0, data.length)) != -1) {
intermediateBuffer.write(data, 0, nRead);
}
intermediateBuffer.flush();
content = intermediateBuffer.toByteArray();
} catch(IOException e) {
Log.e(TAG, "Failed to read style content", e);
return;
} finally {
try {
source.close();
} catch(IOException e) {}
}
if (!mapStylesCollection.addStyleFromByteArray(
SwigUtilities.createQByteArrayAsCopyOf(content), name)) {
Log.w(TAG, "Failed to add style from byte array");
}
}
} }

View file

View file

@ -33,6 +33,12 @@ public class RendererRegistry {
private Map<String, RenderingRulesStorage> renderers = new LinkedHashMap<String, RenderingRulesStorage>(); private Map<String, RenderingRulesStorage> renderers = new LinkedHashMap<String, RenderingRulesStorage>();
public interface IRendererLoadedEventListener {
void onRendererLoaded(String name, RenderingRulesStorage rules, InputStream source);
}
private IRendererLoadedEventListener rendererLoadedEventListener;
public RendererRegistry(){ public RendererRegistry(){
internalRenderers.put(DEFAULT_RENDER, "default.render.xml"); internalRenderers.put(DEFAULT_RENDER, "default.render.xml");
internalRenderers.put("Touring-view_(more-contrast-and-details)", "Touring-view_(more-contrast-and-details)" +".render.xml"); internalRenderers.put("Touring-view_(more-contrast-and-details)", "Touring-view_(more-contrast-and-details)" +".render.xml");
@ -126,12 +132,16 @@ public class RendererRegistry {
} finally { } finally {
is.close(); is.close();
} }
if (rendererLoadedEventListener != null)
rendererLoadedEventListener.onRendererLoaded(name, main, getInputStream(name));
return main; return main;
} }
@SuppressWarnings("resource") @SuppressWarnings("resource")
private InputStream getInputStream(String name) throws FileNotFoundException { public InputStream getInputStream(String name) throws FileNotFoundException {
InputStream is = null; InputStream is;
if("default".equalsIgnoreCase(name)) { if("default".equalsIgnoreCase(name)) {
name = DEFAULT_RENDER; name = DEFAULT_RENDER;
} }
@ -169,5 +179,11 @@ public class RendererRegistry {
this.currentSelectedRender = currentSelectedRender; this.currentSelectedRender = currentSelectedRender;
} }
public void setRendererLoadedEventListener(IRendererLoadedEventListener listener) {
rendererLoadedEventListener = listener;
}
public IRendererLoadedEventListener getRendererLoadedEventListener() {
return rendererLoadedEventListener;
}
} }

View file

@ -59,36 +59,14 @@ public class NativeCoreContext {
ObfsCollection obfsCollection = new ObfsCollection(); ObfsCollection obfsCollection = new ObfsCollection();
obfsCollection.addDirectory(directory.getAbsolutePath(), false); obfsCollection.addDirectory(directory.getAbsolutePath(), false);
MapStylesCollection mapStylesCollection = setupMapStyleCollection(app);
mapRendererContext = new MapRendererContext(app, dm.density); mapRendererContext = new MapRendererContext(app, dm.density);
mapRendererContext.setupObfMap(mapStylesCollection, obfsCollection); mapRendererContext.setupObfMap(new MapStylesCollection(), obfsCollection);
app.getRendererRegistry().setRendererLoadedEventListener(mapRendererContext);
init = true; init = true;
} }
} }
} }
private static MapStylesCollection setupMapStyleCollection(
OsmandApplication app) {
MapStylesCollection mapStylesCollection = new MapStylesCollection();
// Alexey TODO
// internalRenderers.put("Touring-view_(more-contrast-and-details)", "Touring-view_(more-contrast-and-details)" +".render.xml");
// internalRenderers.put("UniRS", "UniRS" + ".render.xml");
// internalRenderers.put("LightRS", "LightRS" + ".render.xml");
// internalRenderers.put("High-contrast-roads", "High-contrast-roads" + ".render.xml");
// internalRenderers.put("Winter-and-ski", "Winter-and-ski" + ".render.xml");
File renderers = app.getAppPath(IndexConstants.RENDERERS_DIR);
File[] lf = renderers.listFiles();
if(lf != null) {
for(File f : lf) {
if(f.getName().endsWith(IndexConstants.RENDERER_INDEX_EXT)) {
mapStylesCollection.addStyleFromFile(f.getAbsolutePath());
}
}
}
return mapStylesCollection;
}
public static MapRendererContext getMapRendererContext() { public static MapRendererContext getMapRendererContext() {
return mapRendererContext; return mapRendererContext;
} }