1313import android .widget .TextView ;
1414
1515import com .google .android .gms .ads .formats .AdChoicesView ;
16- import com .google .android .gms .ads .formats .NativeAdView ;
17- import com .google .android .gms .ads .formats .NativeAppInstallAdView ;
18- import com .google .android .gms .ads .formats .NativeContentAdView ;
16+ import com .google .android .gms .ads .formats .MediaView ;
17+ import com .google .android .gms .ads .formats .UnifiedNativeAdView ;
1918import com .mopub .common .logging .MoPubLog ;
2019import com .mopub .nativeads .GooglePlayServicesNative .GooglePlayServicesNativeAd ;
2120
22- import java .util .Locale ;
2321import java .util .Map ;
2422import java .util .WeakHashMap ;
2523
24+ /**
25+ * The {@link GooglePlayServicesAdRenderer} class is used to render
26+ * GooglePlayServicesStaticNativeAds.
27+ */
2628public class GooglePlayServicesAdRenderer implements MoPubAdRenderer <GooglePlayServicesNativeAd > {
2729
2830 /**
@@ -65,14 +67,14 @@ public class GooglePlayServicesAdRenderer implements MoPubAdRenderer<GooglePlayS
6567 /**
6668 * A view binder containing the layout resource and views to be rendered by the renderer.
6769 */
68- private final ViewBinder mViewBinder ;
70+ private final MediaViewBinder mViewBinder ;
6971
7072 /**
7173 * A weak hash map used to keep track of view holder so that the views can be properly recycled.
7274 */
7375 private final WeakHashMap <View , GoogleStaticNativeViewHolder > mViewHolderMap ;
7476
75- public GooglePlayServicesAdRenderer (ViewBinder viewBinder ) {
77+ public GooglePlayServicesAdRenderer (MediaViewBinder viewBinder ) {
7678 this .mViewBinder = viewBinder ;
7779 this .mViewHolderMap = new WeakHashMap <>();
7880 }
@@ -98,47 +100,35 @@ public void renderAdView(@NonNull View view,
98100 viewHolder = GoogleStaticNativeViewHolder .fromViewBinder (view , mViewBinder );
99101 mViewHolderMap .put (view , viewHolder );
100102 }
103+ removeGoogleUnifiedAdView (view , nativeAd .shouldSwapMargins ());
101104
102- removeGoogleNativeAdView (view , nativeAd . shouldSwapMargins ());
105+ UnifiedNativeAdView unifiedAdView = new UnifiedNativeAdView (view . getContext ());
103106
104- NativeAdView nativeAdView = null ;
105- if (nativeAd .isNativeAppInstallAd ()) {
106- nativeAdView = new NativeAppInstallAdView (view .getContext ());
107- updateAppInstallAdView (nativeAd , viewHolder , (NativeAppInstallAdView ) nativeAdView );
108- } else if (nativeAd .isNativeContentAd ()) {
109- nativeAdView = new NativeContentAdView (view .getContext ());
110- updateContentAdView (nativeAd , viewHolder , (NativeContentAdView ) nativeAdView );
111- }
112-
113- if (nativeAdView != null ) {
114- insertGoogleNativeAdView (nativeAdView , view , nativeAd .shouldSwapMargins ());
115- } else {
116- Log .w (GooglePlayServicesNative .TAG ,
117- "Couldn't add Google native ad view. NativeAdView is null." );
118- }
107+ updateUnifiedAdview (nativeAd , viewHolder , unifiedAdView );
108+ insertGoogleUnifiedAdView (unifiedAdView , view , nativeAd .shouldSwapMargins ());
119109 }
120110
121111 /**
122- * This method will add the given Google native ad view into the view hierarchy of the given
112+ * This method will add the given Google unified ad view into the view hierarchy of the given
123113 * MoPub native ad view.
124114 *
125- * @param googleNativeAdView Google's native ad view to be added as a parent to the MoPub's
126- * view.
127- * @param moPubNativeAdView MoPub's native ad view created by this renderer.
128- * @param swapMargins {@code true} if the margins need to be swapped, {@code false}
129- * otherwise.
115+ * @param googleUnifiedAdView Google's unified ad view to be added as a parent to the MoPub's
116+ * view.
117+ * @param moPubNativeAdView MoPub's native ad view created by this renderer.
118+ * @param swapMargins {@code true} if the margins need to be swapped, {@code false}
119+ * otherwise.
130120 */
131- private static void insertGoogleNativeAdView ( NativeAdView googleNativeAdView ,
132- View moPubNativeAdView ,
133- boolean swapMargins ) {
121+ private static void insertGoogleUnifiedAdView ( UnifiedNativeAdView googleUnifiedAdView ,
122+ View moPubNativeAdView ,
123+ boolean swapMargins ) {
134124 if (moPubNativeAdView instanceof FrameLayout
135125 && moPubNativeAdView .getId () == ID_WRAPPING_FRAME ) {
136- googleNativeAdView .setId (ID_GOOGLE_NATIVE_VIEW );
126+ googleUnifiedAdView .setId (ID_GOOGLE_NATIVE_VIEW );
137127 FrameLayout outerFrame = (FrameLayout ) moPubNativeAdView ;
138128 View actualView = outerFrame .getChildAt (0 );
139129
140130 if (swapMargins ) {
141- // Google native ad view renders the AdChoices icon in one of the four corners of
131+ // Google unified ad view renders the AdChoices icon in one of the four corners of
142132 // its view. If a margin is specified on the actual ad view, the AdChoices view
143133 // might be rendered outside the actual ad view. Moving the margins from the
144134 // actual ad view to Google native ad view will make sure that the AdChoices icon
@@ -151,30 +141,30 @@ private static void insertGoogleNativeAdView(NativeAdView googleNativeAdView,
151141 actualViewParams .topMargin ,
152142 actualViewParams .rightMargin ,
153143 actualViewParams .bottomMargin );
154- googleNativeAdView .setLayoutParams (googleNativeAdViewParams );
144+ googleUnifiedAdView .setLayoutParams (googleNativeAdViewParams );
155145 actualViewParams .setMargins (0 , 0 , 0 , 0 );
156146 } else {
157- googleNativeAdView .setLayoutParams (new ViewGroup .LayoutParams (
147+ googleUnifiedAdView .setLayoutParams (new ViewGroup .LayoutParams (
158148 ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT ));
159149 }
160150
161151 outerFrame .removeView (actualView );
162- googleNativeAdView .addView (actualView , 0 );
163- outerFrame .addView (googleNativeAdView );
152+ googleUnifiedAdView .addView (actualView );
153+ outerFrame .addView (googleUnifiedAdView );
164154 } else {
165155 Log .w (GooglePlayServicesNative .TAG ,
166156 "Couldn't add Google native ad view. Wrapping view not found." );
167157 }
168158 }
169159
170160 /**
171- * This method will remove the Google native ad view from the view hierarchy if one is present.
161+ * This method will remove the Google unified ad view from the view hierarchy if one is present.
172162 *
173- * @param view the view from which to remove the Google native ad view.
163+ * @param view the view from which to remove the Google unified ad view.
174164 * @param swapMargins {@code true} if the margins need to be swapped before removing the
175165 * Google native ad view, {@code false} otherwise.
176166 */
177- protected static void removeGoogleNativeAdView (@ NonNull View view , boolean swapMargins ) {
167+ protected static void removeGoogleUnifiedAdView (@ NonNull View view , boolean swapMargins ) {
178168 if (view instanceof FrameLayout && view .getId () == ID_WRAPPING_FRAME ) {
179169 View adView = view .findViewById (ID_GOOGLE_NATIVE_VIEW );
180170 if (adView != null ) {
@@ -198,123 +188,66 @@ protected static void removeGoogleNativeAdView(@NonNull View view, boolean swapM
198188 outerView .addView (actualNativeView , index );
199189 }
200190
201- if (adView instanceof NativeAdView ) {
202- ((NativeAdView ) adView ).destroy ();
191+ if (adView instanceof UnifiedNativeAdView ) {
192+ ((UnifiedNativeAdView ) adView ).destroy ();
203193 }
204194 }
205195 }
206196 }
207197
208198 /**
209199 * This method will render the given native ad view using the native ad and set the views to
210- * Google's native content ad view.
200+ * Google's native ad view.
211201 *
212202 * @param staticNativeAd a static native ad object containing the required assets to
213203 * set to the native ad view.
214204 * @param staticNativeViewHolder a static native view holder object containing the mapped
215205 * views from the view binder.
216- * @param contentAdView the Google native content ad view in the view hierarchy.
206+ * @param unifiedAdView the Google unified ad view in the view hierarchy.
217207 */
218- private void updateContentAdView (GooglePlayServicesNativeAd staticNativeAd ,
208+
209+
210+ private void updateUnifiedAdview (GooglePlayServicesNativeAd staticNativeAd ,
219211 GoogleStaticNativeViewHolder staticNativeViewHolder ,
220- NativeContentAdView contentAdView ) {
212+ UnifiedNativeAdView unifiedAdView ) {
221213 NativeRendererHelper .addTextView (
222214 staticNativeViewHolder .mTitleView , staticNativeAd .getTitle ());
223- contentAdView .setHeadlineView (staticNativeViewHolder .mTitleView );
215+ unifiedAdView .setHeadlineView (staticNativeViewHolder .mTitleView );
224216 NativeRendererHelper .addTextView (
225217 staticNativeViewHolder .mTextView , staticNativeAd .getText ());
226- contentAdView .setBodyView (staticNativeViewHolder .mTextView );
218+ unifiedAdView .setBodyView (staticNativeViewHolder .mTextView );
219+ if (staticNativeViewHolder .mMediaView != null ) {
220+ MediaView mediaview = new MediaView (unifiedAdView .getContext ());
221+ staticNativeViewHolder .mMediaView .removeAllViews ();
222+ staticNativeViewHolder .mMediaView .addView (mediaview );
223+ unifiedAdView .setMediaView (mediaview );
224+
225+ }
227226 NativeRendererHelper .addTextView (staticNativeViewHolder .mCallToActionView ,
228227 staticNativeAd .getCallToAction ());
229- contentAdView .setCallToActionView (staticNativeViewHolder .mCallToActionView );
230- NativeImageHelper .loadImageView (staticNativeAd .getMainImageUrl (),
231- staticNativeViewHolder .mMainImageView );
232- contentAdView .setImageView (staticNativeViewHolder .mMainImageView );
228+ unifiedAdView .setCallToActionView (staticNativeViewHolder .mCallToActionView );
233229 NativeImageHelper .loadImageView (staticNativeAd .getIconImageUrl (),
234230 staticNativeViewHolder .mIconImageView );
235- contentAdView . setLogoView (staticNativeViewHolder .mIconImageView );
231+ unifiedAdView . setImageView (staticNativeViewHolder .mIconImageView );
236232 if (staticNativeAd .getAdvertiser () != null ) {
237233 NativeRendererHelper .addTextView (
238234 staticNativeViewHolder .mAdvertiserTextView , staticNativeAd .getAdvertiser ());
239- contentAdView .setAdvertiserView (staticNativeViewHolder .mAdvertiserTextView );
235+ unifiedAdView .setAdvertiserView (staticNativeViewHolder .mAdvertiserTextView );
240236 }
241-
242237 // Add the AdChoices icon to the container if one is provided by the publisher.
243238 if (staticNativeViewHolder .mAdChoicesIconContainer != null ) {
244- AdChoicesView adChoicesView = new AdChoicesView (contentAdView .getContext ());
239+ AdChoicesView adChoicesView = new AdChoicesView (unifiedAdView .getContext ());
245240 staticNativeViewHolder .mAdChoicesIconContainer .removeAllViews ();
246241 staticNativeViewHolder .mAdChoicesIconContainer .addView (adChoicesView );
247- contentAdView .setAdChoicesView (adChoicesView );
242+ unifiedAdView .setAdChoicesView (adChoicesView );
248243 }
249244
250245 // Set the privacy information icon to null as the Google Mobile Ads SDK automatically
251246 // renders the AdChoices icon.
252247 NativeRendererHelper .addPrivacyInformationIcon (
253248 staticNativeViewHolder .mPrivacyInformationIconImageView , null , null );
254249
255- contentAdView .setNativeAd (staticNativeAd .getContentAd ());
256- }
257-
258- /**
259- * This method will render the given native ad view using the native ad and set the views to
260- * Google's native app install ad view.
261- *
262- * @param staticNativeAd a static native ad object containing the required assets to set
263- * to the native ad view.
264- * @param staticNativeViewHolder a static native view holder object containing the mapped
265- * views from the view binder.
266- * @param appInstallAdView the Google native app install ad view in the view hierarchy.
267- */
268- private void updateAppInstallAdView (GooglePlayServicesNativeAd staticNativeAd ,
269- GoogleStaticNativeViewHolder staticNativeViewHolder ,
270- NativeAppInstallAdView appInstallAdView ) {
271-
272- NativeRendererHelper .addTextView (
273- staticNativeViewHolder .mTitleView , staticNativeAd .getTitle ());
274- appInstallAdView .setHeadlineView (staticNativeViewHolder .mTitleView );
275- NativeRendererHelper .addTextView (
276- staticNativeViewHolder .mTextView , staticNativeAd .getText ());
277- appInstallAdView .setBodyView (staticNativeViewHolder .mTextView );
278- NativeRendererHelper .addTextView (
279- staticNativeViewHolder .mCallToActionView , staticNativeAd .getCallToAction ());
280- appInstallAdView .setCallToActionView (staticNativeViewHolder .mCallToActionView );
281- NativeImageHelper .loadImageView (
282- staticNativeAd .getMainImageUrl (), staticNativeViewHolder .mMainImageView );
283- appInstallAdView .setImageView (staticNativeViewHolder .mMainImageView );
284- NativeImageHelper .loadImageView (
285- staticNativeAd .getIconImageUrl (), staticNativeViewHolder .mIconImageView );
286- appInstallAdView .setIconView (staticNativeViewHolder .mIconImageView );
287- if (staticNativeAd .getStarRating () != null ) {
288- NativeRendererHelper .addTextView (staticNativeViewHolder .mStarRatingTextView ,
289- String .format (
290- Locale .getDefault (), "%.1f/5 Stars" , staticNativeAd .getStarRating ()));
291- appInstallAdView .setStarRatingView (staticNativeViewHolder .mStarRatingTextView );
292- }
293- if (staticNativeAd .getPrice () != null ) {
294- NativeRendererHelper .addTextView (
295- staticNativeViewHolder .mPriceTextView , staticNativeAd .getPrice ());
296- appInstallAdView .setPriceView (staticNativeViewHolder .mPriceTextView );
297- }
298- if (staticNativeAd .getStore () != null ) {
299- NativeRendererHelper .addTextView (
300- staticNativeViewHolder .mStoreTextView , staticNativeAd .getStore ());
301- appInstallAdView .setStoreView (staticNativeViewHolder .mStoreTextView );
302- }
303-
304- // Set the privacy information icon to null as the Google Mobile Ads SDK automatically
305- // renders the AdChoices icon.
306- NativeRendererHelper .addPrivacyInformationIcon (
307- staticNativeViewHolder .mPrivacyInformationIconImageView , null , null );
308-
309- // Add the AdChoices icon to the container if one is provided by the publisher.
310- if (staticNativeViewHolder .mAdChoicesIconContainer != null ) {
311- AdChoicesView adChoicesView = new AdChoicesView (appInstallAdView .getContext ());
312- staticNativeViewHolder .mAdChoicesIconContainer .removeAllViews ();
313- staticNativeViewHolder .mAdChoicesIconContainer .addView (adChoicesView );
314- appInstallAdView .setAdChoicesView (adChoicesView );
315- }
316-
317- appInstallAdView .setNativeAd (staticNativeAd .getAppInstallAd ());
250+ unifiedAdView .setNativeAd (staticNativeAd .getUnifiedNativeAd ());
318251 }
319252
320253 @ Override
@@ -332,8 +265,6 @@ private static class GoogleStaticNativeViewHolder {
332265 @ Nullable
333266 TextView mCallToActionView ;
334267 @ Nullable
335- ImageView mMainImageView ;
336- @ Nullable
337268 ImageView mIconImageView ;
338269 @ Nullable
339270 ImageView mPrivacyInformationIconImageView ;
@@ -347,26 +278,30 @@ private static class GoogleStaticNativeViewHolder {
347278 TextView mPriceTextView ;
348279 @ Nullable
349280 FrameLayout mAdChoicesIconContainer ;
281+ @ Nullable
282+ MediaLayout mMediaView ;
350283
351284 private static final GoogleStaticNativeViewHolder EMPTY_VIEW_HOLDER =
352285 new GoogleStaticNativeViewHolder ();
353286
354287 @ NonNull
355288 public static GoogleStaticNativeViewHolder fromViewBinder (@ NonNull View view ,
356- @ NonNull ViewBinder viewBinder ) {
289+ @ NonNull MediaViewBinder
290+ viewBinder ) {
357291 final GoogleStaticNativeViewHolder viewHolder = new GoogleStaticNativeViewHolder ();
358292 viewHolder .mMainView = view ;
359293 try {
360294 viewHolder .mTitleView = (TextView ) view .findViewById (viewBinder .titleId );
361295 viewHolder .mTextView = (TextView ) view .findViewById (viewBinder .textId );
362296 viewHolder .mCallToActionView =
363297 (TextView ) view .findViewById (viewBinder .callToActionId );
364- viewHolder .mMainImageView =
365- (ImageView ) view .findViewById (viewBinder .mainImageId );
298+
366299 viewHolder .mIconImageView =
367300 (ImageView ) view .findViewById (viewBinder .iconImageId );
368301 viewHolder .mPrivacyInformationIconImageView =
369302 (ImageView ) view .findViewById (viewBinder .privacyInformationIconImageId );
303+ viewHolder .mMediaView =
304+ (MediaLayout ) view .findViewById (viewBinder .mediaLayoutId );
370305 Map <String , Integer > extraViews = viewBinder .extras ;
371306 Integer starRatingTextViewId = extraViews .get (VIEW_BINDER_KEY_STAR_RATING );
372307 if (starRatingTextViewId != null ) {
@@ -399,4 +334,5 @@ public static GoogleStaticNativeViewHolder fromViewBinder(@NonNull View view,
399334 }
400335 }
401336 }
337+
402338}
0 commit comments