Skip to content

Commit

Permalink
feat(YouTube Music - Hide action bar components): Add support for set…
Browse files Browse the repository at this point in the history
…ting `Override Download action button` on YouTube Music 7.25.53+ inotia00/ReVanced_Extended#2733
  • Loading branch information
inotia00 committed Feb 10, 2025
1 parent 835f675 commit 9adcdbf
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,38 @@
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.facebook.litho.ComponentHost;

import java.util.Map;

import app.revanced.extension.music.settings.Settings;
import app.revanced.extension.music.utils.VideoUtils;
import app.revanced.extension.shared.utils.Logger;
import app.revanced.extension.shared.utils.PackageUtils;

@SuppressWarnings("unused")
public class ActionBarPatch {
private static final boolean HIDE_ACTION_BUTTON_LABEL =
Settings.HIDE_ACTION_BUTTON_LABEL.get();
private static final boolean HIDE_ACTION_BUTTON_LIKE_DISLIKE =
Settings.HIDE_ACTION_BUTTON_LIKE_DISLIKE.get() || PackageUtils.getAppVersionName().compareTo("7.25.00") >= 0;
private static final boolean EXTERNAL_DOWNLOADER_ACTION_BUTTON =
Settings.EXTERNAL_DOWNLOADER_ACTION_BUTTON.get();
private static final boolean SETTINGS_INITIALIZED =
Settings.SETTINGS_INITIALIZED.get();
private static final String ELEMENTS_SENDER_VIEW =
"com.google.android.libraries.youtube.rendering.elements.sender_view";
private static final String EXTERNAL_DOWNLOADER_LAUNCHED =
"external_downloader_launched";
private static String downloadButtonLabel = "";

@NonNull
private static String buttonType = "";

public static boolean hideActionBarLabel() {
return Settings.HIDE_ACTION_BUTTON_LABEL.get();
return HIDE_ACTION_BUTTON_LABEL;
}

public static boolean hideActionButton() {
Expand All @@ -29,24 +49,54 @@ public static boolean hideActionButton() {
}

public static void hideLikeDislikeButton(View view) {
final boolean enabled = Settings.HIDE_ACTION_BUTTON_LIKE_DISLIKE.get();
hideViewUnderCondition(
enabled,
HIDE_ACTION_BUTTON_LIKE_DISLIKE,
view
);
hideViewBy0dpUnderCondition(
enabled,
HIDE_ACTION_BUTTON_LIKE_DISLIKE,
view
);
}

public static void inAppDownloadButtonOnClick(View view) {
if (!Settings.EXTERNAL_DOWNLOADER_ACTION_BUTTON.get()) {
return;
if (EXTERNAL_DOWNLOADER_ACTION_BUTTON &&
buttonType.equals(ActionButton.DOWNLOAD.name)) {
view.setOnClickListener(imageView -> VideoUtils.launchExternalDownloader());
}
}

if (buttonType.equals(ActionButton.DOWNLOAD.name))
view.setOnClickListener(imageView -> VideoUtils.launchExternalDownloader());
public static boolean inAppDownloadButtonOnClick(@Nullable Map<Object, Object> map) {
try {
if (EXTERNAL_DOWNLOADER_ACTION_BUTTON &&
!downloadButtonLabel.isEmpty() &&
map != null &&
map.get(ELEMENTS_SENDER_VIEW) instanceof ComponentHost componentHost &&
componentHost.getContentDescription().toString().equals(downloadButtonLabel)
) {
if (!map.containsKey(EXTERNAL_DOWNLOADER_LAUNCHED)) {
map.put(EXTERNAL_DOWNLOADER_LAUNCHED, Boolean.TRUE);
VideoUtils.runOnMainThreadDelayed(VideoUtils::launchExternalDownloader, 0);
}
return true;
}
} catch (Exception ex) {
Logger.printException(() -> "inAppDownloadButtonOnClick failed", ex);
}

return false;
}

public static CharSequence onLithoTextLoaded(@NonNull Object conversionContext,
@NonNull CharSequence original) {
if (EXTERNAL_DOWNLOADER_ACTION_BUTTON &&
downloadButtonLabel.isEmpty() &&
conversionContext.toString().contains("music_download_button.eml")) {
downloadButtonLabel = original.toString();
Logger.printDebug(() -> "set download button label: " + original);
}

return original;
}

public static void setButtonType(@NonNull Object obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import app.revanced.patches.music.utils.settings.settingsPatch
import app.revanced.patches.music.video.information.videoInformationPatch
import app.revanced.patches.shared.litho.addLithoFilter
import app.revanced.patches.shared.litho.lithoFilterPatch
import app.revanced.patches.shared.textcomponent.hookSpannableString
import app.revanced.patches.shared.textcomponent.textComponentPatch
import app.revanced.util.fingerprint.matchOrThrow
import app.revanced.util.fingerprint.methodOrThrow
import app.revanced.util.getReference
Expand Down Expand Up @@ -50,13 +52,31 @@ val actionBarComponentsPatch = bytecodePatch(
settingsPatch,
lithoFilterPatch,
sharedResourceIdPatch,
textComponentPatch,
videoInformationPatch,
versionCheckPatch,
)

execute {
if (is_7_17_or_greater) {
addLithoFilter(FILTER_CLASS_DESCRIPTOR)
hookSpannableString(ACTIONBAR_CLASS_DESCRIPTOR, "onLithoTextLoaded")

commandResolverFingerprint.methodOrThrow().addInstruction(
0,
"invoke-static {p2}, $ACTIONBAR_CLASS_DESCRIPTOR->inAppDownloadButtonOnClick(Ljava/util/Map;)Z"
)

offlineVideoEndpointFingerprint.methodOrThrow().addInstructionsWithLabels(
0, """
invoke-static {p2}, $ACTIONBAR_CLASS_DESCRIPTOR->inAppDownloadButtonOnClick(Ljava/util/Map;)Z
move-result v0
if-eqz v0, :ignore
return-void
:ignore
nop
"""
)
}

if (!is_7_25_or_greater) {
Expand Down Expand Up @@ -181,12 +201,12 @@ val actionBarComponentsPatch = bytecodePatch(
)
addSwitchPreference(
CategoryType.ACTION_BAR,
"revanced_hide_action_button_share",
"revanced_hide_action_button_radio",
"false"
)
addSwitchPreference(
CategoryType.ACTION_BAR,
"revanced_hide_action_button_radio",
"revanced_hide_action_button_share",
"false"
)
if (!is_7_25_or_greater) {
Expand All @@ -195,17 +215,17 @@ val actionBarComponentsPatch = bytecodePatch(
"revanced_hide_action_button_label",
"false"
)
addSwitchPreference(
CategoryType.ACTION_BAR,
"revanced_external_downloader_action",
"false"
)
addPreferenceWithIntent(
CategoryType.ACTION_BAR,
"revanced_external_downloader_package_name",
"revanced_external_downloader_action"
)
}
addSwitchPreference(
CategoryType.ACTION_BAR,
"revanced_external_downloader_action",
"false"
)
addPreferenceWithIntent(
CategoryType.ACTION_BAR,
"revanced_external_downloader_package_name",
"revanced_external_downloader_action"
)

updatePatchStatus(HIDE_ACTION_BAR_COMPONENTS)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,19 @@ internal val likeDislikeContainerFingerprint = legacyFingerprint(
accessFlags = AccessFlags.PUBLIC or AccessFlags.CONSTRUCTOR,
literals = listOf(likeDislikeContainer)
)

internal val commandResolverFingerprint = legacyFingerprint(
name = "commandResolverFingerprint",
accessFlags = AccessFlags.PUBLIC or AccessFlags.STATIC or AccessFlags.FINAL,
returnType = "Z",
parameters = listOf("L", "L", "Ljava/util/Map;"),
strings = listOf("CommandResolver threw exception during resolution")
)

internal val offlineVideoEndpointFingerprint = legacyFingerprint(
name = "offlineVideoEndpointFingerprint",
accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL,
returnType = "V",
parameters = listOf("L", "Ljava/util/Map;"),
strings = listOf("Object is not an offlineable video: %s")
)
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
<string name="revanced_hide_action_button_add_to_playlist_summary">Hides the Save button.</string>
<string name="revanced_hide_action_button_download_title">Hide Download button</string>
<string name="revanced_hide_action_button_download_summary">Hides the Download button.</string>
<string name="revanced_hide_action_button_share_title">Hide Share button</string>
<string name="revanced_hide_action_button_share_summary">Hides the Share button.</string>
<string name="revanced_hide_action_button_radio_title">Hide Radio button</string>
<string name="revanced_hide_action_button_radio_summary">Hides the Radio button.</string>
<string name="revanced_hide_action_button_share_title">Hide Share button</string>
<string name="revanced_hide_action_button_share_summary">Hides the Share button.</string>
<string name="revanced_hide_action_button_label_title">Hide action button labels</string>
<string name="revanced_hide_action_button_label_summary">Hides the labels of the action buttons.</string>
<string name="revanced_external_downloader_action_title">Override Download action button</string>
Expand Down

0 comments on commit 9adcdbf

Please sign in to comment.