99import android .view .View ;
1010import android .view .ViewGroup ;
1111import android .webkit .ConsoleMessage ;
12+ import android .webkit .JavascriptInterface ;
1213import android .webkit .JsResult ;
1314import android .webkit .WebChromeClient ;
1415import android .webkit .WebSettings ;
@@ -99,31 +100,13 @@ public void onConsoleMessage(String message, int lineNumber, String sourceId) {
99100 });
100101 String htmlEditor = getHtmlEditor ();
101102
103+ mWebView .addJavascriptInterface (new JsCallbackHandler (), "nativeCallbackHandler" );
104+
102105 mWebView .loadDataWithBaseURL ("file:///android_asset/" , htmlEditor , "text/html" , "utf-8" , "" );
103106
104107 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
105108 WebView .setWebContentsDebuggingEnabled (true );
106109 }
107- // TODO: Replace postDelay with a callback from JS
108- mWebView .postDelayed (new Runnable () {
109- @ Override
110- public void run () {
111- String htmlFile = "" ;
112- try {
113- htmlFile = getStringFromAsset ("example-content.html" );
114- htmlFile = htmlFile .replace ("\\ " , "\\ \\ " );
115- htmlFile = htmlFile .replace ("\" " , "\\ \" " );
116- htmlFile = htmlFile .replace ("'" , "\\ '" );
117- htmlFile = htmlFile .replace ("\r " , "\\ r" );
118- htmlFile = htmlFile .replace ("\n " , "\\ n" );
119- } catch (IOException e ) {
120- AppLog .e (T .EDITOR , e .getMessage ());
121- }
122-
123- // Load example file into editor content field
124- mWebView .loadUrl ("JavaScript:ZSSEditor.getField('zss_field_content').setHTML('" + htmlFile + "');" );
125- }
126- }, 5000 );
127110 }
128111
129112 private String getStringFromAsset (String filename ) throws IOException {
@@ -189,4 +172,31 @@ public void appendGallery(MediaGallery mediaGallery) {
189172 public Spanned getSpannedContent () {
190173 return null ;
191174 }
192- }
175+
176+ class JsCallbackHandler {
177+ @ JavascriptInterface
178+ public void executeCallback (final String callbackId ) {
179+ if (callbackId .equals ("callback-dom-loaded" )) {
180+ // Run on UI thread
181+ mWebView .post (new Runnable () {
182+ public void run () {
183+ String htmlFile = "" ;
184+ try {
185+ htmlFile = getStringFromAsset ("example-content.html" );
186+ htmlFile = htmlFile .replace ("\\ " , "\\ \\ " );
187+ htmlFile = htmlFile .replace ("\" " , "\\ \" " );
188+ htmlFile = htmlFile .replace ("'" , "\\ '" );
189+ htmlFile = htmlFile .replace ("\r " , "\\ r" );
190+ htmlFile = htmlFile .replace ("\n " , "\\ n" );
191+ } catch (IOException e ) {
192+ AppLog .e (T .EDITOR , e .getMessage ());
193+ }
194+
195+ // Load example file into editor content field
196+ mWebView .loadUrl ("JavaScript:ZSSEditor.getField('zss_field_content').setHTML('" + htmlFile + "');" );
197+ }
198+ });
199+ }
200+ }
201+ }
202+ }
0 commit comments