-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ad2ictionNativeAdRenderer.java
145 lines (120 loc) · 5.11 KB
/
Ad2ictionNativeAdRenderer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.mopub.nativeads;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.ad2iction.common.VisibleForTesting;
import com.ad2iction.nativeads.NativeResponse;
import com.mopub.common.Preconditions;
import java.util.Iterator;
import java.util.WeakHashMap;
public class Ad2ictionNativeAdRenderer
implements MoPubAdRenderer<Ad2ictionCustomEventNative.Ad2ictionForwardingNativeAd> {
private static final String LOG_TAG = Ad2ictionNativeAdRenderer.class.getSimpleName();
@NonNull private final ViewBinder mViewBinder;
@NonNull private final WeakHashMap<View, Ad2ictionNativeViewHolder> mViewHolderMap;
public Ad2ictionNativeAdRenderer(@NonNull final ViewBinder viewBinder) {
mViewBinder = viewBinder;
mViewHolderMap = new WeakHashMap<>();
}
@Override
@NonNull
public View createAdView(@NonNull final Context context, final ViewGroup parent) {
return LayoutInflater.from(context).inflate(this.mViewBinder.layoutId, parent, false);
}
@Override
public void renderAdView(@NonNull final View view, @NonNull
final Ad2ictionCustomEventNative.Ad2ictionForwardingNativeAd ad2ictionForwardingNativeAd) {
Ad2ictionNativeViewHolder nativeViewHolder = mViewHolderMap.get(view);
if (nativeViewHolder == null) {
nativeViewHolder = Ad2ictionNativeViewHolder.fromViewBinder(view, this.mViewBinder);
mViewHolderMap.put(view, nativeViewHolder);
}
nativeViewHolder.update(ad2ictionForwardingNativeAd.getNativeResponse());
nativeViewHolder
.updateExtras(view, ad2ictionForwardingNativeAd.getNativeResponse(), mViewBinder);
setViewVisibility(nativeViewHolder, View.VISIBLE);
}
private void setViewVisibility(@NonNull final Ad2ictionNativeViewHolder viewHolder,
final int visibility) {
if (viewHolder.staticNativeViewHolder.mainView != null) {
viewHolder.staticNativeViewHolder.mainView.setVisibility(visibility);
}
}
@Override
public boolean supports(@NonNull final BaseNativeAd nativeAd) {
Preconditions.checkNotNull(nativeAd);
return nativeAd instanceof Ad2ictionCustomEventNative.Ad2ictionForwardingNativeAd;
}
private static class Ad2ictionNativeViewHolder {
private final StaticNativeViewHolder staticNativeViewHolder;
@Nullable TextView titleView;
@Nullable TextView textView;
@Nullable TextView callToActionView;
@Nullable ImageView mainImageView;
@Nullable ImageView iconImageView;
@VisibleForTesting
private Ad2ictionNativeViewHolder(final StaticNativeViewHolder staticNativeViewHolder) {
this.staticNativeViewHolder = staticNativeViewHolder;
}
static Ad2ictionNativeViewHolder fromViewBinder(final View view,
final ViewBinder viewBinder) {
StaticNativeViewHolder staticNativeViewHolder =
StaticNativeViewHolder.fromViewBinder(view, viewBinder);
Ad2ictionNativeViewHolder nativeViewHolder =
new Ad2ictionNativeViewHolder(staticNativeViewHolder);
nativeViewHolder.titleView = staticNativeViewHolder.titleView;
nativeViewHolder.textView = staticNativeViewHolder.textView;
nativeViewHolder.callToActionView = staticNativeViewHolder.callToActionView;
nativeViewHolder.mainImageView = staticNativeViewHolder.mainImageView;
nativeViewHolder.iconImageView = staticNativeViewHolder.iconImageView;
return nativeViewHolder;
}
void update(@NonNull NativeResponse nativeResponse) {
this.addTextView(this.titleView, nativeResponse.getTitle());
this.addTextView(this.textView, nativeResponse.getText());
this.addTextView(this.callToActionView, nativeResponse.getCallToAction());
nativeResponse.loadMainImage(this.mainImageView);
nativeResponse.loadIconImage(this.iconImageView);
}
void updateExtras(@NonNull View outerView, @NonNull NativeResponse nativeResponse,
@NonNull ViewBinder viewBinder) {
Iterator i$ = viewBinder.extras.keySet().iterator();
while (i$.hasNext()) {
String key = (String) i$.next();
int resourceId = viewBinder.extras.get(key);
View view = outerView.findViewById(resourceId);
Object content = nativeResponse.getExtra(key);
if (view instanceof ImageView) {
((ImageView) view).setImageDrawable(null);
nativeResponse.loadExtrasImage(key, (ImageView) view);
} else if (view instanceof TextView) {
((TextView) view).setText(null);
if (content instanceof String) {
this.addTextView((TextView) view, (String) content);
}
} else {
Log.d(LOG_TAG, "View bound to " + key +
" should be an instance of TextView or ImageView.");
}
}
}
private void addTextView(@Nullable TextView textView, @Nullable String contents) {
if (textView == null) {
Log.d(LOG_TAG, "Attempted to add text (" + contents + ") to null TextView.");
} else {
textView.setText(null);
if (contents == null) {
Log.d(LOG_TAG, "Attempted to set TextView contents to null.");
} else {
textView.setText(contents);
}
}
}
}
}