Search in sources :

Example 1 with WebResourceRequest

use of android.webkit.WebResourceRequest in project weex-example by KalicyZhou.

the class WXWebView method initWebView.

private void initWebView(WebView wv) {
    WebSettings settings = wv.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setAppCacheEnabled(true);
    settings.setUseWideViewPort(true);
    settings.setDomStorageEnabled(true);
    settings.setSupportZoom(false);
    settings.setBuiltInZoomControls(false);
    wv.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            WXLogUtils.v("tag", "onPageOverride " + url);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            WXLogUtils.v("tag", "onPageStarted " + url);
            if (mOnPageListener != null) {
                mOnPageListener.onPageStart(url);
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            WXLogUtils.v("tag", "onPageFinished " + url);
            if (mOnPageListener != null) {
                mOnPageListener.onPageFinish(url, view.canGoBack(), view.canGoForward());
            }
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            if (mOnErrorListener != null) {
                //mOnErrorListener.onError("error", "page error code:" + error.getErrorCode() + ", desc:" + error.getDescription() + ", url:" + request.getUrl());
                mOnErrorListener.onError("error", "page error");
            }
        }

        @Override
        public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
            super.onReceivedHttpError(view, request, errorResponse);
            if (mOnErrorListener != null) {
                mOnErrorListener.onError("error", "http error");
            }
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            super.onReceivedSslError(view, handler, error);
            if (mOnErrorListener != null) {
                mOnErrorListener.onError("error", "ssl error");
            }
        }
    });
    wv.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            showWebView(newProgress == 100);
            showProgressBar(newProgress != 100);
            WXLogUtils.v("tag", "onPageProgressChanged " + newProgress);
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            if (mOnPageListener != null) {
                mOnPageListener.onReceivedTitle(view.getTitle());
            }
        }
    });
}
Also used : SslErrorHandler(android.webkit.SslErrorHandler) WebResourceRequest(android.webkit.WebResourceRequest) SslError(android.net.http.SslError) Bitmap(android.graphics.Bitmap) WebResourceResponse(android.webkit.WebResourceResponse) WebSettings(android.webkit.WebSettings) WebChromeClient(android.webkit.WebChromeClient) WebResourceError(android.webkit.WebResourceError) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient)

Example 2 with WebResourceRequest

use of android.webkit.WebResourceRequest in project instructure-android by instructure.

the class OAuthWebLogin method createWebView.

private WebView createWebView() {
    web = new WebView(this);
    clearCookies();
    web.getSettings().setLoadWithOverviewMode(true);
    web.getSettings().setJavaScriptEnabled(true);
    web.getSettings().setBuiltInZoomControls(true);
    web.getSettings().setUseWideViewPort(true);
    web.getSettings().setSavePassword(false);
    web.getSettings().setSaveFormData(false);
    web.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    web.getSettings().setAppCacheEnabled(false);
    web.getSettings().setUserAgentString(CanvasAPI.getCandroidUserAgent("candroid", OAuthWebLogin.this));
    web.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains(successURL)) {
                String oAuthRequest = url.substring(url.indexOf(successURL) + successURL.length());
                OAuthAPI.getToken(client_id, client_secret, oAuthRequest, getToken);
            } else if (url.contains(errorURL)) {
                clearCookies();
                view.loadUrl(authenticationURL);
            } else {
                view.loadUrl(url);
            }
            // then it is not handled by default action
            return true;
        }

        public void onPageFinished(WebView view, String url) {
            setProgressBarIndeterminateVisibility(false);
        }

        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                if (url.contains("idp.sfcollege.edu/idp/santafe")) {
                    specialCase = true;
                    String oAuthRequest = url.substring(url.indexOf("hash=") + "hash=".length());
                    OAuthAPI.getToken(client_id, client_secret, oAuthRequest, getToken);
                }
            }
            return super.shouldInterceptRequest(view, url);
        }

        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Uri uriUrl = request.getUrl();
                String url = uriUrl.toString();
                Utils.d("url: " + url);
                if (url.contains("idp.sfcollege.edu/idp/santafe")) {
                    specialCase = true;
                    String oAuthRequest = url.substring(url.indexOf("hash=") + "hash=".length());
                    OAuthAPI.getToken(client_id, client_secret, oAuthRequest, getToken);
                }
            }
            return super.shouldInterceptRequest(view, request);
        }

        @Override
        public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
            httpAuthHandler = handler;
            showAuthenticationDialog();
        }
    });
    return web;
}
Also used : WebResourceResponse(android.webkit.WebResourceResponse) WebResourceRequest(android.webkit.WebResourceRequest) HttpAuthHandler(android.webkit.HttpAuthHandler) WebView(android.webkit.WebView) Uri(android.net.Uri) WebViewClient(android.webkit.WebViewClient)

