Life Simulator: Game Dev v0.6版本的 MD5 值为:ecca8c682c00d3b861fbed7f58e41c8d

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


package com.avocarrot.androidsdk;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.avocarrot.androidsdk.AdLoadTask;
import com.avocarrot.androidsdk.BaseListener;
import com.avocarrot.androidsdk.DynamicConfiguration;
import com.avocarrot.androidsdk.VastParseAndVideoDownloadTask;
import com.avocarrot.androidsdk.logging.AvocarrotLogger;
import com.avocarrot.androidsdk.ui.AdChoicesView;
import com.cmcm.adsdk.Const;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class BaseController<T extends BaseListener> implements AdLoadTask.AdLoadTaskListener {
    static ClickManager clickManager = new ClickManager();
    final int MAX_CLICKS;
    final int MIN_CLICK_TRIGGER;
    private T _listener;
    AdPooling adPooling;
    Avocarrot avocarrot;
    public ImageManager imageManager;
    ImpressionManager impressionManager;
    String placementName;
    Status status;
    VideoManager videoManager;
    SoftReference<Context> weakContext;

    public enum Status {
        LOADING,
        IDLE,
        FAIL
    }

    public BaseController(Context context, String apiKey, String placementName) {
        this(context, apiKey, placementName, "");
    }

    public BaseController(Context context, String apiKey, String placementName, String mediationType) {
        this.weakContext = null;
        this.avocarrot = Avocarrot.getInstance(context);
        this.avocarrot.setKey(apiKey);
        this.avocarrot.setMediationAdapter(mediationType);
        this.impressionManager = new ImpressionManager();
        this.imageManager = new ImageManager(context);
        this.videoManager = new VideoManager(context);
        this.weakContext = new SoftReference<>(context);
        this.placementName = placementName;
        this.status = Status.IDLE;
        this.adPooling = new AdPooling(context, placementName);
        AvocarrotLogger.addPlacementInfo(placementName);
        this.MAX_CLICKS = DynamicConfiguration.getIntFallbackToDefault(DynamicConfiguration.GENERAL, DynamicConfiguration.Settings.maxClicks).intValue();
        this.MIN_CLICK_TRIGGER = DynamicConfiguration.getIntFallbackToDefault(DynamicConfiguration.GENERAL, DynamicConfiguration.Settings.minClickTrigger).intValue();
    }

    public void setLogger(Boolean value, String level) {
        this.avocarrot.setLogger(value.booleanValue(), level);
    }

    public void setSandbox(boolean inSandbox) {
        this.avocarrot.setSandbox(inSandbox);
    }

    protected String getPlacementName() {
        return this.placementName;
    }

    public void clear() {
        this.imageManager.clear();
        this.impressionManager.clear();
        this.videoManager.clear();
        this._listener = null;
    }

    void loadAd() {
        loadAd(1, true);
    }

    public void loadAd(int slot, boolean isPreloading) {
        List<BaseModel> poolAds = getPoolAds();
        if (poolAds.size() > DynamicConfiguration.getIntFallbackToDefault(this.placementName, DynamicConfiguration.Settings.bufferMin).intValue()) {
            onLoadAdDone(isPreloading, poolAds);
            AvocarrotLogger.AvocarrotLog(true, AvocarrotLogger.Levels.DEBUG, "Funnel|LoadAds but pool isn't empty", null, "placement", this.placementName);
            return;
        }
        try {
            if (this.status == Status.IDLE && slot >= 1) {
                AdLoadTask currentLoadTask = new AdLoadTask(this.avocarrot.getApiKey(), new BaseAdRequest(this.placementName, slot, isPreloading, this.avocarrot.getDeviceInfo()), this, this.weakContext.get());
                this.status = Status.LOADING;
                if (Build.VERSION.SDK_INT >= 11) {
                    currentLoadTask.executeOnExecutor(Avocarrot.Executor, new Void[0]);
                } else {
                    currentLoadTask.execute(new Void[0]);
                }
            }
        } catch (Exception e) {
            this.status = Status.FAIL;
            AvocarrotLogger.AvocarrotLog(AvocarrotLogger.Levels.ERROR, "Could not load Ad for placement", e, "placement", this.placementName);
        }
    }

    @Override
    public final void onLoadAdSuccess(BaseAdRequest request, BaseAdResponse response) {
        this.status = Status.IDLE;
        if (response == null || response.getJSONSlots() == null || response.getJSONSlots().length() == 0) {
            onLoadAdFail(request, AdError.GENERIC, new Exception("Null Response OR Response without a slot"));
            AvocarrotLogger.AvocarrotLog(AvocarrotLogger.Levels.WARN, "Load ads list is empty", null, "placement", this.placementName);
            return;
        }
        List<BaseModel> ads = new ArrayList<>();
        int count = response.getJSONSlots().length();
        for (int i = 0; i < count; i++) {
            BaseModel model = new BaseModel(response.getJSONSlots().optJSONObject(i), response.getRequestId());
            if (model.isValid()) {
                ads.add(model);
                if (DynamicConfiguration.getBoolean(DynamicConfiguration.GENERAL, DynamicConfiguration.Settings.preloadVast)) {
                    this.videoManager.requestVideo(model, null, getListener());
                }
            } else {
                AvocarrotLogger.AvocarrotLog(AvocarrotLogger.Levels.WARN, "AdModel is not valid", null, "id", model.getId(), "placement", this.placementName);
            }
        }
        this.adPooling.addToPool(ads);
        onLoadAdDone(request.preloading, ads);
    }

    @Override
    public final void onLoadAdFailure(BaseAdRequest request, AdError error, Exception exception) {
        this.status = Status.FAIL;
        onLoadAdFail(request, error, exception);
    }

    public void onLoadAdDone(boolean isPreloading, List<BaseModel> baseModels) {
        AvocarrotLogger.AvocarrotLog(true, AvocarrotLogger.Levels.DEBUG, "Funnel|onLoadAdDone", null, "placement", this.placementName);
        if (!isPreloading) {
            displayAd();
        }
    }

    public void onLoadAdFail(BaseAdRequest request, AdError error, Exception exception) {
        if (error != null) {
            String requestString = "";
            try {
                requestString = request.getJsonObject().toString();
            } catch (Exception e) {
            }
            AvocarrotLogger.AvocarrotLog(AvocarrotLogger.Levels.ERROR, "onLoadAdFail " + (exception != null ? exception.getMessage() : ""), exception, "request", requestString, "adError", error.toString(), "placement", this.placementName);
        }
        T listener = getListener();
        if (listener != null) {
            listener.onAdError(error);
        }
    }

    public boolean displayAd() {
        AvocarrotLogger.AvocarrotLog(true, AvocarrotLogger.Levels.DEBUG, "Funnel|displayAd", null, "placement", this.placementName);
        return true;
    }

    public void loadMedia(BaseModel model, ImageView fallbackImage, VideoView videoView) {
        if (model == null) {
            AvocarrotLogger.AvocarrotLog(AvocarrotLogger.Levels.ERROR, "Invalid BaseModel", null, "placement", this.placementName);
        } else {
            bindModelToMediaViews(model, fallbackImage, videoView, null);
        }
    }

    public void bindModelToMediaViews(@NonNull final BaseModel model, final ImageView imageView, final VideoView videoView, View videoOnly) {
        if (model.hasVastTag()) {
            if (videoView == null) {
                if (imageView != null) {
                    fallbackToImage(imageView, model.getImage().getUrl());
                    AvocarrotLogger.AvocarrotLog(AvocarrotLogger.Levels.ERROR, "VideoModel without VideoView", null, "id", model.getId(), "placement", this.placementName);
                    return;
                }
                return;
            }
            if (imageView != null) {
                imageView.setVisibility(8);
            }
            if (videoView != null) {
                videoView.setVisibility(0);
            }
            this.videoManager.requestVideo(model, new VastParseAndVideoDownloadTask.Listener() {
                @Override
                public void onSuccess(BaseModel baseModel) {
                    BaseController controller = BaseController.this;
                    VideoModel videoModel = baseModel.getVideo();
                    videoView.setListener(new AvocarrotVideoListener(controller.weakContext.get(), controller));
                    videoView.playVideo(videoModel);
                }

                @Override
                public void onFailure(VastParseAndVideoDownloadTask.Exception error) {
                    if (videoView != null) {
                        videoView.setVisibility(8);
                    }
                    BaseController.this.fallbackToImage(imageView, model.getImage().getUrl());
                    AvocarrotLogger.AvocarrotLog(AvocarrotLogger.Levels.ERROR, "Failed to load Video, fallback to Image ", error, Const.KEY_JUHE, error.getModel().getId(), "placement", BaseController.this.placementName);
                }
            }, getListener());
        } else if (model.getImage().isValid()) {
            if (videoView != null) {
                videoView.setVisibility(8);
            }
            fallbackToImage(imageView, model.getImage().getUrl());
        } else {
            AvocarrotLogger.AvocarrotLog(AvocarrotLogger.Levels.ERROR, "No valid media Asset", null, "id", model.getId(), "placement", this.placementName);
            return;
        }
        if (videoOnly != null) {
            videoOnly.setVisibility(model.hasVastTag() ? 0 : 8);
        }
    }

    void fallbackToImage(ImageView imageView, String url) {
        if (imageView != null) {
            imageView.setVisibility(0);
            this.imageManager.loadImageInto(url, imageView);
        }
    }

    public void bindAdModel2AdView(View view, BaseModel model) {
        if (view != null) {
            this.weakContext = new SoftReference<>(view.getContext());
        }
        if (!this.avocarrot.isMediated() && !adChoicesExistsInLayout(view)) {
            AvocarrotLogger.AvocarrotLog(AvocarrotLogger.Levels.ERROR, "Ad layout must contains an AdChoicesView. Please check Avocarrot Docs : https://www.avocarrot.com/docs", null, "id", model.getId(), "placement", this.placementName);
        }
        try {
            this.impressionManager.observeView(this, model, view, createVisibilityConditions());
            AvocarrotLogger.AvocarrotLog(true, AvocarrotLogger.Levels.DEBUG, "Funnel|bindAdModel2AdView", null, "id", model.getId(), "placement", this.placementName);
        } catch (Exception e) {
            AvocarrotLogger.AvocarrotLog(true, AvocarrotLogger.Levels.ERROR, "Fail to observe View", e, "id", model.getId(), "placement", this.placementName);
        }
    }

    public void impressionRegistered(View view, BaseModel model) {
        AvocarrotLogger.addAdInfo(model != null ? model.getId() : null);
        this.adPooling.removeFromPool(model);
        T listener = getListener();
        if (listener != null) {
            listener.onAdImpression();
        }
        AvocarrotLogger.AvocarrotLog(true, AvocarrotLogger.Levels.DEBUG, "Funnel|onImpressionRegistered", null, "id", model.getId(), "placement", this.placementName);
    }

    public boolean handleClickOnAdView(BaseModel model, View view) {
        AvocarrotLogger.AvocarrotLog(true, AvocarrotLogger.Levels.DEBUG, "Funnel|handleClickOnAdView", null, "placement", this.placementName);
        if (model == null) {
            AvocarrotLogger.AvocarrotLog(AvocarrotLogger.Levels.ERROR, "Could not click empty model", null, "placement", this.placementName);
            return false;
        }
        if (this.weakContext == null || this.weakContext.get() == null) {
            AvocarrotLogger.AvocarrotLog(AvocarrotLogger.Levels.ERROR, "Could not redirect to URL because Activity is not available", null, "placement", this.placementName);
            return false;
        }
        AvocarrotLogger.addAdInfo(model != null ? model.getId() : null);
        if (view == null || !this.impressionManager.isModelConsideredVisible(model)) {
            AvocarrotLogger.Levels levels = AvocarrotLogger.Levels.WARN;
            String[] strArr = new String[6];
            strArr[0] = "visibilityCondition";
            strArr[1] = view == null ? "null" : createVisibilityConditions().toString();
            strArr[2] = "model";
            strArr[3] = model.getId();
            strArr[4] = "placement";
            strArr[5] = this.placementName;
            AvocarrotLogger.AvocarrotLog(levels, "Could not perform click on view that doesn't fulfil the visibility conditions", null, strArr);
            return false;
        }
        String url = model.getDestinationUrl();
        if (TextUtils.isEmpty(url)) {
            AvocarrotLogger.AvocarrotLog(AvocarrotLogger.Levels.ERROR, "Could not redirect to URL because: URL is empty", null, "placement", this.placementName);
            return false;
        }
        int handleClicks = clickManager.getHandleClickCount().inc(model.getId()).intValue();
        if (handleClicks < this.MIN_CLICK_TRIGGER) {
            AvocarrotLogger.AvocarrotLog(true, AvocarrotLogger.Levels.DEBUG, "Trigger click less times than min threshold", null, "clicks", Integer.toString(handleClicks));
            return false;
        }
        if (handleClicks > this.MIN_CLICK_TRIGGER) {
            AvocarrotLogger.AvocarrotLog(true, AvocarrotLogger.Levels.DEBUG, "Trigger click too many times without first load", null, "clicks", Integer.toString(handleClicks));
            return false;
        }
        int loadClicks = clickManager.getLoadClickCount().inc(model.getId()).intValue();
        if (loadClicks > this.MAX_CLICKS) {
            clickManager.getHandleClickCount().reset(model.getId());
            AvocarrotLogger.AvocarrotLog(AvocarrotLogger.Levels.ERROR, "Trigger click above max threshold", null, "placement", this.placementName, "clicks", Integer.toString(loadClicks));
            return false;
        }
        return Utils.redirectUser(this.weakContext.get(), url, model.getClickUrls(), model.getTrackers(), getListener(), model);
    }

    public void loadFullscreenVideo(VideoModel model) {
        if (model == null) {
            AvocarrotLogger.AvocarrotLog(AvocarrotLogger.Levels.ERROR, "Open fullscreen without model", null, "placement", this.placementName);
            return;
        }
        Context context = this.weakContext.get();
        if (context != null) {
            context.startActivity(new Intent(context, (Class<?>) VideoActivity.class).putExtra(VideoActivity.EXTRA_VIDEO_MODEL, (Parcelable) model));
        }
    }

    public void bindAdChoice(AdChoicesView adChoicesView, AdChoices adChoices) {
        if (adChoicesView != null) {
            adChoicesView.setAdChoices(adChoices, this);
        }
    }

    public void loadAdChoiceUrl(String url) {
        try {
            Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(url));
            this.weakContext.get().startActivity(intent);
            AvocarrotLogger.AvocarrotLog(true, AvocarrotLogger.Levels.DEBUG, "Redirect to AdChoice Url", null, url);
        } catch (Exception e) {
            AvocarrotLogger.AvocarrotLog(AvocarrotLogger.Levels.ERROR, "Fail to open AdChoice redirection url");
        }
    }

    VisibilityConditions createVisibilityConditions() {
        return new VisibilityConditions(DynamicConfiguration.getIntFallbackToDefault(this.placementName, DynamicConfiguration.Settings.visibilityPercentage).intValue(), DynamicConfiguration.getIntFallbackToDefault(this.placementName, DynamicConfiguration.Settings.visibilityMinTime).intValue());
    }

    public void setListener(T listener) {
        this._listener = listener;
    }

    public T getListener() {
        return this._listener;
    }

    public void connectExtraFieldsToView(View parent, HashMap<String, Integer> ids, List<ExtraFieldModel> extraFields) {
        if (parent != null && extraFields != null && ids != null) {
            for (ExtraFieldModel extra : extraFields) {
                try {
                    View view = parent.findViewById(ids.get(extra.getId()).intValue());
                    if (view != null) {
                        switch (extra.getType()) {
                            case TEXT:
                                if (view instanceof TextView) {
                                    ((TextView) view).setText(extra.getValue());
                                    break;
                                } else {
                                    break;
                                }
                            case IMAGE:
                                if (view instanceof ImageView) {
                                    this.imageManager.loadImageInto(extra.getValue(), (ImageView) view);
                                    break;
                                } else {
                                    break;
                                }
                        }
                    }
                } catch (Exception e) {
                }
            }
        }
    }

    protected List<BaseModel> getPoolAds() {
        return this.adPooling.getAds();
    }

    boolean adChoicesExistsInLayout(View view) {
        if (view instanceof AdChoicesView) {
            return true;
        }
        if (view instanceof ViewGroup) {
            ViewGroup group = (ViewGroup) view;
            int count = group.getChildCount();
            for (int i = 0; i < count; i++) {
                if (adChoicesExistsInLayout(group.getChildAt(i))) {
                    return true;
                }
            }
        }
        return false;
    }

    public static ClickManager getClickManager() {
        return clickManager;
    }
}