From e093874c9cca1a051da698d23ed1b52d566e26b7 Mon Sep 17 00:00:00 2001 From: Akshay Nandwana Date: Wed, 8 Jan 2025 09:51:20 +0530 Subject: [PATCH] Fix activeNetworkInfo Deprecation (#2968) * deprecate fix * deprecate fix * test fix * added log --- .../google/android/ground/system/NetworkManager.kt | 14 ++++++++++---- .../ground/ui/tos/TermsOfServiceFragmentTest.kt | 7 +++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/ground/src/main/java/com/google/android/ground/system/NetworkManager.kt b/ground/src/main/java/com/google/android/ground/system/NetworkManager.kt index 35e728b6ed..96783b4d7e 100644 --- a/ground/src/main/java/com/google/android/ground/system/NetworkManager.kt +++ b/ground/src/main/java/com/google/android/ground/system/NetworkManager.kt @@ -27,6 +27,7 @@ import javax.inject.Singleton import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.callbackFlow +import timber.log.Timber enum class NetworkStatus { AVAILABLE, @@ -61,11 +62,16 @@ class NetworkManager @Inject constructor(@ApplicationContext private val context } } - /** Returns true iff the device has internet connectivity, false otherwise. */ + /** Returns true if the device has internet connectivity, false otherwise. */ @RequiresPermission("android.permission.ACCESS_NETWORK_STATE") fun isNetworkConnected(): Boolean { - val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager - val networkInfo = cm.activeNetworkInfo - return networkInfo?.isConnected ?: false + val connectivityManager = + context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + val activeNetwork = connectivityManager.activeNetwork + val networkCapabilities = connectivityManager.getNetworkCapabilities(activeNetwork) + + val isConnected = networkCapabilities?.hasCapability(NET_CAPABILITY_INTERNET) ?: false + Timber.d("Network connected: $isConnected") + return isConnected } } diff --git a/ground/src/test/java/com/google/android/ground/ui/tos/TermsOfServiceFragmentTest.kt b/ground/src/test/java/com/google/android/ground/ui/tos/TermsOfServiceFragmentTest.kt index 82e04c7dac..2f2beede2f 100644 --- a/ground/src/test/java/com/google/android/ground/ui/tos/TermsOfServiceFragmentTest.kt +++ b/ground/src/test/java/com/google/android/ground/ui/tos/TermsOfServiceFragmentTest.kt @@ -31,8 +31,10 @@ import com.google.android.ground.launchFragmentInHiltContainer import com.google.android.ground.launchFragmentWithNavController import com.google.android.ground.model.TermsOfService import com.google.android.ground.repository.TermsOfServiceRepository +import com.google.android.ground.system.NetworkManager import com.google.common.truth.Truth.assertThat import com.sharedtest.persistence.remote.FakeRemoteDataStore +import dagger.hilt.android.testing.BindValue import dagger.hilt.android.testing.HiltAndroidTest import javax.inject.Inject import org.hamcrest.BaseMatcher @@ -41,6 +43,8 @@ import org.hamcrest.Matcher import org.hamcrest.Matchers.not import org.junit.Test import org.junit.runner.RunWith +import org.mockito.Mock +import org.mockito.kotlin.whenever import org.robolectric.RobolectricTestRunner @HiltAndroidTest @@ -53,6 +57,8 @@ class TermsOfServiceFragmentTest : BaseHiltTest() { @Inject lateinit var viewModel: TermsOfServiceViewModel private lateinit var navController: NavController + @BindValue @Mock lateinit var networkManager: NetworkManager + private fun withHtml(html: String): Matcher = object : BaseMatcher() { override fun describeTo(description: Description?) { @@ -77,6 +83,7 @@ class TermsOfServiceFragmentTest : BaseHiltTest() { @Test fun termsOfServiceText_shouldBeDisplayed() { + whenever(networkManager.isNetworkConnected()).thenReturn(true) launchFragmentInHiltContainer(bundleOf(Pair("isViewOnly", false))) onView(withId(R.id.termsText))