Puzzle King v2.3.8版本的 MD5 值为:1734eb975c9d2843afa462aec387292e

以下内容为反编译后的 GoogleMediationAdapter.java 源代码,内容仅作参考


package com.applovin.mediation.adapters;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import com.applovin.impl.sdk.utils.AppLovinSdkExtraParameterKey;
import com.applovin.impl.sdk.utils.BundleUtils;
import com.applovin.mediation.MaxAdFormat;
import com.applovin.mediation.MaxReward;
import com.applovin.mediation.adapter.MaxAdViewAdapter;
import com.applovin.mediation.adapter.MaxAdapter;
import com.applovin.mediation.adapter.MaxAdapterError;
import com.applovin.mediation.adapter.MaxInterstitialAdapter;
import com.applovin.mediation.adapter.MaxRewardedAdapter;
import com.applovin.mediation.adapter.MaxRewardedInterstitialAdapter;
import com.applovin.mediation.adapter.MaxSignalProvider;
import com.applovin.mediation.adapter.listeners.MaxAdViewAdapterListener;
import com.applovin.mediation.adapter.listeners.MaxAppOpenAdapterListener;
import com.applovin.mediation.adapter.listeners.MaxInterstitialAdapterListener;
import com.applovin.mediation.adapter.listeners.MaxNativeAdAdapterListener;
import com.applovin.mediation.adapter.listeners.MaxRewardedAdapterListener;
import com.applovin.mediation.adapter.listeners.MaxRewardedInterstitialAdapterListener;
import com.applovin.mediation.adapter.listeners.MaxSignalCollectionListener;
import com.applovin.mediation.adapter.parameters.MaxAdapterInitializationParameters;
import com.applovin.mediation.adapter.parameters.MaxAdapterParameters;
import com.applovin.mediation.adapter.parameters.MaxAdapterResponseParameters;
import com.applovin.mediation.adapter.parameters.MaxAdapterSignalCollectionParameters;
import com.applovin.mediation.nativeAds.MaxNativeAd;
import com.applovin.mediation.nativeAds.MaxNativeAdView;
import com.applovin.sdk.AppLovinSdk;
import com.applovin.sdk.AppLovinSdkConfiguration;
import com.applovin.sdk.AppLovinSdkUtils;
import com.facebook.internal.ServerProtocol;
import com.google.ads.mediation.admob.AdMobAdapter;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdFormat;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdLoader;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MediaContent;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.OnUserEarnedRewardListener;
import com.google.android.gms.ads.RequestConfiguration;
import com.google.android.gms.ads.ResponseInfo;
import com.google.android.gms.ads.appopen.AppOpenAd;
import com.google.android.gms.ads.initialization.AdapterStatus;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
import com.google.android.gms.ads.nativead.MediaView;
import com.google.android.gms.ads.nativead.NativeAd;
import com.google.android.gms.ads.nativead.NativeAdOptions;
import com.google.android.gms.ads.nativead.NativeAdView;
import com.google.android.gms.ads.query.QueryInfo;
import com.google.android.gms.ads.query.QueryInfoGenerationCallback;
import com.google.android.gms.ads.rewarded.RewardItem;
import com.google.android.gms.ads.rewarded.RewardedAd;
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback;
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd;
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoadCallback;
import com.google.firebase.analytics.FirebaseAnalytics;
import java.lang.ref.WeakReference;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

public class GoogleMediationAdapter extends MediationAdapterBase implements MaxSignalProvider, MaxInterstitialAdapter, MaxRewardedInterstitialAdapter, MaxRewardedAdapter, MaxAdViewAdapter {
    private static final AtomicBoolean initialized = new AtomicBoolean();
    private static MaxAdapter.InitializationStatus status;
    private AdView adView;
    private AppOpenAd appOpenAd;
    private AppOpenAdListener appOpenAdListener;
    private InterstitialAd appOpenInterstitialAd;
    private AppOpenAdListener appOpenInterstitialAdListener;
    private InterstitialAd interstitialAd;
    private NativeAd nativeAd;
    private NativeAdView nativeAdView;
    private RewardedAd rewardedAd;
    private RewardedAdListener rewardedAdListener;
    private RewardedInterstitialAd rewardedInterstitialAd;
    private RewardedInterstitialAdListener rewardedInterstitialAdListener;

    @Override
    public String getAdapterVersion() {
        return "21.2.0.2";
    }

    public GoogleMediationAdapter(AppLovinSdk appLovinSdk) {
        super(appLovinSdk);
    }

    @Override
    public void initialize(MaxAdapterInitializationParameters maxAdapterInitializationParameters, Activity activity, final MaxAdapter.OnCompletionListener onCompletionListener) {
        log("Initializing Google SDK...");
        if (initialized.compareAndSet(false, true)) {
            Context context = getContext(activity);
            MobileAds.disableMediationAdapterInitialization(context);
            if (maxAdapterInitializationParameters.getServerParameters().getBoolean("init_without_callback", false)) {
                status = MaxAdapter.InitializationStatus.DOES_NOT_APPLY;
                MobileAds.initialize(context);
                onCompletionListener.onCompletion(status, null);
                return;
            } else {
                status = MaxAdapter.InitializationStatus.INITIALIZING;
                MobileAds.initialize(context, new OnInitializationCompleteListener() {
                    @Override
                    public void onInitializationComplete(InitializationStatus initializationStatus) {
                        AdapterStatus adapterStatus = initializationStatus.getAdapterStatusMap().get("com.google.android.gms.ads.MobileAds");
                        AdapterStatus.State initializationState = adapterStatus != null ? adapterStatus.getInitializationState() : null;
                        GoogleMediationAdapter.this.log("Initialization complete with status " + initializationState);
                        MaxAdapter.InitializationStatus unused = GoogleMediationAdapter.status = AdapterStatus.State.READY == initializationState ? MaxAdapter.InitializationStatus.INITIALIZED_SUCCESS : MaxAdapter.InitializationStatus.INITIALIZED_UNKNOWN;
                        onCompletionListener.onCompletion(GoogleMediationAdapter.status, null);
                    }
                });
                return;
            }
        }
        onCompletionListener.onCompletion(status, null);
    }

    @Override
    public String getSdkVersion() {
        return status != null ? String.valueOf(MobileAds.getVersion()) : getAdapterVersion().substring(0, getAdapterVersion().lastIndexOf(46));
    }

    @Override
    public void onDestroy() {
        log("Destroy called for adapter " + this);
        InterstitialAd interstitialAd = this.interstitialAd;
        if (interstitialAd != null) {
            interstitialAd.setFullScreenContentCallback(null);
            this.interstitialAd = null;
        }
        AppOpenAd appOpenAd = this.appOpenAd;
        if (appOpenAd != null) {
            appOpenAd.setFullScreenContentCallback(null);
            this.appOpenAd = null;
            this.appOpenAdListener = null;
        }
        InterstitialAd interstitialAd2 = this.appOpenInterstitialAd;
        if (interstitialAd2 != null) {
            interstitialAd2.setFullScreenContentCallback(null);
            this.appOpenInterstitialAd = null;
            this.appOpenInterstitialAdListener = null;
        }
        RewardedInterstitialAd rewardedInterstitialAd = this.rewardedInterstitialAd;
        if (rewardedInterstitialAd != null) {
            rewardedInterstitialAd.setFullScreenContentCallback(null);
            this.rewardedInterstitialAd = null;
            this.rewardedInterstitialAdListener = null;
        }
        RewardedAd rewardedAd = this.rewardedAd;
        if (rewardedAd != null) {
            rewardedAd.setFullScreenContentCallback(null);
            this.rewardedAd = null;
            this.rewardedAdListener = null;
        }
        AdView adView = this.adView;
        if (adView != null) {
            adView.destroy();
            this.adView = null;
        }
        NativeAd nativeAd = this.nativeAd;
        if (nativeAd != null) {
            nativeAd.destroy();
            this.nativeAd = null;
        }
        NativeAdView nativeAdView = this.nativeAdView;
        if (nativeAdView != null) {
            nativeAdView.destroy();
            this.nativeAdView = null;
        }
    }

