Skip to content

Commit 3c91ec3

Browse files
authored
Merge pull request #10122 from TeamNewPipe/fix/media-tunneling
Disable media tunneling by default on known unsupported devices
2 parents 6b3f51e + d6a1170 commit 3c91ec3

8 files changed

Lines changed: 252 additions & 19 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 wasMediaTunnelingDisabledAutomatically =
271+
prefs.getInt(automaticTunnelingKey, -1) == 1
272+
&& prefs.getBoolean(tunnelingKey, false);
273+
if (wasMediaTunnelingDisabledAutomatically) {
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 disabledMediaTunnelingAutomaticallyKey =
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 mediaTunnelingAutomaticallyDisabled =
27+
prefs.getInt(disabledMediaTunnelingAutomaticallyKey, -1) == 1;
28+
final String summaryText = getString(R.string.disable_media_tunneling_summary);
29+
disableMediaTunnelingPref.setSummary(mediaTunnelingAutomaticallyDisabled
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(disabledMediaTunnelingAutomaticallyKey, 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: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.schabi.newpipe.settings;
22

3+
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
4+
35
import android.content.Context;
46
import android.content.SharedPreferences;
57
import android.os.Build;
@@ -15,8 +17,6 @@
1517
import java.io.File;
1618
import java.util.Set;
1719

18-
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
19-
2020
/*
2121
* Created by k3b on 07.01.2016.
2222
*
@@ -61,7 +61,7 @@ public static void initSettings(final Context context) {
6161
}
6262

6363
// first run migrations, then setDefaultValues, since the latter requires the correct types
64-
SettingMigrations.initMigrations(context, isFirstRun);
64+
SettingMigrations.runMigrationsIfNeeded(context, isFirstRun);
6565

6666
// readAgain is true so that if new settings are added their default value is set
6767
PreferenceManager.setDefaultValues(context, R.xml.main_settings, true);
@@ -76,6 +76,8 @@ public static void initSettings(final Context context) {
7676

7777
saveDefaultVideoDownloadDirectory(context);
7878
saveDefaultAudioDownloadDirectory(context);
79+
80+
disableMediaTunnelingIfNecessary(context, isFirstRun);
7981
}
8082

8183
static void saveDefaultVideoDownloadDirectory(final Context context) {
@@ -152,4 +154,49 @@ public static boolean showRemoteSearchSuggestions(final Context context,
152154
return showSearchSuggestions(context, sharedPreferences,
153155
R.string.show_remote_search_suggestions_key);
154156
}
157+
158+
private static void disableMediaTunnelingIfNecessary(@NonNull final Context context,
159+
final boolean isFirstRun) {
160+
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
161+
final String disabledTunnelingKey = context.getString(R.string.disable_media_tunneling_key);
162+
final String disabledTunnelingAutomaticallyKey =
163+
context.getString(R.string.disabled_media_tunneling_automatically_key);
164+
final String blacklistVersionKey =
165+
context.getString(R.string.media_tunneling_device_blacklist_version);
166+
167+
final int lastMediaTunnelingUpdate = prefs.getInt(blacklistVersionKey, 0);
168+
final boolean wasDeviceBlacklistUpdated =
169+
DeviceUtils.MEDIA_TUNNELING_DEVICE_BLACKLIST_VERSION != lastMediaTunnelingUpdate;
170+
final boolean wasMediaTunnelingEnabledByUser =
171+
prefs.getInt(disabledTunnelingAutomaticallyKey, -1) == 0
172+
&& !prefs.getBoolean(disabledTunnelingKey, false);
173+
174+
if (Boolean.TRUE.equals(isFirstRun)
175+
|| (wasDeviceBlacklistUpdated && !wasMediaTunnelingEnabledByUser)) {
176+
setMediaTunneling(context);
177+
}
178+
}
179+
180+
/**
181+
* Check if device does not support media tunneling
182+
* and disable that exoplayer feature if necessary.
183+
* @see DeviceUtils#shouldSupportMediaTunneling()
184+
* @param context
185+
*/
186+
public static void setMediaTunneling(@NonNull final Context context) {
187+
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
188+
if (!DeviceUtils.shouldSupportMediaTunneling()) {
189+
prefs.edit()
190+
.putBoolean(context.getString(R.string.disable_media_tunneling_key), true)
191+
.putInt(context.getString(
192+
R.string.disabled_media_tunneling_automatically_key), 1)
193+
.putInt(context.getString(R.string.media_tunneling_device_blacklist_version),
194+
DeviceUtils.MEDIA_TUNNELING_DEVICE_BLACKLIST_VERSION)
195+
.apply();
196+
} else {
197+
prefs.edit()
198+
.putInt(context.getString(R.string.media_tunneling_device_blacklist_version),
199+
DeviceUtils.MEDIA_TUNNELING_DEVICE_BLACKLIST_VERSION).apply();
200+
}
201+
}
155202
}

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.content.SharedPreferences;
55
import android.util.Log;
66

7+
import androidx.annotation.NonNull;
78
import androidx.preference.PreferenceManager;
89

910
import org.schabi.newpipe.R;
@@ -30,9 +31,9 @@ public final class SettingMigrations {
3031
private static final String TAG = SettingMigrations.class.toString();
3132
private static SharedPreferences sp;
3233

33-
public static final Migration MIGRATION_0_1 = new Migration(0, 1) {
34+
private static final Migration MIGRATION_0_1 = new Migration(0, 1) {
3435
@Override
35-
public void migrate(final Context context) {
36+
public void migrate(@NonNull final Context context) {
3637
// We changed the content of the dialog which opens when sharing a link to NewPipe
3738
// by removing the "open detail page" option.
3839
// Therefore, show the dialog once again to ensure users need to choose again and are
@@ -44,9 +45,9 @@ public void migrate(final Context context) {
4445
}
4546
};
4647

47-
public static final Migration MIGRATION_1_2 = new Migration(1, 2) {
48+
private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
4849
@Override
49-
protected void migrate(final Context context) {
50+
protected void migrate(@NonNull final Context context) {
5051
// The new application workflow introduced in #2907 allows minimizing videos
5152
// while playing to do other stuff within the app.
5253
// For an even better workflow, we minimize a stream when switching the app to play in
@@ -63,9 +64,9 @@ protected void migrate(final Context context) {
6364
}
6465
};
6566

66-
public static final Migration MIGRATION_2_3 = new Migration(2, 3) {
67+
private static final Migration MIGRATION_2_3 = new Migration(2, 3) {
6768
@Override
68-
protected void migrate(final Context context) {
69+
protected void migrate(@NonNull final Context context) {
6970
// Storage Access Framework implementation was improved in #5415, allowing the modern
7071
// and standard way to access folders and files to be used consistently everywhere.
7172
// We reset the setting to its default value, i.e. "use SAF", since now there are no
@@ -79,9 +80,9 @@ protected void migrate(final Context context) {
7980
}
8081
};
8182

82-
public static final Migration MIGRATION_3_4 = new Migration(3, 4) {
83+
private static final Migration MIGRATION_3_4 = new Migration(3, 4) {
8384
@Override
84-
protected void migrate(final Context context) {
85+
protected void migrate(@NonNull final Context context) {
8586
// Pull request #3546 added support for choosing the type of search suggestions to
8687
// show, replacing the on-off switch used before, so migrate the previous user choice
8788

@@ -108,9 +109,9 @@ protected void migrate(final Context context) {
108109
}
109110
};
110111

111-
public static final Migration MIGRATION_4_5 = new Migration(4, 5) {
112+
private static final Migration MIGRATION_4_5 = new Migration(4, 5) {
112113
@Override
113-
protected void migrate(final Context context) {
114+
protected void migrate(@NonNull final Context context) {
114115
final boolean brightness = sp.getBoolean("brightness_gesture_control", true);
115116
final boolean volume = sp.getBoolean("volume_gesture_control", true);
116117

@@ -144,10 +145,11 @@ protected void migrate(final Context context) {
144145
/**
145146
* Version number for preferences. Must be incremented every time a migration is necessary.
146147
*/
147-
public static final int VERSION = 5;
148+
private static final int VERSION = 5;
148149

149150

150-
public static void initMigrations(final Context context, final boolean isFirstRun) {
151+
public static void runMigrationsIfNeeded(@NonNull final Context context,
152+
final boolean isFirstRun) {
151153
// setup migrations and check if there is something to do
152154
sp = PreferenceManager.getDefaultSharedPreferences(context);
153155
final String lastPrefVersionKey = context.getString(R.string.last_used_preferences_version);
@@ -212,7 +214,7 @@ private boolean shouldMigrate(final int currentVersion) {
212214
return oldVersion >= currentVersion;
213215
}
214216

215-
protected abstract void migrate(Context context);
217+
protected abstract void migrate(@NonNull Context context);
216218

217219
}
218220

0 commit comments

Comments
 (0)