1
0
Fork 0

For #5540: updates error page copy when no internet is available (#6803)

master
Severin Rudie 2019-12-02 10:26:06 -08:00 committed by GitHub
parent 63cf034bfb
commit 2a95a82e32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 3 deletions

View File

@ -5,6 +5,8 @@
package org.mozilla.fenix
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo
import androidx.annotation.RawRes
import mozilla.components.browser.errorpages.ErrorPages
import mozilla.components.browser.errorpages.ErrorType
@ -45,14 +47,16 @@ class AppRequestInterceptor(private val context: Context) : RequestInterceptor {
errorType: ErrorType,
uri: String?
): RequestInterceptor.ErrorResponse? {
val riskLevel = getRiskLevel(errorType)
val improvedErrorType = improveErrorType(errorType)
context.components.analytics.metrics.track(Event.ErrorPageVisited(errorType))
val riskLevel = getRiskLevel(improvedErrorType)
context.components.analytics.metrics.track(Event.ErrorPageVisited(improvedErrorType))
return RequestInterceptor.ErrorResponse(
ErrorPages.createErrorPage(
context,
errorType,
improvedErrorType,
uri = uri,
htmlResource = riskLevel.htmlRes,
cssResource = riskLevel.cssRes
@ -60,6 +64,24 @@ class AppRequestInterceptor(private val context: Context) : RequestInterceptor {
)
}
/**
* Where possible, this will make the error type more accurate by including information not
* available to AC.
*/
private fun improveErrorType(errorType: ErrorType): ErrorType {
// This is not an ideal solution. For context, see:
// https://github.com/mozilla-mobile/android-components/pull/5068#issuecomment-558415367
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork: NetworkInfo? = cm.activeNetworkInfo
@Suppress("DEPRECATION") // NetworkCallback is not appropriate for this use case
val isConnected: Boolean = activeNetwork?.isConnectedOrConnecting == true
return when {
errorType == ErrorType.ERROR_UNKNOWN_HOST && !isConnected -> ErrorType.ERROR_NO_INTERNET
else -> errorType
}
}
private fun getRiskLevel(errorType: ErrorType): RiskLevel = when (errorType) {
ErrorType.UNKNOWN,
ErrorType.ERROR_NET_INTERRUPT,