Skip to content

Commit 1db7337

Browse files
committed
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.
1 parent 8b63b43 commit 1db7337

7 files changed

Lines changed: 78 additions & 4 deletions

File tree

app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import androidx.activity.result.ActivityResult;
1616
import androidx.activity.result.ActivityResultLauncher;
1717
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult;
18+
import androidx.annotation.NonNull;
1819
import androidx.appcompat.app.AlertDialog;
1920
import androidx.core.content.ContextCompat;
2021
import androidx.preference.Preference;
@@ -230,8 +231,11 @@ private void importDatabase(final StoredFileHelper file, final Uri importDataUri
230231
})
231232
.setPositiveButton(R.string.ok, (dialog, which) -> {
232233
dialog.dismiss();
233-
manager.loadSharedPreferences(PreferenceManager
234-
.getDefaultSharedPreferences(requireContext()));
234+
final Context context = requireContext();
235+
final SharedPreferences prefs = PreferenceManager
236+
.getDefaultSharedPreferences(context);
237+
manager.loadSharedPreferences(prefs);
238+
cleanImport(context, prefs);
235239
finishImport(importDataUri);
236240
})
237241
.show();
@@ -243,6 +247,38 @@ private void importDatabase(final StoredFileHelper file, final Uri importDataUri
243247
}
244248
}
245249

