This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathFlurryCustomEventNative.java
621 lines (526 loc) · 23.5 KB
/
FlurryCustomEventNative.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
package com.mopub.nativeads;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import com.flurry.android.FlurryAgentListener;
import com.flurry.android.ads.FlurryAdErrorType;
import com.flurry.android.ads.FlurryAdNative;
import com.flurry.android.ads.FlurryAdNativeAsset;
import com.flurry.android.ads.FlurryAdNativeListener;
import com.flurry.android.ads.FlurryAdTargeting;
import com.mopub.mobileads.FlurryAgentWrapper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public final class FlurryCustomEventNative extends CustomEventNative {
/**
* Extra image asset for the star-rating of app-install ads.
*/
public static final String EXTRA_STAR_RATING_IMG = "flurry_starratingimage";
/**
* Extra text asset specifying the category for app-install ads.
*/
public static final String EXTRA_APP_CATEGORY = "flurry_appcategorytext";
/**
* Extra image asset for the Flurry native ad branding logo.
*/
public static final String EXTRA_SEC_BRANDING_LOGO = "flurry_brandingimage";
/**
* Key for enabling Flurry debug logging. After manually creating a {@link MoPubNative} object,
* pass in <code>true</code> as a value for this key in the map passed to
* {@link MoPubNative#setLocalExtras(Map)}.
*
* E.g.
*
* <blockquote><pre>
* {@code
* Map<String, Object> adapterExtras = new TreeMap<String, Object>();
* adapterExtras.put(FlurryCustomEventNative.LOCAL_EXTRA_TEST_MODE, true);
* mMoPubNative.setLocalExtras(adapterExtras);
* }
* </pre></blockquote>
*
* Debug logging is disabled on the Flurry SDK by default.
*/
public static final String LOCAL_EXTRA_TEST_MODE = "enableTestMode";
private static final String LOG_TAG = FlurryCustomEventNative.class.getSimpleName();
private static final String ASSET_SEC_HQ_IMAGE = "secHqImage";
private static final String ASSET_SEC_IMAGE = "secImage";
private static final String ASSET_SEC_HQ_RATING_IMG = "secHqRatingImg";
private static final String ASSET_SEC_HQ_BRANDING_LOGO = "secHqBrandingLogo";
private static final String ASSET_SEC_RATING_IMG = "secRatingImg";
private static final String ASSET_APP_RATING = "appRating";
private static final String ASSET_APP_CATEGORY = "appCategory";
private static final String ASSET_HEADLINE = "headline";
private static final String ASSET_SUMMARY = "summary";
private static final String ASSET_CALL_TO_ACTION = "callToAction";
private static final String ASSET_VIDEO = "videoUrl";
private static final double MOPUB_STAR_RATING_SCALE = StaticNativeAd.MAX_STAR_RATING;
@Override
protected void loadNativeAd(@NonNull final Context context,
@NonNull final CustomEventNativeListener customEventNativeListener,
@NonNull final Map<String, Object> localExtras,
@NonNull final Map<String, String> serverExtras) {
final String flurryApiKey;
final String flurryAdSpace;
//Get the FLURRY_APIKEY and FLURRY_ADSPACE from the server.
if (validateExtras(serverExtras)) {
flurryApiKey = serverExtras.get(FlurryAgentWrapper.PARAM_API_KEY);
flurryAdSpace = serverExtras.get(FlurryAgentWrapper.PARAM_AD_SPACE_NAME);
if (FlurryAgentWrapper.getInstance().isSessionActive()) {
fetchFlurryAd(context, flurryAdSpace, localExtras, customEventNativeListener);
} else {
final FlurryAgentListener flurryAgentListener = new FlurryAgentListener() {
@Override
public void onSessionStarted() {
fetchFlurryAd(context, flurryAdSpace, localExtras,
customEventNativeListener);
}
};
FlurryAgentWrapper.getInstance().startSession(context, flurryApiKey,
flurryAgentListener);
}
} else {
customEventNativeListener.onNativeAdFailed(
NativeErrorCode.NATIVE_ADAPTER_CONFIGURATION_ERROR);
Log.i(LOG_TAG, "Failed Native AdFetch: Missing required server extras" +
" [FLURRY_APIKEY and/or FLURRY_ADSPACE].");
}
}
private static synchronized void mapNativeAd(@NonNull final FlurryBaseNativeAd mopubSupportedAd,
@NonNull final FlurryAdNative flurryAdNative) {
FlurryAdNativeAsset coverImageAsset = flurryAdNative.getAsset(ASSET_SEC_HQ_IMAGE);
FlurryAdNativeAsset iconImageAsset = flurryAdNative.getAsset(ASSET_SEC_IMAGE);
if (coverImageAsset != null && !TextUtils.isEmpty(coverImageAsset.getValue())) {
mopubSupportedAd.setMainImageUrl(coverImageAsset.getValue());
}
if (iconImageAsset != null && !TextUtils.isEmpty(iconImageAsset.getValue())) {
mopubSupportedAd.setIconImageUrl(iconImageAsset.getValue());
}
mopubSupportedAd.setTitle(flurryAdNative.getAsset(ASSET_HEADLINE).getValue());
mopubSupportedAd.setText(flurryAdNative.getAsset(ASSET_SUMMARY).getValue());
mopubSupportedAd.addExtra(EXTRA_SEC_BRANDING_LOGO,
flurryAdNative.getAsset(ASSET_SEC_HQ_BRANDING_LOGO).getValue());
if (mopubSupportedAd.isAppInstallAd()) {
// App rating image URL may be null
FlurryAdNativeAsset ratingHqImageAsset = flurryAdNative
.getAsset(ASSET_SEC_HQ_RATING_IMG);
if (ratingHqImageAsset != null && !TextUtils.isEmpty(ratingHqImageAsset.getValue())) {
mopubSupportedAd.addExtra(EXTRA_STAR_RATING_IMG, ratingHqImageAsset.getValue());
} else {
FlurryAdNativeAsset ratingImageAsset = flurryAdNative
.getAsset(ASSET_SEC_RATING_IMG);
if (ratingImageAsset != null && !TextUtils.isEmpty(ratingImageAsset.getValue())) {
mopubSupportedAd.addExtra(EXTRA_STAR_RATING_IMG, ratingImageAsset.getValue());
}
}
FlurryAdNativeAsset appCategoryAsset = flurryAdNative.getAsset(ASSET_APP_CATEGORY);
if (appCategoryAsset != null) {
mopubSupportedAd.addExtra(EXTRA_APP_CATEGORY, appCategoryAsset.getValue());
}
FlurryAdNativeAsset appRatingAsset = flurryAdNative.getAsset(ASSET_APP_RATING);
if (appRatingAsset != null) {
mopubSupportedAd.setStarRating(getStarRatingValue(appRatingAsset.getValue()));
}
}
FlurryAdNativeAsset ctaAsset = flurryAdNative.getAsset(ASSET_CALL_TO_ACTION);
if (ctaAsset != null) {
mopubSupportedAd.setCallToAction(ctaAsset.getValue());
}
if (mopubSupportedAd.getImageUrls().isEmpty()) {
Log.d(LOG_TAG, "preCacheImages: No images to cache for Flurry Native Ad: " +
flurryAdNative.toString());
mopubSupportedAd.onNativeAdLoaded();
} else {
mopubSupportedAd.precacheImages();
}
}
@Nullable
private static Double getStarRatingValue(@Nullable final String appRatingString) {
// App rating String should be of the form X/Y. E.g. 80/100
Double rating = null;
if (appRatingString != null) {
String[] ratingParts = appRatingString.split("/");
if (ratingParts.length == 2) {
try {
float numer = Integer.valueOf(ratingParts[0]);
float denom = Integer.valueOf(ratingParts[1]);
rating = (numer / denom) * MOPUB_STAR_RATING_SCALE;
} catch (NumberFormatException e) { /*Ignore and return null*/ }
}
}
return rating;
}
private boolean validateExtras(final Map<String, String> serverExtras) {
final String flurryApiKey = serverExtras.get(FlurryAgentWrapper.PARAM_API_KEY);
final String flurryAdSpace = serverExtras.get(FlurryAgentWrapper.PARAM_AD_SPACE_NAME);
Log.i(LOG_TAG, "ServerInfo fetched from Mopub " + FlurryAgentWrapper.PARAM_API_KEY + " : "
+ flurryApiKey + " and " + FlurryAgentWrapper.PARAM_AD_SPACE_NAME + " :" +
flurryAdSpace);
return (!TextUtils.isEmpty(flurryApiKey) && !TextUtils.isEmpty(flurryAdSpace));
}
private void fetchFlurryAd(@NonNull Context context, String flurryAdSpace,
@NonNull Map<String, Object> localExtras,
@NonNull CustomEventNativeListener customEventNativeListener) {
final FlurryAdNative flurryAdNative = new FlurryAdNative(context, flurryAdSpace);
if (localExtras.containsKey(LOCAL_EXTRA_TEST_MODE) &&
localExtras.get(LOCAL_EXTRA_TEST_MODE) instanceof Boolean) {
final FlurryAdTargeting targeting = new FlurryAdTargeting();
targeting.setEnableTestAds((Boolean) localExtras.get(LOCAL_EXTRA_TEST_MODE));
}
final FlurryBaseNativeAd flurryNativeAd;
if (shouldAllowVideoNativeAds()) {
flurryNativeAd = new FlurryVideoEnabledNativeAd(context, flurryAdNative,
customEventNativeListener);
} else {
flurryNativeAd = new FlurryStaticNativeAd(context, flurryAdNative,
customEventNativeListener);
}
flurryNativeAd.fetchAd();
}
private boolean shouldAllowVideoNativeAds() {
try {
Class.forName("com.mopub.nativeads.FlurryNativeAdRenderer");
} catch (ClassNotFoundException e) {
return false;
}
return true;
}
/**
* Class that supports Flurry static native ads with the help of the
* {@link MoPubStaticNativeAdRenderer}.
*
* @see FlurryVideoEnabledNativeAd
*/
static class FlurryStaticNativeAd extends StaticNativeAd implements FlurryBaseNativeAd {
private @NonNull final Context mContext;
private @NonNull final CustomEventNativeListener mCustomEventNativeListener;
private @NonNull final FlurryAdNative mFlurryAdNative;
private final FlurryAdNativeListener mFlurryNativelistener = new FlurryBaseAdListener(this) {
@Override
public void onClicked(final FlurryAdNative flurryAdNative) {
super.onClicked(flurryAdNative);
notifyAdClicked();
}
@Override
public void onImpressionLogged(final FlurryAdNative flurryAdNative) {
super.onImpressionLogged(flurryAdNative);
notifyAdImpressed();
}
@Override
public void onError(final FlurryAdNative adNative,
final FlurryAdErrorType adErrorType,
final int errorCode) {
super.onError(adNative, adErrorType, errorCode);
mCustomEventNativeListener.onNativeAdFailed(NativeErrorCode.NETWORK_NO_FILL);
}
};
FlurryStaticNativeAd(@NonNull Context context, @NonNull FlurryAdNative adNative,
@NonNull CustomEventNativeListener mCustomEventNativeListener) {
this.mContext = context;
this.mFlurryAdNative = adNative;
this.mCustomEventNativeListener = mCustomEventNativeListener;
}
// region StaticNativeAd
@Override
public void prepare(@NonNull final View view) {
mFlurryAdNative.setTrackingView(view);
Log.d(LOG_TAG, "prepare(" + mFlurryAdNative.toString() + " " + view.toString() + ")");
}
@Override
public void clear(@NonNull final View view) {
mFlurryAdNative.removeTrackingView();
Log.d(LOG_TAG, "clear(" + mFlurryAdNative.toString() + ")");
}
@Override
public void destroy() {
Log.d(LOG_TAG, "destroy(" + mFlurryAdNative.toString() + ") started.");
mFlurryAdNative.destroy();
FlurryAgentWrapper.getInstance().endSession(mContext);
}
//endregion
// region FlurryBaseNativeAd
@Override
public synchronized void fetchAd() {
Log.d(LOG_TAG, "Fetching Flurry Native Ad now.");
mFlurryAdNative.setListener(mFlurryNativelistener);
mFlurryAdNative.fetchAd();
}
public @NonNull List<String> getImageUrls() {
final List<String> imageUrls = new ArrayList<>(2);
final String mainImageUrl = getMainImageUrl();
if (mainImageUrl != null) {
imageUrls.add(getMainImageUrl());
Log.d(LOG_TAG, "Flurry Native Ad main image found.");
}
final String iconUrl = getIconImageUrl();
if (iconUrl != null) {
imageUrls.add(this.getIconImageUrl());
Log.d(LOG_TAG, "Flurry Native Ad icon image found.");
}
return imageUrls;
}
public boolean isAppInstallAd() {
return mFlurryAdNative.getAsset(ASSET_SEC_RATING_IMG) != null ||
mFlurryAdNative.getAsset(ASSET_SEC_HQ_RATING_IMG) != null ||
mFlurryAdNative.getAsset(ASSET_APP_CATEGORY) != null;
}
@Override
public void onNativeAdLoaded() {
mCustomEventNativeListener.onNativeAdLoaded(this);
}
@Override
public void precacheImages() {
NativeImageHelper.preCacheImages(mContext, getImageUrls(),
new NativeImageHelper.ImageListener() {
@Override
public void onImagesCached() {
Log.d(LOG_TAG, "preCacheImages: Ad image cached.");
mCustomEventNativeListener.onNativeAdLoaded(FlurryStaticNativeAd.this);
}
@Override
public void onImagesFailedToCache(final NativeErrorCode errorCode) {
mCustomEventNativeListener.onNativeAdFailed(errorCode);
Log.d(LOG_TAG, "preCacheImages: Unable to cache Ad image. Error["
+ errorCode.toString() + "]");
}
});
}
//endregion
}
/**
* Class that supports both Flurry static and video native ads with the help of
* {@link FlurryNativeAdRenderer}. This class does not need to be included if you
* are fetching only static ads. Use {@link FlurryStaticNativeAd} instead.
*/
static class FlurryVideoEnabledNativeAd extends BaseNativeAd implements FlurryBaseNativeAd {
private @NonNull final Context mContext;
private @NonNull final CustomEventNativeListener mCustomEventNativeListener;
private @NonNull final FlurryAdNative mFlurryAdNative;
private final FlurryAdNativeListener mFlurryNativelistener = new FlurryBaseAdListener(this) {
@Override
public void onClicked(final FlurryAdNative flurryAdNative) {
super.onClicked(flurryAdNative);
notifyAdClicked();
}
@Override
public void onImpressionLogged(final FlurryAdNative flurryAdNative) {
super.onImpressionLogged(flurryAdNative);
notifyAdImpressed();
}
@Override
public void onError(final FlurryAdNative adNative,
final FlurryAdErrorType adErrorType,
final int errorCode) {
super.onError(adNative, adErrorType, errorCode);
mCustomEventNativeListener.onNativeAdFailed(NativeErrorCode.NETWORK_NO_FILL);
}
};
// Basic fields
@Nullable private String mTitle;
@Nullable private String mText;
@Nullable private String mCallToAction;
@Nullable private String mMainImageUrl;
@Nullable private String mIconImageUrl;
@Nullable private Double mStarRating;
// Extras
@NonNull private final Map<String, Object> mExtras;
FlurryVideoEnabledNativeAd(@NonNull Context context, @NonNull FlurryAdNative adNative,
@NonNull CustomEventNativeListener mCustomEventNativeListener) {
this.mContext = context;
this.mFlurryAdNative = adNative;
this.mCustomEventNativeListener = mCustomEventNativeListener;
this.mExtras = new HashMap<>();
}
// region StaticNativeAd
@Override
public void prepare(@NonNull View view) {
mFlurryAdNative.setTrackingView(view);
Log.d(LOG_TAG, "prepare(" + mFlurryAdNative.toString() + " " + view.toString() + ")");
}
@Override
public void clear(@NonNull View view) {
mFlurryAdNative.removeTrackingView();
Log.d(LOG_TAG, "clear(" + mFlurryAdNative.toString() + ")");
}
@Override
public void destroy() {
Log.d(LOG_TAG, "destroy(" + mFlurryAdNative.toString() + ") started.");
mFlurryAdNative.destroy();
FlurryAgentWrapper.getInstance().endSession(mContext);
}
// endregion
// region FlurryBaseNativeAd
@Override
public synchronized void fetchAd() {
Log.d(LOG_TAG, "Fetching Flurry Native Ad now.");
mFlurryAdNative.setListener(mFlurryNativelistener);
mFlurryAdNative.fetchAd();
}
@Override
public boolean isAppInstallAd() {
return mFlurryAdNative.getAsset(ASSET_SEC_RATING_IMG) != null ||
mFlurryAdNative.getAsset(ASSET_SEC_HQ_RATING_IMG) != null ||
mFlurryAdNative.getAsset(ASSET_APP_CATEGORY) != null;
}
@Override
public void precacheImages() {
NativeImageHelper.preCacheImages(mContext, getImageUrls(),
new NativeImageHelper.ImageListener() {
@Override
public void onImagesCached() {
Log.d(LOG_TAG, "preCacheImages: Ad image cached.");
mCustomEventNativeListener.onNativeAdLoaded(
FlurryVideoEnabledNativeAd.this);
}
@Override
public void onImagesFailedToCache(final NativeErrorCode errorCode) {
mCustomEventNativeListener.onNativeAdFailed(errorCode);
Log.d(LOG_TAG, "preCacheImages: Unable to cache Ad image. Error["
+ errorCode.toString() + "]");
}
});
}
@NonNull
@Override
public List<String> getImageUrls() {
final List<String> imageUrls = new ArrayList<>(2);
final String mainImageUrl = getMainImageUrl();
if (mainImageUrl != null) {
imageUrls.add(getMainImageUrl());
Log.d(LOG_TAG, "Flurry Native Ad main image found.");
}
final String iconUrl = getIconImageUrl();
if (iconUrl != null) {
imageUrls.add(this.getIconImageUrl());
Log.d(LOG_TAG, "Flurry Native Ad icon image found.");
}
return imageUrls;
}
@Nullable
@Override
public String getTitle() {
return mTitle;
}
@Nullable
@Override
public String getText() {
return mText;
}
@Nullable
@Override
public String getCallToAction() {
return mCallToAction;
}
@Nullable
@Override
public String getMainImageUrl() {
return mMainImageUrl;
}
@Nullable
@Override
public String getIconImageUrl() {
return mIconImageUrl;
}
@Nullable
@Override
public Double getStarRating() {
return mStarRating;
}
@NonNull
@Override
public Map<String, Object> getExtras() {
return mExtras;
}
@Override
public void setTitle(@Nullable String title) {
this.mTitle = title;
}
@Override
public void setText(@Nullable String text) {
this.mText = text;
}
@Override
public void setCallToAction(@Nullable String callToAction) {
this.mCallToAction = callToAction;
}
@Override
public void setMainImageUrl(@Nullable String mainImageUrl) {
this.mMainImageUrl = mainImageUrl;
}
@Override
public void setIconImageUrl(@Nullable String iconImageUrl) {
this.mIconImageUrl = iconImageUrl;
}
@Override
public void setStarRating(@Nullable Double starRating) {
this.mStarRating = starRating;
}
@Override
public void addExtra(@NonNull String key, @Nullable Object value) {
mExtras.put(key, value);
}
@Override
public void onNativeAdLoaded() {
mCustomEventNativeListener.onNativeAdLoaded(this);
}
// endregion
boolean isVideoAd() {
return mFlurryAdNative.isVideoAd();
}
void loadVideoIntoView(@NonNull ViewGroup videoView) {
mFlurryAdNative.getAsset(ASSET_VIDEO).loadAssetIntoView(videoView);
}
}
static abstract class FlurryBaseAdListener implements FlurryAdNativeListener {
private final @NonNull FlurryBaseNativeAd mBaseNativeAd;
FlurryBaseAdListener(@NonNull FlurryBaseNativeAd baseNativeAd) {
this.mBaseNativeAd = baseNativeAd;
}
@Override
public void onFetched(final FlurryAdNative flurryAdNative) {
Log.d(LOG_TAG, "onFetched: Native Ad fetched successfully!");
mapNativeAd(mBaseNativeAd, flurryAdNative);
}
@Override
public void onShowFullscreen(final FlurryAdNative flurryAdNative) {
Log.d(LOG_TAG, "onShowFullscreen");
}
@Override
public void onCloseFullscreen(final FlurryAdNative flurryAdNative) {
Log.d(LOG_TAG, "onCloseFullscreen");
}
@Override
public void onAppExit(final FlurryAdNative flurryAdNative) {
Log.d(LOG_TAG, "onAppExit");
}
@Override
public void onClicked(final FlurryAdNative flurryAdNative) {
Log.d(LOG_TAG, "onClicked");
}
@Override
public void onImpressionLogged(final FlurryAdNative flurryAdNative) {
Log.d(LOG_TAG, "onImpressionLogged");
}
@Override
public void onExpanded(final FlurryAdNative flurryAdNative) {
Log.d(LOG_TAG, "onExpanded");
}
@Override
public void onCollapsed(final FlurryAdNative flurryAdNative) {
Log.d(LOG_TAG, "onCollapsed");
}
@Override
public void onError(final FlurryAdNative flurryAdNative,
final FlurryAdErrorType adErrorType,
final int errorCode) {
Log.d(LOG_TAG, String.format("onError: Flurry native ad not available. " +
"Error type: %s. Error code: %s", adErrorType.toString(), errorCode));
}
}
}