add quality settings for mobile data

This commit is contained in:
Bnyro 2022-09-11 17:54:04 +02:00
parent e54954a97d
commit c4580d2d21
6 changed files with 79 additions and 13 deletions

View File

@ -65,9 +65,11 @@ object PreferenceKeys {
const val SEEK_INCREMENT = "seek_increment" const val SEEK_INCREMENT = "seek_increment"
const val PLAYER_VIDEO_FORMAT = "player_video_format" const val PLAYER_VIDEO_FORMAT = "player_video_format"
const val DEFAULT_RESOLUTION = "default_res" const val DEFAULT_RESOLUTION = "default_res"
const val DEFAULT_RESOLUTION_MOBILE = "default_res_mobile"
const val BUFFERING_GOAL = "buffering_goal" const val BUFFERING_GOAL = "buffering_goal"
const val PLAYER_AUDIO_FORMAT = "player_audio_format" const val PLAYER_AUDIO_FORMAT = "player_audio_format"
const val PLAYER_AUDIO_QUALITY = "player_audio_quality" const val PLAYER_AUDIO_QUALITY = "player_audio_quality"
const val PLAYER_AUDIO_QUALITY_MOBILE = "player_audio_quality_mobile"
const val DEFAULT_SUBTITLE = "default_subtitle" const val DEFAULT_SUBTITLE = "default_subtitle"
const val SKIP_BUTTONS = "skip_buttons" const val SKIP_BUTTONS = "skip_buttons"
const val PICTURE_IN_PICTURE = "picture_in_picture" const val PICTURE_IN_PICTURE = "picture_in_picture"

View File

@ -70,6 +70,7 @@ import com.github.libretube.services.BackgroundMode
import com.github.libretube.util.AutoPlayHelper import com.github.libretube.util.AutoPlayHelper
import com.github.libretube.util.BackgroundHelper import com.github.libretube.util.BackgroundHelper
import com.github.libretube.util.ImageHelper import com.github.libretube.util.ImageHelper
import com.github.libretube.util.NetworkHelper
import com.github.libretube.util.NowPlayingNotification import com.github.libretube.util.NowPlayingNotification
import com.github.libretube.util.PlayerHelper import com.github.libretube.util.PlayerHelper
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
@ -224,7 +225,7 @@ class PlayerFragment : BaseFragment() {
createExoPlayer() createExoPlayer()
initializeTransitionLayout() initializeTransitionLayout()
initializeOnClickActions(requireContext()) initializeOnClickActions()
playVideo() playVideo()
showBottomBar() showBottomBar()
@ -290,10 +291,17 @@ class PlayerFragment : BaseFragment() {
"webm" "webm"
) )
defRes = PreferenceHelper.getString( defRes = if (NetworkHelper.isNetworkMobile(requireContext())) {
PreferenceKeys.DEFAULT_RESOLUTION, PreferenceHelper.getString(
"" PreferenceKeys.DEFAULT_RESOLUTION_MOBILE,
) ""
)
} else {
PreferenceHelper.getString(
PreferenceKeys.DEFAULT_RESOLUTION,
""
)
}
bufferingGoal = PreferenceHelper.getString( bufferingGoal = PreferenceHelper.getString(
PreferenceKeys.BUFFERING_GOAL, PreferenceKeys.BUFFERING_GOAL,
@ -465,7 +473,7 @@ class PlayerFragment : BaseFragment() {
exoPlayer.setMediaItem(mediaItem) exoPlayer.setMediaItem(mediaItem)
} else { } else {
val videoUri = videosUrlArray[which] val videoUri = videosUrlArray[which]
val audioUrl = PlayerHelper.getAudioSource(streams.audioStreams!!) val audioUrl = PlayerHelper.getAudioSource(requireContext(), streams.audioStreams!!)
setMediaSource(videoUri, audioUrl) setMediaSource(videoUri, audioUrl)
} }
exoPlayer.seekTo(lastPosition) exoPlayer.seekTo(lastPosition)
@ -475,7 +483,7 @@ class PlayerFragment : BaseFragment() {
} }
// actions that don't depend on video information // actions that don't depend on video information
private fun initializeOnClickActions(context: Context) { private fun initializeOnClickActions() {
binding.closeImageView.setOnClickListener { binding.closeImageView.setOnClickListener {
viewModel.isMiniPlayerVisible.value = false viewModel.isMiniPlayerVisible.value = false
binding.playerMotionLayout.transitionToEnd() binding.playerMotionLayout.transitionToEnd()
@ -1255,7 +1263,7 @@ class PlayerFragment : BaseFragment() {
// search for quality preference in the available stream sources // search for quality preference in the available stream sources
if (pipedStream.contains(defRes)) { if (pipedStream.contains(defRes)) {
val videoUri = videosUrlArray[index] val videoUri = videosUrlArray[index]
val audioUrl = PlayerHelper.getAudioSource(streams.audioStreams!!) val audioUrl = PlayerHelper.getAudioSource(requireContext(), streams.audioStreams!!)
setMediaSource(videoUri, audioUrl) setMediaSource(videoUri, audioUrl)
return return
} }
@ -1275,7 +1283,7 @@ class PlayerFragment : BaseFragment() {
// if nothing found, use the first list entry // if nothing found, use the first list entry
if (videosUrlArray.isNotEmpty()) { if (videosUrlArray.isNotEmpty()) {
val videoUri = videosUrlArray[0] val videoUri = videosUrlArray[0]
val audioUrl = PlayerHelper.getAudioSource(streams.audioStreams!!) val audioUrl = PlayerHelper.getAudioSource(requireContext(), streams.audioStreams!!)
setMediaSource(videoUri, audioUrl) setMediaSource(videoUri, audioUrl)
} }
} }

View File

@ -2,6 +2,8 @@ package com.github.libretube.util
import android.content.Context import android.content.Context
import android.net.ConnectivityManager import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build
object NetworkHelper { object NetworkHelper {
/** /**
@ -26,7 +28,7 @@ object NetworkHelper {
// Bluetooth // Bluetooth
actNw.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH) -> true actNw.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH) -> true
// VPN // VPN
actNw.hasCapability(NetworkCapabilities.TRANSPORT_VPN) -> true actNw.hasTransport(NetworkCapabilities.TRANSPORT_VPN) -> true
else -> false else -> false
} }
} else { } else {
@ -34,6 +36,26 @@ object NetworkHelper {
} }
*/ */
@Suppress("DEPRECATION")
return connectivityManager.activeNetworkInfo?.isConnected ?: false return connectivityManager.activeNetworkInfo?.isConnected ?: false
} }
/**
* Detect whether the current network is mobile data
* @param context Context of the application
* @return isNetworkMobile
*/
@Suppress("DEPRECATION")
fun isNetworkMobile(context: Context): Boolean {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val networkCapabilities = connectivityManager.getNetworkCapabilities(
connectivityManager.activeNetwork ?: return false
)
return networkCapabilities?.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) ?: false
} else {
val activeNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
return activeNetwork != null && activeNetwork.isConnected
}
}
} }

View File

@ -8,9 +8,15 @@ import com.google.android.exoplayer2.ui.CaptionStyleCompat
object PlayerHelper { object PlayerHelper {
// get the audio source following the users preferences // get the audio source following the users preferences
fun getAudioSource(audios: List<PipedStream>): String { fun getAudioSource(context: Context, audios: List<PipedStream>): String {
val audioFormat = PreferenceHelper.getString(PreferenceKeys.PLAYER_AUDIO_FORMAT, "all") val audioFormat = PreferenceHelper.getString(PreferenceKeys.PLAYER_AUDIO_FORMAT, "all")
val audioQuality = PreferenceHelper.getString(PreferenceKeys.PLAYER_AUDIO_QUALITY, "best") val audioQuality = if (
NetworkHelper.isNetworkMobile(context)
) {
PreferenceHelper.getString(PreferenceKeys.PLAYER_AUDIO_QUALITY_MOBILE, "best")
} else {
PreferenceHelper.getString(PreferenceKeys.PLAYER_AUDIO_QUALITY, "best")
}
val mutableAudios = audios.toMutableList() val mutableAudios = audios.toMutableList()
if (audioFormat != "all") { if (audioFormat != "all") {

View File

@ -317,4 +317,6 @@
<string name="delete">Delete</string> <string name="delete">Delete</string>
<string name="trending_layout">Alternative trending layout</string> <string name="trending_layout">Alternative trending layout</string>
<string name="renamePlaylist">Rename playlist</string> <string name="renamePlaylist">Rename playlist</string>
<string name="wifi">WiFi</string>
<string name="mobile_data">Mobile data</string>
</resources> </resources>

View File

@ -2,7 +2,7 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory app:title="@string/quality"> <PreferenceCategory app:title="@string/wifi">
<ListPreference <ListPreference
android:icon="@drawable/ic_hd" android:icon="@drawable/ic_hd"
@ -22,6 +22,32 @@
app:title="@string/playerAudioQuality" app:title="@string/playerAudioQuality"
app:useSimpleSummaryProvider="true" /> app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
<PreferenceCategory app:title="@string/mobile_data">
<ListPreference
android:icon="@drawable/ic_hd"
app:defaultValue=""
app:entries="@array/defres"
app:entryValues="@array/defresValue"
app:key="default_res_mobile"
app:title="@string/defres"
app:useSimpleSummaryProvider="true" />
<ListPreference
android:icon="@drawable/ic_headphones"
app:defaultValue="best"
app:entries="@array/audioQuality"
app:entryValues="@array/audioQualityValues"
app:key="player_audio_quality_mobile"
app:title="@string/playerAudioQuality"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
<PreferenceCategory app:title="@string/quality">
<ListPreference <ListPreference
android:icon="@drawable/ic_videocam" android:icon="@drawable/ic_videocam"
app:defaultValue="webm" app:defaultValue="webm"