Example 3 with WebResourceRequest

use of android.webkit.WebResourceRequest in project summer-android by cn-cerc.

the class FrmScanProduct method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_frm_scan_product);
    instance = this;
    Intent intent = getIntent();
    imgBack = (ImageView) findViewById(R.id.imgBack);
    imgBack.setOnClickListener(this);
    lblTitle = (TextView) findViewById(R.id.lblTitle);
    lblTitle.setText(intent.getStringExtra("title"));
    this.homeUrl = intent.getStringExtra("homeUrl");
    this.returnUrl = intent.getStringExtra("returnUrl");
    this.appendUrl = intent.getStringExtra("appendUrl");
    this.modifyUrl = intent.getStringExtra("modifyUrl");
    this.deleteUrl = intent.getStringExtra("deleteUrl");
    this.viewUrl = intent.getStringExtra("viewUrl");
    chkIsSpare = (CheckBox) findViewById(R.id.chkIsSpare);
    edtBarcode = (EditText) findViewById(R.id.edtBarcode);
    btnSave = (Button) findViewById(R.id.btnSave);
    btnSave.setOnClickListener(this);
    adapter = new ListViewAdapter(this, R.layout.activity_list_scan_product, dataSet, this);
    lstView = (ListView) findViewById(R.id.lstView);
    lstView.setAdapter(adapter);
    webView = (WebView) findViewById(R.id.webView);
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            // 返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器
            view.loadUrl(url);
            return true;
        }
    });
    // 启用支持javascript
    WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    // 打开指定的网页
    webView.loadUrl(MyApp.getFormUrl(homeUrl));
    // 关闭软键盘
    edtBarcode.setInputType(InputType.TYPE_NULL);
    edtBarcode.setOnEditorActionListener(new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                saveBarcode();
            }
            return false;
        }
    });
    // 自动归位焦点
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            Message msg = new Message();
            msg.what = MSG_TIMER;
            handler.sendMessage(msg);
        }
    }, 1000, 200);
}
Also used : WebResourceRequest(android.webkit.WebResourceRequest) Message(android.os.Message) Intent(android.content.Intent) KeyEvent(android.view.KeyEvent) Timer(java.util.Timer) TimerTask(java.util.TimerTask) ListViewAdapter(cn.cerc.summer.android.basis.ListViewAdapter) WebSettings(android.webkit.WebSettings) TextView(android.widget.TextView) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient)

Example 4 with WebResourceRequest

use of android.webkit.WebResourceRequest in project incubator-weex by apache.

the class WXWebView method initWebView.

