Example usage for android.webkit WebView setWebViewClient

List of usage examples for android.webkit WebView setWebViewClient

Introduction

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

Prototype

public void setWebViewClient(WebViewClient client) 

Source Link

Document

Sets the WebViewClient that will receive various notifications and requests.

Usage

From source file:jp.mixi.android.sdk.MixiDialog.java

private WebView setUpWebView(WebView webView) {
    Log.v(TAG, "setwebview");
    webView.getSettings().setUserAgentString(Constants.USER_AGENT);
    webView.setVerticalScrollBarEnabled(false);
    webView.setHorizontalScrollBarEnabled(false);
    webView.setWebViewClient(new MixiWebviewClient());

    webView.getSettings().setJavaScriptEnabled(true);
    webView.addJavascriptInterface(new JsHandler(), "mixi");

    String token = mParams.get("oauth_token");
    int sdkInt = Integer.valueOf(Build.VERSION.SDK);
    // android2.1
    if (sdkInt < Constants.SUPPORTED_SDK_VERSION) {
        Uri uri = Uri.parse(mUrl);// w  w w.  j a v a 2  s  .c  om
        if (token != null) {
            Builder builder = uri.buildUpon();
            builder.appendQueryParameter("oauth_token", token);
            uri = builder.build();
        }
        webView.loadUrl(uri.toString());
    } else {
        HashMap<String, String> extraHeaders = new HashMap<String, String>();
        if (token != null) {
            extraHeaders.put("Authorization", "OAuth " + token);
        }
        webView.loadUrl(mUrl, extraHeaders);
    }

    return webView;
}

From source file:nirwan.cordova.plugin.printer.Printer.java

private void loadContentAsBitmapIntoPrintController(String content, final Intent intent) {
    Activity ctx = cordova.getActivity();
    final WebView page = new WebView(ctx);
    final Printer self = this;

    page.setVisibility(View.INVISIBLE);
    page.getSettings().setJavaScriptEnabled(false);

    page.setWebViewClient(new WebViewClient() {
        @Override/*ww w . ja  va2 s . c o  m*/
        public void onPageFinished(final WebView page, String url) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Bitmap screenshot = self.takeScreenshot(page);
                    File tmpFile = self.saveScreenshotToTmpFile(screenshot);
                    ViewGroup vg = (ViewGroup) (page.getParent());

                    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(tmpFile));

                    vg.removeView(page);
                }
            }, 1000);
        }
    });

    //Set base URI to the assets/www folder
    String baseURL = webView.getUrl();
    baseURL = baseURL.substring(0, baseURL.lastIndexOf('/') + 1);

    ctx.addContentView(page, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    page.loadDataWithBaseURL(baseURL, content, "text/html", "UTF-8", null);
}

From source file:com.sonymobile.androidapp.gridcomputing.fragments.WebviewFragment.java

@Override
public final void onViewCreated(final View view, final Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    final WebView webView = (WebView) view.findViewById(R.id.webview);
    final View progress = view.findViewById(R.id.webview_progress);

    final String researchUrl = getArguments().getString(URL_KEY);

    progress.setVisibility(View.VISIBLE);

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.setWebViewClient(new WebViewClient() {

        @Override/*  w  w w .j  a v  a2s.c o  m*/
        public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
            if (TextUtils.isEmpty(researchUrl) || researchUrl.equals(url)) {
                return false;
            } else {
                view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                return true;
            }
        }

        @Override
        public void onPageFinished(final WebView view, final String url) {
            progress.setVisibility(View.GONE);
        }
    });

    webView.loadUrl(researchUrl);
}

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);
            }/*from   www.j  a v a 2  s .  c o m*/
        });
    }
    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:net.basov.ticketinfo.UI.java

