From 24615afda1dd34d5da543e909ebd1391f5cf4d30 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 12 Dec 2024 17:29:15 -0800 Subject: [PATCH] Remove usage of ProcessLifecycle --- app/core/build.gradle.kts | 1 - .../com/topjohnwu/magisk/core/AppContext.kt | 10 ++++++---- .../magisk/core/utils/NetworkObserver.kt | 18 ++++++------------ .../magisk/core/utils/ProcessLifecycle.java | 15 --------------- gradle/libs.versions.toml | 1 - 5 files changed, 12 insertions(+), 33 deletions(-) delete mode 100644 app/core/src/main/java/com/topjohnwu/magisk/core/utils/ProcessLifecycle.java diff --git a/app/core/build.gradle.kts b/app/core/build.gradle.kts index 8d9c3d5f5..cccf75d7b 100644 --- a/app/core/build.gradle.kts +++ b/app/core/build.gradle.kts @@ -60,5 +60,4 @@ dependencies { implementation(libs.activity) implementation(libs.collection.ktx) implementation(libs.profileinstaller) - implementation(libs.lifecycle.process) } diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/AppContext.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/AppContext.kt index 607333918..52ea40b01 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/AppContext.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/AppContext.kt @@ -16,7 +16,6 @@ import com.topjohnwu.magisk.StubApk import com.topjohnwu.magisk.core.base.UntrackedActivity import com.topjohnwu.magisk.core.utils.LocaleSetting 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 import com.topjohnwu.superuser.Shell @@ -40,6 +39,7 @@ object AppContext : ContextWrapper(null), private var ref = WeakReference(null) private lateinit var application: Application + private lateinit var networkObserver: NetworkObserver init { // Always log full stack trace with Timber @@ -56,6 +56,10 @@ object AppContext : ContextWrapper(null), LocaleSetting.instance.updateResource(resources) } + override fun onActivityStarted(activity: Activity) { + networkObserver.postCurrentState() + } + override fun onActivityResumed(activity: Activity) { if (activity is UntrackedActivity) return ref = WeakReference(activity) @@ -102,8 +106,7 @@ object AppContext : ContextWrapper(null), val lm = getSystemService(LocaleManager::class.java) lm.overrideLocaleConfig = LocaleSetting.localeConfig } - ProcessLifecycle.init(this) - NetworkObserver.init(this) + networkObserver = NetworkObserver.init(this) if (!BuildConfig.DEBUG && !isRunningAsStub) { GlobalScope.launch(Dispatchers.IO) { ProfileInstaller.writeProfile(this@AppContext) @@ -120,7 +123,6 @@ object AppContext : ContextWrapper(null), } override fun onActivityCreated(activity: Activity, bundle: Bundle?) {} - override fun onActivityStarted(activity: Activity) {} override fun onActivityStopped(activity: Activity) {} override fun onActivitySaveInstanceState(activity: Activity, bundle: Bundle) {} override fun onActivityDestroyed(activity: Activity) {} diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/utils/NetworkObserver.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/utils/NetworkObserver.kt index aa9b2b617..ffb6c6f51 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/utils/NetworkObserver.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/utils/NetworkObserver.kt @@ -11,13 +11,10 @@ import android.net.NetworkRequest import android.os.PowerManager import androidx.collection.ArraySet 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 -class NetworkObserver(context: Context): DefaultLifecycleObserver { +class NetworkObserver(context: Context) { private val manager = context.getSystemService()!! private val networkCallback = object : ConnectivityManager.NetworkCallback() { @@ -55,16 +52,13 @@ class NetworkObserver(context: Context): DefaultLifecycleObserver { manager.registerNetworkCallback(request, networkCallback) val filter = IntentFilter(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED) context.applicationContext.registerRuntimeReceiver(receiver, filter) - ProcessLifecycleOwner.get().lifecycle.addObserver(this) } - override fun onStart(owner: LifecycleOwner) { - postCurrentState() - } - - private fun postCurrentState() { - postValue(manager.getNetworkCapabilities(manager.activeNetwork) - ?.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) ?: false) + fun postCurrentState() { + postValue( + manager.getNetworkCapabilities(manager.activeNetwork) + ?.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) == true + ) } private fun postValue(b: Boolean) { diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/utils/ProcessLifecycle.java b/app/core/src/main/java/com/topjohnwu/magisk/core/utils/ProcessLifecycle.java deleted file mode 100644 index 56180b97b..000000000 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/utils/ProcessLifecycle.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.topjohnwu.magisk.core.utils; - -import android.content.Context; - -import androidx.annotation.NonNull; -import androidx.lifecycle.LifecycleDispatcher; -import androidx.lifecycle.ProcessLifecycleOwner; - -// Use Java to bypass Kotlin internal visibility modifier -public class ProcessLifecycle { - public static void init(@NonNull Context context) { - LifecycleDispatcher.init(context); - ProcessLifecycleOwner.init$lifecycle_process_release(context); - } -} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 8c2db717b..3886b0490 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -42,7 +42,6 @@ room-compiler = { module = "androidx.room:room-compiler", version.ref = "room" } swiperefreshlayout = { module = "androidx.swiperefreshlayout:swiperefreshlayout", version = "1.1.0" } transition = { module = "androidx.transition:transition", version = "1.5.1" } collection-ktx = { module = "androidx.collection:collection-ktx", version = "1.4.5" } -lifecycle-process = { module = "androidx.lifecycle:lifecycle-process", version = "2.8.7" } material = { module = "com.google.android.material:material", version = "1.12.0" } jdk-libs = { module = "com.android.tools:desugar_jdk_libs_nio", version = "2.1.3" }