private void initWebView(WebView wv) {
    WebSettings settings = wv.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setAppCacheEnabled(true);
    settings.setUseWideViewPort(true);
    settings.setDomStorageEnabled(true);
    settings.setSupportZoom(false);
    settings.setBuiltInZoomControls(false);
    wv.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            WXLogUtils.v("tag", "onPageOverride " + url);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            WXLogUtils.v("tag", "onPageStarted " + url);
            if (mOnPageListener != null) {
                mOnPageListener.onPageStart(url);
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            WXLogUtils.v("tag", "onPageFinished " + url);
            if (mOnPageListener != null) {
                mOnPageListener.onPageFinish(url, view.canGoBack(), view.canGoForward());
            }
            if (mOnMessageListener != null) {
                evaluateJS("javascript:(window.postMessage = function(message, targetOrigin) {" + "if (message == null || !targetOrigin) return;" + (DOWNGRADE_JS_INTERFACE ? "prompt('" + BRIDGE_NAME + "://postMessage?message=' + JSON.stringify(message) + '&targetOrigin=' + targetOrigin)" : BRIDGE_NAME + ".postMessage(JSON.stringify(message), targetOrigin);") + "})");
            }
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            if (mOnErrorListener != null) {
                // mOnErrorListener.onError("error", "page error code:" + error.getErrorCode() + ", desc:" + error.getDescription() + ", url:" + request.getUrl());
                mOnErrorListener.onError("error", "page error");
            }
        }

        @Override
        public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
            super.onReceivedHttpError(view, request, errorResponse);
            if (mOnErrorListener != null) {
                mOnErrorListener.onError("error", "http error");
            }
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            super.onReceivedSslError(view, handler, error);
            if (mOnErrorListener != null) {
                mOnErrorListener.onError("error", "ssl error");
            }
        }
    });
    wv.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            showWebView(newProgress == 100);
            showProgressBar(newProgress != 100);
            WXLogUtils.v("tag", "onPageProgressChanged " + newProgress);
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            if (mOnPageListener != null) {
                mOnPageListener.onReceivedTitle(view.getTitle());
            }
        }

        @Override
        public boolean onJsPrompt(WebView view, String url, String text, String defaultValue, JsPromptResult result) {
            Uri uri = Uri.parse(text);
            String scheme = uri.getScheme();
            if (TextUtils.equals(scheme, BRIDGE_NAME)) {
                if (TextUtils.equals(uri.getAuthority(), "postMessage")) {
                    String message = uri.getQueryParameter("message");
                    String targetOrigin = uri.getQueryParameter("targetOrigin");
                    onMessage(message, targetOrigin);
                    result.confirm("success");
                } else {
                    result.confirm("fail");
                }
                return true;
            }
            return super.onJsPrompt(view, url, text, defaultValue, result);
        }
    });
    if (!DOWNGRADE_JS_INTERFACE) {
        wv.addJavascriptInterface(new Object() {

            @JavascriptInterface
            public void postMessage(String message, String targetOrigin) {
                onMessage(message, targetOrigin);
            }
        }, BRIDGE_NAME);
    }
}
Also used : SslErrorHandler(android.webkit.SslErrorHandler) WebResourceRequest(android.webkit.WebResourceRequest) SslError(android.net.http.SslError) JavascriptInterface(android.webkit.JavascriptInterface) Uri(android.net.Uri) Bitmap(android.graphics.Bitmap) WebResourceResponse(android.webkit.WebResourceResponse) WebSettings(android.webkit.WebSettings) WebChromeClient(android.webkit.WebChromeClient) WebResourceError(android.webkit.WebResourceError) JSONObject(com.alibaba.fastjson.JSONObject) WebView(android.webkit.WebView) JsPromptResult(android.webkit.JsPromptResult) WebViewClient(android.webkit.WebViewClient)

Example 5 with WebResourceRequest

use of android.webkit.WebResourceRequest in project edx-app-android by edx.

the class WebViewActivity method onCreate.

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview);
    super.setToolbarAsActionBar();
    final ProgressBar progress = (ProgressBar) findViewById(R.id.loading_indicator);
    progress.setVisibility(View.GONE);
    webView = (WebView) findViewById(R.id.webView);
    final URLInterceptorWebViewClient client = new URLInterceptorWebViewClient(this, webView);
    client.setPageStatusListener(new URLInterceptorWebViewClient.IPageStatusListener() {

        @Override
        public void onPageStarted() {
            progress.setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageFinished() {
            progress.setVisibility(View.GONE);
        }

        @Override
        public void onPageLoadError(WebView view, int errorCode, String description, String failingUrl) {
            progress.setVisibility(View.GONE);
        }

        @Override
        public void onPageLoadError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse, boolean isMainRequestFailure) {
            if (isMainRequestFailure) {
                progress.setVisibility(View.GONE);
            }
        }

        @Override
        public void onPagePartiallyLoaded() {
            progress.setVisibility(View.GONE);
        }
    });
    webView.loadUrl(getIntent().getStringExtra(ARG_URL));
    final String title = getIntent().getStringExtra(ARG_TITLE);
    if (!TextUtils.isEmpty(title)) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setTitle(title);
    }
}
Also used : WebResourceRequest(android.webkit.WebResourceRequest) WebResourceResponse(android.webkit.WebResourceResponse) WebView(android.webkit.WebView) ProgressBar(android.widget.ProgressBar) URLInterceptorWebViewClient(org.edx.mobile.view.custom.URLInterceptorWebViewClient)

Aggregations

WebResourceRequest (android.webkit.WebResourceRequest)38 WebView (android.webkit.WebView)37 WebViewClient (android.webkit.WebViewClient)34 Bitmap (android.graphics.Bitmap)22 WebResourceError (android.webkit.WebResourceError)21 WebResourceResponse (android.webkit.WebResourceResponse)16 WebSettings (android.webkit.WebSettings)14 Intent (android.content.Intent)13 View (android.view.View)12 WebChromeClient (android.webkit.WebChromeClient)12 SuppressLint (android.annotation.SuppressLint)11 TextView (android.widget.TextView)9 SslError (android.net.http.SslError)8 SslErrorHandler (android.webkit.SslErrorHandler)8 Uri (android.net.Uri)7 TargetApi (android.annotation.TargetApi)6 IOException (java.io.IOException)5 Nullable (android.support.annotation.Nullable)3 SearchView (android.support.v7.widget.SearchView)3 Toolbar (android.support.v7.widget.Toolbar)3