Repository navigation
feat(connectivity): know whether the device is online in the listing form #388
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
Merged
+304
−118
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
eca904a
feat(listing): name a location after the campus it is on
thanna23 98ad572
feat(listing): refuse a location with a blank name and trim it
thanna23 bc89ebd
feat(listing): set, rename and clear the location in the listing form
thanna23 8dcfa8d
docs(listing): update why an invalid stored location reads as none
thanna23 62728eb
feat(connectivity): know whether the device can reach the internet
thanna23 69be9ba
feat(listing): follow whether the device is online in the listing form
thanna23 03fc642
chore: merge main into feature/listing-location-form-state
thanna23 ef028f4
revert: stop following connectivity in the listing form
thanna23 3115d5f
revert: remove the connectivity repository from this PR
thanna23 4cfcb4e
docs(map): say the listing form keeps the approximate location on the…
thanna23 e9b4d50
style(map): format the LocationRepository KDoc with ktfmt
thanna23 a6742c5
fix(listing): flag a missing location name only once its field is left
thanna23 3b6f370
feat(connectivity): know whether the device can reach the internet
thanna23 c38df59
feat(listing): follow whether the device is online in the listing form
thanna23 fbbca9a
chore: merge main into feature/listing-connectivity
thanna23 d982d95
fix(connectivity): stay online when a previous default network is lost
thanna23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
app/src/main/java/com/android/polybazaar/connectivity/model/ConnectivityRepository.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // Co-authored-by: Claude Code (Claude Opus 5.5) | ||
| package com.android.polybazaar.connectivity.model | ||
|
|
||
| import android.content.Context | ||
| import android.net.ConnectivityManager | ||
| import android.net.Network | ||
| import android.net.NetworkCapabilities | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
|
|
||
| /** Whether the device can reach the internet, for features that need it (#339). */ | ||
| interface ConnectivityRepository { | ||
| /** True while the device has a network that reaches the internet. */ | ||
| val isOnline: StateFlow<Boolean> | ||
| } | ||
|
|
||
| /** | ||
| * A [ConnectivityRepository] that follows Android's default network. | ||
| * | ||
| * Its callback is never unregistered: create one per process, with the application context, and | ||
| * share it, so it lives as long as the app does. | ||
| */ | ||
| class AndroidConnectivityRepository(context: Context) : ConnectivityRepository { | ||
|
|
||
| private val connectivityManager = | ||
| context.applicationContext.getSystemService(ConnectivityManager::class.java) | ||
|
|
||
| private val _isOnline = | ||
| MutableStateFlow( | ||
| reachesInternet( | ||
| connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork) | ||
| ) | ||
| ) | ||
| override val isOnline: StateFlow<Boolean> = _isOnline.asStateFlow() | ||
|
|
||
| /** The default network the callback last reported, or null once it is lost. */ | ||
| @Volatile private var defaultNetwork: Network? = connectivityManager.activeNetwork | ||
|
|
||
| init { | ||
| connectivityManager.registerDefaultNetworkCallback( | ||
| object : ConnectivityManager.NetworkCallback() { | ||
| // Also called right after a new default network becomes available. | ||
| override fun onCapabilitiesChanged(network: Network, capabilities: NetworkCapabilities) { | ||
| defaultNetwork = network | ||
| _isOnline.value = reachesInternet(capabilities) | ||
| } | ||
|
|
||
| // During a Wi-Fi/mobile handoff the old network can be lost after the new one became | ||
| // the default: only losing the current default means the device is offline. | ||
| override fun onLost(network: Network) { | ||
| if (network != defaultNetwork) return | ||
| defaultNetwork = null | ||
| _isOnline.value = false | ||
| } | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| /** Whether [capabilities] reach the internet: validated, so not stuck behind a login page. */ | ||
| private fun reachesInternet(capabilities: NetworkCapabilities?): Boolean = | ||
| capabilities != null && | ||
| capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) && | ||
| capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...c/sharedTest/java/com/android/polybazaar/connectivity/model/FakeConnectivityRepository.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // Co-authored-by: Claude Code (Claude Opus 5.5) | ||
| package com.android.polybazaar.connectivity.model | ||
|
|
||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
|
|
||
| /** A [ConnectivityRepository] for tests: set [isOnline] to go offline or back online. */ | ||
| class FakeConnectivityRepository(online: Boolean = true) : ConnectivityRepository { | ||
| override val isOnline = MutableStateFlow(online) | ||
| } |
80 changes: 80 additions & 0 deletions
80
.../test/java/com/android/polybazaar/connectivity/model/AndroidConnectivityRepositoryTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Co-authored-by: Claude Code (Claude Opus 5.5) | ||
| package com.android.polybazaar.connectivity.model | ||
|
|
||
| import android.content.Context | ||
| import android.net.ConnectivityManager | ||
| import android.net.NetworkCapabilities | ||
| import androidx.test.core.app.ApplicationProvider | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import org.junit.Assert.assertFalse | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.Shadows.shadowOf | ||
| import org.robolectric.shadows.ShadowNetwork | ||
| import org.robolectric.shadows.ShadowNetworkCapabilities | ||
|
|
||
| /** [AndroidConnectivityRepository] on Robolectric's [ConnectivityManager]. */ | ||
| @RunWith(AndroidJUnit4::class) | ||
| class AndroidConnectivityRepositoryTest { | ||
|
|
||
| private val context: Context = ApplicationProvider.getApplicationContext() | ||
| private val connectivityManager = context.getSystemService(ConnectivityManager::class.java) | ||
|
|
||
| private fun capabilities(vararg capabilities: Int): NetworkCapabilities = | ||
| ShadowNetworkCapabilities.newInstance().also { network -> | ||
| capabilities.forEach { shadowOf(network).addCapability(it) } | ||
| } | ||
|
|
||
| private val validated = | ||
| capabilities( | ||
| NetworkCapabilities.NET_CAPABILITY_INTERNET, | ||
| NetworkCapabilities.NET_CAPABILITY_VALIDATED, | ||
| ) | ||
|
|
||
| /** Gives the active network [capabilities], then creates the repository. */ | ||
| private fun repositoryOn(capabilities: NetworkCapabilities?): AndroidConnectivityRepository { | ||
| shadowOf(connectivityManager) | ||
| .setNetworkCapabilities(connectivityManager.activeNetwork, capabilities) | ||
| return AndroidConnectivityRepository(context) | ||
| } | ||
|
|
||
| @Test | ||
| fun startsOffline_withoutAValidatedInternetNetwork() { | ||
| // Behind a login page, the network has the internet capability but is not validated. | ||
| assertFalse( | ||
| repositoryOn(capabilities(NetworkCapabilities.NET_CAPABILITY_INTERNET)).isOnline.value | ||
| ) | ||
| assertFalse(repositoryOn(null).isOnline.value) | ||
| } | ||
|
|
||
| @Test | ||
| fun followsTheDefaultNetwork() { | ||
| val repository = repositoryOn(validated) | ||
| val callback = shadowOf(connectivityManager).networkCallbacks.single() | ||
| val network = connectivityManager.activeNetwork!! | ||
| assertTrue(repository.isOnline.value) | ||
|
|
||
| callback.onLost(network) | ||
| assertFalse(repository.isOnline.value) | ||
|
|
||
| callback.onCapabilitiesChanged(network, validated) | ||
| assertTrue(repository.isOnline.value) | ||
| } | ||
|
|
||
| @Test | ||
| fun staysOnline_whenThePreviousNetworkIsLostAfterAHandoff() { | ||
| val repository = repositoryOn(validated) | ||
| val callback = shadowOf(connectivityManager).networkCallbacks.single() | ||
| val wifi = connectivityManager.activeNetwork!! | ||
| val mobile = ShadowNetwork.newInstance(shadowOf(wifi).netId + 1) | ||
|
|
||
| // Mobile becomes the default, validated, before Wi-Fi's loss arrives. | ||
| callback.onCapabilitiesChanged(mobile, validated) | ||
| callback.onLost(wifi) | ||
| assertTrue(repository.isOnline.value) | ||
|
|
||
| callback.onLost(mobile) | ||
| assertFalse(repository.isOnline.value) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.