Search in sources :

Example 1 with WebResourceError

use of android.webkit.WebResourceError 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 WebResourceError

use of android.webkit.WebResourceError 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 3 with WebResourceError

use of android.webkit.WebResourceError in project CCIP-Android by CCIP-App.

the class PuzzleFragment method onCreateView.

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View view = inflater.inflate(R.layout.fragment_web, container, false);
    setHasOptionsMenu(true);
    mActivity = getActivity();
    progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
    webView = (WebView) view.findViewById(R.id.webView);
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            view.loadUrl(URL_NO_NETWORK);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
        }
    });
    webView.setWebChromeClient(new WebChromeViewClient(progressBar));
    if (PreferenceUtil.getToken(getActivity()) != null) {
        webView.loadUrl(URL_PUZZLE + PreferenceUtil.getToken(getActivity()));
    } else {
        webView.loadUrl("data:text/html, <div>Please login</div>");
    }
    WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setDomStorageEnabled(true);
    if (Build.VERSION.SDK_INT >= 21) {
        settings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
    }
    return view;
}
Also used : WebChromeViewClient(org.sitcon.ccip.network.webclient.WebChromeViewClient) WebResourceRequest(android.webkit.WebResourceRequest) WebSettings(android.webkit.WebSettings) WebResourceError(android.webkit.WebResourceError) Intent(android.content.Intent) WebView(android.webkit.WebView) View(android.view.View) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient) Nullable(android.support.annotation.Nullable)

Example 4 with WebResourceError

use of android.webkit.WebResourceError in project Reader by TheKeeperOfPie.

the class FragmentWeb method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_web, container, false);
    layoutRoot = (RelativeLayout) view;
    toolbar = (Toolbar) view.findViewById(R.id.toolbar);
    toolbar.setTitle(getString(R.string.loading_web_page));
    toolbar.setTitleTextColor(themer.getColorFilterPrimary().getColor());
    toolbar.setOnClickListener(this);
    toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
    toolbar.getNavigationIcon().mutate().setColorFilter(themer.getColorFilterPrimary());
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            hasExited = true;
            mListener.onNavigationBackClick();
        }
    });
    setUpOptionsMenu();
    progressBar = (ProgressBar) view.findViewById(R.id.progress_web);
    webView = (WebView) view.findViewById(R.id.web);
    webView.setBackgroundColor(getResources().getColor(R.color.darkThemeBackground));
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            toolbar.setTitle(url);
            progressBar.setIndeterminate(true);
            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            progressBar.setVisibility(View.GONE);
            toolbar.setTitle(view.getTitle());
        }

        // @Override
        // public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // Log.d(TAG, "shouldOverrideUrlLoading: " + url);
        // 
        // Intent intent = new Intent(Intent.ACTION_VIEW);
        // intent.setData(Uri.parse(url));
        // PackageManager packageManager = view.getContext().getPackageManager();
        // List<ResolveInfo> listResolveInfo = packageManager.queryIntentActivities(intent, 0);
        // 
        // Log.d(TAG, "listResolveInfo: " + listResolveInfo);
        // 
        // boolean containsAll = true;
        // 
        // for (ResolveInfo info : listResolveInfo) {
        // 
        // boolean contains = false;
        // for (ResolveInfo infoDefault : listDefaultWebResolveInfo) {
        // if (infoDefault.activityInfo.name.equals(info.activityInfo.name) &&
        // infoDefault.activityInfo.targetActivity.equals(info.activityInfo.targetActivity)) {
        // contains = true;
        // break;
        // }
        // }
        // if (!contains) {
        // containsAll = false;
        // break;
        // }
        // }
        // 
        // if (!containsAll) {
        // Iterator<ResolveInfo> iterator = listResolveInfo.iterator();
        // while (iterator.hasNext()) {
        // ResolveInfo info = iterator.next();
        // boolean contains = false;
        // for (ResolveInfo infoDefault : listDefaultWebResolveInfo) {
        // if (infoDefault.activityInfo.name.equals(info.activityInfo.name) &&
        // infoDefault.activityInfo.targetActivity.equals(info.activityInfo.targetActivity)) {
        // contains = true;
        // break;
        // }
        // }
        // if (contains) {
        // iterator.remove();
        // }
        // }
        // 
        // listResolveInfo.removeAll(listDefaultWebResolveInfo);
        // Collections.sort(listResolveInfo, new ResolveInfo.DisplayNameComparator(packageManager));
        // listResolveInfo.addAll(listDefaultWebResolveInfo);
        // showExternalLaunchDialog(url, listResolveInfo, packageManager);
        // return true;
        // }
        // return false;
        // }
        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            Log.e(TAG, "WebView error: " + error);
        }
    });
    webView.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            progressBar.setIndeterminate(false);
            progressBar.setProgress(newProgress);
        }

        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            viewWebFullscreen = view;
            viewWebFullscreen.setBackgroundColor(0xFF000000);
            customViewCallback = callback;
            layoutRoot.addView(viewWebFullscreen, ViewGroup.LayoutParams.MATCH_PARENT);
            viewWebFullscreen.bringToFront();
        }

        @Override
        public void onHideCustomView() {
            super.onHideCustomView();
            layoutRoot.removeView(viewWebFullscreen);
            viewWebFullscreen = null;
        }
    });
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setDisplayZoomControls(false);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setDatabaseEnabled(true);
    webView.getSettings().setAppCacheEnabled(true);
    webView.getSettings().setSupportMultipleWindows(false);
    webView.setBackgroundColor(0xFFFFFFFF);
    webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    Log.d(TAG, "savedInstanceState: " + savedInstanceState);
    if (savedInstanceState == null) {
        webView.loadUrl(url);
    } else {
        webView.restoreState(savedInstanceState);
    }
    toolbarActions = (Toolbar) view.findViewById(R.id.toolbar_actions);
    toolbarActions.inflateMenu(R.menu.menu_web_search);
    toolbarActions.setOnMenuItemClickListener(this);
    Menu menuActions = toolbarActions.getMenu();
    for (int index = 0; index < menuActions.size(); index++) {
        menuActions.getItem(index).getIcon().mutate().setColorFilter(themer.getColorFilterIcon());
    }
    return view;
}
Also used : WebResourceRequest(android.webkit.WebResourceRequest) SearchView(android.support.v7.widget.SearchView) View(android.view.View) WebView(android.webkit.WebView) Bitmap(android.graphics.Bitmap) WebChromeClient(android.webkit.WebChromeClient) WebResourceError(android.webkit.WebResourceError) Menu(android.view.Menu) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient)

