Skip to content

Commit 9b3c060

Browse files
authored
feat(YouTube - GmsCore): Require ignoring battery optimizations (#2952)
1 parent bbc4a6b commit 9b3c060

File tree

8 files changed

+54
-39
lines changed

8 files changed

+54
-39
lines changed

src/main/kotlin/app/revanced/patches/music/misc/gms/GmsCoreSupportPatch.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ object GmsCoreSupportPatch : BaseGmsCoreSupportPatch(
3434
PrimeMethodFingerprint,
3535
),
3636
) {
37-
override val gmsCoreVendor by gmsCoreVendorGroupIdOption
37+
override val gmsCoreVendorGroupId by gmsCoreVendorGroupIdOption
3838
}

src/main/kotlin/app/revanced/patches/shared/misc/gms/BaseGmsCoreSupportPatch.kt

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package app.revanced.patches.shared.misc.gms
22

33
import app.revanced.patcher.PatchClass
44
import app.revanced.patcher.data.BytecodeContext
5-
import app.revanced.patcher.extensions.InstructionExtensions.addInstruction
5+
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
66
import app.revanced.patcher.extensions.InstructionExtensions.getInstructions
77
import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction
88
import app.revanced.patcher.fingerprint.MethodFingerprint
@@ -12,7 +12,7 @@ import app.revanced.patches.shared.misc.gms.BaseGmsCoreSupportPatch.Constants.AC
1212
import app.revanced.patches.shared.misc.gms.BaseGmsCoreSupportPatch.Constants.AUTHORITIES
1313
import app.revanced.patches.shared.misc.gms.BaseGmsCoreSupportPatch.Constants.PERMISSIONS
1414
import app.revanced.patches.shared.misc.gms.fingerprints.GmsCoreSupportFingerprint
15-
import app.revanced.patches.shared.misc.gms.fingerprints.GmsCoreSupportFingerprint.GET_GMS_CORE_VENDOR_METHOD_NAME
15+
import app.revanced.patches.shared.misc.gms.fingerprints.GmsCoreSupportFingerprint.GET_GMS_CORE_VENDOR_GROUP_ID_METHOD_NAME
1616
import app.revanced.util.exception
1717
import app.revanced.util.getReference
1818
import app.revanced.util.returnEarly
@@ -32,7 +32,7 @@ import com.android.tools.smali.dexlib2.util.MethodUtil
3232
* @param toPackageName The package name to fall back to if no custom package name is specified in patch options.
3333
* @param primeMethodFingerprint The fingerprint of the "prime" method that needs to be patched.
3434
* @param earlyReturnFingerprints The fingerprints of methods that need to be returned early.
35-
* @param mainActivityOnCreateFingerprint The fingerprint of the main activity's onCreate method.
35+
* @param mainActivityOnCreateFingerprint The fingerprint of the main activity onCreate method.
3636
* @param integrationsPatchDependency The patch responsible for the integrations.
3737
* @param gmsCoreSupportResourcePatch The corresponding resource patch that is used to patch the resources.
3838
* @param dependencies Additional dependencies of this patch.
@@ -60,15 +60,18 @@ abstract class BaseGmsCoreSupportPatch(
6060
integrationsPatchDependency,
6161
) + dependencies,
6262
compatiblePackages = compatiblePackages,
63-
fingerprints = setOf(GmsCoreSupportFingerprint, mainActivityOnCreateFingerprint) + fingerprints,
63+
fingerprints = setOf(
64+
GmsCoreSupportFingerprint,
65+
mainActivityOnCreateFingerprint,
66+
) + fingerprints,
6467
requiresIntegrations = true,
6568
) {
6669
init {
6770
// Manually register all options of the resource patch so that they are visible in the patch API.
6871
gmsCoreSupportResourcePatch.options.values.forEach(options::register)
6972
}
7073

71-
internal abstract val gmsCoreVendor: String?
74+
internal abstract val gmsCoreVendorGroupId: String?
7275

7376
override fun execute(context: BytecodeContext) {
7477
val packageName = ChangePackageNamePatch.setOrGetFallbackPackageName(toPackageName)
@@ -93,16 +96,17 @@ abstract class BaseGmsCoreSupportPatch(
9396
// Return these methods early to prevent the app from crashing.
9497
earlyReturnFingerprints.toList().returnEarly()
9598

96-
// Check the availability of GmsCore.
97-
mainActivityOnCreateFingerprint.result?.mutableMethod?.addInstruction(
98-
1, // Hack to not disturb other patches (such as the integrations patch).
99-
"invoke-static {}, Lapp/revanced/integrations/shared/GmsCoreSupport;->checkAvailability()V",
99+
// Verify GmsCore is installed and whitelisted for power optimizations and background usage.
100+
mainActivityOnCreateFingerprint.result?.mutableMethod?.addInstructions(
101+
1, // Hack to not disturb other patches (such as the YTMusic integrations patch).
102+
"invoke-static/range { p0 .. p0 }, Lapp/revanced/integrations/shared/GmsCoreSupport;->" +
103+
"checkGmsCore(Landroid/content/Context;)V",
100104
) ?: throw mainActivityOnCreateFingerprint.exception
101105

102106
// Change the vendor of GmsCore in ReVanced Integrations.
103107
GmsCoreSupportFingerprint.result?.mutableClass?.methods
104-
?.single { it.name == GET_GMS_CORE_VENDOR_METHOD_NAME }
105-
?.replaceInstruction(0, "const-string v0, \"$gmsCoreVendor\"")
108+
?.single { it.name == GET_GMS_CORE_VENDOR_GROUP_ID_METHOD_NAME }
109+
?.replaceInstruction(0, "const-string v0, \"$gmsCoreVendorGroupId\"")
106110
?: throw GmsCoreSupportFingerprint.exception
107111
}
108112

@@ -146,10 +150,10 @@ abstract class BaseGmsCoreSupportPatch(
146150
in PERMISSIONS,
147151
in ACTIONS,
148152
in AUTHORITIES,
149-
-> referencedString.replace("com.google", gmsCoreVendor!!)
153+
-> referencedString.replace("com.google", gmsCoreVendorGroupId!!)
150154

151155
// No vendor prefix for whatever reason...
152-
"subscribedfeeds" -> "$gmsCoreVendor.subscribedfeeds"
156+
"subscribedfeeds" -> "$gmsCoreVendorGroupId.subscribedfeeds"
153157
else -> null
154158
}
155159

@@ -162,15 +166,15 @@ abstract class BaseGmsCoreSupportPatch(
162166
if (str.startsWith(uriPrefix)) {
163167
return str.replace(
164168
uriPrefix,
165-
"content://${authority.replace("com.google", gmsCoreVendor!!)}",
169+
"content://${authority.replace("com.google", gmsCoreVendorGroupId!!)}",
166170
)
167171
}
168172
}
169173

170174
// gms also has a 'subscribedfeeds' authority, check for that one too
171175
val subFeedsUriPrefix = "content://subscribedfeeds"
172176
if (str.startsWith(subFeedsUriPrefix)) {
173-
return str.replace(subFeedsUriPrefix, "content://$gmsCoreVendor.subscribedfeeds")
177+
return str.replace(subFeedsUriPrefix, "content://$gmsCoreVendorGroupId.subscribedfeeds")
174178
}
175179
}
176180

src/main/kotlin/app/revanced/patches/shared/misc/gms/BaseGmsCoreSupportResourcePatch.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ abstract class BaseGmsCoreSupportResourcePatch(
121121
}
122122

123123
private companion object {
124-
private const val VANCED_VENDOR = "com.mgoogle"
125124
private const val PACKAGE_NAME_REGEX_PATTERN = "^[a-z]\\w*(\\.[a-z]\\w*)+\$"
126125
}
127126
}

src/main/kotlin/app/revanced/patches/shared/misc/gms/fingerprints/GmsCoreSupportFingerprint.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import app.revanced.patcher.fingerprint.MethodFingerprint
55
internal object GmsCoreSupportFingerprint : MethodFingerprint(
66
customFingerprint = { _, classDef ->
77
classDef.type.endsWith("GmsCoreSupport;")
8-
}
8+
},
99
) {
10-
const val GET_GMS_CORE_VENDOR_METHOD_NAME = "getGmsCoreVendor"
10+
const val GET_GMS_CORE_VENDOR_GROUP_ID_METHOD_NAME = "getGmsCoreVendorGroupId"
1111
}

