Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timob 11273 3 0 x: Android: KS Web Views > Local URL: Apple logo and html text not appearing. #3241

Merged
merged 4 commits into from
Oct 16, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public class TiUIWebView extends TiUIView
private static final String TAG = "TiUIWebView";
private TiWebViewClient client;
private boolean changingUrl = false;
private boolean injectBindingCode;
private boolean bindingCodeInjected = false;
private boolean isLocalHTML = false;

private static Enum<?> enumPluginStateOff;
private static Enum<?> enumPluginStateOn;
Expand Down Expand Up @@ -283,7 +284,6 @@ public void setUrl(String url)
String finalUrl = url;
Uri uri = Uri.parse(finalUrl);
boolean originalUrlHasScheme = (uri.getScheme() != null);
injectBindingCode = false;

if (!originalUrlHasScheme) {
finalUrl = getProxy().resolveUrl(null, finalUrl);
Expand All @@ -300,7 +300,23 @@ public void setUrl(String url)
BufferedReader breader = new BufferedReader(reader);
String line = breader.readLine();
while (line != null) {
injectBindingCode = true;
if (!bindingCodeInjected) {
int pos = line.indexOf("<html");
if (pos >= 0) {
int posEnd = line.indexOf(">", pos);
if (posEnd > pos) {
out.append(line.substring(pos, posEnd + 1));
out.append(TiWebViewBinding.SCRIPT_TAG_INJECTION_CODE);
if ((posEnd + 1) < line.length()) {
out.append(line.substring(posEnd + 1));
}
out.append("\n");
bindingCodeInjected = true;
line = breader.readLine();
continue;
}
}
}
out.append(line);
out.append("\n");
line = breader.readLine();
Expand Down Expand Up @@ -331,6 +347,7 @@ public void setUrl(String url)
if (!proxy.hasProperty(TiC.PROPERTY_SCALES_PAGE_TO_FIT)) {
getWebView().getSettings().setLoadWithOverviewMode(true);
}
isLocalHTML = false;
getWebView().loadUrl(finalUrl);
}

Expand Down Expand Up @@ -360,7 +377,7 @@ private String escapeContent(String content)
}
return content;
}

public void setHtml(String html)
{
setHtmlInternal(html, TiC.URL_ANDROID_ASSET_RESOURCES, "text/html");
Expand Down Expand Up @@ -406,13 +423,30 @@ private void setHtmlInternal(String html, String baseUrl, String mimeType)
webView.getSettings().setLoadWithOverviewMode(false);
}

// Set flag to indicate that it's local html (used to determine whether we want to inject binding code)
isLocalHTML = true;

if (html.contains(TiWebViewBinding.SCRIPT_INJECTION_ID)) {
// Our injection code is in there already, go ahead and show.
webView.loadDataWithBaseURL(baseUrl, html, mimeType, "utf-8", baseUrl);
return;
}

int tagStart = html.indexOf("<html");
int tagEnd = -1;
if (tagStart >= 0) {
tagEnd = html.indexOf(">", tagStart + 1);

if (tagEnd > tagStart) {
injectBindingCode = true;
StringBuilder sb = new StringBuilder(html.length() + 2500);
sb.append(html.substring(0, tagEnd + 1));
sb.append(TiWebViewBinding.SCRIPT_TAG_INJECTION_CODE);
if ((tagEnd + 1) < html.length()) {
sb.append(html.substring(tagEnd + 1));
}
webView.loadDataWithBaseURL(baseUrl, sb.toString(), mimeType, "utf-8", baseUrl);
bindingCodeInjected = true;
return;
}
}

Expand Down Expand Up @@ -566,6 +600,11 @@ public void stopLoading()

public boolean shouldInjectBindingCode()
{
return injectBindingCode;
return isLocalHTML && !bindingCodeInjected;
}

public void setBindingCodeInjected(boolean injected)
{
bindingCodeInjected = injected;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public class TiWebViewBinding
// This is based on binding.min.js. If you have to change anything...
// - change binding.js
// - minify binding.js to create binding.min.js
protected final static String SCRIPT_INJECTION_ID = "__ti_injection";
protected final static String INJECTION_CODE;
protected final static String SCRIPT_TAG_INJECTION_CODE;

// This is based on polling.min.js. If you have to change anything...
// - change polling.js
Expand All @@ -52,23 +54,30 @@ public class TiWebViewBinding
POLLING_CODE = pollingCode.toString();
}

StringBuilder allCode = new StringBuilder();
StringBuilder scriptCode = new StringBuilder();
StringBuilder injectionCode = new StringBuilder();
scriptCode.append("\n<script id=\"" + SCRIPT_INJECTION_ID + "\">\n");
if (jsonCode == null) {
Log.w(TAG, "Unable to read JSON code for injection");
} else {
allCode.append(jsonCode);
scriptCode.append(jsonCode);
injectionCode.append(jsonCode);
}

if (tiCode == null) {
Log.w(TAG, "Unable to read Titanium binding code for injection");
} else {
allCode.append("\n");
allCode.append(tiCode.toString());
scriptCode.append("\n");
scriptCode.append(tiCode.toString());
injectionCode.append(tiCode.toString());
}
scriptCode.append("\n</script>\n");
jsonCode = null;
tiCode = null;
INJECTION_CODE = allCode.toString();
allCode = null;
SCRIPT_TAG_INJECTION_CODE = scriptCode.toString();
INJECTION_CODE = injectionCode.toString();
scriptCode = null;
injectionCode = null;
}

private Stack<String> codeSnippets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ public void onPageFinished(WebView view, String url)

if (nativeWebView != null) {
if (webView.shouldInjectBindingCode()) {
webView.getWebView().loadUrl("javascript:" + TiWebViewBinding.INJECTION_CODE);
nativeWebView.loadUrl("javascript:" + TiWebViewBinding.INJECTION_CODE);
}
webView.getWebView().loadUrl("javascript:" + TiWebViewBinding.POLLING_CODE);
nativeWebView.loadUrl("javascript:" + TiWebViewBinding.POLLING_CODE);
}

webView.setBindingCodeInjected(false);
}

public TiWebViewBinding getBinding()
Expand Down