Example 5 with WebResourceError

use of android.webkit.WebResourceError in project Reader by TheKeeperOfPie.

the class ActivityLogin method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    CustomApplication.getComponentMain().inject(this);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
    layoutRoot = (ViewGroup) findViewById(R.id.layout_root);
    Themer themer = new Themer(this);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitleTextColor(themer.getColorFilterPrimary().getColor());
    progressAuth = (ProgressBar) findViewById(R.id.progress_auth);
    webAuth = (WebView) findViewById(R.id.web_auth);
    webAuth.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            Log.d(TAG, "onPageStarted() called with: " + "view = [" + view + "], url = [" + url + "], favicon = [" + favicon + "]");
            progressAuth.setIndeterminate(true);
            progressAuth.setVisibility(View.VISIBLE);
            Uri uri = Uri.parse(url);
            if (uri.getHost().equals(Reddit.REDIRECT_URI.replaceFirst("https://", ""))) {
                String error = uri.getQueryParameter("error");
                String returnedState = uri.getQueryParameter("state");
                if (!TextUtils.isEmpty(error) || !state.equals(returnedState)) {
                    Toast.makeText(ActivityLogin.this, error, Toast.LENGTH_LONG).show();
                    webAuth.loadUrl(Reddit.getUserAuthUrl(state));
                    return;
                }
                // TODO: Failsafe with error and state
                String code = uri.getQueryParameter("code");
                fetchTokens(code);
            }
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            progressAuth.setVisibility(View.GONE);
            toolbar.setTitle(view.getTitle());
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            Log.e(TAG, "WebView error: " + error);
        }
    });
    webAuth.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            progressAuth.setIndeterminate(false);
            progressAuth.setProgress(newProgress);
        }
    });
    webAuth.loadUrl(Reddit.getUserAuthUrl(state));
}
Also used : Bitmap(android.graphics.Bitmap) WebResourceRequest(android.webkit.WebResourceRequest) WebChromeClient(android.webkit.WebChromeClient) WebResourceError(android.webkit.WebResourceError) Themer(com.winsonchiu.reader.theme.Themer) WebView(android.webkit.WebView) Uri(android.net.Uri) WebViewClient(android.webkit.WebViewClient)

Aggregations

WebResourceError (android.webkit.WebResourceError)15 WebResourceRequest (android.webkit.WebResourceRequest)15 WebView (android.webkit.WebView)15 WebViewClient (android.webkit.WebViewClient)15 Bitmap (android.graphics.Bitmap)8 WebSettings (android.webkit.WebSettings)7 WebChromeClient (android.webkit.WebChromeClient)6 SuppressLint (android.annotation.SuppressLint)4 View (android.view.View)4 WebResourceResponse (android.webkit.WebResourceResponse)4 Intent (android.content.Intent)3 Uri (android.net.Uri)3 SslError (android.net.http.SslError)3 SslErrorHandler (android.webkit.SslErrorHandler)3 TargetApi (android.annotation.TargetApi)2 Nullable (android.support.annotation.Nullable)2 WebChromeViewClient (org.sitcon.ccip.network.webclient.WebChromeViewClient)2 DownloadManager (android.app.DownloadManager)1 ActivityNotFoundException (android.content.ActivityNotFoundException)1 ResolveInfo (android.content.pm.ResolveInfo)1