2018-11-12 21:09:39 +01:00
|
|
|
/* Copyright 2018 Conny Duck
|
|
|
|
*
|
|
|
|
* 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.fragment.preference
|
|
|
|
|
|
|
|
import android.os.Bundle
|
2018-12-18 22:05:33 +01:00
|
|
|
import androidx.preference.Preference
|
2018-12-17 15:25:35 +01:00
|
|
|
import androidx.preference.PreferenceFragmentCompat
|
2018-11-12 21:09:39 +01:00
|
|
|
import com.keylesspalace.tusky.PreferencesActivity
|
|
|
|
import com.keylesspalace.tusky.R
|
2019-12-27 06:46:18 +01:00
|
|
|
import com.keylesspalace.tusky.components.compose.ComposeActivity
|
|
|
|
import com.keylesspalace.tusky.components.compose.ComposeActivity.ComposeOptions
|
2018-11-12 21:09:39 +01:00
|
|
|
import com.keylesspalace.tusky.util.ThemeUtils
|
|
|
|
import com.keylesspalace.tusky.util.getNonNullString
|
|
|
|
import com.mikepenz.iconics.IconicsDrawable
|
2020-04-15 18:57:53 +02:00
|
|
|
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
|
|
|
|
import com.mikepenz.iconics.utils.colorInt
|
|
|
|
import com.mikepenz.iconics.utils.sizeRes
|
2018-11-12 21:09:39 +01:00
|
|
|
|
2019-03-30 15:18:16 +01:00
|
|
|
fun PreferenceFragmentCompat.requirePreference(key: String): Preference {
|
|
|
|
return findPreference(key)!!
|
|
|
|
}
|
|
|
|
|
2018-11-12 21:09:39 +01:00
|
|
|
class PreferencesFragment : PreferenceFragmentCompat() {
|
|
|
|
|
|
|
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
|
|
|
|
|
|
|
addPreferencesFromResource(R.xml.preferences)
|
|
|
|
|
2019-03-30 15:18:16 +01:00
|
|
|
val themePreference: Preference = requirePreference("appTheme")
|
2020-04-15 18:57:53 +02:00
|
|
|
themePreference.icon = IconicsDrawable(themePreference.context, GoogleMaterial.Icon.gmd_palette).apply { sizeRes = R.dimen.preference_icon_size; colorInt = ThemeUtils.getColor(themePreference.context, R.attr.iconColor) }
|
2018-11-12 21:09:39 +01:00
|
|
|
|
2019-03-30 15:18:16 +01:00
|
|
|
val emojiPreference: Preference = requirePreference("emojiCompat")
|
2020-04-15 18:57:53 +02:00
|
|
|
emojiPreference.icon = IconicsDrawable(emojiPreference.context, GoogleMaterial.Icon.gmd_sentiment_satisfied).apply { sizeRes = R.dimen.preference_icon_size; colorInt = ThemeUtils.getColor(themePreference.context, R.attr.iconColor) }
|
2018-11-12 21:09:39 +01:00
|
|
|
|
2019-03-30 15:18:16 +01:00
|
|
|
val textSizePreference: Preference = requirePreference("statusTextSize")
|
2020-04-15 18:57:53 +02:00
|
|
|
textSizePreference.icon = IconicsDrawable(textSizePreference.context, GoogleMaterial.Icon.gmd_format_size).apply { sizeRes = R.dimen.preference_icon_size; colorInt = ThemeUtils.getColor(themePreference.context, R.attr.iconColor) }
|
2018-11-12 21:09:39 +01:00
|
|
|
|
2019-03-30 15:18:16 +01:00
|
|
|
val timelineFilterPreferences: Preference = requirePreference("timelineFilterPreferences")
|
2018-11-16 13:31:03 +01:00
|
|
|
timelineFilterPreferences.setOnPreferenceClickListener {
|
|
|
|
activity?.let { activity ->
|
|
|
|
val intent = PreferencesActivity.newIntent(activity, PreferencesActivity.TAB_FILTER_PREFERENCES)
|
|
|
|
activity.startActivity(intent)
|
|
|
|
activity.overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left)
|
2018-11-12 21:09:39 +01:00
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2019-03-30 15:18:16 +01:00
|
|
|
val httpProxyPreferences: Preference = requirePreference("httpProxyPreferences")
|
2018-11-16 13:31:03 +01:00
|
|
|
httpProxyPreferences.setOnPreferenceClickListener {
|
|
|
|
activity?.let { activity ->
|
|
|
|
val intent = PreferencesActivity.newIntent(activity, PreferencesActivity.PROXY_PREFERENCES)
|
|
|
|
activity.startActivity(intent)
|
|
|
|
activity.overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left)
|
2018-11-12 21:09:39 +01:00
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2019-03-30 15:18:16 +01:00
|
|
|
val languagePreference: Preference = requirePreference("language")
|
2020-04-15 18:57:53 +02:00
|
|
|
languagePreference.icon = IconicsDrawable(languagePreference.context, GoogleMaterial.Icon.gmd_translate).apply { sizeRes = R.dimen.preference_icon_size; colorInt = ThemeUtils.getColor(themePreference.context, R.attr.iconColor) }
|
2019-04-21 22:59:33 +02:00
|
|
|
|
|
|
|
val botIndicatorPreference = requirePreference("showBotOverlay")
|
2020-01-30 21:37:28 +01:00
|
|
|
|
|
|
|
botIndicatorPreference.icon = ThemeUtils.getTintedDrawable(requireContext(), R.drawable.ic_bot_24dp, R.attr.iconColor)
|
2019-01-16 15:23:35 +01:00
|
|
|
|
|
|
|
updateStackTracePreference()
|
2018-11-12 21:09:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
updateHttpProxySummary()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun updateHttpProxySummary() {
|
|
|
|
|
2019-03-30 15:18:16 +01:00
|
|
|
val httpProxyPref: Preference = requirePreference("httpProxyPreferences")
|
2018-11-12 21:09:39 +01:00
|
|
|
|
|
|
|
val sharedPreferences = preferenceManager.sharedPreferences
|
|
|
|
|
2019-03-08 20:33:42 +01:00
|
|
|
val httpProxyEnabled = sharedPreferences.getBoolean("httpProxyEnabled", false)
|
2018-11-12 21:09:39 +01:00
|
|
|
|
2019-03-08 20:33:42 +01:00
|
|
|
val httpServer = sharedPreferences.getNonNullString("httpProxyServer", "")
|
2018-11-12 21:09:39 +01:00
|
|
|
|
2019-03-08 20:33:42 +01:00
|
|
|
try {
|
|
|
|
val httpPort = sharedPreferences.getNonNullString("httpProxyPort", "-1").toInt()
|
2018-11-12 21:09:39 +01:00
|
|
|
|
2019-03-08 20:33:42 +01:00
|
|
|
if (httpProxyEnabled && httpServer.isNotBlank() && httpPort > 0 && httpPort < 65535) {
|
|
|
|
httpProxyPref.summary = "$httpServer:$httpPort"
|
|
|
|
return
|
2018-11-12 21:09:39 +01:00
|
|
|
}
|
2019-03-08 20:33:42 +01:00
|
|
|
} catch (e: NumberFormatException) {
|
|
|
|
// user has entered wrong port, fall back to empty summary
|
|
|
|
}
|
2018-11-12 21:09:39 +01:00
|
|
|
|
2019-03-08 20:33:42 +01:00
|
|
|
httpProxyPref.summary = ""
|
2018-11-12 21:09:39 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-01-16 15:23:35 +01:00
|
|
|
private fun updateStackTracePreference() {
|
|
|
|
|
|
|
|
val stackTraceCategory = requirePreference("stackTraceCategory")
|
|
|
|
|
|
|
|
val sharedPreferences = preferenceManager.sharedPreferences
|
|
|
|
val stackTrace = sharedPreferences.getString("stack_trace", null)
|
|
|
|
if (stackTrace.isNullOrEmpty()) {
|
|
|
|
preferenceScreen.removePreference(stackTraceCategory)
|
|
|
|
} else {
|
|
|
|
val sendCrashReportPreference = requirePreference("sendCrashReport")
|
|
|
|
sendCrashReportPreference.setOnPreferenceClickListener {
|
|
|
|
activity?.let { activity ->
|
2019-12-27 06:46:18 +01:00
|
|
|
val intent = ComposeActivity.startIntent(activity, ComposeOptions(
|
|
|
|
tootText = "@ars42525@odakyu.app $stackTrace".substring(0, 400),
|
|
|
|
contentWarning = "Yuito StackTrace"
|
|
|
|
))
|
2019-01-16 15:23:35 +01:00
|
|
|
activity.startActivity(intent)
|
|
|
|
sharedPreferences.edit()
|
|
|
|
.remove("stack_trace")
|
|
|
|
.apply()
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
val stackTracePreference = requirePreference("stackTrace")
|
|
|
|
stackTracePreference.summary = stackTrace
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-12 21:09:39 +01:00
|
|
|
companion object {
|
|
|
|
fun newInstance(): PreferencesFragment {
|
|
|
|
return PreferencesFragment()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|