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 16cf4fba6..b81b9bba2 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/App.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/App.kt @@ -1,8 +1,10 @@ package com.topjohnwu.magisk.core +import android.app.Activity import android.app.Application import android.content.Context import android.content.res.Configuration +import android.os.Bundle import androidx.appcompat.app.AppCompatDelegate import androidx.multidex.MultiDex import androidx.work.WorkManager @@ -12,9 +14,7 @@ import com.topjohnwu.magisk.FileProvider import com.topjohnwu.magisk.core.su.SuCallbackHandler import com.topjohnwu.magisk.core.utils.RootInit import com.topjohnwu.magisk.core.utils.updateConfig -import com.topjohnwu.magisk.di.ActivityTracker import com.topjohnwu.magisk.di.koinModules -import com.topjohnwu.magisk.extensions.get import com.topjohnwu.magisk.extensions.unwrap import com.topjohnwu.superuser.Shell import org.koin.android.ext.koin.androidContext @@ -67,7 +67,7 @@ open class App() : Application() { modules(koinModules) } ResMgr.init(impl) - app.registerActivityLifecycleCallbacks(get()) + app.registerActivityLifecycleCallbacks(ForegroundTracker) WorkManager.initialize(impl.wrapJob(), androidx.work.Configuration.Builder().build()) } @@ -82,3 +82,25 @@ open class App() : Application() { super.onConfigurationChanged(newConfig) } } + +object ForegroundTracker : Application.ActivityLifecycleCallbacks { + + @Volatile + var foreground: Activity? = null + + val hasForeground get() = foreground != null + + override fun onActivityResumed(activity: Activity) { + foreground = activity + } + + override fun onActivityPaused(activity: Activity) { + foreground = 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/src/main/java/com/topjohnwu/magisk/core/download/RemoteFileService.kt b/app/src/main/java/com/topjohnwu/magisk/core/download/RemoteFileService.kt index 222d99783..4cd8df26e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/download/RemoteFileService.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/download/RemoteFileService.kt @@ -1,16 +1,14 @@ package com.topjohnwu.magisk.core.download -import android.app.Activity import android.app.Notification import android.content.Intent import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import com.topjohnwu.magisk.R +import com.topjohnwu.magisk.core.ForegroundTracker import com.topjohnwu.magisk.core.utils.ProgressInputStream import com.topjohnwu.magisk.core.view.Notifications import com.topjohnwu.magisk.data.network.GithubRawServices -import com.topjohnwu.magisk.di.NullActivity -import com.topjohnwu.magisk.extensions.get import com.topjohnwu.magisk.extensions.subscribeK import com.topjohnwu.magisk.extensions.writeTo import com.topjohnwu.magisk.model.entity.internal.DownloadSubject @@ -47,7 +45,7 @@ abstract class RemoteFileService : NotificationService() { failNotify(subject) }) { val newId = finishNotify(subject) - if (get() !is NullActivity) { + if (ForegroundTracker.hasForeground) { onFinished(subject, newId) } } diff --git a/app/src/main/java/com/topjohnwu/magisk/di/ApplicationModule.kt b/app/src/main/java/com/topjohnwu/magisk/di/ApplicationModule.kt index 617917ccc..0963a0680 100644 --- a/app/src/main/java/com/topjohnwu/magisk/di/ApplicationModule.kt +++ b/app/src/main/java/com/topjohnwu/magisk/di/ApplicationModule.kt @@ -1,11 +1,7 @@ package com.topjohnwu.magisk.di -import android.annotation.SuppressLint -import android.app.Activity -import android.app.Application import android.content.Context import android.os.Build -import android.os.Bundle import androidx.localbroadcastmanager.content.LocalBroadcastManager import androidx.preference.PreferenceManager import com.topjohnwu.magisk.core.ResMgr @@ -23,8 +19,6 @@ val applicationModule = module { factory(Protected) { createDEContext(get()) } single(SUTimeout) { get(Protected).getSharedPreferences("su_timeout", 0) } single { PreferenceManager.getDefaultSharedPreferences(get(Protected)) } - single { ActivityTracker() } - factory { get().foreground ?: NullActivity } single { LocalBroadcastManager.getInstance(get()) } } @@ -33,32 +27,3 @@ private fun createDEContext(context: Context): Context { context.createDeviceProtectedStorageContext() else context } - -class ActivityTracker : Application.ActivityLifecycleCallbacks { - - @Volatile - var foreground: Activity? = null - - override fun onActivityCreated(activity: Activity, bundle: Bundle?) {} - - override fun onActivityStarted(activity: Activity) {} - - @Synchronized - override fun onActivityResumed(activity: Activity) { - foreground = activity - } - - @Synchronized - override fun onActivityPaused(activity: Activity) { - foreground = null - } - - override fun onActivityStopped(activity: Activity) {} - - override fun onActivitySaveInstanceState(activity: Activity, bundle: Bundle) {} - - override fun onActivityDestroyed(activity: Activity) {} -} - -@SuppressLint("Registered") -object NullActivity : Activity()