    @Override
    public void collectSignal(MaxAdapterSignalCollectionParameters maxAdapterSignalCollectionParameters, Activity activity, final MaxSignalCollectionListener maxSignalCollectionListener) {
        setRequestConfiguration(maxAdapterSignalCollectionParameters);
        Context context = getContext(activity);
        QueryInfo.generate(context, toAdFormat(maxAdapterSignalCollectionParameters), createAdRequestWithParameters(true, maxAdapterSignalCollectionParameters.getAdFormat(), maxAdapterSignalCollectionParameters, context), new QueryInfoGenerationCallback() {
            @Override
            public void onSuccess(QueryInfo queryInfo) {
                GoogleMediationAdapter.this.log("Signal collection successful");
                maxSignalCollectionListener.onSignalCollected(queryInfo.getQuery());
            }

            @Override
            public void onFailure(String str) {
                GoogleMediationAdapter.this.log("Signal collection failed with error: " + str);
                maxSignalCollectionListener.onSignalCollectionFailed(str);
            }
        });
    }

    @Override
    public void loadInterstitialAd(MaxAdapterResponseParameters maxAdapterResponseParameters, Activity activity, final MaxInterstitialAdapterListener maxInterstitialAdapterListener) {
        final String thirdPartyAdPlacementId = maxAdapterResponseParameters.getThirdPartyAdPlacementId();
        boolean isValidString = AppLovinSdkUtils.isValidString(maxAdapterResponseParameters.getBidResponse());
        StringBuilder sb = new StringBuilder();
        sb.append("Loading ");
        sb.append(isValidString ? "bidding " : "");
        sb.append("interstitial ad: ");
        sb.append(thirdPartyAdPlacementId);
        sb.append("...");
        log(sb.toString());
        updateMuteState(maxAdapterResponseParameters);
        setRequestConfiguration(maxAdapterResponseParameters);
        InterstitialAd.load(activity, thirdPartyAdPlacementId, createAdRequestWithParameters(isValidString, MaxAdFormat.INTERSTITIAL, maxAdapterResponseParameters, activity), new InterstitialAdLoadCallback() {
            @Override
            public void onAdLoaded(InterstitialAd interstitialAd) {
                GoogleMediationAdapter.this.log("Interstitial ad loaded: " + thirdPartyAdPlacementId + "...");
                GoogleMediationAdapter.this.interstitialAd = interstitialAd;
                GoogleMediationAdapter.this.interstitialAd.setFullScreenContentCallback(new InterstitialAdListener(thirdPartyAdPlacementId, maxInterstitialAdapterListener));
                ResponseInfo responseInfo = GoogleMediationAdapter.this.interstitialAd.getResponseInfo();
                String responseId = responseInfo != null ? responseInfo.getResponseId() : null;
                if (AppLovinSdk.VERSION_CODE >= 9150000 && AppLovinSdkUtils.isValidString(responseId)) {
                    Bundle bundle = new Bundle(1);
                    bundle.putString("creative_id", responseId);
                    maxInterstitialAdapterListener.onInterstitialAdLoaded(bundle);
                    return;
                }
                maxInterstitialAdapterListener.onInterstitialAdLoaded();
            }

            @Override
            public void onAdFailedToLoad(LoadAdError loadAdError) {
                MaxAdapterError maxError = GoogleMediationAdapter.toMaxError(loadAdError);
                GoogleMediationAdapter.this.log("Interstitial ad (" + thirdPartyAdPlacementId + ") failed to load with error: " + maxError);
                maxInterstitialAdapterListener.onInterstitialAdLoadFailed(maxError);
            }
        });
    }

    @Override
    public void showInterstitialAd(MaxAdapterResponseParameters maxAdapterResponseParameters, Activity activity, MaxInterstitialAdapterListener maxInterstitialAdapterListener) {
        String thirdPartyAdPlacementId = maxAdapterResponseParameters.getThirdPartyAdPlacementId();
        log("Showing interstitial ad: " + thirdPartyAdPlacementId + "...");
        InterstitialAd interstitialAd = this.interstitialAd;
        if (interstitialAd != null) {
            interstitialAd.show(activity);
            return;
        }
        log("Interstitial ad failed to show: " + thirdPartyAdPlacementId);
        maxInterstitialAdapterListener.onInterstitialAdDisplayFailed(new MaxAdapterError(MaxAdapterError.ERROR_CODE_AD_DISPLAY_FAILED, "Ad Display Failed"));
    }

    @Override
    public void loadAppOpenAd(MaxAdapterResponseParameters maxAdapterResponseParameters, Activity activity, final MaxAppOpenAdapterListener maxAppOpenAdapterListener) {
        final String thirdPartyAdPlacementId = maxAdapterResponseParameters.getThirdPartyAdPlacementId();
        boolean isValidString = AppLovinSdkUtils.isValidString(maxAdapterResponseParameters.getBidResponse());
        boolean z = maxAdapterResponseParameters.getServerParameters().getBoolean("is_inter_placement");
        StringBuilder sb = new StringBuilder();
        sb.append("Loading ");
        sb.append(isValidString ? "bidding " : "");
        sb.append("app open ");
        sb.append(z ? "interstitial " : "");
        sb.append("ad: ");
        sb.append(thirdPartyAdPlacementId);
        sb.append("...");
        log(sb.toString());
        updateMuteState(maxAdapterResponseParameters);
        setRequestConfiguration(maxAdapterResponseParameters);
        Context context = getContext(activity);
        if (z) {
            InterstitialAd.load(context, thirdPartyAdPlacementId, createAdRequestWithParameters(isValidString, MaxAdFormat.INTERSTITIAL, maxAdapterResponseParameters, activity), new InterstitialAdLoadCallback() {
                @Override
                public void onAdLoaded(InterstitialAd interstitialAd) {
                    GoogleMediationAdapter.this.log("App open interstitial ad loaded: " + thirdPartyAdPlacementId + "...");
                    GoogleMediationAdapter.this.appOpenInterstitialAd = interstitialAd;
                    GoogleMediationAdapter googleMediationAdapter = GoogleMediationAdapter.this;
                    googleMediationAdapter.appOpenInterstitialAdListener = new AppOpenAdListener(thirdPartyAdPlacementId, maxAppOpenAdapterListener);
                    GoogleMediationAdapter.this.appOpenInterstitialAd.setFullScreenContentCallback(GoogleMediationAdapter.this.appOpenInterstitialAdListener);
                    ResponseInfo responseInfo = GoogleMediationAdapter.this.appOpenInterstitialAd.getResponseInfo();
                    String responseId = responseInfo != null ? responseInfo.getResponseId() : null;
                    if (AppLovinSdkUtils.isValidString(responseId)) {
                        Bundle bundle = new Bundle(1);
                        bundle.putString("creative_id", responseId);
                        maxAppOpenAdapterListener.onAppOpenAdLoaded(bundle);
                        return;
                    }
                    maxAppOpenAdapterListener.onAppOpenAdLoaded();
                }

                @Override
                public void onAdFailedToLoad(LoadAdError loadAdError) {
                    MaxAdapterError maxError = GoogleMediationAdapter.toMaxError(loadAdError);
                    GoogleMediationAdapter.this.log("App open interstitial ad (" + thirdPartyAdPlacementId + ") failed to load with error: " + maxError);
                    maxAppOpenAdapterListener.onAppOpenAdLoadFailed(maxError);
                }
            });
        } else {
            AppOpenAd.load(context, thirdPartyAdPlacementId, createAdRequestWithParameters(isValidString, MaxAdFormat.APP_OPEN, maxAdapterResponseParameters, activity), AppLovinSdkUtils.getOrientation(context), new AppOpenAd.AppOpenAdLoadCallback() {
                @Override
                public void onAdLoaded(AppOpenAd appOpenAd) {
                    GoogleMediationAdapter.this.log("App open ad loaded: " + thirdPartyAdPlacementId + "...");
                    GoogleMediationAdapter.this.appOpenAd = appOpenAd;
                    GoogleMediationAdapter googleMediationAdapter = GoogleMediationAdapter.this;
                    googleMediationAdapter.appOpenAdListener = new AppOpenAdListener(thirdPartyAdPlacementId, maxAppOpenAdapterListener);
                    appOpenAd.setFullScreenContentCallback(GoogleMediationAdapter.this.appOpenAdListener);
                    ResponseInfo responseInfo = GoogleMediationAdapter.this.appOpenAd.getResponseInfo();
                    String responseId = responseInfo != null ? responseInfo.getResponseId() : null;
                    if (AppLovinSdkUtils.isValidString(responseId)) {
                        Bundle bundle = new Bundle(1);
                        bundle.putString("creative_id", responseId);
                        maxAppOpenAdapterListener.onAppOpenAdLoaded(bundle);
                        return;
                    }
                    maxAppOpenAdapterListener.onAppOpenAdLoaded();
                }

                @Override
                public void onAdFailedToLoad(LoadAdError loadAdError) {
                    MaxAdapterError maxError = GoogleMediationAdapter.toMaxError(loadAdError);
                    GoogleMediationAdapter.this.log("App open ad (" + thirdPartyAdPlacementId + ") failed to load with error: " + maxError);
                    maxAppOpenAdapterListener.onAppOpenAdLoadFailed(maxError);
                }
            });
        }
    }

    @Override
    public void showAppOpenAd(MaxAdapterResponseParameters maxAdapterResponseParameters, Activity activity, MaxAppOpenAdapterListener maxAppOpenAdapterListener) {
        String thirdPartyAdPlacementId = maxAdapterResponseParameters.getThirdPartyAdPlacementId();
        boolean z = maxAdapterResponseParameters.getServerParameters().getBoolean("is_inter_placement");
        StringBuilder sb = new StringBuilder();
        sb.append("Showing app open ");
        sb.append(z ? "interstitial " : "");
        sb.append("ad: ");
        sb.append(thirdPartyAdPlacementId);
        sb.append("...");
        log(sb.toString());
        InterstitialAd interstitialAd = this.appOpenInterstitialAd;
        if (interstitialAd != null) {
            interstitialAd.show(activity);
            return;
        }
        AppOpenAd appOpenAd = this.appOpenAd;
        if (appOpenAd != null) {
            appOpenAd.show(activity);
            return;
        }
        log("App open ad failed to show: " + thirdPartyAdPlacementId);
        maxAppOpenAdapterListener.onAppOpenAdLoadFailed(new MaxAdapterError(MaxAdapterError.ERROR_CODE_AD_DISPLAY_FAILED, "Ad display failed"));
    }

    @Override
    public void loadRewardedInterstitialAd(MaxAdapterResponseParameters maxAdapterResponseParameters, Activity activity, final MaxRewardedInterstitialAdapterListener maxRewardedInterstitialAdapterListener) {
        final String thirdPartyAdPlacementId = maxAdapterResponseParameters.getThirdPartyAdPlacementId();
        boolean isValidString = AppLovinSdkUtils.isValidString(maxAdapterResponseParameters.getBidResponse());
        StringBuilder sb = new StringBuilder();
        sb.append("Loading ");
        sb.append(isValidString ? "bidding " : "");
        sb.append("rewarded interstitial ad: ");
        sb.append(thirdPartyAdPlacementId);
        sb.append("...");
        log(sb.toString());
        updateMuteState(maxAdapterResponseParameters);
        setRequestConfiguration(maxAdapterResponseParameters);
        RewardedInterstitialAd.load(activity, thirdPartyAdPlacementId, createAdRequestWithParameters(isValidString, MaxAdFormat.REWARDED_INTERSTITIAL, maxAdapterResponseParameters, activity), new RewardedInterstitialAdLoadCallback() {
            @Override
            public void onAdLoaded(RewardedInterstitialAd rewardedInterstitialAd) {
                GoogleMediationAdapter.this.log("Rewarded interstitial ad loaded: " + thirdPartyAdPlacementId);
                GoogleMediationAdapter.this.rewardedInterstitialAd = rewardedInterstitialAd;
                GoogleMediationAdapter googleMediationAdapter = GoogleMediationAdapter.this;
                googleMediationAdapter.rewardedInterstitialAdListener = new RewardedInterstitialAdListener(thirdPartyAdPlacementId, maxRewardedInterstitialAdapterListener);
                GoogleMediationAdapter.this.rewardedInterstitialAd.setFullScreenContentCallback(GoogleMediationAdapter.this.rewardedInterstitialAdListener);
                ResponseInfo responseInfo = GoogleMediationAdapter.this.rewardedInterstitialAd.getResponseInfo();
                String responseId = responseInfo != null ? responseInfo.getResponseId() : null;
                if (AppLovinSdk.VERSION_CODE > 9150000 && AppLovinSdkUtils.isValidString(responseId)) {
                    Bundle bundle = new Bundle(1);
                    bundle.putString("creative_id", responseId);
                    maxRewardedInterstitialAdapterListener.onRewardedInterstitialAdLoaded(bundle);
                    return;
                }
                maxRewardedInterstitialAdapterListener.onRewardedInterstitialAdLoaded();
            }

            @Override
            public void onAdFailedToLoad(LoadAdError loadAdError) {
                MaxAdapterError maxError = GoogleMediationAdapter.toMaxError(loadAdError);
                GoogleMediationAdapter.this.log("Rewarded interstitial ad (" + thirdPartyAdPlacementId + ") failed to load with error: " + maxError);
                maxRewardedInterstitialAdapterListener.onRewardedInterstitialAdLoadFailed(maxError);
            }
        });
    }

    @Override
    public void showRewardedInterstitialAd(MaxAdapterResponseParameters maxAdapterResponseParameters, Activity activity, MaxRewardedInterstitialAdapterListener maxRewardedInterstitialAdapterListener) {
        final String thirdPartyAdPlacementId = maxAdapterResponseParameters.getThirdPartyAdPlacementId();
        log("Showing rewarded interstitial ad: " + thirdPartyAdPlacementId + "...");
        if (this.rewardedInterstitialAd != null) {
            configureReward(maxAdapterResponseParameters);
            this.rewardedInterstitialAd.show(activity, new OnUserEarnedRewardListener() {
                @Override
                public void onUserEarnedReward(RewardItem rewardItem) {
                    GoogleMediationAdapter.this.log("Rewarded interstitial ad user earned reward: " + thirdPartyAdPlacementId);
                    GoogleMediationAdapter.this.rewardedInterstitialAdListener.hasGrantedReward = true;
                }
            });
            return;
        }
        log("Rewarded interstitial ad failed to show: " + thirdPartyAdPlacementId);
        maxRewardedInterstitialAdapterListener.onRewardedInterstitialAdDisplayFailed(new MaxAdapterError(MaxAdapterError.ERROR_CODE_AD_DISPLAY_FAILED, "Ad Display Failed"));
    }

    @Override
    public void loadRewardedAd(MaxAdapterResponseParameters maxAdapterResponseParameters, Activity activity, final MaxRewardedAdapterListener maxRewardedAdapterListener) {
        final String thirdPartyAdPlacementId = maxAdapterResponseParameters.getThirdPartyAdPlacementId();
        boolean isValidString = AppLovinSdkUtils.isValidString(maxAdapterResponseParameters.getBidResponse());
        StringBuilder sb = new StringBuilder();
        sb.append("Loading ");
        sb.append(isValidString ? "bidding " : "");
        sb.append("rewarded ad: ");
        sb.append(thirdPartyAdPlacementId);
        sb.append("...");
        log(sb.toString());
        updateMuteState(maxAdapterResponseParameters);
        setRequestConfiguration(maxAdapterResponseParameters);
        RewardedAd.load(activity, thirdPartyAdPlacementId, createAdRequestWithParameters(isValidString, MaxAdFormat.REWARDED, maxAdapterResponseParameters, activity), new RewardedAdLoadCallback() {
            @Override
            public void onAdLoaded(RewardedAd rewardedAd) {
                GoogleMediationAdapter.this.log("Rewarded ad loaded: " + thirdPartyAdPlacementId + "...");
                GoogleMediationAdapter.this.rewardedAd = rewardedAd;
                GoogleMediationAdapter googleMediationAdapter = GoogleMediationAdapter.this;
                googleMediationAdapter.rewardedAdListener = new RewardedAdListener(thirdPartyAdPlacementId, maxRewardedAdapterListener);
                GoogleMediationAdapter.this.rewardedAd.setFullScreenContentCallback(GoogleMediationAdapter.this.rewardedAdListener);
                ResponseInfo responseInfo = GoogleMediationAdapter.this.rewardedAd.getResponseInfo();
                String responseId = responseInfo != null ? responseInfo.getResponseId() : null;
                if (AppLovinSdk.VERSION_CODE >= 9150000 && AppLovinSdkUtils.isValidString(responseId)) {
                    Bundle bundle = new Bundle(1);
                    bundle.putString("creative_id", responseId);
                    maxRewardedAdapterListener.onRewardedAdLoaded(bundle);
                    return;
                }
                maxRewardedAdapterListener.onRewardedAdLoaded();
            }

            @Override
            public void onAdFailedToLoad(LoadAdError loadAdError) {
                MaxAdapterError maxError = GoogleMediationAdapter.toMaxError(loadAdError);
                GoogleMediationAdapter.this.log("Rewarded ad (" + thirdPartyAdPlacementId + ") failed to load with error: " + maxError);
                maxRewardedAdapterListener.onRewardedAdLoadFailed(maxError);
            }
        });
    }

    @Override
    public void showRewardedAd(MaxAdapterResponseParameters maxAdapterResponseParameters, Activity activity, MaxRewardedAdapterListener maxRewardedAdapterListener) {
        final String thirdPartyAdPlacementId = maxAdapterResponseParameters.getThirdPartyAdPlacementId();
        log("Showing rewarded ad: " + thirdPartyAdPlacementId + "...");
        if (this.rewardedAd != null) {
            configureReward(maxAdapterResponseParameters);
            this.rewardedAd.show(activity, new OnUserEarnedRewardListener() {
                @Override
                public void onUserEarnedReward(RewardItem rewardItem) {
                    GoogleMediationAdapter.this.log("Rewarded ad user earned reward: " + thirdPartyAdPlacementId);
                    GoogleMediationAdapter.this.rewardedAdListener.hasGrantedReward = true;
                }
            });
            return;
        }
        log("Rewarded ad failed to show: " + thirdPartyAdPlacementId);
        maxRewardedAdapterListener.onRewardedAdDisplayFailed(new MaxAdapterError(MaxAdapterError.ERROR_CODE_AD_DISPLAY_FAILED, "Ad Display Failed"));
    }

    @Override
    public void loadAdViewAd(MaxAdapterResponseParameters maxAdapterResponseParameters, MaxAdFormat maxAdFormat, Activity activity, MaxAdViewAdapterListener maxAdViewAdapterListener) {
        String thirdPartyAdPlacementId = maxAdapterResponseParameters.getThirdPartyAdPlacementId();
        boolean isValidString = AppLovinSdkUtils.isValidString(maxAdapterResponseParameters.getBidResponse());
        boolean z = maxAdapterResponseParameters.getServerParameters().getBoolean("is_native");
        StringBuilder sb = new StringBuilder();
        sb.append("Loading ");
        sb.append(isValidString ? "bidding " : "");
        sb.append(z ? "native " : "");
        sb.append(maxAdFormat.getLabel());
        sb.append(" ad for placement id: ");
        sb.append(thirdPartyAdPlacementId);
        sb.append("...");
        log(sb.toString());
        setRequestConfiguration(maxAdapterResponseParameters);
        Context context = getContext(activity);
        AdRequest createAdRequestWithParameters = createAdRequestWithParameters(isValidString, maxAdFormat, maxAdapterResponseParameters, context);
        if (z) {
            NativeAdOptions.Builder builder = new NativeAdOptions.Builder();
            builder.setAdChoicesPlacement(getAdChoicesPlacement(maxAdapterResponseParameters));
            builder.setRequestMultipleImages(maxAdFormat == MaxAdFormat.MREC);
            NativeAdViewListener nativeAdViewListener = new NativeAdViewListener(maxAdapterResponseParameters, maxAdFormat, activity, maxAdViewAdapterListener);
            new AdLoader.Builder(context, thirdPartyAdPlacementId).withNativeAdOptions(builder.build()).forNativeAd(nativeAdViewListener).withAdListener(nativeAdViewListener).build().loadAd(createAdRequestWithParameters);
            return;
        }
        AdView adView = new AdView(context);
        this.adView = adView;
        adView.setAdUnitId(thirdPartyAdPlacementId);
        this.adView.setAdListener(new AdViewListener(thirdPartyAdPlacementId, maxAdFormat, maxAdViewAdapterListener));
        this.adView.setAdSize(toAdSize(maxAdFormat, maxAdapterResponseParameters.getServerParameters().getBoolean(AppLovinSdkExtraParameterKey.IS_ADAPTIVE_BANNER, false), context));
        this.adView.loadAd(createAdRequestWithParameters);
    }

    @Override
    public void loadNativeAd(MaxAdapterResponseParameters maxAdapterResponseParameters, Activity activity, MaxNativeAdAdapterListener maxNativeAdAdapterListener) {
        String thirdPartyAdPlacementId = maxAdapterResponseParameters.getThirdPartyAdPlacementId();
        boolean isValidString = AppLovinSdkUtils.isValidString(maxAdapterResponseParameters.getBidResponse());
        StringBuilder sb = new StringBuilder();
        sb.append("Loading ");
        sb.append(isValidString ? "bidding " : "");
        sb.append(" native ad for placement id: ");
        sb.append(thirdPartyAdPlacementId);
        sb.append("...");
        log(sb.toString());
        setRequestConfiguration(maxAdapterResponseParameters);
        Context applicationContext = activity != null ? activity.getApplicationContext() : getApplicationContext();
        AdRequest createAdRequestWithParameters = createAdRequestWithParameters(isValidString, MaxAdFormat.NATIVE, maxAdapterResponseParameters, applicationContext);
        NativeAdOptions.Builder builder = new NativeAdOptions.Builder();
        builder.setAdChoicesPlacement(getAdChoicesPlacement(maxAdapterResponseParameters));
        builder.setRequestMultipleImages(BundleUtils.getString("template", "", maxAdapterResponseParameters.getServerParameters()).contains(FirebaseAnalytics.Param.MEDIUM));
        NativeAdListener nativeAdListener = new NativeAdListener(maxAdapterResponseParameters, applicationContext, maxNativeAdAdapterListener);
        new AdLoader.Builder(applicationContext, thirdPartyAdPlacementId).withNativeAdOptions(builder.build()).forNativeAd(nativeAdListener).withAdListener(nativeAdListener).build().loadAd(createAdRequestWithParameters);
    }

    public static MaxAdapterError toMaxError(AdError adError) {
        int code = adError.getCode();
        MaxAdapterError maxAdapterError = MaxAdapterError.UNSPECIFIED;
        if (code == 0) {
            maxAdapterError = MaxAdapterError.INTERNAL_ERROR;
        } else {
            if (code != 1) {
                if (code != 2) {
                    if (code != 3) {
                        switch (code) {
                            case 8:
                            case 11:
                                maxAdapterError = MaxAdapterError.INVALID_CONFIGURATION;
                                break;
                        }
                    }
                    maxAdapterError = MaxAdapterError.NO_FILL;
                } else {
                    maxAdapterError = MaxAdapterError.NO_CONNECTION;
                }
            }
            maxAdapterError = MaxAdapterError.BAD_REQUEST;
        }
        return new MaxAdapterError(maxAdapterError.getErrorCode(), maxAdapterError.getErrorMessage(), code, adError.getMessage());
    }

    private AdSize toAdSize(MaxAdFormat maxAdFormat, boolean z, Context context) {
        if (maxAdFormat == MaxAdFormat.BANNER || maxAdFormat == MaxAdFormat.LEADER) {
            if (!z) {
                return maxAdFormat == MaxAdFormat.BANNER ? AdSize.BANNER : AdSize.LEADERBOARD;
            }
            Display defaultDisplay = ((WindowManager) context.getSystemService("window")).getDefaultDisplay();
            DisplayMetrics displayMetrics = new DisplayMetrics();
            defaultDisplay.getMetrics(displayMetrics);
            return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(context, AppLovinSdkUtils.pxToDp(context, displayMetrics.widthPixels));
        }
        if (maxAdFormat == MaxAdFormat.MREC) {
            return AdSize.MEDIUM_RECTANGLE;
        }
        throw new IllegalArgumentException("Unsupported ad format: " + maxAdFormat);
    }

    private AdFormat toAdFormat(MaxAdapterSignalCollectionParameters maxAdapterSignalCollectionParameters) {
        MaxAdFormat adFormat = maxAdapterSignalCollectionParameters.getAdFormat();
        if (maxAdapterSignalCollectionParameters.getServerParameters().getBoolean("is_native") || adFormat == MaxAdFormat.NATIVE) {
            return AdFormat.NATIVE;
        }
        if (adFormat.isAdViewAd()) {
            return AdFormat.BANNER;
        }
        if (adFormat == MaxAdFormat.INTERSTITIAL) {
            return AdFormat.INTERSTITIAL;
        }
        if (adFormat == MaxAdFormat.REWARDED) {
            return AdFormat.REWARDED;
        }
        if (adFormat == MaxAdFormat.REWARDED_INTERSTITIAL) {
            return AdFormat.REWARDED_INTERSTITIAL;
        }
        throw new IllegalArgumentException("Unsupported ad format: " + adFormat);
    }

    private void setRequestConfiguration(MaxAdapterParameters maxAdapterParameters) {
        RequestConfiguration.Builder builder = MobileAds.getRequestConfiguration().toBuilder();
        Boolean privacySetting = getPrivacySetting("isAgeRestrictedUser", maxAdapterParameters);
        if (privacySetting != null) {
            builder.setTagForChildDirectedTreatment(privacySetting.booleanValue() ? 1 : 0);
        }
        String string = maxAdapterParameters.getServerParameters().getString("test_device_ids", null);
        if (!TextUtils.isEmpty(string)) {
            builder.setTestDeviceIds(Arrays.asList(string.split(",")));
        }
        MobileAds.setRequestConfiguration(builder.build());
    }

    private AdRequest createAdRequestWithParameters(boolean z, MaxAdFormat maxAdFormat, MaxAdapterParameters maxAdapterParameters, Context context) {
        boolean z2;
        Boolean privacySetting;
        Boolean privacySetting2;
        AdRequest.Builder builder = new AdRequest.Builder();
        Bundle serverParameters = maxAdapterParameters.getServerParameters();
        Bundle bundle = new Bundle(6);
        if (z) {
            z2 = "dv360".equalsIgnoreCase(BundleUtils.getString("bidder", "", serverParameters));
            bundle.putString("query_info_type", z2 ? "requester_type_3" : "requester_type_2");
            if (AppLovinSdk.VERSION_CODE >= 11000000 && maxAdFormat.isAdViewAd()) {
                Object obj = maxAdapterParameters.getLocalExtraParameters().get(AppLovinSdkExtraParameterKey.IS_ADAPTIVE_BANNER);
                if ((obj instanceof String) && ServerProtocol.DIALOG_RETURN_SCOPES_TRUE.equalsIgnoreCase((String) obj)) {
                    AdSize adSize = toAdSize(maxAdFormat, true, context);
                    bundle.putInt("adaptive_banner_w", adSize.getWidth());
                    bundle.putInt("adaptive_banner_h", adSize.getHeight());
                }
            }
            if (maxAdapterParameters instanceof MaxAdapterResponseParameters) {
                String bidResponse = ((MaxAdapterResponseParameters) maxAdapterParameters).getBidResponse();
                if (AppLovinSdkUtils.isValidString(bidResponse)) {
                    builder.setAdString(bidResponse);
                }
            }
        } else {
            z2 = false;
        }
        if (serverParameters.getBoolean("set_mediation_identifier", true)) {
            builder.setRequestAgent(z2 ? "applovin_dv360" : "applovin");
        }
        String string = BundleUtils.getString("event_id", serverParameters);
        if (AppLovinSdkUtils.isValidString(string)) {
            bundle.putString("placement_req_id", string);
        }
        if (getWrappingSdk().getConfiguration().getConsentDialogState() == AppLovinSdkConfiguration.ConsentDialogState.APPLIES && (privacySetting2 = getPrivacySetting("hasUserConsent", maxAdapterParameters)) != null && !privacySetting2.booleanValue()) {
            bundle.putString("npa", "1");
        }
        if (AppLovinSdk.VERSION_CODE >= 91100 && (privacySetting = getPrivacySetting("isDoNotSell", maxAdapterParameters)) != null && privacySetting.booleanValue()) {
            bundle.putInt("rdp", 1);
            PreferenceManager.getDefaultSharedPreferences(context).edit().putInt("gad_rdp", 1).commit();
        }
        if (AppLovinSdk.VERSION_CODE >= 11000000) {
            Map<String, Object> localExtraParameters = maxAdapterParameters.getLocalExtraParameters();
            Object obj2 = localExtraParameters.get("google_max_ad_content_rating");
            if (obj2 instanceof String) {
                bundle.putString("max_ad_content_rating", (String) obj2);
            }
            Object obj3 = localExtraParameters.get("google_content_url");
            if (obj3 instanceof String) {
                builder.setContentUrl((String) obj3);
            }
            Object obj4 = localExtraParameters.get("google_neighbouring_content_url_strings");
            if (obj4 instanceof List) {
                try {
                    builder.setNeighboringContentUrls((List) obj4);
                } catch (Throwable th) {
                    e("Neighbouring content URL strings extra param needs to be of type List<String>.", th);
                }
            }
        }
        builder.addNetworkExtrasBundle(AdMobAdapter.class, bundle);
        return builder.build();
    }

    private Boolean getPrivacySetting(String str, MaxAdapterParameters maxAdapterParameters) {
        try {
            return (Boolean) maxAdapterParameters.getClass().getMethod(str, new Class[0]).invoke(maxAdapterParameters, new Object[0]);
        } catch (Exception e) {
            log("Error getting privacy setting " + str + " with exception: ", e);
            return AppLovinSdk.VERSION_CODE >= 9140000 ? null : false;
        }
    }

    private static void updateMuteState(MaxAdapterResponseParameters maxAdapterResponseParameters) {
        Bundle serverParameters = maxAdapterResponseParameters.getServerParameters();
        if (serverParameters.containsKey("is_muted")) {
            MobileAds.setAppMuted(serverParameters.getBoolean("is_muted"));
        }
    }

    private int getAdChoicesPlacement(MaxAdapterResponseParameters maxAdapterResponseParameters) {
        if (AppLovinSdk.VERSION_CODE < 11000000) {
            return 1;
        }
        Map<String, Object> localExtraParameters = maxAdapterResponseParameters.getLocalExtraParameters();
        Object obj = localExtraParameters != null ? localExtraParameters.get("admob_ad_choices_placement") : null;
        if (isValidAdChoicesPlacement(obj)) {
            return ((Integer) obj).intValue();
        }
        return 1;
    }

