use of android.webkit.ConsoleMessage in project wh-app-android by WhiteHouse.
the class ArticleViewFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_article, container, false);
ButterKnife.inject(this, v);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onConsoleMessage(@NonNull ConsoleMessage cm) {
Timber.d("%s -- From line %d of %s", cm.message(), cm.lineNumber(), cm.sourceId());
return true;
}
});
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (PAGE_TEMPLATE_URL.equals(url)) {
view.loadUrl(String.format("javascript:WhiteHouse.loadPage(%s);", mPageJson));
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("youtube.com/")) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
});
return v;
}
use of android.webkit.ConsoleMessage in project WordPress-Android by wordpress-mobile.
the class EditorWebViewAbstract method configureWebView.
@SuppressLint("SetJavaScriptEnabled")
private void configureWebView() {
WebSettings webSettings = this.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDefaultTextEncodingName("utf-8");
this.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("callback") && mJsCallbackReceiver != null) {
String data = URLDecoder.decode(url);
String[] split = data.split(":", 2);
String callbackId = split[0];
String params = (split.length > 1 ? split[1] : "");
mJsCallbackReceiver.executeCallback(callbackId, params);
}
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
AppLog.e(T.EDITOR, description);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
if (!URLUtil.isNetworkUrl(url)) {
return super.shouldInterceptRequest(view, request);
}
// Request and add an authorization header for HTTPS resource requests.
// Use https:// when requesting the auth header, in case the resource is incorrectly using http://.
// If an auth header is returned, force https:// for the actual HTTP request.
String authHeader = mAuthHeaderRequestListener.onAuthHeaderRequested(UrlUtils.makeHttps(url));
if (StringUtils.notNullStr(authHeader).length() > 0) {
try {
url = UrlUtils.makeHttps(url);
// Keep any existing request headers from the WebResourceRequest
Map<String, String> headerMap = request.getRequestHeaders();
for (Map.Entry<String, String> entry : mHeaderMap.entrySet()) {
headerMap.put(entry.getKey(), entry.getValue());
}
headerMap.put("Authorization", authHeader);
HttpURLConnection conn = HTTPUtils.setupUrlConnection(url, headerMap);
return new WebResourceResponse(conn.getContentType(), conn.getContentEncoding(), conn.getInputStream());
} catch (IOException e) {
AppLog.e(T.EDITOR, e);
}
}
return super.shouldInterceptRequest(view, request);
}
/**
* Compatibility method for API < 21
*/
@SuppressWarnings("deprecation")
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
if (!URLUtil.isNetworkUrl(url)) {
return super.shouldInterceptRequest(view, url);
}
// Request and add an authorization header for HTTPS resource requests.
// Use https:// when requesting the auth header, in case the resource is incorrectly using http://.
// If an auth header is returned, force https:// for the actual HTTP request.
String authHeader = mAuthHeaderRequestListener.onAuthHeaderRequested(UrlUtils.makeHttps(url));
if (StringUtils.notNullStr(authHeader).length() > 0) {
try {
url = UrlUtils.makeHttps(url);
Map<String, String> headerMap = new HashMap<>(mHeaderMap);
headerMap.put("Authorization", authHeader);
HttpURLConnection conn = HTTPUtils.setupUrlConnection(url, headerMap);
return new WebResourceResponse(conn.getContentType(), conn.getContentEncoding(), conn.getInputStream());
} catch (IOException e) {
AppLog.e(T.EDITOR, e);
}
}
return super.shouldInterceptRequest(view, url);
}
});
this.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onConsoleMessage(@NonNull ConsoleMessage cm) {
if (cm.messageLevel() == MessageLevel.ERROR) {
if (mErrorListener != null) {
mErrorListener.onJavaScriptError(cm.sourceId(), cm.lineNumber(), cm.message());
}
AppLog.e(T.EDITOR, cm.message() + " -- From line " + cm.lineNumber() + " of " + cm.sourceId());
} else {
AppLog.d(T.EDITOR, cm.message() + " -- From line " + cm.lineNumber() + " of " + cm.sourceId());
}
return true;
}
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
AppLog.d(T.EDITOR, message);
if (mErrorListener != null) {
mErrorListener.onJavaScriptAlert(url, message);
}
return true;
}
});
}
Aggregations