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.
This commit is contained in:
TobiGr 2023-07-29 22:08:24 +02:00
parent 8b63b437d8
commit 1db73370a7
7 changed files with 78 additions and 4 deletions

View File

@ -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.
*

View File

@ -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()

View File

@ -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;
});
}
}

View File

@ -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();
}
}

View File

@ -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() {

View File

@ -1382,6 +1382,7 @@
<!-- ExoPlayer settings -->
<string name="exoplayer_settings_key">exoplayer_settings_key</string>
<string name="disable_media_tunneling_key">disable_media_tunneling_key</string>
<string name="disabled_media_tunneling_automatically_key">disabled_media_tunneling_automatically_key</string>
<string name="use_exoplayer_decoder_fallback_key">use_exoplayer_decoder_fallback_key</string>
<string name="always_use_exoplayer_set_output_surface_workaround_key">always_use_exoplayer_set_output_surface_workaround_key</string>
</resources>

View File

@ -480,7 +480,8 @@
<string name="show_original_time_ago_title">Show original time ago on items</string>
<string name="show_original_time_ago_summary">Original texts from services will be visible in stream items</string>
<string name="disable_media_tunneling_title">Disable media tunneling</string>
<string name="disable_media_tunneling_summary">Disable media tunneling if you experience a black screen or stuttering on video playback</string>
<string name="disable_media_tunneling_summary">Disable media tunneling if you experience a black screen or stuttering on video playback.</string>
<string name="disable_media_tunneling_automatic_info">Media tunneling was disabled by default on your device because your device model is known to not support it.</string>
<string name="show_image_indicators_title">Show image indicators</string>
<string name="show_image_indicators_summary">Show Picasso colored ribbons on top of images indicating their source: red for network, blue for disk and green for memory</string>
<string name="show_crash_the_player_title">Show \"Crash the player\"</string>