Example usage for android.webkit WebViewClient WebViewClient

List of usage examples for android.webkit WebViewClient WebViewClient

Introduction

In this page you can find the example usage for android.webkit WebViewClient WebViewClient.

Prototype

WebViewClient

Source Link

Usage

From source file:com.github.pockethub.android.accounts.LoginWebViewActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WebView webView = new WebView(this);

    // Needs the be activated to allow GitHub to perform their requests.
    webView.getSettings().setJavaScriptEnabled(true);

    String userAgent = webView.getSettings().getUserAgentString();
    // Remove chrome from the user agent since GitHub checks it incorrectly
    userAgent = userAgent.replaceAll("Chrome/\\d{2}\\.\\d\\.\\d\\.\\d", "");
    webView.getSettings().setUserAgentString(userAgent);

    String url = getIntent().getStringExtra(LoginActivity.INTENT_EXTRA_URL);
    webView.loadUrl(url);/* w  w  w  .  j  a v  a 2s  .c  o  m*/

    webView.setWebViewClient(new WebViewClient() {
        MaterialDialog dialog = new MaterialDialog.Builder(LoginWebViewActivity.this).content(R.string.loading)
                .progress(true, 0).build();

        @Override
        public void onPageStarted(android.webkit.WebView view, String url, Bitmap favicon) {
            dialog.show();
        }

        @Override
        public void onPageFinished(android.webkit.WebView view, String url) {
            dialog.dismiss();
        }

        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public WebResourceResponse shouldInterceptRequest(android.webkit.WebView view,
                WebResourceRequest request) {
            return shouldIntercept(request.getUrl().toString());
        }

        @Override
        public WebResourceResponse shouldInterceptRequest(android.webkit.WebView view, String url) {
            return shouldIntercept(url);
        }

        @Override
        public boolean shouldOverrideUrlLoading(android.webkit.WebView view, String url) {
            Uri uri = Uri.parse(url);
            return overrideOAuth(uri) || super.shouldOverrideUrlLoading(view, url);
        }

        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public boolean shouldOverrideUrlLoading(android.webkit.WebView view, WebResourceRequest request) {

            return overrideOAuth(request.getUrl()) || super.shouldOverrideUrlLoading(view, request);
        }

        /**
         * This method will inject polyfills to the auth javascript if the version is
         * below Lollipop. After Lollipop WebView is updated via the Play Store so the polyfills
         * are not needed.
         *
         * @param url The requests url
         * @return null if there request should not be altered or a new response
         *     instance with polyfills.
         */
        private WebResourceResponse shouldIntercept(String url) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                return null;
            }

            if (url.matches(".*frameworks.*.js")) {
                InputStream in1 = null;
                InputStream in2 = null;
                Response response = null;
                try {
                    response = new OkHttpClient.Builder().build()
                            .newCall(new Request.Builder().get().url(url).build()).execute();

                    if (response.body() != null) {
                        in1 = response.body().byteStream();
                    }

                    in2 = getAssets().open("polyfills.js");
                } catch (IOException e) {
                    e.printStackTrace();
                }

                if (response == null) {
                    return null;
                }

                SequenceInputStream inputStream = new SequenceInputStream(in2, in1);
                return new WebResourceResponse("text/javascript", "utf-8", inputStream);
            } else {
                return null;
            }
        }

        private boolean overrideOAuth(Uri uri) {
            if (uri.getScheme().equals(getString(R.string.github_oauth_scheme))) {
                Intent data = new Intent();
                data.setData(uri);
                setResult(RESULT_OK, data);
                finish();
                return true;
            }

            return false;
        }
    });

    setContentView(webView);
}

From source file:com.battlelancer.seriesguide.getglueapi.GetGlueAuthActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview);
    setupActionBar();//from   ww  w . j a v  a  2  s .c  o  m

    WebView webview = (WebView) findViewById(R.id.webView);

    final FragmentActivity activity = this;
    webview.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(activity, getString(R.string.getglue_authfailed) + " " + description,
                    Toast.LENGTH_LONG).show();

            finish();
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith(GetGlueCheckin.OAUTH_CALLBACK_URL)) {
                Uri uri = Uri.parse(url);

                new RetrieveAccessTokenTask(getApplicationContext()).execute(uri);

                finish();
                return true;
            }
            return false;
        }
    });

    // mWebview.getSettings().setJavaScriptEnabled(true);

    // make sure we start fresh
    webview.clearCache(true);

    Timber.d("Initiating authorization request...");
    Resources res = getResources();
    try {
        OAuthClientRequest request = com.uwetrottmann.getglue.GetGlue
                .getAuthorizationRequest(BuildConfig.TVTAG_CLIENT_ID, GetGlueCheckin.OAUTH_CALLBACK_URL);
        webview.loadUrl(request.getLocationUri());
    } catch (OAuthSystemException e) {
        Timber.e(e, "Auth request failed");
    }
}

