Skip to main content

Internet use on the Pi

A lot of people like about it giving inbuilt add blocker the standard form of Chromium  with all the adds or other issues made by  Google for why it should load very fast for  you ,it kind of reminds me of using the older browsers before it became all about the ads which Google made into big thing in the past or present a like.   I think a problem many people have not worked out yet if you want to make a website attractive  you need balance between to much ads and no ads for things to work well but the Pi takes care of this problem out of the bow by it's starting browser being decided to get around the internet speed you loss from to many ads. You can if you prefer easy install Brave I use it a lot of  the system as well seems to work as well as the starter internet browser you get so kind of what you prefer you may prefer Firefox or something else. Tor works well on it as well so if you want to go down more the privacy mode many options exist and will work...

2015/04/05/Execute-JavaScript-in-Android-without-WebView/



April 16, 2022

The reasons I need do some Javascript code in Andriod based project was because of me running the background thread (in doinbackground part in an Asynctask), I want want to use Webview


Why not WebView


The first problem here is that Webview is heavy which then makes it run slow overall which is bigger problem when you are doing large kind of proejct and you need to add as well a lot of webview related setting which slows things done further when you want to do things like code snippet so my thoughts it’s not worth doing it because of these sort of problems overall.


Second main points I want to make which is UI related webview should be created in a UI thread which will then load Javascript code in the background of the thread then pass the results bac kto the UI it’s workable overall but it’s not safe in my thoughts.


If you can call methods on webview from any other thread than your app UI thread it can cause unexpected results. https://developer.android.com/guide/webapps/migrating#Threads


Last but not final Webview is fragmented after Android 4.4 or level 19 is using Webview based on Chromium style and even oo this is a good thing my mindset is 4.3 so a older version which could give you problems. You should try and code two versions of Webview in related code to just try and make it work. https://www.theengineer.info/2023/02/google-drive-programming-style-and.html


A lot of code should be re-wrote.

JavaScript  Brand computer graphic


Luckily, the good people in Mozilla make a project called Rhino. It is typically embedded into Java applications to provide scripting to end users.


How to use Rhino?


Rhino is a open source project, you can get the source code using this command:


git clone https://github.com/mozilla/rhino.git


Of course, you don’t have to get the source code. If you want to use it in your Android


project. Download Rhino first, unzip it, put the js.jar file under libs folder. It is very small, so you don’t need to worry your apk file will be ridiculously large because of this one external jar.


Here is some simple code to execute JavaScript code.


Object[] params = new Object[] { “javaScriptParam” };


// Every Rhino VM begins with the enter()


// This Context is not Android’s Context


Context rhino = Context.enter();


// Turn off optimization to make Rhino Android compatible


rhino.setOptimizationLevel(-1);


try {


Scriptable scope = rhino.initStandardObjects();


// Note the forth argument is 1, which means the JavaScript source has


// been compressed to only one line using something like YUI


rhino.evaluateString(scope, javaScriptCode, “JavaScript”, 1, null);


// Get the functionName defined in JavaScriptCode


Object obj = scope.get(functionNameInJavaScriptCode, scope);


if (obj instanceof Function) {


Function jsFunction = (Function) obj;


// Call the function with params


Object jsResult = jsFunction.call(rhino, scope, scope, params);


// Parse the jsResult object to a String


String result = Context.toString(jsResult);


}


} finally {


Context.exit();


}


As you can see, it is quite easy. You can dig up more in Rhino document.


Proguard & Obfuscation


You can set up Rhino’s proguard setting by adding this following line into your proguard-android.txt file.


-keep class org.mozilla.javascript.** { *; }


When you run the assembleRelease task, with minifyEnabled true of course, you still can see a lot of warning like these:


Warning: org.mozilla.javascript.tools.shell.JSConsole: can’t find referenced class javax.swing.JMenu


Warning: org.mozilla.javascript.tools.shell.JSConsole: can’t find referenced class javax.swing.JMenu


Warning: org.mozilla.javascript.tools.shell.JSConsole: can’t find referenced class javax.swing.JMenu


Warning: org.mozilla.javascript.tools.shell.JSConsole: can’t find referenced class javax.swing.ButtonGroup


Warning: org.mozilla.javascript.tools.shell.JSConsole: can’t find referenced class javax.swing.JScrollPane


Warning: org.mozilla.javascript.tools.shell.JSConsole: can’t find


referenced class java.awt.event.ActionEvent


Warning: org.mozilla.javascript.tools.shell.JSConsole: can’t find referenced class java.awt.event.ActionEvent


Warning: org.mozilla.javascript.tools.shell.JSConsole$1: can’t find referenced class javax.swing.filechooser.FileFilter


Warning: org.mozilla.javascript.tools.shell.JSConsole$2: can’t find referenced class java.awt.event.WindowAdapter


Warning: org.mozilla.javascript.tools.shell.JSConsole$2: can’t find referenced class java.awt.event.WindowEvent


Warning: org.mozilla.javascript.tools.shell.JSConsole$2: can’t find referenced class java.awt.event.WindowEvent


Warning: org.mozilla.javascript.xml.impl.xmlbeans.LogicalEquality: can’t find referenced class org.apache.xmlbeans.XmlCursor


Warning: org.mozilla.javascript.xml.impl.xmlbeans.LogicalEquality: can’t find referenced class org.apache.xmlbeans.XmlCursor


Warning: org.mozilla.javascript.xml.impl.xmlbeans.LogicalEquality: can’t find referenced class org.apache.xmlbeans.XmlCursor


Luckily, evan with these warnings, the app runs fine. You can ignore them(that’s what I do), or adding this line inot your proguard-android.txt file.


-dontwarn org.mozilla.javascript.**


Comments

Popular posts from this blog

SGIN Laptop 15.6" Display Laptop 12GB RAM 512GB SSD Storage(TF Card 512GB), Celeron, Full Size Keyboard, 2xUSB 3.0, Dual Band WiFi (Black) Brand: SGIN

 Key problems here The mouse can click itself without someone touching it can't be switched of meaning anything you trying to do you can't shut it off meaning you are screwed when the mouse stops working they offered me £50 discount if I received many instead of trying to trick me to give good review make a better product? IT scams at me low quality crap from China made using second hand parts or poorly designed a equipment it stopped working very quick after I  started it and also I noticed it Struble to install Windows new system on the computer to complete it crashed a few times here on the first day I Got it.  https://www.theengineer.info/2024/01/is-windows-11-as-bad-as-so-many-people.html Microphone does not work well and it can work or not work at random. Speaker is super quiet hard to hear anything using it and also  it at random makes a horrible loud sound even when you not doing anything major with the computer real big appointment here. Everything seem to o...

Is Windows 11 as bad as so many people claim?

 First thoughts It loads fast it has a much better display than Windows 10.  I was not a fan of Windows 10 it was overall very unattractive to see or work with overall and they tried so hard to like force the app ideas towards people which did not improve the user experience in anyway overall just make things get slow or not work well overall. Only thing I did not like the idea of being forced to make a email address or use run it seemed pretty overkill to me.  https://www.techradar.com/news/windows-11-home-and-pro The start up system is a bit slow here since so much updating has  get done in order for it to be ready to work. It would be good idea to not force people to start using Edge give other options such as Chrome or Brave or something else would be good idea since Edge is pretty slow running overall and not that good from a speed viewpoint Brave is my favorite browser at the moment for speed improvements and also because it protects your data better than Googl...