1616
1717package com .runrev .android .libraries ;
1818
19+ import com .runrev .android .Engine ;
20+ import com .runrev .android .nativecontrol .NativeControlModule ;
21+
1922import android .content .*;
2023import android .content .pm .*;
2124import android .graphics .*;
@@ -66,21 +69,24 @@ public boolean shouldOverrideUrlLoading(WebView p_view, String p_url)
6669 public void onPageStarted (WebView view , String url , Bitmap favicon )
6770 {
6871 //Log.i(TAG, "onPageStarted() - " + url);
69- doStartedLoading (toAPKPath (url ));
70- m_module .getEngine ().wakeEngineThread ();
72+ //doStartedLoading(toAPKPath(url));
73+ doStartedLoading (url );
74+ wakeEngineThread ();
7175 }
7276
7377 public void onPageFinished (WebView view , String url )
7478 {
7579 //Log.i(TAG, "onPageFinished() - " + url);
76- doFinishedLoading (toAPKPath (url ));
77- m_module .getEngine ().wakeEngineThread ();
80+ //doFinishedLoading(toAPKPath(url));
81+ doFinishedLoading (url );
82+ wakeEngineThread ();
7883 }
7984
8085 public void onReceivedError (WebView view , int errorCode , String description , String failingUrl )
8186 {
82- doLoadingError (toAPKPath (failingUrl ), description );
83- m_module .getEngine ().wakeEngineThread ();
87+ //doLoadingError(toAPKPath(failingUrl), description);
88+ doLoadingError (failingUrl , description );
89+ wakeEngineThread ();
8490 }
8591 });
8692
@@ -195,7 +201,7 @@ public void onHideCustomView()
195201 getSettings ().setJavaScriptEnabled (true );
196202 getSettings ().setPluginState (WebSettings .PluginState .ON );
197203 getSettings ().setBuiltInZoomControls (true );
198- addJavascriptInterface (new JSInterface (), "revjs " );
204+ addJavascriptInterface (new JSInterface (), "liveCode " );
199205 }
200206
201207 private static Method mWebView_getOverScrollMode ;
@@ -214,24 +220,132 @@ public void onHideCustomView()
214220 }
215221 }
216222
223+ class JSInterface
224+ {
225+ private String [] m_js_handlers = new String [0 ];
226+
227+ private void setJSHandlers (String [] p_handlers )
228+ {
229+ m_js_handlers = p_handlers ;
230+ }
231+
232+ @ JavascriptInterface
233+ public void invokeHandler (String p_handler , String [] p_args )
234+ {
235+ if (Arrays .asList (m_js_handlers ).contains (p_handler ))
236+ {
237+ doCallJSHandler (p_handler , p_args );
238+ wakeEngineThread ();
239+ }
240+ }
241+
242+ @ JavascriptInterface
243+ public void storeExecuteJavaScriptResult (String p_tag , String p_result )
244+ {
245+ doJSExecutionResult (p_tag , p_result );
246+ wakeEngineThread ();
247+ }
248+ }
249+
250+ /* PROPERTIES */
251+
252+ public void setUrl (String p_url )
253+ {
254+ //p_url = fromAPKPath(p_url);
255+
256+ //loadUrl(p_url);
257+ loadUrl (p_url );
258+ }
259+
260+ //public String getUrl()
261+ //{
262+ // return toAPKPath(super.getUrl());
263+ //}
264+
265+ public boolean getScrollingEnabled ()
266+ {
267+ return m_scrolling_enabled ;
268+ }
269+
270+ public void setScrollingEnabled (boolean p_enabled )
271+ {
272+ if (m_scrolling_enabled == p_enabled )
273+ return ;
274+ m_scrolling_enabled = p_enabled ;
275+ setHorizontalScrollBarEnabled (p_enabled );
276+ setVerticalScrollBarEnabled (p_enabled );
277+ getSettings ().setBuiltInZoomControls (p_enabled );
278+ if (!m_scrolling_enabled )
279+ {
280+ setOnTouchListener (new View .OnTouchListener () {
281+ @ Override
282+ public boolean onTouch (View v , MotionEvent event ) {
283+ return (event .getAction () == MotionEvent .ACTION_MOVE );
284+ }
285+ });
286+ }
287+ else
288+ setOnTouchListener (null );
289+ }
290+
291+ /* ACTIONS */
292+
293+ public void goBack (int p_steps )
294+ {
295+ goBackOrForward (-p_steps );
296+ }
297+
298+ public void goForward (int p_steps )
299+ {
300+ goBackOrForward (p_steps );
301+ }
302+
303+ /*
304+ public void reload()
305+ {
306+ super.reload();
307+ }
308+ */
309+
310+ public void stop ()
311+ {
312+ stopLoading ();
313+ }
314+
315+ public void loadHtml (String p_base_url , String p_html )
316+ {
317+ //loadDataWithBaseURL(fromAPKPath(p_base_url), p_html, "text/html", "utf-8", null);
318+ loadDataWithBaseURL (p_base_url , p_html , "text/html" , "utf-8" , null );
319+ }
320+
321+ public String executeJavaScript (String p_javascript )
322+ {
323+ SecureRandom t_random = new SecureRandom ();
324+ long t_tag = 0 ;
325+ while (t_tag == 0 )
326+ t_tag = t_random .nextLong ();
327+
328+ // Log.i(TAG, "generated tag: " + t_tag);
329+ String t_js = String .format ("javascript:window.liveCode.storeExecuteJavaScriptResult('%d', eval('%s'))" , t_tag , escapeJSString (p_javascript ));
330+ // Log.i(TAG, t_js);
331+ loadUrl (t_js );
332+
333+ return Long .toString (t_tag );
334+ }
335+
336+ /* NATIVE METHODS */
337+
338+ public native void doJSExecutionResult (String tag , String result );
339+ public native void doStartedLoading (String url );
340+ public native void doFinishedLoading (String url );
341+ public native void doLoadingError (String url , String error );
217342 }
218343
219-
220- public BrowserControl (NativeControlModule p_module )
221- {
222- super (p_module );
223- m_scrolling_enabled = true ;
224- }
225-
226- class JSInterface
344+ private static void wakeEngineThread ()
227345 {
228- public void storeResult (String p_tag , String p_result )
229- {
230- doJSExecutionResult (p_tag , p_result );
231- m_module .getEngine ().wakeEngineThread ();
232- }
233- }
234-
346+ Engine .getEngine ().wakeEngineThread ();
347+ }
348+
235349 static final Pattern ACCEPTED_URI_SCHEMA = Pattern .compile (
236350 "(?i)" + // switch on case insensitive matching
237351 "(" + // begin group for schema
@@ -276,16 +390,16 @@ private static boolean useExternalHandler(Context p_context, String p_url)
276390 return false ;
277391 }
278392
279- public boolean handleBackPressed ()
280- {
281- if (m_custom_view_container != null && m_chrome_client != null )
282- {
283- m_chrome_client .onHideCustomView ();
284- return true ;
285- }
286-
287- return false ;
288- }
393+ // public boolean handleBackPressed()
394+ // {
395+ // if (m_custom_view_container != null && m_chrome_client != null)
396+ // {
397+ // m_chrome_client.onHideCustomView();
398+ // return true;
399+ // }
400+ //
401+ // return false;
402+ // }
289403
290404 public static String stripExtraSlashes (String p_path )
291405 {
@@ -302,7 +416,7 @@ public static String stripExtraSlashes(String p_path)
302416 private static final String s_file_url_prefix = "file://" ;
303417 private static final String s_livecode_file_url_prefix = "file:" ;
304418
305- public String toAPKPath (String p_url )
419+ public static String toAPKPath (String p_url )
306420 {
307421 if (!p_url .startsWith (s_file_url_prefix ))
308422 return p_url ;
@@ -312,16 +426,16 @@ public String toAPKPath(String p_url)
312426 if (!t_url .startsWith (s_asset_path ))
313427 return p_url ;
314428
315- String t_package_path = m_module .getEngine ().getPackagePath ();
429+ String t_package_path = Engine .getEngine ().getPackagePath ();
316430 return s_livecode_file_url_prefix + t_package_path + t_url .substring (s_asset_path .length ());
317431 }
318432
319- public String fromAPKPath (String p_url )
433+ public static String fromAPKPath (String p_url )
320434 {
321435 if (!p_url .startsWith (s_livecode_file_url_prefix ))
322436 return p_url ;
323437
324- String t_package_path = m_module .getEngine ().getPackagePath ();
438+ String t_package_path = Engine .getEngine ().getPackagePath ();
325439 String t_url = stripExtraSlashes (p_url .substring (s_livecode_file_url_prefix .length ()));
326440
327441 if (!t_url .startsWith (t_package_path ))
@@ -330,18 +444,7 @@ public String fromAPKPath(String p_url)
330444 return s_file_url_prefix + s_asset_path + t_url .substring (t_package_path .length ());
331445 }
332446
333- public void setUrl (String p_url )
334- {
335- p_url = fromAPKPath (p_url );
336-
337- ((WebView )m_control_view ).loadUrl (p_url );
338- }
339-
340- public String getUrl ()
341- {
342- return toAPKPath (((WebView )m_control_view ).getUrl ());
343- }
344-
447+ /*
345448 public boolean canGoBack()
346449 {
347450 return ((WebView)m_control_view).canGoBack();
@@ -381,58 +484,8 @@ public void setCanBounce(boolean p_enabled)
381484 {
382485 }
383486 }
487+ */
384488
385- public boolean getScrollingEnabled ()
386- {
387- return m_scrolling_enabled ;
388- }
389-
390- public void setScrollingEnabled (boolean p_enabled )
391- {
392- if (m_scrolling_enabled == p_enabled )
393- return ;
394- m_scrolling_enabled = p_enabled ;
395- ((WebView )m_control_view ).setHorizontalScrollBarEnabled (p_enabled );
396- ((WebView )m_control_view ).setVerticalScrollBarEnabled (p_enabled );
397- ((WebView )m_control_view ).getSettings ().setBuiltInZoomControls (p_enabled );
398- if (!m_scrolling_enabled )
399- {
400- m_control_view .setOnTouchListener (new View .OnTouchListener () {
401- @ Override
402- public boolean onTouch (View v , MotionEvent event ) {
403- return (event .getAction () == MotionEvent .ACTION_MOVE );
404- }
405- });
406- }
407- else
408- m_control_view .setOnTouchListener (null );
409- }
410-
411- public void goBack (int p_steps )
412- {
413- ((WebView )m_control_view ).goBackOrForward (-p_steps );
414- }
415-
416- public void goForward (int p_steps )
417- {
418- ((WebView )m_control_view ).goBackOrForward (p_steps );
419- }
420-
421- public void reload ()
422- {
423- ((WebView )m_control_view ).reload ();
424- }
425-
426- public void stop ()
427- {
428- ((WebView )m_control_view ).stopLoading ();
429- }
430-
431- public void loadData (String p_base_url , String p_html )
432- {
433- ((WebView )m_control_view ).loadDataWithBaseURL (fromAPKPath (p_base_url ), p_html , "text/html" , "utf-8" , null );
434- }
435-
436489 public static String escapeJSString (String p_string )
437490 {
438491 String t_result = "" ;
@@ -475,23 +528,10 @@ public static String escapeJSString(String p_string)
475528 return t_result ;
476529 }
477530
478- public String executeJavaScript (String p_javascript )
479- {
480- SecureRandom t_random = new SecureRandom ();
481- long t_tag = 0 ;
482- while (t_tag == 0 )
483- t_tag = t_random .nextLong ();
484-
485- // Log.i(TAG, "generated tag: " + t_tag);
486- String t_js = String .format ("javascript:window.revjs.storeResult('%d', eval('%s'))" , t_tag , escapeJSString (p_javascript ));
487- // Log.i(TAG, t_js);
488- ((WebView )m_control_view ).loadUrl (t_js );
489-
490- return Long .toString (t_tag );
491- }
492-
493- public static native void doJSExecutionResult (String tag , String result );
494- public native void doStartedLoading (String url );
495- public native void doFinishedLoading (String url );
496- public native void doLoadingError (String url , String error );
531+ //////////
532+
533+ public static Object createBrowserView ()
534+ {
535+ return new LibBrowserWebView (Engine .getContext ());
536+ }
497537}
0 commit comments