From source file:com.wms.ezyoukuuploader.sdk.activity.GetRefreshTokenActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.youku_login);

    progressBarLoading = (ProgressBar) findViewById(R.id.progressBarLoading);

    webviewYouku = (WebView) findViewById(R.id.webViewYouku);
    webviewYouku.setWebViewClient(new WebViewClient() {
        @Override//from w ww .j  a v  a  2 s.  c  o  m
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            String[] str = url.split("=")[1].split("&");
            authorizationCode = str[0];
            String refreshToken = getRefreshToken();
            if (refreshToken != null) {
                Toast.makeText(getApplicationContext(), getString(R.string.loginSuccessful), Toast.LENGTH_LONG)
                        .show();
                finish();
            }

            return false;
        }
    });
    webviewYouku.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int progress) {
            if (progress == 100) {
                progressBarLoading.setVisibility(View.INVISIBLE);
            }
        }
    });
    String url = YoukuConstants.YOUKU_AUTHORIZE_URL + "?" + YoukuConstants.PARAM_CLIENT_ID + "="
            + getString(R.string.YOUKU_APP_CLIENT_ID) + "&" + YoukuConstants.PARAM_RESPONSE_TYPE + "="
            + YoukuConstants.PARAM_AUTHORIZATION_CODE + "&" + YoukuConstants.PARAM_REDIRECT_URI + "="
            + getString(R.string.YOUKU_APP_REDIRECT_URI);
    webviewYouku.loadUrl(url);

    Toast.makeText(this, getString(R.string.pleaseLogin), Toast.LENGTH_LONG).show();
}

From source file:cl.monsoon.s1next.view.fragment.HelpFragment.java

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    WebPageViewModel viewModel = new WebPageViewModel();
    mFragmentHelpBinding.setWebPageViewModel(viewModel);
    mWebView.setWebViewClient(new WebViewClient() {

        @Override/*  w  w  w .jav a  2s  .  co m*/
        public void onPageFinished(WebView view, String url) {
            viewModel.setFinishedLoading(true);
        }
    });

    // restore the state of WebView when configuration changes
    // see http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/
    if (savedInstanceState == null) {
        mWebView.loadUrl(HELP_PAGE_URL);
    } else {
        mWebView.restoreState(savedInstanceState);
        // if we haven't finished loading before
        if (mWebView.getUrl() == null) {
            mWebView.loadUrl(HELP_PAGE_URL);
        }
    }
}

From source file:com.klinker.android.spotify.fragment.BaseOAuthFragment.java

/**
 * Set up the webview client that information should be displayed in
 *//*from   w w w  .  j a va  2s. co m*/
public WebViewClient getWebViewClient() {
    return new WebViewClient() {
        private boolean authComplete = false;

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            Log.v(TAG, "callback url: " + url);
            if (shouldRedirect(url)) {
                view.stopLoading();
                authComplete = processPageFinished(url, authComplete);
            }
        }
    };
}

From source file:com.prasanna.android.stacknetwork.utils.MarkdownFormatter.java

public static void loadText(final WebView webView, final String text) {
    webView.setWebChromeClient(new WebChromeClient());
    webView.setWebViewClient(new WebViewClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadDataWithBaseURL(BASE_URL,
            CODE_HTML_PREFIX + MarkdownFormatter.escapeHtml(text) + CODE_HTML_SUFFIX,
            HttpContentTypes.TEXT_HTML, HTTP.UTF_8, null);
}

From source file:com.drisoftie.cwdroid.frag.FragBoard.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    swipeBoard = (SwipeRefreshLayout) inflater.inflate(R.layout.frag_board, container, false);
    webBoard = (WebView) swipeBoard.findViewById(R.id.web_board);
    webBoard.requestFocus(View.FOCUS_DOWN);

    webBoard.setWebViewClient(new WebViewClient() {
        @Override/*from   ww w .  j a v a 2 s. c  om*/
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            getArguments().putString(FragBoard.class.getName(), url);
            return true;
        }
    });

    return webBoard;
}

From source file:com.drisoftie.cwdroid.frag.FragShoutbox.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    swipeShoutbox = (SwipeRefreshLayout) inflater.inflate(R.layout.frag_shoutbox, container, false);
    webShoutbox = (WebView) swipeShoutbox.findViewById(R.id.web_shoutbox);

    webShoutbox.setWebViewClient(new WebViewClient() {
        @Override//from w  w  w .ja va 2  s  .c  om
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            getArguments().putString(FragShoutbox.class.getName(), url);
            return true;
        }
    });
    return webShoutbox;
}

From source file:com.hhs.hfnavigator.slidingtabs.home.BusFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_webview, null);

    swipeRefreshLayout = (SwipeRefreshLayout) root.findViewById(R.id.swipe);
    progressWheel = (ProgressWheel) root.findViewById(R.id.webViewProgress);

    swipeRefreshLayout.setEnabled(false);

    final WebView webView = (WebView) root.findViewById(R.id.webView);
    if (webView != null) {
        webView.loadUrl("http://vqr.mx/DmkD");
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setDisplayZoomControls(false);
        webView.setWebViewClient(new WebViewClient() {

            public void onPageFinished(WebView view, String url) {
                progressWheel.stopSpinning();
                swipeRefreshLayout.setRefreshing(false);
                webView.setVisibility(View.VISIBLE);
            }//  www .  j a  va  2 s . c  om
        });
    }
    progressWheel.spin();
    webView.setVisibility(View.INVISIBLE);

    //        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    //            @Override
    //            public void onRefresh() {
    //                if (CheckNetwork.isInternetAvailable(getActivity())) {
    //                    webView.reload();
    //                }
    //            }
    //        });
    //
    //        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
    //                android.R.color.holo_red_light,
    //                android.R.color.holo_green_light,
    //                android.R.color.holo_orange_light);

    return root;
}

From source file:com.android.mail.ui.HelpFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
    setHasOptionsMenu(true);// www  .j a va2 s .  c  o  m

    final View view = inflater.inflate(R.layout.help_fragment, container, false);
    if (view != null) {
        mWebView = (WebView) view.findViewById(R.id.webview);
        mWebView.setWebViewClient(new WebViewClient());
        if (state != null) {
            mWebView.restoreState(state);
        }
    }

    return view;
}