From ad7e79ecdb5efb9a0fe185a066bba7d792bcf8a0 Mon Sep 17 00:00:00 2001 From: DaVinci9196 <150454414+DaVinci9196@users.noreply.github.com> Date: Sun, 15 Dec 2024 04:27:29 +0800 Subject: [PATCH] DynamiteModule: Added method (#2664) Co-authored-by: Marvin W --- .../gms/ads/dynamite/ModuleDescriptor.java | 17 +++++++++++++++++ .../gms/chimera/container/DynamiteContext.java | 7 +++++-- .../chimera/container/DynamiteModuleInfo.java | 11 +++++++++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/play-services-ads/core/src/main/java/com/google/android/gms/dynamite/descriptors/com/google/android/gms/ads/dynamite/ModuleDescriptor.java b/play-services-ads/core/src/main/java/com/google/android/gms/dynamite/descriptors/com/google/android/gms/ads/dynamite/ModuleDescriptor.java index 4583d77b57..f147bc76dd 100644 --- a/play-services-ads/core/src/main/java/com/google/android/gms/dynamite/descriptors/com/google/android/gms/ads/dynamite/ModuleDescriptor.java +++ b/play-services-ads/core/src/main/java/com/google/android/gms/dynamite/descriptors/com/google/android/gms/ads/dynamite/ModuleDescriptor.java @@ -5,10 +5,27 @@ package com.google.android.gms.dynamite.descriptors.com.google.android.gms.ads.dynamite; +import android.content.Context; +import android.content.ContextWrapper; +import android.webkit.WebSettings; import androidx.annotation.Keep; @Keep public class ModuleDescriptor { public static final String MODULE_ID = "com.google.android.gms.ads.dynamite"; public static final int MODULE_VERSION = 230500001; + + /** + * The ads module might try to access the user agent, requiring initialization on the main thread, + * which may result in deadlocks when invoked from any other thread. This only happens with microG, + * because we don't use the highly privileged SELinux Sandbox that regular Play Services uses + * (which allows apps to read the user-agent from Play Services instead of the WebView). To prevent + * the issue we pre-emptively initialize the WebView. + */ + public static void init(Context context) { + if (context instanceof ContextWrapper) { + context = ((ContextWrapper) context).getBaseContext(); + } + WebSettings.getDefaultUserAgent(context); + } } diff --git a/play-services-core/src/main/java/com/google/android/gms/chimera/container/DynamiteContext.java b/play-services-core/src/main/java/com/google/android/gms/chimera/container/DynamiteContext.java index cbc93d2777..f93fdae0d3 100644 --- a/play-services-core/src/main/java/com/google/android/gms/chimera/container/DynamiteContext.java +++ b/play-services-core/src/main/java/com/google/android/gms/chimera/container/DynamiteContext.java @@ -88,11 +88,14 @@ public static DynamiteContext create(String moduleId, Context originalContext) { DynamiteModuleInfo moduleInfo = new DynamiteModuleInfo(moduleId); Context gmsContext = originalContext.createPackageContext(Constants.GMS_PACKAGE_NAME, 0); Context originalAppContext = originalContext.getApplicationContext(); + DynamiteContext dynamiteContext; if (originalAppContext == null || originalAppContext == originalContext) { - return new DynamiteContext(moduleInfo, originalContext, gmsContext, null); + dynamiteContext = new DynamiteContext(moduleInfo, originalContext, gmsContext, null); } else { - return new DynamiteContext(moduleInfo, originalContext, gmsContext, new DynamiteContext(moduleInfo, originalAppContext, gmsContext, null)); + dynamiteContext = new DynamiteContext(moduleInfo, originalContext, gmsContext, new DynamiteContext(moduleInfo, originalAppContext, gmsContext, null)); } + moduleInfo.init(dynamiteContext); + return dynamiteContext; } catch (PackageManager.NameNotFoundException e) { Log.w(TAG, e); return null; diff --git a/play-services-core/src/main/java/com/google/android/gms/chimera/container/DynamiteModuleInfo.java b/play-services-core/src/main/java/com/google/android/gms/chimera/container/DynamiteModuleInfo.java index 637a269fca..bad67c00a5 100644 --- a/play-services-core/src/main/java/com/google/android/gms/chimera/container/DynamiteModuleInfo.java +++ b/play-services-core/src/main/java/com/google/android/gms/chimera/container/DynamiteModuleInfo.java @@ -8,8 +8,7 @@ import java.util.Collection; import java.util.Collections; -import static android.content.Context.CONTEXT_IGNORE_SECURITY; -import static android.content.Context.CONTEXT_INCLUDE_CODE; +import android.content.Context; public class DynamiteModuleInfo { private Class descriptor; @@ -51,4 +50,12 @@ public Collection getMergedClasses() { return Collections.emptySet(); } } + + public void init(Context dynamiteContext) { + try { + descriptor.getMethod("init", Context.class).invoke(null, dynamiteContext); + } catch (Exception e) { + // Ignore + } + } }