Copy styles to app dir (it speeds up rendering)

This commit is contained in:
Victor Shcherb 2015-04-15 18:30:37 +02:00
parent e83021c57f
commit eab650987d
4 changed files with 79 additions and 68 deletions

View file

@ -193,39 +193,6 @@ public class AppInitializer implements IProgress {
} }
// TODO
private void applicationInstalledFirstTime(final Activity ctx) {
final OsmandApplication app = (OsmandApplication)ctx.getApplication();
boolean netOsmandWasInstalled = false;
try {
ApplicationInfo applicationInfo = ctx.getPackageManager().getApplicationInfo("net.osmand", PackageManager.GET_META_DATA);
netOsmandWasInstalled = applicationInfo != null && !Version.isFreeVersion(app);
} catch (PackageManager.NameNotFoundException e) {
netOsmandWasInstalled = false;
}
if (netOsmandWasInstalled) {
// Builder builder = new AccessibleAlertBuilder(this);
// builder.setMessage(R.string.osmand_net_previously_installed);
// builder.setPositiveButton(R.string.shared_string_ok, null);
// builder.show();
} else {
AlertDialog.Builder builder = new AccessibleAlertBuilder(ctx);
builder.setMessage(R.string.first_time_msg);
builder.setPositiveButton(R.string.first_time_download, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ctx.startActivity(new Intent(ctx, app.getAppCustomization().getDownloadIndexActivity()));
}
});
builder.setNegativeButton(R.string.first_time_continue, null);
builder.show();
}
}
private void indexRegionsBoundaries(List<String> warnings) { private void indexRegionsBoundaries(List<String> warnings) {
try { try {
File file = app.getAppPath("regions.ocbf"); File file = app.getAppPath("regions.ocbf");
@ -299,7 +266,7 @@ public class AppInitializer implements IProgress {
String clang = "".equals(lang) ? new Locale(lang).getLanguage() : lang; String clang = "".equals(lang) ? new Locale(lang).getLanguage() : lang;
app.regions.setLocale(clang); app.regions.setLocale(clang);
app.poiFilters = startupInit(new PoiFiltersHelper(app), PoiFiltersHelper.class); app.poiFilters = startupInit(new PoiFiltersHelper(app), PoiFiltersHelper.class);
app.rendererRegistry = startupInit(new RendererRegistry(), RendererRegistry.class); app.rendererRegistry = startupInit(new RendererRegistry(app), RendererRegistry.class);
app.targetPointsHelper = startupInit(new TargetPointsHelper(app), TargetPointsHelper.class); app.targetPointsHelper = startupInit(new TargetPointsHelper(app), TargetPointsHelper.class);
} }
@ -380,7 +347,7 @@ public class AppInitializer implements IProgress {
notifyEvent(InitEvents.POI_TYPES_INITIALIZED); notifyEvent(InitEvents.POI_TYPES_INITIALIZED);
app.resourceManager.reloadIndexesOnStart(this, warnings); app.resourceManager.reloadIndexesOnStart(this, warnings);
app.resourceManager.initRenderers(this); app.getRendererRegistry().initRenderers(this);
notifyEvent(InitEvents.INIT_RENDERERS); notifyEvent(InitEvents.INIT_RENDERERS);
// native depends on renderers // native depends on renderers
initNativeCore(); initNativeCore();

View file

@ -216,6 +216,7 @@ public class MapRenderRepositories {
if (searchRequest != null) { if (searchRequest != null) {
searchRequest.setInterrupted(true); searchRequest.setInterrupted(true);
} }
log.info("Interrupt rendering map");
} }
private boolean checkWhetherInterrupted() { private boolean checkWhetherInterrupted() {

View file

@ -3,6 +3,7 @@ package net.osmand.plus.render;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Collection; import java.util.Collection;
@ -10,9 +11,13 @@ import java.util.LinkedHashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Map; import java.util.Map;
import net.osmand.IProgress;
import net.osmand.IndexConstants;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication;
import net.osmand.render.RenderingRulesStorage; import net.osmand.render.RenderingRulesStorage;
import net.osmand.render.RenderingRulesStorage.RenderingRulesStorageResolver; import net.osmand.render.RenderingRulesStorage.RenderingRulesStorageResolver;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
@ -41,8 +46,11 @@ public class RendererRegistry {
} }
private IRendererLoadedEventListener rendererLoadedEventListener; private IRendererLoadedEventListener rendererLoadedEventListener;
private OsmandApplication app;
public RendererRegistry(){ public RendererRegistry(OsmandApplication app){
this.app = app;
internalRenderers.put(DEFAULT_RENDER, "default.render.xml"); internalRenderers.put(DEFAULT_RENDER, "default.render.xml");
internalRenderers.put(TOURING_VIEW, "Touring-view_(more-contrast-and-details)" +".render.xml"); internalRenderers.put(TOURING_VIEW, "Touring-view_(more-contrast-and-details)" +".render.xml");
internalRenderers.put("UniRS", "UniRS" + ".render.xml"); internalRenderers.put("UniRS", "UniRS" + ".render.xml");
@ -150,12 +158,66 @@ public class RendererRegistry {
if(externalRenderers.containsKey(name)){ if(externalRenderers.containsKey(name)){
is = new FileInputStream(externalRenderers.get(name)); is = new FileInputStream(externalRenderers.get(name));
} else if(internalRenderers.containsKey(name)){ } else if(internalRenderers.containsKey(name)){
is = RenderingRulesStorage.class.getResourceAsStream(internalRenderers.get(name)); File fl = getFileForInternalStyle(name);
if(fl.exists()) {
is = new FileInputStream(fl);
} else {
copyFileForInternalStyle(name);
is = RenderingRulesStorage.class.getResourceAsStream(internalRenderers.get(name));
}
} else { } else {
throw new IllegalArgumentException("Not found " + name); //$NON-NLS-1$ throw new IllegalArgumentException("Not found " + name); //$NON-NLS-1$
} }
return is; return is;
} }
public void copyFileForInternalStyle(String name) {
try {
FileOutputStream fout = new FileOutputStream(getFileForInternalStyle(name));
Algorithms.streamCopy(RenderingRulesStorage.class.getResourceAsStream(internalRenderers.get(name)),
fout);
fout.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
public Map<String, String> getInternalRenderers() {
return internalRenderers;
}
public File getFileForInternalStyle(String name) {
if(internalRenderers.containsKey(name)) {
return new File(app.getAppPath(IndexConstants.RENDERERS_DIR), "style.render.xml");
}
File fl = new File(app.getAppPath(IndexConstants.RENDERERS_DIR), internalRenderers.get(name));
return fl;
}
public void initRenderers(IProgress progress) {
File file = app.getAppPath(IndexConstants.RENDERERS_DIR);
file.mkdirs();
Map<String, File> externalRenderers = new LinkedHashMap<String, File>();
if (file.exists() && file.canRead()) {
File[] lf = file.listFiles();
if (lf != null) {
for (File f : lf) {
if (f != null && f.getName().endsWith(IndexConstants.RENDERER_INDEX_EXT)) {
String name = f.getName().substring(0, f.getName().length() - IndexConstants.RENDERER_INDEX_EXT.length());
externalRenderers.put(name, f);
}
}
}
}
setExternalRenderers(externalRenderers);
String r = app.getSettings().RENDERER.get();
if(r != null){
RenderingRulesStorage obj = getRenderer(r);
if(obj != null){
setCurrentSelectedRender(obj);
}
}
}
public void setExternalRenderers(Map<String, File> externalRenderers) { public void setExternalRenderers(Map<String, File> externalRenderers) {

View file

@ -51,7 +51,6 @@ import net.osmand.plus.resources.AsyncLoadingThread.TileLoadDownloadRequest;
import net.osmand.plus.resources.AsyncLoadingThread.TransportLoadRequest; import net.osmand.plus.resources.AsyncLoadingThread.TransportLoadRequest;
import net.osmand.plus.srtmplugin.SRTMPlugin; import net.osmand.plus.srtmplugin.SRTMPlugin;
import net.osmand.plus.views.OsmandMapLayer.DrawSettings; import net.osmand.plus.views.OsmandMapLayer.DrawSettings;
import net.osmand.render.RenderingRulesStorage;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils; import net.osmand.util.MapUtils;
@ -465,6 +464,12 @@ public class ResourceManager {
context.getSettings().PREVIOUS_INSTALLED_VERSION.set(fv); context.getSettings().PREVIOUS_INSTALLED_VERSION.set(fv);
copyRegionsBoundaries(); copyRegionsBoundaries();
copyPoiTypes(); copyPoiTypes();
for (String internalStyle : context.getRendererRegistry().getInternalRenderers().keySet()) {
File fl = context.getRendererRegistry().getFileForInternalStyle(internalStyle);
if (fl.exists()) {
context.getRendererRegistry().copyFileForInternalStyle(internalStyle);
}
}
} catch (SQLiteException e) { } catch (SQLiteException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
} catch (IOException e) { } catch (IOException e) {
@ -481,8 +486,9 @@ public class ResourceManager {
try { try {
File file = context.getAppPath("regions.ocbf"); File file = context.getAppPath("regions.ocbf");
if (file != null) { if (file != null) {
Algorithms.streamCopy(OsmandRegions.class.getResourceAsStream("regions.ocbf"), new FileOutputStream( FileOutputStream fout = new FileOutputStream(file);
file)); Algorithms.streamCopy(OsmandRegions.class.getResourceAsStream("regions.ocbf"), fout);
fout.close();
} }
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
@ -493,8 +499,9 @@ public class ResourceManager {
try { try {
File file = context.getAppPath("poi_types.xml"); File file = context.getAppPath("poi_types.xml");
if (file != null) { if (file != null) {
Algorithms.streamCopy(MapPoiTypes.class.getResourceAsStream("poi_types.xml"), new FileOutputStream( FileOutputStream fout = new FileOutputStream(file);
file)); Algorithms.streamCopy(MapPoiTypes.class.getResourceAsStream("poi_types.xml"), fout);
fout.close();
} }
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
@ -556,7 +563,6 @@ public class ResourceManager {
isBundledAssetsXml.close(); isBundledAssetsXml.close();
} }
//TODO consider some other place for this method?
public static void copyAssets(AssetManager assetManager, String assetName, File file) throws IOException { public static void copyAssets(AssetManager assetManager, String assetName, File file) throws IOException {
if(file.exists()){ if(file.exists()){
Algorithms.removeAllFiles(file); Algorithms.removeAllFiles(file);
@ -569,31 +575,6 @@ public class ResourceManager {
Algorithms.closeStream(is); Algorithms.closeStream(is);
} }
public void initRenderers(IProgress progress) {
File file = context.getAppPath(IndexConstants.RENDERERS_DIR);
file.mkdirs();
Map<String, File> externalRenderers = new LinkedHashMap<String, File>();
if (file.exists() && file.canRead()) {
File[] lf = file.listFiles();
if (lf != null) {
for (File f : lf) {
if (f != null && f.getName().endsWith(IndexConstants.RENDERER_INDEX_EXT)) {
String name = f.getName().substring(0, f.getName().length() - IndexConstants.RENDERER_INDEX_EXT.length());
externalRenderers.put(name, f);
}
}
}
}
context.getRendererRegistry().setExternalRenderers(externalRenderers);
String r = context.getSettings().RENDERER.get();
if(r != null){
RenderingRulesStorage obj = context.getRendererRegistry().getRenderer(r);
if(obj != null){
context.getRendererRegistry().setCurrentSelectedRender(obj);
}
}
}
private List<File> collectFiles(File dir, String ext, List<File> files) { private List<File> collectFiles(File dir, String ext, List<File> files) {
if(dir.exists() && dir.canRead()) { if(dir.exists() && dir.canRead()) {
File[] lf = dir.listFiles(); File[] lf = dir.listFiles();