Search in sources :

Example 71 with WebViewClient

use of android.webkit.WebViewClient in project Osmand by osmandapp.

the class HelpArticleDialogFragment method onCreateView.

@SuppressLint("SetJavaScriptEnabled")
@NonNull
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_help_article, container, false);
    Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
    toolbar.setNavigationContentDescription(R.string.access_shared_string_navigate_up);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            dismiss();
        }
    });
    String name = getArguments().getString(NAME);
    if (name != null) {
        toolbar.setTitle(name);
    }
    String assetName = getArguments().getString(ASSET_NAME);
    String url = getArguments().getString(URL);
    webView = (WebView) view.findViewById(R.id.webView);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setDisplayZoomControls(false);
    webView.getSettings().setSupportZoom(true);
    // Scale web view font size with system font size
    float scale = getActivity().getResources().getConfiguration().fontScale;
    if (android.os.Build.VERSION.SDK_INT >= 14) {
        webView.getSettings().setTextZoom((int) (scale * 100f));
    } else {
        if (scale <= 0.7f) {
            webView.getSettings().setTextSize(WebSettings.TextSize.SMALLEST);
        } else if (scale <= 0.85f) {
            webView.getSettings().setTextSize(WebSettings.TextSize.SMALLER);
        } else if (scale <= 1.0f) {
            webView.getSettings().setTextSize(WebSettings.TextSize.NORMAL);
        } else if (scale <= 1.15f) {
            webView.getSettings().setTextSize(WebSettings.TextSize.LARGER);
        } else {
            webView.getSettings().setTextSize(WebSettings.TextSize.LARGEST);
        }
    }
    if (assetName != null) {
        String fileContents = getAssetAsString(assetName, getActivity());
        StringBuilder sb = new StringBuilder();
        sb.append(HEADER_INNER);
        sb.append(fileContents);
        sb.append(FOOTER_INNER);
        webView.loadDataWithBaseURL("https://osmand.net", sb.toString(), null, "utf-8", null);
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("https://osmand.net/features?id=")) {
                    String id = url.substring("https://osmand.net/features?id=".length());
                    dismiss();
                    instantiateWithAsset("feature_articles/" + id + ".html", getString(R.string.shared_string_help)).show(getActivity().getSupportFragmentManager(), "DIALOG_HELP_ARTICLE");
                }
                return false;
            }
        });
    } else if (url != null) {
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        if (savedInstanceState != null) {
            webView.restoreState(savedInstanceState);
        } else {
            webView.setWebViewClient(new WebViewClient() {

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }
            });
            webView.loadUrl(url);
        }
    } else {
        throw new IllegalArgumentException("HelpArticleDialogFragment should be " + "instantiated either with ASSET_NAME or with URL");
    }
    return view;
}
Also used : WebView(android.webkit.WebView) View(android.view.View) WebView(android.webkit.WebView) Toolbar(android.support.v7.widget.Toolbar) WebViewClient(android.webkit.WebViewClient) NonNull(android.support.annotation.NonNull) SuppressLint(android.annotation.SuppressLint)

Example 72 with WebViewClient

use of android.webkit.WebViewClient in project Osmand by osmandapp.

the class PrintDialogActivity method openFile.

private void openFile(Uri uri) {
    webView.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            createWebPrintJob(view);
        }
    });
    webView.loadUrl(uri.toString());
}
Also used : WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient)

Example 73 with WebViewClient

use of android.webkit.WebViewClient in project Osmand by osmandapp.

the class AbstractCard method openUrl.

