[android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread tatebn
By the way, this code is located in a subclassed WebView. On Jan 18, 9:53 am, tatebn brandonnt...@gmail.com wrote: I'm trying to evaluate javascript with a semaphore lock.  I need to query some web content about which context menus to show and need it to be synchronous.  When I try to acquire

Re: [android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread Daniel Drozdzewski
Your semaphore allows ZERO passes through, in which case you have to release first. Either change the parameter to Semaphore constructor or release before calling Semaphore.acquire() Daniel On 18 January 2012 15:00, tatebn brandonnt...@gmail.com wrote: By the way, this code is located in a

[android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread tatebn
I need the semaphore to hold there so that the return doesn't happen until the value is sent back from javascript. How do I get the web view to continue evaluating the javascript in the background? On Jan 18, 10:05 am, Daniel Drozdzewski daniel.drozdzew...@gmail.com wrote: Your semaphore

Re: [android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread Daniel Drozdzewski
Why can't you just let webview evaluate the JavaScript? On 18 January 2012 15:18, tatebn brandonnt...@gmail.com wrote: I need the semaphore to hold there so that the return doesn't happen until the value is sent back from javascript.  How do I get the web view to continue evaluating the

[android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread tatebn
WebView doesn't evaluate the JavaScript synchronously. I need an immediate return not a call to a separate interface method. The only method I've found of anyone evaluating javascript synchronously is using this semaphore method, but I can't get the javascript to continue evaluating when the

[android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread Streets Of Boston
We use a countdown latch instead: private CountDownLatch latch = null; public void run(String javaScript, int waitInMilliSecs) { latch = new CountDownLatch(1); runJS(javaScript); // this method just runs the given javascript in the WebView. try { latch.await(waitInMilliSecs,

[android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread Streets Of Boston
We use a countdown latch instead: String returnValue; private CountDownLatch latch = null; public String run(String javaScript, int waitInMilliSecs) { latch = new CountDownLatch(1); runJS(javaScript); // this method just runs the given javascript in the WebView. try {

[android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread tatebn
I'm getting the same issue using the CountDownLatch. The WebView can't load the url because of the lock. On Jan 18, 11:01 am, Streets Of Boston flyingdutc...@gmail.com wrote: We use a countdown latch instead: String returnValue; private CountDownLatch latch = null; public String

Re: [android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread Daniel Drozdzewski
Why does your JS has to be synchronous? Simply within JS make body of your page a throbber and only swap it back to its actual content as a last statement in the JS function that you want to process. On 18 January 2012 16:24, tatebn brandonnt...@gmail.com wrote: I'm getting the same issue

[android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread Streets Of Boston
... we use it successfully all over the place. For some reason, it seems that the stack-frame of your call to loadUrl in your code needs to finish before the javascript in 'code' is actually executed. The call to 'loadUrl' should cause the javascript to be executed on a separate thread

[android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread tatebn
I need to put up a context menu in the app when the user selects text in the WebView. The options in that context menu depend on yes or no answers I pull from javascript. The content is local content that is set up to deliver these answers. if(webView.needsOption1()) // Add option1 to

[android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread tatebn
I've tried executing the javascript in an async task, which I thought would fix any threading issues. But I get the same issue. How do you have your threads specified when you run yours? Are you running this from a WebView subclass or from a separate class for you javascript interface? I've

[android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread Streets Of Boston
It's from a separate class (runJS does something like mWebView.loadUrl(javaScriptUrl);) In my sample code, the *run *and *runJS *are usually executed on the main UI thread (that's why there the *await *call has a (short) timeout). The WebView's JavaScript execution's callback to Java (setValue

Re: [android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread Daniel Drozdzewski
You can bind Java code from within your JavaScript by calling *WebView.addJavascriptInterface()*. In other words you could call one method within Java object of your choosing, when user selects A in UI or different method of different object, when user interacts with UI differently. JavaDocs are

[android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread tatebn
My getJSValue() is run on the main thread as well. As far as the latch.await goes, If I run webView.loadUrl() in an asyncTask I get the full time out (10 seconds for testing). If I just run webView.loadUrl() in the current task, there is no time out. The latch.await call instantly comes back,

Re: [android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread Daniel Drozdzewski
It should read that you can bind your Java code so it is accessible from JavaScript. Obviously the biding is defined within your Java code, and JavaScript assumes existence of said binding. Reading through the example should clarify... On 18 January 2012 17:41, Daniel Drozdzewski

[android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread tatebn
Nevermind that. Misread the log. The latch.await times out every time. But the javascript is not evaluated until after the timeout. On Jan 18, 12:21 pm, Streets Of Boston flyingdutc...@gmail.com wrote: It's from a separate class (runJS does something like mWebView.loadUrl(javaScriptUrl);)

[android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread tatebn
Thanks, but I have the javascript to java binding working. My issue is that I need to wait and return the value set via that binding from javascript. But it seems that the loadUrl() method in the web view isn't using it's own thread. So any waiting I'm doing stops the javascript from executing.

[android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread Streets Of Boston
Very odd. The call to loadUrl should just post a message with the Url on the WebViewCore which, in its turn, will run this url (javascript) on its own thread-pool. I would examine your JavaScript as well. Maybe it does something that needs to be run on the same thread on which you call

[android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread tatebn
The javascript is actually generated in the getJSValue method. For this test I'm using 2 + 2 as my expression. So I don't think the actual js is the issue. How do I tell which thread which method call is happening in? On Jan 18, 1:20 pm, Streets Of Boston flyingdutc...@gmail.com wrote: Very

[android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread tatebn
Nevermind. Looks like loadUrl() is being loaded in the main thread. And setValue() is being called in the WebViewCoreThread. On Jan 18, 2:12 pm, tatebn brandonnt...@gmail.com wrote: The javascript is actually generated in the getJSValue method.  For this test I'm using 2 + 2 as my expression.  

[android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread Streets Of Boston
That looks good. Now enable the latch again (call await) and run your app. When your app hangs, debug it: Press 'pause' on your app that will suspend all your threads. Then open/expand the threads so you see each thread's stack-trace. One should be at the 'await' call. Look at other threads

[android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread tatebn
Ok. I got it. And I feel a little bit stupid. I have multiple url schemes I'm using within the app to do things like send analytics, etc. One of those schemes is finishedloading://, which tells me that the page is ready for me to inject javascript. In my shouldOverrideMethod, if

[android-developers] Re: Problem evaluating javascript with a semaphore lock

2012-01-18 Thread Streets Of Boston
Ah... we've all been there. Any programmer/developer can relate to the situation that one missing silly semicolon messing up your code can be overlooked for a long time, followed by a big forehead-slapping. Anywhose, it's quite amazing what info you can get by just examining your