2020-02-26 05:49:15 +11:00
|
|
|
/* Copyright 2020 Tusky Contributors
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky
|
|
|
|
|
|
|
|
import android.app.Application
|
2023-02-05 06:19:01 +11:00
|
|
|
import android.content.SharedPreferences
|
2020-05-09 00:53:38 +10:00
|
|
|
import android.util.Log
|
2023-06-11 21:17:30 +10:00
|
|
|
import androidx.work.Constraints
|
|
|
|
import androidx.work.ExistingPeriodicWorkPolicy
|
|
|
|
import androidx.work.PeriodicWorkRequestBuilder
|
2020-05-13 02:46:49 +10:00
|
|
|
import androidx.work.WorkManager
|
2021-05-17 03:53:27 +10:00
|
|
|
import autodispose2.AutoDisposePlugins
|
2023-06-30 02:37:27 +10:00
|
|
|
import com.keylesspalace.tusky.components.notifications.NotificationHelper
|
2020-02-26 05:49:15 +11:00
|
|
|
import com.keylesspalace.tusky.di.AppInjector
|
2023-02-05 06:19:01 +11:00
|
|
|
import com.keylesspalace.tusky.settings.PrefKeys
|
2023-02-26 07:22:49 +11:00
|
|
|
import com.keylesspalace.tusky.settings.SCHEMA_VERSION
|
2022-12-31 23:01:35 +11:00
|
|
|
import com.keylesspalace.tusky.util.APP_THEME_DEFAULT
|
2020-11-24 06:05:48 +11:00
|
|
|
import com.keylesspalace.tusky.util.LocaleManager
|
2022-12-31 23:01:35 +11:00
|
|
|
import com.keylesspalace.tusky.util.setAppNightMode
|
2023-06-11 21:17:30 +10:00
|
|
|
import com.keylesspalace.tusky.worker.PruneCacheWorker
|
|
|
|
import com.keylesspalace.tusky.worker.WorkerFactory
|
2020-02-26 05:49:15 +11:00
|
|
|
import dagger.android.DispatchingAndroidInjector
|
|
|
|
import dagger.android.HasAndroidInjector
|
2022-04-27 02:50:58 +10:00
|
|
|
import de.c1710.filemojicompat_defaults.DefaultEmojiPackList
|
|
|
|
import de.c1710.filemojicompat_ui.helpers.EmojiPackHelper
|
|
|
|
import de.c1710.filemojicompat_ui.helpers.EmojiPreference
|
2021-05-17 03:53:27 +10:00
|
|
|
import io.reactivex.rxjava3.plugins.RxJavaPlugins
|
2020-02-26 05:49:15 +11:00
|
|
|
import org.conscrypt.Conscrypt
|
|
|
|
import java.security.Security
|
2023-06-11 21:17:30 +10:00
|
|
|
import java.util.concurrent.TimeUnit
|
2020-02-26 05:49:15 +11:00
|
|
|
import javax.inject.Inject
|
|
|
|
|
|
|
|
class TuskyApplication : Application(), HasAndroidInjector {
|
|
|
|
@Inject
|
|
|
|
lateinit var androidInjector: DispatchingAndroidInjector<Any>
|
2020-11-24 06:05:48 +11:00
|
|
|
|
2020-02-26 05:49:15 +11:00
|
|
|
@Inject
|
2023-06-11 21:17:30 +10:00
|
|
|
lateinit var workerFactory: WorkerFactory
|
2020-02-26 05:49:15 +11:00
|
|
|
|
2022-11-17 05:45:18 +11:00
|
|
|
@Inject
|
|
|
|
lateinit var localeManager: LocaleManager
|
|
|
|
|
2023-02-05 06:19:01 +11:00
|
|
|
@Inject
|
|
|
|
lateinit var sharedPreferences: SharedPreferences
|
|
|
|
|
2020-02-26 05:49:15 +11:00
|
|
|
override fun onCreate() {
|
2020-11-24 06:05:48 +11:00
|
|
|
// Uncomment me to get StrictMode violation logs
|
|
|
|
// if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
|
|
|
// StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder()
|
|
|
|
// .detectDiskReads()
|
|
|
|
// .detectDiskWrites()
|
|
|
|
// .detectNetwork()
|
|
|
|
// .detectUnbufferedIo()
|
|
|
|
// .penaltyLog()
|
|
|
|
// .build())
|
|
|
|
// }
|
2020-02-26 05:49:15 +11:00
|
|
|
super.onCreate()
|
|
|
|
|
|
|
|
Security.insertProviderAt(Conscrypt.newProvider(), 1)
|
|
|
|
|
|
|
|
AutoDisposePlugins.setHideProxies(false) // a small performance optimization
|
|
|
|
|
|
|
|
AppInjector.init(this)
|
|
|
|
|
2023-02-28 00:07:28 +11:00
|
|
|
// Migrate shared preference keys and defaults from version to version.
|
|
|
|
val oldVersion = sharedPreferences.getInt(PrefKeys.SCHEMA_VERSION, 0)
|
2023-02-26 07:22:49 +11:00
|
|
|
if (oldVersion != SCHEMA_VERSION) {
|
|
|
|
upgradeSharedPreferences(oldVersion, SCHEMA_VERSION)
|
2023-02-05 06:19:01 +11:00
|
|
|
}
|
2020-02-26 05:49:15 +11:00
|
|
|
|
2022-04-27 02:50:58 +10:00
|
|
|
// In this case, we want to have the emoji preferences merged with the other ones
|
|
|
|
// Copied from PreferenceManager.getDefaultSharedPreferenceName
|
|
|
|
EmojiPreference.sharedPreferenceName = packageName + "_preferences"
|
|
|
|
EmojiPackHelper.init(this, DefaultEmojiPackList.get(this), allowPackImports = false)
|
2020-02-26 05:49:15 +11:00
|
|
|
|
|
|
|
// init night mode
|
2023-02-05 06:19:01 +11:00
|
|
|
val theme = sharedPreferences.getString("appTheme", APP_THEME_DEFAULT)
|
2022-12-31 23:01:35 +11:00
|
|
|
setAppNightMode(theme)
|
2020-02-26 05:49:15 +11:00
|
|
|
|
2022-11-17 05:45:18 +11:00
|
|
|
localeManager.setLocale()
|
|
|
|
|
2020-05-09 00:53:38 +10:00
|
|
|
RxJavaPlugins.setErrorHandler {
|
|
|
|
Log.w("RxJava", "undeliverable exception", it)
|
|
|
|
}
|
2020-11-24 06:05:48 +11:00
|
|
|
|
2023-06-30 02:37:27 +10:00
|
|
|
NotificationHelper.createWorkerNotificationChannel(this)
|
|
|
|
|
2020-12-01 17:39:30 +11:00
|
|
|
WorkManager.initialize(
|
2021-06-29 05:13:24 +10:00
|
|
|
this,
|
|
|
|
androidx.work.Configuration.Builder()
|
2023-06-11 21:17:30 +10:00
|
|
|
.setWorkerFactory(workerFactory)
|
2021-06-29 05:13:24 +10:00
|
|
|
.build()
|
2020-12-01 17:39:30 +11:00
|
|
|
)
|
2023-06-11 21:17:30 +10:00
|
|
|
|
|
|
|
// Prune the database every ~ 12 hours when the device is idle.
|
|
|
|
val pruneCacheWorker = PeriodicWorkRequestBuilder<PruneCacheWorker>(12, TimeUnit.HOURS)
|
|
|
|
.setConstraints(Constraints.Builder().setRequiresDeviceIdle(true).build())
|
|
|
|
.build()
|
|
|
|
WorkManager.getInstance(this).enqueueUniquePeriodicWork(
|
|
|
|
PruneCacheWorker.PERIODIC_WORK_TAG,
|
|
|
|
ExistingPeriodicWorkPolicy.KEEP,
|
|
|
|
pruneCacheWorker
|
|
|
|
)
|
2020-02-26 05:49:15 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun androidInjector() = androidInjector
|
2023-02-05 06:19:01 +11:00
|
|
|
|
|
|
|
private fun upgradeSharedPreferences(oldVersion: Int, newVersion: Int) {
|
|
|
|
Log.d(TAG, "Upgrading shared preferences: $oldVersion -> $newVersion")
|
|
|
|
val editor = sharedPreferences.edit()
|
|
|
|
|
2023-02-28 00:07:28 +11:00
|
|
|
if (oldVersion < 2023022701) {
|
|
|
|
// These preferences are (now) handled in AccountPreferenceHandler. Remove them from shared for clarity.
|
|
|
|
|
|
|
|
editor.remove(PrefKeys.ALWAYS_OPEN_SPOILER)
|
|
|
|
editor.remove(PrefKeys.ALWAYS_SHOW_SENSITIVE_MEDIA)
|
|
|
|
editor.remove(PrefKeys.MEDIA_PREVIEW_ENABLED)
|
|
|
|
}
|
2023-02-05 06:19:01 +11:00
|
|
|
|
|
|
|
editor.putInt(PrefKeys.SCHEMA_VERSION, newVersion)
|
|
|
|
editor.apply()
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
private const val TAG = "TuskyApplication"
|
|
|
|
}
|
2021-06-29 05:13:24 +10:00
|
|
|
}
|