2021-12-02 18:11:34 +00:00
|
|
|
package corewala.buran.ui.settings
|
|
|
|
|
|
|
|
import android.app.Activity.RESULT_OK
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
import android.content.SharedPreferences
|
|
|
|
import android.graphics.Color
|
|
|
|
import android.graphics.drawable.ColorDrawable
|
|
|
|
import android.net.Uri
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.provider.OpenableColumns
|
|
|
|
import android.view.inputmethod.EditorInfo
|
|
|
|
import androidx.appcompat.app.AppCompatDelegate
|
|
|
|
import androidx.preference.*
|
|
|
|
import corewala.buran.Buran
|
|
|
|
import corewala.buran.R
|
|
|
|
|
|
|
|
|
|
|
|
const val PREFS_SET_CLIENT_CERT_REQ = 20
|
|
|
|
|
|
|
|
class SettingsFragment: PreferenceFragmentCompat(), Preference.OnPreferenceChangeListener {
|
|
|
|
|
|
|
|
lateinit var prefs: SharedPreferences
|
|
|
|
|
|
|
|
private lateinit var clientCertPref: Preference
|
|
|
|
private lateinit var useClientCertPreference: SwitchPreferenceCompat
|
|
|
|
|
|
|
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
|
|
|
|
|
|
|
prefs = preferenceManager.sharedPreferences
|
|
|
|
|
|
|
|
val context = preferenceManager.context
|
|
|
|
val screen = preferenceManager.createPreferenceScreen(context)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Buran App Settings
|
|
|
|
*/
|
|
|
|
val appCategory = PreferenceCategory(context)
|
|
|
|
appCategory.key = "app_category"
|
|
|
|
appCategory.title = getString(R.string.configure_buran)
|
|
|
|
screen.addPreference(appCategory)
|
|
|
|
|
|
|
|
//Home ---------------------------------------------
|
|
|
|
val homePreference = EditTextPreference(context)
|
|
|
|
homePreference.title = getString(R.string.home_capsule)
|
|
|
|
homePreference.key = "home_capsule"
|
|
|
|
homePreference.dialogTitle = getString(R.string.home_capsule)
|
|
|
|
|
|
|
|
val homecapsule = preferenceManager.sharedPreferences.getString(
|
|
|
|
"home_capsule",
|
|
|
|
Buran.DEFAULT_HOME_CAPSULE
|
|
|
|
)
|
|
|
|
|
|
|
|
homePreference.summary = homecapsule
|
|
|
|
homePreference.positiveButtonText = getString(R.string.update)
|
|
|
|
homePreference.negativeButtonText = getString(R.string.cancel)
|
|
|
|
homePreference.title = getString(R.string.home_capsule)
|
|
|
|
homePreference.setOnPreferenceChangeListener { _, newValue ->
|
|
|
|
homePreference.summary = newValue.toString()
|
|
|
|
true
|
|
|
|
}
|
|
|
|
homePreference.setOnBindEditTextListener{ editText ->
|
|
|
|
editText.imeOptions = EditorInfo.IME_ACTION_DONE
|
|
|
|
editText.setSelection(editText.text.toString().length)//Set caret position to end
|
|
|
|
}
|
|
|
|
appCategory.addPreference(homePreference)
|
|
|
|
|
2022-04-20 19:28:11 +00:00
|
|
|
//Search ---------------------------------------------
|
|
|
|
val searchPreference = EditTextPreference(context)
|
|
|
|
searchPreference.title = getString(R.string.search_engine)
|
|
|
|
searchPreference.key = "search_base"
|
|
|
|
searchPreference.dialogTitle = getString(R.string.search_base)
|
|
|
|
|
|
|
|
val searchengine = preferenceManager.sharedPreferences.getString(
|
|
|
|
"search_base",
|
|
|
|
Buran.DEFAULT_SEARCH_BASE
|
|
|
|
)
|
|
|
|
|
|
|
|
searchPreference.summary = searchengine
|
|
|
|
searchPreference.positiveButtonText = getString(R.string.update)
|
|
|
|
searchPreference.negativeButtonText = getString(R.string.cancel)
|
|
|
|
searchPreference.title = getString(R.string.search_engine)
|
|
|
|
searchPreference.setOnPreferenceChangeListener { _, newValue ->
|
|
|
|
searchPreference.summary = newValue.toString()
|
|
|
|
true
|
|
|
|
}
|
|
|
|
searchPreference.setOnBindEditTextListener{ editText ->
|
|
|
|
editText.imeOptions = EditorInfo.IME_ACTION_DONE
|
|
|
|
editText.setSelection(editText.text.toString().length)//Set caret position to end
|
|
|
|
}
|
|
|
|
appCategory.addPreference(searchPreference)
|
|
|
|
|
2022-03-13 20:58:10 +00:00
|
|
|
//Updates ---------------------------------------------
|
|
|
|
val checkForUpdates = SwitchPreferenceCompat(context)
|
|
|
|
checkForUpdates.setDefaultValue(true)
|
|
|
|
checkForUpdates.key = "check_for_updates"
|
|
|
|
checkForUpdates.title = getString(R.string.check_for_updates)
|
|
|
|
appCategory.addPreference(checkForUpdates)
|
|
|
|
|
2022-05-03 16:25:57 +00:00
|
|
|
//Certificates
|
|
|
|
buildClientCertificateSection(context, screen)
|
2021-12-02 18:11:34 +00:00
|
|
|
|
2021-12-08 20:42:41 +00:00
|
|
|
//Appearance --------------------------------------------
|
|
|
|
buildAppearanceSection(context, appCategory)
|
2021-12-02 18:11:34 +00:00
|
|
|
|
|
|
|
//Accessibility ------------------------------------
|
|
|
|
buildsAccessibility(context, screen)
|
|
|
|
|
|
|
|
//Web ----------------------------------------------
|
|
|
|
buildWebSection(context, screen)
|
|
|
|
|
|
|
|
preferenceScreen = screen
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun buildWebSection(context: Context?, screen: PreferenceScreen){
|
|
|
|
val webCategory = PreferenceCategory(context)
|
|
|
|
webCategory.key = "web_category"
|
|
|
|
webCategory.title = getString(R.string.web_content)
|
|
|
|
screen.addPreference(webCategory)
|
|
|
|
|
2021-12-14 23:22:14 +00:00
|
|
|
val aboutCustomTabPref = Preference(context)
|
|
|
|
aboutCustomTabPref.summary = getString(R.string.web_content_label)
|
|
|
|
aboutCustomTabPref.isPersistent = false
|
|
|
|
aboutCustomTabPref.isSelectable = false
|
|
|
|
webCategory.addPreference(aboutCustomTabPref)
|
2021-12-02 18:11:34 +00:00
|
|
|
|
|
|
|
val useCustomTabsPreference = SwitchPreferenceCompat(context)
|
|
|
|
useCustomTabsPreference.setDefaultValue(true)
|
|
|
|
useCustomTabsPreference.key = Buran.PREF_KEY_USE_CUSTOM_TAB
|
|
|
|
useCustomTabsPreference.title = getString(R.string.web_content_switch_label)
|
|
|
|
webCategory.addPreference(useCustomTabsPreference)
|
|
|
|
|
2022-01-09 17:22:36 +00:00
|
|
|
val showInlineImages = SwitchPreferenceCompat(context)
|
2022-01-09 17:27:24 +00:00
|
|
|
showInlineImages.setDefaultValue(false)
|
2022-01-09 17:22:36 +00:00
|
|
|
showInlineImages.key = "show_inline_images"
|
|
|
|
showInlineImages.title = getString(R.string.show_inline_images)
|
|
|
|
webCategory.addPreference(showInlineImages)
|
|
|
|
|
2021-12-02 18:11:34 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 20:42:41 +00:00
|
|
|
private fun buildAppearanceSection(context: Context?, appCategory: PreferenceCategory) {
|
|
|
|
val appearanceCategory = PreferenceCategory(context)
|
|
|
|
appearanceCategory.key = "appearance_category"
|
|
|
|
appearanceCategory.title = getString(R.string.appearance)
|
|
|
|
appCategory.addPreference(appearanceCategory)
|
2021-12-02 18:11:34 +00:00
|
|
|
|
2021-12-08 18:57:06 +00:00
|
|
|
val themeLabels = mutableListOf<String>()
|
|
|
|
val themeValues = mutableListOf<String>()
|
|
|
|
themeLabels.add(getString(R.string.system_default))
|
|
|
|
themeLabels.add(getString(R.string.light))
|
|
|
|
themeLabels.add(getString(R.string.dark))
|
|
|
|
themeValues.add("theme_FollowSystem")
|
|
|
|
themeValues.add("theme_Light")
|
|
|
|
themeValues.add("theme_Dark")
|
|
|
|
|
|
|
|
val themePreference = ListPreference(context)
|
|
|
|
themePreference.key = "theme"
|
|
|
|
themePreference.setDialogTitle(R.string.theme)
|
|
|
|
themePreference.setTitle(R.string.theme)
|
|
|
|
themePreference.setSummary(R.string.prefs_override_theme)
|
|
|
|
themePreference.setDefaultValue("theme_FollowSystem")
|
|
|
|
themePreference.entries = themeLabels.toTypedArray()
|
|
|
|
themePreference.entryValues = themeValues.toTypedArray()
|
2021-12-08 20:42:41 +00:00
|
|
|
appearanceCategory.addPreference(themePreference)
|
2021-12-08 18:57:06 +00:00
|
|
|
|
|
|
|
themePreference.setOnPreferenceChangeListener{ _, theme ->
|
|
|
|
when (theme) {
|
|
|
|
"theme_FollowSystem" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
|
|
|
|
"theme_Light" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
|
|
|
|
"theme_Dark" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
2021-12-02 18:11:34 +00:00
|
|
|
|
|
|
|
val coloursCSV = resources.openRawResource(R.raw.colours).bufferedReader().use { it.readLines() }
|
|
|
|
|
2021-12-08 18:57:06 +00:00
|
|
|
val colourLabels = mutableListOf<String>()
|
|
|
|
val colourValues = mutableListOf<String>()
|
2021-12-02 18:11:34 +00:00
|
|
|
|
|
|
|
coloursCSV.forEach{ line ->
|
|
|
|
val colour = line.split(",")
|
2021-12-08 18:57:06 +00:00
|
|
|
colourLabels.add(colour[0])
|
|
|
|
colourValues.add(colour[1])
|
2021-12-02 18:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val backgroundColourPreference = ListPreference(context)
|
2021-12-03 17:18:16 +00:00
|
|
|
backgroundColourPreference.key = "background_colour"
|
2021-12-02 18:11:34 +00:00
|
|
|
backgroundColourPreference.setDialogTitle(R.string.prefs_override_page_background_dialog_title)
|
|
|
|
backgroundColourPreference.setTitle(R.string.prefs_override_page_background_title)
|
|
|
|
backgroundColourPreference.setSummary(R.string.prefs_override_page_background)
|
|
|
|
backgroundColourPreference.setDefaultValue("#XXXXXX")
|
2021-12-08 18:57:06 +00:00
|
|
|
backgroundColourPreference.entries = colourLabels.toTypedArray()
|
|
|
|
backgroundColourPreference.entryValues = colourValues.toTypedArray()
|
2021-12-02 18:11:34 +00:00
|
|
|
|
|
|
|
backgroundColourPreference.setOnPreferenceChangeListener { _, colour ->
|
|
|
|
when (colour) {
|
|
|
|
"#XXXXXX" -> this.view?.background = null
|
|
|
|
else -> this.view?.background = ColorDrawable(Color.parseColor("$colour"))
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2021-12-08 20:42:41 +00:00
|
|
|
appearanceCategory.addPreference(backgroundColourPreference)
|
2021-12-02 18:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun buildsAccessibility(context: Context?, screen: PreferenceScreen){
|
|
|
|
val accessibilityCategory = PreferenceCategory(context)
|
|
|
|
accessibilityCategory.key = "accessibility_category"
|
|
|
|
accessibilityCategory.title = getString(R.string.accessibility)
|
|
|
|
screen.addPreference(accessibilityCategory)
|
|
|
|
|
|
|
|
//Accessibility - inline icons
|
|
|
|
val showInlineIconsPreference = SwitchPreferenceCompat(context)
|
2021-12-03 17:16:33 +00:00
|
|
|
showInlineIconsPreference.setDefaultValue(true)
|
2021-12-02 18:11:34 +00:00
|
|
|
showInlineIconsPreference.key = "show_inline_icons"
|
|
|
|
showInlineIconsPreference.title = getString(R.string.show_inline_icons)
|
|
|
|
accessibilityCategory.addPreference(showInlineIconsPreference)
|
2022-01-27 01:21:43 +00:00
|
|
|
|
|
|
|
//Accessibility - full-width buttons
|
|
|
|
val showLinkButtonsPreference = SwitchPreferenceCompat(context)
|
|
|
|
showLinkButtonsPreference.setDefaultValue(false)
|
|
|
|
showLinkButtonsPreference.key = "show_link_buttons"
|
2022-01-27 01:28:33 +00:00
|
|
|
showLinkButtonsPreference.title = getString(R.string.show_link_buttons)
|
2022-01-27 01:21:43 +00:00
|
|
|
accessibilityCategory.addPreference(showLinkButtonsPreference)
|
2021-12-02 18:11:34 +00:00
|
|
|
}
|
|
|
|
|
2022-05-03 16:25:57 +00:00
|
|
|
private fun buildClientCertificateSection(context: Context?, screen: PreferenceScreen) {
|
2021-12-02 18:11:34 +00:00
|
|
|
|
2022-05-03 16:25:57 +00:00
|
|
|
val certificateCategory = PreferenceCategory(context)
|
|
|
|
certificateCategory.key = "certificate_category"
|
|
|
|
certificateCategory.title = getString(R.string.client_certificate)
|
|
|
|
screen.addPreference(certificateCategory)
|
2021-12-02 18:11:34 +00:00
|
|
|
|
2022-05-03 16:25:57 +00:00
|
|
|
val aboutPref = Preference(context)
|
|
|
|
aboutPref.summary = getString(R.string.pkcs_notice)
|
|
|
|
aboutPref.isPersistent = false
|
|
|
|
aboutPref.isSelectable = false
|
|
|
|
certificateCategory.addPreference(aboutPref)
|
2021-12-02 18:11:34 +00:00
|
|
|
|
2022-05-03 16:25:57 +00:00
|
|
|
clientCertPref = Preference(context)
|
|
|
|
clientCertPref.title = getString(R.string.client_certificate)
|
|
|
|
clientCertPref.key = Buran.PREF_KEY_CLIENT_CERT_HUMAN_READABLE
|
2021-12-02 18:11:34 +00:00
|
|
|
|
2022-05-03 16:25:57 +00:00
|
|
|
val clientCertUriHumanReadable = preferenceManager.sharedPreferences.getString(
|
|
|
|
Buran.PREF_KEY_CLIENT_CERT_HUMAN_READABLE,
|
|
|
|
null
|
|
|
|
)
|
|
|
|
|
|
|
|
val hasCert = clientCertUriHumanReadable != null
|
|
|
|
if (!hasCert) {
|
|
|
|
clientCertPref.summary = getString(R.string.tap_to_select_client_certificate)
|
|
|
|
} else {
|
|
|
|
clientCertPref.summary = clientCertUriHumanReadable
|
|
|
|
}
|
2021-12-02 18:11:34 +00:00
|
|
|
|
2022-05-03 16:25:57 +00:00
|
|
|
clientCertPref.setOnPreferenceClickListener {
|
|
|
|
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
|
|
|
|
addCategory(Intent.CATEGORY_OPENABLE)
|
|
|
|
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
|
|
|
type = "*/*"
|
2021-12-02 18:11:34 +00:00
|
|
|
}
|
2022-05-03 16:25:57 +00:00
|
|
|
startActivityForResult(intent, PREFS_SET_CLIENT_CERT_REQ)
|
|
|
|
true
|
|
|
|
}
|
2021-12-02 18:11:34 +00:00
|
|
|
|
2022-05-03 16:25:57 +00:00
|
|
|
certificateCategory.addPreference(clientCertPref)
|
2021-12-02 18:11:34 +00:00
|
|
|
|
|
|
|
|
2022-05-03 16:25:57 +00:00
|
|
|
val clientCertPassword = EditTextPreference(context)
|
|
|
|
clientCertPassword.key = Buran.PREF_KEY_CLIENT_CERT_PASSWORD
|
|
|
|
clientCertPassword.title = getString(R.string.client_certificate_password)
|
2021-12-02 18:11:34 +00:00
|
|
|
|
2022-05-03 16:25:57 +00:00
|
|
|
val certPasword = preferenceManager.sharedPreferences.getString(
|
|
|
|
Buran.PREF_KEY_CLIENT_CERT_PASSWORD,
|
|
|
|
null
|
|
|
|
)
|
|
|
|
if (certPasword != null && certPasword.isNotEmpty()) {
|
|
|
|
clientCertPassword.summary = getDots(certPasword)
|
|
|
|
} else {
|
|
|
|
clientCertPassword.summary = getString(R.string.no_password)
|
|
|
|
}
|
|
|
|
clientCertPassword.dialogTitle = getString(R.string.client_certificate_password)
|
|
|
|
clientCertPassword.setOnPreferenceChangeListener { _, newValue ->
|
|
|
|
val passphrase = "$newValue"
|
|
|
|
if (passphrase.isEmpty()) {
|
2021-12-02 18:11:34 +00:00
|
|
|
clientCertPassword.summary = getString(R.string.no_password)
|
2022-05-03 16:25:57 +00:00
|
|
|
} else {
|
|
|
|
clientCertPassword.summary = getDots(passphrase)
|
2021-12-02 18:11:34 +00:00
|
|
|
}
|
|
|
|
|
2022-05-03 16:25:57 +00:00
|
|
|
true//update the value
|
|
|
|
}
|
2021-12-02 18:11:34 +00:00
|
|
|
|
2022-05-03 16:25:57 +00:00
|
|
|
certificateCategory.addPreference(clientCertPassword)
|
2021-12-02 18:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun getDots(value: String): String {
|
|
|
|
val sb = StringBuilder()
|
|
|
|
repeat(value.length){
|
|
|
|
sb.append("•")
|
|
|
|
}
|
|
|
|
return sb.toString()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPreferenceChange(preference: Preference?, newValue: Any?): Boolean {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
|
|
if(requestCode == PREFS_SET_CLIENT_CERT_REQ && resultCode == RESULT_OK){
|
|
|
|
data?.data?.also { uri ->
|
|
|
|
preferenceManager.sharedPreferences.edit().putString(
|
|
|
|
Buran.PREF_KEY_CLIENT_CERT_URI,
|
|
|
|
uri.toString()
|
|
|
|
).apply()
|
|
|
|
persistPermissions(uri)
|
|
|
|
findFilename(uri)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
super.onActivityResult(requestCode, resultCode, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun persistPermissions(uri: Uri) {
|
|
|
|
val contentResolver = requireContext().contentResolver
|
|
|
|
|
|
|
|
val takeFlags: Int = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
|
|
|
|
contentResolver.takePersistableUriPermission(uri, takeFlags)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun findFilename(uri: Uri) {
|
|
|
|
|
|
|
|
var readableReference = uri.toString()
|
|
|
|
if (uri.scheme == "content") {
|
|
|
|
requireContext().contentResolver.query(uri, null, null, null, null).use { cursor ->
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
readableReference = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
preferenceManager.sharedPreferences.edit().putString(
|
|
|
|
Buran.PREF_KEY_CLIENT_CERT_HUMAN_READABLE,
|
|
|
|
readableReference
|
|
|
|
).apply()
|
|
|
|
clientCertPref.summary = readableReference
|
|
|
|
useClientCertPreference.isChecked = true
|
|
|
|
}
|
|
|
|
}
|