mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 14:20:30 +05:30
commit
3cf149be97
@ -34,6 +34,7 @@ import com.github.libretube.activities.hideKeyboard
|
||||
import com.github.libretube.adapters.ChaptersAdapter
|
||||
import com.github.libretube.adapters.CommentsAdapter
|
||||
import com.github.libretube.adapters.TrendingAdapter
|
||||
import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding
|
||||
import com.github.libretube.databinding.FragmentPlayerBinding
|
||||
import com.github.libretube.dialogs.AddtoPlaylistDialog
|
||||
import com.github.libretube.dialogs.DownloadDialog
|
||||
@ -93,6 +94,7 @@ class PlayerFragment : Fragment() {
|
||||
|
||||
private val TAG = "PlayerFragment"
|
||||
private lateinit var binding: FragmentPlayerBinding
|
||||
private lateinit var playerBinding: ExoStyledPlayerControlViewBinding
|
||||
|
||||
private var videoId: String? = null
|
||||
private var playlistId: String? = null
|
||||
@ -145,6 +147,7 @@ class PlayerFragment : Fragment() {
|
||||
savedInstanceState: Bundle?
|
||||
): View {
|
||||
binding = FragmentPlayerBinding.inflate(layoutInflater, container, false)
|
||||
playerBinding = binding.player.binding
|
||||
// Inflate the layout for this fragment
|
||||
return binding.root
|
||||
}
|
||||
@ -227,7 +230,7 @@ class PlayerFragment : Fragment() {
|
||||
.remove(this)
|
||||
.commit()
|
||||
}
|
||||
binding.player.binding.closeImageButton.setOnClickListener {
|
||||
playerBinding.closeImageButton.setOnClickListener {
|
||||
isMiniPlayerVisible = false
|
||||
binding.playerMotionLayout.transitionToEnd()
|
||||
val mainActivity = activity as MainActivity
|
||||
@ -259,11 +262,8 @@ class PlayerFragment : Fragment() {
|
||||
toggleComments()
|
||||
}
|
||||
|
||||
val fullScreenButton = binding.player.binding.fullscreen
|
||||
val exoTitle = binding.player.binding.exoTitle
|
||||
|
||||
// FullScreen button trigger
|
||||
fullScreenButton.setOnClickListener {
|
||||
playerBinding.fullscreen.setOnClickListener {
|
||||
exoPlayerView.hideController()
|
||||
if (!isFullScreen) {
|
||||
with(binding.playerMotionLayout) {
|
||||
@ -273,8 +273,8 @@ class PlayerFragment : Fragment() {
|
||||
|
||||
binding.mainContainer.isClickable = true
|
||||
binding.linLayout.visibility = View.GONE
|
||||
fullScreenButton.setImageResource(R.drawable.ic_fullscreen_exit)
|
||||
exoTitle.visibility = View.VISIBLE
|
||||
playerBinding.fullscreen.setImageResource(R.drawable.ic_fullscreen_exit)
|
||||
playerBinding.exoTitle.visibility = View.VISIBLE
|
||||
|
||||
val mainActivity = activity as MainActivity
|
||||
mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE
|
||||
@ -286,8 +286,8 @@ class PlayerFragment : Fragment() {
|
||||
|
||||
binding.mainContainer.isClickable = false
|
||||
binding.linLayout.visibility = View.VISIBLE
|
||||
fullScreenButton.setImageResource(R.drawable.ic_fullscreen)
|
||||
exoTitle.visibility = View.INVISIBLE
|
||||
playerBinding.fullscreen.setImageResource(R.drawable.ic_fullscreen)
|
||||
playerBinding.exoTitle.visibility = View.INVISIBLE
|
||||
|
||||
val mainActivity = activity as MainActivity
|
||||
mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
|
||||
@ -296,8 +296,7 @@ class PlayerFragment : Fragment() {
|
||||
}
|
||||
|
||||
// switching between original aspect ratio (black bars) and zoomed to fill device screen
|
||||
val aspectRatioButton = binding.player.binding.aspectRatioButton
|
||||
aspectRatioButton.setOnClickListener {
|
||||
playerBinding.aspectRatioButton.setOnClickListener {
|
||||
if (isZoomed) {
|
||||
exoPlayerView.resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIT
|
||||
isZoomed = false
|
||||
@ -308,13 +307,12 @@ class PlayerFragment : Fragment() {
|
||||
}
|
||||
|
||||
// lock and unlock the player
|
||||
val lockPlayerButton = binding.player.binding.lockPlayer
|
||||
lockPlayerButton.setOnClickListener {
|
||||
playerBinding.lockPlayer.setOnClickListener {
|
||||
// change the locked/unlocked icon
|
||||
if (!isPlayerLocked) {
|
||||
lockPlayerButton.setImageResource(R.drawable.ic_locked)
|
||||
playerBinding.lockPlayer.setImageResource(R.drawable.ic_locked)
|
||||
} else {
|
||||
lockPlayerButton.setImageResource(R.drawable.ic_unlocked)
|
||||
playerBinding.lockPlayer.setImageResource(R.drawable.ic_unlocked)
|
||||
}
|
||||
|
||||
// show/hide all the controls
|
||||
@ -373,6 +371,7 @@ class PlayerFragment : Fragment() {
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
try {
|
||||
saveWatchPosition()
|
||||
mediaSession.isActive = false
|
||||
mediaSession.release()
|
||||
mediaSessionConnector.setPlayer(null)
|
||||
@ -386,6 +385,25 @@ class PlayerFragment : Fragment() {
|
||||
}
|
||||
}
|
||||
|
||||
// save the watch position if video isn't finished and option enabled
|
||||
private fun saveWatchPosition() {
|
||||
val watchPositionsEnabled = PreferenceHelper.getBoolean(
|
||||
requireContext(),
|
||||
"watch_positions_toggle",
|
||||
true
|
||||
)
|
||||
if (watchPositionsEnabled && exoPlayer.currentPosition != exoPlayer.duration) {
|
||||
PreferenceHelper.saveWatchPosition(
|
||||
requireContext(),
|
||||
videoId!!,
|
||||
exoPlayer.currentPosition
|
||||
)
|
||||
} else if (watchPositionsEnabled) {
|
||||
// delete watch position if video has ended
|
||||
PreferenceHelper.removeWatchPosition(requireContext(), videoId!!)
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkForSegments() {
|
||||
if (!exoPlayer.isPlaying || !sponsorBlockPrefs.sponsorBlockEnabled) return
|
||||
|
||||
@ -447,6 +465,7 @@ class PlayerFragment : Fragment() {
|
||||
val position = arguments?.getLong("timeStamp")!! * 1000
|
||||
exoPlayer.seekTo(position)
|
||||
}
|
||||
seekToWatchPosition()
|
||||
exoPlayer.play()
|
||||
exoPlayerView.useController = true
|
||||
initializePlayerNotification(requireContext())
|
||||
@ -466,6 +485,14 @@ class PlayerFragment : Fragment() {
|
||||
run()
|
||||
}
|
||||
|
||||
// seek to saved watch position if available
|
||||
private fun seekToWatchPosition() {
|
||||
val watchPositions = PreferenceHelper.getWatchPositions(requireContext())
|
||||
watchPositions.forEach {
|
||||
if (it.videoId == videoId) exoPlayer.seekTo(it.position)
|
||||
}
|
||||
}
|
||||
|
||||
// the function is working recursively
|
||||
private fun initAutoPlay() {
|
||||
// save related streams for autoplay
|
||||
@ -629,7 +656,7 @@ class PlayerFragment : Fragment() {
|
||||
binding.playerTitle.text = response.title
|
||||
binding.playerDescription.text = response.description
|
||||
|
||||
binding.player.binding.exoTitle.text = response.title
|
||||
playerBinding.exoTitle.text = response.title
|
||||
|
||||
// Listener for play and pause icon change
|
||||
exoPlayer.addListener(object : Player.Listener {
|
||||
@ -801,9 +828,6 @@ class PlayerFragment : Fragment() {
|
||||
PreferenceHelper.getString(requireContext(), "player_video_format", "WEBM")
|
||||
val defres = PreferenceHelper.getString(requireContext(), "default_res", "")!!
|
||||
|
||||
val qualityText = binding.player.binding.qualityText
|
||||
val qualitySelect = binding.player.binding.qualitySelect
|
||||
|
||||
var videosNameArray: Array<CharSequence> = arrayOf()
|
||||
var videosUrlArray: Array<Uri> = arrayOf()
|
||||
|
||||
@ -843,7 +867,7 @@ class PlayerFragment : Fragment() {
|
||||
val videoUri = videosUrlArray[index]
|
||||
val audioUrl = getMostBitRate(response.audioStreams!!)
|
||||
setMediaSource(subtitle, videoUri, audioUrl)
|
||||
qualityText.text = videosNameArray[index]
|
||||
playerBinding.qualityText.text = videosNameArray[index]
|
||||
return@lit
|
||||
} else if (response.hls != null) {
|
||||
val mediaItem: MediaItem = MediaItem.Builder()
|
||||
@ -874,11 +898,11 @@ class PlayerFragment : Fragment() {
|
||||
val videoUri = videosUrlArray[0]
|
||||
val audioUrl = getMostBitRate(response.audioStreams!!)
|
||||
setMediaSource(subtitle, videoUri, audioUrl)
|
||||
qualityText.text = videosNameArray[0]
|
||||
playerBinding.qualityText.text = videosNameArray[0]
|
||||
}
|
||||
}
|
||||
|
||||
qualitySelect.setOnClickListener {
|
||||
playerBinding.qualitySelect.setOnClickListener {
|
||||
// Dialog for quality selection
|
||||
val builder: MaterialAlertDialogBuilder? = activity?.let {
|
||||
MaterialAlertDialogBuilder(it)
|
||||
@ -905,7 +929,7 @@ class PlayerFragment : Fragment() {
|
||||
setMediaSource(subtitle, videoUri, audioUrl)
|
||||
}
|
||||
exoPlayer.seekTo(lastPosition)
|
||||
qualityText.text = videosNameArray[which]
|
||||
playerBinding.qualityText.text = videosNameArray[which]
|
||||
}
|
||||
val dialog = builder.create()
|
||||
dialog.show()
|
||||
@ -986,12 +1010,12 @@ class PlayerFragment : Fragment() {
|
||||
|
||||
private fun lockPlayer(isLocked: Boolean) {
|
||||
val visibility = if (isLocked) View.VISIBLE else View.INVISIBLE
|
||||
binding.player.binding.exoTopBarRight.visibility = visibility
|
||||
binding.player.binding.exoPlayPause.visibility = visibility
|
||||
binding.player.binding.exoFfwdWithAmount.visibility = visibility
|
||||
binding.player.binding.exoRewWithAmount.visibility = visibility
|
||||
binding.player.binding.exoBottomBar.visibility = visibility
|
||||
binding.player.binding.exoTitle.visibility =
|
||||
playerBinding.exoTopBarRight.visibility = visibility
|
||||
playerBinding.exoPlayPause.visibility = visibility
|
||||
playerBinding.exoFfwdWithAmount.visibility = visibility
|
||||
playerBinding.exoRewWithAmount.visibility = visibility
|
||||
playerBinding.exoBottomBar.visibility = visibility
|
||||
playerBinding.exoTitle.visibility =
|
||||
if (isLocked && isFullScreen) View.VISIBLE else View.INVISIBLE
|
||||
}
|
||||
|
||||
@ -1153,7 +1177,7 @@ class PlayerFragment : Fragment() {
|
||||
enableTransition(R.id.yt_transition, false)
|
||||
}
|
||||
binding.mainContainer.isClickable = true
|
||||
binding.player.binding.exoTopBar.visibility = View.GONE
|
||||
playerBinding.exoTopBar.visibility = View.GONE
|
||||
val mainActivity = activity as MainActivity
|
||||
mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
|
||||
isFullScreen = false
|
||||
@ -1165,7 +1189,7 @@ class PlayerFragment : Fragment() {
|
||||
exoPlayerView.showController()
|
||||
exoPlayerView.useController = true
|
||||
binding.mainContainer.isClickable = false
|
||||
binding.player.binding.exoTopBar.visibility = View.VISIBLE
|
||||
playerBinding.exoTopBar.visibility = View.VISIBLE
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,6 @@
|
||||
package com.github.libretube.obj
|
||||
|
||||
data class WatchPosition(
|
||||
val videoId: String,
|
||||
val position: Long
|
||||
)
|
@ -25,10 +25,11 @@ class AdvancedSettings : PreferenceFragmentCompat() {
|
||||
true
|
||||
}
|
||||
|
||||
// clear watch history
|
||||
// clear watch history and positions
|
||||
val clearWatchHistory = findPreference<Preference>("clear_watch_history")
|
||||
clearWatchHistory?.setOnPreferenceClickListener {
|
||||
PreferenceHelper.removePreference(requireContext(), "watch_history")
|
||||
PreferenceHelper.removePreference(requireContext(), "watch_positions")
|
||||
true
|
||||
}
|
||||
|
||||
|
@ -6,6 +6,7 @@ import androidx.preference.PreferenceManager
|
||||
import com.github.libretube.obj.CustomInstance
|
||||
import com.github.libretube.obj.Streams
|
||||
import com.github.libretube.obj.WatchHistoryItem
|
||||
import com.github.libretube.obj.WatchPosition
|
||||
import com.google.common.reflect.TypeToken
|
||||
import com.google.gson.Gson
|
||||
import java.lang.reflect.Type
|
||||
@ -144,11 +145,11 @@ object PreferenceHelper {
|
||||
val watchHistory = getWatchHistory(context)
|
||||
|
||||
// delete entries that have the same videoId
|
||||
var indexToRemove = Int.MAX_VALUE
|
||||
var indexToRemove: Int? = null
|
||||
watchHistory.forEachIndexed { index, item ->
|
||||
if (item.videoId == videoId) indexToRemove = index
|
||||
}
|
||||
if (indexToRemove != Int.MAX_VALUE) watchHistory.removeAt(indexToRemove)
|
||||
if (indexToRemove != null) watchHistory.removeAt(indexToRemove!!)
|
||||
|
||||
watchHistory += watchHistoryItem
|
||||
|
||||
@ -168,6 +169,55 @@ object PreferenceHelper {
|
||||
}
|
||||
}
|
||||
|
||||
fun saveWatchPosition(context: Context, videoId: String, position: Long) {
|
||||
val editor = getDefaultSharedPreferencesEditor(context)
|
||||
|
||||
val watchPositions = getWatchPositions(context)
|
||||
val watchPositionItem = WatchPosition(videoId, position)
|
||||
|
||||
var indexToRemove: Int? = null
|
||||
watchPositions.forEachIndexed { index, item ->
|
||||
if (item.videoId == videoId) indexToRemove = index
|
||||
}
|
||||
|
||||
if (indexToRemove != null) watchPositions.removeAt(indexToRemove!!)
|
||||
|
||||
watchPositions += watchPositionItem
|
||||
|
||||
val gson = Gson()
|
||||
val json = gson.toJson(watchPositions)
|
||||
editor.putString("watch_positions", json).commit()
|
||||
}
|
||||
|
||||
fun removeWatchPosition(context: Context, videoId: String) {
|
||||
val editor = getDefaultSharedPreferencesEditor(context)
|
||||
|
||||
val watchPositions = getWatchPositions(context)
|
||||
|
||||
var indexToRemove: Int? = null
|
||||
watchPositions.forEachIndexed { index, item ->
|
||||
if (item.videoId == videoId) indexToRemove = index
|
||||
}
|
||||
|
||||
if (indexToRemove != null) watchPositions.removeAt(indexToRemove!!)
|
||||
|
||||
val gson = Gson()
|
||||
val json = gson.toJson(watchPositions)
|
||||
editor.putString("watch_positions", json).commit()
|
||||
}
|
||||
|
||||
fun getWatchPositions(context: Context): ArrayList<WatchPosition> {
|
||||
val settings = getDefaultSharedPreferences(context)
|
||||
val gson = Gson()
|
||||
val json: String = settings.getString("watch_positions", "")!!
|
||||
val type: Type = object : TypeToken<List<WatchPosition?>?>() {}.type
|
||||
return try {
|
||||
gson.fromJson(json, type)
|
||||
} catch (e: Exception) {
|
||||
arrayListOf()
|
||||
}
|
||||
}
|
||||
|
||||
private fun getDefaultSharedPreferences(context: Context): SharedPreferences {
|
||||
return PreferenceManager.getDefaultSharedPreferences(context)
|
||||
}
|
||||
|
@ -208,4 +208,6 @@
|
||||
<string name="account">Account</string>
|
||||
<string name="restore">Restore</string>
|
||||
<string name="watch_history">Watch History</string>
|
||||
<string name="watch_positions">Remember position</string>
|
||||
<string name="watch_positions_summary">Remember the watch position and automatically seek to it.</string>
|
||||
</resources>
|
@ -54,6 +54,13 @@
|
||||
app:key="watch_history_toggle"
|
||||
app:title="@string/watch_history" />
|
||||
|
||||
<SwitchPreference
|
||||
android:defaultValue="true"
|
||||
android:icon="@drawable/ic_play_filled"
|
||||
app:key="watch_position_toggle"
|
||||
app:title="@string/watch_positions"
|
||||
app:summary="@string/watch_positions_summary"/>
|
||||
|
||||
<Preference
|
||||
android:icon="@drawable/ic_trash"
|
||||
app:key="clear_watch_history"
|
||||
|
Loading…
Reference in New Issue
Block a user