Skip to content

Commit

Permalink
fix(youtube/settings): fix non functional back button in settings (#2178
Browse files Browse the repository at this point in the history
)
  • Loading branch information
LisoUseInAIKyrios authored and oSumAtrIX committed May 19, 2023
1 parent 95bbf46 commit 46da834
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,11 @@ class ReturnYouTubeDislikeResourcePatch : ResourcePatch {
}

override fun execute(context: ResourceContext): PatchResult {
val youtubePackage = "com.google.android.youtube"
SettingsPatch.addPreference(
Preference(
StringResource("revanced_ryd_settings_title", "Return YouTube Dislike"),
StringResource("revanced_ryd_settings_summary", "Settings for Return YouTube Dislike"),
Preference.Intent(
youtubePackage,
"ryd_settings",
"com.google.android.libraries.social.licenses.LicenseActivity"
)
SettingsPatch.createReVancedSettingsIntent("ryd_settings")
)
)
// merge strings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,11 @@ import app.revanced.util.resources.ResourceUtils.mergeStrings
class SponsorBlockResourcePatch : ResourcePatch {

override fun execute(context: ResourceContext): PatchResult {
val youtubePackage = "com.google.android.youtube"
SettingsPatch.addPreference(
Preference(
StringResource("sb_settings", "SponsorBlock"),
StringResource("revanced_sponsorblock_settings_title", "SponsorBlock"),
StringResource("revanced_sponsorblock_settings_summary", "SponsorBlock related settings"),
Preference.Intent(
youtubePackage,
"sponsorblock_settings",
"com.google.android.libraries.social.licenses.LicenseActivity"
)
SettingsPatch.createReVancedSettingsIntent("sponsorblock_settings")
)
)
val classLoader = this.javaClass.classLoader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,17 @@ class SettingsPatch : BytecodePatch(
}
} ?: return SetThemeFingerprint.toErrorResult()

// set the theme based on the preference of the device

// Modify the license activity and remove all existing layout code.
// Must modify an existing activity and cannot add a new activity to the manifest,
// as that fails for root installations.
LicenseActivityFingerprint.result!!.apply licenseActivity@{
mutableMethod.apply {
fun buildSettingsActivityInvokeString(
registers: String = "p0",
classDescriptor: String = SETTINGS_ACTIVITY_DESCRIPTOR,
methodName: String = "initializeSettings",
parameters: String = this@licenseActivity.mutableClass.type
parameters: String = "Landroid/app/Activity;"
) = getSetThemeInstructionString(registers, classDescriptor, methodName, parameters)

// initialize the settings
Expand All @@ -77,9 +80,6 @@ class SettingsPatch : BytecodePatch(
return-void
"""
)

// set the current theme
addInstruction(0, buildSettingsActivityInvokeString(methodName = "setTheme"))
}

// remove method overrides
Expand All @@ -88,14 +88,13 @@ class SettingsPatch : BytecodePatch(
}
}


return PatchResultSuccess()
}

internal companion object {
private const val INTEGRATIONS_PACKAGE = "app/revanced/integrations"

private const val SETTINGS_ACTIVITY_DESCRIPTOR = "L$INTEGRATIONS_PACKAGE/settingsmenu/ReVancedSettingActivity;"

private const val THEME_HELPER_DESCRIPTOR = "L$INTEGRATIONS_PACKAGE/utils/ThemeHelper;"
private const val SET_THEME_METHOD_NAME = "setTheme"

Expand All @@ -110,6 +109,15 @@ class SettingsPatch : BytecodePatch(
fun renameIntentsTargetPackage(newPackage: String) {
SettingsResourcePatch.overrideIntentsTargetPackage = newPackage
}

/**
* Creates an intent to open ReVanced settings of the given name
*/
fun createReVancedSettingsIntent(settingsName: String) = Preference.Intent(
"com.google.android.youtube",
settingsName,
"com.google.android.libraries.social.licenses.LicenseActivity"
)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch
import app.revanced.util.resources.ResourceUtils
import app.revanced.util.resources.ResourceUtils.copyResources
import app.revanced.util.resources.ResourceUtils.mergeStrings
import org.w3c.dom.Element
import org.w3c.dom.Node

@Name("settings-resource-patch")
Expand All @@ -34,40 +35,51 @@ class SettingsResourcePatch : AbstractSettingsResourcePatch(
it.type == "string" && it.name == "app_theme_appearance_dark"
}!!.id


// Create missing directory for the resources.
context["res/drawable-ldrtl-xxxhdpi"].mkdirs()


// Copy layout resources.
/*
* copy layout resources
*/
arrayOf(
ResourceUtils.ResourceGroup(
"layout",
"revanced_settings_toolbar.xml",
"revanced_settings_with_toolbar.xml",
"revanced_settings_with_toolbar_layout.xml"
), ResourceUtils.ResourceGroup(
// required resource for back button, because when the base APK is used, this resource will not exist
"drawable-xxxhdpi", "quantum_ic_arrow_back_white_24.png"
), ResourceUtils.ResourceGroup(
// required resource for back button, because when the base APK is used, this resource will not exist
"drawable-ldrtl-xxxhdpi", "quantum_ic_arrow_back_white_24.png"
)
ResourceUtils.ResourceGroup("layout", "revanced_settings_with_toolbar.xml")
).forEach { resourceGroup ->
context.copyResources("settings", resourceGroup)
}

preferencesEditor = context.xmlEditor["res/xml/settings_fragment.xml"]

// Add the ReVanced settings to the YouTube settings.
val youtubePackage = "com.google.android.youtube"
// Modify the manifest and add an data intent filter to the LicenseActivity.
// Some devices freak out if undeclared data is passed to an intent,
// and this change appears to fix the issue.
context.xmlEditor["AndroidManifest.xml"].use { editor ->
// An xml regular expression would probably work better than this manual searching.
val manifestNodes = editor.file.getElementsByTagName("manifest").item(0).childNodes
for (i in 0..manifestNodes.length) {
val node = manifestNodes.item(i)
if (node != null && node.nodeName == "application") {
val applicationNodes = node.childNodes
for (j in 0..applicationNodes.length) {
val applicationChild = applicationNodes.item(j)
if (applicationChild is Element && applicationChild.nodeName == "activity"
&& applicationChild.getAttribute("android:name") == "com.google.android.libraries.social.licenses.LicenseActivity"
) {
val intentFilter = editor.file.createElement("intent-filter")
val mimeType = editor.file.createElement("data")
mimeType.setAttribute("android:mimeType", "text/plain")
intentFilter.appendChild(mimeType)
applicationChild.appendChild(intentFilter)
break
}
}
}
}
}


// Add the ReVanced settings to the YouTube settings
SettingsPatch.addPreference(
Preference(
StringResource("revanced_settings", "ReVanced"),
StringResource("revanced_settings_summary", "ReVanced specific settings"),
Preference.Intent(
youtubePackage, "revanced_settings", "com.google.android.libraries.social.licenses.LicenseActivity"
)
SettingsPatch.createReVancedSettingsIntent("revanced_settings")
)
)

Expand Down
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions src/main/resources/settings/host/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="revanced_settings_title">ReVanced</string>
<string name="revanced_settings_confirm_user_dialog_title">Do you wish to proceed?</string>
<string name="revanced_settings_reset">Reset</string>

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<merge>
<include layout="@layout/revanced_settings_with_toolbar_layout"/>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:transitionGroup="true">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/yt_white1"
android:elevation="4dp">

<android.support.v7.widget.Toolbar
android:id="@+id/revanced_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/ytBrandBackgroundSolid"
app:navigationIcon="@drawable/yt_outline_arrow_left_black_24"
app:title="@string/revanced_settings" />
</FrameLayout>

<FrameLayout
android:id="@+id/revanced_settings_fragments"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>
</merge>

This file was deleted.

0 comments on commit 46da834

Please sign in to comment.