From a9b00dd53724f29259bcbe4984995074f399a961 Mon Sep 17 00:00:00 2001 From: Viktor De Pasquale Date: Sat, 27 Apr 2019 11:27:42 +0200 Subject: [PATCH] Updated deprecation statements and moved components init after attaching base context This needed to be done in order to get the Koin working as it requires injection before calling onCreate --- app/src/main/java/com/topjohnwu/magisk/App.kt | 39 ++++++++----------- .../topjohnwu/magisk/di/ApplicationModule.kt | 2 + .../com/topjohnwu/magisk/di/DatabaseModule.kt | 3 +- .../magisk/ui/base/BaseActivity.java | 4 ++ .../magisk/ui/base/BaseFragment.java | 4 ++ 5 files changed, 29 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/App.kt b/app/src/main/java/com/topjohnwu/magisk/App.kt index d9372e04e..4fcec157c 100644 --- a/app/src/main/java/com/topjohnwu/magisk/App.kt +++ b/app/src/main/java/com/topjohnwu/magisk/App.kt @@ -11,12 +11,12 @@ import android.os.Build import android.os.Bundle import androidx.appcompat.app.AppCompatDelegate import androidx.multidex.MultiDex -import androidx.preference.PreferenceManager import com.topjohnwu.magisk.data.database.MagiskDB import com.topjohnwu.magisk.data.database.RepoDatabaseHelper import com.topjohnwu.magisk.di.koinModules import com.topjohnwu.magisk.utils.LocaleManager import com.topjohnwu.magisk.utils.RootUtils +import com.topjohnwu.magisk.utils.inject import com.topjohnwu.net.Networking import com.topjohnwu.superuser.Shell import org.koin.android.ext.android.inject @@ -27,36 +27,31 @@ import java.util.concurrent.ThreadPoolExecutor open class App : Application(), Application.ActivityLifecycleCallbacks { - // Global resources lateinit var protectedContext: Context - val prefs: SharedPreferences - get() = PreferenceManager.getDefaultSharedPreferences( - protectedContext - ) - val DB: MagiskDB by lazy { MagiskDB(protectedContext) } + + @Deprecated("Use dependency injection") + val prefs: SharedPreferences by inject() + @Deprecated("Use dependency injection") + val DB: MagiskDB by inject() @Deprecated("Use dependency injection") val repoDB: RepoDatabaseHelper by inject() + @Volatile private var foreground: Activity? = null - override fun onCreate() { - super.onCreate() + override fun attachBaseContext(base: Context) { + super.attachBaseContext(base) + MultiDex.install(base) + Timber.plant(Timber.DebugTree()) startKoin { androidContext(this@App) modules(koinModules) } - Timber.plant(Timber.DebugTree()) - } - - override fun attachBaseContext(base: Context) { - super.attachBaseContext(base) - MultiDex.install(base) protectedContext = baseContext self = this deContext = base - registerActivityLifecycleCallbacks(this) if (Build.VERSION.SDK_INT >= 24) { protectedContext = base.createDeviceProtectedStorageContext() @@ -64,6 +59,8 @@ open class App : Application(), Application.ActivityLifecycleCallbacks { deContext.moveSharedPreferencesFrom(base, base.defaultPrefsName) } + registerActivityLifecycleCallbacks(this) + Networking.init(base) LocaleManager.setLocale(this) } @@ -99,19 +96,17 @@ open class App : Application(), Application.ActivityLifecycleCallbacks { companion object { - //fixme this should be at least weak reference, me no likey @SuppressLint("StaticFieldLeak") @Deprecated("Use dependency injection") @JvmStatic lateinit var self: App - //fixme this should be at least weak reference, me no likey @SuppressLint("StaticFieldLeak") - @Deprecated("Use dependency injection") + @Deprecated("Use dependency injection; replace with protectedContext") @JvmStatic lateinit var deContext: Context - //fixme me no likey + @Deprecated("Use Rx or similar") @JvmField var THREAD_POOL: ThreadPoolExecutor @@ -124,11 +119,11 @@ open class App : Application(), Application.ActivityLifecycleCallbacks { THREAD_POOL = AsyncTask.THREAD_POOL_EXECUTOR as ThreadPoolExecutor } - //fixme me no likey @Deprecated("") @JvmStatic fun foreground(): Activity? { - return self.foreground + val app: App by inject() + return app.foreground } } } 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 b185ff135..861f1ce27 100644 --- a/app/src/main/java/com/topjohnwu/magisk/di/ApplicationModule.kt +++ b/app/src/main/java/com/topjohnwu/magisk/di/ApplicationModule.kt @@ -1,6 +1,7 @@ package com.topjohnwu.magisk.di import android.content.Context +import androidx.preference.PreferenceManager import com.skoumal.teanity.rxbus.RxBus import com.topjohnwu.magisk.App import org.koin.dsl.module @@ -15,4 +16,5 @@ val applicationModule = module { get().protectedContext .getSharedPreferences("su_timeout", 0) } + single { PreferenceManager.getDefaultSharedPreferences(get().protectedContext) } } diff --git a/app/src/main/java/com/topjohnwu/magisk/di/DatabaseModule.kt b/app/src/main/java/com/topjohnwu/magisk/di/DatabaseModule.kt index 27257a4df..0f79c302c 100644 --- a/app/src/main/java/com/topjohnwu/magisk/di/DatabaseModule.kt +++ b/app/src/main/java/com/topjohnwu/magisk/di/DatabaseModule.kt @@ -1,11 +1,12 @@ package com.topjohnwu.magisk.di import com.topjohnwu.magisk.App +import com.topjohnwu.magisk.data.database.MagiskDB import com.topjohnwu.magisk.data.database.RepoDatabaseHelper import org.koin.dsl.module val databaseModule = module { - single { get().DB } + single { MagiskDB(get().protectedContext) } single { RepoDatabaseHelper(get()) } } diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/base/BaseActivity.java b/app/src/main/java/com/topjohnwu/magisk/ui/base/BaseActivity.java index d348727bb..59721eb43 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/base/BaseActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/ui/base/BaseActivity.java @@ -26,6 +26,10 @@ import androidx.collection.SparseArrayCompat; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; +/** + * @deprecated This class is not to be used. It is marked for deletion. + */ +@Deprecated public abstract class BaseActivity extends AppCompatActivity implements Event.AutoListener, IBaseLeanback { static int[] EMPTY_INT_ARRAY = new int[0]; diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/base/BaseFragment.java b/app/src/main/java/com/topjohnwu/magisk/ui/base/BaseFragment.java index a6efd95b4..10be31170 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/base/BaseFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/ui/base/BaseFragment.java @@ -8,6 +8,10 @@ import com.topjohnwu.magisk.utils.Event; import androidx.fragment.app.Fragment; import butterknife.Unbinder; +/** + * @deprecated This class is not to be used. It is marked for deletion. + */ +@Deprecated public abstract class BaseFragment extends Fragment implements Event.AutoListener { public App app = App.self;