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
import android.annotation.SuppressLint
import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.res.Configuration
import android.os.AsyncTask
import android.os.Build
import android.os.Bundle
import androidx.appcompat.app.AppCompatDelegate
import androidx.multidex.MultiDex
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_Impl
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.RootUtils
import com.topjohnwu.net.Networking
@ -26,13 +24,10 @@ import org.koin.core.context.startKoin
import timber.log.Timber
import java.util.concurrent.ThreadPoolExecutor
open class App : Application(), Application.ActivityLifecycleCallbacks {
open class App : Application() {
lateinit var protectedContext: Context
@Volatile
private var foreground: Activity? = null
override fun attachBaseContext(base: Context) {
super.attachBaseContext(base)
if (BuildConfig.DEBUG)
@ -54,7 +49,7 @@ open class App : Application(), Application.ActivityLifecycleCallbacks {
deContext.moveSharedPreferencesFrom(base, base.defaultPrefsName)
}
registerActivityLifecycleCallbacks(this)
registerActivityLifecycleCallbacks(get())
Networking.init(base)
LocaleManager.setLocale(this)
@ -65,28 +60,6 @@ open class App : Application(), Application.ActivityLifecycleCallbacks {
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"
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
import android.app.Activity
import android.app.Application
import android.content.Context
import android.os.Bundle
import androidx.preference.PreferenceManager
import com.skoumal.teanity.rxbus.RxBus
import com.topjohnwu.magisk.App
import org.koin.dsl.module
val applicationModule = module {
single { RxBus() }
factory { get<Context>().resources }
@ -15,4 +17,31 @@ val applicationModule = module {
factory(Protected) { get<App>().protectedContext }
single(SUTimeout) { get<Context>(Protected).getSharedPreferences("su_timeout", 0) }
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
inline fun <reified T : Any> inject(
inline fun <reified T> inject(
qualifier: Qualifier? = null,
noinline parameters: ParametersDefinition? = null
) = lazy { get<T>(qualifier, parameters) }
inline fun <reified T : Any> get(
inline fun <reified T> get(
qualifier: Qualifier? = null,
noinline parameters: ParametersDefinition? = null
): T = getKoin().get(qualifier, parameters)

View File

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

View File

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