Only launch FlashActivity if app is foreground

This commit is contained in:
topjohnwu 2019-07-28 03:38:27 -07:00
parent 44368383f4
commit 9adfb382e8
5 changed files with 49 additions and 50 deletions

View File

@ -1,13 +1,11 @@
package com.topjohnwu.magisk package com.topjohnwu.magisk
import android.annotation.SuppressLint import android.annotation.SuppressLint
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.AsyncTask import android.os.AsyncTask
import android.os.Build import android.os.Build
import android.os.Bundle
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import androidx.multidex.MultiDex import androidx.multidex.MultiDex
import androidx.room.Room import androidx.room.Room
@ -16,7 +14,7 @@ import androidx.work.impl.WorkDatabase_Impl
import com.topjohnwu.magisk.data.database.RepoDatabase import com.topjohnwu.magisk.data.database.RepoDatabase
import com.topjohnwu.magisk.data.database.RepoDatabase_Impl import com.topjohnwu.magisk.data.database.RepoDatabase_Impl
import com.topjohnwu.magisk.di.koinModules import com.topjohnwu.magisk.di.koinModules
import com.topjohnwu.magisk.extensions.inject import com.topjohnwu.magisk.extensions.get
import com.topjohnwu.magisk.utils.LocaleManager import com.topjohnwu.magisk.utils.LocaleManager
import com.topjohnwu.magisk.utils.RootUtils import com.topjohnwu.magisk.utils.RootUtils
import com.topjohnwu.net.Networking import com.topjohnwu.net.Networking
@ -26,13 +24,10 @@ import org.koin.core.context.startKoin
import timber.log.Timber import timber.log.Timber
import java.util.concurrent.ThreadPoolExecutor import java.util.concurrent.ThreadPoolExecutor
open class App : Application(), Application.ActivityLifecycleCallbacks { open class App : Application() {
lateinit var protectedContext: Context lateinit var protectedContext: Context
@Volatile
private var foreground: Activity? = null
override fun attachBaseContext(base: Context) { override fun attachBaseContext(base: Context) {
super.attachBaseContext(base) super.attachBaseContext(base)
if (BuildConfig.DEBUG) if (BuildConfig.DEBUG)
@ -54,7 +49,7 @@ open class App : Application(), Application.ActivityLifecycleCallbacks {
deContext.moveSharedPreferencesFrom(base, base.defaultPrefsName) deContext.moveSharedPreferencesFrom(base, base.defaultPrefsName)
} }
registerActivityLifecycleCallbacks(this) registerActivityLifecycleCallbacks(get())
Networking.init(base) Networking.init(base)
LocaleManager.setLocale(this) LocaleManager.setLocale(this)
@ -65,28 +60,6 @@ open class App : Application(), Application.ActivityLifecycleCallbacks {
LocaleManager.setLocale(this) LocaleManager.setLocale(this)
} }
//region ActivityLifecycleCallbacks
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) {}
//endregion
private val Context.defaultPrefsName get() = "${packageName}_preferences" private val Context.defaultPrefsName get() = "${packageName}_preferences"
companion object { companion object {
@ -120,12 +93,5 @@ open class App : Application(), Application.ActivityLifecycleCallbacks {
} }
} }
} }
@Deprecated("")
@JvmStatic
fun foreground(): Activity? {
val app: App by inject()
return app.foreground
}
} }
} }

View File

@ -1,12 +1,14 @@
package com.topjohnwu.magisk.di package com.topjohnwu.magisk.di
import android.app.Activity
import android.app.Application
import android.content.Context import android.content.Context
import android.os.Bundle
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.skoumal.teanity.rxbus.RxBus import com.skoumal.teanity.rxbus.RxBus
import com.topjohnwu.magisk.App import com.topjohnwu.magisk.App
import org.koin.dsl.module import org.koin.dsl.module
val applicationModule = module { val applicationModule = module {
single { RxBus() } single { RxBus() }
factory { get<Context>().resources } factory { get<Context>().resources }
@ -15,4 +17,31 @@ val applicationModule = module {
factory(Protected) { get<App>().protectedContext } factory(Protected) { get<App>().protectedContext }
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() as Application.ActivityLifecycleCallbacks }
factory { (get<Application.ActivityLifecycleCallbacks>() as ActivityTracker).foreground }
}
private class ActivityTracker : Application.ActivityLifecycleCallbacks {
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) {}
}

View File

