Skip to content

Commit c64e29e

Browse files
author
LisoUseInAIKyrios
committed
refactor(YouTube - Seekbar): Remove obsolete splash screen color code
1 parent b2dd008 commit c64e29e

File tree

4 files changed

+1
-243
lines changed

4 files changed

+1
-243
lines changed

extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/theme/ProgressBarDrawable.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/theme/SeekbarColorPatch.java

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import static app.revanced.extension.shared.Utils.clamp;
55
import static app.revanced.extension.youtube.patches.theme.ThemePatch.SplashScreenAnimationStyle;
66

7-
import android.content.res.Resources;
87
import android.graphics.Color;
9-
import android.graphics.drawable.AnimatedVectorDrawable;
108

119
import com.airbnb.lottie.LottieAnimationView;
1210

@@ -15,7 +13,6 @@
1513
import java.io.InputStream;
1614
import java.nio.charset.StandardCharsets;
1715
import java.util.Arrays;
18-
import java.util.Locale;
1916
import java.util.Scanner;
2017

2118
import app.revanced.extension.shared.Logger;
@@ -104,27 +101,6 @@ public static int getSeekbarColor() {
104101
return customSeekbarColor;
105102
}
106103

107-
private static int colorChannelTo3Bits(int channel8Bits) {
108-
final float channel3Bits = channel8Bits * 7 / 255f;
109-
110-
// If a color channel is near zero, then allow rounding up so values between
111-
// 0x12 and 0x23 will show as 0x24. But always round down when the channel is
112-
// near full saturation, otherwise rounding to nearest will cause all values
113-
// between 0xEC and 0xFE to always show as full saturation (0xFF).
114-
return channel3Bits < 6
115-
? Math.round(channel3Bits)
116-
: (int) channel3Bits;
117-
}
118-
119-
@SuppressWarnings("SameParameterValue")
120-
private static String get9BitStyleIdentifier(int color24Bit) {
121-
final int r3 = colorChannelTo3Bits(Color.red(color24Bit));
122-
final int g3 = colorChannelTo3Bits(Color.green(color24Bit));
123-
final int b3 = colorChannelTo3Bits(Color.blue(color24Bit));
124-
125-
return String.format(Locale.US, "splash_seekbar_color_style_%d_%d_%d", r3, g3, b3);
126-
}
127-
128104
/**
129105
* injection point.
130106
*/
@@ -135,36 +111,6 @@ public static boolean useLotteLaunchSplashScreen(boolean original) {
135111
return original; // false = drawable style, true = lottie style.
136112
}
137113

138-
/**
139-
* Injection point.
140-
* Old drawable style launch screen.
141-
*/
142-
public static void setSplashAnimationDrawableTheme(AnimatedVectorDrawable vectorDrawable) {
143-
// Alternatively a ColorMatrixColorFilter can be used to change the color of the drawable
144-
// without using any styles, but a color filter cannot selectively change the seekbar
145-
// while keeping the red YT logo untouched.
146-
// Even if the seekbar color xml value is changed to a completely different color (such as green),
147-
// a color filter still cannot be selectively applied when the drawable has more than 1 color.
148-
try {
149-
// Must set the color even if custom seekbar is off,
150-
// because the xml color was replaced with a themed value.
151-
String seekbarStyle = get9BitStyleIdentifier(customSeekbarColor);
152-
Logger.printDebug(() -> "Using splash seekbar style: " + seekbarStyle);
153-
154-
final int styleIdentifierDefault = Utils.getResourceIdentifierOrThrow(
155-
seekbarStyle,
156-
"style"
157-
);
158-
159-
Resources.Theme theme = Utils.getContext().getResources().newTheme();
160-
theme.applyStyle(styleIdentifierDefault, true);
161-
162-
vectorDrawable.applyTheme(theme);
163-
} catch (Exception ex) {
164-
Logger.printException(() -> "setSplashAnimationDrawableTheme failure", ex);
165-
}
166-
}
167-
168114
/**
169115
* Injection point.
170116
* Modern Lottie style animation.

patches/src/main/kotlin/app/revanced/patches/youtube/layout/seekbar/SeekbarColorPatch.kt

Lines changed: 1 addition & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package app.revanced.patches.youtube.layout.seekbar
22

33
import app.revanced.patcher.Fingerprint
4-
import app.revanced.patcher.extensions.InstructionExtensions.addInstruction
54
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
65
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
76
import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction
8-
import app.revanced.patcher.patch.PatchException
97
import app.revanced.patcher.patch.bytecodePatch
108
import app.revanced.patcher.patch.resourcePatch
119
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
@@ -17,17 +15,13 @@ import app.revanced.patches.shared.misc.mapping.resourceMappingPatch
1715
import app.revanced.patches.shared.misc.mapping.resourceMappings
1816
import app.revanced.patches.youtube.misc.extension.sharedExtensionPatch
1917
import app.revanced.patches.youtube.misc.playservice.is_19_34_or_greater
20-
import app.revanced.patches.youtube.misc.playservice.is_19_46_or_greater
2118
import app.revanced.patches.youtube.misc.playservice.is_19_49_or_greater
2219
import app.revanced.patches.youtube.misc.playservice.versionCheckPatch
2320
import app.revanced.patches.youtube.misc.settings.settingsPatch
2421
import app.revanced.patches.youtube.shared.mainActivityOnCreateFingerprint
25-
import app.revanced.util.copyXmlNode
26-
import app.revanced.util.findElementByAttributeValueOrThrow
2722
import app.revanced.util.findInstructionIndicesReversedOrThrow
2823
import app.revanced.util.getReference
2924
import app.revanced.util.indexOfFirstInstructionOrThrow
30-
import app.revanced.util.inputStreamFromBundledResource
3125
import app.revanced.util.insertLiteralOverride
3226
import com.android.tools.smali.dexlib2.AccessFlags
3327
import com.android.tools.smali.dexlib2.Opcode
@@ -38,9 +32,6 @@ import com.android.tools.smali.dexlib2.iface.instruction.TwoRegisterInstruction
3832
import com.android.tools.smali.dexlib2.iface.reference.MethodReference
3933
import com.android.tools.smali.dexlib2.immutable.ImmutableMethod
4034
import com.android.tools.smali.dexlib2.immutable.ImmutableMethodParameter
41-
import org.w3c.dom.Element
42-
import java.io.ByteArrayInputStream
43-
import kotlin.use
4435

4536
internal var reelTimeBarPlayedColorId = -1L
4637
private set
@@ -57,8 +48,6 @@ internal var ytTextSecondaryId = -1L
5748
internal var inlineTimeBarLiveSeekableRangeId = -1L
5849
private set
5950

60-
internal const val splashSeekbarColorAttributeName = "splash_custom_seekbar_color"
61-
6251
private val seekbarColorResourcePatch = resourcePatch {
6352
dependsOn(
6453
settingsPatch,
@@ -92,21 +81,6 @@ private val seekbarColorResourcePatch = resourcePatch {
9281
"inline_time_bar_live_seekable_range"
9382
]
9483

95-
// Modify the resume playback drawable and replace the progress bar with a custom drawable.
96-
document("res/drawable/resume_playback_progressbar_drawable.xml").use { document ->
97-
val layerList = document.getElementsByTagName("layer-list").item(0) as Element
98-
val progressNode = layerList.getElementsByTagName("item").item(1) as Element
99-
if (!progressNode.getAttributeNode("android:id").value.endsWith("progress")) {
100-
throw PatchException("Could not find progress bar")
101-
}
102-
val scaleNode = progressNode.getElementsByTagName("scale").item(0) as Element
103-
val shapeNode = scaleNode.getElementsByTagName("shape").item(0) as Element
104-
val replacementNode = document.createElement(
105-
"app.revanced.extension.youtube.patches.theme.ProgressBarDrawable",
106-
)
107-
scaleNode.replaceChild(replacementNode, shapeNode)
108-
}
109-
11084
ytYoutubeMagentaColorId = resourceMappings[
11185
"color",
11286
"yt_youtube_magenta",
@@ -115,99 +89,9 @@ private val seekbarColorResourcePatch = resourcePatch {
11589
"attr",
11690
"ytStaticBrandRed",
11791
]
118-
119-
// Add attribute and styles for splash screen custom color.
120-
// Using a style is the only way to selectively change just the seekbar fill color.
121-
//
122-
// Because the style colors must be hard coded for all color possibilities,
123-
// instead of allowing 24 bit color the style is restricted to 9-bit (3 bits per color channel)
124-
// and the style color closest to the users custom color is used for the splash screen.
125-
arrayOf(
126-
inputStreamFromBundledResource("seekbar/values", "attrs.xml")!! to "res/values/attrs.xml",
127-
ByteArrayInputStream(create9BitSeekbarColorStyles().toByteArray()) to "res/values/styles.xml"
128-
).forEach { (source, destination) ->
129-
"resources".copyXmlNode(
130-
document(source),
131-
document(destination),
132-
).close()
133-
}
134-
135-
fun setSplashDrawablePathFillColor(xmlFileNames: Iterable<String>, vararg resourceNames: String) {
136-
xmlFileNames.forEach { xmlFileName ->
137-
document(xmlFileName).use { document ->
138-
val childNodes = document.childNodes
139-
140-
resourceNames.forEach { elementId ->
141-
val element = childNodes.findElementByAttributeValueOrThrow(
142-
"android:name",
143-
elementId
144-
)
145-
146-
val attribute = "android:fillColor"
147-
if (!element.hasAttribute(attribute)) {
148-
throw PatchException("Could not find $attribute for $elementId")
149-
}
150-
151-
element.setAttribute(attribute, "?attr/$splashSeekbarColorAttributeName")
152-
}
153-
}
154-
}
155-
}
156-
157-
setSplashDrawablePathFillColor(
158-
listOf(
159-
"res/drawable/\$startup_animation_light__0.xml",
160-
"res/drawable/\$startup_animation_dark__0.xml"
161-
),
162-
"_R_G_L_10_G_D_0_P_0"
163-
)
164-
165-
if (!is_19_46_or_greater) {
166-
// Resources removed in 19.46+
167-
setSplashDrawablePathFillColor(
168-
listOf(
169-
"res/drawable/\$buenos_aires_animation_light__0.xml",
170-
"res/drawable/\$buenos_aires_animation_dark__0.xml"
171-
),
172-
"_R_G_L_8_G_D_0_P_0"
173-
)
174-
}
17592
}
17693
}
17794

178-
/**
179-
* Generate a style xml with all combinations of 9-bit colors.
180-
*/
181-
private fun create9BitSeekbarColorStyles(): String = StringBuilder().apply {
182-
append("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
183-
append("<resources>\n")
184-
185-
for (red in 0..7) {
186-
for (green in 0..7) {
187-
for (blue in 0..7) {
188-
val name = "${red}_${green}_${blue}"
189-
190-
fun roundTo3BitHex(channel8Bits: Int) =
191-
(channel8Bits * 255 / 7).toString(16).padStart(2, '0')
192-
val r = roundTo3BitHex(red)
193-
val g = roundTo3BitHex(green)
194-
val b = roundTo3BitHex(blue)
195-
val color = "#ff$r$g$b"
196-
197-
append(
198-
"""
199-
<style name="splash_seekbar_color_style_$name">
200-
<item name="$splashSeekbarColorAttributeName">$color</item>
201-
</style>
202-
"""
203-
)
204-
}
205-
}
206-
}
207-
208-
append("</resources>")
209-
}.toString()
210-
21195
private const val EXTENSION_CLASS_DESCRIPTOR = "Lapp/revanced/extension/youtube/patches/theme/SeekbarColorPatch;"
21296

21397
val seekbarColorPatch = bytecodePatch(
@@ -344,21 +228,6 @@ val seekbarColorPatch = bytecodePatch(
344228

345229
// Hook the splash animation to set the a seekbar color.
346230
mainActivityOnCreateFingerprint.method.apply {
347-
val drawableIndex = indexOfFirstInstructionOrThrow {
348-
val reference = getReference<MethodReference>()
349-
reference?.definingClass == "Landroid/widget/ImageView;"
350-
&& reference.name == "getDrawable"
351-
}
352-
val checkCastIndex = indexOfFirstInstructionOrThrow(drawableIndex, Opcode.CHECK_CAST)
353-
val drawableRegister = getInstruction<OneRegisterInstruction>(checkCastIndex).registerA
354-
355-
addInstruction(
356-
checkCastIndex + 1,
357-
"invoke-static { v$drawableRegister }, $EXTENSION_CLASS_DESCRIPTOR->" +
358-
"setSplashAnimationDrawableTheme(Landroid/graphics/drawable/AnimatedVectorDrawable;)V"
359-
)
360-
361-
// Replace the Lottie animation view setAnimation(int) call.
362231
val setAnimationIntMethodName = lottieAnimationViewSetAnimationIntFingerprint.originalMethod.name
363232

364233
findInstructionIndicesReversedOrThrow {
@@ -371,13 +240,11 @@ val seekbarColorPatch = bytecodePatch(
371240
replaceInstruction(
372241
index,
373242
"invoke-static { v${instruction.registerC}, v${instruction.registerD} }, " +
374-
"$EXTENSION_CLASS_DESCRIPTOR->setSplashAnimationLottie" +
375-
"(Lcom/airbnb/lottie/LottieAnimationView;I)V"
243+
"$EXTENSION_CLASS_DESCRIPTOR->setSplashAnimationLottie(Lcom/airbnb/lottie/LottieAnimationView;I)V"
376244
)
377245
}
378246
}
379247

380-
381248
// Add non obfuscated method aliases for `setAnimation(int)`
382249
// and `setAnimation(InputStream, String)` so extension code can call them.
383250
lottieAnimationViewSetAnimationIntFingerprint.classDef.methods.apply {

patches/src/main/resources/seekbar/values/attrs.xml

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)