250+
/**
251+
* Remove settings that are not supposed to be imported on different devices
252+
* and reset them to default values.
253+
* @param context the context used for the import
254+
* @param prefs the preferences used while running the import
255+
*/
256+
private void cleanImport(@NonNull final Context context,
257+
@NonNull final SharedPreferences prefs) {
258+
// Check if media tunnelling needs to be disabled automatically,
259+
// if it was disabled automatically in the imported preferences.
260+
final String tunnelingKey = context.getString(R.string.disable_media_tunneling_key);
261+
final String automaticTunnelingKey =
262+
context.getString(R.string.disabled_media_tunneling_automatically_key);
263+
// R.string.disable_media_tunneling_key should always be true
264+
// if R.string.disabled_media_tunneling_automatically_key equals 1,
265+
// but we double check here just to be sure and to avoid regressions
266+
// caused by possible later modification of the media tunneling functionality.
267+
// R.string.disabled_media_tunneling_automatically_key == 0:
268+
// automatic value overridden by user in settings
269+
// R.string.disabled_media_tunneling_automatically_key == -1: not set
270+
final boolean wasMediaTunnelingEnabledAutomatically =
271+
prefs.getInt(automaticTunnelingKey, -1) == 1
272+
&& prefs.getBoolean(tunnelingKey, false);
273+
if (wasMediaTunnelingEnabledAutomatically) {
274+
prefs.edit()
275+
.putInt(automaticTunnelingKey, -1)
276+
.putBoolean(tunnelingKey, false)
277+
.apply();
278+
NewPipeSettings.setMediaTunneling(context);
279+
}
280+
}
281+
246282
/**
247283
* Save import path and restart system.
248284
*

app/src/main/java/org/schabi/newpipe/settings/ContentSettingsManager.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ class ContentSettingsManager(private val fileLocator: NewPipeFileLocator) {
6767
return ZipHelper.extractFileFromZip(file, fileLocator.settings.path, "newpipe.settings")
6868
}
6969

70+
/**
71+
* Remove all shared preferences from the app and load the preferences supplied to the manager.
72+
*/
7073
fun loadSharedPreferences(preferences: SharedPreferences) {
7174
try {
7275
val preferenceEditor = preferences.edit()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,45 @@
11
package org.schabi.newpipe.settings;
22

3+
import android.content.SharedPreferences;
34
import android.os.Bundle;
45

56
import androidx.annotation.Nullable;
7+
import androidx.preference.Preference;
8+
import androidx.preference.PreferenceManager;
9+
import androidx.preference.SwitchPreferenceCompat;
10+
11+
import org.schabi.newpipe.R;
612

713
public class ExoPlayerSettingsFragment extends BasePreferenceFragment {
814

915
@Override
1016
public void onCreatePreferences(@Nullable final Bundle savedInstanceState,
1117
@Nullable final String rootKey) {
1218
addPreferencesFromResourceRegistry();
19+
20+
final String disableMediaTunnelingAutomaticallyKey =
21+
getString(R.string.disabled_media_tunneling_automatically_key);
22+
final SwitchPreferenceCompat disableMediaTunnelingPref =
23+
(SwitchPreferenceCompat) requirePreference(R.string.disable_media_tunneling_key);
24+
final SharedPreferences prefs = PreferenceManager
25+
.getDefaultSharedPreferences(requireContext());
26+
final boolean mediaTunnelingAutomaticallyEnabled =
27+
prefs.getInt(disableMediaTunnelingAutomaticallyKey, -1) == 1;
28+
final String summaryText = getString(R.string.disable_media_tunneling_summary);
29+
disableMediaTunnelingPref.setSummary(mediaTunnelingAutomaticallyEnabled
30+
? summaryText + getString(R.string.disable_media_tunneling_automatic_info)
31+
: summaryText);
32+
33+
disableMediaTunnelingPref.setOnPreferenceChangeListener((Preference p, Object enabled) -> {
34+
if (Boolean.FALSE.equals(enabled)) {
35+
PreferenceManager.getDefaultSharedPreferences(requireContext())
36+
.edit()
37+
.putInt(disableMediaTunnelingAutomaticallyKey, 0)
38+
.apply();
39+
// the info text might have been shown before
40+
p.setSummary(R.string.disable_media_tunneling_summary);
41+
}
42+
return true;
43+
});
1344
}
1445
}

app/src/main/java/org/schabi/newpipe/settings/NewPipeSettings.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ public static void setMediaTunneling(@NonNull final Context context) {
167167
if (!DeviceUtils.shouldSupportMediaTunneling()) {
168168
PreferenceManager.getDefaultSharedPreferences(context).edit()
169169
.putBoolean(context.getString(R.string.disable_media_tunneling_key), true)
170+
.putInt(context.getString(
171+
R.string.disabled_media_tunneling_automatically_key), 1)
170172
.apply();
171173
}
172174
}

app/src/main/java/org/schabi/newpipe/util/DeviceUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public static int getWindowHeight(@NonNull final WindowManager windowManager) {
255255
* See https://github.com/TeamNewPipe/NewPipe/issues/5911
256256
* @Note Add a new {@link org.schabi.newpipe.settings.SettingMigrations.Migration} which calls
257257
* {@link org.schabi.newpipe.settings.NewPipeSettings#setMediaTunneling(Context)}
258-
* when adding a new device to the method
258+
* when adding a new device to the method.
259259
* @return {@code false} if affected device; {@code true} otherwise
260260
*/
261261
public static boolean shouldSupportMediaTunneling() {

app/src/main/res/values/settings_keys.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,7 @@
13821382
<!-- ExoPlayer settings -->
13831383
<string name="exoplayer_settings_key">exoplayer_settings_key</string>
13841384
<string name="disable_media_tunneling_key">disable_media_tunneling_key</string>
1385+
<string name="disabled_media_tunneling_automatically_key">disabled_media_tunneling_automatically_key</string>
13851386
<string name="use_exoplayer_decoder_fallback_key">use_exoplayer_decoder_fallback_key</string>
13861387
<string name="always_use_exoplayer_set_output_surface_workaround_key">always_use_exoplayer_set_output_surface_workaround_key</string>
13871388
</resources>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,8 @@
480480
<string name="show_original_time_ago_title">Show original time ago on items</string>
481481
<string name="show_original_time_ago_summary">Original texts from services will be visible in stream items</string>
482482
<string name="disable_media_tunneling_title">Disable media tunneling</string>
483-
<string name="disable_media_tunneling_summary">Disable media tunneling if you experience a black screen or stuttering on video playback</string>
483+
<string name="disable_media_tunneling_summary">Disable media tunneling if you experience a black screen or stuttering on video playback.</string>
484+
<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>
484485
<string name="show_image_indicators_title">Show image indicators</string>
485486
<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>
486487
<string name="show_crash_the_player_title">Show \"Crash the player\"</string>

0 commit comments

Comments
 (0)