Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable media tunneling by default on known unsupported devices #10122

Merged
merged 5 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand Down Expand Up @@ -230,8 +231,11 @@ private void importDatabase(final StoredFileHelper file, final Uri importDataUri
})
.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();
Expand All @@ -243,6 +247,38 @@ private void importDatabase(final StoredFileHelper file, final Uri importDataUri
}
}

/**
* 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 wasMediaTunnelingDisabledAutomatically =
prefs.getInt(automaticTunnelingKey, -1) == 1
&& prefs.getBoolean(tunnelingKey, false);
if (wasMediaTunnelingDisabledAutomatically) {
prefs.edit()
.putInt(automaticTunnelingKey, -1)
.putBoolean(tunnelingKey, false)
.apply();
NewPipeSettings.setMediaTunneling(context);
}
}

/**
* Save import path and restart system.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
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 {

@Override
public void onCreatePreferences(@Nullable final Bundle savedInstanceState,
@Nullable final String rootKey) {
addPreferencesFromResourceRegistry();

final String disabledMediaTunnelingAutomaticallyKey =
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 mediaTunnelingAutomaticallyDisabled =
prefs.getInt(disabledMediaTunnelingAutomaticallyKey, -1) == 1;
final String summaryText = getString(R.string.disable_media_tunneling_summary);
disableMediaTunnelingPref.setSummary(mediaTunnelingAutomaticallyDisabled
? 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(disabledMediaTunnelingAutomaticallyKey, 0)
.apply();
// the info text might have been shown before
p.setSummary(R.string.disable_media_tunneling_summary);
}
return true;
});
}
}
53 changes: 50 additions & 3 deletions app/src/main/java/org/schabi/newpipe/settings/NewPipeSettings.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.schabi.newpipe.settings;

import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
Expand All @@ -15,8 +17,6 @@
import java.io.File;
import java.util.Set;

import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

/*
* Created by k3b on 07.01.2016.
*
Expand Down Expand Up @@ -61,7 +61,7 @@ public static void initSettings(final Context context) {
}

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

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

saveDefaultVideoDownloadDirectory(context);
saveDefaultAudioDownloadDirectory(context);

disableMediaTunnelingIfNecessary(context, isFirstRun);
}

static void saveDefaultVideoDownloadDirectory(final Context context) {
Expand Down Expand Up @@ -152,4 +154,49 @@ public static boolean showRemoteSearchSuggestions(final Context context,
return showSearchSuggestions(context, sharedPreferences,
R.string.show_remote_search_suggestions_key);
}

private static void disableMediaTunnelingIfNecessary(@NonNull final Context context,
final boolean isFirstRun) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
final String disabledTunnelingKey = context.getString(R.string.disable_media_tunneling_key);
final String disabledTunnelingAutomaticallyKey =
context.getString(R.string.disabled_media_tunneling_automatically_key);
final String blacklistVersionKey =
context.getString(R.string.media_tunneling_device_blacklist_version);

final int lastMediaTunnelingUpdate = prefs.getInt(blacklistVersionKey, 0);
final boolean wasDeviceBlacklistUpdated =
DeviceUtils.MEDIA_TUNNELING_DEVICE_BLACKLIST_VERSION != lastMediaTunnelingUpdate;
final boolean wasMediaTunnelingEnabledByUser =
prefs.getInt(disabledTunnelingAutomaticallyKey, -1) == 0
&& !prefs.getBoolean(disabledTunnelingKey, false);

if (Boolean.TRUE.equals(isFirstRun)
|| (wasDeviceBlacklistUpdated && !wasMediaTunnelingEnabledByUser)) {
setMediaTunneling(context);
}
}

/**
* Check if device does not support media tunneling
* and disable that exoplayer feature if necessary.
* @see DeviceUtils#shouldSupportMediaTunneling()
* @param context
*/
public static void setMediaTunneling(@NonNull final Context context) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (!DeviceUtils.shouldSupportMediaTunneling()) {
prefs.edit()
.putBoolean(context.getString(R.string.disable_media_tunneling_key), true)
.putInt(context.getString(
R.string.disabled_media_tunneling_automatically_key), 1)
.putInt(context.getString(R.string.media_tunneling_device_blacklist_version),
DeviceUtils.MEDIA_TUNNELING_DEVICE_BLACKLIST_VERSION)
.apply();
} else {
prefs.edit()
.putInt(context.getString(R.string.media_tunneling_device_blacklist_version),
DeviceUtils.MEDIA_TUNNELING_DEVICE_BLACKLIST_VERSION).apply();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.SharedPreferences;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.preference.PreferenceManager;

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

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

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

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

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

Expand All @@ -108,9 +109,9 @@ protected void migrate(final Context context) {
}
};

public static final Migration MIGRATION_4_5 = new Migration(4, 5) {
private static final Migration MIGRATION_4_5 = new Migration(4, 5) {
@Override
protected void migrate(final Context context) {
protected void migrate(@NonNull final Context context) {
final boolean brightness = sp.getBoolean("brightness_gesture_control", true);
final boolean volume = sp.getBoolean("volume_gesture_control", true);

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


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

protected abstract void migrate(Context context);
protected abstract void migrate(@NonNull Context context);

}

Expand Down
Loading