-
Notifications
You must be signed in to change notification settings - Fork 123
fix(datastore): Update network connection availability checking status logic in ReachabilityMonitor of Datastore #2854
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
Open
aladine
wants to merge
16
commits into
aws-amplify:main
Choose a base branch
from
aladine:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
74a01ef
Add ConnectivityNetworkCallback class in ReachabilityMonitor
dantrongamz 116d12a
Move NetworkCapabilitiesUtil to a new util class and add unit test
dantrongamz 98a4859
Merge branch 'aws-amplify:main' into main
aladine 49817af
Move NetworkCapabilitiesUtil to a new util class and add unit test
dantrongamz dbe6727
fix conflict
dantrongamz 2397126
Merge branch 'main' into main
aladine 9811c61
Merge branch 'main' into main
aladine 6527cd6
Merge branch 'aws-amplify:main' into main
aladine 1e153dd
Update followed comment aws-datastore/src/main/java/com/amplifyframew…
aladine e69a7d3
fix: add new NetworkExtensions follow suggestions
dantrongamz ff110de
Remove unused currentNetwork and change unit test to use mockk
dantrongamz 4d7a27e
fix: update unit test with mockk
dantrongamz 4cdbb86
Merge branch 'main' into main
aladine e1e8722
fix: refactor onAvailable method to use network capabilities if avail…
dantrongamz 4c6adb3
Merge branch 'main' into main
aladine 5c9ab37
Merge branch 'main' into main
aladine 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
51 changes: 51 additions & 0 deletions
51
aws-datastore/src/main/java/com/amplifyframework/datastore/extensions/NetworkExtensions.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,51 @@ | ||
/* | ||
* Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
package com.amplifyframework.datastore.extensions | ||
|
||
import android.net.ConnectivityManager | ||
import android.net.NetworkCapabilities | ||
import android.os.Build | ||
import androidx.annotation.VisibleForTesting | ||
|
||
// Return network capabilities based on Connectivity Manager | ||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) | ||
fun ConnectivityManager.networkCapabilitiesOrNull() = try { | ||
getNetworkCapabilities(activeNetwork) | ||
} catch (ignored: SecurityException) { | ||
// Android 11 may throw a 'package does not belong' security exception here. | ||
// Google fixed Android 14, 13 and 12 with the issue where Chaland Jean patched those versions. | ||
// Android 11 is too old, so that's why we have to catch this exception here to be safe. | ||
// https://android.googlesource.com/platform/frameworks/base/+/249be21013e389837f5b2beb7d36890b25ecfaaf%5E%21/ | ||
// We need to catch this to prevent app crash. | ||
null | ||
} | ||
|
||
// Return whether internet is reachable based on network capabilities. | ||
fun NetworkCapabilities?.isInternetReachable() = when { | ||
this == null -> false | ||
hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true | ||
hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true | ||
hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true | ||
else -> false | ||
} | ||
|
||
// Check whether network is available based on connectivity manager | ||
// the same logic as https://github.com/aws-amplify/amplify-android/blob/main/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/BaseTransferWorker.kt#L176 | ||
fun ConnectivityManager.isNetworkAvailable(): Boolean = | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { | ||
networkCapabilitiesOrNull()?.isInternetReachable() ?: false | ||
} else { | ||
activeNetworkInfo?.isConnected ?: false | ||
} |
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
62 changes: 62 additions & 0 deletions
62
...atastore/src/test/java/com/amplifyframework/datastore/extensions/NetworkExtensionsTest.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,62 @@ | ||
package com.amplifyframework.datastore.extensions | ||
|
||
import android.net.ConnectivityManager | ||
import android.net.NetworkCapabilities | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNull | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Assert.assertFalse | ||
import org.junit.Test | ||
|
||
class NetworkCapabilitiesUtilTest { | ||
|
||
@Test | ||
fun testGetNetworkCapabilitiesSecurityException() { | ||
val mockConnectivityManager = mockk<ConnectivityManager> { | ||
every { activeNetwork } throws SecurityException() | ||
} | ||
|
||
val networkCapabilities = mockConnectivityManager.networkCapabilitiesOrNull() | ||
assertNull(networkCapabilities) | ||
} | ||
|
||
@Test | ||
fun testGetNetworkCapabilities() { | ||
val expectedNetworkCapabilities = mockk<NetworkCapabilities>() | ||
val mockConnectivityManager = mockk<ConnectivityManager> { | ||
every { getNetworkCapabilities(any()) } returns expectedNetworkCapabilities | ||
} | ||
|
||
val networkCapabilities = mockConnectivityManager.networkCapabilitiesOrNull() | ||
|
||
assertEquals(expectedNetworkCapabilities, networkCapabilities) | ||
} | ||
|
||
@Test | ||
fun testIsInternetReachable() { | ||
val networkCapabilitiesWithCellular = mockk<NetworkCapabilities> { | ||
every { hasTransport(NetworkCapabilities.TRANSPORT_WIFI) } returns false | ||
every { hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) } returns true | ||
every { hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) } returns false | ||
} | ||
|
||
val networkCapabilitiesWithWifi = mockk<NetworkCapabilities> { | ||
every { hasTransport(NetworkCapabilities.TRANSPORT_WIFI) } returns true | ||
every { hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) } returns false | ||
every { hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) } returns false | ||
} | ||
|
||
val networkCapabilitiesWithEthernet = mockk<NetworkCapabilities> { | ||
every { hasTransport(NetworkCapabilities.TRANSPORT_WIFI) } returns false | ||
every { hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) } returns false | ||
every { hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) } returns true | ||
} | ||
|
||
assertTrue(networkCapabilitiesWithCellular.isInternetReachable()) | ||
assertTrue(networkCapabilitiesWithWifi.isInternetReachable()) | ||
assertTrue(networkCapabilitiesWithEthernet.isInternetReachable()) | ||
assertFalse(null.isInternetReachable()) | ||
} | ||
} |
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
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.