Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix activeNetworkInfo Deprecation #2968

Merged
merged 7 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
shobhitagarwal1612 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading