Wolves Themes v1.0版本的 MD5 值为:cddc92b47f6b7c4a195c9625507eb51c
以下内容为反编译后的 MoPubCustomEventNative.java 源代码,内容仅作参考
package com.mopub.nativeads;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import com.mobvista.msdk.base.entity.CampaignEx;
import com.mopub.common.DataKeys;
import com.mopub.common.VisibleForTesting;
import com.mopub.common.logging.MoPubLog;
import com.mopub.common.util.Numbers;
import com.mopub.nativeads.CustomEventNative;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONObject;
public class MoPubCustomEventNative extends CustomEventNative {
static class MoPubStaticNativeAd extends StaticNativeAd {
@VisibleForTesting
static final String PRIVACY_INFORMATION_CLICKTHROUGH_URL = "https://www.mopub.com/optout";
@NonNull
private final Context mContext;
@NonNull
private final CustomEventNative.CustomEventNativeListener mCustomEventNativeListener;
@NonNull
private final ImpressionTracker mImpressionTracker;
@NonNull
private final JSONObject mJsonObject;
@NonNull
private final NativeClickHandler mNativeClickHandler;
public enum Parameter {
IMPRESSION_TRACKER("imptracker", true),
CLICK_TRACKER("clktracker", true),
TITLE("title", false),
TEXT("text", false),
MAIN_IMAGE("mainimage", false),
ICON_IMAGE("iconimage", false),
CLICK_DESTINATION("clk", false),
FALLBACK("fallback", false),
CALL_TO_ACTION(CampaignEx.JSON_KEY_CTA_TEXT, false),
STAR_RATING("starrating", false);
@NonNull
@VisibleForTesting
static final Set<String> requiredKeys = new HashSet();
@NonNull
final String name;
final boolean required;
static {
Parameter[] values;
for (Parameter parameter : values()) {
if (parameter.required) {
requiredKeys.add(parameter.name);
}
}
}
Parameter(String str, boolean z) {
this.name = str;
this.required = z;
}
@Nullable
static Parameter from(@NonNull String str) {
Parameter[] values;
for (Parameter parameter : values()) {
if (parameter.name.equals(str)) {
return parameter;
}
}
return null;
}
}
MoPubStaticNativeAd(@NonNull Context context, @NonNull JSONObject jSONObject, @NonNull ImpressionTracker impressionTracker, @NonNull NativeClickHandler nativeClickHandler, @NonNull CustomEventNative.CustomEventNativeListener customEventNativeListener) {
this.mJsonObject = jSONObject;
this.mContext = context.getApplicationContext();
this.mImpressionTracker = impressionTracker;
this.mNativeClickHandler = nativeClickHandler;
this.mCustomEventNativeListener = customEventNativeListener;
}
private void addInstanceVariable(@NonNull Parameter parameter, @Nullable Object obj) {
try {
switch (parameter) {
case MAIN_IMAGE:
setMainImageUrl((String) obj);
return;
case ICON_IMAGE:
setIconImageUrl((String) obj);
return;
case IMPRESSION_TRACKER:
addImpressionTrackers(obj);
return;
case CLICK_DESTINATION:
setClickDestinationUrl((String) obj);
return;
case CLICK_TRACKER:
parseClickTrackers(obj);
return;
case CALL_TO_ACTION:
setCallToAction((String) obj);
return;
case TITLE:
setTitle((String) obj);
return;
case TEXT:
setText((String) obj);
return;
case STAR_RATING:
setStarRating(Numbers.parseDouble(obj));
return;
default:
MoPubLog.d("Unable to add JSON key to internal mapping: " + parameter.name);
return;
}
} catch (ClassCastException e) {
if (parameter.required) {
throw e;
}
MoPubLog.d("Ignoring class cast exception for optional key: " + parameter.name);
}
}
private boolean containsRequiredKeys(@NonNull JSONObject jSONObject) {
HashSet hashSet = new HashSet();
Iterator<String> keys = jSONObject.keys();
while (keys.hasNext()) {
hashSet.add(keys.next());
}
return hashSet.containsAll(Parameter.requiredKeys);
}
private boolean isImageKey(@Nullable String str) {
return str != null && str.toLowerCase(Locale.US).endsWith("image");
}
private void parseClickTrackers(@NonNull Object obj) {
if (obj instanceof JSONArray) {
addClickTrackers(obj);
} else {
addClickTracker((String) obj);
}
}
@Override
public void clear(@NonNull View view) {
this.mImpressionTracker.removeView(view);
this.mNativeClickHandler.clearOnClickListener(view);
}
@Override
public void destroy() {
this.mImpressionTracker.destroy();
}
@NonNull
List<String> getAllImageUrls() {
ArrayList arrayList = new ArrayList();
if (getMainImageUrl() != null) {
arrayList.add(getMainImageUrl());
}
if (getIconImageUrl() != null) {
arrayList.add(getIconImageUrl());
}
arrayList.addAll(getExtrasImageUrls());
return arrayList;
}
@NonNull
List<String> getExtrasImageUrls() {
ArrayList arrayList = new ArrayList(getExtras().size());
for (Map.Entry<String, Object> entry : getExtras().entrySet()) {
if (isImageKey(entry.getKey()) && (entry.getValue() instanceof String)) {
arrayList.add((String) entry.getValue());
}
}
return arrayList;
}
@Override
public void handleClick(@Nullable View view) {
notifyAdClicked();
this.mNativeClickHandler.openClickDestinationUrl(getClickDestinationUrl(), view);
}
void loadAd() {
if (!containsRequiredKeys(this.mJsonObject)) {
throw new IllegalArgumentException("JSONObject did not contain required keys.");
}
Iterator<String> keys = this.mJsonObject.keys();
while (keys.hasNext()) {
String next = keys.next();
Parameter from = Parameter.from(next);
if (from != null) {
try {
addInstanceVariable(from, this.mJsonObject.opt(next));
} catch (ClassCastException unused) {
throw new IllegalArgumentException("JSONObject key (" + next + ") contained unexpected value.");
}
} else {
addExtra(next, this.mJsonObject.opt(next));
}
}
setPrivacyInformationIconClickThroughUrl(PRIVACY_INFORMATION_CLICKTHROUGH_URL);
this.mCustomEventNativeListener.onNativeAdLoaded(this);
}
@Override
public void prepare(@NonNull View view) {
this.mImpressionTracker.addView(view, this);
this.mNativeClickHandler.setOnClickListener(view, this);
}
@Override
public void recordImpression(@NonNull View view) {
notifyAdImpressed();
}
}
@Override
public void loadNativeAd(@NonNull Context context, @NonNull CustomEventNative.CustomEventNativeListener customEventNativeListener, @NonNull Map<String, Object> map, @NonNull Map<String, String> map2) {
Object obj = map.get(DataKeys.JSON_BODY_KEY);
if (!(obj instanceof JSONObject)) {
customEventNativeListener.onNativeAdFailed(NativeErrorCode.INVALID_RESPONSE);
return;
}
try {
new MoPubStaticNativeAd(context, (JSONObject) obj, new ImpressionTracker(context), new NativeClickHandler(context), customEventNativeListener).loadAd();
} catch (IllegalArgumentException unused) {
customEventNativeListener.onNativeAdFailed(NativeErrorCode.UNSPECIFIED);
}
}
}