Create ForegroundTracker

This commit is contained in:
topjohnwu 2020-06-17 04:07:31 -07:00
parent e7f1c03151
commit eecb66f4f1
3 changed files with 27 additions and 42 deletions

View File

@ -1,8 +1,10 @@
package com.topjohnwu.magisk.core package com.topjohnwu.magisk.core
import android.app.Activity
import android.app.Application import android.app.Application
import android.content.Context import android.content.Context
import android.content.res.Configuration import android.content.res.Configuration
import android.os.Bundle
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import androidx.multidex.MultiDex import androidx.multidex.MultiDex
import androidx.work.WorkManager 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.su.SuCallbackHandler
import com.topjohnwu.magisk.core.utils.RootInit import com.topjohnwu.magisk.core.utils.RootInit
import com.topjohnwu.magisk.core.utils.updateConfig import com.topjohnwu.magisk.core.utils.updateConfig
import com.topjohnwu.magisk.di.ActivityTracker
import com.topjohnwu.magisk.di.koinModules import com.topjohnwu.magisk.di.koinModules
import com.topjohnwu.magisk.extensions.get
import com.topjohnwu.magisk.extensions.unwrap import com.topjohnwu.magisk.extensions.unwrap
import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.Shell
import org.koin.android.ext.koin.androidContext import org.koin.android.ext.koin.androidContext
@ -67,7 +67,7 @@ open class App() : Application() {
modules(koinModules) modules(koinModules)
} }
ResMgr.init(impl) ResMgr.init(impl)
app.registerActivityLifecycleCallbacks(get<ActivityTracker>()) app.registerActivityLifecycleCallbacks(ForegroundTracker)
WorkManager.initialize(impl.wrapJob(), androidx.work.Configuration.Builder().build()) WorkManager.initialize(impl.wrapJob(), androidx.work.Configuration.Builder().build())
} }
@ -82,3 +82,25 @@ open class App() : Application() {
super.onConfigurationChanged(newConfig) 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) {}
}

View File

@ -1,16 +1,14 @@
package com.topjohnwu.magisk.core.download package com.topjohnwu.magisk.core.download
import android.app.Activity
import android.app.Notification import android.app.Notification
import android.content.Intent import android.content.Intent
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import com.topjohnwu.magisk.R import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.ForegroundTracker
import com.topjohnwu.magisk.core.utils.ProgressInputStream import com.topjohnwu.magisk.core.utils.ProgressInputStream
import com.topjohnwu.magisk.core.view.Notifications import com.topjohnwu.magisk.core.view.Notifications
import com.topjohnwu.magisk.data.network.GithubRawServices 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.subscribeK
import com.topjohnwu.magisk.extensions.writeTo import com.topjohnwu.magisk.extensions.writeTo
import com.topjohnwu.magisk.model.entity.internal.DownloadSubject import com.topjohnwu.magisk.model.entity.internal.DownloadSubject
@ -47,7 +45,7 @@ abstract class RemoteFileService : NotificationService() {
failNotify(subject) failNotify(subject)
}) { }) {
val newId = finishNotify(subject) val newId = finishNotify(subject)
if (get<Activity>() !is NullActivity) { if (ForegroundTracker.hasForeground) {
onFinished(subject, newId) onFinished(subject, newId)
} }
} }

View File

@ -1,11 +1,7 @@
package com.topjohnwu.magisk.di package com.topjohnwu.magisk.di
import android.annotation.SuppressLint
import android.app.Activity
import android.app.Application
import android.content.Context import android.content.Context
import android.os.Build import android.os.Build
import android.os.Bundle
import androidx.localbroadcastmanager.content.LocalBroadcastManager import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.topjohnwu.magisk.core.ResMgr import com.topjohnwu.magisk.core.ResMgr
@ -23,8 +19,6 @@ val applicationModule = module {
factory(Protected) { createDEContext(get()) } factory(Protected) { createDEContext(get()) }
single(SUTimeout) { get<Context>(Protected).getSharedPreferences("su_timeout", 0) } single(SUTimeout) { get<Context>(Protected).getSharedPreferences("su_timeout", 0) }
single { PreferenceManager.getDefaultSharedPreferences(get<Context>(Protected)) } single { PreferenceManager.getDefaultSharedPreferences(get<Context>(Protected)) }
single { ActivityTracker() }
factory { get<ActivityTracker>().foreground ?: NullActivity }
single { LocalBroadcastManager.getInstance(get()) } single { LocalBroadcastManager.getInstance(get()) }
} }
@ -33,32 +27,3 @@ private fun createDEContext(context: Context): Context {
context.createDeviceProtectedStorageContext() context.createDeviceProtectedStorageContext()
else context 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()