Skip to content

Commit

Permalink
Fix activeNetworkInfo Deprecation (#2968)
Browse files Browse the repository at this point in the history
* deprecate fix

* deprecate fix

* test fix

* added log
  • Loading branch information
anandwana001 authored Jan 8, 2025
1 parent 0280bf5 commit e093874
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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<View> =
object : BaseMatcher<View>() {
override fun describeTo(description: Description?) {
Expand All @@ -77,6 +83,7 @@ class TermsOfServiceFragmentTest : BaseHiltTest() {

@Test
fun termsOfServiceText_shouldBeDisplayed() {
whenever(networkManager.isNetworkConnected()).thenReturn(true)
launchFragmentInHiltContainer<TermsOfServiceFragment>(bundleOf(Pair("isViewOnly", false)))

onView(withId(R.id.termsText))
Expand Down

0 comments on commit e093874

Please sign in to comment.