cloudstream/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsFragment.kt

860 lines
34 KiB
Kotlin
Raw Normal View History

2021-06-10 23:00:22 +00:00
package com.lagradost.cloudstream3.ui.settings
2021-11-03 19:38:50 +00:00
2021-10-30 18:14:12 +00:00
import android.app.UiModeManager
2022-04-10 22:00:03 +00:00
import android.content.ClipData
import android.content.ClipboardManager
2021-10-30 18:14:12 +00:00
import android.content.Context
2021-11-07 22:10:19 +00:00
import android.content.Intent
2021-10-30 18:14:12 +00:00
import android.content.res.Configuration
2021-11-07 22:10:19 +00:00
import android.net.Uri
import android.os.Build
2021-06-10 23:00:22 +00:00
import android.os.Bundle
2021-11-01 15:33:46 +00:00
import android.os.Environment
2021-11-07 22:10:19 +00:00
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
2021-11-01 15:33:46 +00:00
import androidx.activity.result.contract.ActivityResultContracts
2021-09-07 19:16:12 +00:00
import androidx.appcompat.app.AlertDialog
2021-08-07 01:53:45 +00:00
import androidx.preference.Preference
2021-06-10 23:00:22 +00:00
import androidx.preference.PreferenceFragmentCompat
2021-08-30 21:42:58 +00:00
import androidx.preference.PreferenceManager
2021-11-08 18:13:39 +00:00
import androidx.recyclerview.widget.RecyclerView
2021-11-01 15:33:46 +00:00
import com.hippo.unifile.UniFile
import com.lagradost.cloudstream3.APIHolder.apis
2021-09-12 14:10:22 +00:00
import com.lagradost.cloudstream3.APIHolder.getApiDubstatusSettings
import com.lagradost.cloudstream3.APIHolder.getApiProviderLangSettings
2021-11-01 15:33:46 +00:00
import com.lagradost.cloudstream3.AcraApplication
import com.lagradost.cloudstream3.AcraApplication.Companion.removeKey
2022-01-07 19:27:25 +00:00
import com.lagradost.cloudstream3.CommonActivity.setLocale
import com.lagradost.cloudstream3.CommonActivity.showToast
2021-09-12 14:10:22 +00:00
import com.lagradost.cloudstream3.DubStatus
2021-06-10 23:00:22 +00:00
import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.app
2021-09-02 14:06:43 +00:00
import com.lagradost.cloudstream3.mvvm.logError
2022-01-13 21:09:05 +00:00
import com.lagradost.cloudstream3.mvvm.normalSafeApiCall
import com.lagradost.cloudstream3.network.initClient
2021-11-12 16:55:54 +00:00
import com.lagradost.cloudstream3.syncproviders.AccountManager
import com.lagradost.cloudstream3.syncproviders.OAuth2API
import com.lagradost.cloudstream3.syncproviders.OAuth2API.Companion.aniListApi
import com.lagradost.cloudstream3.syncproviders.OAuth2API.Companion.malApi
2021-09-12 14:10:22 +00:00
import com.lagradost.cloudstream3.ui.APIRepository
import com.lagradost.cloudstream3.ui.search.SearchResultBuilder
2022-02-12 20:20:56 +00:00
import com.lagradost.cloudstream3.ui.subtitles.ChromecastSubtitlesFragment
2021-08-07 01:53:45 +00:00
import com.lagradost.cloudstream3.ui.subtitles.SubtitlesFragment
2022-03-04 15:39:56 +00:00
import com.lagradost.cloudstream3.utils.BackupUtils.backup
import com.lagradost.cloudstream3.utils.BackupUtils.restorePrompt
import com.lagradost.cloudstream3.utils.HOMEPAGE_API
import com.lagradost.cloudstream3.utils.InAppUpdater.Companion.runAutoUpdate
2021-09-04 11:38:35 +00:00
import com.lagradost.cloudstream3.utils.Qualities
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showBottomDialog
2021-09-02 14:06:43 +00:00
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showDialog
2021-09-12 14:10:22 +00:00
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showMultiDialog
2022-04-18 19:31:14 +00:00
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showNginxTextInputDialog
import com.lagradost.cloudstream3.utils.SubtitleHelper
2022-04-06 15:16:08 +00:00
import com.lagradost.cloudstream3.utils.SubtitleHelper.getFlagFromIso
2021-12-12 02:33:17 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe
2021-08-04 13:30:34 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.hideKeyboard
2021-11-07 22:10:19 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.setImage
2022-04-10 22:00:03 +00:00
import com.lagradost.cloudstream3.utils.VideoDownloadManager
2021-11-01 15:33:46 +00:00
import com.lagradost.cloudstream3.utils.VideoDownloadManager.getBasePath
import com.lagradost.cloudstream3.utils.VideoDownloadManager.getDownloadDir
2022-04-10 22:00:03 +00:00
import kotlinx.android.synthetic.main.logcat.*
import okhttp3.internal.closeQuietly
import java.io.BufferedReader
2021-11-01 15:33:46 +00:00
import java.io.File
2022-04-10 22:00:03 +00:00
import java.io.InputStreamReader
import java.io.OutputStream
import kotlin.concurrent.thread
2021-06-10 23:00:22 +00:00
2021-09-02 14:06:43 +00:00
2021-06-10 23:00:22 +00:00
class SettingsFragment : PreferenceFragmentCompat() {
2021-10-30 18:14:12 +00:00
companion object {
2022-03-04 15:39:56 +00:00
private fun Context.getLayoutInt(): Int {
2021-10-30 18:14:12 +00:00
val settingsManager = PreferenceManager.getDefaultSharedPreferences(this)
2022-02-18 19:29:48 +00:00
return settingsManager.getInt(this.getString(R.string.app_layout_key), -1)
}
fun Context.isTvSettings(): Boolean {
var value = getLayoutInt()
2021-10-30 18:14:12 +00:00
if (value == -1) {
value = if (isAutoTv()) 1 else 0
}
2022-02-18 19:29:48 +00:00
return value == 1 || value == 2
}
2022-03-04 15:39:56 +00:00
fun Context.isTrueTvSettings(): Boolean {
2022-03-29 21:50:07 +00:00
var value = getLayoutInt()
if (value == -1) {
value = if (isAutoTv()) 1 else 0
}
return value == 1
2021-10-30 18:14:12 +00:00
}
2022-03-04 15:39:56 +00:00
fun Context.isEmulatorSettings(): Boolean {
return getLayoutInt() == 2
}
2021-10-30 18:14:12 +00:00
private fun Context.isAutoTv(): Boolean {
val uiModeManager = getSystemService(Context.UI_MODE_SERVICE) as UiModeManager?
// AFT = Fire TV
return uiModeManager?.currentModeType == Configuration.UI_MODE_TYPE_TELEVISION || Build.MODEL.contains("AFT")
2021-10-30 18:14:12 +00:00
}
2021-11-07 22:10:19 +00:00
2022-04-02 01:38:55 +00:00
const val accountEnabled = true
2021-10-30 18:14:12 +00:00
}
2021-09-04 11:38:35 +00:00
private var beneneCount = 0
2021-11-01 15:33:46 +00:00
// Open file picker
2022-03-04 15:39:56 +00:00
private val pathPicker =
registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) { uri ->
// It lies, it can be null if file manager quits.
if (uri == null) return@registerForActivityResult
val context = context ?: AcraApplication.context ?: return@registerForActivityResult
// RW perms for the path
val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
context.contentResolver.takePersistableUriPermission(uri, flags)
val file = UniFile.fromUri(context, uri)
println("Selected URI path: $uri - Full path: ${file.filePath}")
// Stores the real URI using download_path_key
// Important that the URI is stored instead of filepath due to permissions.
2021-11-01 15:33:46 +00:00
PreferenceManager.getDefaultSharedPreferences(context)
2022-03-04 15:39:56 +00:00
.edit().putString(getString(R.string.download_path_key), uri.toString()).apply()
// From URI -> File path
// File path here is purely for cosmetic purposes in settings
(file.filePath ?: uri.toString()).let {
PreferenceManager.getDefaultSharedPreferences(context)
.edit().putString(getString(R.string.download_path_pref), it).apply()
}
2021-11-01 15:33:46 +00:00
}
2021-09-19 22:52:39 +00:00
// idk, if you find a way of automating this it would be great
// https://www.iemoji.com/view/emoji/1794/flags/antarctica
// Emoji Character Encoding Data --> C/C++/Java Src
2022-04-06 15:16:08 +00:00
// https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes leave blank for auto
2021-09-04 11:38:35 +00:00
private val languages = arrayListOf(
2022-04-06 15:16:08 +00:00
Triple("", "Spanish", "es"),
Triple("", "English", "en"),
Triple("", "Viet Nam", "vi"),
Triple("", "Dutch", "nl"),
Triple("", "French", "fr"),
Triple("", "Greek", "el"),
Triple("", "Swedish", "sv"),
Triple("", "Tagalog", "tl"),
Triple("", "Polish", "pl"),
Triple("", "Hindi", "hi"),
Triple("", "Malayalam", "ml"),
Triple("", "Norsk", "no"),
Triple("", "German", "de"),
Triple("", "Arabic", "ar"),
Triple("", "Turkish", "tr"),
Triple("", "Macedonian", "mk"),
Triple("", "Portuguese (Brazil)", "pt"),
Triple("", "Romanian", "ro"),
Triple("", "Italian", "it"),
Triple("", "Chinese", "zh"),
2021-09-19 22:52:39 +00:00
).sortedBy { it.second } //ye, we go alphabetical, so ppl don't put their lang on top
2021-08-30 21:42:58 +00:00
2021-11-12 16:55:54 +00:00
private fun showAccountSwitch(context: Context, api: AccountManager) {
val accounts = api.getAccounts() ?: return
2021-11-08 18:13:39 +00:00
val builder =
AlertDialog.Builder(context, R.style.AlertDialogCustom).setView(R.layout.account_switch)
val dialog = builder.show()
dialog.findViewById<TextView>(R.id.account_add)?.setOnClickListener {
2022-04-01 20:05:34 +00:00
try {
api.authenticate()
} catch (e: Exception) {
logError(e)
}
2021-11-08 18:13:39 +00:00
}
val ogIndex = api.accountIndex
2021-11-12 16:55:54 +00:00
val items = ArrayList<OAuth2API.LoginInfo>()
2021-11-08 18:13:39 +00:00
for (index in accounts) {
api.accountIndex = index
val accountInfo = api.loginInfo()
2021-11-08 18:13:39 +00:00
if (accountInfo != null) {
items.add(accountInfo)
}
}
api.accountIndex = ogIndex
val adapter = AccountAdapter(items, R.layout.account_single) {
2021-12-12 02:33:17 +00:00
dialog?.dismissSafe(activity)
api.changeAccount(it.card.accountIndex)
2021-11-08 18:13:39 +00:00
}
val list = dialog.findViewById<RecyclerView>(R.id.account_list)
list?.adapter = adapter
}
private fun showLoginInfo(api: AccountManager, info: OAuth2API.LoginInfo) {
2021-11-07 22:10:19 +00:00
val builder =
2022-03-04 15:39:56 +00:00
AlertDialog.Builder(context ?: return, R.style.AlertDialogCustom)
.setView(R.layout.account_managment)
2021-11-07 22:10:19 +00:00
val dialog = builder.show()
dialog.findViewById<ImageView>(R.id.account_profile_picture)?.setImage(info.profilePicture)
dialog.findViewById<TextView>(R.id.account_logout)?.setOnClickListener {
api.logOut()
dialog.dismissSafe(activity)
2021-11-07 22:10:19 +00:00
}
(info.name ?: context?.getString(R.string.no_data))?.let {
dialog.findViewById<TextView>(R.id.account_name)?.text = it
}
2021-11-07 22:10:19 +00:00
dialog.findViewById<TextView>(R.id.account_site)?.text = api.name
2021-11-08 18:13:39 +00:00
dialog.findViewById<TextView>(R.id.account_switch_account)?.setOnClickListener {
2021-12-12 02:33:17 +00:00
dialog.dismissSafe(activity)
2021-11-08 18:13:39 +00:00
showAccountSwitch(it.context, api)
}
2021-11-07 22:10:19 +00:00
}
2022-03-04 15:39:56 +00:00
private fun getPref(id: Int): Preference? {
return try {
findPreference(getString(id))
2022-03-16 21:54:58 +00:00
} catch (e: Exception) {
2022-03-04 15:39:56 +00:00
logError(e)
null
}
}
2022-04-06 15:16:08 +00:00
private fun getFolderSize(dir: File): Long {
2022-03-16 21:54:58 +00:00
var size: Long = 0
dir.listFiles()?.let {
for (file in it) {
size += if (file.isFile) {
// System.out.println(file.getName() + " " + file.length());
file.length()
} else getFolderSize(file)
}
}
return size
}
2021-06-10 23:00:22 +00:00
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
2021-08-04 13:30:34 +00:00
hideKeyboard()
2021-06-10 23:00:22 +00:00
setPreferencesFromResource(R.xml.settings, rootKey)
2022-02-04 20:49:35 +00:00
val settingsManager = PreferenceManager.getDefaultSharedPreferences(requireContext())
2022-02-01 20:05:16 +00:00
2022-03-16 21:31:21 +00:00
getPref(R.string.video_buffer_length_key)?.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.video_buffer_length_names)
val prefValues = resources.getIntArray(R.array.video_buffer_length_values)
2022-02-01 20:05:16 +00:00
val currentPrefSize =
2022-03-16 21:31:21 +00:00
settingsManager.getInt(getString(R.string.video_buffer_length_key), 0)
2022-02-01 20:05:16 +00:00
2022-03-16 21:31:21 +00:00
activity?.showDialog(
2022-02-01 20:05:16 +00:00
prefNames.toList(),
prefValues.indexOf(currentPrefSize),
2022-03-16 21:31:21 +00:00
getString(R.string.video_buffer_length_settings),
2022-02-01 20:05:16 +00:00
true,
{}) {
settingsManager.edit()
2022-03-16 21:31:21 +00:00
.putInt(getString(R.string.video_buffer_length_key), prefValues[it])
.apply()
}
return@setOnPreferenceClickListener true
}
getPref(R.string.video_buffer_size_key)?.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.video_buffer_size_names)
val prefValues = resources.getIntArray(R.array.video_buffer_size_values)
val currentPrefSize =
settingsManager.getInt(getString(R.string.video_buffer_size_key), 0)
activity?.showDialog(
prefNames.toList(),
prefValues.indexOf(currentPrefSize),
getString(R.string.video_buffer_size_settings),
true,
{}) {
settingsManager.edit()
.putInt(getString(R.string.video_buffer_size_key), prefValues[it])
.apply()
}
return@setOnPreferenceClickListener true
}
2022-03-16 21:54:58 +00:00
getPref(R.string.video_buffer_clear_key)?.let { pref ->
val cacheDir = context?.cacheDir ?: return@let
fun updateSummery() {
try {
pref.summary =
getString(R.string.mb_format).format(getFolderSize(cacheDir) / (1024L * 1024L))
} catch (e: Exception) {
logError(e)
}
}
updateSummery()
pref.setOnPreferenceClickListener {
try {
cacheDir.deleteRecursively()
updateSummery()
} catch (e: Exception) {
logError(e)
}
return@setOnPreferenceClickListener true
}
}
2022-03-16 21:31:21 +00:00
getPref(R.string.video_buffer_disk_key)?.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.video_buffer_size_names)
val prefValues = resources.getIntArray(R.array.video_buffer_size_values)
val currentPrefSize =
settingsManager.getInt(getString(R.string.video_buffer_disk_key), 0)
activity?.showDialog(
prefNames.toList(),
prefValues.indexOf(currentPrefSize),
getString(R.string.video_buffer_disk_settings),
true,
{}) {
settingsManager.edit()
.putInt(getString(R.string.video_buffer_disk_key), prefValues[it])
2022-02-01 20:05:16 +00:00
.apply()
}
return@setOnPreferenceClickListener true
}
2021-12-12 02:33:17 +00:00
2022-03-04 15:39:56 +00:00
getPref(R.string.subtitle_settings_key)?.setOnPreferenceClickListener {
2021-12-12 02:33:17 +00:00
SubtitlesFragment.push(activity, false)
return@setOnPreferenceClickListener true
}
2021-09-07 19:16:12 +00:00
2022-03-04 15:39:56 +00:00
getPref(R.string.subtitle_settings_chromecast_key)?.setOnPreferenceClickListener {
2022-02-12 20:20:56 +00:00
ChromecastSubtitlesFragment.push(activity, false)
return@setOnPreferenceClickListener true
}
getPref(R.string.poster_ui_key)?.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.poster_ui_options)
val keys = resources.getStringArray(R.array.poster_ui_options_values)
val prefValues = keys.map {
settingsManager.getBoolean(it, true)
}.mapIndexedNotNull { index, b ->
if (b) {
index
} else null
}
activity?.showMultiDialog(
prefNames.toList(),
prefValues,
getString(R.string.poster_ui_settings),
{}) { list ->
val edit = settingsManager.edit()
for ((i, key) in keys.withIndex()) {
edit.putBoolean(key, list.contains(i))
}
edit.apply()
SearchResultBuilder.updateCache(it.context)
}
return@setOnPreferenceClickListener true
}
2022-03-04 15:39:56 +00:00
val syncApis =
listOf(Pair(R.string.mal_key, malApi), Pair(R.string.anilist_key, aniListApi))
2022-04-01 20:05:34 +00:00
for ((key, api) in syncApis) {
getPref(key)?.apply {
2021-11-07 22:10:19 +00:00
isVisible = accountEnabled
2022-03-04 15:39:56 +00:00
title =
getString(R.string.login_format).format(api.name, getString(R.string.account))
setOnPreferenceClickListener { _ ->
val info = api.loginInfo()
if (info != null) {
showLoginInfo(api, info)
} else {
2022-04-01 20:05:34 +00:00
try {
api.authenticate()
} catch (e: Exception) {
logError(e)
}
2021-11-07 22:10:19 +00:00
}
return@setOnPreferenceClickListener true
}
}
}
2022-03-04 15:39:56 +00:00
getPref(R.string.legal_notice_key)?.setOnPreferenceClickListener {
2021-09-07 19:16:12 +00:00
val builder: AlertDialog.Builder = AlertDialog.Builder(it.context)
builder.setTitle(R.string.legal_notice)
builder.setMessage(R.string.legal_notice_text)
builder.show()
return@setOnPreferenceClickListener true
}
2021-09-04 11:38:35 +00:00
2022-03-04 15:39:56 +00:00
getPref(R.string.display_sub_key)?.setOnPreferenceClickListener {
2021-09-12 14:10:22 +00:00
activity?.getApiDubstatusSettings()?.let { current ->
val dublist = DubStatus.values()
val names = dublist.map { it.name }
val currentList = ArrayList<Int>()
for (i in current) {
currentList.add(dublist.indexOf(i))
}
2021-12-12 02:33:17 +00:00
activity?.showMultiDialog(
2021-09-12 14:10:22 +00:00
names,
currentList,
getString(R.string.display_subbed_dubbed_settings),
{}) { selectedList ->
APIRepository.dubStatusActive = selectedList.map { dublist[it] }.toHashSet()
settingsManager.edit().putStringSet(
this.getString(R.string.display_sub_key),
selectedList.map { names[it] }.toMutableSet()
).apply()
}
}
return@setOnPreferenceClickListener true
}
2022-03-04 15:39:56 +00:00
getPref(R.string.provider_lang_key)?.setOnPreferenceClickListener {
activity?.getApiProviderLangSettings()?.let { current ->
val allLangs = HashSet<String>()
for (api in apis) {
allLangs.add(api.lang)
}
val currentList = ArrayList<Int>()
for (i in current) {
currentList.add(allLangs.indexOf(i))
}
2021-09-12 14:10:22 +00:00
2022-04-06 15:16:08 +00:00
val names = allLangs.map {
val emoji = getFlagFromIso(it)
val name = SubtitleHelper.fromTwoLettersToLanguage(it)
val fullName = "$emoji $name"
Pair(it, fullName)
}
2021-12-12 02:33:17 +00:00
activity?.showMultiDialog(
names.map { it.second },
currentList,
getString(R.string.provider_lang_settings),
{}) { selectedList ->
settingsManager.edit().putStringSet(
this.getString(R.string.provider_lang_key),
selectedList.map { names[it].first }.toMutableSet()
).apply()
//APIRepository.providersActive = it.context.getApiSettings()
}
}
2021-09-12 14:10:22 +00:00
return@setOnPreferenceClickListener true
}
2021-11-07 22:10:19 +00:00
2021-11-01 15:33:46 +00:00
fun getDownloadDirs(): List<String> {
2022-01-13 21:09:05 +00:00
return normalSafeApiCall {
val defaultDir = getDownloadDir()?.filePath
// app_name_download_path = Cloudstream and does not change depending on release.
// DOES NOT WORK ON SCOPED STORAGE.
2022-03-04 15:39:56 +00:00
val secondaryDir =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) null else Environment.getExternalStorageDirectory().absolutePath +
File.separator + resources.getString(R.string.app_name_download_path)
2022-01-13 21:09:05 +00:00
val first = listOf(defaultDir, secondaryDir)
(try {
val currentDir = context?.getBasePath()?.let { it.first?.filePath ?: it.second }
(first +
requireContext().getExternalFilesDirs("").mapNotNull { it.path } +
currentDir)
} catch (e: Exception) {
first
}).filterNotNull().distinct()
} ?: emptyList()
2021-11-01 15:33:46 +00:00
}
2022-03-04 15:39:56 +00:00
getPref(R.string.download_path_key)?.setOnPreferenceClickListener {
2021-11-01 15:33:46 +00:00
val dirs = getDownloadDirs()
val currentDir =
2022-03-04 15:39:56 +00:00
settingsManager.getString(getString(R.string.download_path_pref), null)
?: getDownloadDir().toString()
2021-11-01 15:33:46 +00:00
2021-12-12 02:33:17 +00:00
activity?.showBottomDialog(
2021-11-01 15:33:46 +00:00
dirs + listOf("Custom"),
dirs.indexOf(currentDir),
getString(R.string.download_path_pref),
true,
{}) {
// Last = custom
if (it == dirs.size) {
2022-02-18 21:20:35 +00:00
try {
pathPicker.launch(Uri.EMPTY)
2022-03-04 15:39:56 +00:00
} catch (e: Exception) {
2022-02-18 21:20:35 +00:00
logError(e)
}
2021-11-01 15:33:46 +00:00
} else {
// Sets both visual and actual paths.
// key = used path
// pref = visual path
2022-03-04 15:39:56 +00:00
settingsManager.edit()
.putString(getString(R.string.download_path_key), dirs[it]).apply()
settingsManager.edit()
.putString(getString(R.string.download_path_pref), dirs[it]).apply()
2021-11-04 10:22:06 +00:00
}
}
2021-11-04 10:42:51 +00:00
return@setOnPreferenceClickListener true
}
2021-11-07 22:10:19 +00:00
getPref(R.string.nginx_url_key)?.setOnPreferenceClickListener {
activity?.showNginxTextInputDialog(
settingsManager.getString(getString(R.string.nginx_url_pref), "Nginx server url")
.toString(),
settingsManager.getString(getString(R.string.nginx_url_key), "")
.toString(), // key: the actual you use rn
android.text.InputType.TYPE_TEXT_VARIATION_URI, // uri
{}) {
settingsManager.edit()
.putString(getString(R.string.nginx_url_key), it)
.apply() // change the stored url in nginx_url_key to it
}
return@setOnPreferenceClickListener true
}
getPref(R.string.nginx_credentials)?.setOnPreferenceClickListener {
activity?.showNginxTextInputDialog(
settingsManager.getString(
getString(R.string.nginx_credentials_title),
"Nginx Credentials"
).toString(),
settingsManager.getString(getString(R.string.nginx_credentials), "")
.toString(), // key: the actual you use rn
android.text.InputType.TYPE_TEXT_VARIATION_URI,
{}) {
settingsManager.edit()
.putString(getString(R.string.nginx_credentials), it)
.apply() // change the stored url in nginx_url_key to it
}
return@setOnPreferenceClickListener true
}
2022-03-04 15:39:56 +00:00
getPref(R.string.prefer_media_type_key)?.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.media_type_pref)
val prefValues = resources.getIntArray(R.array.media_type_pref_values)
2021-11-03 19:38:50 +00:00
val currentPrefMedia =
settingsManager.getInt(getString(R.string.prefer_media_type_key), 0)
2021-12-12 02:33:17 +00:00
activity?.showBottomDialog(
prefNames.toList(),
prefValues.indexOf(currentPrefMedia),
getString(R.string.preferred_media_settings),
true,
{}) {
settingsManager.edit()
.putInt(getString(R.string.prefer_media_type_key), prefValues[it])
.apply()
removeKey(HOMEPAGE_API)
(context ?: AcraApplication.context)?.let { ctx -> app.initClient(ctx) }
}
return@setOnPreferenceClickListener true
2021-11-04 10:42:51 +00:00
}
2022-04-10 22:00:03 +00:00
getPref(R.string.show_logcat_key)?.setOnPreferenceClickListener { pref ->
val builder =
AlertDialog.Builder(pref.context, R.style.AlertDialogCustom)
.setView(R.layout.logcat)
val dialog = builder.create()
dialog.show()
val log = StringBuilder()
try {
//https://developer.android.com/studio/command-line/logcat
val process = Runtime.getRuntime().exec("logcat -d")
val bufferedReader = BufferedReader(
InputStreamReader(process.inputStream)
)
var line: String?
while (bufferedReader.readLine().also { line = it } != null) {
log.append(line)
}
} catch (e: Exception) {
logError(e) // kinda ironic
}
val text = log.toString()
dialog.text1?.text = text
dialog.copy_btt?.setOnClickListener {
val serviceClipboard =
(activity?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager?)
?: return@setOnClickListener
val clip = ClipData.newPlainText("logcat", text)
serviceClipboard.setPrimaryClip(clip)
dialog.dismissSafe(activity)
}
dialog.clear_btt?.setOnClickListener {
Runtime.getRuntime().exec("logcat -c")
dialog.dismissSafe(activity)
}
dialog.save_btt?.setOnClickListener {
var fileStream: OutputStream? = null
try {
fileStream =
VideoDownloadManager.setupStream(
it.context,
"logcat",
null,
"txt",
false
).fileStream
fileStream?.writer()?.write(text)
} catch (e: Exception) {
logError(e)
} finally {
fileStream?.closeQuietly()
dialog.dismissSafe(activity)
}
}
dialog.close_btt?.setOnClickListener {
dialog.dismissSafe(activity)
}
return@setOnPreferenceClickListener true
}
2022-03-04 15:39:56 +00:00
getPref(R.string.app_layout_key)?.setOnPreferenceClickListener {
2021-11-04 10:42:51 +00:00
val prefNames = resources.getStringArray(R.array.app_layout)
val prefValues = resources.getIntArray(R.array.app_layout_values)
2021-10-30 18:14:12 +00:00
2021-11-04 10:42:51 +00:00
val currentLayout =
settingsManager.getInt(getString(R.string.app_layout_key), -1)
2021-12-12 02:33:17 +00:00
activity?.showBottomDialog(
2021-11-04 10:42:51 +00:00
prefNames.toList(),
prefValues.indexOf(currentLayout),
getString(R.string.app_layout),
true,
{}) {
try {
2022-03-04 15:39:56 +00:00
settingsManager.edit()
.putInt(getString(R.string.app_layout_key), prefValues[it])
2021-11-04 10:42:51 +00:00
.apply()
activity?.recreate()
} catch (e: Exception) {
logError(e)
2021-10-30 18:14:12 +00:00
}
}
2021-11-04 10:42:51 +00:00
return@setOnPreferenceClickListener true
}
2021-10-30 18:14:12 +00:00
2022-03-04 15:39:56 +00:00
getPref(R.string.backup_key)?.setOnPreferenceClickListener {
activity?.backup()
return@setOnPreferenceClickListener true
}
getPref(R.string.restore_key)?.setOnPreferenceClickListener {
activity?.restorePrompt()
return@setOnPreferenceClickListener true
}
getPref(R.string.primary_color_key)?.setOnPreferenceClickListener {
2021-11-04 10:42:51 +00:00
val prefNames = resources.getStringArray(R.array.themes_overlay_names)
val prefValues = resources.getStringArray(R.array.themes_overlay_names_values)
2021-10-31 01:17:56 +00:00
2021-11-04 10:42:51 +00:00
val currentLayout =
settingsManager.getString(getString(R.string.primary_color_key), prefValues.first())
2021-12-12 02:33:17 +00:00
2022-04-09 21:07:52 +00:00
activity?.showDialog(
2021-11-04 10:42:51 +00:00
prefNames.toList(),
prefValues.indexOf(currentLayout),
getString(R.string.primary_color_settings),
true,
{}) {
try {
2022-03-04 15:39:56 +00:00
settingsManager.edit()
.putString(getString(R.string.primary_color_key), prefValues[it])
2021-11-04 10:42:51 +00:00
.apply()
activity?.recreate()
} catch (e: Exception) {
logError(e)
2021-10-31 01:17:56 +00:00
}
}
2021-11-04 10:42:51 +00:00
return@setOnPreferenceClickListener true
}
2021-10-31 01:17:56 +00:00
2022-03-04 15:39:56 +00:00
getPref(R.string.app_theme_key)?.setOnPreferenceClickListener {
2021-11-04 10:42:51 +00:00
val prefNames = resources.getStringArray(R.array.themes_names)
val prefValues = resources.getStringArray(R.array.themes_names_values)
2021-10-31 14:14:01 +00:00
2021-11-04 10:42:51 +00:00
val currentLayout =
settingsManager.getString(getString(R.string.app_theme_key), prefValues.first())
2021-12-12 02:33:17 +00:00
activity?.showBottomDialog(
2021-11-04 10:42:51 +00:00
prefNames.toList(),
prefValues.indexOf(currentLayout),
getString(R.string.app_theme_settings),
true,
{}) {
try {
2022-03-04 15:39:56 +00:00
settingsManager.edit()
.putString(getString(R.string.app_theme_key), prefValues[it])
2021-11-04 10:22:06 +00:00
.apply()
2021-11-04 10:42:51 +00:00
activity?.recreate()
} catch (e: Exception) {
logError(e)
2021-11-01 15:33:46 +00:00
}
}
2021-11-04 10:42:51 +00:00
return@setOnPreferenceClickListener true
}
2021-11-01 15:33:46 +00:00
2022-03-04 15:39:56 +00:00
getPref(R.string.quality_pref_key)?.setOnPreferenceClickListener {
2022-04-18 19:31:14 +00:00
val prefValues = Qualities.values().map { it.value }.reversed().toMutableList()
prefValues.remove(Qualities.Unknown.value)
val prefNames = prefValues.map { Qualities.getStringByInt(it) }
2021-09-04 11:38:35 +00:00
2021-11-04 10:42:51 +00:00
val currentQuality =
settingsManager.getInt(
2022-01-07 19:27:25 +00:00
getString(R.string.quality_pref_key),
2021-11-04 10:42:51 +00:00
Qualities.values().last().value
)
2021-12-12 02:33:17 +00:00
activity?.showBottomDialog(
2021-11-04 10:42:51 +00:00
prefNames.toList(),
prefValues.indexOf(currentQuality),
getString(R.string.watch_quality_pref),
true,
{}) {
2022-01-07 19:27:25 +00:00
settingsManager.edit().putInt(getString(R.string.quality_pref_key), prefValues[it])
2021-11-04 10:42:51 +00:00
.apply()
2021-09-04 11:38:35 +00:00
}
2021-11-04 10:42:51 +00:00
return@setOnPreferenceClickListener true
}
getPref(R.string.prefer_limit_title_key)?.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.limit_title_pref_names)
val prefValues = resources.getIntArray(R.array.limit_title_pref_values)
val current = settingsManager.getInt(getString(R.string.prefer_limit_title_key), 0)
activity?.showBottomDialog(
prefNames.toList(),
prefValues.indexOf(current),
getString(R.string.limit_title),
true,
{}) {
settingsManager.edit()
.putInt(getString(R.string.prefer_limit_title_key), prefValues[it])
.apply()
}
return@setOnPreferenceClickListener true
}
2022-03-04 15:39:56 +00:00
getPref(R.string.dns_key)?.setOnPreferenceClickListener {
2021-11-04 10:42:51 +00:00
val prefNames = resources.getStringArray(R.array.dns_pref)
val prefValues = resources.getIntArray(R.array.dns_pref_values)
2021-11-04 10:42:51 +00:00
val currentDns =
settingsManager.getInt(getString(R.string.dns_pref), 0)
2021-12-12 02:33:17 +00:00
activity?.showBottomDialog(
2021-11-04 10:42:51 +00:00
prefNames.toList(),
prefValues.indexOf(currentDns),
getString(R.string.dns_pref),
true,
{}) {
settingsManager.edit().putInt(getString(R.string.dns_pref), prefValues[it]).apply()
(context ?: AcraApplication.context)?.let { ctx -> app.initClient(ctx) }
2021-11-04 10:42:51 +00:00
}
return@setOnPreferenceClickListener true
}
2021-08-30 22:07:39 +00:00
2021-11-04 10:42:51 +00:00
try {
beneneCount = settingsManager.getInt(getString(R.string.benene_count), 0)
2022-03-04 15:39:56 +00:00
getPref(R.string.benene_count)?.let { pref ->
pref.summary =
if (beneneCount <= 0) getString(R.string.benene_count_text_none) else getString(
R.string.benene_count_text
).format(
beneneCount
)
pref.setOnPreferenceClickListener {
try {
beneneCount++
settingsManager.edit().putInt(getString(R.string.benene_count), beneneCount)
.apply()
it.summary = getString(R.string.benene_count_text).format(beneneCount)
} catch (e: Exception) {
logError(e)
}
2021-11-04 10:42:51 +00:00
2022-03-04 15:39:56 +00:00
return@setOnPreferenceClickListener true
2021-08-30 21:42:58 +00:00
}
}
2021-11-04 10:42:51 +00:00
} catch (e: Exception) {
e.printStackTrace()
}
2021-08-30 21:42:58 +00:00
2022-03-04 15:39:56 +00:00
getPref(R.string.manual_check_update_key)?.setOnPreferenceClickListener {
2021-11-04 10:42:51 +00:00
thread {
if (!requireActivity().runAutoUpdate(false)) {
activity?.runOnUiThread {
showToast(activity, R.string.no_update_found, Toast.LENGTH_SHORT)
}
}
}
2021-11-04 10:42:51 +00:00
return@setOnPreferenceClickListener true
}
2021-09-02 14:06:43 +00:00
2022-03-04 15:39:56 +00:00
getPref(R.string.locale_key)?.setOnPreferenceClickListener { pref ->
2021-11-04 10:42:51 +00:00
val tempLangs = languages.toMutableList()
if (beneneCount > 100) {
tempLangs.add(Triple("\uD83E\uDD8D", "mmmm... monke", "mo"))
}
val current = getCurrentLocale()
val languageCodes = tempLangs.map { it.third }
2022-04-06 15:16:08 +00:00
val languageNames = tempLangs.map { (emoji, name, iso) ->
val flag = emoji.ifBlank { getFlagFromIso(iso) ?: "ERROR" }
"$flag $name"
}
2021-11-04 10:42:51 +00:00
val index = languageCodes.indexOf(current)
2021-12-12 02:33:17 +00:00
activity?.showDialog(
2021-11-04 10:42:51 +00:00
languageNames, index, getString(R.string.app_language), true, { }
) { languageIndex ->
try {
val code = languageCodes[languageIndex]
setLocale(activity, code)
settingsManager.edit().putString(getString(R.string.locale_key), code).apply()
activity?.recreate()
} catch (e: Exception) {
logError(e)
2021-11-04 10:22:06 +00:00
}
2021-09-02 14:06:43 +00:00
}
2021-11-04 10:42:51 +00:00
return@setOnPreferenceClickListener true
2021-09-02 14:06:43 +00:00
}
2021-11-04 10:42:51 +00:00
}
2021-09-02 14:06:43 +00:00
2021-11-04 10:42:51 +00:00
private fun getCurrentLocale(): String {
2022-02-04 20:49:35 +00:00
val res = requireContext().resources
2021-09-02 14:06:43 +00:00
// Change locale settings in the app.
2021-11-04 10:42:51 +00:00
// val dm = res.displayMetrics
val conf = res.configuration
return conf?.locale?.language ?: "en"
}
}