public void displayMainScreen(final WebView wv) {
    wv.setWebViewClient(new MyWebViewClient() {
        @Override/*from w w w. j  a va2 s .c om*/
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(wv, url);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                view.evaluateJavascript("javascript:jreplace('" + header_json.toString() + "')", null);
                view.evaluateJavascript("javascript:jreplace('" + ticket_json.toString() + "')", null);
                view.evaluateJavascript("javascript:jreplace('" + ic_json.toString() + "')", null);
                view.evaluateJavascript("javascript:jreplace('" + dump_json.toString() + "')", null);
            } else {
                view.loadUrl("javascript:jreplace('" + header_json.toString() + "')");
                view.loadUrl("javascript:jreplace('" + ticket_json.toString() + "')");
                view.loadUrl("javascript:jreplace('" + ic_json.toString() + "')");
                view.loadUrl("javascript:jreplace('" + dump_json.toString() + "')");
            }
            wv.clearCache(true);
            wv.clearHistory();
            //TODO: remove debug
            //Log.d("hhhh", header_json.toString());
            //Log.d("tttt", ticket_json.toString());
            //Log.d("iiii", ic_json.toString());
        }
    });
    Context c = wv.getContext();
    wv.loadUrl("file:///android_asset/" + c.getString(R.string.ticket_ui_file));
}

From source file:com.saltedge.sdk.webview.SEWebViewTools.java

public void initializeWithUrl(Activity activity, final WebView webView, final String url,
        WebViewRedirectListener listener) {
    this.activity = activity;
    this.webViewRedirectListener = listener;
    progressDialog = UITools.createProgressDialog(this.activity);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.setWebViewClient(new CustomWebViewClient());
    webView.setWebChromeClient(new WebChromeClient() {

        @Override//w  w w .j  a v a 2s .  c  o  m
        public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,
                FileChooserParams fileChooserParams) {
            SETools.uploadMessage = filePathCallback;
            pickFile();
            return true;
        }

    });
    webView.loadUrl(url);
}

From source file:com.example.ruby.mygetgps.ui.activities.MainActivity.java

private void setupWebView() {
    WebView webview = (WebView) findViewById(R.id.webview);
    webview.setWebChromeClient(new WebChromeClient() {
        public void onGeolocationPermissionsShowPrompt(String origin,
                GeolocationPermissions.Callback callback) {
            callback.invoke(origin, true, false);
        }/*from  www. j a v a2  s  . c o  m*/
    });
    webview.setWebViewClient(new WebViewClient());
    webview.clearCache(true);
    webview.clearHistory();
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webview.loadUrl("https://getgpsserverrails-0.herokuapp.com/summaries");
}

From source file:bolts.WebViewAppLinkResolver.java

@Override
public Task<AppLink> getAppLinkFromUrlInBackground(final Uri url) {
    final Capture<String> content = new Capture<String>();
    final Capture<String> contentType = new Capture<String>();
    return Task.callInBackground(new Callable<Void>() {
        @Override//from  w  w w  . j a  va2 s. co m
        public Void call() throws Exception {
            URL currentURL = new URL(url.toString());
            URLConnection connection = null;
            while (currentURL != null) {
                // Fetch the content at the given URL.
                connection = currentURL.openConnection();
                if (connection instanceof HttpURLConnection) {
                    // Unfortunately, this doesn't actually follow redirects if they go from http->https,
                    // so we have to do that manually.
                    ((HttpURLConnection) connection).setInstanceFollowRedirects(true);
                }
                connection.setRequestProperty(PREFER_HEADER, META_TAG_PREFIX);
                connection.connect();

                if (connection instanceof HttpURLConnection) {
                    HttpURLConnection httpConnection = (HttpURLConnection) connection;
                    if (httpConnection.getResponseCode() >= 300 && httpConnection.getResponseCode() < 400) {
                        currentURL = new URL(httpConnection.getHeaderField("Location"));
                        httpConnection.disconnect();
                    } else {
                        currentURL = null;
                    }
                } else {
                    currentURL = null;
                }
            }

            try {
                content.set(readFromConnection(connection));
                contentType.set(connection.getContentType());
            } finally {
                if (connection instanceof HttpURLConnection) {
                    ((HttpURLConnection) connection).disconnect();
                }
            }
            return null;
        }
    }).onSuccessTask(new Continuation<Void, Task<JSONArray>>() {
        @Override
        public Task<JSONArray> then(Task<Void> task) throws Exception {
            // Load the content in a WebView and use JavaScript to extract the meta tags.
            final TaskCompletionSource<JSONArray> tcs = new TaskCompletionSource<>();
            final WebView webView = new WebView(context);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setNetworkAvailable(false);
            webView.setWebViewClient(new WebViewClient() {
                private boolean loaded = false;

                private void runJavaScript(WebView view) {
                    if (!loaded) {
                        // After the first resource has been loaded (which will be the pre-populated data)
                        // run the JavaScript meta tag extraction script
                        loaded = true;
                        view.loadUrl(TAG_EXTRACTION_JAVASCRIPT);
                    }
                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    runJavaScript(view);
                }

                @Override
                public void onLoadResource(WebView view, String url) {
                    super.onLoadResource(view, url);
                    runJavaScript(view);
                }
            });
            // Inject an object that will receive the JSON for the extracted JavaScript tags
            webView.addJavascriptInterface(new Object() {
                @JavascriptInterface
                public void setValue(String value) {
                    try {
                        tcs.trySetResult(new JSONArray(value));
                    } catch (JSONException e) {
                        tcs.trySetError(e);
                    }
                }
            }, "boltsWebViewAppLinkResolverResult");
            String inferredContentType = null;
            if (contentType.get() != null) {
                inferredContentType = contentType.get().split(";")[0];
            }
            webView.loadDataWithBaseURL(url.toString(), content.get(), inferredContentType, null, null);
            return tcs.getTask();
        }
    }, Task.UI_THREAD_EXECUTOR).onSuccess(new Continuation<JSONArray, AppLink>() {
        @Override
        public AppLink then(Task<JSONArray> task) throws Exception {
            Map<String, Object> alData = parseAlData(task.getResult());
            AppLink appLink = makeAppLinkFromAlData(alData, url);
            return appLink;
        }
    });
}

