Merge pull request #1270 from Bnyro/master

add quality settings for mobile data
This commit is contained in:
Bnyro 2022-09-11 17:55:41 +02:00 committed by GitHub
commit e9795c884b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 PLAYER_VIDEO_FORMAT = "player_video_format"
const val DEFAULT_RESOLUTION = "default_res"
const val DEFAULT_RESOLUTION_MOBILE = "default_res_mobile"
const val BUFFERING_GOAL = "buffering_goal"
const val PLAYER_AUDIO_FORMAT = "player_audio_format"
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 SKIP_BUTTONS = "skip_buttons"
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.BackgroundHelper
import com.github.libretube.util.ImageHelper
import com.github.libretube.util.NetworkHelper
import com.github.libretube.util.NowPlayingNotification
import com.github.libretube.util.PlayerHelper
import com.github.libretube.util.PreferenceHelper
@ -224,7 +225,7 @@ class PlayerFragment : BaseFragment() {
createExoPlayer()
initializeTransitionLayout()
initializeOnClickActions(requireContext())
initializeOnClickActions()
playVideo()
showBottomBar()
@ -290,10 +291,17 @@ class PlayerFragment : BaseFragment() {
"webm"
)
defRes = PreferenceHelper.getString(
PreferenceKeys.DEFAULT_RESOLUTION,
""
)
defRes = if (NetworkHelper.isNetworkMobile(requireContext())) {
PreferenceHelper.getString(
PreferenceKeys.DEFAULT_RESOLUTION_MOBILE,
""
)
} else {
PreferenceHelper.getString(
PreferenceKeys.DEFAULT_RESOLUTION,
""
)
}
bufferingGoal = PreferenceHelper.getString(
PreferenceKeys.BUFFERING_GOAL,
@ -465,7 +473,7 @@ class PlayerFragment : BaseFragment() {
exoPlayer.setMediaItem(mediaItem)
} else {
val videoUri = videosUrlArray[which]
val audioUrl = PlayerHelper.getAudioSource(streams.audioStreams!!)
val audioUrl = PlayerHelper.getAudioSource(requireContext(), streams.audioStreams!!)
setMediaSource(videoUri, audioUrl)
}
exoPlayer.seekTo(lastPosition)
@ -475,7 +483,7 @@ class PlayerFragment : BaseFragment() {
}
// actions that don't depend on video information
private fun initializeOnClickActions(context: Context) {
private fun initializeOnClickActions() {
binding.closeImageView.setOnClickListener {
viewModel.isMiniPlayerVisible.value = false
binding.playerMotionLayout.transitionToEnd()
@ -1255,7 +1263,7 @@ class PlayerFragment : BaseFragment() {
// search for quality preference in the available stream sources
if (pipedStream.contains(defRes)) {
val videoUri = videosUrlArray[index]
val audioUrl = PlayerHelper.getAudioSource(streams.audioStreams!!)
val audioUrl = PlayerHelper.getAudioSource(requireContext(), streams.audioStreams!!)
setMediaSource(videoUri, audioUrl)
return
}
@ -1275,7 +1283,7 @@ class PlayerFragment : BaseFragment() {
// if nothing found, use the first list entry
if (videosUrlArray.isNotEmpty()) {
val videoUri = videosUrlArray[0]
val audioUrl = PlayerHelper.getAudioSource(streams.audioStreams!!)
val audioUrl = PlayerHelper.getAudioSource(requireContext(), streams.audioStreams!!)
setMediaSource(videoUri, audioUrl)
}
}

View File

@ -2,6 +2,8 @@ package com.github.libretube.util
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build
object NetworkHelper {
/**
@ -26,7 +28,7 @@ object NetworkHelper {
// Bluetooth
actNw.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH) -> true
// VPN
actNw.hasCapability(NetworkCapabilities.TRANSPORT_VPN) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_VPN) -> true
else -> false
}
} else {
@ -34,6 +36,26 @@ object NetworkHelper {
}
*/
@Suppress("DEPRECATION")
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 {
// 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 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()
if (audioFormat != "all") {

View File

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

View File

@ -2,7 +2,7 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory app:title="@string/quality">
<PreferenceCategory app:title="@string/wifi">
<ListPreference
android:icon="@drawable/ic_hd"
@ -22,6 +22,32 @@
app:title="@string/playerAudioQuality"
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
android:icon="@drawable/ic_videocam"
app:defaultValue="webm"