Fix ActivityTracker

Koin does not support nullable types
This commit is contained in:
topjohnwu 2019-07-29 04:18:05 -07:00
parent 7d93ca5c73
commit 9784353223
3 changed files with 17 additions and 10 deletions

View File

@ -13,6 +13,7 @@ import androidx.work.impl.WorkDatabase
import androidx.work.impl.WorkDatabase_Impl 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.ActivityTracker
import com.topjohnwu.magisk.di.koinModules import com.topjohnwu.magisk.di.koinModules
import com.topjohnwu.magisk.extensions.get import com.topjohnwu.magisk.extensions.get
import com.topjohnwu.magisk.utils.LocaleManager import com.topjohnwu.magisk.utils.LocaleManager
@ -26,7 +27,7 @@ import java.util.concurrent.ThreadPoolExecutor
open class App : Application() { open class App : Application() {
lateinit var protectedContext: Context lateinit var deContext: Context
override fun attachBaseContext(base: Context) { override fun attachBaseContext(base: Context) {
super.attachBaseContext(base) super.attachBaseContext(base)
@ -39,15 +40,15 @@ open class App : Application() {
modules(koinModules) modules(koinModules)
} }
protectedContext = baseContext deContext = base
self = this self = this
if (Build.VERSION.SDK_INT >= 24) { if (Build.VERSION.SDK_INT >= 24) {
protectedContext = base.createDeviceProtectedStorageContext() deContext = base.createDeviceProtectedStorageContext()
protectedContext.moveSharedPreferencesFrom(base, base.defaultPrefsName) deContext.moveSharedPreferencesFrom(base, defaultPrefsName)
} }
registerActivityLifecycleCallbacks(get()) registerActivityLifecycleCallbacks(get<ActivityTracker>())
Networking.init(base) Networking.init(base)
LocaleManager.setLocale(this) LocaleManager.setLocale(this)

View File

@ -1,5 +1,6 @@
package com.topjohnwu.magisk.di package com.topjohnwu.magisk.di
import android.annotation.SuppressLint
import android.app.Activity import android.app.Activity
import android.app.Application import android.app.Application
import android.content.Context import android.content.Context
@ -14,15 +15,16 @@ val applicationModule = module {
factory { get<Context>().resources } factory { get<Context>().resources }
factory { get<Context>() as App } factory { get<Context>() as App }
factory { get<Context>().packageManager } factory { get<Context>().packageManager }
factory(Protected) { get<App>().protectedContext } factory(Protected) { get<App>().deContext }
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 } single { ActivityTracker() }
factory { (get<Application.ActivityLifecycleCallbacks>() as ActivityTracker).foreground } factory { get<ActivityTracker>().foreground ?: NullActivity }
} }
private class ActivityTracker : Application.ActivityLifecycleCallbacks { class ActivityTracker : Application.ActivityLifecycleCallbacks {
@Volatile
var foreground: Activity? = null var foreground: Activity? = null
override fun onActivityCreated(activity: Activity, bundle: Bundle?) {} override fun onActivityCreated(activity: Activity, bundle: Bundle?) {}
@ -45,3 +47,6 @@ private class ActivityTracker : Application.ActivityLifecycleCallbacks {
override fun onActivityDestroyed(activity: Activity) {} override fun onActivityDestroyed(activity: Activity) {}
} }
@SuppressLint("Registered")
object NullActivity : Activity()

View File

@ -7,6 +7,7 @@ import com.skoumal.teanity.extensions.subscribeK
import com.topjohnwu.magisk.Config 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.di.NullActivity
import com.topjohnwu.magisk.extensions.firstMap import com.topjohnwu.magisk.extensions.firstMap
import com.topjohnwu.magisk.extensions.get import com.topjohnwu.magisk.extensions.get
import com.topjohnwu.magisk.extensions.writeTo import com.topjohnwu.magisk.extensions.writeTo
@ -56,7 +57,7 @@ abstract class RemoteFileService : NotificationService() {
} }
}) { }) {
val newId = finishNotify(it, subject) val newId = finishNotify(it, subject)
get<Activity?>()?.run { if (get<Activity>() !is NullActivity) {
onFinished(it, subject, newId) onFinished(it, subject, newId)
} }
} }