|
|
|
@ -1,27 +1,38 @@
|
|
|
|
package org.koitharu.kotatsu.settings
|
|
|
|
package org.koitharu.kotatsu.settings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
import android.content.Intent
|
|
|
|
import android.content.SharedPreferences
|
|
|
|
import android.content.SharedPreferences
|
|
|
|
|
|
|
|
import android.net.Uri
|
|
|
|
|
|
|
|
import android.os.Build
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.os.Bundle
|
|
|
|
|
|
|
|
import android.provider.Settings
|
|
|
|
import android.view.View
|
|
|
|
import android.view.View
|
|
|
|
import androidx.appcompat.app.AppCompatDelegate
|
|
|
|
import androidx.appcompat.app.AppCompatDelegate
|
|
|
|
|
|
|
|
import androidx.core.app.LocaleManagerCompat
|
|
|
|
import androidx.core.view.postDelayed
|
|
|
|
import androidx.core.view.postDelayed
|
|
|
|
import androidx.preference.ListPreference
|
|
|
|
import androidx.preference.ListPreference
|
|
|
|
import androidx.preference.Preference
|
|
|
|
import androidx.preference.Preference
|
|
|
|
import androidx.preference.TwoStatePreference
|
|
|
|
import androidx.preference.TwoStatePreference
|
|
|
|
import com.google.android.material.color.DynamicColors
|
|
|
|
import com.google.android.material.color.DynamicColors
|
|
|
|
import dagger.hilt.android.AndroidEntryPoint
|
|
|
|
import dagger.hilt.android.AndroidEntryPoint
|
|
|
|
import java.util.*
|
|
|
|
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.R
|
|
|
|
import org.koitharu.kotatsu.R
|
|
|
|
import org.koitharu.kotatsu.base.ui.BasePreferenceFragment
|
|
|
|
import org.koitharu.kotatsu.base.ui.BasePreferenceFragment
|
|
|
|
import org.koitharu.kotatsu.base.ui.util.ActivityRecreationHandle
|
|
|
|
import org.koitharu.kotatsu.base.ui.util.ActivityRecreationHandle
|
|
|
|
import org.koitharu.kotatsu.core.prefs.AppSettings
|
|
|
|
import org.koitharu.kotatsu.core.prefs.AppSettings
|
|
|
|
import org.koitharu.kotatsu.core.prefs.ListMode
|
|
|
|
import org.koitharu.kotatsu.core.prefs.ListMode
|
|
|
|
import org.koitharu.kotatsu.parsers.util.names
|
|
|
|
import org.koitharu.kotatsu.parsers.util.names
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.parsers.util.toTitleCase
|
|
|
|
import org.koitharu.kotatsu.settings.protect.ProtectSetupActivity
|
|
|
|
import org.koitharu.kotatsu.settings.protect.ProtectSetupActivity
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.settings.utils.ActivityListPreference
|
|
|
|
import org.koitharu.kotatsu.settings.utils.SliderPreference
|
|
|
|
import org.koitharu.kotatsu.settings.utils.SliderPreference
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.utils.ext.getLocalesConfig
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.utils.ext.map
|
|
|
|
import org.koitharu.kotatsu.utils.ext.setDefaultValueCompat
|
|
|
|
import org.koitharu.kotatsu.utils.ext.setDefaultValueCompat
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.utils.ext.toList
|
|
|
|
|
|
|
|
import java.util.Date
|
|
|
|
|
|
|
|
import java.util.Locale
|
|
|
|
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
|
|
|
|
|
|
|
@AndroidEntryPoint
|
|
|
|
@AndroidEntryPoint
|
|
|
|
class AppearanceSettingsFragment :
|
|
|
|
class AppearanceSettingsFragment :
|
|
|
|
@ -52,7 +63,7 @@ class AppearanceSettingsFragment :
|
|
|
|
entries = entryValues.map { value ->
|
|
|
|
entries = entryValues.map { value ->
|
|
|
|
val formattedDate = settings.getDateFormat(value.toString()).format(now)
|
|
|
|
val formattedDate = settings.getDateFormat(value.toString()).format(now)
|
|
|
|
if (value == "") {
|
|
|
|
if (value == "") {
|
|
|
|
"${context.getString(R.string.system_default)} ($formattedDate)"
|
|
|
|
getString(R.string.default_s, formattedDate)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
formattedDate
|
|
|
|
formattedDate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -62,6 +73,20 @@ class AppearanceSettingsFragment :
|
|
|
|
}
|
|
|
|
}
|
|
|
|
findPreference<TwoStatePreference>(AppSettings.KEY_PROTECT_APP)
|
|
|
|
findPreference<TwoStatePreference>(AppSettings.KEY_PROTECT_APP)
|
|
|
|
?.isChecked = !settings.appPassword.isNullOrEmpty()
|
|
|
|
?.isChecked = !settings.appPassword.isNullOrEmpty()
|
|
|
|
|
|
|
|
findPreference<ActivityListPreference>(AppSettings.KEY_APP_LOCALE)?.run {
|
|
|
|
|
|
|
|
initLocalePicker(this)
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
|
|
|
|
|
|
activityIntent = Intent(
|
|
|
|
|
|
|
|
Settings.ACTION_APP_LOCALE_SETTINGS,
|
|
|
|
|
|
|
|
Uri.fromParts("package", context.packageName, null),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
summaryProvider = Preference.SummaryProvider<ActivityListPreference> {
|
|
|
|
|
|
|
|
val locale = AppCompatDelegate.getApplicationLocales().get(0)
|
|
|
|
|
|
|
|
locale?.getDisplayName(locale)?.toTitleCase(locale) ?: getString(R.string.automatic)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
setDefaultValueCompat("")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
@ -79,16 +104,23 @@ class AppearanceSettingsFragment :
|
|
|
|
AppSettings.KEY_THEME -> {
|
|
|
|
AppSettings.KEY_THEME -> {
|
|
|
|
AppCompatDelegate.setDefaultNightMode(settings.theme)
|
|
|
|
AppCompatDelegate.setDefaultNightMode(settings.theme)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AppSettings.KEY_DYNAMIC_THEME -> {
|
|
|
|
AppSettings.KEY_DYNAMIC_THEME -> {
|
|
|
|
postRestart()
|
|
|
|
postRestart()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AppSettings.KEY_THEME_AMOLED -> {
|
|
|
|
AppSettings.KEY_THEME_AMOLED -> {
|
|
|
|
postRestart()
|
|
|
|
postRestart()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AppSettings.KEY_APP_PASSWORD -> {
|
|
|
|
AppSettings.KEY_APP_PASSWORD -> {
|
|
|
|
findPreference<TwoStatePreference>(AppSettings.KEY_PROTECT_APP)
|
|
|
|
findPreference<TwoStatePreference>(AppSettings.KEY_PROTECT_APP)
|
|
|
|
?.isChecked = !settings.appPassword.isNullOrEmpty()
|
|
|
|
?.isChecked = !settings.appPassword.isNullOrEmpty()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AppSettings.KEY_APP_LOCALE -> {
|
|
|
|
|
|
|
|
AppCompatDelegate.setApplicationLocales(settings.appLocales)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -104,6 +136,7 @@ class AppearanceSettingsFragment :
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
else -> super.onPreferenceTreeClick(preference)
|
|
|
|
else -> super.onPreferenceTreeClick(preference)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -113,4 +146,45 @@ class AppearanceSettingsFragment :
|
|
|
|
activityRecreationHandle.recreateAll()
|
|
|
|
activityRecreationHandle.recreateAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun initLocalePicker(preference: ListPreference) {
|
|
|
|
|
|
|
|
val locales = resources.getLocalesConfig()
|
|
|
|
|
|
|
|
.toList()
|
|
|
|
|
|
|
|
.sortedWith(LocaleComparator(preference.context))
|
|
|
|
|
|
|
|
preference.entries = Array(locales.size + 1) { i ->
|
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
|
|
|
|
getString(R.string.automatic)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
val lc = locales[i - 1]
|
|
|
|
|
|
|
|
lc.getDisplayName(lc).toTitleCase(lc)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
preference.entryValues = Array(locales.size + 1) { i ->
|
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
|
|
|
|
""
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
locales[i - 1].toLanguageTag()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class LocaleComparator(context: Context) : Comparator<Locale> {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private val deviceLocales = LocaleManagerCompat.getSystemLocales(context)
|
|
|
|
|
|
|
|
.map { it.language }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun compare(a: Locale, b: Locale): Int {
|
|
|
|
|
|
|
|
return if (a === b) {
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
val indexA = deviceLocales.indexOf(a.language)
|
|
|
|
|
|
|
|
val indexB = deviceLocales.indexOf(b.language)
|
|
|
|
|
|
|
|
if (indexA == -1 && indexB == -1) {
|
|
|
|
|
|
|
|
compareValues(a.language, b.language)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
-2 - (indexA - indexB)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|