mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-03-03 07:01:19 +00:00
Bug 1480095 - Add example custom error page to GeckoView Example r=esawin
MozReview-Commit-ID: HeYOkwNHYXq
This commit is contained in:
parent
e07c741622
commit
cd232d5aac
28
mobile/android/geckoview_example/src/main/assets/error.html
Normal file
28
mobile/android/geckoview_example/src/main/assets/error.html
Normal file
@ -0,0 +1,28 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Boom!</title>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
|
||||
<style>
|
||||
body {
|
||||
background-color: red;
|
||||
color: white;
|
||||
font-family: sans;
|
||||
}
|
||||
|
||||
div.container {
|
||||
width: 75%;
|
||||
margin: auto;
|
||||
text-align: center;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>Boom!</h1>
|
||||
<p>Something bad happened...</p>
|
||||
<p>$ERROR</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
@ -38,6 +38,10 @@ import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.ProgressBar;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Locale;
|
||||
|
||||
@ -345,6 +349,48 @@ public class GeckoViewActivity extends AppCompatActivity {
|
||||
manager.enqueue(req);
|
||||
}
|
||||
|
||||
private String mErrorTemplate;
|
||||
private String createErrorPage(final String error) {
|
||||
if (mErrorTemplate == null) {
|
||||
InputStream stream = null;
|
||||
BufferedReader reader = null;
|
||||
StringBuilder builder = new StringBuilder();
|
||||
try {
|
||||
stream = getResources().getAssets().open("error.html");
|
||||
reader = new BufferedReader(new InputStreamReader(stream));
|
||||
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
builder.append(line);
|
||||
builder.append("\n");
|
||||
}
|
||||
|
||||
mErrorTemplate = builder.toString();
|
||||
} catch (IOException e) {
|
||||
Log.d(LOGTAG, "Failed to open error page template", e);
|
||||
return null;
|
||||
} finally {
|
||||
if (stream != null) {
|
||||
try {
|
||||
stream.close();
|
||||
} catch (IOException e) {
|
||||
Log.e(LOGTAG, "Failed to close error page template stream", e);
|
||||
}
|
||||
}
|
||||
|
||||
if (reader != null) {
|
||||
try {
|
||||
reader.close();
|
||||
} catch (IOException e) {
|
||||
Log.e(LOGTAG, "Failed to close error page template reader", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return mErrorTemplate.replace("$ERROR", error);
|
||||
}
|
||||
|
||||
private class ExampleContentDelegate implements GeckoSession.ContentDelegate {
|
||||
@Override
|
||||
public void onTitleChange(GeckoSession session, String title) {
|
||||
@ -591,11 +637,135 @@ public class GeckoViewActivity extends AppCompatActivity {
|
||||
return GeckoResult.fromValue(newSession);
|
||||
}
|
||||
|
||||
public void onLoadError(final GeckoSession session, final String uri,
|
||||
final int category, final int error) {
|
||||
private String categoryToString(final int category) {
|
||||
switch (category) {
|
||||
case ERROR_CATEGORY_UNKNOWN:
|
||||
return "ERROR_CATEGORY_UNKNOWN";
|
||||
case ERROR_CATEGORY_SECURITY:
|
||||
return "ERROR_CATEGORY_SECURITY";
|
||||
case ERROR_CATEGORY_NETWORK:
|
||||
return "ERROR_CATEGORY_NETWORK";
|
||||
case ERROR_CATEGORY_CONTENT:
|
||||
return "ERROR_CATEGORY_CONTENT";
|
||||
case ERROR_CATEGORY_URI:
|
||||
return "ERROR_CATEGORY_URI";
|
||||
case ERROR_CATEGORY_PROXY:
|
||||
return "ERROR_CATEGORY_PROXY";
|
||||
case ERROR_CATEGORY_SAFEBROWSING:
|
||||
return "ERROR_CATEGORY_SAFEBROWSING";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
private String errorToString(final int error) {
|
||||
switch (error) {
|
||||
case ERROR_UNKNOWN:
|
||||
return "ERROR_UNKNOWN";
|
||||
case ERROR_SECURITY_SSL:
|
||||
return "ERROR_SECURITY_SSL";
|
||||
case ERROR_SECURITY_BAD_CERT:
|
||||
return "ERROR_SECURITY_BAD_CERT";
|
||||
case ERROR_NET_RESET:
|
||||
return "ERROR_NET_RESET";
|
||||
case ERROR_NET_INTERRUPT:
|
||||
return "ERROR_NET_INTERRUPT";
|
||||
case ERROR_NET_TIMEOUT:
|
||||
return "ERROR_NET_TIMEOUT";
|
||||
case ERROR_CONNECTION_REFUSED:
|
||||
return "ERROR_CONNECTION_REFUSED";
|
||||
case ERROR_UNKNOWN_PROTOCOL:
|
||||
return "ERROR_UNKNOWN_PROTOCOL";
|
||||
case ERROR_UNKNOWN_HOST:
|
||||
return "ERROR_UNKNOWN_HOST";
|
||||
case ERROR_UNKNOWN_SOCKET_TYPE:
|
||||
return "ERROR_UNKNOWN_SOCKET_TYPE";
|
||||
case ERROR_UNKNOWN_PROXY_HOST:
|
||||
return "ERROR_UNKNOWN_PROXY_HOST";
|
||||
case ERROR_MALFORMED_URI:
|
||||
return "ERROR_MALFORMED_URI";
|
||||
case ERROR_REDIRECT_LOOP:
|
||||
return "ERROR_REDIRECT_LOOP";
|
||||
case ERROR_SAFEBROWSING_PHISHING_URI:
|
||||
return "ERROR_SAFEBROWSING_PHISHING_URI";
|
||||
case ERROR_SAFEBROWSING_MALWARE_URI:
|
||||
return "ERROR_SAFEBROWSING_MALWARE_URI";
|
||||
case ERROR_SAFEBROWSING_UNWANTED_URI:
|
||||
return "ERROR_SAFEBROWSING_UNWANTED_URI";
|
||||
case ERROR_SAFEBROWSING_HARMFUL_URI:
|
||||
return "ERROR_SAFEBROWSING_HARMFUL_URI";
|
||||
case ERROR_CONTENT_CRASHED:
|
||||
return "ERROR_CONTENT_CRASHED";
|
||||
case ERROR_OFFLINE:
|
||||
return "ERROR_OFFLINE";
|
||||
case ERROR_PORT_BLOCKED:
|
||||
return "ERROR_PORT_BLOCKED";
|
||||
case ERROR_PROXY_CONNECTION_REFUSED:
|
||||
return "ERROR_PROXY_CONNECTION_REFUSED";
|
||||
case ERROR_FILE_NOT_FOUND:
|
||||
return "ERROR_FILE_NOT_FOUND";
|
||||
case ERROR_FILE_ACCESS_DENIED:
|
||||
return "ERROR_FILE_ACCESS_DENIED";
|
||||
case ERROR_INVALID_CONTENT_ENCODING:
|
||||
return "ERROR_INVALID_CONTENT_ENCODING";
|
||||
case ERROR_UNSAFE_CONTENT_TYPE:
|
||||
return "ERROR_UNSAFE_CONTENT_TYPE";
|
||||
case ERROR_CORRUPTED_CONTENT:
|
||||
return "ERROR_CORRUPTED_CONTENT";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
private String createErrorPage(final int category, final int error) {
|
||||
if (mErrorTemplate == null) {
|
||||
InputStream stream = null;
|
||||
BufferedReader reader = null;
|
||||
StringBuilder builder = new StringBuilder();
|
||||
try {
|
||||
stream = getResources().getAssets().open("error.html");
|
||||
reader = new BufferedReader(new InputStreamReader(stream));
|
||||
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
builder.append(line);
|
||||
builder.append("\n");
|
||||
}
|
||||
|
||||
mErrorTemplate = builder.toString();
|
||||
} catch (IOException e) {
|
||||
Log.d(LOGTAG, "Failed to open error page template", e);
|
||||
return null;
|
||||
} finally {
|
||||
if (stream != null) {
|
||||
try {
|
||||
stream.close();
|
||||
} catch (IOException e) {
|
||||
Log.e(LOGTAG, "Failed to close error page template stream", e);
|
||||
}
|
||||
}
|
||||
|
||||
if (reader != null) {
|
||||
try {
|
||||
reader.close();
|
||||
} catch (IOException e) {
|
||||
Log.e(LOGTAG, "Failed to close error page template reader", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return GeckoViewActivity.this.createErrorPage(categoryToString(category) + " : " + errorToString(error));
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeckoResult<String> onLoadError(final GeckoSession session, final String uri,
|
||||
final int category, final int error) {
|
||||
Log.d(LOGTAG, "onLoadError=" + uri +
|
||||
" error category=" + category +
|
||||
" error=" + error);
|
||||
|
||||
return GeckoResult.fromValue("data:text/html," + createErrorPage(category, error));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user