Skip to content

Commit

Permalink
Add explainer, use Context instead of ContextWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
mar-v-in committed Dec 14, 2024
1 parent ec636fd commit e4323dc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import android.content.Context;
import android.content.ContextWrapper;
import android.util.Log;
import android.webkit.WebSettings;
import androidx.annotation.Keep;

Expand All @@ -16,9 +15,17 @@ public class ModuleDescriptor {
public static final String MODULE_ID = "com.google.android.gms.ads.dynamite";
public static final int MODULE_VERSION = 230500001;

public static void init(ContextWrapper context) {
Context baseContext = context.getBaseContext();
WebSettings.getDefaultUserAgent(baseContext);
Log.d("ModuleDescriptor", "init: context: " + baseContext);
/**
* 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +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.ContextWrapper;
import android.content.Context;

public class DynamiteModuleInfo {
private Class<?> descriptor;
Expand Down Expand Up @@ -53,9 +51,9 @@ public Collection<String> getMergedClasses() {
}
}

public void init(ContextWrapper dynamiteContext) {
public void init(Context dynamiteContext) {
try {
descriptor.getMethod("init", ContextWrapper.class).invoke(null, dynamiteContext);
descriptor.getMethod("init", Context.class).invoke(null, dynamiteContext);
} catch (Exception e) {
// Ignore
}
Expand Down

0 comments on commit e4323dc

Please sign in to comment.