From 9784353223096ce0e38816015dce4d606d3fd777 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 29 Jul 2019 04:18:05 -0700 Subject: [PATCH] Fix ActivityTracker Koin does not support nullable types --- app/src/main/java/com/topjohnwu/magisk/App.kt | 11 ++++++----- .../com/topjohnwu/magisk/di/ApplicationModule.kt | 13 +++++++++---- .../magisk/model/download/RemoteFileService.kt | 3 ++- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/App.kt b/app/src/main/java/com/topjohnwu/magisk/App.kt index 1c6e14a5b..feba85a59 100644 --- a/app/src/main/java/com/topjohnwu/magisk/App.kt +++ b/app/src/main/java/com/topjohnwu/magisk/App.kt @@ -13,6 +13,7 @@ import androidx.work.impl.WorkDatabase 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.ActivityTracker import com.topjohnwu.magisk.di.koinModules import com.topjohnwu.magisk.extensions.get import com.topjohnwu.magisk.utils.LocaleManager @@ -26,7 +27,7 @@ import java.util.concurrent.ThreadPoolExecutor open class App : Application() { - lateinit var protectedContext: Context + lateinit var deContext: Context override fun attachBaseContext(base: Context) { super.attachBaseContext(base) @@ -39,15 +40,15 @@ open class App : Application() { modules(koinModules) } - protectedContext = baseContext + deContext = base self = this if (Build.VERSION.SDK_INT >= 24) { - protectedContext = base.createDeviceProtectedStorageContext() - protectedContext.moveSharedPreferencesFrom(base, base.defaultPrefsName) + deContext = base.createDeviceProtectedStorageContext() + deContext.moveSharedPreferencesFrom(base, defaultPrefsName) } - registerActivityLifecycleCallbacks(get()) + registerActivityLifecycleCallbacks(get()) Networking.init(base) LocaleManager.setLocale(this) 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 016dfff90..7a2788a7e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/di/ApplicationModule.kt +++ b/app/src/main/java/com/topjohnwu/magisk/di/ApplicationModule.kt @@ -1,5 +1,6 @@ package com.topjohnwu.magisk.di +import android.annotation.SuppressLint import android.app.Activity import android.app.Application import android.content.Context @@ -14,15 +15,16 @@ val applicationModule = module { factory { get().resources } factory { get() as App } factory { get().packageManager } - factory(Protected) { get().protectedContext } + factory(Protected) { get().deContext } single(SUTimeout) { get(Protected).getSharedPreferences("su_timeout", 0) } single { PreferenceManager.getDefaultSharedPreferences(get(Protected)) } - single { ActivityTracker() as Application.ActivityLifecycleCallbacks } - factory { (get() as ActivityTracker).foreground } + single { ActivityTracker() } + factory { get().foreground ?: NullActivity } } -private class ActivityTracker : Application.ActivityLifecycleCallbacks { +class ActivityTracker : Application.ActivityLifecycleCallbacks { + @Volatile var foreground: Activity? = null override fun onActivityCreated(activity: Activity, bundle: Bundle?) {} @@ -45,3 +47,6 @@ private class ActivityTracker : Application.ActivityLifecycleCallbacks { override fun onActivityDestroyed(activity: Activity) {} } + +@SuppressLint("Registered") +object NullActivity : Activity() diff --git a/app/src/main/java/com/topjohnwu/magisk/model/download/RemoteFileService.kt b/app/src/main/java/com/topjohnwu/magisk/model/download/RemoteFileService.kt index bcd11fd2a..368f0e80a 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/download/RemoteFileService.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/download/RemoteFileService.kt @@ -7,6 +7,7 @@ import com.skoumal.teanity.extensions.subscribeK import com.topjohnwu.magisk.Config import com.topjohnwu.magisk.R import com.topjohnwu.magisk.data.network.GithubRawServices +import com.topjohnwu.magisk.di.NullActivity import com.topjohnwu.magisk.extensions.firstMap import com.topjohnwu.magisk.extensions.get import com.topjohnwu.magisk.extensions.writeTo @@ -56,7 +57,7 @@ abstract class RemoteFileService : NotificationService() { } }) { val newId = finishNotify(it, subject) - get()?.run { + if (get() !is NullActivity) { onFinished(it, subject, newId) } }