src/main/kotlin/app/revanced/patches/youtube/misc/announcements/AnnouncementsPatch.kt

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ package app.revanced.patches.youtube.misc.announcements
22

33
import app.revanced.patcher.data.BytecodeContext
44
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
5-
import app.revanced.patcher.extensions.InstructionExtensions.getInstructions
65
import app.revanced.patcher.patch.BytecodePatch
76
import app.revanced.patcher.patch.annotation.CompatiblePackage
87
import app.revanced.patcher.patch.annotation.Patch
98
import app.revanced.patches.all.misc.resources.AddResourcesPatch
109
import app.revanced.patches.shared.misc.settings.preference.SwitchPreference
1110
import app.revanced.patches.youtube.misc.settings.SettingsPatch
12-
import app.revanced.patches.youtube.shared.fingerprints.MainActivityFingerprint
13-
import app.revanced.util.exception
14-
import com.android.tools.smali.dexlib2.Opcode
11+
import app.revanced.patches.youtube.shared.fingerprints.MainActivityOnCreateFingerprint
12+
import app.revanced.util.resultOrThrow
1513

1614
@Patch(
1715
name = "Announcements",
@@ -21,7 +19,7 @@ import com.android.tools.smali.dexlib2.Opcode
2119
)
2220
@Suppress("unused")
2321
object AnnouncementsPatch : BytecodePatch(
24-
setOf(MainActivityFingerprint)
22+
setOf(MainActivityOnCreateFingerprint)
2523
) {
2624
private const val INTEGRATIONS_CLASS_DESCRIPTOR =
2725
"Lapp/revanced/integrations/youtube/patches/announcements/AnnouncementsPatch;"
@@ -33,16 +31,11 @@ object AnnouncementsPatch : BytecodePatch(
3331
SwitchPreference("revanced_announcements")
3432
)
3533

36-
val onCreateMethod = MainActivityFingerprint.result?.let {
37-
it.mutableClass.methods.find { method -> method.name == "onCreate" }
38-
} ?: throw MainActivityFingerprint.exception
39-
40-
val superCallIndex = onCreateMethod.getInstructions().indexOfFirst { it.opcode == Opcode.INVOKE_SUPER_RANGE }
41-
42-
onCreateMethod.addInstructions(
43-
superCallIndex + 1,
44-
"invoke-static { v1 }, $INTEGRATIONS_CLASS_DESCRIPTOR->showAnnouncement(Landroid/app/Activity;)V"
34+
MainActivityOnCreateFingerprint.resultOrThrow().mutableMethod.addInstructions(
35+
// Insert index must be great than the insert index used by GmsCoreSupport,
36+
// as both patch the same method and GmsCore check should be first.
37+
1,
38+
"invoke-static/range { p0 .. p0 }, $INTEGRATIONS_CLASS_DESCRIPTOR->showAnnouncement(Landroid/app/Activity;)V"
4539
)
46-
4740
}
4841
}

src/main/kotlin/app/revanced/patches/youtube/misc/gms/GmsCoreSupportPatch.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import app.revanced.patches.youtube.misc.gms.Constants.YOUTUBE_PACKAGE_NAME
99
import app.revanced.patches.youtube.misc.gms.GmsCoreSupportResourcePatch.gmsCoreVendorGroupIdOption
1010
import app.revanced.patches.youtube.misc.gms.fingerprints.*
1111
import app.revanced.patches.youtube.misc.integrations.IntegrationsPatch
12-
import app.revanced.patches.youtube.shared.fingerprints.HomeActivityFingerprint
12+
import app.revanced.patches.youtube.shared.fingerprints.MainActivityOnCreateFingerprint
1313

1414
@Suppress("unused")
1515
object GmsCoreSupportPatch : BaseGmsCoreSupportPatch(
@@ -23,7 +23,7 @@ object GmsCoreSupportPatch : BaseGmsCoreSupportPatch(
2323
CastDynamiteModuleV2Fingerprint,
2424
CastContextFetchFingerprint,
2525
),
26-
mainActivityOnCreateFingerprint = HomeActivityFingerprint,
26+
mainActivityOnCreateFingerprint = MainActivityOnCreateFingerprint,
2727
integrationsPatchDependency = IntegrationsPatch::class,
2828
dependencies = setOf(
2929
HideCastButtonPatch::class,
@@ -57,5 +57,5 @@ object GmsCoreSupportPatch : BaseGmsCoreSupportPatch(
5757
PrimeMethodFingerprint,
5858
),
5959
) {
60-
override val gmsCoreVendor by gmsCoreVendorGroupIdOption
60+
override val gmsCoreVendorGroupId by gmsCoreVendorGroupIdOption
6161
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package app.revanced.patches.youtube.shared.fingerprints
2+
3+
import app.revanced.patcher.fingerprint.MethodFingerprint
4+
5+
internal object MainActivityOnCreateFingerprint : MethodFingerprint(
6+
returnType = "V",
7+
parameters = listOf("Landroid/os/Bundle;"),
8+
customFingerprint = { methodDef, classDef ->
9+
methodDef.name == "onCreate" &&
10+
(classDef.type.endsWith("MainActivity;")
11+
// Old versions of YouTube called this class "WatchWhileActivity" instead.
12+
|| classDef.type.endsWith("WatchWhileActivity;"))
13+
}
14+
)

src/main/resources/addresources/values/strings.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
<string name="revanced_settings_import_failure_parse">Import failed: %s</string>
1414
</patch>
1515
<patch id="misc.gms.BaseGmsCoreSupportResourcePatch">
16-
<string name="gms_core_not_installed_warning">GmsCore is not installed. Please install.</string>
17-
<string name="gms_core_not_running_warning">GmsCore is failing to run. Please follow the \"Don\'t kill my app\" guide for GmsCore.</string>
16+
<string name="gms_core_toast_not_installed_message">GmsCore is not installed. Install it.</string>
17+
<string name="gms_core_toast_not_whitelisted_message">Follow the \"Don\'t kill my app\" guide for GmsCore.</string>
18+
<string name="gms_core_dialog_title">Action needed</string>
19+
<string name="gms_core_dialog_not_whitelisted_using_battery_optimizations_message">GmsCore is not whitelisted from battery optimization.\n\nFollow the \"Don\'t kill my app\" guide for GmsCore.</string>
20+
<string name="gms_core_dialog_not_whitelisted_not_allowed_in_background_message">GmsCore does not have permission to run in the background.\n\nFollow the \"Don\'t kill my app\" guide for GmsCore.</string>
21+
<string name="gms_core_dialog_ok_button_text">Open website</string>
1822
</patch>
1923
</app>
2024
<app id="youtube">
@@ -881,6 +885,7 @@
881885
<string name="revanced_announcements_summary_off">Announcements are not shown on startup</string>
882886
<string name="revanced_announcements_enabled_summary">Show announcements on startup</string>
883887
<string name="revanced_announcements_connection_failed">Failed connecting to announcements provider</string>
888+
<string name="revanced_announcements_dialog_dismiss">Dismiss</string>
884889
</patch>
885890
<patch id="misc.autorepeat.AutoRepeatPatch">
886891
<string name="revanced_auto_repeat_title">Enable auto-repeat</string>

0 commit comments

Comments
 (0)