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

Cannot start a stream, because isNetworkAvailable not updated #592

Open
devalexx opened this issue Dec 6, 2024 · 1 comment
Open

Cannot start a stream, because isNetworkAvailable not updated #592

devalexx opened this issue Dec 6, 2024 · 1 comment

Comments

@devalexx
Copy link

devalexx commented Dec 6, 2024

Cannot start a stream, because isNetworkAvailable not updated and isInitialized = false here: https://github.com/crackededed/Xtra/blob/master/app/src/main/java/com/github/andreyasadchy/xtra/ui/player/BasePlayerFragment.kt#L207 .
Debugged and found that on com/github/andreyasadchy/xtra/ui/common/BaseNetworkFragment.kt:51 online = false, even though wifi is ON. It started happening ~2 months ago. I suppose because of these change
https://github.com/crackededed/Xtra/blob/master/app/src/main/java/com/github/andreyasadchy/xtra/ui/common/BaseNetworkFragment.kt#L50
https://github.com/crackededed/Xtra/blob/master/app/src/main/java/com/github/andreyasadchy/xtra/ui/main/MainViewModel.kt#L91
(within this commit fd2fe31)
Looks like it's happening only on inactive WiFi disconnection (when device was left for >10 mins), because when I turn on/off wifi manually it works well.
Probably isNetworkAvailable wasn't updated properly and before, but this refactoring revealed this issue. Or maybe it's because android target version change. Also it relies on ConnectivityManager.CONNECTIVITY_ACTION which is deprecated. Any ideas if it could be connected?
I have to disable and enable wifi to workaround it.

Looks like we should go with a job like https://stackoverflow.com/questions/48527171/detect-connectivity-change-in-android-7-and-above-when-app-is-killed-in-backgrou

@crackededed
Copy link
Owner

Also it relies on ConnectivityManager.CONNECTIVITY_ACTION which is deprecated.

yeah this should probably be updated. i looked at it before and i have this code saved but i don't remember if it works or not.

    private val networkCallback = object : ConnectivityManager.NetworkCallback() {
        override fun onAvailable(network: Network) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                viewModel.setNetworkAvailable(true)
            }
        }

        override fun onLost(network: Network) {
            viewModel.setNetworkAvailable(false)
        }

        override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
                networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) &&
                networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
                viewModel.setNetworkAvailable(true)
            }
        }
    }


        val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        when {
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.N -> connectivityManager.registerDefaultNetworkCallback(networkCallback)
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> connectivityManager.registerNetworkCallback(
                NetworkRequest.Builder()
                    .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                    .addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
                    .build(), networkCallback)
            Build.VERSION.SDK_INT < Build.VERSION_CODES.M -> connectivityManager.registerNetworkCallback(
                NetworkRequest.Builder()
                    .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                    .build(), networkCallback)
        }


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        val networkCapabilities = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
        networkCapabilities?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) == true &&
                networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
    } else @Suppress("DEPRECATION") {
        val activeNetwork = connectivityManager.activeNetworkInfo ?: connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_VPN)
        activeNetwork?.isConnectedOrConnecting == true
    }

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

No branches or pull requests

2 participants