Skip to content

Commit

Permalink
feat: Selective filtering of search/home/subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
LisoUseInAIKyrios committed Mar 14, 2024
1 parent d89490b commit 6e9cc14
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.revanced.patches.youtube.layout.buttons.navigation

import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.InstructionExtensions.addInstruction
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.patch.BytecodePatch
Expand All @@ -16,8 +17,13 @@ import app.revanced.patches.youtube.layout.buttons.navigation.utils.InjectionUti
import app.revanced.patches.youtube.misc.integrations.IntegrationsPatch
import app.revanced.patches.youtube.misc.settings.SettingsPatch
import app.revanced.util.exception
import app.revanced.util.getReference
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
import com.android.tools.smali.dexlib2.iface.reference.MethodReference

// TODO: break this patch apart so other patches can check what tab is active (without depending on this patch).
@Patch(
name = "Navigation buttons",
description = "Adds options to hide and change navigation buttons (such as the Shorts button).",
Expand Down Expand Up @@ -54,7 +60,7 @@ import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
)
@Suppress("unused")
object NavigationButtonsPatch : BytecodePatch(
setOf(AddCreateButtonViewFingerprint),
setOf(AddCreateButtonViewFingerprint, ActionBarSearchResultsFingerprint),
) {
private const val INTEGRATIONS_CLASS_DESCRIPTOR =
"Lapp/revanced/integrations/youtube/patches/NavigationButtonsPatch;"
Expand Down Expand Up @@ -106,17 +112,17 @@ object NavigationButtonsPatch : BytecodePatch(
* Inject hooks
*/

val enumHook = "sput-object v$REGISTER_TEMPLATE_REPLACEMENT, " +
"$INTEGRATIONS_CLASS_DESCRIPTOR->lastNavigationButton:Ljava/lang/Enum;"
val enumHook = "invoke-static { v$REGISTER_TEMPLATE_REPLACEMENT }, " +
"$INTEGRATIONS_CLASS_DESCRIPTOR->setLastAppNavigationEnum(Ljava/lang/Enum;)V"
val buttonHook = "invoke-static { v$REGISTER_TEMPLATE_REPLACEMENT }, " +
"$INTEGRATIONS_CLASS_DESCRIPTOR->hideButton(Landroid/view/View;)V"
"$INTEGRATIONS_CLASS_DESCRIPTOR->navigationTabLoaded(Landroid/view/View;)V"

// Inject bottom to top to not mess up the indices
mapOf(
buttonHook to buttonHookInsertIndex,
enumHook to enumHookInsertIndex,
).forEach { (hook, insertIndex) ->
initializeButtonsResult.mutableMethod.injectHook(hook, insertIndex)
initializeButtonsResult.mutableMethod.injectHook(insertIndex, hook)
}

/*
Expand Down Expand Up @@ -154,14 +160,31 @@ object NavigationButtonsPatch : BytecodePatch(

PivotBarCreateButtonViewFingerprint.result!!.apply {
val insertIndex = scanResult.patternScanResult!!.endIndex
mutableMethod.injectHook(
insertIndex,
"invoke-static { v$REGISTER_TEMPLATE_REPLACEMENT }, " +
"$INTEGRATIONS_CLASS_DESCRIPTOR->createTabLoaded(Landroid/view/View;)V"
)
}

/*
* Inject hooks
*/
val hook = "invoke-static { v$REGISTER_TEMPLATE_REPLACEMENT }, " +
"$INTEGRATIONS_CLASS_DESCRIPTOR->hideCreateButton(Landroid/view/View;)V"
/**
* Search bar TextView
*/

mutableMethod.injectHook(hook, insertIndex)
}
// Two different layouts are used at the hooked code.
// Insert before the first ViewGroup method call after inflating,
// so this works regardless which layout is used.
ActionBarSearchResultsFingerprint.result?.mutableMethod?.apply {
val instructionIndex = implementation!!.instructions.indexOfFirst {
it.opcode == Opcode.INVOKE_VIRTUAL &&
it.getReference<MethodReference>()?.name == "setLayoutDirection"
}
val register = getInstruction<FiveRegisterInstruction>(instructionIndex).registerC
addInstruction(
instructionIndex,
"invoke-static { v$register }, " +
"$INTEGRATIONS_CLASS_DESCRIPTOR->searchBarResultsViewLoaded(Landroid/view/View;)V"
)
} ?: ActionBarSearchResultsFingerprint.exception
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package app.revanced.patches.youtube.layout.buttons.navigation

import app.revanced.util.exception
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchException
import app.revanced.patcher.patch.annotation.Patch
import app.revanced.patches.shared.misc.mapping.ResourceMappingPatch
import app.revanced.patches.youtube.layout.buttons.navigation.fingerprints.InitializeButtonsFingerprint
import app.revanced.patches.youtube.layout.buttons.navigation.fingerprints.PivotBarConstructorFingerprint
import app.revanced.util.exception

@Patch(
description = "Resolves necessary fingerprints.",
Expand All @@ -17,11 +16,17 @@ internal object ResolvePivotBarFingerprintsPatch : BytecodePatch(
setOf(PivotBarConstructorFingerprint)
) {
internal var imageOnlyTabResourceId: Long = -1
internal var actionBarSearchResultsViewMicId: Long = -1

override fun execute(context: BytecodeContext) {
// imageOnlyTabResourceId is used in InitializeButtonsFingerprint fingerprint
ResourceMappingPatch.resourceMappings.find { it.type == "layout" && it.name == "image_only_tab" }
?.let { imageOnlyTabResourceId = it.id } ?: throw PatchException("Failed to find resource")
imageOnlyTabResourceId = ResourceMappingPatch.resourceMappings.single {
it.type == "layout" && it.name == "image_only_tab"
}.id

actionBarSearchResultsViewMicId = ResourceMappingPatch.resourceMappings.single {
it.type == "layout" && it.name == "action_bar_search_results_view_mic"
}.id

PivotBarConstructorFingerprint.result?.let {
// Resolve InitializeButtonsFingerprint on the class of the method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal object InjectionUtils {
* @param insertIndex The index to insert the instruction at.
* [MOVE_RESULT_OBJECT] has to be the previous instruction before [insertIndex].
*/
fun MutableMethod.injectHook(hook: String, insertIndex: Int) {
fun MutableMethod.injectHook(insertIndex: Int, hook: String) {
val injectTarget = this

// Register to pass to the hook
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ import app.revanced.patcher.patch.annotation.Patch
import app.revanced.patcher.util.smali.ExternalLabel
import app.revanced.patches.all.misc.resources.AddResourcesPatch
import app.revanced.patches.shared.misc.settings.preference.InputType
import app.revanced.patches.shared.misc.settings.preference.NonInteractivePreference
import app.revanced.patches.shared.misc.settings.preference.PreferenceScreen
import app.revanced.patches.shared.misc.settings.preference.PreferenceScreen.Sorting
import app.revanced.patches.shared.misc.settings.preference.SwitchPreference
import app.revanced.patches.shared.misc.settings.preference.TextPreference
import app.revanced.patches.youtube.layout.buttons.navigation.NavigationButtonsPatch
import app.revanced.patches.youtube.layout.hide.general.fingerprints.ParseElementFromBufferFingerprint
import app.revanced.patches.youtube.layout.hide.general.fingerprints.PlayerOverlayFingerprint
import app.revanced.patches.youtube.layout.hide.general.fingerprints.ShowWatermarkFingerprint
import app.revanced.patches.youtube.misc.litho.filter.LithoFilterPatch
import app.revanced.patches.youtube.misc.playertype.PlayerTypeHookPatch
import app.revanced.patches.youtube.misc.settings.SettingsPatch
import app.revanced.util.exception
import com.android.tools.smali.dexlib2.Opcode
Expand All @@ -33,6 +36,8 @@ import com.android.tools.smali.dexlib2.iface.instruction.TwoRegisterInstruction
LithoFilterPatch::class,
SettingsPatch::class,
AddResourcesPatch::class,
NavigationButtonsPatch::class, // FIXME: use a sub patch
PlayerTypeHookPatch::class // Used by Keyword Content filter.
],
compatiblePackages = [
CompatiblePackage(
Expand Down Expand Up @@ -121,12 +126,15 @@ object HideLayoutComponentsPatch : BytecodePatch(

SettingsPatch.PreferenceScreen.FEED.addPreferences(
PreferenceScreen(
key = "revanced_hide_keyword_content_preference_screen",
key = "revanced_hide_keyword_content_screen",
sorting = Sorting.UNSORTED,
preferences = setOf(
SwitchPreference("revanced_hide_keyword_content"),
SwitchPreference("revanced_hide_keyword_content_search"),
SwitchPreference("revanced_hide_keyword_content_home"),
SwitchPreference("revanced_hide_keyword_content_subscriptions"),
TextPreference("revanced_hide_keyword_content_phrases", inputType = InputType.TEXT_MULTI_LINE),
),
NonInteractivePreference("revanced_hide_keyword_content_about")
)
)
)

Expand Down
18 changes: 13 additions & 5 deletions src/main/resources/addresources/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,21 @@
<string name="revanced_custom_filter_strings_title">Custom filter</string>
<string name="revanced_custom_filter_strings_summary">List of component path builder strings to filter separated by new line</string>
<string name="revanced_custom_filter_toast_invalid_syntax">Invalid custom filter: %s</string>
<string name="revanced_hide_keyword_content_preference_screen_title">Hide keyword content</string>
<string name="revanced_hide_keyword_content_preference_screen_summary">Hide feed and search result videos using keyword filters</string>
<string name="revanced_hide_keyword_content_title">Enable keyword filtering</string>
<string name="revanced_hide_keyword_content_summary_on">Feed/search is filtered to hide content that matches keyword phrases\n\nLimitations\n• Some Shorts may not be hidden\n• Some UI elements may not be hidden\n• Searching for a keyword may show no results</string>
<string name="revanced_hide_keyword_content_summary_off">Feed/search results are not filtered by keywords</string>
<string name="revanced_hide_keyword_content_screen_title">Hide keyword content</string>
<string name="revanced_hide_keyword_content_screen_summary">Hide feed and search result videos using keyword filters</string>
<string name="revanced_hide_keyword_content_home_title">Hide home videos by keywords</string>
<string name="revanced_hide_keyword_content_home_summary_on">Videos in the home tab are filtered by keywords</string>
<string name="revanced_hide_keyword_content_home_summary_off">Videos in the home tab are not filtered by keywords</string>
<string name="revanced_hide_keyword_content_subscriptions_title">Hide subscription videos by keywords</string>x
<string name="revanced_hide_keyword_content_subscriptions_summary_on">Videos in the subscriptions tab are filtered by keywords</string>
<string name="revanced_hide_keyword_content_subscriptions_summary_off">Videos in the subscriptions tab are not filtered by keywords</string>
<string name="revanced_hide_keyword_content_search_title">Hide search results by keywords</string>x
<string name="revanced_hide_keyword_content_search_summary_on">Search results are filtered by keywords</string>
<string name="revanced_hide_keyword_content_search_summary_off">Search results are not filtered by keywords</string>
<string name="revanced_hide_keyword_content_phrases_title">Keywords to hide</string>
<string name="revanced_hide_keyword_content_phrases_summary">Keywords and phrases to hide, separated by new lines\n\nWords with uppercase letters in the middle must be entered with the casing (ie: iPhone, TikTok, LeBlanc)</string>
<string name="revanced_hide_keyword_content_about_title">About keyword filtering</string>
<string name="revanced_hide_keyword_content_about_summary">Home/Subscription/Search results are filtered to hide content that matches keyword phrases\n\nLimitations\n• Some Shorts may not be hidden\n• Some UI elements may not be hidden\n• Searching for a keyword may show no results</string>
<string name="revanced_hide_keyword_toast_invalid_length" formatted="false">Invalid keyword length (must be at least %s characters): %s</string>
</patch>
<patch id="ad.general.HideAdsResourcePatch">
Expand Down

0 comments on commit 6e9cc14

Please sign in to comment.