@SuppressLint("SetJavaScriptEnabled")
@SuppressWarnings("deprecation")
protected static void openUrl(@NonNull Context ctx, @NonNull OsmandApplication app, @Nullable String title, @NonNull String url, boolean externalLink, boolean hasImageUrl) {
    if (externalLink) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        ctx.startActivity(intent);
        return;
    }
    final Dialog dialog = new Dialog(ctx, app.getSettings().isLightContent() ? R.style.OsmandLightTheme : R.style.OsmandDarkTheme);
    LinearLayout ll = new LinearLayout(ctx);
    ll.setOrientation(LinearLayout.VERTICAL);
    final Toolbar topBar = new Toolbar(ctx);
    topBar.setClickable(true);
    Drawable back = app.getIconsCache().getIcon(R.drawable.ic_action_remove_dark);
    topBar.setNavigationIcon(back);
    topBar.setNavigationContentDescription(R.string.shared_string_close);
    topBar.setTitle(title);
    topBar.setBackgroundColor(ContextCompat.getColor(ctx, getResIdFromAttribute(ctx, R.attr.pstsTabBackground)));
    topBar.setTitleTextColor(ContextCompat.getColor(ctx, getResIdFromAttribute(ctx, R.attr.pstsTextColor)));
    topBar.setNavigationOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(final View v) {
            dialog.dismiss();
        }
    });
    final WebView wv = new WebView(ctx);
    wv.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    });
    WebSettings settings = wv.getSettings();
    if (hasImageUrl) {
        settings.setDefaultTextEncodingName("utf-8");
        settings.setBuiltInZoomControls(true);
        settings.setDisplayZoomControls(false);
        settings.setSupportZoom(true);
    }
    /*
		//Scale web view font size with system font size
		float scale = ctx.getResources().getConfiguration().fontScale;
		if (android.os.Build.VERSION.SDK_INT >= 14) {
			settings.setTextZoom((int) (scale * 100f));
		} else {
			if (scale <= 0.7f) {
				settings.setTextSize(WebSettings.TextSize.SMALLEST);
			} else if (scale <= 0.85f) {
				settings.setTextSize(WebSettings.TextSize.SMALLER);
			} else if (scale <= 1.0f) {
				settings.setTextSize(WebSettings.TextSize.NORMAL);
			} else if (scale <= 1.15f) {
				settings.setTextSize(WebSettings.TextSize.LARGER);
			} else {
				settings.setTextSize(WebSettings.TextSize.LARGEST);
			}
		}
*/
    wv.setBackgroundColor(Color.argb(1, 0, 0, 0));
    // wv.setScrollContainer(false);
    wv.getSettings().setJavaScriptEnabled(true);
    if (hasImageUrl) {
        wv.loadData("<html><body style='margin:0;padding:0'><img style='max-width:100%;max-height:100%;' src='" + url + "'/></body></html>", "text/html", "UTF-8");
    } else {
        wv.loadUrl(url);
    }
    ll.addView(topBar);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
    lp.weight = 1;
    ll.addView(wv, lp);
    dialog.setContentView(ll);
    /*
		wv.setFocusable(true);
		wv.setFocusableInTouchMode(true);
		wv.requestFocus(View.FOCUS_DOWN);
		wv.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
					case MotionEvent.ACTION_DOWN:
					case MotionEvent.ACTION_UP:
						if (!v.hasFocus()) {
							v.requestFocus();
						}
						break;
				}
				return false;
			}
		});
		*/
    dialog.setCancelable(true);
    dialog.show();
}
Also used : Drawable(android.graphics.drawable.Drawable) Intent(android.content.Intent) View(android.view.View) WebView(android.webkit.WebView) Dialog(android.app.Dialog) WebSettings(android.webkit.WebSettings) WebView(android.webkit.WebView) LinearLayout(android.widget.LinearLayout) Toolbar(android.support.v7.widget.Toolbar) WebViewClient(android.webkit.WebViewClient) SuppressLint(android.annotation.SuppressLint)

Example 74 with WebViewClient

use of android.webkit.WebViewClient in project ttdj by soonphe.

