1
0
Fork 0
fenix/app/src/main/java/org/mozilla/fenix/settings/CustomizationFragment.kt

133 lines
4.7 KiB
Kotlin
Raw Normal View History

2019-04-02 02:53:37 +02:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.fenix.settings
import android.annotation.SuppressLint
2019-06-23 19:13:52 +02:00
import android.os.Build
import android.os.Build.VERSION.SDK_INT
2019-04-02 02:53:37 +02:00
import android.os.Bundle
import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.PreferenceFragmentCompat
import org.mozilla.fenix.R
import org.mozilla.fenix.components.metrics.Event
2020-07-23 04:23:38 +02:00
import org.mozilla.fenix.components.toolbar.ToolbarPosition
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.settings
2019-11-25 21:36:47 +01:00
import org.mozilla.fenix.ext.showToolbar
import org.mozilla.fenix.utils.view.addToRadioGroup
2019-04-02 02:53:37 +02:00
/**
* Lets the user customize the UI.
*/
@Suppress("TooManyFunctions")
class CustomizationFragment : PreferenceFragmentCompat() {
2019-04-02 02:53:37 +02:00
private lateinit var radioLightTheme: RadioButtonPreference
private lateinit var radioDarkTheme: RadioButtonPreference
private lateinit var radioAutoBatteryTheme: RadioButtonPreference
private lateinit var radioFollowDeviceTheme: RadioButtonPreference
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.customization_preferences, rootKey)
2019-04-02 02:53:37 +02:00
}
override fun onResume() {
super.onResume()
showToolbar(getString(R.string.preferences_customize))
2019-04-02 02:53:37 +02:00
setupPreferences()
}
private fun setupPreferences() {
bindFollowDeviceTheme()
bindDarkTheme()
bindLightTheme()
bindAutoBatteryTheme()
setupRadioGroups()
setupToolbarCategory()
2019-04-02 02:53:37 +02:00
}
private fun setupRadioGroups() {
addToRadioGroup(
radioLightTheme,
radioDarkTheme,
if (SDK_INT >= Build.VERSION_CODES.P) {
radioFollowDeviceTheme
} else {
radioAutoBatteryTheme
}
)
2019-04-02 02:53:37 +02:00
}
private fun bindLightTheme() {
radioLightTheme = requirePreference(R.string.pref_key_light_theme)
2019-04-02 02:53:37 +02:00
radioLightTheme.onClickListener {
setNewTheme(AppCompatDelegate.MODE_NIGHT_NO)
2019-04-02 02:53:37 +02:00
}
}
@SuppressLint("WrongConstant")
// Suppressing erroneous lint warning about using MODE_NIGHT_AUTO_BATTERY, a likely library bug
2019-04-02 02:53:37 +02:00
private fun bindAutoBatteryTheme() {
radioAutoBatteryTheme = requirePreference(R.string.pref_key_auto_battery_theme)
2019-04-02 02:53:37 +02:00
radioAutoBatteryTheme.onClickListener {
setNewTheme(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY)
2019-04-02 02:53:37 +02:00
}
}
private fun bindDarkTheme() {
radioDarkTheme = requirePreference(R.string.pref_key_dark_theme)
2019-04-02 02:53:37 +02:00
radioDarkTheme.onClickListener {
requireContext().components.analytics.metrics.track(
Event.DarkThemeSelected(
Event.DarkThemeSelected.Source.SETTINGS
)
)
setNewTheme(AppCompatDelegate.MODE_NIGHT_YES)
2019-04-02 02:53:37 +02:00
}
}
private fun bindFollowDeviceTheme() {
radioFollowDeviceTheme = requirePreference(R.string.pref_key_follow_device_theme)
2019-06-23 19:13:52 +02:00
if (SDK_INT >= Build.VERSION_CODES.P) {
2019-04-02 02:53:37 +02:00
radioFollowDeviceTheme.onClickListener {
setNewTheme(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
2019-04-02 02:53:37 +02:00
}
}
}
private fun setNewTheme(mode: Int) {
if (AppCompatDelegate.getDefaultNightMode() == mode) return
AppCompatDelegate.setDefaultNightMode(mode)
activity?.recreate()
with(requireComponents.core) {
engine.settings.preferredColorScheme = getPreferredColorScheme()
}
requireComponents.useCases.sessionUseCases.reload.invoke()
}
private fun setupToolbarCategory() {
val topPreference = requirePreference<RadioButtonPreference>(R.string.pref_key_toolbar_top)
topPreference.onClickListener {
requireContext().components.analytics.metrics.track(Event.ToolbarPositionChanged(
Event.ToolbarPositionChanged.Position.TOP
))
}
val bottomPreference = requirePreference<RadioButtonPreference>(R.string.pref_key_toolbar_bottom)
bottomPreference.onClickListener {
requireContext().components.analytics.metrics.track(Event.ToolbarPositionChanged(
Event.ToolbarPositionChanged.Position.BOTTOM
))
}
2020-07-23 04:23:38 +02:00
val toolbarPosition = requireContext().settings().toolbarPosition
topPreference.setCheckedWithoutClickListener(toolbarPosition == ToolbarPosition.TOP)
bottomPreference.setCheckedWithoutClickListener(toolbarPosition == ToolbarPosition.BOTTOM)
addToRadioGroup(topPreference, bottomPreference)
}
2019-04-02 02:53:37 +02:00
}