One of the activities in my app has a map on it.
I'm using Leaflet as my map and in order to use it I used WebView.
Then, In my code I use:
String Map_HTML = "<html>\n" +
"<head>\n" +
"\n" +
" <title>Quick Start - Leaflet</title>\n" +
"\n" +
" <meta charset=\"utf-8\" />\n" +
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
TONS OF HTML HERE
" color: 'red',\n" +
" fillColor: '#8275FE',\n" +
" fillOpacity: 0.3,\n" +
" weight: '0'\n" +
"}).addTo(mymap);\n" +
"\n" +
"\n" +
"var popup = L.popup();\n" +
"\n" +
"</script>\n" +
"\n" +
"</body>\n" +
"</html>";
WebView MapView = (WebView) findViewById(R.id.wb_map);
WebSettings webSettings = MapView.getSettings();
webSettings.setJavaScriptEnabled(true);
MapView.loadData(Map_HTML, "text/html", null);
In the end of the script I use webSettings.setJavaScriptEnabled(true); which gives a warning saying:
Using setJavaScriptEnabled can introduce XSS vulnerabilities into your application, review carefully.
From what I read, some people got messages from play store that asks them to update the app so it won't have this.
Since my goal to publish the app soon, I was wondering if there is an option to run it without this line?
I tried to remove it but then it just didn't show anything in the WebView.
Thank you
add this line
#SuppressLint("SetJavaScriptEnabled")
before
webSettings.setJavaScriptEnabled(true);
Documentation
Related
My Code:
web.loadData("<html><body> " + result.getHtmltext().replaceAll("<span class=\"stl_23 stl_10\" style=\"word-spacing:0em;\">-------------------------------</span>",
"<img height=\" 500\" width=\"500\" src=" + " \" " + "file:///android_asset/logo_transparent.png" + "\" " + "/>") + "</body></html>", "text/html; charset=UTF-8", null);
But doesn't work:
You can give it a try
String htmlData = "<body>" + "<img src=\"logo_transparent.png\"/></body>";
webView.loadDataWithBaseURL("file:///android_asset/",htmlData , "text/html", "utf-8",null);
with this, it will start picking up images from the assets folder directly.
I have a utility which creates daily reports as Excel sheets containing the file processed details the failed ones and then sends out a mail to users and this code is written in Java.
When the value of the failed files is 0 the mail has a blank sent instead of 0.
This is the sample code
//sample piece of code
String bodyText = "<html>"
+ "<body style =\"font-family: Calibri; font-size:11pt; background-color:white \">"
+ "<table style =\"border:1px solid black;background-color:#DBE5F1;width:100%\">"
+ "<tr style = \"background-color:#DBE5F1 ;text-align-left; font-size:11pt\">"
+ "<br>"
+ "  Hi All,"
+ "</br>"
+ "<br>"
+ statusLine
+ "</br>"
+ " "
+"<br>"
+ "</tr>"
+ "<td style = \"text-align:right;width:10%;border-right:1px solid black\">"
+ caseCreated
+ "</td>"
+ "<td style = \"width:10%;border-right:1px solid black;text-align:right\">"
+ Failure
+ "</td>"
+"</html>";
//and for composing the mail message
message.setContent(bodyText, "text/html");
I would want to send statistical information to my clients showing the number of transactions processed on every terminal or branch. I am using Apache Commons Email to send HTML emails.
I would like to send a pie-chart data like this one on the site.
My java code is basic extracted from.
It goes like:
public void testHtmlEmailPiechart()
throws UnsupportedEncodingException, EmailException, MalformedURLException {
HtmlEmail email = new HtmlEmail();
email.setHostName(emailServer);
email.setSmtpPort(587);
email.setSSLOnConnect(true);
email.setAuthentication(userName, password);
email.setCharset(emailEncoding);
email.addTo(receiver, "Mwesigye John Bosco");
email.setFrom(userName, "Enovate system emailing alert");
email.setSubject("Conkev aml Engine Statistics");
URL url = new URL("https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcROXe8tn1ljtctM53TkLJhLs6gEX56CvL0shvyq1V6wg7tXUDH8KRyVP30");
// URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
String cid2 = email.embed(url, "logo.gif");
email.setHtmlMsg("<html>\n" +
" <head>\n" +
" <script type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"></script>\n" +
" <script type=\"text/javascript\">\n" +
" google.charts.load(\"current\", {packages:[\"corechart\"]});\n" +
" google.charts.setOnLoadCallback(drawChart);\n" +
" function drawChart() {\n" +
" var data = google.visualization.arrayToDataTable([\n" +
" ['Task', 'Hours per Day'],\n" +
" ['Work', 11],\n" +
" ['Eat', 2],\n" +
" ['Commute', 2],\n" +
" ['Watch TV', 2],\n" +
" ['Sleep', 7]\n" +
" ]);\n" +
"\n" +
" var options = {\n" +
" title: 'My Daily Activities',\n" +
" is3D: true,\n" +
" };\n" +
"\n" +
" var chart = new google.visualization.PieChart(document.getElementById('piechart_3d'));\n" +
" chart.draw(data, options);\n" +
" }\n" +
" </script>\n" +
" </head>\n" +
" <body>\n" +
" <div id=\"piechart_3d\" style=\"width: 900px; height: 500px;\">Piechart Data</div>\n" +
" </body>\n" +
"</html>");
email.setTextMsg("Your email client does not support HTML messages");
email.send();
}
My guess is that the JavaScript is not recognized because the code works like sending images,styling fonts and I have sent to my email address some sample mail. I would like your help or recommendation of any material I can read to achieve this as long as am using Java.The processes is automated running in the background so no user interface is involved.
Thanks.
I'm trying to implement a code editor in JavaFx using
https://gist.github.com/jewelsea/1463485
but the issue is that it downloads the CSS/Javascript from the internet to load CodeMirror. Is there a way to download those files and put them so that it can load them locally on Netbeans? I'm experienced with Java but not so much JavaFX or HTML.
I
Please put the CSS/Javascript in the classpath and use the following code:
private final String editingTemplate =
"<!doctype html>" +
"<html>" +
"<head>" +
" <link rel=\"stylesheet\" href=\"codemirror.css\">" +
" <script src=\"codemirror.js\"></script>" +
" <script src=\"clike.js\"></script>" +
"</head>" +
"<body>" +
"<form><textarea id=\"code\" name=\"code\">\n" +
"${code}" +
"</textarea></form>" +
"<script>" +
" var editor = CodeMirror.fromTextArea(document.getElementById(\"code\"), {" +
" lineNumbers: true," +
" matchBrackets: true," +
" mode: \"text/x-java\"" +
" });" +
"</script>" +
"</body>" +
"</html>";
I am just trying to pick the CSS/Javascript from the classpath instead of the internet
I'm showing a google map inside a BrowserField.
This is the relevant code:
private String setUpHtmlString(Coordinates coordinates){
StringBuffer mapString = new StringBuffer();
mapString.append("" +
"<!DOCTYPE html> " +
"<html> " +
" <head> " +
" <meta http-equiv='content-type' content='text/html; charset=UTF-8' /> " +
" <title>Google Maps Multiple Markers</title> " +
" <script src='http://maps.google.com/maps/api/js?sensor=false' type='text/javascript'></script>" +
" </head> " +
" <body> " +
" <div id='map' style='width: 500px; height: 600px;'></div> " +
" <script type='text/javascript'> " +
" var locations = [ ");
for (int i = 0; i < _placesStringArray.length; i++) {
Address address = ((Place)_hashTablePlaces.get(_placesStringArray[i])).getAddress();
mapString.append("['"+address.getDescription()+"', "+address.getLatitude()+", "+address.getLongitude()+", "+i+"]");
if(i<_placesStringArray.length - 1)
mapString.append(",");
}
mapString.append("];");
mapString.append(
" var map = new google.maps.Map(document.getElementById('map'), { " +
" zoom: 15, " +
" center: new google.maps.LatLng(-25.290646, -57.584080), " +
" mapTypeId: google.maps.MapTypeId.ROADMAP }); " +
" var infowindow = new google.maps.InfoWindow(); " +
" var marker, i; " +
" for (i = 0; i < locations.length; i++) { " +
" marker = new google.maps.Marker({ " +
"icon:'https://maps.google.com/mapfiles/kml/shapes/schools_maps.png', "+
" position: new google.maps.LatLng(locations[i][1], locations[i][2]), map: map }); " +
" google.maps.event.addListener(marker, 'click', (function(marker, i) { " +
" return function() { " +
" infowindow.setContent(locations[i][0]); " +
" infowindow.open(map, marker); " +
" } " +
" })(marker, i)); } " +
" </script>" +
" </body>" +
"</html>");
return mapString.toString();
}
As you can see, the icon is pointing to an external url, but how should a write the path to a image file inside the img folder
of my app.
I tried to reference it in many ways, like these:
"icon:'local:///assets/images/marker.png'
"icon:'resources/images/marker.png'
with no success.
Thanks in advance.
I won't advice you to hardcode the html inside the .java files, because the code is not readable and also hard to find for future modifications. Inline javascript is already a bad thing, but putting everything into a string in java code is a step beyond in terms of coupling.
Option #1: the cleanest way
Instead, you can have a resource html file and load it using a "local:///" URL, as the BrowserField demo shows: Display HTML content from a resource in your application
In the same directory you can place javascript and css files, as you would do in a regular static web site. So theoretically you could also place image resource files there and reference them from html or js files. The URLs don't need the full path (e.g.: instead of local:///assets/images/marker.png you would have local:///marker.png).
If you need to insert dynamic content into the html, then you can always insert placeholders into a template html resource file, and then do a string.replace from java code to replace the placeholders with the dynamic fields.
This way you are addressing modifiability and separation of concerns.
Option #2: the dirty hack
I've shown you why your code was dirty, but as so many things in life it can still get worse. You can add a new layer of unmodifiability by encoding your image file to a base64 string, and setting a "data://" url to the image in markup or javascript. It might be acceptable for you if the icon is really small and you know in advance you are not going to change it frequently, but be aware base64 strings can grow really large.
Option #3 (not tested)
This is also kind of a hack. Assuming you can open your image using Class.getResourceAsStream, you could instantiate a ProtocolController in java code, then call setNavigationRequestHandler to set a handler that intercepts the kind of requests you are interested in, and pass the content loaded using Class.getResourceAsStream.
Bonus option: (not tested)
And here's a link to a BB forum post, where a guy shows you can also reference the image using a URL starting with "cod:///".