the class BCWXWapPaymentActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 根布局参数
    LinearLayout.LayoutParams layoutParamsRoot = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    // 根布局
    LinearLayout layoutRoot = new LinearLayout(this);
    layoutRoot.setLayoutParams(layoutParamsRoot);
    layoutRoot.setOrientation(LinearLayout.VERTICAL);
    layoutRoot.setBackgroundColor(0xFFF5F5F5);
    webView = new WebView(this);
    LinearLayout.LayoutParams webViewLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    layoutRoot.addView(webView, webViewLayoutParams);
    setContentView(layoutRoot);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    // 设置可以支持缩放
    webSettings.setSupportZoom(true);
    webSettings.setUseWideViewPort(true);
    // 设置默认加载的可视范围是大视野范围
    webSettings.setLoadWithOverviewMode(true);
    // 设置出现缩放工具
    webSettings.setBuiltInZoomControls(true);
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // 如下方案可在非微信内部WebView的H5页面中调出微信支付
            if (url.startsWith("weixin://wap/pay?")) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);
                return true;
            }
            return super.shouldOverrideUrlLoading(view, url);
        }
    });
    url = getIntent().getStringExtra("url");
    webView.loadUrl(url);
}
Also used : WebSettings(android.webkit.WebSettings) Intent(android.content.Intent) WebView(android.webkit.WebView) LinearLayout(android.widget.LinearLayout) WebViewClient(android.webkit.WebViewClient)

Example 75 with WebViewClient

use of android.webkit.WebViewClient in project DMGameApp by xiaohaibin.

the class WebDetailsActivity method initWeb.

@SuppressLint("SetJavaScriptEnabled")
private void initWeb() {
    WebSettings settings = webView.getSettings();
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    });
    webView.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (progressBar == null) {
                return;
            }
            if (newProgress == 100) {
                progressBar.setVisibility(View.GONE);
            } else {
                if (progressBar.getVisibility() == View.GONE) {
                    progressBar.setVisibility(View.VISIBLE);
                }
                progressBar.setProgress(newProgress);
            }
        }
    });
    settings.setDefaultTextEncodingName("utf-8");
    settings.setJavaScriptEnabled(true);
    settings.setSupportZoom(true);
    settings.setBuiltInZoomControls(true);
    settings.setUseWideViewPort(true);
    settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    settings.setLoadWithOverviewMode(true);
    // 隐藏缩放控件
    settings.setDisplayZoomControls(false);
    // 解决HTTPS协议下出现的mixed content问题
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
    }
    settings.setCacheMode(WebSettings.LOAD_DEFAULT);
    settings.setDomStorageEnabled(true);
    settings.setDatabaseEnabled(true);
    settings.setAppCachePath(getCacheDir().getPath());
    settings.setAppCacheEnabled(true);
    showWebData();
}
Also used : WebSettings(android.webkit.WebSettings) WebChromeClient(android.webkit.WebChromeClient) WebView(android.webkit.WebView) SuppressLint(android.annotation.SuppressLint) WebViewClient(android.webkit.WebViewClient) SuppressLint(android.annotation.SuppressLint)

Aggregations

WebViewClient (android.webkit.WebViewClient)247 WebView (android.webkit.WebView)229 WebSettings (android.webkit.WebSettings)77 View (android.view.View)71 WebChromeClient (android.webkit.WebChromeClient)69 SuppressLint (android.annotation.SuppressLint)61 Intent (android.content.Intent)60 Bitmap (android.graphics.Bitmap)59 WebResourceRequest (android.webkit.WebResourceRequest)35 TextView (android.widget.TextView)26 LinearLayout (android.widget.LinearLayout)22 WebResourceError (android.webkit.WebResourceError)21 WebResourceResponse (android.webkit.WebResourceResponse)16 Uri (android.net.Uri)15 SslError (android.net.http.SslError)13 Bundle (android.os.Bundle)13 Toolbar (android.support.v7.widget.Toolbar)13 KeyEvent (android.view.KeyEvent)13 SslErrorHandler (android.webkit.SslErrorHandler)13 TargetApi (android.annotation.TargetApi)12