From 2d1823f2a15262a16be62f29f57a8b19229024e0 Mon Sep 17 00:00:00 2001 From: fokin33 Date: Sat, 31 Jan 2026 00:15:05 +0300 Subject: [PATCH] PR#12691 fix by remarks --- .../org/schabi/newpipe/DownloaderImpl.java | 2 +- .../datasource/YoutubeHttpDataSource.java | 2 +- .../player/helper/PlayerDataSource.java | 10 ++-- .../settings/ProxySettingsFragment.java | 60 ++++++++++++++++--- .../settings/SettingsResourceRegistry.java | 1 + .../{settings => util}/ProxyManager.java | 2 +- .../newpipe/util/image/PicassoHelper.java | 2 +- .../us/shandian/giga/get/DownloadMission.java | 18 +++++- .../giga/service/DownloadManagerService.java | 2 +- app/src/main/res/menu/main_menu.xml | 9 --- app/src/main/res/values-ru/strings.xml | 10 ++++ app/src/main/res/values-ru/strings_proxy.xml | 11 ---- app/src/main/res/values/strings.xml | 19 ++++++ app/src/main/res/values/strings_proxy.xml | 20 ------- 14 files changed, 107 insertions(+), 61 deletions(-) rename app/src/main/java/org/schabi/newpipe/{settings => util}/ProxyManager.java (98%) delete mode 100644 app/src/main/res/menu/main_menu.xml delete mode 100644 app/src/main/res/values-ru/strings_proxy.xml delete mode 100644 app/src/main/res/values/strings_proxy.xml diff --git a/app/src/main/java/org/schabi/newpipe/DownloaderImpl.java b/app/src/main/java/org/schabi/newpipe/DownloaderImpl.java index 4b33c4c30..59ab0b61d 100644 --- a/app/src/main/java/org/schabi/newpipe/DownloaderImpl.java +++ b/app/src/main/java/org/schabi/newpipe/DownloaderImpl.java @@ -11,7 +11,7 @@ import org.schabi.newpipe.extractor.downloader.Downloader; import org.schabi.newpipe.extractor.downloader.Request; import org.schabi.newpipe.extractor.downloader.Response; import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; -import org.schabi.newpipe.settings.ProxyManager; +import org.schabi.newpipe.util.ProxyManager; import org.schabi.newpipe.util.InfoCache; import java.io.IOException; diff --git a/app/src/main/java/org/schabi/newpipe/player/datasource/YoutubeHttpDataSource.java b/app/src/main/java/org/schabi/newpipe/player/datasource/YoutubeHttpDataSource.java index ed32450ba..44c0f8458 100644 --- a/app/src/main/java/org/schabi/newpipe/player/datasource/YoutubeHttpDataSource.java +++ b/app/src/main/java/org/schabi/newpipe/player/datasource/YoutubeHttpDataSource.java @@ -46,7 +46,7 @@ import com.google.common.collect.Sets; import com.google.common.net.HttpHeaders; import org.schabi.newpipe.DownloaderImpl; -import org.schabi.newpipe.settings.ProxyManager; +import org.schabi.newpipe.util.ProxyManager; import java.io.IOException; import java.io.InputStream; diff --git a/app/src/main/java/org/schabi/newpipe/player/helper/PlayerDataSource.java b/app/src/main/java/org/schabi/newpipe/player/helper/PlayerDataSource.java index 2bae110ed..dd1b2f028 100644 --- a/app/src/main/java/org/schabi/newpipe/player/helper/PlayerDataSource.java +++ b/app/src/main/java/org/schabi/newpipe/player/helper/PlayerDataSource.java @@ -18,12 +18,10 @@ import com.google.android.exoplayer2.source.smoothstreaming.DefaultSsChunkSource import com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource; import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DefaultDataSource; -import com.google.android.exoplayer2.upstream.DefaultHttpDataSource; import com.google.android.exoplayer2.upstream.TransferListener; import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor; import com.google.android.exoplayer2.upstream.cache.SimpleCache; -import org.schabi.newpipe.DownloaderImpl; import org.schabi.newpipe.extractor.services.youtube.dashmanifestcreators.YoutubeOtfDashManifestCreator; import org.schabi.newpipe.extractor.services.youtube.dashmanifestcreators.YoutubePostLiveStreamDvrDashManifestCreator; import org.schabi.newpipe.extractor.services.youtube.dashmanifestcreators.YoutubeProgressiveDashManifestCreator; @@ -86,12 +84,14 @@ public class PlayerDataSource { // make sure the static cache was created: needed by CacheFactories below instantiateCacheIfNeeded(context); - // generic data source factories use DefaultHttpDataSource.Factory + // generic data source factories now use YoutubeHttpDataSource.Factory to support proxies + final YoutubeHttpDataSource.Factory youtubeHttpDataSourceFactory = + getYoutubeHttpDataSourceFactory(context, false, false); cachelessDataSourceFactory = new DefaultDataSource.Factory(context, - new DefaultHttpDataSource.Factory().setUserAgent(DownloaderImpl.USER_AGENT)) + youtubeHttpDataSourceFactory) .setTransferListener(transferListener); cacheDataSourceFactory = new CacheFactory(context, transferListener, cache, - new DefaultHttpDataSource.Factory().setUserAgent(DownloaderImpl.USER_AGENT)); + youtubeHttpDataSourceFactory); // YouTube-specific data source factories use getYoutubeHttpDataSourceFactory() ytHlsCacheDataSourceFactory = new CacheFactory(context, transferListener, cache, diff --git a/app/src/main/java/org/schabi/newpipe/settings/ProxySettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/ProxySettingsFragment.java index f181909bd..8bf99d6f1 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ProxySettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/ProxySettingsFragment.java @@ -1,26 +1,70 @@ package org.schabi.newpipe.settings; +import android.app.Activity; +import android.app.AlertDialog; +import android.content.SharedPreferences; import android.os.Bundle; - import androidx.annotation.Nullable; -import androidx.preference.ListPreference; - import org.schabi.newpipe.R; +import org.schabi.newpipe.util.NavigationHelper; /** * A fragment that displays proxy settings. */ public class ProxySettingsFragment extends BasePreferenceFragment { + private boolean preferencesChanged = false; + private SharedPreferences.OnSharedPreferenceChangeListener preferenceChangeListener; + @Override public void onCreatePreferences(@Nullable final Bundle savedInstanceState, @Nullable final String rootKey) { - addPreferencesFromResource(R.xml.proxy_settings); + //addPreferencesFromResource(R.xml.proxy_settings); + addPreferencesFromResourceRegistry(); + preferenceChangeListener = (sharedPreferences, key) -> { + preferencesChanged = true; + }; + getPreferenceScreen().getSharedPreferences() + .registerOnSharedPreferenceChangeListener(preferenceChangeListener); + } - final ListPreference proxyTypePreference = findPreference("proxy_type"); - if (proxyTypePreference != null) { - proxyTypePreference.setSummaryProvider( - ListPreference.SimpleSummaryProvider.getInstance()); + @Override + public void onStop() { + super.onStop(); + if (preferencesChanged && getActivity() != null && !getActivity().isFinishing()) { + showRestartDialog(); + } + } + + private void showRestartDialog() { + // Show Alert Dialogue + final Activity activity = getActivity(); + if (activity == null) { + return; + } + final AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); + builder.setMessage(R.string.restart_app_message); + builder.setTitle(R.string.restart_app_title); + builder.setCancelable(true); + builder.setPositiveButton(R.string.ok, (dialogInterface, i) -> { + // Restarts the app + if (activity == null) { + return; + } + NavigationHelper.restartApp(activity); + }); + builder.setNegativeButton(R.string.cancel, (dialogInterface, i) -> { + }); + final android.app.AlertDialog alertDialog = builder.create(); + alertDialog.show(); + } + + @Override + public void onDestroy() { + super.onDestroy(); + if (preferenceChangeListener != null && getPreferenceScreen() != null) { + getPreferenceScreen().getSharedPreferences() + .unregisterOnSharedPreferenceChangeListener(preferenceChangeListener); } } } diff --git a/app/src/main/java/org/schabi/newpipe/settings/SettingsResourceRegistry.java b/app/src/main/java/org/schabi/newpipe/settings/SettingsResourceRegistry.java index 06e0a7c1e..43ccdb2a4 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/SettingsResourceRegistry.java +++ b/app/src/main/java/org/schabi/newpipe/settings/SettingsResourceRegistry.java @@ -42,6 +42,7 @@ public final class SettingsResourceRegistry { add(VideoAudioSettingsFragment.class, R.xml.video_audio_settings); add(ExoPlayerSettingsFragment.class, R.xml.exoplayer_settings); add(BackupRestoreSettingsFragment.class, R.xml.backup_restore_settings); + add(ProxySettingsFragment.class, R.xml.proxy_settings); } private SettingRegistryEntry add( diff --git a/app/src/main/java/org/schabi/newpipe/settings/ProxyManager.java b/app/src/main/java/org/schabi/newpipe/util/ProxyManager.java similarity index 98% rename from app/src/main/java/org/schabi/newpipe/settings/ProxyManager.java rename to app/src/main/java/org/schabi/newpipe/util/ProxyManager.java index 5e22fccd3..cec665ba7 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ProxyManager.java +++ b/app/src/main/java/org/schabi/newpipe/util/ProxyManager.java @@ -1,4 +1,4 @@ -package org.schabi.newpipe.settings; +package org.schabi.newpipe.util; import android.content.Context; import android.content.SharedPreferences; diff --git a/app/src/main/java/org/schabi/newpipe/util/image/PicassoHelper.java b/app/src/main/java/org/schabi/newpipe/util/image/PicassoHelper.java index 77679fa48..ef513064d 100644 --- a/app/src/main/java/org/schabi/newpipe/util/image/PicassoHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/image/PicassoHelper.java @@ -23,7 +23,7 @@ import com.squareup.picasso.Transformation; import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.Image; -import org.schabi.newpipe.settings.ProxyManager; +import org.schabi.newpipe.util.ProxyManager; import java.io.File; import java.io.IOException; diff --git a/app/src/main/java/us/shandian/giga/get/DownloadMission.java b/app/src/main/java/us/shandian/giga/get/DownloadMission.java index 54340ce5d..77cfb0bab 100644 --- a/app/src/main/java/us/shandian/giga/get/DownloadMission.java +++ b/app/src/main/java/us/shandian/giga/get/DownloadMission.java @@ -1,5 +1,6 @@ package us.shandian.giga.get; +import android.content.Context; import android.os.Handler; import android.system.ErrnoException; import android.system.OsConstants; @@ -17,6 +18,7 @@ import java.io.InterruptedIOException; import java.io.Serializable; import java.net.ConnectException; import java.net.HttpURLConnection; +import java.net.Proxy; import java.net.SocketTimeoutException; import java.net.URL; import java.net.UnknownHostException; @@ -25,6 +27,7 @@ import java.util.Objects; import javax.net.ssl.SSLException; +import org.schabi.newpipe.util.ProxyManager; import org.schabi.newpipe.streams.io.StoredFileHelper; import us.shandian.giga.postprocessing.Postprocessing; import us.shandian.giga.service.DownloadManagerService; @@ -34,7 +37,7 @@ import static org.schabi.newpipe.BuildConfig.DEBUG; public class DownloadMission extends Mission { private static final long serialVersionUID = 6L;// last bump: 07 october 2019 - + private final Context context; static final int BUFFER_SIZE = 64 * 1024; static final int BLOCK_SIZE = 512 * 1024; @@ -153,9 +156,10 @@ public class DownloadMission extends Mission { public transient Thread[] threads = new Thread[0]; public transient Thread init = null; - public DownloadMission(String[] urls, StoredFileHelper storage, char kind, Postprocessing psInstance) { + public DownloadMission(final Context context, String[] urls, StoredFileHelper storage, char kind, Postprocessing psInstance) { if (Objects.requireNonNull(urls).length < 1) throw new IllegalArgumentException("urls array is empty"); + this.context = context; this.urls = urls; this.kind = kind; this.offsets = new long[urls.length]; @@ -164,6 +168,7 @@ public class DownloadMission extends Mission { this.storage = storage; this.psAlgorithm = psInstance; + if (DEBUG && psInstance == null && urls.length > 1) { Log.w(TAG, "mission created with multiple urls ¿missing post-processing algorithm?"); } @@ -219,7 +224,14 @@ public class DownloadMission extends Mission { } HttpURLConnection openConnection(String url, boolean headRequest, long rangeStart, long rangeEnd) throws IOException { - HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); + final ProxyManager proxyManager = new ProxyManager(context); + final Proxy proxy = proxyManager.getProxy(); + final HttpURLConnection conn; + if (proxy != null) { + conn = (HttpURLConnection) new URL(url).openConnection(proxy); + } else { + conn = (HttpURLConnection) new URL(url).openConnection(); + } conn.setInstanceFollowRedirects(true); conn.setRequestProperty("User-Agent", DownloaderImpl.USER_AGENT); conn.setRequestProperty("Accept", "*/*"); diff --git a/app/src/main/java/us/shandian/giga/service/DownloadManagerService.java b/app/src/main/java/us/shandian/giga/service/DownloadManagerService.java index 76da18b2d..65c8b623a 100755 --- a/app/src/main/java/us/shandian/giga/service/DownloadManagerService.java +++ b/app/src/main/java/us/shandian/giga/service/DownloadManagerService.java @@ -408,7 +408,7 @@ public class DownloadManagerService extends Service { else ps = Postprocessing.getAlgorithm(psName, psArgs, streamInfo); - final DownloadMission mission = new DownloadMission(urls, storage, kind, ps); + final DownloadMission mission = new DownloadMission(this, urls, storage, kind, ps); mission.threadCount = threads; mission.source = streamInfo.getUrl(); mission.nearLength = nearLength; diff --git a/app/src/main/res/menu/main_menu.xml b/app/src/main/res/menu/main_menu.xml deleted file mode 100644 index 9f4698a60..000000000 --- a/app/src/main/res/menu/main_menu.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index b96667f93..0c160ed07 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -34,6 +34,14 @@ Внешний видеоплеер Внешний аудиоплеер Воспроизведение в фоновом режиме + Настройки прокси + Использовать прокси + Перенаправлять трафик через прокси + Хост прокси + Имя хоста или IP-адрес прокси + Порт прокси + Номер порта прокси + Введите номер порта прокси Тема Тёмная Светлая @@ -868,4 +876,6 @@ Во время воспроизведения получена ошибка HTTP 403 от сервера, вероятно, вызванная блокировкой IP-адреса или проблемами деобфускации URL-адреса потоковой передачи %1$s отказался предоставить данные, запросив логин для подтверждения, что запросчик не бот.\n\nВозможно, ваш IP-адрес временно заблокирован %1$s. Вы можете подождать некоторое время или переключиться на другой IP-адрес (например, включив/выключив VPN или переключившись с Wi-Fi на мобильный интернет). Этот контент недоступен для выбранной страны контента.\n\nИзмените свой выбор в разделе «Настройки > Контент > Страна контента по умолчанию». + Перезапуск приложения + Настройки прокси изменены. Для применения изменений требуется перезапуск приложения. diff --git a/app/src/main/res/values-ru/strings_proxy.xml b/app/src/main/res/values-ru/strings_proxy.xml deleted file mode 100644 index 65e4eb562..000000000 --- a/app/src/main/res/values-ru/strings_proxy.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - Настройки прокси - Использовать прокси - Перенаправлять трафик через прокси - Хост прокси - Имя хоста или IP-адрес прокси - Порт прокси - Номер порта прокси - Введите номер порта прокси - \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1a2a5b6f1..37e14eda7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -159,6 +159,23 @@ Player notification Configure current playing stream notification Backup and restore + Proxy Settings + Use proxy + Redirect traffic through a proxy + Proxy host + Hostname or IP address of the proxy + Proxy port + Port number of the proxy + Enter the proxy port number + Proxy type + + HTTP + SOCKS + + + HTTP + SOCKS + Playing in background Playing in popup mode Content @@ -882,4 +899,6 @@ HTTP error 403 received from server while playing, likely caused by an IP ban or streaming URL deobfuscation issues %1$s refused to provide data, asking for a login to confirm the requester is not a bot.\n\nYour IP might have been temporarily banned by %1$s, you can wait some time or switch to a different IP (for example by turning on/off a VPN, or by switching from WiFi to mobile data). This content is not available for the currently selected content country.\n\nChange your selection from \"Settings > Content > Default content country\". + Restart application + The proxy settings have been changed. A restart of the application is required for the changes to take effect. diff --git a/app/src/main/res/values/strings_proxy.xml b/app/src/main/res/values/strings_proxy.xml deleted file mode 100644 index 117c8cca5..000000000 --- a/app/src/main/res/values/strings_proxy.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - Proxy Settings - Use proxy - Redirect traffic through a proxy - Proxy host - Hostname or IP address of the proxy - Proxy port - Port number of the proxy - Enter the proxy port number - Proxy type - - HTTP - SOCKS - - - HTTP - SOCKS - - \ No newline at end of file