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

add set cache method in Util #846

Merged
merged 1 commit into from
Oct 17, 2024
Merged

add set cache method in Util #846

merged 1 commit into from
Oct 17, 2024

Conversation

snyderra
Copy link

When used on android mobile devices the standard java.net, network interfaces and address classes does not match the state of the Connectivity manager. for example when connectivity manager make a call back saying that an ip is available, Util.checkIfValidAddress(it,"") will throw an exception saying it is not valid. This is the case even if Util.resetCacheAddresses(true,true) is called first

Adding this method allow for the connectivity manager to make a call back like the following:

val networkCallback =  object : NetworkCallback() {
        override fun onLinkPropertiesChanged(network: Network, linkProperties: LinkProperties) {
            linkProperties.linkAddresses.forEach {
                Log.d(TAG, "Link Address: ${it.address}")
            // This in theory, should be called when the network changes and should update to the most current.
            // Android for some reason does not reflect all the changes through the JDK
            Util.resetCacheAddresses(true,true)
            Log.d(TAG, "Valid Java Interfaces: ${Util.getAllAvailableInterfaces()}")
            Log.d(TAG, "Valid Java Address: ${Util.getAllAvailableAddresses(null)}")

            //This is a workaround to get the correct address that android does not reflect via the JDK
            Util.setCacheAddresses(
                // filtering may not be necessary. was beneficial in my case
                Util.getAllAvailableInterfaces().filter { it.supportsMulticast() }, 
                linkProperties.linkAddresses.map{it.address}.filter{it.isLinkLocalAddress.not() && it.isLoopbackAddress.not()}
            )

            // sanity check
            v4.forEach {
                Log.d(TAG, "Checking Link Address: ${it.hostAddress}")
                Util.checkIfValidAddress(it,"CHECK")
            }
            Log.d(TAG, "Valid Android Interfaces: ${Util.getAllAvailableInterfaces()}")
            Log.d(TAG, "Valid Android Address: ${Util.getAllAvailableAddresses(null)}")
        }

        override fun onAvailable(network: Network) {
            //Actions to take with connection available.
        }

        override fun onLost(network: Network) {
            //Actions to take with lost connection.
        }
    }

 connectivityManager.registerNetworkCallback(
            NetworkRequest.Builder().build(),
            networkCallback
        )

@belaban belaban merged commit 0424b0b into belaban:master Oct 17, 2024
12 checks passed
@belaban
Copy link
Owner

belaban commented Oct 17, 2024

Hey Rob
applied, thanks! This is only on master (5.4), ok with you or do you need a backport to 5.3.x?

@snyderra
Copy link
Author

If it's not too much work a backport would be great if a 5.4 release is going to be awhile. Currently I just have a patched 5.3 version pushed to a private MVN repo.

@belaban
Copy link
Owner

belaban commented Oct 17, 2024

Back-ported to 5.3.x.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants