From b9e8ed2ba653a0f23635a1ac80969089140ffbc1 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Tue, 5 Mar 2024 18:11:59 +0400 Subject: [PATCH 01/12] fix(YouTube - Downloads): Use new task context --- .../revanced/integrations/shared/Utils.java | 7 ++- .../youtube/patches/DownloadsPatch.java | 52 +++++++++++++++---- .../youtube/settings/Settings.java | 4 +- .../videoplayer/BottomControlButton.java | 5 ++ .../videoplayer/ExternalDownloadButton.java | 10 ++-- 5 files changed, 63 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/shared/Utils.java b/app/src/main/java/app/revanced/integrations/shared/Utils.java index c2aa2f6bdc..f92c9cfd5f 100644 --- a/app/src/main/java/app/revanced/integrations/shared/Utils.java +++ b/app/src/main/java/app/revanced/integrations/shared/Utils.java @@ -40,7 +40,7 @@ public class Utils { @SuppressLint("StaticFieldLeak") - public static Context context; + private static Context context; private static String versionName; @@ -233,6 +233,11 @@ public static Context getContext() { return context; } + public static void setContext(Context appContext) { + context = appContext; + Logger.printDebug(() -> "Set context: " + appContext); // Cannot log before context is set. + } + public static void setClipboard(@NonNull String text) { android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); android.content.ClipData clip = android.content.ClipData.newPlainText("ReVanced", text); diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java index 7147284d43..40117717d8 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java @@ -1,28 +1,59 @@ package app.revanced.integrations.youtube.patches; +import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; +import android.widget.ImageView; + import app.revanced.integrations.shared.Logger; import app.revanced.integrations.shared.StringRef; import app.revanced.integrations.shared.Utils; import app.revanced.integrations.youtube.settings.Settings; +import app.revanced.integrations.youtube.videoplayer.ExternalDownloadButton; +@SuppressWarnings("unused") public final class DownloadsPatch { + + /** + * Injection point. + */ public static boolean inAppDownloadButtonOnClick() { - if (!Settings.USE_IN_APP_DOWNLOAD_BUTTON.get()) - return false; + try { + if (!Settings.EXTERNAL_DOWNLOADER_ACTION_BUTTON.get()) { + return false; + } - launchExternalDownloader(); - return true; + // Utils context is the application context, and not an activity context. + Context context = Utils.getContext(); + boolean useIntentNewTask = true; + + // Use the overlay button image view (which is part of an intent) if it's available. + // Otherwise fall back on using the application context. + ExternalDownloadButton instance = ExternalDownloadButton.getInstance(); + if (instance != null) { + ImageView view = instance.getButtonImageView(); + if (view != null) { + context = view.getContext(); + useIntentNewTask = false; + } + } + launchExternalDownloader(context, useIntentNewTask); + return true; + } catch (Exception ex) { + Logger.printException(() -> "inAppDownloadButtonOnClick failure", ex); + } + return false; } - public static void launchExternalDownloader() { + /** + * @param useIntentNewTask If the intent should use the new task flag. + * Setting this to true will always minimize YT when the intent is launched, + * even when using NewPipe or any other app that initially opens as an overlay. + * This should be used only if the context is not that of an activity. + */ + public static void launchExternalDownloader(Context context, boolean useIntentNewTask) { Logger.printDebug(() -> "Launching external downloader"); - final var context = Utils.getContext(); - // Trim string to avoid any accidental whitespace. var downloaderPackageName = Settings.EXTERNAL_DOWNLOADER_PACKAGE_NAME.get().trim(); @@ -47,6 +78,9 @@ public static void launchExternalDownloader() { intent.setType("text/plain"); intent.setPackage(downloaderPackageName); intent.putExtra("android.intent.extra.TEXT", content); + if (useIntentNewTask) { + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + } context.startActivity(intent); Logger.printDebug(() -> "Launched the intent with the content: " + content); diff --git a/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java b/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java index 2ed879db41..a12f1e813b 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java +++ b/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java @@ -16,9 +16,9 @@ public class Settings extends BaseSettings { // External downloader public static final BooleanSetting EXTERNAL_DOWNLOADER = new BooleanSetting("revanced_external_downloader", FALSE); + public static final BooleanSetting EXTERNAL_DOWNLOADER_ACTION_BUTTON = new BooleanSetting("revanced_external_downloader_action_button", FALSE); public static final StringSetting EXTERNAL_DOWNLOADER_PACKAGE_NAME = new StringSetting("revanced_external_downloader_name", - "org.schabi.newpipe" /* NewPipe */, parent(EXTERNAL_DOWNLOADER)); - public static final BooleanSetting USE_IN_APP_DOWNLOAD_BUTTON = new BooleanSetting("revanced_use_in_app_download_button", TRUE); + "org.schabi.newpipe" /* NewPipe */, parentsAny(EXTERNAL_DOWNLOADER, EXTERNAL_DOWNLOADER_ACTION_BUTTON)); // Copy video URL public static final BooleanSetting COPY_VIDEO_URL = new BooleanSetting("revanced_copy_video_url", FALSE); diff --git a/app/src/main/java/app/revanced/integrations/youtube/videoplayer/BottomControlButton.java b/app/src/main/java/app/revanced/integrations/youtube/videoplayer/BottomControlButton.java index aacd93677a..3d85200678 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/videoplayer/BottomControlButton.java +++ b/app/src/main/java/app/revanced/integrations/youtube/videoplayer/BottomControlButton.java @@ -62,6 +62,11 @@ public BottomControlButton(@NonNull ViewGroup bottomControlsViewGroup, @NonNull buttonRef = new WeakReference<>(imageView); } + @Nullable + public ImageView getButtonImageView() { + return buttonRef.get(); + } + public void setVisibility(boolean visible) { if (isVisible == visible) return; isVisible = visible; diff --git a/app/src/main/java/app/revanced/integrations/youtube/videoplayer/ExternalDownloadButton.java b/app/src/main/java/app/revanced/integrations/youtube/videoplayer/ExternalDownloadButton.java index a02c202330..dd19f34dd6 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/videoplayer/ExternalDownloadButton.java +++ b/app/src/main/java/app/revanced/integrations/youtube/videoplayer/ExternalDownloadButton.java @@ -5,10 +5,9 @@ import androidx.annotation.Nullable; +import app.revanced.integrations.shared.Logger; import app.revanced.integrations.youtube.patches.DownloadsPatch; -import app.revanced.integrations.youtube.patches.VideoInformation; import app.revanced.integrations.youtube.settings.Settings; -import app.revanced.integrations.shared.Logger; @SuppressWarnings("unused") public class ExternalDownloadButton extends BottomControlButton { @@ -36,6 +35,11 @@ public static void initializeButton(View view) { } } + @Nullable + public static ExternalDownloadButton getInstance() { + return instance; + } + /** * Injection point. */ @@ -44,7 +48,7 @@ public static void changeVisibility(boolean showing) { } private static void onDownloadClick(View view) { - DownloadsPatch.launchExternalDownloader(); + DownloadsPatch.launchExternalDownloader(view.getContext(), false); } } From aab96fa32527fd7a8b673bbd5868b79d74d4fbb7 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Tue, 5 Mar 2024 18:36:09 +0400 Subject: [PATCH 02/12] refactor --- .../youtube/patches/DownloadsPatch.java | 16 +++++++--------- .../videoplayer/ExternalDownloadButton.java | 2 +- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java index 40117717d8..68d3277722 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java @@ -25,7 +25,7 @@ public static boolean inAppDownloadButtonOnClick() { // Utils context is the application context, and not an activity context. Context context = Utils.getContext(); - boolean useIntentNewTask = true; + boolean isActivityContext = false; // Use the overlay button image view (which is part of an intent) if it's available. // Otherwise fall back on using the application context. @@ -34,10 +34,10 @@ public static boolean inAppDownloadButtonOnClick() { ImageView view = instance.getButtonImageView(); if (view != null) { context = view.getContext(); - useIntentNewTask = false; + isActivityContext = true; } } - launchExternalDownloader(context, useIntentNewTask); + launchExternalDownloader(context, isActivityContext); return true; } catch (Exception ex) { Logger.printException(() -> "inAppDownloadButtonOnClick failure", ex); @@ -46,12 +46,10 @@ public static boolean inAppDownloadButtonOnClick() { } /** - * @param useIntentNewTask If the intent should use the new task flag. - * Setting this to true will always minimize YT when the intent is launched, - * even when using NewPipe or any other app that initially opens as an overlay. - * This should be used only if the context is not that of an activity. + * @param isActivityContext If the context parameter is for an Activity. If this is false, then + * the downloader is opened as a new task (which forces YT to minimize). */ - public static void launchExternalDownloader(Context context, boolean useIntentNewTask) { + public static void launchExternalDownloader(Context context, boolean isActivityContext) { Logger.printDebug(() -> "Launching external downloader"); // Trim string to avoid any accidental whitespace. @@ -78,7 +76,7 @@ public static void launchExternalDownloader(Context context, boolean useIntentNe intent.setType("text/plain"); intent.setPackage(downloaderPackageName); intent.putExtra("android.intent.extra.TEXT", content); - if (useIntentNewTask) { + if (isActivityContext) { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } context.startActivity(intent); diff --git a/app/src/main/java/app/revanced/integrations/youtube/videoplayer/ExternalDownloadButton.java b/app/src/main/java/app/revanced/integrations/youtube/videoplayer/ExternalDownloadButton.java index dd19f34dd6..f512a705fd 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/videoplayer/ExternalDownloadButton.java +++ b/app/src/main/java/app/revanced/integrations/youtube/videoplayer/ExternalDownloadButton.java @@ -48,7 +48,7 @@ public static void changeVisibility(boolean showing) { } private static void onDownloadClick(View view) { - DownloadsPatch.launchExternalDownloader(view.getContext(), false); + DownloadsPatch.launchExternalDownloader(view.getContext(), true); } } From cbe7a663c0720730bb028db901379a2233a37fa0 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Tue, 5 Mar 2024 19:44:55 +0400 Subject: [PATCH 03/12] fix: use activity context if download overlay button is turned off --- .../integrations/youtube/patches/DownloadsPatch.java | 8 ++++---- .../youtube/videoplayer/BottomControlButton.java | 7 +++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java index 68d3277722..84b8305caa 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java @@ -27,13 +27,13 @@ public static boolean inAppDownloadButtonOnClick() { Context context = Utils.getContext(); boolean isActivityContext = false; - // Use the overlay button image view (which is part of an intent) if it's available. + // If possible, use the context of the overlay button group (which is part of an activity). // Otherwise fall back on using the application context. ExternalDownloadButton instance = ExternalDownloadButton.getInstance(); if (instance != null) { - ImageView view = instance.getButtonImageView(); - if (view != null) { - context = view.getContext(); + Context activityContext = instance.getActivityContext(); + if (activityContext != null) { + context = activityContext; isActivityContext = true; } } diff --git a/app/src/main/java/app/revanced/integrations/youtube/videoplayer/BottomControlButton.java b/app/src/main/java/app/revanced/integrations/youtube/videoplayer/BottomControlButton.java index 3d85200678..8366c8b425 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/videoplayer/BottomControlButton.java +++ b/app/src/main/java/app/revanced/integrations/youtube/videoplayer/BottomControlButton.java @@ -1,5 +1,6 @@ package app.revanced.integrations.youtube.videoplayer; +import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; @@ -20,6 +21,7 @@ public abstract class BottomControlButton { private static final Animation fadeOut; private final WeakReference buttonRef; + private final WeakReference activityContextRef; private final BooleanSetting setting; protected boolean isVisible; @@ -60,11 +62,12 @@ public BottomControlButton(@NonNull ViewGroup bottomControlsViewGroup, @NonNull imageView.setVisibility(View.GONE); buttonRef = new WeakReference<>(imageView); + activityContextRef = new WeakReference<>(imageView.getContext()); } @Nullable - public ImageView getButtonImageView() { - return buttonRef.get(); + public Context getActivityContext() { + return activityContextRef.get(); } public void setVisibility(boolean visible) { From db4ecffb232d21ca2ec8da8823421923c575bf16 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Wed, 6 Mar 2024 12:23:58 +0400 Subject: [PATCH 04/12] fix: Use main activity for intent context --- .../youtube/patches/DownloadsPatch.java | 47 +++++++++++-------- .../patches/PlayerOverlaysHookPatch.java | 16 +------ 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java index 84b8305caa..40045b7472 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java @@ -1,19 +1,31 @@ package app.revanced.integrations.youtube.patches; +import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; -import android.widget.ImageView; + +import androidx.annotation.NonNull; + +import java.lang.ref.WeakReference; import app.revanced.integrations.shared.Logger; import app.revanced.integrations.shared.StringRef; import app.revanced.integrations.shared.Utils; import app.revanced.integrations.youtube.settings.Settings; -import app.revanced.integrations.youtube.videoplayer.ExternalDownloadButton; @SuppressWarnings("unused") public final class DownloadsPatch { + private static WeakReference activityRef = new WeakReference<>(null); + + /** + * Injection point. + */ + public static void activityCreated(Activity mainActivity) { + activityRef = new WeakReference<>(mainActivity); + } + /** * Injection point. */ @@ -23,20 +35,16 @@ public static boolean inAppDownloadButtonOnClick() { return false; } - // Utils context is the application context, and not an activity context. - Context context = Utils.getContext(); - boolean isActivityContext = false; - - // If possible, use the context of the overlay button group (which is part of an activity). + // If possible, use the main activity as the context. // Otherwise fall back on using the application context. - ExternalDownloadButton instance = ExternalDownloadButton.getInstance(); - if (instance != null) { - Context activityContext = instance.getActivityContext(); - if (activityContext != null) { - context = activityContext; - isActivityContext = true; - } + Context context = activityRef.get(); + boolean isActivityContext = true; + if (context == null) { + // Utils context is the application context, and not an activity context. + context = Utils.getContext(); + isActivityContext = false; } + launchExternalDownloader(context, isActivityContext); return true; } catch (Exception ex) { @@ -49,8 +57,8 @@ public static boolean inAppDownloadButtonOnClick() { * @param isActivityContext If the context parameter is for an Activity. If this is false, then * the downloader is opened as a new task (which forces YT to minimize). */ - public static void launchExternalDownloader(Context context, boolean isActivityContext) { - Logger.printDebug(() -> "Launching external downloader"); + public static void launchExternalDownloader(@NonNull Context context, boolean isActivityContext) { + Logger.printDebug(() -> "Launching external downloader with context: " + context); // Trim string to avoid any accidental whitespace. var downloaderPackageName = Settings.EXTERNAL_DOWNLOADER_PACKAGE_NAME.get().trim(); @@ -76,14 +84,13 @@ public static void launchExternalDownloader(Context context, boolean isActivityC intent.setType("text/plain"); intent.setPackage(downloaderPackageName); intent.putExtra("android.intent.extra.TEXT", content); - if (isActivityContext) { + if (!isActivityContext) { + Logger.printDebug(() -> "Using new task intent"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } context.startActivity(intent); - - Logger.printDebug(() -> "Launched the intent with the content: " + content); } catch (Exception error) { - Logger.printException(() -> "Failed to launch the intent: " + error, error); + Logger.printException(() -> "Failed to launch intent: " + error, error); } } } diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/PlayerOverlaysHookPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/PlayerOverlaysHookPatch.java index 7070d8a564..32732cdcc3 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/PlayerOverlaysHookPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/PlayerOverlaysHookPatch.java @@ -6,24 +6,12 @@ import app.revanced.integrations.youtube.shared.PlayerOverlays; -/** - * Hook receiver class for 'player-overlays-hook' patch - * - * @usedBy app.revanced.patches.youtube.misc.playeroverlay.patch.PlayerOverlaysHookPatch - * @smali Lapp/revanced/integrations/patches/PlayerOverlaysHookPatch; - */ @SuppressWarnings("unused") public class PlayerOverlaysHookPatch { /** * Injection point. - * - * @param thisRef reference to the view - * @smali YouTubePlayerOverlaysLayout_onFinishInflateHook(Ljava / lang / Object ;)V */ - public static void YouTubePlayerOverlaysLayout_onFinishInflateHook(@Nullable Object thisRef) { - if (thisRef == null) return; - if (thisRef instanceof ViewGroup) { - PlayerOverlays.attach((ViewGroup) thisRef); - } + public static void playerOverlayInflated(ViewGroup group) { + PlayerOverlays.attach(group); } } \ No newline at end of file From 79e8a9519af4b8ab88214714cb3d208f55b0295e Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Wed, 6 Mar 2024 12:27:32 +0400 Subject: [PATCH 05/12] fix: Hide action button preference if spoofing to a version the hook does not work --- .../preference/ReVancedPreferenceFragment.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java b/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java index 5542d61a60..83aad6019c 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java +++ b/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java @@ -1,9 +1,14 @@ package app.revanced.integrations.youtube.settings.preference; +import android.os.Build; import android.preference.ListPreference; import android.preference.Preference; + +import androidx.annotation.RequiresApi; + import app.revanced.integrations.shared.settings.preference.AbstractPreferenceFragment; import app.revanced.integrations.youtube.patches.playback.speed.CustomPlaybackSpeedPatch; +import app.revanced.integrations.youtube.patches.spoof.SpoofAppVersionPatch; import app.revanced.integrations.youtube.settings.Settings; /** @@ -12,6 +17,8 @@ * @noinspection deprecation */ public class ReVancedPreferenceFragment extends AbstractPreferenceFragment { + + @RequiresApi(api = Build.VERSION_CODES.O) @Override protected void initialize() { super.initialize(); @@ -21,5 +28,12 @@ protected void initialize() { if (defaultSpeedPreference instanceof ListPreference) { CustomPlaybackSpeedPatch.initializeListPreference((ListPreference) defaultSpeedPreference); } + + // Action button hook does not work on older versions. + // Remove the preference to make things simpler. + Preference downloadActionButton = findPreference(Settings.EXTERNAL_DOWNLOADER_ACTION_BUTTON.key); + if (downloadActionButton != null && SpoofAppVersionPatch.isSpoofingToEqualOrLessThan("18.23.36")) { + downloadActionButton.getParent().removePreference(downloadActionButton); + } } } From 50b49c2e1237b3e5ef2420ef1f63ae4cb652e52b Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Wed, 6 Mar 2024 15:15:18 +0400 Subject: [PATCH 06/12] refactor: use less than instead of lessThanOrEquals --- .../youtube/patches/DownloadsPatch.java | 5 +++++ .../patches/HideBreakingNewsPatch.java | 2 +- .../patches/ReturnYouTubeDislikePatch.java | 2 +- .../patches/spoof/SpoofAppVersionPatch.java | 22 ++++--------------- .../ReturnYouTubeDislike.java | 2 +- .../youtube/settings/Settings.java | 10 +++++++++ .../ReVancedPreferenceFragment.java | 14 ++++++++---- 7 files changed, 32 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java index 40045b7472..6e30ee059e 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java @@ -12,6 +12,7 @@ import app.revanced.integrations.shared.Logger; import app.revanced.integrations.shared.StringRef; import app.revanced.integrations.shared.Utils; +import app.revanced.integrations.youtube.patches.spoof.SpoofAppVersionPatch; import app.revanced.integrations.youtube.settings.Settings; @SuppressWarnings("unused") @@ -26,6 +27,10 @@ public static void activityCreated(Activity mainActivity) { activityRef = new WeakReference<>(mainActivity); } + public static boolean shouldHideActionButtonOverridePreference() { + return SpoofAppVersionPatch.isSpoofingToLessThan("18.24.00"); + } + /** * Injection point. */ diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/HideBreakingNewsPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/HideBreakingNewsPatch.java index fe9e57c0b0..bd6d89fa57 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/HideBreakingNewsPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/HideBreakingNewsPatch.java @@ -16,7 +16,7 @@ public class HideBreakingNewsPatch { * Breaking news does not appear to be present in these older versions anyways. */ private static final boolean isSpoofingOldVersionWithHorizontalCardListWatchHistory = - SpoofAppVersionPatch.isSpoofingToEqualOrLessThan("17.31.00"); + SpoofAppVersionPatch.isSpoofingToLessThan("18.01.00"); /** * Injection point. diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/ReturnYouTubeDislikePatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/ReturnYouTubeDislikePatch.java index 0cb687f638..4938fb3765 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/ReturnYouTubeDislikePatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/ReturnYouTubeDislikePatch.java @@ -46,7 +46,7 @@ public class ReturnYouTubeDislikePatch { public static final boolean IS_SPOOFING_TO_NON_LITHO_SHORTS_PLAYER = - SpoofAppVersionPatch.isSpoofingToEqualOrLessThan("18.33.40"); + SpoofAppVersionPatch.isSpoofingToLessThan("18.34.00"); /** * RYD data for the current video on screen. diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/spoof/SpoofAppVersionPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/spoof/SpoofAppVersionPatch.java index 93219797da..3f9f850b04 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/spoof/SpoofAppVersionPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/spoof/SpoofAppVersionPatch.java @@ -1,26 +1,12 @@ package app.revanced.integrations.youtube.patches.spoof; -import app.revanced.integrations.shared.Logger; import app.revanced.integrations.youtube.settings.Settings; @SuppressWarnings("unused") public class SpoofAppVersionPatch { - private static final boolean SPOOF_APP_VERSION_ENABLED; - private static final String SPOOF_APP_VERSION_TARGET; - - static { - // TODO: remove this migration code - // Spoof targets below 17.33 that no longer reliably work. - if (Settings.SPOOF_APP_VERSION_TARGET.get().compareTo("17.33.01") < 0) { - Logger.printInfo(() -> "Resetting spoof app version target"); - Settings.SPOOF_APP_VERSION_TARGET.resetToDefault(); - } - // End migration - - SPOOF_APP_VERSION_ENABLED = Settings.SPOOF_APP_VERSION.get(); - SPOOF_APP_VERSION_TARGET = Settings.SPOOF_APP_VERSION_TARGET.get(); - } + private static final boolean SPOOF_APP_VERSION_ENABLED = Settings.SPOOF_APP_VERSION.get(); + private static final String SPOOF_APP_VERSION_TARGET = Settings.SPOOF_APP_VERSION_TARGET.get(); /** * Injection point @@ -30,8 +16,8 @@ public static String getYouTubeVersionOverride(String version) { return version; } - public static boolean isSpoofingToEqualOrLessThan(String version) { - return SPOOF_APP_VERSION_ENABLED && SPOOF_APP_VERSION_TARGET.compareTo(version) <= 0; + public static boolean isSpoofingToLessThan(String version) { + return SPOOF_APP_VERSION_ENABLED && SPOOF_APP_VERSION_TARGET.compareTo(version) < 0; } } diff --git a/app/src/main/java/app/revanced/integrations/youtube/returnyoutubedislike/ReturnYouTubeDislike.java b/app/src/main/java/app/revanced/integrations/youtube/returnyoutubedislike/ReturnYouTubeDislike.java index 7e7fe68af4..bfff1b155b 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/returnyoutubedislike/ReturnYouTubeDislike.java +++ b/app/src/main/java/app/revanced/integrations/youtube/returnyoutubedislike/ReturnYouTubeDislike.java @@ -91,7 +91,7 @@ public enum Vote { private static final char MIDDLE_SEPARATOR_CHARACTER = '◎'; // 'bullseye' private static final boolean IS_SPOOFING_TO_OLD_SEPARATOR_COLOR - = SpoofAppVersionPatch.isSpoofingToEqualOrLessThan("18.09.39"); + = SpoofAppVersionPatch.isSpoofingToLessThan("18.10.00"); /** * Cached lookup of all video ids. diff --git a/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java b/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java index a12f1e813b..9289b3b1b9 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java +++ b/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java @@ -1,7 +1,9 @@ package app.revanced.integrations.youtube.settings; +import app.revanced.integrations.shared.Logger; import app.revanced.integrations.shared.settings.*; import app.revanced.integrations.shared.settings.preference.SharedPrefCategory; +import app.revanced.integrations.youtube.patches.spoof.SpoofAppVersionPatch; import app.revanced.integrations.youtube.sponsorblock.SponsorBlockSettings; import java.util.Arrays; @@ -335,6 +337,14 @@ public class Settings extends BaseSettings { // and more time should be given for users who rarely upgrade. migrateOldSettingToNew(DEPRECATED_SB_UUID_OLD_MIGRATION_SETTING, SB_PRIVATE_USER_ID); + + // Old spoof versions that no longer work reliably + if (SpoofAppVersionPatch.isSpoofingToLessThan("17.33.00")) { + Logger.printInfo(() -> "Resetting spoof app version target"); + Settings.SPOOF_APP_VERSION_TARGET.resetToDefault(); + } + + // endregion } } diff --git a/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java b/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java index 83aad6019c..38324f3adf 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java +++ b/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java @@ -3,12 +3,13 @@ import android.os.Build; import android.preference.ListPreference; import android.preference.Preference; +import android.preference.PreferenceGroup; import androidx.annotation.RequiresApi; import app.revanced.integrations.shared.settings.preference.AbstractPreferenceFragment; +import app.revanced.integrations.youtube.patches.DownloadsPatch; import app.revanced.integrations.youtube.patches.playback.speed.CustomPlaybackSpeedPatch; -import app.revanced.integrations.youtube.patches.spoof.SpoofAppVersionPatch; import app.revanced.integrations.youtube.settings.Settings; /** @@ -31,9 +32,14 @@ protected void initialize() { // Action button hook does not work on older versions. // Remove the preference to make things simpler. - Preference downloadActionButton = findPreference(Settings.EXTERNAL_DOWNLOADER_ACTION_BUTTON.key); - if (downloadActionButton != null && SpoofAppVersionPatch.isSpoofingToEqualOrLessThan("18.23.36")) { - downloadActionButton.getParent().removePreference(downloadActionButton); + if (DownloadsPatch.shouldHideActionButtonOverridePreference()) { + Preference downloadActionButton = findPreference(Settings.EXTERNAL_DOWNLOADER_ACTION_BUTTON.key); + if (downloadActionButton != null) { + PreferenceGroup group = downloadActionButton.getParent(); + if (group != null) { + downloadActionButton.getParent().removePreference(downloadActionButton); + } + } } } } From 16f9489f5cbc600b862f9280fb8f9de15d61997a Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Wed, 6 Mar 2024 15:16:17 +0400 Subject: [PATCH 07/12] fix: use try/catch to prevent taking down the app if something goes wrong --- .../ReVancedPreferenceFragment.java | 31 +++++++++++-------- .../videoplayer/BottomControlButton.java | 8 ----- .../videoplayer/ExternalDownloadButton.java | 5 --- 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java b/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java index 38324f3adf..7a7d857e88 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java +++ b/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java @@ -7,6 +7,7 @@ import androidx.annotation.RequiresApi; +import app.revanced.integrations.shared.Logger; import app.revanced.integrations.shared.settings.preference.AbstractPreferenceFragment; import app.revanced.integrations.youtube.patches.DownloadsPatch; import app.revanced.integrations.youtube.patches.playback.speed.CustomPlaybackSpeedPatch; @@ -24,22 +25,26 @@ public class ReVancedPreferenceFragment extends AbstractPreferenceFragment { protected void initialize() { super.initialize(); - // If the preference was included, then initialize it based on the available playback speed - Preference defaultSpeedPreference = findPreference(Settings.PLAYBACK_SPEED_DEFAULT.key); - if (defaultSpeedPreference instanceof ListPreference) { - CustomPlaybackSpeedPatch.initializeListPreference((ListPreference) defaultSpeedPreference); - } + try { + // If the preference was included, then initialize it based on the available playback speed + Preference defaultSpeedPreference = findPreference(Settings.PLAYBACK_SPEED_DEFAULT.key); + if (defaultSpeedPreference instanceof ListPreference) { + CustomPlaybackSpeedPatch.initializeListPreference((ListPreference) defaultSpeedPreference); + } - // Action button hook does not work on older versions. - // Remove the preference to make things simpler. - if (DownloadsPatch.shouldHideActionButtonOverridePreference()) { - Preference downloadActionButton = findPreference(Settings.EXTERNAL_DOWNLOADER_ACTION_BUTTON.key); - if (downloadActionButton != null) { - PreferenceGroup group = downloadActionButton.getParent(); - if (group != null) { - downloadActionButton.getParent().removePreference(downloadActionButton); + // Action button hook does not work on older versions. + // Remove the preference to make things simpler. + if (DownloadsPatch.shouldHideActionButtonOverridePreference()) { + Preference downloadActionButton = findPreference(Settings.EXTERNAL_DOWNLOADER_ACTION_BUTTON.key); + if (downloadActionButton != null) { + PreferenceGroup group = downloadActionButton.getParent(); + if (group != null) { + downloadActionButton.getParent().removePreference(downloadActionButton); + } } } + } catch (Exception ex) { + Logger.printException(() -> "initialize failure", ex); } } } diff --git a/app/src/main/java/app/revanced/integrations/youtube/videoplayer/BottomControlButton.java b/app/src/main/java/app/revanced/integrations/youtube/videoplayer/BottomControlButton.java index 8366c8b425..aacd93677a 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/videoplayer/BottomControlButton.java +++ b/app/src/main/java/app/revanced/integrations/youtube/videoplayer/BottomControlButton.java @@ -1,6 +1,5 @@ package app.revanced.integrations.youtube.videoplayer; -import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; @@ -21,7 +20,6 @@ public abstract class BottomControlButton { private static final Animation fadeOut; private final WeakReference buttonRef; - private final WeakReference activityContextRef; private final BooleanSetting setting; protected boolean isVisible; @@ -62,12 +60,6 @@ public BottomControlButton(@NonNull ViewGroup bottomControlsViewGroup, @NonNull imageView.setVisibility(View.GONE); buttonRef = new WeakReference<>(imageView); - activityContextRef = new WeakReference<>(imageView.getContext()); - } - - @Nullable - public Context getActivityContext() { - return activityContextRef.get(); } public void setVisibility(boolean visible) { diff --git a/app/src/main/java/app/revanced/integrations/youtube/videoplayer/ExternalDownloadButton.java b/app/src/main/java/app/revanced/integrations/youtube/videoplayer/ExternalDownloadButton.java index f512a705fd..378cde3e1c 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/videoplayer/ExternalDownloadButton.java +++ b/app/src/main/java/app/revanced/integrations/youtube/videoplayer/ExternalDownloadButton.java @@ -35,11 +35,6 @@ public static void initializeButton(View view) { } } - @Nullable - public static ExternalDownloadButton getInstance() { - return instance; - } - /** * Injection point. */ From 053c5441bd4afcfa339962ef86ec3c930258fa33 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Wed, 6 Mar 2024 18:17:38 +0400 Subject: [PATCH 08/12] fix: Support spoofed old versions --- .../integrations/youtube/patches/DownloadsPatch.java | 4 ---- .../preference/ReVancedPreferenceFragment.java | 12 ------------ 2 files changed, 16 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java index 6e30ee059e..0e61cd98ef 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java @@ -27,10 +27,6 @@ public static void activityCreated(Activity mainActivity) { activityRef = new WeakReference<>(mainActivity); } - public static boolean shouldHideActionButtonOverridePreference() { - return SpoofAppVersionPatch.isSpoofingToLessThan("18.24.00"); - } - /** * Injection point. */ diff --git a/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java b/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java index 7a7d857e88..6b54b17663 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java +++ b/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java @@ -31,18 +31,6 @@ protected void initialize() { if (defaultSpeedPreference instanceof ListPreference) { CustomPlaybackSpeedPatch.initializeListPreference((ListPreference) defaultSpeedPreference); } - - // Action button hook does not work on older versions. - // Remove the preference to make things simpler. - if (DownloadsPatch.shouldHideActionButtonOverridePreference()) { - Preference downloadActionButton = findPreference(Settings.EXTERNAL_DOWNLOADER_ACTION_BUTTON.key); - if (downloadActionButton != null) { - PreferenceGroup group = downloadActionButton.getParent(); - if (group != null) { - downloadActionButton.getParent().removePreference(downloadActionButton); - } - } - } } catch (Exception ex) { Logger.printException(() -> "initialize failure", ex); } From 34fc6cefe7cc82f9d098c2e13e9b5730ceb069be Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Wed, 6 Mar 2024 21:12:34 +0400 Subject: [PATCH 09/12] fix: Fix downloader opening when offline downloading a playlist --- .../youtube/patches/DownloadsPatch.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java index 0e61cd98ef..9ec24344a4 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java @@ -6,6 +6,7 @@ import android.content.pm.PackageManager; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import java.lang.ref.WeakReference; @@ -27,6 +28,21 @@ public static void activityCreated(Activity mainActivity) { activityRef = new WeakReference<>(mainActivity); } + /** + * Injection point. + * + * Call if download playlist is pressed, or if download button is used + * for old spoofed version (both playlists and the player action button). + */ + public static boolean inAppDownloadPlaylistLegacyOnClick(@Nullable String videoId) { + if (videoId == null || videoId.isEmpty()) { + // videoId is null or empty if download playlist is pressed. + Logger.printDebug(() -> "Ignoring playlist download button press"); + return false; + } + return inAppDownloadButtonOnClick(); + } + /** * Injection point. */ From 949bcc544e59d9018924d6372c5d4b85dd5b67d7 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Wed, 6 Mar 2024 23:30:00 +0100 Subject: [PATCH 10/12] Apply suggestions from code review --- .../app/revanced/integrations/youtube/settings/Settings.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java b/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java index 9289b3b1b9..6122e8af57 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java +++ b/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java @@ -338,7 +338,7 @@ public class Settings extends BaseSettings { migrateOldSettingToNew(DEPRECATED_SB_UUID_OLD_MIGRATION_SETTING, SB_PRIVATE_USER_ID); - // Old spoof versions that no longer work reliably + // Old spoof versions that no longer work reliably. if (SpoofAppVersionPatch.isSpoofingToLessThan("17.33.00")) { Logger.printInfo(() -> "Resetting spoof app version target"); Settings.SPOOF_APP_VERSION_TARGET.resetToDefault(); From ea9099ee4e0c31b4f1c7bdf91bf77612339a06e1 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Wed, 6 Mar 2024 23:30:16 +0100 Subject: [PATCH 11/12] Apply suggestions from code review --- .../youtube/settings/preference/ReVancedPreferenceFragment.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java b/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java index 6b54b17663..1f3bd6c012 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java +++ b/app/src/main/java/app/revanced/integrations/youtube/settings/preference/ReVancedPreferenceFragment.java @@ -26,7 +26,7 @@ protected void initialize() { super.initialize(); try { - // If the preference was included, then initialize it based on the available playback speed + // If the preference was included, then initialize it based on the available playback speed. Preference defaultSpeedPreference = findPreference(Settings.PLAYBACK_SPEED_DEFAULT.key); if (defaultSpeedPreference instanceof ListPreference) { CustomPlaybackSpeedPatch.initializeListPreference((ListPreference) defaultSpeedPreference); From 07f67b468637dba715cff983ff77215023d31a77 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Thu, 7 Mar 2024 12:22:54 +0400 Subject: [PATCH 12/12] comments --- .../revanced/integrations/youtube/patches/DownloadsPatch.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java index 9ec24344a4..9904ff7505 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/DownloadsPatch.java @@ -33,6 +33,9 @@ public static void activityCreated(Activity mainActivity) { * * Call if download playlist is pressed, or if download button is used * for old spoofed version (both playlists and the player action button). + * + * Downloading playlists is not supported yet, + * as the hooked code does not easily expose the playlist id. */ public static boolean inAppDownloadPlaylistLegacyOnClick(@Nullable String videoId) { if (videoId == null || videoId.isEmpty()) {