use of android.webkit.WebResourceError in project router-companion-android by rm3l.
the class OpenWebManagementPageActivity method getWebClient.
@Override
protected WebViewClient getWebClient() {
return new WebViewClient() {
@Override
public void onPageCommitVisible(WebView view, String url) {
mSwipeRefreshLayout.setRefreshing(false);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
mSwipeRefreshLayout.setRefreshing(true);
OpenWebManagementPageActivity.this.mSavedUrl = url;
}
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
final int errorCode = error.getErrorCode();
final CharSequence description = error.getDescription();
FirebaseCrashlytics.getInstance().log("GOT Page error : code : " + errorCode + ", Desc : " + description);
showError(OpenWebManagementPageActivity.this, errorCode);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Utils.reportException(null, new WebException(("Error: " + failingUrl + " - errorCode=" + errorCode + ": " + description)));
Toast.makeText(OpenWebManagementPageActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
@Override
public void onReceivedHttpAuthRequest(WebView view, final HttpAuthHandler handler, String host, String realm) {
runOnUiThread(new Runnable() {
@Override
public void run() {
final LayoutInflater layoutInflater = getLayoutInflater();
// If the current SSH authentication method is 'Password' , use that,
// otherwise, ask user to supply such info
String password = null;
if (SSHAuthenticationMethod.PASSWORD.equals(mRouter.getSshAuthenticationMethod())) {
password = mRouter.getPasswordPlain();
}
final View mAuthPromptDialogview = layoutInflater.inflate(R.layout.activity_open_web_mgmt_interface_http_auth_prompt, null);
final ScrollView contentScrollView = (ScrollView) mAuthPromptDialogview.findViewById(R.id.http_auth_prompt_scrollview);
final EditText usernamePrompt = (EditText) mAuthPromptDialogview.findViewById(R.id.http_auth_prompt_username);
final EditText passwordPrompt = (EditText) mAuthPromptDialogview.findViewById(R.id.http_auth_prompt_password);
passwordPrompt.setText(password, TextView.BufferType.EDITABLE);
final CheckBox showPasswordPrompt = (CheckBox) mAuthPromptDialogview.findViewById(R.id.http_auth_prompt_password_show_checkbox);
showPasswordPrompt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked) {
passwordPrompt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
Utils.scrollToView(contentScrollView, passwordPrompt);
passwordPrompt.requestFocus();
} else {
passwordPrompt.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
Utils.scrollToView(contentScrollView, passwordPrompt);
passwordPrompt.requestFocus();
}
passwordPrompt.setSelection(passwordPrompt.length());
}
});
final AlertDialog.Builder httpBasicAuthCredsDialogPrompt = new AlertDialog.Builder(OpenWebManagementPageActivity.this).setTitle(mUrl).setView(mAuthPromptDialogview).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.cancel();
}
}).setPositiveButton("Go", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final String httpUser = usernamePrompt.getText().toString();
final String httpPassword = passwordPrompt.getText().toString();
//
// mWebview.setHttpAuthUsernamePassword(mUrl,
//
// Strings.nullToEmpty(mRealm), httpUser, httpPassword);
handler.proceed(httpUser, httpPassword);
// mWebview.loadUrl(mUrl);
//
// mLoadingView.setVisibility(View.GONE);
}
});
httpBasicAuthCredsDialogPrompt.create().show();
}
});
}
@Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, final SslError error) {
// Case of self-signed certificates
runOnUiThread(new Runnable() {
@Override
public void run() {
final AlertDialog.Builder builder = new AlertDialog.Builder(OpenWebManagementPageActivity.this);
builder.setMessage(getString(R.string.notification_error_ssl_cert_invalid, error.getUrl()));
builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.proceed();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.cancel();
}
});
final AlertDialog dialog = builder.create();
dialog.show();
}
});
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// https://fabric.io/lemra-inc2/android/apps/org.rm3l.ddwrt/issues/568283d6f5d3a7f76bb8f2dc
if (url == null) {
return false;
}
return false;
// if (Uri.parse(url).getHost().endsWith("rm3l.org")) {
// // This is my web site, so do not override; let my WebView
// load the page
// return false;
// } else if (url.startsWith("mailto:")){
// startActivity(new Intent(Intent.ACTION_SENDTO,
// Uri.parse(url)));
// return true;
// }
// // Otherwise, the link is not for a page on my site, so launch
// another Activity that handles URLs
// final Intent intent = new Intent(Intent.ACTION_VIEW,
// Uri.parse(url));
// startActivity(intent);
// return true;
}
private void showError(Context mContext, int errorCode) {
// Prepare message
String message = null;
String title = null;
if (errorCode == WebViewClient.ERROR_AUTHENTICATION) {
message = "User authentication failed on server";
title = "Auth Error";
} else if (errorCode == WebViewClient.ERROR_TIMEOUT) {
message = "The server is taking too much time to communicate. Try again later.";
title = "Connection Timeout";
} else if (errorCode == WebViewClient.ERROR_TOO_MANY_REQUESTS) {
message = "Too many requests during this load";
title = "Too Many Requests";
} else if (errorCode == WebViewClient.ERROR_UNKNOWN) {
message = "Generic error";
title = "Unknown Error";
} else if (errorCode == WebViewClient.ERROR_BAD_URL) {
message = "Check entered URL..";
title = "Malformed URL";
} else if (errorCode == WebViewClient.ERROR_CONNECT) {
message = "Failed to connect to the server";
title = "Connection";
} else if (errorCode == WebViewClient.ERROR_FAILED_SSL_HANDSHAKE) {
message = "Failed to perform SSL handshake";
title = "SSL Handshake Failed";
} else if (errorCode == WebViewClient.ERROR_HOST_LOOKUP) {
message = "Server or proxy hostname lookup failed";
title = "Host Lookup Error";
} else if (errorCode == WebViewClient.ERROR_PROXY_AUTHENTICATION) {
message = "User authentication failed on proxy";
title = "Proxy Auth Error";
} else if (errorCode == WebViewClient.ERROR_REDIRECT_LOOP) {
message = "Too many redirects";
title = "Redirect Loop Error";
} else if (errorCode == WebViewClient.ERROR_UNSUPPORTED_AUTH_SCHEME) {
message = "Unsupported authentication scheme (not basic or digest)";
title = "Auth Scheme Error";
} else if (errorCode == WebViewClient.ERROR_UNSUPPORTED_SCHEME) {
message = "Unsupported URI scheme";
title = "URI Scheme Error";
} else if (errorCode == WebViewClient.ERROR_FILE) {
message = "Generic file error";
title = "File";
} else if (errorCode == WebViewClient.ERROR_FILE_NOT_FOUND) {
message = "File not found";
title = "File";
} else if (errorCode == WebViewClient.ERROR_IO) {
message = "The server failed to communicate. Try again later.";
title = "IO Error";
}
if (message != null) {
new AlertDialog.Builder(mContext).setMessage(message).setTitle(title).setIcon(android.R.drawable.ic_dialog_alert).setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
setResult(RESULT_CANCELED);
}
}).show();
}
}
};
}
Aggregations