OsmAnd/OsmAnd-java/src/net/osmand/NativeLibrary.java

428 lines
13 KiB
Java
Raw Normal View History

package net.osmand;
2016-09-15 10:51:29 +02:00
import gnu.trove.list.array.TIntArrayList;
2014-06-21 02:51:40 +02:00
import java.io.ByteArrayOutputStream;
import java.io.File;
2014-06-21 02:51:40 +02:00
import java.io.FileInputStream;
import java.io.FileOutputStream;
2014-06-21 02:51:40 +02:00
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
2014-06-21 02:51:40 +02:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
2016-09-08 00:27:45 +02:00
import java.util.LinkedHashMap;
import java.util.Map;
import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion;
import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteSubregion;
import net.osmand.binary.RouteDataObject;
2016-09-08 00:27:45 +02:00
import net.osmand.data.MapObject;
import net.osmand.data.QuadRect;
import net.osmand.render.RenderingRuleSearchRequest;
import net.osmand.render.RenderingRulesStorage;
2014-01-22 23:41:43 +01:00
import net.osmand.router.PrecalculatedRouteDirection;
import net.osmand.router.RouteCalculationProgress;
import net.osmand.router.RouteSegmentResult;
import net.osmand.router.RoutingConfiguration;
2014-06-21 02:51:40 +02:00
import net.osmand.util.Algorithms;
2014-02-05 17:57:24 +01:00
import org.apache.commons.logging.Log;
public class NativeLibrary {
2013-08-17 23:33:19 +02:00
2015-03-06 19:37:54 +01:00
public NativeLibrary() {
2013-08-17 23:33:19 +02:00
}
public static class RenderingGenerationResult {
public RenderingGenerationResult(ByteBuffer bitmap) {
bitmapBuffer = bitmap;
}
public final ByteBuffer bitmapBuffer;
}
public static class NativeSearchResult {
public long nativeHandler;
private NativeSearchResult(long nativeHandler) {
this.nativeHandler = nativeHandler;
}
@Override
protected void finalize() throws Throwable {
deleteNativeResult();
super.finalize();
}
public void deleteNativeResult() {
if (nativeHandler != 0) {
deleteSearchResult(nativeHandler);
nativeHandler = 0;
}
}
}
public static class NativeRouteSearchResult {
public long nativeHandler;
public RouteDataObject[] objects;
public RouteSubregion region;
public NativeRouteSearchResult(long nativeHandler, RouteDataObject[] objects) {
this.nativeHandler = nativeHandler;
this.objects = objects;
}
@Override
protected void finalize() throws Throwable {
deleteNativeResult();
super.finalize();
}
public void deleteNativeResult() {
if (nativeHandler != 0) {
deleteRouteSearchResult(nativeHandler);
nativeHandler = 0;
}
}
}
/**
2013-08-17 23:33:19 +02:00
* @param
* - must be null if there is no need to append to previous results returns native handle to results
*/
public NativeSearchResult searchObjectsForRendering(int sleft, int sright, int stop, int sbottom, int zoom,
RenderingRuleSearchRequest request, boolean skipDuplicates, Object objectWithInterruptedField, String msgIfNothingFound) {
int renderRouteDataFile = 0;
if (request.searchRenderingAttribute("showRoadMapsAttribute")) {
renderRouteDataFile = request.getIntPropertyValue(request.ALL.R_ATTR_INT_VALUE);
}
return new NativeSearchResult(searchNativeObjectsForRendering(sleft, sright, stop, sbottom, zoom, request, skipDuplicates,
renderRouteDataFile, objectWithInterruptedField, msgIfNothingFound));
}
public RouteDataObject[] getDataObjects(NativeRouteSearchResult rs, int x31, int y31) {
if (rs.nativeHandler == 0) {
// do not throw exception because it is expected situation
return new RouteDataObject[0];
}
return getRouteDataObjects(rs.region.routeReg, rs.nativeHandler, x31, y31);
}
public boolean initMapFile(String filePath) {
2015-03-06 19:37:54 +01:00
return initBinaryMapFile(filePath);
}
public boolean initCacheMapFile(String filePath) {
return initCacheMapFiles(filePath);
}
public boolean closeMapFile(String filePath) {
return closeBinaryMapFile(filePath);
}
public RouteSegmentResult[] runNativeRouting(int sx31, int sy31, int ex31, int ey31, RoutingConfiguration config,
2014-01-22 23:41:43 +01:00
RouteRegion[] regions, RouteCalculationProgress progress, PrecalculatedRouteDirection precalculatedRouteDirection,
boolean basemap) {
2014-02-05 17:57:24 +01:00
// config.router.printRules(System.out);
return nativeRouting(new int[] { sx31, sy31, ex31, ey31 }, config, config.initialDirection == null ? -360 : config.initialDirection.floatValue(),
2014-01-22 23:41:43 +01:00
regions, progress, precalculatedRouteDirection, basemap);
}
public NativeRouteSearchResult loadRouteRegion(RouteSubregion sub, boolean loadObjects) {
NativeRouteSearchResult lr = loadRoutingData(sub.routeReg, sub.routeReg.getName(), sub.routeReg.getFilePointer(), sub, loadObjects);
if (lr != null && lr.nativeHandler != 0) {
lr.region = sub;
}
return lr;
}
/**/
protected static native NativeRouteSearchResult loadRoutingData(RouteRegion reg, String regName, int regfp, RouteSubregion subreg,
boolean loadObjects);
2016-09-06 00:43:11 +02:00
protected static native void deleteRenderingContextHandle(long handle);
protected static native void deleteRouteSearchResult(long searchResultHandle);
protected static native RouteDataObject[] getRouteDataObjects(RouteRegion reg, long rs, int x31, int y31);
2014-02-05 17:57:24 +01:00
protected static native RouteSegmentResult[] nativeRouting(int[] coordinates, RoutingConfiguration r,
2014-01-22 23:41:43 +01:00
float initDirection, RouteRegion[] regions, RouteCalculationProgress progress, PrecalculatedRouteDirection precalculatedRouteDirection, boolean basemap);
protected static native void deleteSearchResult(long searchResultHandle);
protected static native boolean initBinaryMapFile(String filePath);
protected static native boolean initCacheMapFiles(String filePath);
protected static native boolean closeBinaryMapFile(String filePath);
protected static native void initRenderingRulesStorage(RenderingRulesStorage storage);
protected static native RenderingGenerationResult generateRenderingIndirect(RenderingContext rc, long searchResultHandler,
boolean isTransparent, RenderingRuleSearchRequest render, boolean encodePng);
protected static native long searchNativeObjectsForRendering(int sleft, int sright, int stop, int sbottom, int zoom,
RenderingRuleSearchRequest request, boolean skipDuplicates, int renderRouteDataFile, Object objectWithInterruptedField,
String msgIfNothingFound);
2014-06-21 02:32:49 +02:00
protected static native boolean initFontType(byte[] byteData, String name, boolean bold, boolean italic);
2016-09-08 00:27:45 +02:00
2016-11-28 09:04:07 +01:00
protected static native RenderedObject[] searchRenderedObjects(RenderingContext context, int x, int y, boolean notvisible);
2016-09-08 00:27:45 +02:00
public RenderedObject[] searchRenderedObjectsFromContext(RenderingContext context, int x, int y) {
2016-11-28 09:04:07 +01:00
return searchRenderedObjects(context, x, y, false);
}
public RenderedObject[] searchRenderedObjectsFromContext(RenderingContext context, int x, int y, boolean notvisible) {
return searchRenderedObjects(context, x, y, notvisible);
2016-09-08 00:27:45 +02:00
}
/**/
// Empty native impl
/*
* protected static NativeRouteSearchResult loadRoutingData(RouteRegion reg, String regName, int regfp,RouteSubregion subreg, boolean
* loadObjects) { return null;}
*
* protected static void deleteRouteSearchResult(long searchResultHandle) {}
*
* protected static RouteDataObject[] getRouteDataObjects(RouteRegion reg, long rs, int x31, int y31){return null;}
*
* protected static RouteSegmentResult[] nativeRouting(int[] coordinates, int[] state, String[] keyConfig, String[] valueConfig, float
* initDirection, RouteRegion[] regions, RouteCalculationProgress progress) {return null;}
*
* protected static void deleteSearchResult(long searchResultHandle) {}
*
* protected static boolean initBinaryMapFile(String filePath) {return false;}
*
* protected static boolean initCacheMapFiles(String filePath) {return false;}
*
* protected static boolean closeBinaryMapFile(String filePath) {return false;}
*
* protected static void initRenderingRulesStorage(RenderingRulesStorage storage) {}
*
*
* protected static RenderingGenerationResult generateRenderingIndirect(RenderingContext rc, long searchResultHandler, boolean
* isTransparent, RenderingRuleSearchRequest render, boolean encodePng) { return null; }
*
* protected static long searchNativeObjectsForRendering(int sleft, int sright, int stop, int sbottom, int zoom,
* RenderingRuleSearchRequest request, boolean skipDuplicates, int renderRouteDataFile, Object objectWithInterruptedField, String
* msgIfNothingFound) { return 0; }
*
* public static void testRoutingPing() {}
*
* public static int testNativeRouting(String obfPath, double sLat, double sLon, double eLat, double eLon) {return 0;} /*
*/
private static final Log log = PlatformUtil.getLog(NativeLibrary.class);
2013-08-17 23:33:19 +02:00
public static boolean loadNewLib(String path) {
return load("OsmAndJNI", path);
}
2013-08-07 19:12:01 +02:00
public static boolean loadOldLib(String path) {
boolean b = true;
b &= load("osmand", path);
return b;
}
public static boolean load(String libBaseName, String path) {
// look for a pre-installed library
if (path != null) {
try {
2013-05-27 10:07:15 +02:00
System.load(path + "/" + System.mapLibraryName(libBaseName));
return true;
} catch (UnsatisfiedLinkError e) {
log.error(e);
} // fall through
}
// guess what a bundled library would be called
String osname = System.getProperty("os.name").toLowerCase();
String osarch = System.getProperty("os.arch");
if (osname.startsWith("mac os")) {
osname = "mac";
osarch = "universal";
}
if (osname.startsWith("windows"))
osname = "win";
if (osname.startsWith("sunos"))
osname = "solaris";
if (osarch.startsWith("i") && osarch.endsWith("86"))
osarch = "x86";
String libname = libBaseName + "-" + osname + '-' + osarch + ".lib";
// try a bundled library
try {
ClassLoader cl = NativeLibrary.class.getClassLoader();
InputStream in = cl.getResourceAsStream( libname);
if (in == null) {
log.error("libname: " + libname + " not found");
return false;
}
File tmplib = File.createTempFile(libBaseName + "-", ".lib");
tmplib.deleteOnExit();
OutputStream out = new FileOutputStream(tmplib);
byte[] buf = new byte[1024];
for (int len; (len = in.read(buf)) != -1;)
out.write(buf, 0, len);
in.close();
out.close();
System.load(tmplib.getAbsolutePath());
return true;
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage(), e);
} catch (UnsatisfiedLinkError e) {
log.error(e.getMessage(), e);
} // fall through
return false;
}
2013-08-17 23:33:19 +02:00
2014-06-21 02:51:40 +02:00
/**
* Compares two {@code int} values.
* @return 0 if lhs = rhs, less than 0 if lhs < rhs, and greater than 0 if lhs > rhs.
* @since 1.7
*/
public static int ccmp(int lhs, int rhs) {
return lhs < rhs ? -1 : (lhs == rhs ? 0 : 1);
}
2015-09-09 18:22:47 +02:00
public void loadFontData(File dr) {
File[] lf = dr.listFiles();
if (lf == null) {
2014-06-21 02:51:40 +02:00
System.err.println("No fonts loaded from " + dr.getAbsolutePath());
return;
}
2015-09-09 18:22:47 +02:00
ArrayList<File> lst = new ArrayList<File>(Arrays.asList(lf));
2014-06-21 02:51:40 +02:00
Collections.sort(lst, new Comparator<File>() {
@Override
public int compare(File arg0, File arg1) {
return ccmp(order(arg0), order(arg1));
}
private int order(File a) {
final String nm = a.getName().toLowerCase();
if(nm.contains("OpenSans".toLowerCase())) {
if(nm.contains("Regular".toLowerCase())) {
return 0;
}
return 1;
}
if(nm.contains("Fallback".toLowerCase())) {
return 3;
}
if(nm.contains("MTLmr3m".toLowerCase())) {
return 5;
}
return 2;
}
});
for(File f : lst) {
final String name = f.getName();
2017-03-20 13:16:49 +01:00
if(!name.endsWith(".ttf") && !name.endsWith(".otf")) {
2014-06-21 02:51:40 +02:00
continue;
}
try {
ByteArrayOutputStream ous = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream(f);
Algorithms.streamCopy(fis, ous);
fis.close();
initFontType(ous.toByteArray(), name.substring(0, name.length() - 4), name.toLowerCase().contains("bold"),
name.toLowerCase().contains("italic"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
2016-09-08 00:27:45 +02:00
public static class RenderedObject extends MapObject {
private Map<String, String> tags = new LinkedHashMap<>();
private QuadRect bbox = new QuadRect();
2016-09-15 10:54:07 +02:00
private TIntArrayList x = new TIntArrayList();
private TIntArrayList y = new TIntArrayList();
2016-09-19 10:11:07 +02:00
private String iconRes;
2016-11-28 09:04:07 +01:00
private int order;
private boolean visible;
2016-09-08 00:27:45 +02:00
public Map<String, String> getTags() {
return tags;
}
public boolean isText() {
return !getName().isEmpty();
}
2016-11-28 09:04:07 +01:00
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
2016-09-15 10:51:29 +02:00
public void addLocation(int x, int y) {
this.x.add(x);
this.y.add(y);
}
public TIntArrayList getX() {
return x;
}
2016-09-19 10:11:07 +02:00
public String getIconRes() {
return iconRes;
}
public void setIconRes(String iconRes) {
this.iconRes = iconRes;
}
2016-11-28 09:04:07 +01:00
public void setVisible(boolean visible) {
this.visible = visible;
}
public boolean isVisible() {
return visible;
}
2016-09-15 10:51:29 +02:00
public TIntArrayList getY() {
return y;
2016-09-08 00:27:45 +02:00
}
public void setBbox(int left, int top, int right, int bottom) {
bbox = new QuadRect(left, top, right, bottom);
}
public QuadRect getBbox() {
return bbox;
}
public void setNativeId(long id) {
setId(id);
}
public void putTag(String t, String v) {
tags.put(t, v);
}
}
2014-06-21 02:51:40 +02:00
}