@ -6,12 +6,12 @@ import org.koin.core.qualifier.Qualifier
fun getKoin() = GlobalContext.get().koin fun getKoin() = GlobalContext.get().koin
inline fun <reified T : Any> inject( inline fun <reified T> inject(
qualifier: Qualifier? = null, qualifier: Qualifier? = null,
noinline parameters: ParametersDefinition? = null noinline parameters: ParametersDefinition? = null
) = lazy { get<T>(qualifier, parameters) } ) = lazy { get<T>(qualifier, parameters) }
inline fun <reified T : Any> get( inline fun <reified T> get(
qualifier: Qualifier? = null, qualifier: Qualifier? = null,
noinline parameters: ParametersDefinition? = null noinline parameters: ParametersDefinition? = null
): T = getKoin().get(qualifier, parameters) ): T = getKoin().get(qualifier, parameters)

View File

@ -40,7 +40,7 @@ abstract class NotificationService : Service() {
} }
} }
protected fun finishWork( protected fun finishNotify(
id: Int, id: Int,
editBody: (NotificationCompat.Builder) -> NotificationCompat.Builder? = { null } editBody: (NotificationCompat.Builder) -> NotificationCompat.Builder? = { null }
) : Int { ) : Int {

View File

@ -1,5 +1,6 @@
package com.topjohnwu.magisk.model.download package com.topjohnwu.magisk.model.download
import android.app.Activity
import android.content.Intent import android.content.Intent
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import com.skoumal.teanity.extensions.subscribeK import com.skoumal.teanity.extensions.subscribeK
@ -7,6 +8,7 @@ import com.topjohnwu.magisk.Config
import com.topjohnwu.magisk.R import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.data.network.GithubRawServices import com.topjohnwu.magisk.data.network.GithubRawServices
import com.topjohnwu.magisk.extensions.firstMap import com.topjohnwu.magisk.extensions.firstMap
import com.topjohnwu.magisk.extensions.get
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
import com.topjohnwu.magisk.model.entity.internal.DownloadSubject.Magisk import com.topjohnwu.magisk.model.entity.internal.DownloadSubject.Magisk
@ -15,7 +17,6 @@ import com.topjohnwu.magisk.utils.ProgInputStream
import com.topjohnwu.magisk.view.Notifications import com.topjohnwu.magisk.view.Notifications
import com.topjohnwu.superuser.ShellUtils import com.topjohnwu.superuser.ShellUtils
import io.reactivex.Single import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import okhttp3.ResponseBody import okhttp3.ResponseBody
import org.koin.android.ext.android.inject import org.koin.android.ext.android.inject
import timber.log.Timber import timber.log.Timber
@ -47,12 +48,15 @@ abstract class RemoteFileService : NotificationService() {
private fun start(subject: DownloadSubject) = search(subject) private fun start(subject: DownloadSubject) = search(subject)
.onErrorResumeNext(download(subject)) .onErrorResumeNext(download(subject))
.doOnSubscribe { update(subject.hashCode()) { it.setContentTitle(subject.title) } } .doOnSubscribe { update(subject.hashCode()) { it.setContentTitle(subject.title) } }
.observeOn(AndroidSchedulers.mainThread()) .subscribeK(onError = {
.doOnError { remove(subject.hashCode()) } Timber.e(it)
.doOnSuccess { remove(subject.hashCode())
val id = finish(it, subject) }) {
runCatching { onFinished(it, subject, id) }.onFailure { Timber.e(it) } val newId = finishNotify(it, subject)
}.subscribeK() get<Activity?>()?.run {
onFinished(it, subject, newId)
}
}
private fun search(subject: DownloadSubject) = Single.fromCallable { private fun search(subject: DownloadSubject) = Single.fromCallable {
if (!Config.isDownloadCacheEnabled) { if (!Config.isDownloadCacheEnabled) {
@ -98,7 +102,7 @@ abstract class RemoteFileService : NotificationService() {
} }
} }
private fun finish(file: File, subject: DownloadSubject) = finishWork(subject.hashCode()) { private fun finishNotify(file: File, subject: DownloadSubject) = finishNotify(subject.hashCode()) {
it.addActions(file, subject) it.addActions(file, subject)
.setContentText(getString(R.string.download_complete)) .setContentText(getString(R.string.download_complete))
.setSmallIcon(android.R.drawable.stat_sys_download_done) .setSmallIcon(android.R.drawable.stat_sys_download_done)