Skip to content

Commit

Permalink
feat(YouTube): add Change share sheet patch
Browse files Browse the repository at this point in the history
  • Loading branch information
inotia00 committed Aug 11, 2024
1 parent 4aec1a1 commit 7339479
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package app.revanced.patches.youtube.misc.share

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.patches.shared.litho.LithoFilterPatch
import app.revanced.patches.youtube.misc.share.fingerprints.BottomSheetRecyclerViewFingerprint
import app.revanced.patches.youtube.misc.share.fingerprints.UpdateShareSheetCommandFingerprint
import app.revanced.patches.youtube.utils.compatibility.Constants.COMPATIBLE_PACKAGE
import app.revanced.patches.youtube.utils.integrations.Constants.COMPONENTS_PATH
import app.revanced.patches.youtube.utils.integrations.Constants.MISC_PATH
import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch
import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.BottomSheetRecyclerView
import app.revanced.patches.youtube.utils.settings.SettingsPatch
import app.revanced.util.getTargetIndexOrThrow
import app.revanced.util.getWideLiteralInstructionIndex
import app.revanced.util.patch.BaseBytecodePatch
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
import com.android.tools.smali.dexlib2.iface.instruction.TwoRegisterInstruction

@Suppress("unused")
object ShareSheetPatch : BaseBytecodePatch(
name = "Change share sheet",
description = "Add option to change from in-app share sheet to system share sheet.",
dependencies = setOf(
LithoFilterPatch::class,
SettingsPatch::class,
SharedResourceIdPatch::class
),
compatiblePackages = COMPATIBLE_PACKAGE,
fingerprints = setOf(
BottomSheetRecyclerViewFingerprint,
UpdateShareSheetCommandFingerprint,
)
) {
private const val INTEGRATIONS_CLASS_DESCRIPTOR =
"$MISC_PATH/ShareSheetPatch;"

private const val FILTER_CLASS_DESCRIPTOR =
"$COMPONENTS_PATH/ShareSheetMenuFilter;"

override fun execute(context: BytecodeContext) {

// Detects that the Share sheet panel has been invoked.
BottomSheetRecyclerViewFingerprint.resultOrThrow().mutableMethod.apply {
val constIndex = getWideLiteralInstructionIndex(BottomSheetRecyclerView)
val targetIndex = getTargetIndexOrThrow(constIndex, Opcode.CHECK_CAST)
val targetRegister = getInstruction<OneRegisterInstruction>(targetIndex).registerA

addInstruction(
targetIndex + 1,
"invoke-static {v$targetRegister}, $INTEGRATIONS_CLASS_DESCRIPTOR->onShareSheetMenuCreate(Landroid/support/v7/widget/RecyclerView;)V"
)
}

// Remove the app list from the Share sheet panel on YouTube.
UpdateShareSheetCommandFingerprint.resultOrThrow().let {
it.mutableMethod.apply {
val targetIndex = it.scanResult.patternScanResult!!.endIndex
val targetRegister = getInstruction<TwoRegisterInstruction>(targetIndex).registerA

addInstructions(
targetIndex + 1, """
invoke-static {v$targetRegister}, $INTEGRATIONS_CLASS_DESCRIPTOR->overridePackageName(Ljava/lang/String;)Ljava/lang/String;
move-result-object v$targetRegister
"""
)
}
}

LithoFilterPatch.addFilter(FILTER_CLASS_DESCRIPTOR)

/**
* Add settings
*/
SettingsPatch.addPreference(
arrayOf(
"PREFERENCE_CATEGORY: MISC_EXPERIMENTAL_FLAGS",
"SETTINGS: CHANGE_SHARE_SHEET"
)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package app.revanced.patches.youtube.misc.share.fingerprints

import app.revanced.patcher.extensions.or
import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.BottomSheetRecyclerView
import app.revanced.util.fingerprint.LiteralValueFingerprint
import com.android.tools.smali.dexlib2.AccessFlags

internal object BottomSheetRecyclerViewFingerprint : LiteralValueFingerprint(
returnType = "Lj${'$'}/util/Optional;",
accessFlags = AccessFlags.PROTECTED or AccessFlags.FINAL,
parameters = emptyList(),
literalSupplier = { BottomSheetRecyclerView }
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package app.revanced.patches.youtube.misc.share.fingerprints

import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.MethodFingerprint
import app.revanced.util.getReference
import app.revanced.util.indexOfFirstInstruction
import com.android.tools.smali.dexlib2.AccessFlags
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.reference.FieldReference

internal object UpdateShareSheetCommandFingerprint : MethodFingerprint(
returnType = "V",
accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL,
parameters = listOf("L", "Ljava/util/Map;"),
opcodes = listOf(
Opcode.IF_EQZ,
Opcode.INVOKE_INTERFACE,
Opcode.MOVE_RESULT_OBJECT,
Opcode.CHECK_CAST,
Opcode.IGET_OBJECT
),
customFingerprint = custom@{ methodDef, _ ->
methodDef.indexOfFirstInstruction {
opcode == Opcode.SGET_OBJECT &&
getReference<FieldReference>()?.name == "updateShareSheetCommand"
} >= 0
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ object SharedResourceIdPatch : ResourcePatch() {
var Bar = -1L
var BarContainerHeight = -1L
var BottomSheetFooterText = -1L
var BottomSheetRecyclerView = -1L
var BottomUiContainerStub = -1L
var CaptionToggleContainer = -1L
var CastMediaRouteButton = -1L
Expand Down Expand Up @@ -136,6 +137,7 @@ object SharedResourceIdPatch : ResourcePatch() {
Bar = getId(LAYOUT, "bar")
BarContainerHeight = getId(DIMEN, "bar_container_height")
BottomSheetFooterText = getId(ID, "bottom_sheet_footer_text")
BottomSheetRecyclerView = getId(LAYOUT, "bottom_sheet_recycler_view")
BottomUiContainerStub = getId(ID, "bottom_ui_container_stub")
CaptionToggleContainer = getId(ID, "caption_toggle_container")
CastMediaRouteButton = getId(LAYOUT, "castmediaroutebutton")
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/youtube/settings/host/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,9 @@ Tap on the continue button and disable battery optimizations."</string>
<string name="revanced_disable_quic_protocol_title">Disable QUIC protocol</string>
<string name="revanced_disable_quic_protocol_summary">"Disable CronetEngine's QUIC protocol."</string>

<string name="revanced_change_share_sheet_title">Change share sheet</string>
<string name="revanced_change_share_sheet_summary_on">System share sheet is used.</string>
<string name="revanced_change_share_sheet_summary_off">In-app share sheet is used.</string>
<string name="revanced_enable_opus_codec_title">Enable OPUS codec</string>
<string name="revanced_enable_opus_codec_summary">Enable the OPUS codec if the player response includes the OPUS codec.</string>

Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/youtube/settings/xml/revanced_prefs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,9 @@
<!-- PREFERENCE_CATEGORY: MISC_EXPERIMENTAL_FLAGS
<PreferenceCategory android:title="@string/revanced_preference_category_experimental_flag" android:layout="@layout/revanced_settings_preferences_category"/>PREFERENCE_CATEGORY: MISC_EXPERIMENTAL_FLAGS -->

<!-- SETTINGS: CHANGE_SHARE_SHEET
<SwitchPreference android:title="@string/revanced_change_share_sheet_title" android:key="revanced_change_share_sheet" android:summaryOn="@string/revanced_change_share_sheet_summary_on" android:summaryOff="@string/revanced_change_share_sheet_summary_off" />SETTINGS: CHANGE_SHARE_SHEET -->

<!-- SETTINGS: ENABLE_OPUS_CODEC
<SwitchPreference android:title="@string/revanced_enable_opus_codec_title" android:key="revanced_enable_opus_codec" android:summary="@string/revanced_enable_opus_codec_summary" />SETTINGS: ENABLE_OPUS_CODEC -->

Expand Down Expand Up @@ -742,6 +745,7 @@
</PreferenceCategory>

<PreferenceCategory android:title="@string/revanced_preference_screen_misc_title" android:layout="@layout/revanced_settings_preferences_category">
<Preference android:title="Change share sheet" android:summary="@string/revanced_patches_excluded" android:selectable="false"/>
<Preference android:title="Disable QUIC protocol" android:summary="@string/revanced_patches_excluded" android:selectable="false"/>
<Preference android:title="Enable debug logging" android:summary="@string/revanced_patches_excluded" android:selectable="false"/>
<Preference android:title="Enable external browser" android:summary="@string/revanced_patches_excluded" android:selectable="false"/>
Expand Down

0 comments on commit 7339479

Please sign in to comment.