    private boolean isValidAdChoicesPlacement(Object obj) {
        if (obj instanceof Integer) {
            Integer num = (Integer) obj;
            if (num.intValue() == 0 || num.intValue() == 1 || num.intValue() == 3 || num.intValue() == 2) {
                return true;
            }
        }
        return false;
    }

    public Context getContext(Activity activity) {
        return activity != null ? activity.getApplicationContext() : getApplicationContext();
    }

    public class InterstitialAdListener extends FullScreenContentCallback {
        private final MaxInterstitialAdapterListener listener;
        private final String placementId;

        InterstitialAdListener(String str, MaxInterstitialAdapterListener maxInterstitialAdapterListener) {
            this.placementId = str;
            this.listener = maxInterstitialAdapterListener;
        }

        @Override
        public void onAdShowedFullScreenContent() {
            GoogleMediationAdapter.this.log("Interstitial ad shown: " + this.placementId);
        }

        @Override
        public void onAdFailedToShowFullScreenContent(AdError adError) {
            MaxAdapterError maxAdapterError = new MaxAdapterError(MaxAdapterError.ERROR_CODE_AD_DISPLAY_FAILED, "Ad Display Failed", adError.getCode(), adError.getMessage());
            GoogleMediationAdapter.this.log("Interstitial ad (" + this.placementId + ") failed to show with error: " + maxAdapterError);
            this.listener.onInterstitialAdDisplayFailed(maxAdapterError);
        }

        @Override
        public void onAdImpression() {
            GoogleMediationAdapter.this.log("Interstitial ad impression recorded: " + this.placementId);
            this.listener.onInterstitialAdDisplayed();
        }

        @Override
        public void onAdClicked() {
            GoogleMediationAdapter.this.log("Interstitial ad clicked: " + this.placementId);
            this.listener.onInterstitialAdClicked();
        }

        @Override
        public void onAdDismissedFullScreenContent() {
            GoogleMediationAdapter.this.log("Interstitial ad hidden: " + this.placementId);
            this.listener.onInterstitialAdHidden();
        }
    }

    public class AppOpenAdListener extends FullScreenContentCallback {
        private final MaxAppOpenAdapterListener listener;
        private final String placementId;

        AppOpenAdListener(String str, MaxAppOpenAdapterListener maxAppOpenAdapterListener) {
            this.placementId = str;
            this.listener = maxAppOpenAdapterListener;
        }

        @Override
        public void onAdShowedFullScreenContent() {
            GoogleMediationAdapter.this.log("App open ad shown: " + this.placementId);
        }

        @Override
        public void onAdFailedToShowFullScreenContent(AdError adError) {
            MaxAdapterError maxAdapterError = new MaxAdapterError(MaxAdapterError.ERROR_CODE_AD_DISPLAY_FAILED, "Ad display failed", adError.getCode(), adError.getMessage());
            GoogleMediationAdapter.this.log("App open ad (" + this.placementId + ") failed to show with error: " + maxAdapterError);
            this.listener.onAppOpenAdDisplayFailed(maxAdapterError);
        }

        @Override
        public void onAdImpression() {
            GoogleMediationAdapter.this.log("App open ad impression recorded: " + this.placementId);
            this.listener.onAppOpenAdDisplayed();
        }

        @Override
        public void onAdClicked() {
            GoogleMediationAdapter.this.log("App open ad clicked: " + this.placementId);
            this.listener.onAppOpenAdClicked();
        }

        @Override
        public void onAdDismissedFullScreenContent() {
            GoogleMediationAdapter.this.log("App open ad hidden: " + this.placementId);
            this.listener.onAppOpenAdHidden();
        }
    }

    public class RewardedInterstitialAdListener extends FullScreenContentCallback {
        private boolean hasGrantedReward;
        private final MaxRewardedInterstitialAdapterListener listener;
        private final String placementId;

        private RewardedInterstitialAdListener(String str, MaxRewardedInterstitialAdapterListener maxRewardedInterstitialAdapterListener) {
            this.placementId = str;
            this.listener = maxRewardedInterstitialAdapterListener;
        }

        @Override
        public void onAdShowedFullScreenContent() {
            GoogleMediationAdapter.this.log("Rewarded interstitial ad shown: " + this.placementId);
            this.listener.onRewardedInterstitialAdVideoStarted();
        }

        @Override
        public void onAdFailedToShowFullScreenContent(AdError adError) {
            MaxAdapterError maxAdapterError = new MaxAdapterError(MaxAdapterError.ERROR_CODE_AD_DISPLAY_FAILED, "Ad Display Failed", adError.getCode(), adError.getMessage());
            GoogleMediationAdapter.this.log("Rewarded interstitial ad (" + this.placementId + ") failed to show with error: " + maxAdapterError);
            this.listener.onRewardedInterstitialAdDisplayFailed(maxAdapterError);
        }

        @Override
        public void onAdImpression() {
            GoogleMediationAdapter.this.log("Rewarded interstitial ad impression recorded: " + this.placementId);
            this.listener.onRewardedInterstitialAdDisplayed();
        }

        @Override
        public void onAdClicked() {
            GoogleMediationAdapter.this.log("Rewarded interstitial ad clicked: " + this.placementId);
            this.listener.onRewardedInterstitialAdClicked();
        }

        @Override
        public void onAdDismissedFullScreenContent() {
            this.listener.onRewardedInterstitialAdVideoCompleted();
            if (this.hasGrantedReward || GoogleMediationAdapter.this.shouldAlwaysRewardUser()) {
                MaxReward reward = GoogleMediationAdapter.this.getReward();
                GoogleMediationAdapter.this.log("Rewarded interstitial ad rewarded user with reward: " + reward);
                this.listener.onUserRewarded(reward);
            }
            GoogleMediationAdapter.this.log("Rewarded interstitial ad hidden: " + this.placementId);
            this.listener.onRewardedInterstitialAdHidden();
        }
    }

    public class RewardedAdListener extends FullScreenContentCallback {
        private boolean hasGrantedReward;
        private final MaxRewardedAdapterListener listener;
        private final String placementId;

        RewardedAdListener(String str, MaxRewardedAdapterListener maxRewardedAdapterListener) {
            this.placementId = str;
            this.listener = maxRewardedAdapterListener;
        }

        @Override
        public void onAdShowedFullScreenContent() {
            GoogleMediationAdapter.this.log("Rewarded ad shown: " + this.placementId);
            this.listener.onRewardedAdVideoStarted();
        }

        @Override
        public void onAdFailedToShowFullScreenContent(AdError adError) {
            MaxAdapterError maxAdapterError = new MaxAdapterError(MaxAdapterError.ERROR_CODE_AD_DISPLAY_FAILED, "Ad Display Failed", adError.getCode(), adError.getMessage());
            GoogleMediationAdapter.this.log("Rewarded ad (" + this.placementId + ") failed to show with error: " + maxAdapterError);
            this.listener.onRewardedAdDisplayFailed(maxAdapterError);
        }

        @Override
        public void onAdImpression() {
            GoogleMediationAdapter.this.log("Rewarded ad impression recorded: " + this.placementId);
            this.listener.onRewardedAdDisplayed();
        }

        @Override
        public void onAdClicked() {
            GoogleMediationAdapter.this.log("Rewarded ad clicked: " + this.placementId);
            this.listener.onRewardedAdClicked();
        }

        @Override
        public void onAdDismissedFullScreenContent() {
            this.listener.onRewardedAdVideoCompleted();
            if (this.hasGrantedReward || GoogleMediationAdapter.this.shouldAlwaysRewardUser()) {
                MaxReward reward = GoogleMediationAdapter.this.getReward();
                GoogleMediationAdapter.this.log("Rewarded user with reward: " + reward);
                this.listener.onUserRewarded(reward);
            }
            GoogleMediationAdapter.this.log("Rewarded ad hidden: " + this.placementId);
            this.listener.onRewardedAdHidden();
        }
    }

