Slight refactor native core context

This commit is contained in:
Victor Shcherb 2014-12-02 07:53:01 +01:00
parent 5a53fb41a4
commit 31086faff2
6 changed files with 173 additions and 305 deletions

View file

@ -1,6 +1,5 @@
package net.osmand.core.android; package net.osmand.core.android;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
@ -10,13 +9,13 @@ import net.osmand.core.jni.IObfsCollection;
import net.osmand.core.jni.IRasterMapLayerProvider; import net.osmand.core.jni.IRasterMapLayerProvider;
import net.osmand.core.jni.MapObjectsSymbolsProvider; import net.osmand.core.jni.MapObjectsSymbolsProvider;
import net.osmand.core.jni.MapPresentationEnvironment; import net.osmand.core.jni.MapPresentationEnvironment;
import net.osmand.core.jni.MapPresentationEnvironment.LanguagePreference;
import net.osmand.core.jni.MapPrimitivesProvider; import net.osmand.core.jni.MapPrimitivesProvider;
import net.osmand.core.jni.MapPrimitiviser; import net.osmand.core.jni.MapPrimitiviser;
import net.osmand.core.jni.MapRasterLayerProvider_Software; import net.osmand.core.jni.MapRasterLayerProvider_Software;
import net.osmand.core.jni.MapStylesCollection; 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.MapPresentationEnvironment.LanguagePreference;
import net.osmand.core.jni.ResolvedMapStyle; import net.osmand.core.jni.ResolvedMapStyle;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
@ -28,7 +27,6 @@ import net.osmand.util.Algorithms;
/** /**
* Context container and utility class for MapRendererView and derivatives. * Context container and utility class for MapRendererView and derivatives.
*
* @author Alexey Pelykh * @author Alexey Pelykh
* *
*/ */
@ -37,58 +35,27 @@ public class MapRendererContext {
private static final int OBF_RASTER_LAYER = 0; private static final int OBF_RASTER_LAYER = 0;
private OsmandApplication app; private OsmandApplication app;
/** // input parameters
* Cached map styles per name
*/
private Map<String, ResolvedMapStyle> mapStyles = new HashMap<String, ResolvedMapStyle>();
/**
* Reference to OBF map symbols provider (if used)
*/
private IMapTiledSymbolsProvider obfMapSymbolsProvider;
/**
* Map styles collection
*/
private MapStylesCollection mapStylesCollection; private MapStylesCollection mapStylesCollection;
/**
* Reference to map presentation environment (if used)
*/
private MapPresentationEnvironment mapPresentationEnvironment;
/**
* Reference to OBFs collection (if present)
*/
private IObfsCollection obfsCollection; private IObfsCollection obfsCollection;
/** private boolean nightMode;
* Reference to map renderer view that is currently managed by this private final float density;
* context
*/ // ached objects
private Map<String, ResolvedMapStyle> mapStyles = new HashMap<String, ResolvedMapStyle>();
private CachedMapPresentation presentationObjectParams;
private MapPresentationEnvironment mapPresentationEnvironment;
private IMapTiledSymbolsProvider obfMapSymbolsProvider;
private IRasterMapLayerProvider obfMapRasterLayerProvider;
private MapRendererView mapRendererView; private MapRendererView mapRendererView;
private float cachedReferenceTileSize;
/** public MapRendererContext(OsmandApplication app, float density) {
* Display density factor
*/
private float displayDensityFactor = 1;
/**
* Reference tile size on screen in pixels
*/
private float referenceTileSizef = 256;
/**
* Raster tile size in texels
*/
private int rasterTileSize = 256;
private CachedMapPresentation presentationObjectParams;
public MapRendererContext(OsmandApplication app) {
this.app = app; this.app = app;
this.density = density;
} }
/** /**
@ -102,7 +69,157 @@ public class MapRendererContext {
} }
this.mapRendererView = mapRendererView; this.mapRendererView = mapRendererView;
if (mapRendererView != null) { if (mapRendererView != null) {
apply(); applyCurrentContextToView();
}
}
public boolean isNightMode() {
return nightMode;
}
public void setNightMode(boolean nightMode) {
this.nightMode = nightMode;
updateMapSettings();
}
public void updateMapSettings() {
if (mapRendererView instanceof AtlasMapRendererView && cachedReferenceTileSize != getReferenceTileSize()) {
((AtlasMapRendererView) mapRendererView).setReferenceTileSizeOnScreenInPixels(getReferenceTileSize());
}
if(mapPresentationEnvironment != null) {
updateMapPresentationEnvironment();
}
}
/**
* Setup OBF map on layer 0 with symbols
* @param obfsCollection OBFs collection
*/
public void setupObfMap(MapStylesCollection mapStylesCollection, IObfsCollection obfsCollection) {
this.obfsCollection = obfsCollection;
this.mapStylesCollection = mapStylesCollection;
updateMapPresentationEnvironment();
recreateRasterAndSymbolsProvider();
}
protected float getDisplayDensityFactor() {
return (float) Math.pow(2, Math.sqrt((app.getSettings().getSettingsZoomScale() + density)));
}
protected int getRasterTileSize() {
return Integer.highestOneBit((int) getReferenceTileSize() - 1) * 2;
}
private float getReferenceTileSize() {
return 256 * getDisplayDensityFactor();
}
/**
* Update map presentation environment and everything that depends on it
*/
private void updateMapPresentationEnvironment() {
float displayDensityFactor = getDisplayDensityFactor();
// Create new map presentation environment
String langId = app.getSettings().MAP_PREFERRED_LOCALE.get();
// TODO make setting
LanguagePreference langPref = LanguagePreference.LocalizedOrNative;
String rendName = app.getSettings().RENDERER.get();
if (rendName.length() == 0 || rendName.equals(RendererRegistry.DEFAULT_RENDER)) {
rendName = "default";
}
if (!mapStyles.containsKey(rendName)) {
mapStyles.put(rendName, mapStylesCollection.getResolvedStyleByName(rendName));
}
ResolvedMapStyle mapStyle = mapStyles.get(rendName);
CachedMapPresentation pres = new CachedMapPresentation(langId, langPref, mapStyle, displayDensityFactor);
if (this.presentationObjectParams == null || !this.presentationObjectParams.equalsFields(pres)) {
this.presentationObjectParams = pres;
mapPresentationEnvironment = new MapPresentationEnvironment(mapStyle, displayDensityFactor, langId,
langPref);
}
QStringStringHash convertedStyleSettings = getMapStyleSettings();
mapPresentationEnvironment.setSettings(convertedStyleSettings);
if (obfMapRasterLayerProvider != null || obfMapSymbolsProvider != null) {
recreateRasterAndSymbolsProvider();
}
}
protected QStringStringHash getMapStyleSettings() {
// Apply map style settings
OsmandSettings prefs = app.getSettings();
RenderingRulesStorage storage = app.getRendererRegistry().getCurrentSelectedRenderer();
Map<String, String> props = new HashMap<String, String>();
for (RenderingRuleProperty customProp : storage.PROPS.getCustomRules()) {
if (customProp.isBoolean()) {
CommonPreference<Boolean> pref = prefs.getCustomRenderBooleanProperty(customProp.getAttrName());
props.put(customProp.getAttrName(), pref.get() + "");
} else {
CommonPreference<String> settings = prefs.getCustomRenderProperty(customProp.getAttrName());
String res = settings.get();
if (!Algorithms.isEmpty(res)) {
props.put(customProp.getAttrName(), res);
}
}
}
QStringStringHash convertedStyleSettings = new QStringStringHash();
for (Iterator<Map.Entry<String, String>> itSetting = props.entrySet().iterator(); itSetting.hasNext();) {
Map.Entry<String, String> setting = itSetting.next();
convertedStyleSettings.set(setting.getKey(), setting.getValue());
}
if (nightMode) {
convertedStyleSettings.set("nightMode", "true");
}
return convertedStyleSettings;
}
private void recreateRasterAndSymbolsProvider() {
// Create new map primitiviser
MapPrimitiviser mapPrimitiviser = new MapPrimitiviser(mapPresentationEnvironment);
ObfMapObjectsProvider obfMapObjectsProvider = new ObfMapObjectsProvider(obfsCollection);
// Create new map primitives provider
MapPrimitivesProvider mapPrimitivesProvider = new MapPrimitivesProvider(obfMapObjectsProvider, mapPrimitiviser,
getRasterTileSize());
updateObfMapRasterLayerProvider(mapPrimitivesProvider);
updateObfMapSymbolsProvider(mapPrimitivesProvider);
}
private void updateObfMapRasterLayerProvider(MapPrimitivesProvider mapPrimitivesProvider) {
// Create new OBF map raster layer provider
obfMapRasterLayerProvider = new MapRasterLayerProvider_Software(mapPrimitivesProvider);
// In case there's bound view and configured layer, perform setup
if (mapRendererView != null) {
mapRendererView.setMapLayerProvider(OBF_RASTER_LAYER, obfMapRasterLayerProvider);
}
}
private void updateObfMapSymbolsProvider(MapPrimitivesProvider mapPrimitivesProvider) {
// If there's current provider and bound view, remove it
if (obfMapSymbolsProvider != null && mapRendererView != null) {
mapRendererView.removeSymbolsProvider(obfMapSymbolsProvider);
}
// Create new OBF map symbols provider
obfMapSymbolsProvider = new MapObjectsSymbolsProvider(mapPrimitivesProvider, getReferenceTileSize());
// If there's bound view, add new provider
if (mapRendererView != null) {
mapRendererView.addSymbolsProvider(obfMapSymbolsProvider);
}
}
private void applyCurrentContextToView() {
if (mapRendererView instanceof AtlasMapRendererView) {
cachedReferenceTileSize = getReferenceTileSize();
((AtlasMapRendererView)mapRendererView).setReferenceTileSizeOnScreenInPixels(cachedReferenceTileSize);
}
// Layers
if (obfMapRasterLayerProvider != null) {
mapRendererView.setMapLayerProvider(OBF_RASTER_LAYER, obfMapRasterLayerProvider);
}
// Symbols
if (obfMapSymbolsProvider != null) {
mapRendererView.addSymbolsProvider(obfMapSymbolsProvider);
} }
} }
@ -144,231 +261,4 @@ public class MapRendererContext {
} }
/**
* Set display density factor and update context (if needed)
* @param displayDensityFactor New display density factor
*/
public void setDisplayDensityFactor(float displayDensityFactor) {
this.displayDensityFactor = displayDensityFactor;
referenceTileSizef = 256.0f * displayDensityFactor;
rasterTileSize = Integer.highestOneBit((int) referenceTileSizef - 1) * 2;
if (mapRendererView instanceof AtlasMapRendererView)
((AtlasMapRendererView) mapRendererView)
.setReferenceTileSizeOnScreenInPixels(referenceTileSizef);
if (mapPresentationEnvironment != null)
updateMapPresentationEnvironment();
}
/**
* Update map presentation environment and everything that depends on it
*/
private void updateMapPresentationEnvironment() {
// Create new map presentation environment
String langId = app.getSettings().MAP_PREFERRED_LOCALE.get();
// TODO make setting
LanguagePreference langPref = LanguagePreference.LocalizedOrNative;
String rendName = app.getSettings().RENDERER.get();
if(rendName.length() == 0 || rendName.equals(RendererRegistry.DEFAULT_RENDER)) {
rendName = "default";
}
if(!mapStyles.containsKey(rendName)) {
mapStyles.put(rendName, mapStylesCollection.getResolvedStyleByName(rendName));
}
ResolvedMapStyle mapStyle = mapStyles.get(rendName);
CachedMapPresentation pres = new CachedMapPresentation(langId, langPref, mapStyle, displayDensityFactor);
if (this.presentationObjectParams == null
|| !this.presentationObjectParams.equalsFields(pres)) {
this.presentationObjectParams = pres;
mapPresentationEnvironment = new MapPresentationEnvironment(
mapStyle, displayDensityFactor, langId, langPref);
}
// Apply map style settings
OsmandSettings prefs = app.getSettings();
RenderingRulesStorage storage = app.getRendererRegistry()
.getCurrentSelectedRenderer();
Map<String, String> props = new HashMap<String, String>();
for (RenderingRuleProperty customProp : storage.PROPS.getCustomRules()) {
if (customProp.isBoolean()) {
CommonPreference<Boolean> pref = prefs
.getCustomRenderBooleanProperty(customProp
.getAttrName());
props.put(customProp.getAttrName(), pref.get() + "");
} else {
CommonPreference<String> settings = prefs
.getCustomRenderProperty(customProp.getAttrName());
String res = settings.get();
if (!Algorithms.isEmpty(res)) {
props.put(customProp.getAttrName(), res);
}
}
}
QStringStringHash convertedStyleSettings = new QStringStringHash();
for (Iterator<Map.Entry<String, String>> itSetting = props.entrySet()
.iterator(); itSetting.hasNext();) {
Map.Entry<String, String> setting = itSetting.next();
convertedStyleSettings.set(setting.getKey(), setting.getValue());
}
if(nightMode) {
convertedStyleSettings.set("nightMode", "true");
}
mapPresentationEnvironment.setSettings(convertedStyleSettings);
// Update all dependencies
if (mapPrimitiviser != null) {
updateMapPrimitiviser();
}
}
/**
* Reference to map primitiviser (if used)
*/
private MapPrimitiviser mapPrimitiviser;
/**
* Update map primitiviser and everything that depends on it
*/
private void updateMapPrimitiviser() {
// Create new map primitiviser
mapPrimitiviser = new MapPrimitiviser(mapPresentationEnvironment);
// Update all dependencies
if (mapPrimitivesProvider != null)
updateMapPrimitivesProvider();
}
/**
* Reference to OBF map objects provider (if used)
*/
private ObfMapObjectsProvider _obfMapObjectsProvider;
/**
* Update OBF map objects provider and everything that depends on it
*/
private void updateObfMapObjectsProvider() {
_obfMapObjectsProvider = new ObfMapObjectsProvider(
obfsCollection);
// Update all dependencies
if (mapPrimitivesProvider != null)
updateMapPrimitivesProvider();
}
/**
* Reference to map primitives provider (if used)
*/
private MapPrimitivesProvider mapPrimitivesProvider;
/**
* Update map primitives provider and everything that depends on it
*/
private void updateMapPrimitivesProvider() {
// Create new map primitives provider
mapPrimitivesProvider = new MapPrimitivesProvider(
_obfMapObjectsProvider,
mapPrimitiviser,
rasterTileSize);
// Update all dependencies
if (obfMapRasterLayerProvider != null)
updateObfMapRasterLayerProvider();
if (obfMapSymbolsProvider != null)
updateObfMapSymbolsProvider();
}
/**
* Reference to OBF map raster layer provider (if used)
*/
private IRasterMapLayerProvider obfMapRasterLayerProvider;
/**
* Index of OBF map raster layer in bound map renderer view (if set)
*/
private Integer obfMapRasterLayer;
private boolean nightMode;
/**
* Update OBF map raster layer provider and everything that depends on it
*/
private void updateObfMapRasterLayerProvider() {
// Create new OBF map raster layer provider
obfMapRasterLayerProvider = new MapRasterLayerProvider_Software(
mapPrimitivesProvider);
// In case there's bound view and configured layer, perform setup
if(mapRendererView != null && obfMapRasterLayer != null)
mapRendererView.setMapLayerProvider(obfMapRasterLayer, obfMapRasterLayerProvider);
}
/**
* Update OBF map symbols provider and everything that depends on it
*/
private void updateObfMapSymbolsProvider() {
// If there's current provider and bound view, remove it
if (obfMapSymbolsProvider != null && mapRendererView != null)
mapRendererView.removeSymbolsProvider(obfMapSymbolsProvider);
// Create new OBF map symbols provider
obfMapSymbolsProvider = new MapObjectsSymbolsProvider(
mapPrimitivesProvider,
referenceTileSizef);
// If there's bound view, add new provider
if (mapRendererView != null)
mapRendererView.addSymbolsProvider(obfMapSymbolsProvider);
}
/**
* Apply current context to view
*/
private void apply() {
if (mapRendererView instanceof AtlasMapRendererView)
((AtlasMapRendererView)mapRendererView).setReferenceTileSizeOnScreenInPixels(referenceTileSizef);
// Layers
if (obfMapRasterLayer != null && obfMapRasterLayerProvider != null)
mapRendererView.setMapLayerProvider(obfMapRasterLayer, obfMapRasterLayerProvider);
// Symbols
if (obfMapSymbolsProvider != null)
mapRendererView.addSymbolsProvider(obfMapSymbolsProvider);
}
/**
* Setup OBF map on layer 0 with symbols
* @param obfsCollection OBFs collection
*/
public void setupObfMap(MapStylesCollection mapStylesCollection, IObfsCollection obfsCollection) {
this.obfsCollection = obfsCollection;
this.mapStylesCollection = mapStylesCollection;
this.obfMapRasterLayer = OBF_RASTER_LAYER;
updateMapPresentationEnvironment();
updateMapPrimitiviser();
updateMapPrimitivesProvider();
updateObfMapObjectsProvider();
updateObfMapRasterLayerProvider();
updateObfMapSymbolsProvider();
}
public void updateMapSettings() {
if (mapPresentationEnvironment != null) {
updateMapPresentationEnvironment();
}
}
public boolean isNightMode() {
return nightMode;
}
public void setNightMode(boolean nightMode) {
this.nightMode = nightMode;
updateMapSettings();
}
} }

