use of android.webkit.WebResourceRequest in project WeexErosFramework by bmfe.
the class BMWXWebView method initWebView.
private void initWebView(WebView wv) {
WebSettings settings = wv.getSettings();
settings.setAppCacheEnabled(true);
settings.setUseWideViewPort(true);
settings.setDomStorageEnabled(true);
settings.setSupportZoom(false);
settings.setBuiltInZoomControls(false);
addWebJavascriptInterface(wv);
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());
}
}
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
mContentHeight = Integer.parseInt(message);
mContentHeight = mContentHeight / (WXViewUtils.defaultPixelScaleFactor(getView().getContext()) * 2);
Map<String, String> params = new HashMap<>();
params.put("contentHeight", mContentHeight + "");
if (mWXWeb != null) {
mWXWeb.fireEvent("bmPageFinish", params);
}
result.confirm();
return true;
}
});
}
use of android.webkit.WebResourceRequest in project PocketHub by pockethub.
the class LoginWebViewActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
WebView.setWebContentsDebuggingEnabled(true);
// 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);
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);
}
use of android.webkit.WebResourceRequest in project LiYuJapanese by 54wall.
the class WebViewActivity method initWebView.
private void initWebView() {
webView.setWebChromeClient(new MyWebChromeClient(this));
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
webView.loadUrl(url);
progressBar.setVisibility(View.GONE);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
});
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
webView.setHorizontalScrollBarEnabled(false);
webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getPointerCount() > 1) {
// Multi touch detected
return true;
}
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
{
// save the x
m_downX = event.getX();
}
break;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
{
// set x so that it doesn't move
event.setLocation(m_downX, event.getY());
}
break;
}
return false;
}
});
}
use of android.webkit.WebResourceRequest in project WordPress-Android by wordpress-mobile.
the class EditorWebViewAbstract method configureWebView.
@SuppressLint("SetJavaScriptEnabled")
private void configureWebView() {
WebSettings webSettings = this.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDefaultTextEncodingName("utf-8");
this.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("callback") && mJsCallbackReceiver != null) {
String data = URLDecoder.decode(url);
String[] split = data.split(":", 2);
String callbackId = split[0];
String params = (split.length > 1 ? split[1] : "");
mJsCallbackReceiver.executeCallback(callbackId, params);
}
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
AppLog.e(T.EDITOR, description);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
if (!URLUtil.isNetworkUrl(url)) {
return super.shouldInterceptRequest(view, request);
}
// Request and add an authorization header for HTTPS resource requests.
// Use https:// when requesting the auth header, in case the resource is incorrectly using http://.
// If an auth header is returned, force https:// for the actual HTTP request.
String authHeader = mAuthHeaderRequestListener.onAuthHeaderRequested(UrlUtils.makeHttps(url));
if (StringUtils.notNullStr(authHeader).length() > 0) {
try {
url = UrlUtils.makeHttps(url);
// Keep any existing request headers from the WebResourceRequest
Map<String, String> headerMap = request.getRequestHeaders();
for (Map.Entry<String, String> entry : mHeaderMap.entrySet()) {
headerMap.put(entry.getKey(), entry.getValue());
}
headerMap.put("Authorization", authHeader);
HttpURLConnection conn = HTTPUtils.setupUrlConnection(url, headerMap);
return new WebResourceResponse(conn.getContentType(), conn.getContentEncoding(), conn.getInputStream());
} catch (IOException e) {
AppLog.e(T.EDITOR, e);
}
}
return super.shouldInterceptRequest(view, request);
}
/**
* Compatibility method for API < 21
*/
@SuppressWarnings("deprecation")
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
if (!URLUtil.isNetworkUrl(url)) {
return super.shouldInterceptRequest(view, url);
}
// Request and add an authorization header for HTTPS resource requests.
// Use https:// when requesting the auth header, in case the resource is incorrectly using http://.
// If an auth header is returned, force https:// for the actual HTTP request.
String authHeader = mAuthHeaderRequestListener.onAuthHeaderRequested(UrlUtils.makeHttps(url));
if (StringUtils.notNullStr(authHeader).length() > 0) {
try {
url = UrlUtils.makeHttps(url);
Map<String, String> headerMap = new HashMap<>(mHeaderMap);
headerMap.put("Authorization", authHeader);
HttpURLConnection conn = HTTPUtils.setupUrlConnection(url, headerMap);
return new WebResourceResponse(conn.getContentType(), conn.getContentEncoding(), conn.getInputStream());
} catch (IOException e) {
AppLog.e(T.EDITOR, e);
}
}
return super.shouldInterceptRequest(view, url);
}
});
this.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onConsoleMessage(@NonNull ConsoleMessage cm) {
if (cm.messageLevel() == MessageLevel.ERROR) {
if (mErrorListener != null) {
mErrorListener.onJavaScriptError(cm.sourceId(), cm.lineNumber(), cm.message());
}
AppLog.e(T.EDITOR, cm.message() + " -- From line " + cm.lineNumber() + " of " + cm.sourceId());
} else {
AppLog.d(T.EDITOR, cm.message() + " -- From line " + cm.lineNumber() + " of " + cm.sourceId());
}
return true;
}
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
AppLog.d(T.EDITOR, message);
if (mErrorListener != null) {
mErrorListener.onJavaScriptAlert(url, message);
}
return true;
}
});
}
use of android.webkit.WebResourceRequest in project Palm300Heroes by nicolite.
the class WebViewActivity method initWebView.
private void initWebView() {
settings = webView.getSettings();
settings.setLoadWithOverviewMode(true);
// 使页面支持缩放
settings.setBuiltInZoomControls(true);
// 支持缩放
settings.setSupportZoom(true);
// 缩放按钮
settings.setDisplayZoomControls(false);
// 支持自动加载图片
settings.setLoadsImagesAutomatically(true);
// 开启DOM
settings.setDomStorageEnabled(true);
// 关闭webview中缓存
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setAppCacheEnabled(true);
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
settings.setDefaultTextEncodingName("UTF-8");
// 将图片调整到适合webview的大小
settings.setUseWideViewPort(true);
// //当webview调用requestFocus时为webview设置节点
settings.setNeedInitialFocus(true);
// 允许访问文件
settings.setAllowFileAccess(true);
// 支持缩放
settings.setSupportZoom(true);
settings.setPluginState(WebSettings.PluginState.ON);
webView.setFocusable(true);
webView.setFocusableInTouchMode(true);
webView.addJavascriptInterface(new JsInject(this), "imageListener");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String s) {
if (s.startsWith("http") || s.startsWith("https")) {
webView.loadUrl(s);
} else if (s.startsWith("intent")) {
try {
Intent intent = Intent.parseUri(s, Intent.URI_INTENT_SCHEME);
intent.addCategory("android.intent.category.BROWSABLE");
intent.setComponent(null);
intent.setSelector(null);
List<ResolveInfo> resolves = context.getPackageManager().queryIntentActivities(intent, 0);
if (resolves.size() > 0) {
startActivityIfNeeded(intent, -1);
}
return true;
} catch (URISyntaxException e) {
e.printStackTrace();
}
} else {
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(s));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
return super.shouldOverrideUrlLoading(webView, s);
}
@Override
public void onPageStarted(WebView webView, String s, Bitmap bitmap) {
super.onPageStarted(webView, s, bitmap);
progressBar.setProgress(20);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (view != null && toolbarTitle != null) {
String title = view.getTitle();
if (!TextUtils.isEmpty(title) && !title.contains("about:blank")) {
toolbarTitle.setText(title);
}
}
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
});
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView webView, int i) {
super.onProgressChanged(webView, i);
if (progressBar != null) {
if (i == 100) {
progressBar.setVisibility(View.GONE);
if (type != TYPE_VIDEO && type != TYPE_TY_CARTOON && type != TYPE_THEME_MUSIC) {
addImgClickListener();
}
if (type == TYPE_THEME_MUSIC) {
changThemeMusicDom();
}
} else {
if (View.GONE == progressBar.getVisibility()) {
progressBar.setVisibility(View.VISIBLE);
}
if (i <= 20) {
progressBar.setProgress(20);
} else {
progressBar.setProgress(i);
}
}
}
}
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
if (!TextUtils.isEmpty(title) && !title.contains("about:blank")) {
toolbarTitle.setText(title);
}
}
});
}
Aggregations