List of usage examples for android.webkit WebResourceRequest getUrl
Uri getUrl();
From source file:de.baumann.hhsmoodle.helper.helper_webView.java
public static void webView_WebViewClient(final Activity from, final SwipeRefreshLayout swipeRefreshLayout, final WebView webView) { webView.setWebViewClient(new WebViewClient() { ProgressDialog progressDialog;//ww w. jav a 2 s . c o m int numb; public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); ViewPager viewPager = (ViewPager) from.findViewById(R.id.viewpager); SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(from); sharedPref.edit().putString("loadURL", webView.getUrl()).apply(); if (viewPager.getCurrentItem() == 0) { if (url != null) { from.setTitle(webView.getTitle()); } } if (url != null && url.contains("moodle.huebsch.ka.schule-bw.de/moodle/") && url.contains("/login/")) { if (viewPager.getCurrentItem() == 0 && numb != 1) { progressDialog = new ProgressDialog(from); progressDialog.setTitle(from.getString(R.string.login_title)); progressDialog.setMessage(from.getString(R.string.login_text)); progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, from.getString(R.string.toast_settings), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { helper_main.switchToActivity(from, Activity_settings.class, true); } }); progressDialog.show(); numb = 1; progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { numb = 0; } }); } } else if (progressDialog != null && progressDialog.isShowing()) { progressDialog.cancel(); numb = 0; } swipeRefreshLayout.setRefreshing(false); class_SecurePreferences sharedPrefSec = new class_SecurePreferences(from, "sharedPrefSec", "Ywn-YM.XK$b:/:&CsL8;=L,y4", true); String username = sharedPrefSec.getString("username"); String password = sharedPrefSec.getString("password"); final String js = "javascript:" + "document.getElementById('password').value = '" + password + "';" + "document.getElementById('username').value = '" + username + "';" + "var ans = document.getElementsByName('answer');" + "document.getElementById('loginbtn').click()"; if (Build.VERSION.SDK_INT >= 19) { view.evaluateJavascript(js, new ValueCallback<String>() { @Override public void onReceiveValue(String s) { } }); } else { view.loadUrl(js); } } @SuppressWarnings("deprecation") @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { final Uri uri = Uri.parse(url); return handleUri(uri); } @TargetApi(Build.VERSION_CODES.N) @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { final Uri uri = request.getUrl(); return handleUri(uri); } private boolean handleUri(final Uri uri) { Log.i(TAG, "Uri =" + uri); final String url = uri.toString(); // Based on some condition you need to determine if you are going to load the url // in your web view itself or in a browser. // You can use `host` or `scheme` or any part of the `uri` to decide. if (url.startsWith("http")) return false;//open web links as usual //try to find browse activity to handle uri Uri parsedUri = Uri.parse(url); PackageManager packageManager = from.getPackageManager(); Intent browseIntent = new Intent(Intent.ACTION_VIEW).setData(parsedUri); if (browseIntent.resolveActivity(packageManager) != null) { from.startActivity(browseIntent); return true; } //if not activity found, try to parse intent:// if (url.startsWith("intent:")) { try { Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); if (intent.resolveActivity(from.getPackageManager()) != null) { from.startActivity(intent); return true; } //try to find fallback url String fallbackUrl = intent.getStringExtra("browser_fallback_url"); if (fallbackUrl != null) { webView.loadUrl(fallbackUrl); return true; } //invite to install Intent marketIntent = new Intent(Intent.ACTION_VIEW) .setData(Uri.parse("market://details?id=" + intent.getPackage())); if (marketIntent.resolveActivity(packageManager) != null) { from.startActivity(marketIntent); return true; } } catch (URISyntaxException e) { //not an intent uri } } return true;//do nothing in other cases } }); }
From source file:de.baumann.browser.helper.helper_webView.java
public static void webView_WebViewClient(final Activity from, final SwipeRefreshLayout swipeRefreshLayout, final WebView webView, final EditText editText) { webView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); swipeRefreshLayout.setRefreshing(false); editText.setText(webView.getTitle()); if (webView.getTitle() != null && !webView.getTitle().equals("about:blank")) { try { final Database_History db = new Database_History(from); db.addBookmark(webView.getTitle(), webView.getUrl()); db.close();//from w w w . j a v a2s . c o m } catch (Exception e) { e.printStackTrace(); } } } @SuppressWarnings("deprecation") @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { final Uri uri = Uri.parse(url); return handleUri(uri); } @TargetApi(Build.VERSION_CODES.N) @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { final Uri uri = request.getUrl(); return handleUri(uri); } private boolean handleUri(final Uri uri) { Log.i(TAG, "Uri =" + uri); final String url = uri.toString(); // Based on some condition you need to determine if you are going to load the url // in your web view itself or in a browser. // You can use `host` or `scheme` or any part of the `uri` to decide. if (url.startsWith("http")) return false;//open web links as usual //try to find browse activity to handle uri Uri parsedUri = Uri.parse(url); PackageManager packageManager = from.getPackageManager(); Intent browseIntent = new Intent(Intent.ACTION_VIEW).setData(parsedUri); if (browseIntent.resolveActivity(packageManager) != null) { from.startActivity(browseIntent); return true; } //if not activity found, try to parse intent:// if (url.startsWith("intent:")) { try { Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); if (intent.resolveActivity(from.getPackageManager()) != null) { from.startActivity(intent); return true; } //try to find fallback url String fallbackUrl = intent.getStringExtra("browser_fallback_url"); if (fallbackUrl != null) { webView.loadUrl(fallbackUrl); return true; } //invite to install Intent marketIntent = new Intent(Intent.ACTION_VIEW) .setData(Uri.parse("market://details?id=" + intent.getPackage())); if (marketIntent.resolveActivity(packageManager) != null) { from.startActivity(marketIntent); return true; } } catch (URISyntaxException e) { //not an intent uri } } return true;//do nothing in other cases } }); }
From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.webview.GBWebClient.java
@TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override//from w w w. j av a 2 s .com public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { Uri parsedUri = request.getUrl(); LOG.debug("WEBVIEW shouldInterceptRequest URL: " + parsedUri.toString()); WebResourceResponse mimickedReply = mimicReply(parsedUri); if (mimickedReply != null) return mimickedReply; return super.shouldInterceptRequest(view, request); }
From source file:uk.ac.hutton.ics.buntata.fragment.EulaFragment.java
private void showEulaForType(EulaUtils.EulaType type) { this.type = type; webView.setVisibility(View.VISIBLE); int resource = -1; switch (type) { case COMMERCIAL: resource = R.raw.eula_commercial; break;/*from ww w . ja va2 s . co m*/ case CONSUMER: resource = R.raw.eula_consumer; break; } String prompt = ""; try { InputStream inputStream = getResources().openRawResource(resource); byte[] buffer = new byte[inputStream.available()]; inputStream.read(buffer); prompt = new String(buffer); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } webView.loadDataWithBaseURL(null, prompt, "text/html", "utf-8", null); webView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { buttonBar.setVisibility(View.VISIBLE); } @SuppressWarnings("deprecation") @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return handleUri(url); } @TargetApi(Build.VERSION_CODES.N) @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { return handleUri(request.getUrl().toString()); } private boolean handleUri(String url) { if (url.startsWith("http:") || url.startsWith("https:")) { return false; } try { // Otherwise allow the OS to handle things like tel, mailto, etc. Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); getActivity().startActivity(intent); return true; } catch (ActivityNotFoundException e) { return false; } } }); }
From source file:com.microsoft.azure.engagement.fragment.FeaturesFragment.java
@SuppressLint("SetJavaScriptEnabled") @Nullable/*from w ww . ja va2s . c om*/ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View view = inflater.inflate(R.layout.fragment_features, container, false); progressBar = view.findViewById(R.id.progressBar); errorContainer = view.findViewById(R.id.errorContainer); retryButton = view.findViewById(R.id.retryButton); retryButton.setOnClickListener(this); webView = (WebView) view.findViewById(R.id.webView); webView.setWebViewClient(new WebViewClient() { @TargetApi(android.os.Build.VERSION_CODES.M) @Override public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError err) { onReceivedError(view, err.getErrorCode(), err.getDescription().toString(), req.getUrl().toString()); } @SuppressWarnings("deprecation") @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { webViewError = true; } @Override public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) { super.onReceivedHttpError(view, request, errorResponse); webViewError = true; } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); progressBar.setVisibility(View.GONE); webView.setVisibility(webViewError ? View.GONE : View.VISIBLE); errorContainer.setVisibility(webViewError ? View.VISIBLE : View.GONE); } }); webView.getSettings().setJavaScriptEnabled(true); displayLoadingView(); webView.loadUrl(getString(R.string.drawer_features_url)); AzmeTracker.startActivity(getActivity(), "features"); return view; }
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.ja v a2 s . 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:org.freespanish.diccionario.fragments.search.SearchFragment.java
@Override public void setupViews() { this.searchPresenter = new SearchPresenterImpl(this); this.searchWebview.setWebViewClient(new WebViewClient() { void handleLink(String url) { url = url.substring(url.lastIndexOf("/") + 1, url.length()); SearchFragment.this.searchPresenter.prepareDefinition(url, true); }/*from www. j av a2s . co m*/ @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); setProgressBarVisibility(View.GONE); } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.contains("fetch")) handleLink(url); return true; } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { if (request.getUrl().toString().contains("fetch")) handleLink(request.getUrl().toString()); return true; } }); if (pendingWordToSearch != null) { this.searchInfoLayout.setVisibility(View.GONE); onWordSearched(pendingWordToSearch); pendingWordToSearch = null; } }
From source file:felixwiemuth.lincal.ui.HtmlDialogFragment.java
@NonNull @SuppressLint("InflateParams") @Override/* w w w . ja v a2 s. c o m*/ public Dialog onCreateDialog(Bundle savedInstanceState) { View content = LayoutInflater.from(getActivity()).inflate(R.layout.html_dialog_fragment, null); webView = (WebView) content.findViewById(R.id.html_dialog_fragment_web_view); // Set the WebViewClient (in API <24 have to parse URI manually) if (Build.VERSION.SDK_INT >= 24) { webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest webResourceRequest) { Uri uri = webResourceRequest.getUrl(); // @TargetApi(Build.VERSION_CODES.N_MR1) return HtmlDialogFragment.this.loadUrl(webView, uri); } }); } else { //TODO test on an API < 24 device webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView webView, String url) { Uri uri = Uri.parse(url); return HtmlDialogFragment.this.loadUrl(webView, uri); } }); } indeterminateProgress = (ProgressBar) content .findViewById(R.id.html_dialog_fragment_indeterminate_progress); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); Bundle arguments = getArguments(); // if argument for title is given (string or int referencing a string resource) set the title if (arguments.getString(ARG_TITLE) != null) { builder.setTitle(arguments.getString(ARG_TITLE)); } else { builder.setTitle(getArguments().getInt(ARG_TITLE)); //TODO error handling } builder.setView(content); return builder.create(); }
From source file:ph.com.globe.connect.AuthenticationActivity.java
/** * On activity create process.//from w w w . ja v a2 s. c om * * @param savedInstanceState instance state */ @Override public void onCreate(Bundle savedInstanceState) { // call base create super.onCreate(savedInstanceState); // initialize web view final WebView webview = new WebView(this); // set content view of the activity setContentView(webview); // get the app id from intent final String appId = getIntent().getStringExtra("app_id"); // get the app secret from intent final String appSecret = getIntent().getStringExtra("app_secret"); // set web view client webview.setWebViewClient(new WebViewClient() { /** * Let's catch all url changes. * * @param view current view * @param url current url * @return boolean */ @SuppressWarnings("deprecation") @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // parse the uri final Uri uri = Uri.parse(url); // are we still on globe labs? if (uri.toString().indexOf(ROOT_URL) != 0) { // get the code String code = uri.getQueryParameter("code"); // get access token request try { getAccessToken(appId, appSecret, code); } catch (ApiException | HttpRequestException e) { e.printStackTrace(); } return false; } // load uri view.loadUrl(uri.toString()); return false; } /** * Let's catch all url changes. * * @param view current view * @param request web resource request * @return boolean */ @TargetApi(Build.VERSION_CODES.N) @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { // parse the uri final Uri uri = Uri.parse(request.getUrl().toString()); // are we still on globe labs? if (uri.toString().indexOf(ROOT_URL) != 0) { // get the code String code = uri.getQueryParameter("code"); // get access token request try { getAccessToken(appId, appSecret, code); } catch (ApiException | HttpRequestException e) { e.printStackTrace(); } return false; } // load uri view.loadUrl(uri.toString()); return false; } }); try { // set dialog url String DIALOG_URL = this.buildUrl(this.DIALOG_URL, appId); // load the url webview.loadUrl(DIALOG_URL); } catch (ApiException e) { e.printStackTrace(); } }
From source file:com.taobao.luaview.view.LVWebView.java
@TargetApi(Build.VERSION_CODES.M) private void setupDefaultWebViewClient() { if (mWebView != null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { mWebView.setWebViewClient(new WebViewClient() { @Override/*from www.j ava 2s.co m*/ public boolean shouldOverrideUrlLoading(WebView view, String url) { return false; // ???? } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); mIsLoading = true; if (LuaUtil.isTable(mLuaUserdata.getCallback())) { LuaUtil.callFunction(LuaUtil.getFunction(mLuaUserdata.getCallback(), "onPageStarted")); } } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); mIsLoading = false; if (LuaUtil.isTable(mLuaUserdata.getCallback())) { LuaUtil.callFunction(LuaUtil.getFunction(mLuaUserdata.getCallback(), "onPageFinished")); } if (LVWebView.this.isRefreshing()) { LVWebView.this.setRefreshing(false); } } @Override public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); mIsLoading = false; if (LuaUtil.isTable(mLuaUserdata.getCallback())) { LuaUtil.callFunction(LuaUtil.getFunction(mLuaUserdata.getCallback(), "onReceivedError"), valueOf(error.getErrorCode()), valueOf(String.valueOf(error.getDescription())), valueOf(String.valueOf(request.getUrl()))); } if (LVWebView.this.isRefreshing()) { LVWebView.this.setRefreshing(false); } } }); } else { mWebView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return false; // ???? } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); mIsLoading = true; if (LuaUtil.isTable(mLuaUserdata.getCallback())) { LuaUtil.callFunction(LuaUtil.getFunction(mLuaUserdata.getCallback(), "onPageStarted")); } } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); mIsLoading = false; if (LuaUtil.isTable(mLuaUserdata.getCallback())) { LuaUtil.callFunction(LuaUtil.getFunction(mLuaUserdata.getCallback(), "onPageFinished")); } if (LVWebView.this.isRefreshing()) { LVWebView.this.setRefreshing(false); } } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { super.onReceivedError(view, errorCode, description, failingUrl); mIsLoading = false; if (LuaUtil.isTable(mLuaUserdata.getCallback())) { LuaUtil.callFunction(LuaUtil.getFunction(mLuaUserdata.getCallback(), "onReceivedError"), errorCode, description, failingUrl); } if (LVWebView.this.isRefreshing()) { LVWebView.this.setRefreshing(false); } } }); } } }