Merge pull request #2503 from Bnyro/data-saver-metered

Option to allow data saver on metered networks
This commit is contained in:
Bnyro 2022-12-25 16:10:36 +01:00 committed by GitHub
commit dd1c215ba7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 37 additions and 15 deletions

View File

@ -82,6 +82,7 @@ import com.github.libretube.ui.sheets.CommentsSheet
import com.github.libretube.ui.sheets.PlayingQueueSheet
import com.github.libretube.util.BackgroundHelper
import com.github.libretube.util.DashHelper
import com.github.libretube.util.DataSaverMode
import com.github.libretube.util.ImageHelper
import com.github.libretube.util.NowPlayingNotification
import com.github.libretube.util.PlayerHelper
@ -651,13 +652,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
setupSeekbarPreview()
if (!isLive) seekToWatchPosition()
exoPlayer.prepare()
if (!PreferenceHelper.getBoolean(
PreferenceKeys.DATA_SAVER_MODE,
false
)
) {
exoPlayer.play()
}
if (!DataSaverMode.isEnabled(requireContext())) exoPlayer.play()
if (binding.playerMotionLayout.progress != 1.0f) {
// show controllers when not in picture in picture mode

View File

@ -0,0 +1,16 @@
package com.github.libretube.util
import android.content.Context
import com.github.libretube.constants.PreferenceKeys
object DataSaverMode {
fun isEnabled(context: Context): Boolean {
val pref = PreferenceHelper.getString(PreferenceKeys.DATA_SAVER_MODE, "disabled")
return when (pref) {
"enabled" -> true
"disabled" -> false
"metered"-> NetworkHelper.isNetworkMobile(context)
else -> throw IllegalArgumentException()
}
}
}

View File

@ -52,11 +52,7 @@ object ImageHelper {
*/
fun loadImage(url: String?, target: ImageView) {
// only load the image if the data saver mode is disabled
val dataSaverModeEnabled = PreferenceHelper.getBoolean(
PreferenceKeys.DATA_SAVER_MODE,
false
)
if (!dataSaverModeEnabled) target.load(url, imageLoader)
if (!DataSaverMode.isEnabled(target.context)) target.load(url, imageLoader)
}
fun downloadImage(context: Context, url: String, fileName: String) {

View File

@ -109,7 +109,7 @@ class NowPlayingNotification(
player: Player,
callback: PlayerNotificationManager.BitmapCallback
): Bitmap? {
if (PreferenceHelper.getBoolean(PreferenceKeys.DATA_SAVER_MODE, false)) return null
if (DataSaverMode.isEnabled(context)) return null
var bitmap: Bitmap? = null

View File

@ -99,6 +99,6 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/thumbnailcard"
app:shapeAppearance="@style/CircleImageView"
app:srcCompat="@mipmap/ic_launcher" />
tools:srcCompat="@mipmap/ic_launcher" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -425,4 +425,16 @@
<item>name_reversed</item>
</string-array>
<string-array name="dataSaverModeOptions">
<item>@string/disabled</item>
<item>@string/network_metered</item>
<item>@string/enabled</item>
</string-array>
<string-array name="dataSaverModeValues">
<item>disabled</item>
<item>metered</item>
<item>enabled</item>
</string-array>
</resources>

View File

@ -4,7 +4,10 @@
<PreferenceCategory app:title="@string/advanced">
<SwitchPreferenceCompat
<ListPreference
android:defaultValue="disabled"
android:entries="@array/dataSaverModeOptions"
android:entryValues="@array/dataSaverModeValues"
android:icon="@drawable/ic_data_saver"
android:summary="@string/data_saver_mode_summary"
app:key="data_saver_mode"