View file

@ -11,6 +11,7 @@ import java.util.Random;
import net.osmand.access.AccessibleAlertBuilder; import net.osmand.access.AccessibleAlertBuilder;
import net.osmand.plus.OsmAndAppCustomization; import net.osmand.plus.OsmAndAppCustomization;
import net.osmand.plus.OsmAndLocationProvider;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.Version; import net.osmand.plus.Version;
@ -74,6 +75,7 @@ public class DashboardActivity extends BaseDownloadActivity {
private static final int SETTINGS_ID = 1; private static final int SETTINGS_ID = 1;
private static final int EXIT_ID = 2; private static final int EXIT_ID = 2;
private ProgressDialog startProgressDialog; private ProgressDialog startProgressDialog;
private OsmAndLocationProvider lp;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -93,6 +95,7 @@ public class DashboardActivity extends BaseDownloadActivity {
return; return;
} }
setContentView(R.layout.dashboard); setContentView(R.layout.dashboard);
lp = getMyApplication().getLocationProvider();
final String textVersion = Version.getFullVersion(getMyApplication()); final String textVersion = Version.getFullVersion(getMyApplication());
getSupportActionBar().setTitle(textVersion); getSupportActionBar().setTitle(textVersion);
@ -103,6 +106,7 @@ public class DashboardActivity extends BaseDownloadActivity {
setupContributionVersion(); setupContributionVersion();
addFragments(); addFragments();
initApp(this, getMyApplication()); initApp(this, getMyApplication());
} }
@Override @Override

