Skip to content

Commit 19c2742

Browse files
feat(YouTube - Hide Shorts components): Add option to hide Shorts in watch history (ReVanced#4214)
1 parent 892f86c commit 19c2742

File tree

5 files changed

+54
-37
lines changed

5 files changed

+54
-37
lines changed

extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/components/ShortsFilter.java

+27-19
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ boolean isFiltered(@Nullable String identifier, String path, byte[] protobufBuff
297297
if (matchedGroup == suggestedAction) {
298298
// Skip searching the buffer if all suggested actions are set to hidden.
299299
// This has a secondary effect of hiding all new un-identified actions
300-
// under the assumption that the user wants all actions hidden.
300+
// under the assumption that the user wants all suggestions hidden.
301301
if (isEverySuggestedActionFilterEnabled()) {
302302
return super.isFiltered(path, identifier, protobufBufferArray, matchedGroup, contentType, contentIndex);
303303
}
@@ -324,19 +324,22 @@ boolean isFiltered(@Nullable String identifier, String path, byte[] protobufBuff
324324
}
325325

326326
private static boolean shouldHideShortsFeedItems() {
327+
// Known issue if hide home is on but at least one other hide is off:
328+
//
329+
// Shorts suggestions will load in the background if a video is opened and
330+
// immediately minimized before any suggestions are loaded.
331+
// In this state the player type will show minimized, which cannot
332+
// distinguish between Shorts suggestions loading in the player and between
333+
// scrolling thru search/home/subscription tabs while a player is minimized.
327334
final boolean hideHome = Settings.HIDE_SHORTS_HOME.get();
328335
final boolean hideSubscriptions = Settings.HIDE_SHORTS_SUBSCRIPTIONS.get();
329336
final boolean hideSearch = Settings.HIDE_SHORTS_SEARCH.get();
337+
final boolean hideHistory = Settings.HIDE_SHORTS_HISTORY.get();
330338

331-
if (hideHome && hideSubscriptions && hideSearch) {
332-
// Shorts suggestions can load in the background if a video is opened and
333-
// then immediately minimized before any suggestions are loaded.
334-
// In this state the player type will show minimized, which makes it not possible to
335-
// distinguish between Shorts suggestions loading in the player and between
336-
// scrolling thru search/home/subscription tabs while a player is minimized.
337-
//
338-
// To avoid this situation for users that never want to show Shorts (all hide Shorts options are enabled)
339-
// then hide all Shorts everywhere including the Library history and Library playlists.
339+
if (!hideHome && !hideSubscriptions && !hideSearch && !hideHistory) {
340+
return false;
341+
}
342+
if (hideHome && hideSubscriptions && hideSearch && hideHistory) {
340343
return true;
341344
}
342345

@@ -352,24 +355,29 @@ private static boolean shouldHideShortsFeedItems() {
352355
}
353356

354357
// Avoid checking navigation button status if all other Shorts should show.
355-
if (!hideHome && !hideSubscriptions) {
358+
if (!hideHome && !hideSubscriptions && !hideHistory) {
356359
return false;
357360
}
358361

362+
// Check navigation absolutely last since the check may block this thread.
359363
NavigationButton selectedNavButton = NavigationButton.getSelectedNavigationButton();
360364
if (selectedNavButton == null) {
361365
return hideHome; // Unknown tab, treat the same as home.
362366
}
363-
if (selectedNavButton == NavigationButton.HOME) {
364-
return hideHome;
365-
}
366-
if (selectedNavButton == NavigationButton.SUBSCRIPTIONS) {
367-
return hideSubscriptions;
368-
}
369-
// User must be in the library tab. Don't hide the history or any playlists here.
370-
return false;
367+
368+
return switch (selectedNavButton) {
369+
case HOME -> hideHome;
370+
case SUBSCRIPTIONS -> hideSubscriptions;
371+
case LIBRARY -> hideHistory;
372+
default -> false;
373+
};
371374
}
372375

376+
/**
377+
* Injection point. Only used if patching older than 19.03.
378+
* This hook may be obsolete even for old versions
379+
* as they now use a litho layout like newer versions.
380+
*/
373381
public static void hideShortsShelf(final View shortsShelfView) {
374382
if (shouldHideShortsFeedItems()) {
375383
Utils.hideViewByLayoutParams(shortsShelfView);

extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java

+1
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ public class Settings extends BaseSettings {
233233
public static final BooleanSetting HIDE_SHORTS_FULL_VIDEO_LINK_LABEL = new BooleanSetting("revanced_hide_shorts_full_video_link_label", FALSE);
234234
public static final BooleanSetting HIDE_SHORTS_GREEN_SCREEN_BUTTON = new BooleanSetting("revanced_hide_shorts_green_screen_button", TRUE);
235235
public static final BooleanSetting HIDE_SHORTS_HASHTAG_BUTTON = new BooleanSetting("revanced_hide_shorts_hashtag_button", TRUE);
236+
public static final BooleanSetting HIDE_SHORTS_HISTORY = new BooleanSetting("revanced_hide_shorts_history", FALSE);
236237
public static final BooleanSetting HIDE_SHORTS_HOME = new BooleanSetting("revanced_hide_shorts_home", FALSE);
237238
public static final BooleanSetting HIDE_SHORTS_INFO_PANEL = new BooleanSetting("revanced_hide_shorts_info_panel", TRUE);
238239
public static final BooleanSetting HIDE_SHORTS_JOIN_BUTTON = new BooleanSetting("revanced_hide_shorts_join_button", TRUE);

extensions/youtube/src/main/java/app/revanced/extension/youtube/shared/NavigationBar.java

+16-12
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,21 @@ public static boolean isSearchBarActive() {
5454
* How long to wait for the set nav button latch to be released. Maximum wait time must
5555
* be as small as possible while still allowing enough time for the nav bar to update.
5656
*
57-
* YT calls it's back button handlers out of order,
58-
* and litho starts filtering before the navigation bar is updated.
57+
* YT calls it's back button handlers out of order, and litho starts filtering before the
58+
* navigation bar is updated. Fixing this situation and not needlessly waiting requires
59+
* somehow detecting if a back button key/gesture will not change the active tab.
5960
*
60-
* Fixing this situation and not needlessly waiting requires somehow
61-
* detecting if a back button key-press will cause a tab change.
61+
* On average the time between pressing the back button and the first litho event is
62+
* about 10-20ms. Waiting up to 75-150ms should be enough time to handle normal use cases
63+
* and not be noticeable, since YT typically takes 100-200ms (or more) to update the view.
6264
*
63-
* Typically after pressing the back button, the time between the first litho event and
64-
* when the nav button is updated is about 10-20ms. Using 50-100ms here should be enough time
65-
* and not noticeable, since YT typically takes 100-200ms (or more) to update the view anyways.
65+
* This delay is only noticeable when the device back button/gesture will not
66+
* change the current navigation tab, such as backing out of the watch history.
6667
*
6768
* This issue can also be avoided on a patch by patch basis, by avoiding calls to
6869
* {@link NavigationButton#getSelectedNavigationButton()} unless absolutely necessary.
6970
*/
70-
private static final long LATCH_AWAIT_TIMEOUT_MILLISECONDS = 75;
71+
private static final long LATCH_AWAIT_TIMEOUT_MILLISECONDS = 120;
7172

7273
/**
7374
* Used as a workaround to fix the issue of YT calling back button handlers out of order.
@@ -113,7 +114,8 @@ private static void waitForNavButtonLatchIfNeeded() {
113114
// The latch is released from the main thread, and waiting from the main thread will always timeout.
114115
// This situation has only been observed when navigating out of a submenu and not changing tabs.
115116
// and for that use case the nav bar does not change so it's safe to return here.
116-
Logger.printDebug(() -> "Cannot block main thread waiting for nav button. Using last known navbar button status.");
117+
Logger.printDebug(() -> "Cannot block main thread waiting for nav button. " +
118+
"Using last known navbar button status.");
117119
return;
118120
}
119121

@@ -131,7 +133,9 @@ private static void waitForNavButtonLatchIfNeeded() {
131133
Logger.printDebug(() -> "Latch wait timed out");
132134

133135
} catch (InterruptedException ex) {
134-
Logger.printException(() -> "Latch wait interrupted failure", ex); // Will never happen.
136+
// Calling YouTube thread was interrupted.
137+
Logger.printException(() -> "Latch wait interrupted", ex);
138+
Thread.currentThread().interrupt(); // Restore interrupt status flag.
135139
}
136140
}
137141

@@ -283,8 +287,8 @@ public enum NavigationButton {
283287
*
284288
* All code calling this method should handle a null return value.
285289
*
286-
* <b>Due to issues with how YT processes physical back button events,
287-
* this patch uses workarounds that can cause this method to take up to 75ms
290+
* <b>Due to issues with how YT processes physical back button/gesture events,
291+
* this patch uses workarounds that can cause this method to take up to 120ms
288292
* if the device back button was recently pressed.</b>
289293
*
290294
* @return The active navigation tab.

patches/src/main/kotlin/app/revanced/patches/youtube/layout/hide/shorts/HideShortsComponentsPatch.kt

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ private val hideShortsComponentsResourcePatch = resourcePatch {
7171
SwitchPreference("revanced_hide_shorts_home"),
7272
SwitchPreference("revanced_hide_shorts_subscriptions"),
7373
SwitchPreference("revanced_hide_shorts_search"),
74+
SwitchPreference("revanced_hide_shorts_history"),
7475

7576
PreferenceScreenPreference(
7677
key = "revanced_shorts_player_screen",

patches/src/main/resources/addresources/values/strings.xml

+9-6
Original file line numberDiff line numberDiff line change
@@ -632,15 +632,18 @@ Note: Enabling this also forcibly hides video ads"</string>
632632
<string name="revanced_shorts_player_screen_summary">Hide or show components in the Shorts player</string>
633633
<!-- 'home' should be translated using the same localized wording YouTube displays for the home tab. -->
634634
<string name="revanced_hide_shorts_home_title">Hide Shorts in home feed</string>
635-
<string name="revanced_hide_shorts_home_summary_on">Shorts in home feed are hidden</string>
636-
<string name="revanced_hide_shorts_home_summary_off">Shorts in home feed are shown</string>
635+
<string name="revanced_hide_shorts_home_summary_on">Home feed and related videos are hidden</string>
636+
<string name="revanced_hide_shorts_home_summary_off">Home feed and related videos are shown</string>
637637
<!-- 'subscription' should be translated using the same localized wording YouTube displays for the subscription tab. -->
638638
<string name="revanced_hide_shorts_subscriptions_title">Hide Shorts in subscription feed</string>
639-
<string name="revanced_hide_shorts_subscriptions_summary_on">Shorts in subscription feed are hidden</string>
640-
<string name="revanced_hide_shorts_subscriptions_summary_off">Shorts in subscription feed are shown</string>
639+
<string name="revanced_hide_shorts_subscriptions_summary_on">Hidden in subscription feed</string>
640+
<string name="revanced_hide_shorts_subscriptions_summary_off">Shown in subscription feed</string>
641641
<string name="revanced_hide_shorts_search_title">Hide Shorts in search results</string>
642-
<string name="revanced_hide_shorts_search_summary_on">Shorts in search results are hidden</string>
643-
<string name="revanced_hide_shorts_search_summary_off">Shorts in search results are shown</string>
642+
<string name="revanced_hide_shorts_search_summary_on">Hidden in search results</string>
643+
<string name="revanced_hide_shorts_search_summary_off">Shown in search results</string>
644+
<string name="revanced_hide_shorts_history_title">Hide Shorts in watch history</string>
645+
<string name="revanced_hide_shorts_history_summary_on">Hidden in watch history</string>
646+
<string name="revanced_hide_shorts_history_summary_off">Shown in watch history</string>
644647
<!-- 'join' should be translated using the same localized wording YouTube displays for the button. -->
645648
<string name="revanced_hide_shorts_join_button_title">Hide join button</string>
646649
<string name="revanced_hide_shorts_join_button_summary_on">Join button is hidden</string>

0 commit comments

Comments
 (0)