OptionsFragment.kt 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package com.sduduzog.slimlauncher.ui.options
  2. import android.content.Context.MODE_PRIVATE
  3. import android.content.Intent
  4. import android.os.Bundle
  5. import android.provider.Settings
  6. import android.text.SpannableStringBuilder
  7. import android.text.Spanned
  8. import android.text.style.TextAppearanceSpan
  9. import android.view.LayoutInflater
  10. import android.view.View
  11. import android.view.ViewGroup
  12. import androidx.core.content.edit
  13. import androidx.navigation.Navigation
  14. import com.sduduzog.slimlauncher.R
  15. import com.sduduzog.slimlauncher.datasource.UnlauncherDataSource
  16. import com.sduduzog.slimlauncher.ui.dialogs.ChangeThemeDialog
  17. import com.sduduzog.slimlauncher.ui.dialogs.ChooseTimeFormatDialog
  18. import com.sduduzog.slimlauncher.utils.BaseFragment
  19. import com.sduduzog.slimlauncher.utils.createTitleAndSubtitleText
  20. import com.sduduzog.slimlauncher.utils.isActivityDefaultLauncher
  21. import dagger.hilt.android.AndroidEntryPoint
  22. import kotlinx.android.synthetic.main.options_fragment.*
  23. import javax.inject.Inject
  24. @AndroidEntryPoint
  25. class OptionsFragment : BaseFragment() {
  26. @Inject
  27. lateinit var unlauncherDataSource: UnlauncherDataSource
  28. override fun getFragmentView(): ViewGroup = options_fragment
  29. override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
  30. return inflater.inflate(R.layout.options_fragment, container, false)
  31. }
  32. override fun onActivityCreated(savedInstanceState: Bundle?) {
  33. super.onActivityCreated(savedInstanceState)
  34. options_fragment_device_settings.setOnClickListener {
  35. val intent = Intent(Settings.ACTION_SETTINGS)
  36. launchActivity(it, intent)
  37. }
  38. options_fragment_device_settings.setOnLongClickListener {
  39. val intent = Intent(Settings.ACTION_HOME_SETTINGS)
  40. launchActivity(it, intent)
  41. true
  42. }
  43. options_fragment_change_theme.setOnClickListener {
  44. val changeThemeDialog = ChangeThemeDialog.getThemeChooser()
  45. changeThemeDialog.showNow(childFragmentManager, "THEME_CHOOSER")
  46. }
  47. options_fragment_choose_time_format.setOnClickListener {
  48. val chooseTimeFormatDialog = ChooseTimeFormatDialog.getInstance()
  49. chooseTimeFormatDialog.showNow(childFragmentManager, "TIME_FORMAT_CHOOSER")
  50. }
  51. options_fragment_toggle_status_bar.setOnClickListener {
  52. val settings = requireContext().getSharedPreferences(getString(R.string.prefs_settings), MODE_PRIVATE)
  53. val isHidden = settings.getBoolean(getString(R.string.prefs_settings_key_toggle_status_bar), false)
  54. settings.edit {
  55. putBoolean(getString(R.string.prefs_settings_key_toggle_status_bar), !isHidden)
  56. }
  57. }
  58. options_fragment_customise_apps.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_optionsFragment_to_customiseAppsFragment))
  59. options_fragment_customize_quick_buttons.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_optionsFragment_to_customiseQuickButtonsFragment))
  60. options_fragment_customize_app_drawer.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_optionsFragment_to_customiseAppDrawerFragment))
  61. }
  62. override fun onStart() {
  63. super.onStart()
  64. // setting up the switch text, since changing the default launcher re-starts the activity
  65. // this should able to adapt to it.
  66. setupAutomaticDeviceWallpaperSwitch()
  67. }
  68. private fun setupAutomaticDeviceWallpaperSwitch() {
  69. val prefsRepo = unlauncherDataSource.corePreferencesRepo
  70. val appIsDefaultLauncher = isActivityDefaultLauncher(activity)
  71. setupDeviceWallpaperSwitchText(appIsDefaultLauncher)
  72. options_fragment_auto_device_theme_wallpaper.isEnabled = appIsDefaultLauncher
  73. prefsRepo.liveData().observe(viewLifecycleOwner) {
  74. // always uncheck once app isn't default launcher
  75. options_fragment_auto_device_theme_wallpaper.isChecked = appIsDefaultLauncher && !it.keepDeviceWallpaper
  76. }
  77. options_fragment_auto_device_theme_wallpaper.setOnCheckedChangeListener { _, checked ->
  78. prefsRepo.updateKeepDeviceWallpaper(!checked)
  79. }
  80. }
  81. /**
  82. * Adds a hint text underneath the default text when app is not the default launcher.
  83. */
  84. private fun setupDeviceWallpaperSwitchText(appIsDefaultLauncher: Boolean) {
  85. val text = if (appIsDefaultLauncher) {
  86. getText(R.string.customize_app_drawer_fragment_auto_theme_wallpaper_text)
  87. } else {
  88. buildSwitchTextWithHint()
  89. }
  90. options_fragment_auto_device_theme_wallpaper.text = text
  91. }
  92. private fun buildSwitchTextWithHint(): CharSequence {
  93. val titleText = getText(R.string.customize_app_drawer_fragment_auto_theme_wallpaper_text)
  94. // have a title text and a subtitle text to indicate that adapting the
  95. // wallpaper can only be done when app it the default launcher
  96. val subTitleText = getText(R.string.customize_app_drawer_fragment_auto_theme_wallpaper_subtext_no_default_launcher)
  97. return createTitleAndSubtitleText(requireContext(), titleText, subTitleText)
  98. }
  99. }