2022-07-31 12:15:26 +02:00
|
|
|
package app.revanced.patches.youtube.misc.settings.bytecode.patch
|
|
|
|
|
|
|
|
import app.revanced.patcher.annotation.Description
|
|
|
|
import app.revanced.patcher.annotation.Name
|
|
|
|
import app.revanced.patcher.annotation.Version
|
|
|
|
import app.revanced.patcher.data.impl.BytecodeData
|
|
|
|
import app.revanced.patcher.extensions.addInstruction
|
|
|
|
import app.revanced.patcher.extensions.addInstructions
|
|
|
|
import app.revanced.patcher.patch.PatchResult
|
|
|
|
import app.revanced.patcher.patch.PatchResultSuccess
|
2022-08-03 03:53:35 +02:00
|
|
|
import app.revanced.patcher.patch.annotations.DependsOn
|
2022-07-31 12:15:26 +02:00
|
|
|
import app.revanced.patcher.patch.annotations.Patch
|
|
|
|
import app.revanced.patcher.patch.impl.BytecodePatch
|
2022-08-03 03:09:52 +02:00
|
|
|
import app.revanced.patcher.util.smali.toInstruction
|
2022-07-31 12:15:26 +02:00
|
|
|
import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch
|
2022-09-26 04:12:09 +02:00
|
|
|
import app.revanced.patches.youtube.misc.mapping.patch.ResourceMappingResourcePatch
|
2022-07-31 12:15:26 +02:00
|
|
|
import app.revanced.patches.youtube.misc.settings.annotations.SettingsCompatibility
|
|
|
|
import app.revanced.patches.youtube.misc.settings.bytecode.fingerprints.LicenseActivityFingerprint
|
|
|
|
import app.revanced.patches.youtube.misc.settings.bytecode.fingerprints.ReVancedSettingsActivityFingerprint
|
2022-08-03 03:09:52 +02:00
|
|
|
import app.revanced.patches.youtube.misc.settings.bytecode.fingerprints.ThemeSetterFingerprint
|
2022-08-22 01:59:43 +02:00
|
|
|
import app.revanced.patches.youtube.misc.settings.framework.components.BasePreference
|
|
|
|
import app.revanced.patches.youtube.misc.settings.framework.components.impl.ArrayResource
|
|
|
|
import app.revanced.patches.youtube.misc.settings.framework.components.impl.Preference
|
|
|
|
import app.revanced.patches.youtube.misc.settings.framework.components.impl.PreferenceScreen
|
|
|
|
import app.revanced.patches.youtube.misc.settings.framework.components.impl.StringResource
|
2022-07-31 12:15:26 +02:00
|
|
|
import app.revanced.patches.youtube.misc.settings.resource.patch.SettingsResourcePatch
|
2022-08-22 01:59:43 +02:00
|
|
|
import org.jf.dexlib2.util.MethodUtil
|
|
|
|
import java.io.Closeable
|
2022-07-31 12:15:26 +02:00
|
|
|
|
|
|
|
@Patch
|
2022-08-03 03:53:35 +02:00
|
|
|
@DependsOn(
|
|
|
|
[
|
|
|
|
IntegrationsPatch::class,
|
|
|
|
SettingsResourcePatch::class,
|
|
|
|
]
|
|
|
|
)
|
2022-07-31 12:15:26 +02:00
|
|
|
@Name("settings")
|
|
|
|
@Description("Adds settings for ReVanced to YouTube.")
|
|
|
|
@SettingsCompatibility
|
|
|
|
@Version("0.0.1")
|
|
|
|
class SettingsPatch : BytecodePatch(
|
2022-08-03 03:09:52 +02:00
|
|
|
listOf(LicenseActivityFingerprint, ReVancedSettingsActivityFingerprint, ThemeSetterFingerprint)
|
2022-08-22 01:59:43 +02:00
|
|
|
), Closeable {
|
2022-07-31 12:15:26 +02:00
|
|
|
override fun execute(data: BytecodeData): PatchResult {
|
|
|
|
val licenseActivityResult = LicenseActivityFingerprint.result!!
|
|
|
|
val settingsResult = ReVancedSettingsActivityFingerprint.result!!
|
2022-08-03 03:09:52 +02:00
|
|
|
val themeSetterResult = ThemeSetterFingerprint.result!!
|
2022-07-31 12:15:26 +02:00
|
|
|
|
|
|
|
val licenseActivityClass = licenseActivityResult.mutableClass
|
|
|
|
val settingsClass = settingsResult.mutableClass
|
|
|
|
|
|
|
|
val onCreate = licenseActivityResult.mutableMethod
|
|
|
|
val setThemeMethodName = "setTheme"
|
|
|
|
val initializeSettings = settingsResult.mutableMethod
|
|
|
|
|
2022-08-03 03:09:52 +02:00
|
|
|
val setThemeInstruction =
|
|
|
|
"invoke-static {v0}, Lapp/revanced/integrations/utils/ThemeHelper;->setTheme(Ljava/lang/Object;)V".toInstruction(
|
|
|
|
themeSetterResult.mutableMethod
|
|
|
|
)
|
|
|
|
|
|
|
|
// add instructions to set the theme of the settings activity
|
|
|
|
themeSetterResult.mutableMethod.implementation!!.let {
|
|
|
|
it.addInstruction(
|
2022-09-21 04:11:16 +02:00
|
|
|
themeSetterResult.scanResult.patternScanResult!!.startIndex,
|
2022-08-03 03:09:52 +02:00
|
|
|
setThemeInstruction
|
|
|
|
)
|
|
|
|
|
|
|
|
it.addInstruction(
|
|
|
|
it.instructions.size - 1, // add before return
|
|
|
|
setThemeInstruction
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-22 01:59:43 +02:00
|
|
|
// add the setTheme call to the onCreate method to not affect the offsets
|
2022-07-31 12:15:26 +02:00
|
|
|
onCreate.addInstructions(
|
|
|
|
1,
|
|
|
|
"""
|
|
|
|
invoke-static { p0 }, ${settingsClass.type}->${initializeSettings.name}(${licenseActivityClass.type})V
|
|
|
|
return-void
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2022-08-22 01:59:43 +02:00
|
|
|
// add the initializeSettings call to the onCreate method
|
2022-07-31 12:15:26 +02:00
|
|
|
onCreate.addInstruction(
|
|
|
|
0,
|
|
|
|
"invoke-static { p0 }, ${settingsClass.type}->$setThemeMethodName(${licenseActivityClass.type})V"
|
|
|
|
)
|
2022-08-03 03:09:52 +02:00
|
|
|
|
2022-08-22 01:59:43 +02:00
|
|
|
// get rid of, now, useless overridden methods
|
|
|
|
licenseActivityResult.mutableClass.methods.removeIf { it.name != "onCreate" && !MethodUtil.isConstructor(it) }
|
|
|
|
|
2022-07-31 12:15:26 +02:00
|
|
|
return PatchResultSuccess()
|
|
|
|
}
|
2022-08-03 03:09:52 +02:00
|
|
|
|
|
|
|
internal companion object {
|
2022-08-22 01:59:43 +02:00
|
|
|
// TODO: hide this somehow
|
2022-09-26 04:12:09 +02:00
|
|
|
var appearanceStringId: Long = ResourceMappingResourcePatch.resourceMappings.find {
|
2022-08-03 03:09:52 +02:00
|
|
|
it.type == "string" && it.name == "app_theme_appearance_dark"
|
|
|
|
}!!.id
|
2022-08-22 01:59:43 +02:00
|
|
|
|
|
|
|
fun addString(identifier: String, value: String, formatted: Boolean = true) =
|
|
|
|
SettingsResourcePatch.addString(identifier, value, formatted)
|
|
|
|
|
|
|
|
fun addPreferenceScreen(preferenceScreen: app.revanced.patches.youtube.misc.settings.framework.components.impl.PreferenceScreen) =
|
|
|
|
SettingsResourcePatch.addPreferenceScreen(preferenceScreen)
|
|
|
|
|
|
|
|
fun addPreference(preference: Preference) =
|
|
|
|
SettingsResourcePatch.addPreference(preference)
|
|
|
|
|
|
|
|
fun addArray(arrayResource: ArrayResource) =
|
|
|
|
SettingsResourcePatch.addArray(arrayResource)
|
|
|
|
|
|
|
|
fun renameIntentsTargetPackage(newPackage: String) {
|
|
|
|
SettingsResourcePatch.overrideIntentsTargetPackage = newPackage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Preference screens patches should add their settings to.
|
|
|
|
*/
|
|
|
|
internal enum class PreferenceScreen(
|
|
|
|
private val key: String,
|
|
|
|
private val title: String,
|
|
|
|
private val summary: String? = null,
|
|
|
|
private val preferences: MutableList<BasePreference> = mutableListOf()
|
|
|
|
) : Closeable {
|
|
|
|
ADS("ads", "Ads", "Ad related settings"),
|
|
|
|
INTERACTIONS("interactions", "Interaction", "Settings related to interactions"),
|
|
|
|
LAYOUT("layout", "Layout", "Settings related to the layout"),
|
|
|
|
MISC("misc", "Miscellaneous", "Miscellaneous patches");
|
|
|
|
|
|
|
|
override fun close() {
|
|
|
|
if (preferences.size == 0) return
|
|
|
|
|
|
|
|
addPreferenceScreen(
|
|
|
|
PreferenceScreen(
|
|
|
|
key,
|
|
|
|
StringResource("${key}_title", title),
|
|
|
|
preferences,
|
|
|
|
summary?.let { summary ->
|
|
|
|
StringResource("${key}_summary", summary)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add preferences to the preference screen.
|
|
|
|
*/
|
|
|
|
fun addPreferences(vararg preferences: BasePreference) = this.preferences.addAll(preferences)
|
2022-08-03 03:09:52 +02:00
|
|
|
}
|
2022-08-22 01:59:43 +02:00
|
|
|
|
|
|
|
override fun close() = PreferenceScreen.values().forEach(PreferenceScreen::close)
|
|
|
|
|
2022-07-31 12:15:26 +02:00
|
|
|
}
|