    private class AdViewListener extends AdListener {
        final MaxAdFormat adFormat;
        final MaxAdViewAdapterListener listener;
        final String placementId;

        AdViewListener(String str, MaxAdFormat maxAdFormat, MaxAdViewAdapterListener maxAdViewAdapterListener) {
            this.placementId = str;
            this.adFormat = maxAdFormat;
            this.listener = maxAdViewAdapterListener;
        }

        @Override
        public void onAdLoaded() {
            GoogleMediationAdapter.this.log(this.adFormat.getLabel() + " ad loaded: " + this.placementId);
            if (AppLovinSdk.VERSION_CODE < 9150000) {
                this.listener.onAdViewAdLoaded(GoogleMediationAdapter.this.adView);
                return;
            }
            Bundle bundle = new Bundle(3);
            ResponseInfo responseInfo = GoogleMediationAdapter.this.adView.getResponseInfo();
            String responseId = responseInfo != null ? responseInfo.getResponseId() : null;
            if (AppLovinSdkUtils.isValidString(responseId)) {
                bundle.putString("creative_id", responseId);
            }
            AdSize adSize = GoogleMediationAdapter.this.adView.getAdSize();
            if (adSize != null) {
                bundle.putInt("ad_width", adSize.getWidth());
                bundle.putInt("ad_height", adSize.getHeight());
            }
            this.listener.onAdViewAdLoaded(GoogleMediationAdapter.this.adView, bundle);
        }

        @Override
        public void onAdFailedToLoad(LoadAdError loadAdError) {
            MaxAdapterError maxError = GoogleMediationAdapter.toMaxError(loadAdError);
            GoogleMediationAdapter.this.log(this.adFormat.getLabel() + " ad (" + this.placementId + ") failed to load with error code: " + maxError);
            this.listener.onAdViewAdLoadFailed(maxError);
        }

        @Override
        public void onAdImpression() {
            GoogleMediationAdapter.this.log(this.adFormat.getLabel() + " ad shown: " + this.placementId);
            this.listener.onAdViewAdDisplayed();
        }

        @Override
        public void onAdOpened() {
            GoogleMediationAdapter.this.log(this.adFormat.getLabel() + " ad opened");
            this.listener.onAdViewAdClicked();
        }

        @Override
        public void onAdClosed() {
            GoogleMediationAdapter.this.log(this.adFormat.getLabel() + " ad closed");
        }
    }

    private class NativeAdViewListener extends AdListener implements NativeAd.OnNativeAdLoadedListener {
        final WeakReference<Activity> activityRef;
        final MaxAdFormat adFormat;
        final MaxAdViewAdapterListener listener;
        final String placementId;
        final Bundle serverParameters;

        NativeAdViewListener(MaxAdapterResponseParameters maxAdapterResponseParameters, MaxAdFormat maxAdFormat, Activity activity, MaxAdViewAdapterListener maxAdViewAdapterListener) {
            this.placementId = maxAdapterResponseParameters.getThirdPartyAdPlacementId();
            this.serverParameters = maxAdapterResponseParameters.getServerParameters();
            this.activityRef = new WeakReference<>(activity);
            this.adFormat = maxAdFormat;
            this.listener = maxAdViewAdapterListener;
        }

