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

216 lines
7.6 KiB
Kotlin
Raw Normal View History

2021-06-10 23:00:22 +00:00
package com.lagradost.cloudstream3.ui.settings
2021-10-30 18:14:12 +00:00
import android.app.UiModeManager
import android.content.Context
import android.content.res.Configuration
import android.os.Build
2021-06-10 23:00:22 +00:00
import android.os.Bundle
2022-05-15 18:38:32 +00:00
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
2022-06-04 00:11:32 +00:00
import androidx.annotation.StringRes
import androidx.core.view.children
2022-06-03 17:18:13 +00:00
import androidx.core.view.isVisible
2022-05-15 18:38:32 +00:00
import androidx.fragment.app.Fragment
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
2023-07-14 00:28:49 +00:00
import com.google.android.material.appbar.MaterialToolbar
2021-06-10 23:00:22 +00:00
import com.lagradost.cloudstream3.R
2023-07-14 00:28:49 +00:00
import com.lagradost.cloudstream3.databinding.MainSettingsBinding
2021-09-02 14:06:43 +00:00
import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.accountManagers
2022-06-03 17:18:13 +00:00
import com.lagradost.cloudstream3.ui.home.HomeFragment
2022-06-04 00:11:32 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar
2022-05-15 18:38:32 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.navigate
2022-06-03 17:18:13 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.setImage
2022-08-21 20:13:53 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.toPx
2021-11-01 15:33:46 +00:00
import java.io.File
2021-09-02 14:06:43 +00:00
2022-05-15 18:38:32 +00:00
class SettingsFragment : Fragment() {
2021-10-30 18:14:12 +00:00
companion object {
2022-05-15 18:38:32 +00:00
var beneneCount = 0
2023-07-14 00:28:49 +00:00
private var isTv: Boolean = false
private var isTrueTv: Boolean = false
2022-08-28 23:52:15 +00:00
2022-05-15 18:38:32 +00:00
fun PreferenceFragmentCompat?.getPref(id: Int): Preference? {
if (this == null) return null
return try {
findPreference(getString(id))
} catch (e: Exception) {
logError(e)
null
}
}
2022-08-21 20:13:53 +00:00
/**
* On TV you cannot properly scroll to the bottom of settings, this fixes that.
* */
fun PreferenceFragmentCompat.setPaddingBottom() {
2022-08-28 23:52:15 +00:00
if (isTvSettings()) {
2022-08-21 20:13:53 +00:00
listView?.setPadding(0, 0, 0, 100.toPx)
}
}
2022-08-07 21:11:13 +00:00
fun Fragment?.setUpToolbar(title: String) {
if (this == null) return
2023-07-14 00:28:49 +00:00
val settingsToolbar = view?.findViewById<MaterialToolbar>(R.id.settings_toolbar) ?: return
settingsToolbar.apply {
2022-08-07 21:11:13 +00:00
setTitle(title)
setNavigationIcon(R.drawable.ic_baseline_arrow_back_24)
setNavigationOnClickListener {
activity?.onBackPressed()
}
}
2023-07-14 00:28:49 +00:00
fixPaddingStatusbar(settingsToolbar)
2022-08-07 21:11:13 +00:00
}
fun Fragment?.setUpToolbar(@StringRes title: Int) {
2022-06-04 00:11:32 +00:00
if (this == null) return
2023-07-14 00:28:49 +00:00
val settingsToolbar = view?.findViewById<MaterialToolbar>(R.id.settings_toolbar) ?: return
settingsToolbar.apply {
2022-06-04 00:11:32 +00:00
setTitle(title)
setNavigationIcon(R.drawable.ic_baseline_arrow_back_24)
children.firstOrNull { it is ImageView }?.tag = getString(R.string.tv_no_focus_tag)
2022-06-04 00:11:32 +00:00
setNavigationOnClickListener {
activity?.onBackPressed()
}
}
2023-07-14 00:28:49 +00:00
fixPaddingStatusbar(settingsToolbar)
2022-06-04 00:11:32 +00:00
}
2022-05-15 18:38:32 +00:00
fun getFolderSize(dir: File): Long {
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
}
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)
}
2022-08-28 23:52:15 +00:00
private fun Context.isTvSettings(): Boolean {
2022-02-18 19:29:48 +00:00
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-08-28 23:52:15 +00:00
private 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-08-28 23:52:15 +00:00
fun Context.updateTv() {
isTrueTv = isTrueTvSettings()
isTv = isTvSettings()
}
fun isTrueTvSettings(): Boolean {
return isTrueTv
}
fun isTvSettings(): Boolean {
return isTv
}
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
val model = Build.MODEL.lowercase()
2022-05-15 18:38:32 +00:00
return uiModeManager?.currentModeType == Configuration.UI_MODE_TYPE_TELEVISION || Build.MODEL.contains(
"AFT"
) || model.contains("firestick") || model.contains("fire tv") || model.contains("chromecast")
2021-10-30 18:14:12 +00:00
}
}
2023-07-14 00:28:49 +00:00
override fun onDestroyView() {
binding = null
super.onDestroyView()
}
var binding: MainSettingsBinding? = null
2022-05-15 18:38:32 +00:00
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
2023-07-14 00:28:49 +00:00
): View {
val localBinding = MainSettingsBinding.inflate(inflater, container, false)
binding = localBinding
return localBinding.root
//return inflater.inflate(R.layout.main_settings, container, false)
2021-11-08 18:13:39 +00:00
}
2022-05-15 18:38:32 +00:00
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
fun navigate(id: Int) {
activity?.navigate(id, Bundle())
2021-11-07 22:10:19 +00:00
}
2023-07-16 00:25:36 +00:00
// used to debug leaks showToast(activity,"${VideoDownloadManager.downloadStatusEvent.size} : ${VideoDownloadManager.downloadProgressEvent.size}")
2022-08-28 23:52:15 +00:00
val isTrueTv = isTrueTvSettings()
2022-05-27 16:39:00 +00:00
for (syncApi in accountManagers) {
2022-06-03 17:18:13 +00:00
val login = syncApi.loginInfo()
val pic = login?.profilePicture ?: continue
2023-07-14 00:28:49 +00:00
if (binding?.settingsProfilePic?.setImage(
2022-06-03 17:18:13 +00:00
pic,
errorImageDrawable = HomeFragment.errorProfilePic
) == true
) {
2023-07-14 00:28:49 +00:00
binding?.settingsProfileText?.text = login.name
binding?.settingsProfile?.isVisible = true
2022-06-03 17:18:13 +00:00
break
}
}
2023-07-14 00:28:49 +00:00
binding?.apply {
listOf(
settingsGeneral to R.id.action_navigation_global_to_navigation_settings_general,
settingsPlayer to R.id.action_navigation_global_to_navigation_settings_player,
settingsCredits to R.id.action_navigation_global_to_navigation_settings_account,
settingsUi to R.id.action_navigation_global_to_navigation_settings_ui,
settingsProviders to R.id.action_navigation_global_to_navigation_settings_providers,
settingsUpdates to R.id.action_navigation_global_to_navigation_settings_updates,
settingsExtensions to R.id.action_navigation_global_to_navigation_settings_extensions,
2023-07-14 00:28:49 +00:00
).forEach { (view, navigationId) ->
view.apply {
setOnClickListener {
navigate(navigationId)
}
if (isTrueTv) {
isFocusable = true
isFocusableInTouchMode = true
}
2022-05-27 16:39:00 +00:00
}
}
2023-09-18 21:22:39 +00:00
// Default focus on TV
if (isTrueTv) {
settingsGeneral.requestFocus()
}
}
2021-11-04 10:42:51 +00:00
}
}