diff --git a/app/src/main/java/com/topjohnwu/magisk/core/App.kt b/app/src/main/java/com/topjohnwu/magisk/core/App.kt index 6d5590078..f524b8a6a 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/App.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/App.kt @@ -8,6 +8,7 @@ import android.os.Bundle import com.topjohnwu.magisk.StubApk import com.topjohnwu.magisk.core.di.ServiceLocator import com.topjohnwu.magisk.core.utils.DispatcherExecutor +import com.topjohnwu.magisk.core.utils.NetworkObserver import com.topjohnwu.magisk.core.utils.ProcessLifecycle import com.topjohnwu.magisk.core.utils.RootUtils import com.topjohnwu.magisk.core.utils.ShellInit @@ -82,6 +83,7 @@ open class App() : Application() { override fun onCreate() { super.onCreate() ProcessLifecycle.init(this) + NetworkObserver.init(this) } override fun onConfigurationChanged(newConfig: Configuration) { diff --git a/app/src/main/java/com/topjohnwu/magisk/core/Info.kt b/app/src/main/java/com/topjohnwu/magisk/core/Info.kt index c74dbd7e2..118c7c062 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/Info.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/Info.kt @@ -1,13 +1,10 @@ package com.topjohnwu.magisk.core -import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import com.topjohnwu.magisk.StubApk -import com.topjohnwu.magisk.core.di.AppContext import com.topjohnwu.magisk.core.ktx.getProperty import com.topjohnwu.magisk.core.model.UpdateInfo import com.topjohnwu.magisk.core.repository.NetworkService -import com.topjohnwu.magisk.core.utils.NetworkObserver import com.topjohnwu.superuser.ShellUtils.fastCmd val isRunningAsStub get() = Info.stub != null @@ -42,14 +39,7 @@ object Info { getProperty("ro.kernel.qemu", "0") == "1" || getProperty("ro.boot.qemu", "0") == "1" - val isConnected: LiveData by lazy { - MutableLiveData(false).also { field -> - NetworkObserver.observe(AppContext) { - remote = EMPTY_REMOTE - field.postValue(it) - } - } - } + val isConnected = MutableLiveData(false) val showSuperUser: Boolean get() { return env.isActive && (Const.USER_ID == 0 diff --git a/app/src/main/java/com/topjohnwu/magisk/core/utils/NetworkObserver.kt b/app/src/main/java/com/topjohnwu/magisk/core/utils/NetworkObserver.kt index 543903075..aa9b2b617 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/utils/NetworkObserver.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/utils/NetworkObserver.kt @@ -14,14 +14,10 @@ import androidx.core.content.getSystemService import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.ProcessLifecycleOwner +import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.core.ktx.registerRuntimeReceiver -typealias ConnectionCallback = (Boolean) -> Unit - -class NetworkObserver( - context: Context, - private val callback: ConnectionCallback -): DefaultLifecycleObserver { +class NetworkObserver(context: Context): DefaultLifecycleObserver { private val manager = context.getSystemService()!! private val networkCallback = object : ConnectivityManager.NetworkCallback() { @@ -29,11 +25,11 @@ class NetworkObserver( override fun onAvailable(network: Network) { activeList.add(network) - callback(true) + postValue(true) } override fun onLost(network: Network) { activeList.remove(network) - callback(!activeList.isEmpty()) + postValue(!activeList.isEmpty()) } } @@ -45,7 +41,7 @@ class NetworkObserver( } override fun onReceive(context: Context, intent: Intent) { if (context.isIdleMode()) { - callback(false) + postValue(false) } else { postCurrentState() } @@ -67,13 +63,18 @@ class NetworkObserver( } private fun postCurrentState() { - callback(manager.getNetworkCapabilities(manager.activeNetwork) + postValue(manager.getNetworkCapabilities(manager.activeNetwork) ?.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) ?: false) } + private fun postValue(b: Boolean) { + Info.remote = Info.EMPTY_REMOTE + Info.isConnected.postValue(b) + } + companion object { - fun observe(context: Context, callback: ConnectionCallback): NetworkObserver { - return NetworkObserver(context, callback).apply { postCurrentState() } + fun init(context: Context): NetworkObserver { + return NetworkObserver(context).apply { postCurrentState() } } } }