Search in sources :

Example 1 with ConsoleMessage

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;
}
Also used : WebChromeClient(android.webkit.WebChromeClient) Intent(android.content.Intent) WebView(android.webkit.WebView) InjectView(butterknife.InjectView) View(android.view.View) WebView(android.webkit.WebView) ConsoleMessage(android.webkit.ConsoleMessage) WebViewClient(android.webkit.WebViewClient)

Example 2 with ConsoleMessage

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;
        }
    });
}
Also used : WebResourceRequest(android.webkit.WebResourceRequest) IOException(java.io.IOException) SuppressLint(android.annotation.SuppressLint) ConsoleMessage(android.webkit.ConsoleMessage) JsResult(android.webkit.JsResult) HttpURLConnection(java.net.HttpURLConnection) WebResourceResponse(android.webkit.WebResourceResponse) WebSettings(android.webkit.WebSettings) WebChromeClient(android.webkit.WebChromeClient) WebView(android.webkit.WebView) TargetApi(android.annotation.TargetApi) HashMap(java.util.HashMap) Map(java.util.Map) WebViewClient(android.webkit.WebViewClient) SuppressLint(android.annotation.SuppressLint)

Aggregations

ConsoleMessage (android.webkit.ConsoleMessage)2 WebChromeClient (android.webkit.WebChromeClient)2 WebView (android.webkit.WebView)2 WebViewClient (android.webkit.WebViewClient)2 SuppressLint (android.annotation.SuppressLint)1 TargetApi (android.annotation.TargetApi)1 Intent (android.content.Intent)1 View (android.view.View)1 JsResult (android.webkit.JsResult)1 WebResourceRequest (android.webkit.WebResourceRequest)1 WebResourceResponse (android.webkit.WebResourceResponse)1 WebSettings (android.webkit.WebSettings)1 InjectView (butterknife.InjectView)1 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1