pwittchen / ReactiveNetwork

Android library listening network connection state and Internet connectivity with RxJava Observables
http://pwittchen.github.io/ReactiveNetwork/docs/RxJava2.x/
Apache License 2.0
2.53k stars 276 forks source link

A question about the state of the Internet #375

Closed VahidGarousi closed 4 years ago

VahidGarousi commented 4 years ago

Hi @pwittchen Thank you for this practical library I had a question about the functionality of this library I receive the following message in logcat : Mobile : Galaxy J7 Prime The phone keeps sending the following message in logcat: (HTTPLog)-Static: isSBSettingEnabled false I/System.out: (HTTPLog)-Static: isSBSettingEnabled false Could not establish connection with WalledGardenStrategy => Handshake failed

pwittchen commented 4 years ago

Hi,

Thanks for the question. This problem may be related to clearTextTraffic issue introduced in the one of the recent Android versions. Please, have a look at this section in documentation: https://github.com/pwittchen/ReactiveNetwork#cleartext-traffic.

Regards, Piotr

VahidGarousi commented 4 years ago

I did, but unfortunately the error still remains

pwittchen commented 4 years ago

Do you have such attribute in <application> tag in AndroidManifest.xml?

  <application android:usesCleartextTraffic="true">

This issue may be related to the Manifest configuration, Android OS or the specific device.

VahidGarousi commented 4 years ago

I was finally able to fix it. I used an auxiliary variable. If you would like to send you my Base Fragment Class? If someone whose application was based on fragment uses this class

pwittchen commented 4 years ago

Sure, you can put your solution in this issue, if you think someone else may have similar problem.

VahidGarousi commented 4 years ago

Base Fragment

abstract class BaseFragment : Fragment() {

    @get:LayoutRes
    abstract val layoutRes: Int

    abstract fun getCoordinatorLayout(): Int
    protected var rootView: View? = null
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        if (rootView == null) {
            rootView = inflater.inflate(layoutRes, container, false)
        }
        if (!Tools.isNetworkAvailable(activity!!)) {
        }
        return rootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setupViews()
    }

    abstract fun setupViews()
}

Observer Fragment

abstract class ObserverFragment : BaseFragment() {
    protected var compositeDisposable = CompositeDisposable()
    protected var isSubscribeCalled = false
    private var internetDisposable: Disposable? = null

    private var connectivityDisposable: Disposable? = null

    protected lateinit var dialogInternetConnection: DialogInternetConnection

    private var isConnected: Boolean = false

    override fun onStart() {
        super.onStart()
        connectivityDisposable =
            ReactiveNetwork.observeNetworkConnectivity(context!!.applicationContext)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe { connectivity ->
                    Log.d("LOGGER", connectivity.toString())
                    val state = connectivity.state()
                    val name = connectivity.typeName()
                    Log.i(
                        "LOGGER",
                        "connectivityDisposable" + String.format(
                            "state: %s, typeName: %s",
                            state,
                            name
                        )
                    )
                    if (connectivity.state() == NetworkInfo.State.CONNECTED) {
                        isConnected = true
                        subscribe()
                        dialogInternetConnection.dismiss()
                    } else {
                        isConnected = false
                    }
                }
        internetDisposable = ReactiveNetwork.observeInternetConnectivity()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe { isConnectedToInternet ->
                Log.i("LOGGER", "internetDisposable " + isConnectedToInternet.toString())
                if (isConnectedToInternet) {
                    isConnected = true
                    dialogInternetConnection.dismiss()
                } else {
                    isConnected = false
                    dialogInternetConnection =
                        DialogInternetConnection(object : DialogInternetConnection.OnBtnTryClicked {
                            override fun onClicked() {
                                if (isConnected) {
                                    subscribe()
                                    dialogInternetConnection.dismiss()
                                } else {
                                    (activity as BaseActivity).showToast("Please check your internet connection!")
                                }
                            }
                        })
                    dialogInternetConnection.isCancelable = false
                    dialogInternetConnection.show(fragmentManager!!, null)
                }
            }
        if (!isSubscribeCalled) {
            subscribe()
            isSubscribeCalled = true
        }
    }

    override fun onStop() {
        super.onStop()
        unSubscribe()
        safelyDispose(connectivityDisposable)
        safelyDispose(internetDisposable)
    }

    private fun safelyDispose(disposable: Disposable?) {
        if (disposable != null && !disposable.isDisposed) {
            disposable.dispose()
        }
    }
    abstract fun subscribe()

    fun unSubscribe() {
        compositeDisposable.clear()
    }
}