        @Override
        public void onNativeAdLoaded(final NativeAd nativeAd) {
            GoogleMediationAdapter.this.log("Native " + this.adFormat.getLabel() + " ad loaded: " + this.placementId);
            if (!TextUtils.isEmpty(nativeAd.getHeadline())) {
                GoogleMediationAdapter.this.nativeAd = nativeAd;
                final Activity activity = this.activityRef.get();
                final Context context = GoogleMediationAdapter.this.getContext(activity);
                final MediaView mediaView = new MediaView(context);
                MediaContent mediaContent = nativeAd.getMediaContent();
                if (mediaContent != null) {
                    mediaView.setMediaContent(mediaContent);
                }
                NativeAd.Image icon = nativeAd.getIcon();
                MaxNativeAd.MaxNativeAdImage maxNativeAdImage = null;
                if (icon != null) {
                    if (icon.getDrawable() != null) {
                        maxNativeAdImage = new MaxNativeAd.MaxNativeAdImage(icon.getDrawable());
                    } else {
                        maxNativeAdImage = new MaxNativeAd.MaxNativeAdImage(icon.getUri());
                    }
                }
                final MaxNativeAd build = new MaxNativeAd.Builder().setAdFormat(this.adFormat).setIcon(maxNativeAdImage).setTitle(nativeAd.getHeadline()).setBody(nativeAd.getBody()).setMediaView(mediaView).setCallToAction(nativeAd.getCallToAction()).build();
                final String string = BundleUtils.getString("template", "", this.serverParameters);
                if (string.contains("vertical") && AppLovinSdk.VERSION_CODE < 9140500) {
                    GoogleMediationAdapter.this.log("Vertical native banners are only supported on MAX SDK 9.14.5 and above. Default horizontal native template will be used.");
                }
                AppLovinSdkUtils.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        MaxNativeAdView maxNativeAdView;
                        if (AppLovinSdk.VERSION_CODE < 9140000) {
                            GoogleMediationAdapter.this.log("Native ads with media views are only supported on MAX SDK version 9.14.0 and above. Default native template will be used.");
                            maxNativeAdView = new MaxNativeAdView(build, activity);
                        } else if (AppLovinSdk.VERSION_CODE >= 11010000) {
                            maxNativeAdView = new MaxNativeAdView(build, string, context);
                        } else {
                            maxNativeAdView = new MaxNativeAdView(build, string, activity);
                        }
                        GoogleMediationAdapter.this.nativeAdView = new NativeAdView(context);
                        GoogleMediationAdapter.this.nativeAdView.setIconView(maxNativeAdView.getIconContentView());
                        GoogleMediationAdapter.this.nativeAdView.setHeadlineView(maxNativeAdView.getTitleTextView());
                        GoogleMediationAdapter.this.nativeAdView.setBodyView(maxNativeAdView.getBodyTextView());
                        GoogleMediationAdapter.this.nativeAdView.setMediaView(mediaView);
                        GoogleMediationAdapter.this.nativeAdView.setCallToActionView(maxNativeAdView.getCallToActionButton());
                        GoogleMediationAdapter.this.nativeAdView.setNativeAd(nativeAd);
                        GoogleMediationAdapter.this.nativeAdView.addView(maxNativeAdView);
                        ResponseInfo responseInfo = nativeAd.getResponseInfo();
                        String responseId = responseInfo != null ? responseInfo.getResponseId() : null;
                        if (AppLovinSdk.VERSION_CODE < 9150000 || !AppLovinSdkUtils.isValidString(responseId)) {
                            NativeAdViewListener.this.listener.onAdViewAdLoaded(GoogleMediationAdapter.this.nativeAdView);
                            return;
                        }
                        Bundle bundle = new Bundle(1);
                        bundle.putString("creative_id", responseId);
                        NativeAdViewListener.this.listener.onAdViewAdLoaded(GoogleMediationAdapter.this.nativeAdView, bundle);
                    }
                });
                return;
            }
            GoogleMediationAdapter.this.log("Native " + this.adFormat.getLabel() + " ad failed to load: Google native ad is missing one or more required assets");
            this.listener.onAdViewAdLoadFailed(MaxAdapterError.INVALID_CONFIGURATION);
            nativeAd.destroy();
        }

        @Override
        public void onAdFailedToLoad(LoadAdError loadAdError) {
            MaxAdapterError maxError = GoogleMediationAdapter.toMaxError(loadAdError);
            GoogleMediationAdapter.this.log("Native " + this.adFormat.getLabel() + " ad (" + this.placementId + ") failed to load with error: " + maxError);
            this.listener.onAdViewAdLoadFailed(maxError);
        }

        @Override
        public void onAdImpression() {
            GoogleMediationAdapter.this.log("Native " + this.adFormat.getLabel() + " ad shown");
            this.listener.onAdViewAdDisplayed();
        }

        @Override
        public void onAdClicked() {
            GoogleMediationAdapter.this.log("Native " + this.adFormat.getLabel() + " ad clicked");
            this.listener.onAdViewAdClicked();
        }

        @Override
        public void onAdOpened() {
            GoogleMediationAdapter.this.log("Native " + this.adFormat.getLabel() + " ad opened");
            this.listener.onAdViewAdExpanded();
        }

        @Override
        public void onAdClosed() {
            GoogleMediationAdapter.this.log("Native " + this.adFormat.getLabel() + " ad closed");
            this.listener.onAdViewAdCollapsed();
        }
    }

    private class NativeAdListener extends AdListener implements NativeAd.OnNativeAdLoadedListener {
        final Context context;
        final MaxNativeAdAdapterListener listener;
        final String placementId;
        final Bundle serverParameters;

        public NativeAdListener(MaxAdapterResponseParameters maxAdapterResponseParameters, Context context, MaxNativeAdAdapterListener maxNativeAdAdapterListener) {
            this.placementId = maxAdapterResponseParameters.getThirdPartyAdPlacementId();
            this.serverParameters = maxAdapterResponseParameters.getServerParameters();
            this.context = context;
            this.listener = maxNativeAdAdapterListener;
        }

        @Override
        public void onNativeAdLoaded(final NativeAd nativeAd) {
            GoogleMediationAdapter.this.log("Native ad loaded: " + this.placementId);
            GoogleMediationAdapter.this.nativeAd = nativeAd;
            if (AppLovinSdkUtils.isValidString(BundleUtils.getString("template", "", this.serverParameters)) && TextUtils.isEmpty(nativeAd.getHeadline())) {
                GoogleMediationAdapter.this.e("Native ad (" + nativeAd + ") does not have required assets.");
                this.listener.onNativeAdLoadFailed(new MaxAdapterError(MaxAdapterError.ERROR_CODE_MISSING_REQUIRED_NATIVE_AD_ASSETS, "Missing Native Ad Assets"));
                return;
            }
            AppLovinSdkUtils.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    float f;
                    ImageView imageView;
                    Drawable drawable;
                    MaxNativeAd.MaxNativeAdImage maxNativeAdImage;
                    MediaContent mediaContent = nativeAd.getMediaContent();
                    List<NativeAd.Image> images = nativeAd.getImages();
                    if (mediaContent != null) {
                        MediaView mediaView = new MediaView(NativeAdListener.this.context);
                        mediaView.setMediaContent(mediaContent);
                        drawable = mediaContent.getMainImage();
                        f = mediaContent.getAspectRatio();
                        imageView = mediaView;
                    } else {
                        if (images != null && images.size() > 0) {
                            NativeAd.Image image = images.get(0);
                            ImageView imageView2 = new ImageView(NativeAdListener.this.context);
                            Drawable drawable2 = image.getDrawable();
                            if (drawable2 != null) {
                                imageView2.setImageDrawable(drawable2);
                                f = drawable2.getIntrinsicWidth() / drawable2.getIntrinsicHeight();
                                drawable = null;
                                imageView = imageView2;
                            }
                        }
                        f = 0.0f;
                        imageView = null;
                        drawable = null;
                    }
                    NativeAd.Image icon = nativeAd.getIcon();
                    if (icon == null) {
                        maxNativeAdImage = null;
                    } else if (icon.getDrawable() != null) {
                        maxNativeAdImage = new MaxNativeAd.MaxNativeAdImage(icon.getDrawable());
                    } else {
                        maxNativeAdImage = new MaxNativeAd.MaxNativeAdImage(icon.getUri());
                    }
                    MaxNativeAd.Builder callToAction = new MaxNativeAd.Builder().setAdFormat(MaxAdFormat.NATIVE).setIcon(maxNativeAdImage).setTitle(nativeAd.getHeadline()).setAdvertiser(nativeAd.getAdvertiser()).setBody(nativeAd.getBody()).setMediaView(imageView).setCallToAction(nativeAd.getCallToAction());
                    if (AppLovinSdk.VERSION_CODE >= 11040399) {
                        callToAction.setMainImage(new MaxNativeAd.MaxNativeAdImage(drawable));
                    }
                    if (AppLovinSdk.VERSION_CODE >= 11040000) {
                        callToAction.setMediaContentAspectRatio(f);
                    }
                    MaxGoogleNativeAd maxGoogleNativeAd = new MaxGoogleNativeAd(callToAction);
                    ResponseInfo responseInfo = nativeAd.getResponseInfo();
                    String responseId = responseInfo != null ? responseInfo.getResponseId() : null;
                    Bundle bundle = new Bundle(1);
                    bundle.putString("creative_id", responseId);
                    NativeAdListener.this.listener.onNativeAdLoaded(maxGoogleNativeAd, bundle);
                }
            });
        }

        @Override
        public void onAdFailedToLoad(LoadAdError loadAdError) {
            MaxAdapterError maxError = GoogleMediationAdapter.toMaxError(loadAdError);
            GoogleMediationAdapter.this.log("Native ad (" + this.placementId + ") failed to load with error: " + maxError);
            this.listener.onNativeAdLoadFailed(maxError);
        }

        @Override
        public void onAdImpression() {
            GoogleMediationAdapter.this.log("Native ad shown");
            this.listener.onNativeAdDisplayed(null);
        }

        @Override
        public void onAdClicked() {
            GoogleMediationAdapter.this.log("Native ad clicked");
            this.listener.onNativeAdClicked();
        }

        @Override
        public void onAdOpened() {
            GoogleMediationAdapter.this.log("Native ad opened");
        }

        @Override
        public void onAdClosed() {
            GoogleMediationAdapter.this.log("Native ad closed");
        }
    }

    private class MaxGoogleNativeAd extends MaxNativeAd {
        public MaxGoogleNativeAd(MaxNativeAd.Builder builder) {
            super(builder);
        }

        @Override
        public void prepareViewForInteraction(MaxNativeAdView maxNativeAdView) {
            NativeAd nativeAd = GoogleMediationAdapter.this.nativeAd;
            if (nativeAd == null) {
                GoogleMediationAdapter.this.e("Failed to register native ad views: native ad is null.");
                return;
            }
            GoogleMediationAdapter.this.nativeAdView = new NativeAdView(maxNativeAdView.getContext());
            View mainView = maxNativeAdView.getMainView();
            maxNativeAdView.removeView(mainView);
            GoogleMediationAdapter.this.nativeAdView.addView(mainView);
            maxNativeAdView.addView(GoogleMediationAdapter.this.nativeAdView);
            GoogleMediationAdapter.this.nativeAdView.setIconView(maxNativeAdView.getIconImageView());
            GoogleMediationAdapter.this.nativeAdView.setHeadlineView(maxNativeAdView.getTitleTextView());
            GoogleMediationAdapter.this.nativeAdView.setAdvertiserView(maxNativeAdView.getAdvertiserTextView());
            GoogleMediationAdapter.this.nativeAdView.setBodyView(maxNativeAdView.getBodyTextView());
            GoogleMediationAdapter.this.nativeAdView.setCallToActionView(maxNativeAdView.getCallToActionButton());
            View mediaView = getMediaView();
            if (mediaView instanceof MediaView) {
                GoogleMediationAdapter.this.nativeAdView.setMediaView((MediaView) mediaView);
            } else if (mediaView instanceof ImageView) {
                GoogleMediationAdapter.this.nativeAdView.setImageView(mediaView);
            }
            GoogleMediationAdapter.this.nativeAdView.setNativeAd(nativeAd);
        }
    }
}