use of com.codename1.javascript.JavascriptContext in project CodenameOne by codenameone.
the class FileSystemTests method testFileClass.
private void testFileClass() throws Exception {
FileSystemStorage fs = FileSystemStorage.getInstance();
String appRoot = fs.getAppHomePath();
if (!appRoot.endsWith("" + fs.getFileSystemSeparator())) {
appRoot += fs.getFileSystemSeparator();
}
String appRootPath = appRoot.substring(6);
appRootPath = StringUtil.replaceAll(appRootPath, "\\", "/");
while (appRootPath.startsWith("/")) {
appRootPath = appRootPath.substring(1);
}
appRootPath = "/" + appRootPath;
String appRootUrl = StringUtil.replaceAll("file:" + appRootPath, " ", "%20");
assertTrue(appRoot.startsWith("file:/"), "App root doesn't start with file:/");
File hello = new File("hello world.txt");
assertEqual(appRoot + "hello world.txt", hello.getAbsolutePath(), "Incorrect absolute path for file");
assertEqual(appRootUrl + "hello%20world.txt", hello.toURL().toString(), "Incorrect URL.");
String contents = "hello world " + System.currentTimeMillis();
// Let's try writing to the file
try (OutputStream os = fs.openOutputStream(hello.getAbsolutePath())) {
os.write(contents.getBytes("UTF-8"));
}
// Read in the contents via the URL's open stream.
try (InputStream is = hello.toURL().openStream()) {
String readContent = Util.readToString(is);
assertEqual(contents, readContent, "Contents file differed after reading using URL.openStream()");
} catch (IOException fex) {
throw new RuntimeException("Failed to open stream for url " + hello, fex);
}
contents = "<!doctype html><html><head><title>Hello World</title></head><body><div id='hello'>Hello World</div></body></html>";
hello = new File("hello world.html");
try (OutputStream os = hello.toURL().openConnection().getOutputStream()) {
os.write(contents.getBytes("UTF-8"));
}
// Read in the contents via the URL's open stream.
try (InputStream is = hello.toURL().openStream()) {
String readContent = Util.readToString(is);
assertEqual(contents, readContent, "Contents file differed after reading using URL.openStream()");
} catch (IOException fex) {
throw new RuntimeException("Failed to open stream for url " + hello, fex);
}
// TODO: Find out why this test fails.
if (false && BrowserComponent.isNativeBrowserSupported()) {
BrowserComponent bc = new BrowserComponent();
Res res = new Res();
bc.addWebEventListener("onLoad", e -> {
Log.p("URL: " + bc.getURL());
if (!"Hello World".equals(bc.getTitle())) {
res.error = new RuntimeException("Incorrect page title. Should be Hello World but was " + bc.getTitle());
}
JavascriptContext ctx = new JavascriptContext(bc);
Log.p("Inner html: " + ctx.get("document.getElementById('hello').innerHTML"));
synchronized (res) {
res.complete = true;
res.notifyAll();
}
});
bc.setURL(hello.toURL().toString());
while (!res.complete) {
Display.getInstance().invokeAndBlock(() -> {
Util.sleep(100);
});
}
if (res.error != null) {
assertNull(res.error, res.error.getMessage());
}
}
}
use of com.codename1.javascript.JavascriptContext in project codenameone-google-maps by codenameone.
the class MapContainer method initBrowserComponent.
private void initBrowserComponent(String htmlApiKey) {
if (debug) {
Log.e(new RuntimeException("Initializing Browser Component. This stack trace is just for tracking purposes. It is NOT a real exception"));
}
// System.out.println("About to check location");
Location loc = LocationManager.getLocationManager().getLastKnownLocation();
try {
// if (true)return;
// System.out.println("About to load map text");
String str = Util.readToString(Display.getInstance().getResourceAsStream(null, "/com_codename1_googlemaps_MapContainer.html"));
// System.out.println("Map text: "+str);
str = StringUtil.replaceAll(str, "YOUR_API_KEY", htmlApiKey);
// System.out.println("Finished setting API key");
str = StringUtil.replaceAll(str, "//origin = MAPCONTAINER_ORIGIN", "origin = {lat: " + loc.getLatitude() + ", lng: " + loc.getLongitude() + "};");
// System.out.println("Finished setting origin");
internalBrowser.setPage(str, "/");
if (debug) {
Log.e(new RuntimeException("Adding onLoad Listener. This stack trace is just for tracking purposes. It is NOT a real exception"));
}
internalBrowser.addWebEventListener("onLoad", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (debug) {
Log.e(new RuntimeException("Inside onLoad Listener. This stack trace is just for tracking purposes. It is NOT a real exception"));
}
// JavascriptContext ctx = new JavascriptContext(internalBrowser);
// browserBridge.ctx = ctx;
internalBrowser.execute("com_codename1_googlemaps_MapContainer = {}");
internalBrowser.addJSCallback("com_codename1_googlemaps_MapContainer.fireTapEvent = function(x,y){callback.onSuccess(x+','+y)};", new SuccessCallback<BrowserComponent.JSRef>() {
public void onSucess(BrowserComponent.JSRef value) {
String[] parts = Util.split(value.getValue(), ",");
int x = new Double(Double.parseDouble(parts[0])).intValue();
int y = new Double(Double.parseDouble(parts[1])).intValue();
fireTapEvent(x, y);
}
});
internalBrowser.addJSCallback("com_codename1_googlemaps_MapContainer.fireLongPressEvent = function(x,y){callback.onSuccess(x+','+y)};", new SuccessCallback<BrowserComponent.JSRef>() {
public void onSucess(BrowserComponent.JSRef value) {
String[] parts = Util.split(value.getValue(), ",");
int x = new Double(Double.parseDouble(parts[0])).intValue();
int y = new Double(Double.parseDouble(parts[1])).intValue();
fireLongPressEvent(x, y);
}
});
internalBrowser.addJSCallback("com_codename1_googlemaps_MapContainer.fireMapChangeEvent = function(zoom,lat,lon){callback.onSuccess(zoom+','+lat+','+lon)};", new SuccessCallback<BrowserComponent.JSRef>() {
public void onSucess(BrowserComponent.JSRef value) {
String[] parts = Util.split(value.getValue(), ",");
int zoom = Integer.parseInt(parts[0]);
double lat = Double.parseDouble(parts[1]);
double lon = Double.parseDouble(parts[2]);
fireMapListenerEvent(zoom, lat, lon);
}
});
internalBrowser.addJSCallback("com_codename1_googlemaps_MapContainer.fireMarkerEvent = function(id){callback.onSuccess(id)};", new SuccessCallback<BrowserComponent.JSRef>() {
public void onSucess(BrowserComponent.JSRef value) {
fireMarkerEvent(value.getInt());
}
});
internalBrowser.execute("callback.onSuccess(com_codename1_googlemaps_MapContainer_bridge)", new SuccessCallback<BrowserComponent.JSRef>() {
public void onSucess(BrowserComponent.JSRef value) {
if ("null".equals(value.getValue()) || value.getJSType() == BrowserComponent.JSType.UNDEFINED) {
internalBrowser.execute("com_codename1_googlemaps_MapContainer_onReady=function(bridge){callback.onSuccess(bridge)};", new SuccessCallback<BrowserComponent.JSRef>() {
public void onSucess(BrowserComponent.JSRef value) {
browserBridge.ready = true;
browserBridge.ready(null);
}
});
} else {
browserBridge.ready = true;
browserBridge.ready(null);
}
}
});
// /System.out.println("Bridge is ready");
if (debug) {
Log.p("About to fire browserBridge.ready(null) event to kick things off");
}
browserBridge.ready(null);
}
});
return;
} catch (IOException ex) {
ex.printStackTrace();
}
}
Aggregations