View file

@ -59,8 +59,6 @@ import android.widget.Toast;
*/ */
public class MapActivityLayers { public class MapActivityLayers {
private final MapActivity activity; private final MapActivity activity;
// the order of layer should be preserved ! when you are inserting new layer // the order of layer should be preserved ! when you are inserting new layer

View file

@ -11,4 +11,5 @@ public class DashBaseFragment extends SherlockFragment {
public OsmandApplication getMyApplication(){ public OsmandApplication getMyApplication(){
return (OsmandApplication) getActivity().getApplication(); return (OsmandApplication) getActivity().getApplication();
} }
} }

View file

@ -1,23 +0,0 @@
package net.osmand.plus.render;
import net.osmand.NativeLibrary;
/**
* Created by Denis on 02.10.2014.
*/
public class NativeCppLibrary extends NativeLibrary {
public NativeCppLibrary(boolean newLibrary) {
super(newLibrary);
}
public static void loadLibrary(String name) {
try {
System.out.println("Loading " + name);
System.loadLibrary(name);
} catch( UnsatisfiedLinkError e ) {
System.err.println("Failed to load '"+name + "':" + e);
throw e;
}
}
}

View file

@ -55,16 +55,14 @@ public class NativeCoreContext {
WindowManager mgr = (WindowManager)app.getSystemService(Context.WINDOW_SERVICE); WindowManager mgr = (WindowManager)app.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics(); DisplayMetrics dm = new DisplayMetrics();
mgr.getDefaultDisplay().getMetrics(dm); mgr.getDefaultDisplay().getMetrics(dm);
float displayDensityFactor = (float) Math.pow(2, (app.getSettings().getSettingsZoomScale() + Math.sqrt(Math.max(0, dm.density - 1)))); float density = Math.max(0, dm.density - 1);
ObfsCollection obfsCollection = new ObfsCollection(); ObfsCollection obfsCollection = new ObfsCollection();
obfsCollection.addDirectory(directory.getAbsolutePath(), false); obfsCollection.addDirectory(directory.getAbsolutePath(), false);
MapStylesCollection mapStylesCollection = setupMapStyleCollection(app); MapStylesCollection mapStylesCollection = setupMapStyleCollection(app);
mapRendererContext = new MapRendererContext(app); mapRendererContext = new MapRendererContext(app, density);
mapRendererContext.setDisplayDensityFactor(displayDensityFactor);
mapRendererContext.setupObfMap(mapStylesCollection, obfsCollection); mapRendererContext.setupObfMap(mapStylesCollection, obfsCollection);
init = true; init = true;
} }
} }