From cc03275faa5f5b5d7ddabd966570d708440c8275 Mon Sep 17 00:00:00 2001 From: Aman S Date: Fri, 23 May 2025 15:24:19 +0530 Subject: [PATCH] NTP-67490 || Network Validation changes (#16280) --- .../com/navi/common/network/NetworkUtil.kt | 11 +- .../java/com/navi/common/utils/CommonUtils.kt | 111 +----------------- 2 files changed, 5 insertions(+), 117 deletions(-) diff --git a/android/navi-common/src/main/java/com/navi/common/network/NetworkUtil.kt b/android/navi-common/src/main/java/com/navi/common/network/NetworkUtil.kt index 742d7d4ce9..a7678f58e9 100644 --- a/android/navi-common/src/main/java/com/navi/common/network/NetworkUtil.kt +++ b/android/navi-common/src/main/java/com/navi/common/network/NetworkUtil.kt @@ -18,13 +18,10 @@ import com.navi.common.firebaseremoteconfig.FirebaseRemoteConfigHelper import com.navi.common.network.models.ErrorMessage import com.navi.common.network.models.GenericResponse import com.navi.common.utils.CommonNaviAnalytics -import com.navi.common.utils.CommonUtils.isNetworkPoor import com.navi.common.utils.CommonUtils.isNetworkQualityPoor import com.navi.common.utils.GENERAL_ERROR import com.navi.common.utils.NO_INTERNET -import com.navi.common.utils.NetworkStats import com.navi.common.utils.SOCKET_TIMEOUT -import com.navi.common.utils.getDownloadNetworkStrength import com.navi.common.utils.log import java.net.ConnectException import java.net.SocketTimeoutException @@ -37,10 +34,6 @@ import timber.log.Timber fun handleException(e: Throwable, tag: String? = null): ErrorMessage { Timber.d(e, "Failure during processing") - val signalLevel = NaviTrackEvent.signalInfo.level - val signalType = NaviTrackEvent.signalInfo.type.name - val maxBandwidth = getDownloadNetworkStrength(AppServiceManager.application) - val networkSpeed = NetworkStats.networkSpeed NaviAnalyticsHelper.recordException(e) val errorMessage = ErrorMessage() if (!BaseUtils.isNetworkAvailable(AppServiceManager.application)) { @@ -52,7 +45,7 @@ fun handleException(e: Throwable, tag: String? = null): ErrorMessage { } else if (e is UnknownHostException) { errorMessage.statusCode = ApiConstants.API_CODE_UNKNOWN_HOST } else if (e is SocketTimeoutException) { - if (isNetworkPoor(maxBandwidth, networkSpeed, signalLevel, signalType)) { + if (isNetworkQualityPoor(BandwidthAccessor.networkQuality)) { errorMessage.statusCode = ApiConstants.API_CODE_SLOW_NETWORK } else { errorMessage.statusCode = ApiConstants.API_CODE_SOCKET_TIMEOUT @@ -61,7 +54,7 @@ fun handleException(e: Throwable, tag: String? = null): ErrorMessage { } else if (e is JsonParseException) { errorMessage.statusCode = ApiConstants.API_WRONG_ERROR_RESPONSE } else if (e is SSLHandshakeException || e is SSLPeerUnverifiedException) { - if (isNetworkPoor(maxBandwidth, networkSpeed, signalLevel, signalType)) { + if (isNetworkQualityPoor(BandwidthAccessor.networkQuality)) { errorMessage.statusCode = ApiConstants.API_CODE_SLOW_NETWORK } else { errorMessage.statusCode = ApiConstants.API_SECURED_CONNECTION_EXCEPTION diff --git a/android/navi-common/src/main/java/com/navi/common/utils/CommonUtils.kt b/android/navi-common/src/main/java/com/navi/common/utils/CommonUtils.kt index e70b2d3ac9..8ce2869ea8 100644 --- a/android/navi-common/src/main/java/com/navi/common/utils/CommonUtils.kt +++ b/android/navi-common/src/main/java/com/navi/common/utils/CommonUtils.kt @@ -7,7 +7,6 @@ package com.navi.common.utils -import android.annotation.SuppressLint import android.text.TextUtils import androidx.appcompat.app.AppCompatActivity import androidx.compose.foundation.gestures.FlingBehavior @@ -24,11 +23,11 @@ import com.google.accompanist.pager.PagerDefaults import com.google.accompanist.pager.PagerState import com.google.gson.JsonParseException import com.navi.alfred.utils.log -import com.navi.analytics.model.SignalType import com.navi.analytics.model.UserLocation import com.navi.analytics.utils.NaviTrackEvent import com.navi.bandwidthbuddy.BandwidthQuality import com.navi.base.AppServiceManager +import com.navi.base.bandwidthbuddy.BandwidthAccessor import com.navi.base.model.LineItem import com.navi.base.sharedpref.CommonPrefConstants import com.navi.base.sharedpref.PreferenceManager @@ -36,7 +35,6 @@ import com.navi.base.utils.BaseUtils import com.navi.base.utils.EMPTY import com.navi.base.utils.NetWatchManger import com.navi.base.utils.PROD -import com.navi.base.utils.orZero import com.navi.base.utils.toDoubleWithSafe import com.navi.common.BuildConfig import com.navi.common.CommonLibManager @@ -47,12 +45,8 @@ import com.navi.common.constants.HTTPMethods.POST import com.navi.common.constants.HTTPMethods.PUT import com.navi.common.constants.QA import com.navi.common.firebaseremoteconfig.FirebaseRemoteConfigHelper -import com.navi.common.firebaseremoteconfig.FirebaseRemoteConfigHelper.LOW_NETWORK_BANDWIDTH_THRESHOLD import com.navi.common.firebaseremoteconfig.FirebaseRemoteConfigHelper.LOW_NETWORK_SIGNAL_ERROR_CODE_ENABLED import com.navi.common.firebaseremoteconfig.FirebaseRemoteConfigHelper.LOW_NETWORK_SIGNAL_QUALITY_LIST -import com.navi.common.firebaseremoteconfig.FirebaseRemoteConfigHelper.LOW_NETWORK_SIGNAL_THRESHOLD -import com.navi.common.firebaseremoteconfig.FirebaseRemoteConfigHelper.LOW_NETWORK_SPEED_THRESHOLD -import com.navi.common.firebaseremoteconfig.FirebaseRemoteConfigHelper.NETWORK_SPEED_CHECK_FREQUENCY import com.navi.common.model.common.ErrorLog import com.navi.common.network.ApiConstants import com.navi.common.network.ApiConstants.API_CODE_CONNECT_EXCEPTION @@ -74,10 +68,6 @@ import com.navi.common.utils.Constants.AM_BIG import com.navi.common.utils.Constants.AM_SMALL import com.navi.common.utils.Constants.DECIMAL import com.navi.common.utils.Constants.MINUS -import com.navi.common.utils.Constants.NETWORK_BANDWIDTH_THRESHOLD_REACHED -import com.navi.common.utils.Constants.NETWORK_SIGNAL_STRENGTH_THRESHOLD_REACHED -import com.navi.common.utils.Constants.NETWORK_SPEED_THRESHOLD_REACHED -import com.navi.common.utils.Constants.NO_NETWORK_THRESHOLDS_REACHED import com.navi.common.utils.Constants.PM_BIG import com.navi.common.utils.Constants.PM_SMALL import com.navi.common.utils.Constants.ZERO_STRING @@ -315,10 +305,6 @@ object CommonUtils { } fun getLocalErrorResponse(exception: Exception): ErrorMessage { - val signalLevel = NaviTrackEvent.signalInfo.level - val signalType = NaviTrackEvent.signalInfo.type.name - val maxBandwidth = getDownloadNetworkStrength(AppServiceManager.application) - val networkSpeed = NetworkStats.networkSpeed val errorMessage = ErrorMessage() errorMessage.exception = exception.message if (!isNetworkAvailable() || exception is ConnectException) { @@ -348,7 +334,7 @@ object CommonUtils { AppServiceManager.application.getString(R.string.internet_too_slow) errorMessage.description = AppServiceManager.application.getString(R.string.no_internet_connection_description) - if (isNetworkPoor(maxBandwidth, networkSpeed, signalLevel, signalType)) { + if (isNetworkQualityPoor(BandwidthAccessor.networkQuality)) { errorMessage.statusCode = API_CODE_SLOW_NETWORK } else { errorMessage.statusCode = API_CODE_SOCKET_TIMEOUT @@ -356,7 +342,7 @@ object CommonUtils { } else if (exception is SSLHandshakeException || exception is SSLPeerUnverifiedException) { val isRestartNeeded = fetchNewKeyFromFirebaseAndRestart() if (isRestartNeeded.not()) { - if (isNetworkPoor(maxBandwidth, networkSpeed, signalLevel, signalType)) { + if (isNetworkQualityPoor(BandwidthAccessor.networkQuality)) { errorMessage.message = AppServiceManager.application.getString(R.string.internet_too_slow) errorMessage.description = @@ -468,97 +454,6 @@ object CommonUtils { return errorMessage } - /** - * Determines if the current network condition is poor based on bandwidth, speed, and signal - * strength if the frequency for network speed checks is enabled - * - * @param maxBandwidth The maximum bandwidth of the network connection in KBps. - * @param networkSpeed The current network speed in Bps. - * @param signalLevel The signal strength level (e.g., WiFi signal strength or cellular signal - * strength). - * @param signalType The type of signal (e.g., WiFi, CELLULAR, or UNKNOWN). - * @return `true` if any of the following conditions are met: - * - The maximum bandwidth is below the configured low network bandwidth threshold. - * - The current network speed is below the configured low network speed threshold. - * - The signal level is below the configured low network signal threshold, or the signal - * type is unknown. Otherwise, returns `false`. - * - * Note: - * - - * - If the network speed check frequency is set to 0 or negative, the check is skipped - * entirely. - * - Threshold values for bandwidth, speed, and signal are fetched dynamically using - * `FirebaseRemoteConfigHelper`. - * - If any of these thresholds are set to 0 or negative, that specific check is ignored. - */ - @SuppressLint("DefaultLocale") - fun isNetworkPoor( - maxBandwidth: Float, - networkSpeed: Float, - signalLevel: Int, - signalType: String, - ): Boolean { - val lowNetworkSpeedThreshold = - FirebaseRemoteConfigHelper.getLong(LOW_NETWORK_SPEED_THRESHOLD) - val lowNetworkSignalThreshold = - FirebaseRemoteConfigHelper.getLong(LOW_NETWORK_SIGNAL_THRESHOLD) - val lowNetworkBandwidthThreshold = - FirebaseRemoteConfigHelper.getLong(LOW_NETWORK_BANDWIDTH_THRESHOLD) - val eventAttributes = - mapOf( - "networkSpeed" to String.format("%.1f", networkSpeed.orZero()), - "lowNetworkSpeedThreshold" to lowNetworkSpeedThreshold.toString(), - "signalLevel" to signalLevel.toString(), - "signalType" to signalType, - "lowNetworkSignalThreshold" to lowNetworkSignalThreshold.toString(), - "maxBandwidth" to maxBandwidth.toString(), - "lowNetworkBandwidthThreshold" to lowNetworkBandwidthThreshold.toString(), - ) - return when { - // Check if network speed checks are disabled or network speed is invalid - FirebaseRemoteConfigHelper.getLong(NETWORK_SPEED_CHECK_FREQUENCY) <= 0 || - networkSpeed < 0 -> false - - // Network speed is below the threshold - lowNetworkSpeedThreshold > 0 && networkSpeed < lowNetworkSpeedThreshold -> { - NaviTrackEvent.trackEvent( - eventName = NETWORK_SPEED_THRESHOLD_REACHED, - eventValues = eventAttributes, - ) - true - } - - // Signal strength is below the threshold or unknown - lowNetworkSignalThreshold > 0 && - (signalLevel < lowNetworkSignalThreshold || - signalType == SignalType.UNKNOWN.name) -> { - NaviTrackEvent.trackEvent( - eventName = NETWORK_SIGNAL_STRENGTH_THRESHOLD_REACHED, - eventValues = eventAttributes, - ) - true - } - - // Bandwidth is below the threshold - lowNetworkBandwidthThreshold > 0 && maxBandwidth < lowNetworkBandwidthThreshold -> { - NaviTrackEvent.trackEvent( - eventName = NETWORK_BANDWIDTH_THRESHOLD_REACHED, - eventValues = eventAttributes, - ) - true - } - - // No thresholds are reached - else -> { - NaviTrackEvent.trackEvent( - eventName = NO_NETWORK_THRESHOLDS_REACHED, - eventValues = eventAttributes, - ) - false - } - } - } - fun isNetworkQualityPoor(networkQuality: BandwidthQuality?): Boolean { val isLowNetworkSignalErrorCodeEnabled = FirebaseRemoteConfigHelper.getBoolean(LOW_NETWORK_SIGNAL_ERROR_CODE_ENABLED)