From 2a95a82e32d9d73f1804cf632f3bb98aaef3ec02 Mon Sep 17 00:00:00 2001 From: Severin Rudie Date: Mon, 2 Dec 2019 10:26:06 -0800 Subject: [PATCH] For #5540: updates error page copy when no internet is available (#6803) --- .../mozilla/fenix/AppRequestInterceptor.kt | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/AppRequestInterceptor.kt b/app/src/main/java/org/mozilla/fenix/AppRequestInterceptor.kt index becdda797..92a8b07b5 100644 --- a/app/src/main/java/org/mozilla/fenix/AppRequestInterceptor.kt +++ b/app/src/main/java/org/mozilla/fenix/AppRequestInterceptor.kt @@ -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,