Skip to content

Commit

Permalink
Add subtitle settings in player (#1417)
Browse files Browse the repository at this point in the history
  • Loading branch information
dependencyrejection authored Dec 6, 2024
1 parent 5a7ff3d commit 7ad563b
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ abstract class AbstractPlayerFragment(
private fun onSubStyleChanged(style: SaveCaptionStyle) {
if (player is CS3IPlayer) {
player.updateSubtitleStyle(style)
// Forcefully update the subtitle encoding in case the edge size is changed
player.seekTime(-1)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.*
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.OptIn
Expand All @@ -24,6 +25,7 @@ import androidx.preference.PreferenceManager
import androidx.media3.common.Format.NO_VALUE
import androidx.media3.common.MimeTypes
import androidx.media3.common.util.UnstableApi
import androidx.navigation.findNavController
import com.lagradost.cloudstream3.*
import com.lagradost.cloudstream3.APIHolder.getApiFromNameNull
import com.lagradost.cloudstream3.AcraApplication.Companion.setKey
Expand All @@ -36,6 +38,7 @@ import com.lagradost.cloudstream3.databinding.DialogOnlineSubtitlesBinding
import com.lagradost.cloudstream3.databinding.FragmentPlayerBinding
import com.lagradost.cloudstream3.databinding.PlayerSelectSourceAndSubsBinding
import com.lagradost.cloudstream3.databinding.PlayerSelectTracksBinding
import com.lagradost.cloudstream3.databinding.SubtitleSettingsDialogBinding
import com.lagradost.cloudstream3.mvvm.*
import com.lagradost.cloudstream3.subtitles.AbstractSubApi
import com.lagradost.cloudstream3.subtitles.AbstractSubtitleEntities
Expand All @@ -51,6 +54,7 @@ import com.lagradost.cloudstream3.ui.settings.Globals.PHONE
import com.lagradost.cloudstream3.ui.settings.Globals.TV
import com.lagradost.cloudstream3.ui.settings.Globals.isLayout
import com.lagradost.cloudstream3.ui.subtitles.SUBTITLE_AUTO_SELECT_KEY
import com.lagradost.cloudstream3.ui.subtitles.SubtitlesFragment
import com.lagradost.cloudstream3.ui.subtitles.SubtitlesFragment.Companion.getAutoSelectLanguageISO639_1
import com.lagradost.cloudstream3.utils.*
import com.lagradost.cloudstream3.utils.AppContextUtils.sortSubs
Expand Down Expand Up @@ -686,6 +690,12 @@ class GeneratorPlayer : FullScreenPlayer() {

var shouldDismiss = true

binding.subtitleSettingsBtt.setOnClickListener {
normalSafeApiCall {
SubtitlesFragment().show(this.parentFragmentManager, "SubtitleSettings")
}
}

fun dismiss() {
if (isPlaying) {
player.handleEvent(CSPlayerEvent.Play)
Expand Down Expand Up @@ -823,7 +833,7 @@ class GeneratorPlayer : FullScreenPlayer() {
text = prefNames[if (index == -1) 0 else index]
}

binding.subtitlesClickSettings.setOnClickListener {
binding.subtitlesEncodingFormat.setOnClickListener {
val settingsManager = PreferenceManager.getDefaultSharedPreferences(ctx)

val prefNames = ctx.resources.getStringArray(R.array.subtitles_encoding_list)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import androidx.annotation.FontRes
import androidx.annotation.OptIn
import androidx.annotation.Px
import androidx.core.content.res.ResourcesCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.DialogFragment
import androidx.media3.common.text.Cue
import androidx.media3.common.util.UnstableApi
import androidx.media3.ui.CaptionStyleCompat
Expand Down Expand Up @@ -76,7 +76,7 @@ data class SaveCaptionStyle @OptIn(UnstableApi::class) constructor(
const val DEF_SUBS_ELEVATION = 20

@OptIn(androidx.media3.common.util.UnstableApi::class)
class SubtitlesFragment : Fragment() {
class SubtitlesFragment : DialogFragment() {
companion object {
val applyStyleEvent = Event<SaveCaptionStyle>()

Expand Down Expand Up @@ -114,6 +114,7 @@ class SubtitlesFragment : Fragment() {
fun push(activity: Activity?, hide: Boolean = true) {
activity.navigate(R.id.global_to_navigation_subtitles, Bundle().apply {
putBoolean("hide", hide)
putBoolean("popFragment", true)
})
}

Expand Down Expand Up @@ -265,9 +266,19 @@ class SubtitlesFragment : Fragment() {
onColorSelectedEvent -= ::onColorSelected
}

override fun onStart() {
super.onStart()
dialog?.window?.setWindowAnimations(R.style.DialogFullscreen)
}

override fun getTheme(): Int {
return R.style.DialogFullscreen
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
hide = arguments?.getBoolean("hide") ?: true
val popFragment = arguments?.getBoolean("popFragment") ?: false
onColorSelectedEvent += ::onColorSelected
onDialogDismissedEvent += ::onDialogDismissed
binding?.subsImportText?.text = getString(R.string.subs_import_text).format(
Expand Down Expand Up @@ -578,14 +589,22 @@ class SubtitlesFragment : Fragment() {
}

cancelBtt.setOnClickListener {
activity?.popCurrentPage()
if (popFragment) {
activity?.popCurrentPage()
} else {
dismiss()
}
}

applyBtt.setOnClickListener {
it.context.saveStyle(state)
applyStyleEvent.invoke(state)
it.context.fromSaveToStyle(state)
activity?.popCurrentPage()
if (popFragment) {
activity?.popCurrentPage()
} else {
dismiss()
}
}
}
}
Expand Down
45 changes: 31 additions & 14 deletions app/src/main/res/layout/player_select_source_and_subs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
android:layout_rowWeight="1"
android:background="?attr/primaryBlackBackground"
android:listSelector="@drawable/outline_drawable_forced"
android:nextFocusUp="@id/profiles_click_settings"
android:nextFocusRight="@id/sort_subtitles"
android:nextFocusUp="@id/profiles_click_settings"
android:nextFocusDown="@id/apply_btt"
android:requiresFadingEdge="vertical"
tools:layout_height="100dp"
Expand All @@ -90,21 +90,18 @@
android:orientation="horizontal"
tools:ignore="UseCompoundDrawables">

<LinearLayout
android:id="@+id/subtitles_click_settings"
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:focusable="true"
android:foreground="@drawable/outline_drawable_forced"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp">
android:orientation="horizontal">

<TextView
android:id="@+id/subtitles_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:paddingVertical="10dp"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:text="@string/pick_subtitle"
Expand All @@ -116,20 +113,40 @@
android:id="@+id/subtitles_encoding_format"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:gravity="center"
android:layout_toStartOf="@+id/subtitle_settings_btt"
android:layout_toEndOf="@id/subtitles_text"
android:focusable="true"
android:foreground="@drawable/outline_drawable_forced"
android:gravity="start"
android:maxLines="1"
android:paddingHorizontal="10dp"
android:paddingVertical="10dp"
android:textColor="?attr/textColor"
android:textSize="15sp"
tools:text="Thai (TIS 620-2533/ISO 8859-11)" />
</LinearLayout>

<ImageView
android:id="@+id/subtitle_settings_btt"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:contentDescription="@string/subtitles_settings"
android:foreground="@drawable/outline_drawable_forced"
android:paddingHorizontal="10dp"
android:paddingVertical="10dp"
android:src="@drawable/ic_outline_settings_24" />
</RelativeLayout>

<ImageView
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_gravity="end|center_vertical"
android:layout_marginTop="0dp"
android:layout_marginEnd="10dp"

android:contentDescription="@string/home_change_provider_img_des"
android:src="@drawable/ic_outline_settings_24"
android:visibility="gone" />
Expand All @@ -142,9 +159,9 @@
android:layout_rowWeight="1"
android:background="?attr/primaryBlackBackground"
android:listSelector="@drawable/outline_drawable_forced"
android:nextFocusUp="@id/subtitles_click_settings"
android:nextFocusLeft="@id/sort_providers"
android:nextFocusRight="@id/apply_btt"
android:nextFocusUp="@id/subtitles_click_settings"
android:nextFocusDown="@id/apply_btt"
android:requiresFadingEdge="vertical"
tools:layout_height="200dp"
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/layout/subtitle_settings_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/subtitle_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>
11 changes: 10 additions & 1 deletion app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@
<!-- Needed for leanback fuckery -->
<item name="android:colorAccent">@color/colorPrimaryLavender</item>
</style>

<style name="customRatingBar" parent="@style/Widget.AppCompat.RatingBar">

<item name="android:progressDrawable">@drawable/abc_ratingbar_indicator_material</item>
Expand Down Expand Up @@ -521,6 +521,15 @@
<item name="android:windowBackground">@color/transparent</item>
</style>

<style name="DialogFullscreen">
<item name="android:windowEnterAnimation">@anim/enter_anim</item>
<item name="android:windowExitAnimation">@anim/exit_anim</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>

<style name="AlertDialogCustomBlack" parent="Theme.AppCompat.Dialog.Alert">
<item name="android:windowBackground">?attr/primaryBlackBackground</item>
<item name="android:layout_width">match_parent</item>
Expand Down

0 comments on commit 7ad563b

Please sign in to comment.