Permalink
Browse files

[android] hack to workaround the use of AsyncTask.SERIAL_EXECUTOR ins…

…ide MoPub initialization
  • Loading branch information...
aldenml committed Aug 9, 2018
1 parent d750829 commit 591d9e9bd567843629409372906f6e4f34bf2c89
Showing with 25 additions and 0 deletions.
  1. +25 −0 android/src/com/frostwire/android/offers/MoPubAdNetwork.java
@@ -19,6 +19,7 @@

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;

@@ -34,12 +35,15 @@
import com.mopub.common.privacy.ConsentStatus;
import com.mopub.common.privacy.ConsentStatusChangeListener;
import com.mopub.common.privacy.PersonalInfoManager;
import com.mopub.common.util.Reflection;
import com.mopub.mobileads.AdMobBannerAdapter;
import com.mopub.mobileads.AdMobInterstitialAdapter;
import com.mopub.mobileads.MoPubErrorCode;
import com.mopub.mobileads.MoPubInterstitial;

import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@@ -87,7 +91,9 @@ public void initialize(Activity activity) {
new AdMobBannerAdapter.GooglePlayServicesMediationSettings(npaBundle),
new AdMobInterstitialAdapter.GooglePlayServicesMediationSettings(npaBundle))
.build();
fixExecutor(true);
MoPub.initializeSdk(activity, sdkConfiguration, () -> {
fixExecutor(false);
LOG.info("MoPub initialization finished");
starting = false;
start();
@@ -97,6 +103,25 @@ public void initialize(Activity activity) {
LOG.info("initialize() MoPub.initializeSdk invoked, starting=" + starting + ", started=" + started());
}

private void fixExecutor(boolean change) {
try {
LOG.info("MoPub -> fixExecutor with change=" + change);
Field f = Reflection.getPrivateField(AsyncTask.class, "sDefaultExecutor");

Field modifiersField = Field.class.getDeclaredField("accessFlags");
modifiersField.setAccessible(true);
modifiersField.setInt(f, f.getModifiers() & ~Modifier.FINAL);

if (change) {
f.set(null, AsyncTask.THREAD_POOL_EXECUTOR);
} else {
f.set(null, AsyncTask.SERIAL_EXECUTOR);
}
} catch (Exception e) {
LOG.info("MoPub -> fixExecutor error change=" + change + " msg=" + e.getMessage());
}
}

private static void loadConsentDialogAsync(MoPubAdNetwork mopubAdNetwork) {
PersonalInfoManager personalInfoManager = MoPub.getPersonalInformationManager();
//personalInfoManager.forceGdprApplies(); //uncomment to test in the US

3 comments on commit 591d9e9

@aldenml

This comment has been minimized.

Collaborator

aldenml replied Aug 9, 2018

@gubatron this works for me, let's discuss the solution tomorrow and prepare the report to MoPub

@gubatron

This comment has been minimized.

Collaborator

gubatron replied Aug 9, 2018

👍

@gubatron

This comment has been minimized.

Collaborator

gubatron replied Aug 9, 2018

I love you. it works! both after shutdown and after recent apps dismissal.

Please sign in to comment.