From source file:com.purchasingpower.inappbrowser.WebViewActivity.java

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

    //setContentView(R.layout.activity_main);
    setContentView(getResources().getIdentifier("nativebrowserplugin", "layout", getPackageName()));
    if (!checkPermission()) {
        requestPermission();/*from  w  ww. jav  a  2  s . co  m*/
    }
    Intent intent = getIntent();
    String url = intent.getExtras().getString("url");
    final WebView myWebView = (WebView) findViewById(
            getResources().getIdentifier("webview", "id", getPackageName()));
    myWebView.setWebViewClient(new MyWebViewClient());

    webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setAllowFileAccess(true);
    myWebView.setWebChromeClient(new PQChromeClient());
    //if SDK version is greater of 19 then activate hardware acceleration otherwise activate software acceleration
    if (Build.VERSION.SDK_INT >= 19) {
        myWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    } else if (Build.VERSION.SDK_INT >= 11 && Build.VERSION.SDK_INT < 19) {
        myWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    myWebView.loadUrl(url);

    back = (ImageView) findViewById(getResources().getIdentifier("back", "id", getPackageName()));
    back.setImageResource(getResources().getIdentifier("back_gray", "drawable", getPackageName()));
    forward = (ImageView) findViewById(getResources().getIdentifier("forward", "id", getPackageName()));
    forward.setImageResource(getResources().getIdentifier("forward_gray", "drawable", getPackageName()));
    done = (TextView) findViewById(getResources().getIdentifier("done", "id", getPackageName()));
    pbar = (ProgressBar) findViewById(getResources().getIdentifier("pbar", "id", getPackageName()));
    pbar.setVisibility(View.VISIBLE);
    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (myWebView.canGoBack()) {
                myWebView.goBack();
            }
        }
    });
    forward.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (myWebView.canGoForward()) {
                myWebView.goForward();
            }
        }
    });
    done.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.v("Done", "Clicked");
            finish();
        }
    });
}

From source file:com.github.ajasmin.telususageandroidwidget.ReportAccountErrorActivity.java

private void setupWebView() {
    // Load cached usage report in the WebView
    WebView webview = (WebView) findViewById(R.id.web_view);
    try {/*from   w w  w .  j av a  2s.  co m*/
        URL url = getFileStreamPath("" + appWidgetId).toURL();
        webview.loadUrl(url.toString());
    } catch (MalformedURLException e) {
        throw new Error(e);
    }

    // Prevent links in the WebView from being used
    webview.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return true;
        }
    });
}