2017-05-21 07:42:06 -04:00
|
|
|
package eu.kanade.tachiyomi
|
|
|
|
|
2021-04-19 15:33:08 -04:00
|
|
|
import android.os.Build
|
2020-09-14 17:52:00 -04:00
|
|
|
import androidx.core.content.edit
|
|
|
|
import androidx.preference.PreferenceManager
|
2020-03-13 17:29:51 -04:00
|
|
|
import eu.kanade.tachiyomi.data.backup.BackupCreatorJob
|
2017-05-21 07:42:06 -04:00
|
|
|
import eu.kanade.tachiyomi.data.library.LibraryUpdateJob
|
2020-09-15 17:53:01 -04:00
|
|
|
import eu.kanade.tachiyomi.data.preference.PreferenceKeys
|
2017-05-21 07:42:06 -04:00
|
|
|
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
2020-12-12 11:26:42 -05:00
|
|
|
import eu.kanade.tachiyomi.data.track.TrackManager
|
2017-12-11 14:01:28 -05:00
|
|
|
import eu.kanade.tachiyomi.data.updater.UpdaterJob
|
2020-03-20 22:53:24 -04:00
|
|
|
import eu.kanade.tachiyomi.extension.ExtensionUpdateJob
|
2021-04-11 13:10:03 -04:00
|
|
|
import eu.kanade.tachiyomi.network.PREF_DOH_CLOUDFLARE
|
2020-05-01 19:59:08 -04:00
|
|
|
import eu.kanade.tachiyomi.ui.library.LibrarySort
|
2021-06-26 13:30:16 -04:00
|
|
|
import eu.kanade.tachiyomi.ui.library.setting.SortDirectionSetting
|
|
|
|
import eu.kanade.tachiyomi.ui.library.setting.SortModeSetting
|
2021-04-28 08:32:47 -04:00
|
|
|
import eu.kanade.tachiyomi.ui.reader.setting.OrientationType
|
2020-12-13 17:52:21 -05:00
|
|
|
import eu.kanade.tachiyomi.util.system.toast
|
2020-09-14 17:52:00 -04:00
|
|
|
import eu.kanade.tachiyomi.widget.ExtendedNavigationView
|
2020-12-12 11:26:42 -05:00
|
|
|
import uy.kohesive.injekt.Injekt
|
|
|
|
import uy.kohesive.injekt.api.get
|
2017-05-21 07:42:06 -04:00
|
|
|
import java.io.File
|
|
|
|
|
|
|
|
object Migrations {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs a migration when the application is updated.
|
|
|
|
*
|
|
|
|
* @param preferences Preferences of the application.
|
|
|
|
* @return true if a migration is performed, false otherwise.
|
|
|
|
*/
|
|
|
|
fun upgrade(preferences: PreferencesHelper): Boolean {
|
|
|
|
val context = preferences.context
|
2020-04-24 10:39:55 -04:00
|
|
|
|
2020-12-12 11:26:42 -05:00
|
|
|
val oldVersion = preferences.lastVersionCode().get()
|
2017-05-21 07:42:06 -04:00
|
|
|
if (oldVersion < BuildConfig.VERSION_CODE) {
|
|
|
|
preferences.lastVersionCode().set(BuildConfig.VERSION_CODE)
|
|
|
|
|
2021-08-13 18:28:07 -04:00
|
|
|
// Always set up background tasks to ensure they're running
|
|
|
|
if (BuildConfig.INCLUDE_UPDATER) {
|
|
|
|
UpdaterJob.setupTask(context)
|
|
|
|
}
|
|
|
|
ExtensionUpdateJob.setupTask(context)
|
|
|
|
LibraryUpdateJob.setupTask(context)
|
|
|
|
BackupCreatorJob.setupTask(context)
|
|
|
|
|
2020-03-07 13:11:02 -05:00
|
|
|
// Fresh install
|
|
|
|
if (oldVersion == 0) {
|
|
|
|
return false
|
|
|
|
}
|
2017-05-21 07:42:06 -04:00
|
|
|
|
|
|
|
if (oldVersion < 14) {
|
2020-03-07 13:00:58 -05:00
|
|
|
// Restore jobs after upgrading to Evernote's job scheduler.
|
2020-04-24 10:39:55 -04:00
|
|
|
if (BuildConfig.INCLUDE_UPDATER) {
|
2020-03-07 13:00:58 -05:00
|
|
|
UpdaterJob.setupTask(context)
|
2017-05-21 07:42:06 -04:00
|
|
|
}
|
2020-03-07 13:00:58 -05:00
|
|
|
LibraryUpdateJob.setupTask(context)
|
2017-05-21 07:42:06 -04:00
|
|
|
}
|
|
|
|
if (oldVersion < 15) {
|
|
|
|
// Delete internal chapter cache dir.
|
|
|
|
File(context.cacheDir, "chapter_disk_cache").deleteRecursively()
|
|
|
|
}
|
|
|
|
if (oldVersion < 19) {
|
|
|
|
// Move covers to external files dir.
|
|
|
|
val oldDir = File(context.externalCacheDir, "cover_disk_cache")
|
|
|
|
if (oldDir.exists()) {
|
|
|
|
val destDir = context.getExternalFilesDir("covers")
|
|
|
|
if (destDir != null) {
|
2020-03-07 13:00:58 -05:00
|
|
|
oldDir.listFiles()?.forEach {
|
2017-05-21 07:42:06 -04:00
|
|
|
it.renameTo(File(destDir, it.name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-10 06:29:04 -04:00
|
|
|
if (oldVersion < 26) {
|
|
|
|
// Delete external chapter cache dir.
|
|
|
|
val extCache = context.externalCacheDir
|
|
|
|
if (extCache != null) {
|
|
|
|
val chapterCache = File(extCache, "chapter_disk_cache")
|
|
|
|
if (chapterCache.exists()) {
|
|
|
|
chapterCache.deleteRecursively()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-13 17:29:51 -04:00
|
|
|
if (oldVersion < 43) {
|
|
|
|
// Restore jobs after migrating from Evernote's job scheduler to WorkManager.
|
2020-04-24 10:39:55 -04:00
|
|
|
if (BuildConfig.INCLUDE_UPDATER) {
|
2020-03-13 17:29:51 -04:00
|
|
|
UpdaterJob.setupTask(context)
|
|
|
|
}
|
|
|
|
LibraryUpdateJob.setupTask(context)
|
|
|
|
BackupCreatorJob.setupTask(context)
|
2020-04-29 09:09:18 -04:00
|
|
|
|
|
|
|
// New extension update check job
|
2020-04-29 09:42:56 -04:00
|
|
|
ExtensionUpdateJob.setupTask(context)
|
2020-03-13 17:29:51 -04:00
|
|
|
}
|
2020-05-01 19:59:08 -04:00
|
|
|
if (oldVersion < 44) {
|
|
|
|
// Reset sorting preference if using removed sort by source
|
2021-06-26 13:30:16 -04:00
|
|
|
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
|
|
|
|
|
|
|
val oldSortingMode = prefs.getInt(PreferenceKeys.librarySortingMode, 0)
|
|
|
|
|
2020-12-12 23:50:28 -05:00
|
|
|
@Suppress("DEPRECATION")
|
2021-06-26 13:30:16 -04:00
|
|
|
if (oldSortingMode == LibrarySort.SOURCE) {
|
|
|
|
prefs.edit {
|
|
|
|
putInt(PreferenceKeys.librarySortingMode, LibrarySort.ALPHA)
|
|
|
|
}
|
2020-05-01 19:59:08 -04:00
|
|
|
}
|
|
|
|
}
|
2020-09-14 17:52:00 -04:00
|
|
|
if (oldVersion < 52) {
|
|
|
|
// Migrate library filters to tri-state versions
|
|
|
|
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
|
|
|
fun convertBooleanPrefToTriState(key: String): Int {
|
|
|
|
val oldPrefValue = prefs.getBoolean(key, false)
|
2020-09-27 18:13:20 -04:00
|
|
|
return if (oldPrefValue) ExtendedNavigationView.Item.TriStateGroup.State.INCLUDE.value
|
|
|
|
else ExtendedNavigationView.Item.TriStateGroup.State.IGNORE.value
|
2020-09-14 17:52:00 -04:00
|
|
|
}
|
|
|
|
prefs.edit {
|
2020-09-15 17:53:01 -04:00
|
|
|
putInt(PreferenceKeys.filterDownloaded, convertBooleanPrefToTriState("pref_filter_downloaded_key"))
|
2020-09-14 17:52:00 -04:00
|
|
|
remove("pref_filter_downloaded_key")
|
2020-09-15 17:53:01 -04:00
|
|
|
|
|
|
|
putInt(PreferenceKeys.filterUnread, convertBooleanPrefToTriState("pref_filter_unread_key"))
|
2020-09-14 17:52:00 -04:00
|
|
|
remove("pref_filter_unread_key")
|
2020-09-15 17:53:01 -04:00
|
|
|
|
|
|
|
putInt(PreferenceKeys.filterCompleted, convertBooleanPrefToTriState("pref_filter_completed_key"))
|
2020-09-14 17:52:00 -04:00
|
|
|
remove("pref_filter_completed_key")
|
|
|
|
}
|
2020-12-18 23:20:42 -05:00
|
|
|
}
|
2020-12-21 17:10:08 -05:00
|
|
|
if (oldVersion < 54) {
|
2020-12-12 11:26:42 -05:00
|
|
|
// Force MAL log out due to login flow change
|
2020-12-18 23:20:42 -05:00
|
|
|
// v52: switched from scraping to WebView
|
|
|
|
// v53: switched from WebView to OAuth
|
2020-12-12 11:26:42 -05:00
|
|
|
val trackManager = Injekt.get<TrackManager>()
|
2020-12-13 17:52:21 -05:00
|
|
|
if (trackManager.myAnimeList.isLogged) {
|
|
|
|
trackManager.myAnimeList.logout()
|
|
|
|
context.toast(R.string.myanimelist_relogin)
|
|
|
|
}
|
2020-09-14 17:52:00 -04:00
|
|
|
}
|
2021-04-11 13:10:03 -04:00
|
|
|
if (oldVersion < 57) {
|
|
|
|
// Migrate DNS over HTTPS setting
|
|
|
|
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
|
|
|
val wasDohEnabled = prefs.getBoolean("enable_doh", false)
|
|
|
|
if (wasDohEnabled) {
|
|
|
|
prefs.edit {
|
|
|
|
putInt(PreferenceKeys.dohProvider, PREF_DOH_CLOUDFLARE)
|
|
|
|
remove("enable_doh")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-23 22:37:43 -04:00
|
|
|
if (oldVersion < 59) {
|
|
|
|
// Reset rotation to Free after replacing Lock
|
2021-04-28 08:32:47 -04:00
|
|
|
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
|
|
|
if (prefs.contains("pref_rotation_type_key")) {
|
|
|
|
prefs.edit {
|
|
|
|
putInt("pref_rotation_type_key", 1)
|
|
|
|
}
|
|
|
|
}
|
2021-04-19 15:33:08 -04:00
|
|
|
|
|
|
|
// Disable update check for Android 5.x users
|
|
|
|
if (BuildConfig.INCLUDE_UPDATER && Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
|
|
|
|
UpdaterJob.cancelTask(context)
|
|
|
|
}
|
2021-04-23 22:37:43 -04:00
|
|
|
}
|
2021-04-28 08:32:47 -04:00
|
|
|
if (oldVersion < 60) {
|
2021-04-28 08:55:30 -04:00
|
|
|
// Re-enable update check that was prevously accidentally disabled for M
|
|
|
|
if (BuildConfig.INCLUDE_UPDATER && Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
|
|
|
|
UpdaterJob.setupTask(context)
|
|
|
|
}
|
|
|
|
|
2021-04-28 08:32:47 -04:00
|
|
|
// Migrate Rotation and Viewer values to default values for viewer_flags
|
|
|
|
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
|
|
|
val newOrientation = when (prefs.getInt("pref_rotation_type_key", 1)) {
|
|
|
|
1 -> OrientationType.FREE.flagValue
|
|
|
|
2 -> OrientationType.PORTRAIT.flagValue
|
|
|
|
3 -> OrientationType.LANDSCAPE.flagValue
|
|
|
|
4 -> OrientationType.LOCKED_PORTRAIT.flagValue
|
|
|
|
5 -> OrientationType.LOCKED_LANDSCAPE.flagValue
|
|
|
|
else -> OrientationType.FREE.flagValue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reading mode flag and prefValue is the same value
|
|
|
|
val newReadingMode = prefs.getInt("pref_default_viewer_key", 1)
|
|
|
|
|
|
|
|
prefs.edit {
|
|
|
|
putInt("pref_default_orientation_type_key", newOrientation)
|
|
|
|
remove("pref_rotation_type_key")
|
|
|
|
putInt("pref_default_reading_mode_key", newReadingMode)
|
|
|
|
remove("pref_default_viewer_key")
|
|
|
|
}
|
|
|
|
}
|
2021-05-22 11:52:28 -04:00
|
|
|
if (oldVersion < 61) {
|
|
|
|
// Handle removed every 1 or 2 hour library updates
|
|
|
|
val updateInterval = preferences.libraryUpdateInterval().get()
|
|
|
|
if (updateInterval == 1 || updateInterval == 2) {
|
|
|
|
preferences.libraryUpdateInterval().set(3)
|
|
|
|
LibraryUpdateJob.setupTask(context, 3)
|
|
|
|
}
|
|
|
|
}
|
2021-06-26 13:30:16 -04:00
|
|
|
if (oldVersion < 64) {
|
|
|
|
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
|
|
|
|
|
|
|
val oldSortingMode = prefs.getInt(PreferenceKeys.librarySortingMode, 0)
|
|
|
|
val oldSortingDirection = prefs.getBoolean(PreferenceKeys.librarySortingDirection, true)
|
|
|
|
|
|
|
|
@Suppress("DEPRECATION")
|
|
|
|
val newSortingMode = when (oldSortingMode) {
|
|
|
|
LibrarySort.ALPHA -> SortModeSetting.ALPHABETICAL
|
|
|
|
LibrarySort.LAST_READ -> SortModeSetting.LAST_READ
|
|
|
|
LibrarySort.LAST_CHECKED -> SortModeSetting.LAST_CHECKED
|
|
|
|
LibrarySort.UNREAD -> SortModeSetting.UNREAD
|
|
|
|
LibrarySort.TOTAL -> SortModeSetting.TOTAL_CHAPTERS
|
|
|
|
LibrarySort.LATEST_CHAPTER -> SortModeSetting.LATEST_CHAPTER
|
|
|
|
LibrarySort.CHAPTER_FETCH_DATE -> SortModeSetting.DATE_FETCHED
|
|
|
|
LibrarySort.DATE_ADDED -> SortModeSetting.DATE_ADDED
|
|
|
|
else -> SortModeSetting.ALPHABETICAL
|
|
|
|
}
|
|
|
|
|
|
|
|
val newSortingDirection = when (oldSortingDirection) {
|
|
|
|
true -> SortDirectionSetting.ASCENDING
|
|
|
|
else -> SortDirectionSetting.DESCENDING
|
|
|
|
}
|
|
|
|
|
|
|
|
prefs.edit(commit = true) {
|
|
|
|
remove(PreferenceKeys.librarySortingMode)
|
|
|
|
remove(PreferenceKeys.librarySortingDirection)
|
|
|
|
}
|
|
|
|
|
|
|
|
prefs.edit {
|
|
|
|
putString(PreferenceKeys.librarySortingMode, newSortingMode.name)
|
|
|
|
putString(PreferenceKeys.librarySortingDirection, newSortingDirection.name)
|
|
|
|
}
|
|
|
|
}
|
2021-08-13 18:28:07 -04:00
|
|
|
|
2020-07-04 14:36:56 -04:00
|
|
|
return true
|
2017-05-21 07:42:06 -04:00
|
|
|
}
|
2020-12-12 11:26:42 -05:00
|
|
|
|
2017-05-21 07:42:06 -04:00
|
|
|
return false
|
|
|
|
}
|
2020-02-26 18:03:34 -05:00
|
|
|
}
|