From 1db73370a7219e64b472c77b57f9436c2abb4fcf Mon Sep 17 00:00:00 2001 From: TobiGr Date: Sat, 29 Jul 2023 22:08:24 +0200 Subject: [PATCH] Ensure that imports handle disabling media tunneling correctly Store in preferences whether media tunneling was disabled automatically. Show info in ExoPlayer settings if media tunneling was disabled autmatically. --- .../settings/ContentSettingsFragment.java | 40 ++++++++++++++++++- .../settings/ContentSettingsManager.kt | 3 ++ .../settings/ExoPlayerSettingsFragment.java | 31 ++++++++++++++ .../newpipe/settings/NewPipeSettings.java | 2 + .../org/schabi/newpipe/util/DeviceUtils.java | 2 +- app/src/main/res/values/settings_keys.xml | 1 + app/src/main/res/values/strings.xml | 3 +- 7 files changed, 78 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java index 434056b20..38a1fd8f8 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java @@ -15,6 +15,7 @@ import android.widget.Toast; import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult; +import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import androidx.core.content.ContextCompat; import androidx.preference.Preference; @@ -230,8 +231,11 @@ public class ContentSettingsFragment extends BasePreferenceFragment { }) .setPositiveButton(R.string.ok, (dialog, which) -> { dialog.dismiss(); - manager.loadSharedPreferences(PreferenceManager - .getDefaultSharedPreferences(requireContext())); + final Context context = requireContext(); + final SharedPreferences prefs = PreferenceManager + .getDefaultSharedPreferences(context); + manager.loadSharedPreferences(prefs); + cleanImport(context, prefs); finishImport(importDataUri); }) .show(); @@ -243,6 +247,38 @@ public class ContentSettingsFragment extends BasePreferenceFragment { } } + /** + * Remove settings that are not supposed to be imported on different devices + * and reset them to default values. + * @param context the context used for the import + * @param prefs the preferences used while running the import + */ + private void cleanImport(@NonNull final Context context, + @NonNull final SharedPreferences prefs) { + // Check if media tunnelling needs to be disabled automatically, + // if it was disabled automatically in the imported preferences. + final String tunnelingKey = context.getString(R.string.disable_media_tunneling_key); + final String automaticTunnelingKey = + context.getString(R.string.disabled_media_tunneling_automatically_key); + // R.string.disable_media_tunneling_key should always be true + // if R.string.disabled_media_tunneling_automatically_key equals 1, + // but we double check here just to be sure and to avoid regressions + // caused by possible later modification of the media tunneling functionality. + // R.string.disabled_media_tunneling_automatically_key == 0: + // automatic value overridden by user in settings + // R.string.disabled_media_tunneling_automatically_key == -1: not set + final boolean wasMediaTunnelingEnabledAutomatically = + prefs.getInt(automaticTunnelingKey, -1) == 1 + && prefs.getBoolean(tunnelingKey, false); + if (wasMediaTunnelingEnabledAutomatically) { + prefs.edit() + .putInt(automaticTunnelingKey, -1) + .putBoolean(tunnelingKey, false) + .apply(); + NewPipeSettings.setMediaTunneling(context); + } + } + /** * Save import path and restart system. * diff --git a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsManager.kt b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsManager.kt index 8adf6a649..92be93a29 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsManager.kt +++ b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsManager.kt @@ -67,6 +67,9 @@ class ContentSettingsManager(private val fileLocator: NewPipeFileLocator) { return ZipHelper.extractFileFromZip(file, fileLocator.settings.path, "newpipe.settings") } + /** + * Remove all shared preferences from the app and load the preferences supplied to the manager. + */ fun loadSharedPreferences(preferences: SharedPreferences) { try { val preferenceEditor = preferences.edit() diff --git a/app/src/main/java/org/schabi/newpipe/settings/ExoPlayerSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/ExoPlayerSettingsFragment.java index 7e740f869..1cceea336 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ExoPlayerSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/ExoPlayerSettingsFragment.java @@ -1,8 +1,14 @@ package org.schabi.newpipe.settings; +import android.content.SharedPreferences; import android.os.Bundle; import androidx.annotation.Nullable; +import androidx.preference.Preference; +import androidx.preference.PreferenceManager; +import androidx.preference.SwitchPreferenceCompat; + +import org.schabi.newpipe.R; public class ExoPlayerSettingsFragment extends BasePreferenceFragment { @@ -10,5 +16,30 @@ public class ExoPlayerSettingsFragment extends BasePreferenceFragment { public void onCreatePreferences(@Nullable final Bundle savedInstanceState, @Nullable final String rootKey) { addPreferencesFromResourceRegistry(); + + final String disableMediaTunnelingAutomaticallyKey = + getString(R.string.disabled_media_tunneling_automatically_key); + final SwitchPreferenceCompat disableMediaTunnelingPref = + (SwitchPreferenceCompat) requirePreference(R.string.disable_media_tunneling_key); + final SharedPreferences prefs = PreferenceManager + .getDefaultSharedPreferences(requireContext()); + final boolean mediaTunnelingAutomaticallyEnabled = + prefs.getInt(disableMediaTunnelingAutomaticallyKey, -1) == 1; + final String summaryText = getString(R.string.disable_media_tunneling_summary); + disableMediaTunnelingPref.setSummary(mediaTunnelingAutomaticallyEnabled + ? summaryText + getString(R.string.disable_media_tunneling_automatic_info) + : summaryText); + + disableMediaTunnelingPref.setOnPreferenceChangeListener((Preference p, Object enabled) -> { + if (Boolean.FALSE.equals(enabled)) { + PreferenceManager.getDefaultSharedPreferences(requireContext()) + .edit() + .putInt(disableMediaTunnelingAutomaticallyKey, 0) + .apply(); + // the info text might have been shown before + p.setSummary(R.string.disable_media_tunneling_summary); + } + return true; + }); } } diff --git a/app/src/main/java/org/schabi/newpipe/settings/NewPipeSettings.java b/app/src/main/java/org/schabi/newpipe/settings/NewPipeSettings.java index 9e246574e..7057f0ec9 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/NewPipeSettings.java +++ b/app/src/main/java/org/schabi/newpipe/settings/NewPipeSettings.java @@ -167,6 +167,8 @@ public final class NewPipeSettings { if (!DeviceUtils.shouldSupportMediaTunneling()) { PreferenceManager.getDefaultSharedPreferences(context).edit() .putBoolean(context.getString(R.string.disable_media_tunneling_key), true) + .putInt(context.getString( + R.string.disabled_media_tunneling_automatically_key), 1) .apply(); } } diff --git a/app/src/main/java/org/schabi/newpipe/util/DeviceUtils.java b/app/src/main/java/org/schabi/newpipe/util/DeviceUtils.java index 7111b8de9..111072a96 100644 --- a/app/src/main/java/org/schabi/newpipe/util/DeviceUtils.java +++ b/app/src/main/java/org/schabi/newpipe/util/DeviceUtils.java @@ -255,7 +255,7 @@ public final class DeviceUtils { * See https://github.com/TeamNewPipe/NewPipe/issues/5911 * @Note Add a new {@link org.schabi.newpipe.settings.SettingMigrations.Migration} which calls * {@link org.schabi.newpipe.settings.NewPipeSettings#setMediaTunneling(Context)} - * when adding a new device to the method + * when adding a new device to the method. * @return {@code false} if affected device; {@code true} otherwise */ public static boolean shouldSupportMediaTunneling() { diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index eebe29d81..b2e2925f3 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -1382,6 +1382,7 @@ exoplayer_settings_key disable_media_tunneling_key + disabled_media_tunneling_automatically_key use_exoplayer_decoder_fallback_key always_use_exoplayer_set_output_surface_workaround_key diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 75acedf9e..e4fcdeedc 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -480,7 +480,8 @@ Show original time ago on items Original texts from services will be visible in stream items Disable media tunneling - Disable media tunneling if you experience a black screen or stuttering on video playback + Disable media tunneling if you experience a black screen or stuttering on video playback. + Media tunneling was disabled by default on your device because your device model is known to not support it. Show image indicators Show Picasso colored ribbons on top of images indicating their source: red for network, blue for disk and green for memory Show \"Crash the player\"