/
NendMediationAdapter.java
439 lines (384 loc) · 13.9 KB
/
NendMediationAdapter.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
package com.google.ads.mediation.nend;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.mediation.Adapter;
import com.google.android.gms.ads.mediation.InitializationCompleteCallback;
import com.google.android.gms.ads.mediation.MediationAdLoadCallback;
import com.google.android.gms.ads.mediation.MediationConfiguration;
import com.google.android.gms.ads.mediation.MediationNativeAdapter;
import com.google.android.gms.ads.mediation.MediationNativeListener;
import com.google.android.gms.ads.mediation.MediationRewardedAd;
import com.google.android.gms.ads.mediation.MediationRewardedAdCallback;
import com.google.android.gms.ads.mediation.MediationRewardedAdConfiguration;
import com.google.android.gms.ads.mediation.NativeMediationAdRequest;
import com.google.android.gms.ads.mediation.VersionInfo;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;
import net.nend.android.NendAdInterstitial.NendAdInterstitialShowResult;
import net.nend.android.NendAdInterstitial.NendAdInterstitialStatusCode;
import net.nend.android.NendAdRewardItem;
import net.nend.android.NendAdRewardedActionListener;
import net.nend.android.NendAdRewardedVideo;
import net.nend.android.NendAdVideo;
import net.nend.android.NendAdVideoPlayingState;
import net.nend.android.NendAdVideoPlayingStateListener;
import net.nend.android.NendAdVideoType;
import net.nend.android.NendAdView.NendError;
/**
* The {@link NendMediationAdapter} to load and show Nend rewarded video ads.
*/
public class NendMediationAdapter extends Adapter
implements MediationNativeAdapter, MediationRewardedAd, NendAdRewardedActionListener {
static final String TAG = NendMediationAdapter.class.getSimpleName();
// region Error codes
// Nend adapter error domain.
public static final String ERROR_DOMAIN = "com.google.ads.mediation.nend";
// Nend SDK error domain.
public static final String NEND_SDK_ERROR_DOMAIN = "net.nend.android";
/**
* Nend adapter errors.
*/
@Retention(RetentionPolicy.SOURCE)
@IntDef(value = {
ERROR_REQUIRES_ACTIVITY_CONTEXT,
ERROR_INVALID_SERVER_PARAMETERS,
ERROR_AD_NOT_READY,
ERROR_AD_FAILED_TO_PLAY,
ERROR_BANNER_SIZE_MISMATCH,
ERROR_NULL_CONTEXT
})
public @interface AdapterError {
}
/**
* Nend requires an activity context to load and show ads.
*/
public static final int ERROR_REQUIRES_ACTIVITY_CONTEXT = 101;
/**
* Missing or invalid server parameters (e.g. nend Spot ID is null).
*/
public static final int ERROR_INVALID_SERVER_PARAMETERS = 102;
/**
* Ad is not yet ready to be shown.
*/
public static final int ERROR_AD_NOT_READY = 103;
/**
* Nend failed to play the ad.
*/
public static final int ERROR_AD_FAILED_TO_PLAY = 104;
/**
* The requested ad size does not match a nend supported banner size.
*/
public static final int ERROR_BANNER_SIZE_MISMATCH = 105;
/**
* The {@link Context} object reference is {@code null} and/or was recently released from memory.
*/
public static final int ERROR_NULL_CONTEXT = 106;
/**
* Convert nend's {@link NendAdInterstitialStatusCode} to a mediation specific error code.
*
* @param statusCode nend's interstitial status code.
* @return the mediation specific error code.
*/
public static int getMediationErrorCode(@NonNull NendAdInterstitialStatusCode statusCode) {
switch (statusCode) {
case SUCCESS:
return 200;
case INVALID_RESPONSE_TYPE:
return 201;
case FAILED_AD_REQUEST:
return 202;
case FAILED_AD_DOWNLOAD:
return 203;
}
// Error '299' to indicate that the error is new and has not been supported by the adapter yet.
return 299;
}
/**
* Convert nend's {@link NendAdInterstitialShowResult} to a mediation specific error code.
*
* @param statusCode nend's show result.
* @return the mediation specific error code.
*/
public static int getMediationErrorCode(@NonNull NendAdInterstitialShowResult statusCode) {
switch (statusCode) {
case AD_SHOW_SUCCESS:
return 300;
case AD_LOAD_INCOMPLETE:
return 301;
case AD_REQUEST_INCOMPLETE:
return 302;
case AD_DOWNLOAD_INCOMPLETE:
return 303;
case AD_FREQUENCY_NOT_REACHABLE:
return 304;
case AD_SHOW_ALREADY:
return 305;
}
// Error '399' to indicate that the error is new and has not been supported by the adapter yet.
return 399;
}
/**
* Convert nend's {@link NendError} to a mediation specific error code.
*
* @param nendError nend's error object.
* @return the mediation specific error code.
*/
public static int getMediationErrorCode(@NonNull NendError nendError) {
switch (nendError) {
case AD_SIZE_TOO_LARGE:
return 400;
case INVALID_RESPONSE_TYPE:
return 401;
case FAILED_AD_REQUEST:
return 402;
case FAILED_AD_DOWNLOAD:
return 403;
case AD_SIZE_DIFFERENCES:
return 404;
case UNSUPPORTED_DEVICE:
return 405;
}
// Error '499' to indicate that the error is new and has not been supported by the adapter yet.
return 499;
}
// endregion
static final String KEY_USER_ID = "key_user_id";
static final String KEY_API_KEY = "apiKey";
static final String KEY_SPOT_ID = "spotId";
static final String MEDIATION_NAME_ADMOB = "AdMob";
private NendAdRewardedVideo mRewardedVideo;
private MediationAdLoadCallback<MediationRewardedAd, MediationRewardedAdCallback> mAdLoadCallback;
private MediationRewardedAdCallback mRewardedAdCallback;
public enum FormatType {
TYPE_VIDEO,
TYPE_NORMAL
}
private NendNativeAdForwarder nativeAdForwarder;
/**
* {@link Adapter} implementation
*/
@Override
@NonNull
public VersionInfo getVersionInfo() {
String versionString = BuildConfig.ADAPTER_VERSION;
String[] splits = versionString.split("\\.");
if (splits.length >= 4) {
int major = Integer.parseInt(splits[0]);
int minor = Integer.parseInt(splits[1]);
int micro = Integer.parseInt(splits[2]) * 100 + Integer.parseInt(splits[3]);
return new VersionInfo(major, minor, micro);
}
String logMessage =
String.format(
"Unexpected adapter version format: %s. Returning 0.0.0 for adapter version.",
versionString);
Log.w(TAG, logMessage);
return new VersionInfo(0, 0, 0);
}
@Override
@NonNull
public VersionInfo getSDKVersionInfo() {
String versionString = net.nend.android.BuildConfig.NEND_SDK_VERSION;
String[] splits = versionString.split("\\.");
if (splits.length >= 3) {
int major = Integer.parseInt(splits[0]);
int minor = Integer.parseInt(splits[1]);
int micro = Integer.parseInt(splits[2]);
return new VersionInfo(major, minor, micro);
}
String logMessage =
String.format(
"Unexpected SDK version format: %s. Returning 0.0.0 for SDK version.", versionString);
Log.w(TAG, logMessage);
return new VersionInfo(0, 0, 0);
}
@Override
public void initialize(@NonNull Context context,
@NonNull InitializationCompleteCallback initializationCompleteCallback,
@NonNull List<MediationConfiguration> mediationConfigurations) {
// Nend SDK does not have any API for initialization.
initializationCompleteCallback.onInitializationSucceeded();
}
@Override
public void loadRewardedAd(
@NonNull MediationRewardedAdConfiguration mediationRewardedAdConfiguration,
@NonNull MediationAdLoadCallback<MediationRewardedAd, MediationRewardedAdCallback>
mediationAdLoadCallback) {
Context context = mediationRewardedAdConfiguration.getContext();
if (!(context instanceof Activity)) {
AdError error = new AdError(ERROR_REQUIRES_ACTIVITY_CONTEXT,
"Nend requires an Activity context to load an ad.", ERROR_DOMAIN);
Log.w(TAG, error.getMessage());
mediationAdLoadCallback.onFailure(error);
return;
}
Bundle serverParameters = mediationRewardedAdConfiguration.getServerParameters();
Bundle networkExtras = mediationRewardedAdConfiguration.getMediationExtras();
String apiKey = serverParameters.getString(KEY_API_KEY);
if (TextUtils.isEmpty(apiKey)) {
AdError error = new AdError(ERROR_INVALID_SERVER_PARAMETERS, "Missing or invalid API key.",
ERROR_DOMAIN);
Log.w(TAG, error.getMessage());
mediationAdLoadCallback.onFailure(error);
return;
}
int spotID = Integer.parseInt(serverParameters.getString(KEY_SPOT_ID, "0"));
if (spotID <= 0) {
AdError error = new AdError(ERROR_INVALID_SERVER_PARAMETERS, "Missing or invalid spot ID.",
ERROR_DOMAIN);
Log.w(TAG, error.getMessage());
mediationAdLoadCallback.onFailure(error);
return;
}
mAdLoadCallback = mediationAdLoadCallback;
mRewardedVideo = new NendAdRewardedVideo(context, spotID, apiKey);
mRewardedVideo.setActionListener(NendMediationAdapter.this);
mRewardedVideo.setMediationName(MEDIATION_NAME_ADMOB);
mRewardedVideo.setUserId(networkExtras.getString(KEY_USER_ID, ""));
mRewardedVideo.loadAd();
}
@Override
public void showAd(@NonNull Context context) {
if (!mRewardedVideo.isLoaded()) {
AdError error = new AdError(ERROR_AD_NOT_READY, "nend rewarded ad not ready yet.",
ERROR_DOMAIN);
Log.w(TAG, error.getMessage());
if (mRewardedAdCallback != null) {
mRewardedAdCallback.onAdFailedToShow(error);
}
return;
}
if (!(context instanceof Activity)) {
AdError error = new AdError(ERROR_REQUIRES_ACTIVITY_CONTEXT,
"nend requires an Activity context to show ads.", ERROR_DOMAIN);
Log.e(TAG, error.getMessage());
if (mRewardedAdCallback != null) {
mRewardedAdCallback.onAdFailedToShow(error);
}
return;
}
mRewardedVideo.showAd((Activity) context);
}
/**
* {@link MediationNativeAdapter} implementation
*/
@Override
public void onResume() {
if (nativeAdForwarder != null) {
nativeAdForwarder.onResume();
}
}
@Override
public void onPause() {
if (nativeAdForwarder != null) {
nativeAdForwarder.onPause();
}
}
@Override
public void onDestroy() {
if (nativeAdForwarder != null) {
nativeAdForwarder.onDestroy();
nativeAdForwarder = null;
}
}
@Override
public void requestNativeAd(@NonNull Context context,
@NonNull MediationNativeListener mediationNativeListener,
@NonNull Bundle serverParameters, @NonNull NativeMediationAdRequest nativeMediationAdRequest,
@Nullable Bundle mediationExtras) {
nativeAdForwarder = new NendNativeAdForwarder(NendMediationAdapter.this);
nativeAdForwarder.requestNativeAd(context, mediationNativeListener, serverParameters,
nativeMediationAdRequest, mediationExtras);
}
/**
* {@link NendAdRewardedActionListener} implementation
*/
@Override
public void onLoaded(@NonNull NendAdVideo nendAdVideo) {
if (mRewardedVideo.getType() == NendAdVideoType.NORMAL) {
NendAdVideoPlayingState state = mRewardedVideo.playingState();
if (state != null) {
state.setPlayingStateListener(new NendAdVideoPlayingStateListener() {
@Override
public void onStarted(@NonNull NendAdVideo nendAdVideo) {
if (mRewardedAdCallback != null) {
mRewardedAdCallback.onVideoStart();
}
}
@Override
public void onStopped(@NonNull NendAdVideo nendAdVideo) {
// No relevant event to forward to the Google Mobile Ads SDK.
}
@Override
public void onCompleted(@NonNull NendAdVideo nendAdVideo) {
if (mRewardedAdCallback != null) {
mRewardedAdCallback.onVideoComplete();
}
}
});
}
}
if (mAdLoadCallback != null) {
mRewardedAdCallback = mAdLoadCallback.onSuccess(NendMediationAdapter.this);
}
}
@Override
public void onFailedToLoad(@NonNull NendAdVideo nendAdVideo, int errorCode) {
String errorMessage = String
.format("Nend SDK returned an ad load failure callback with code: %d", errorCode);
AdError error = new AdError(errorCode, errorMessage, NEND_SDK_ERROR_DOMAIN);
Log.e(TAG, error.getMessage());
if (mAdLoadCallback != null) {
mAdLoadCallback.onFailure(error);
}
mRewardedVideo.releaseAd();
}
@Override
public void onRewarded(@NonNull NendAdVideo nendAdVideo,
@NonNull NendAdRewardItem nendAdRewardItem) {
if (mRewardedAdCallback != null) {
mRewardedAdCallback.onUserEarnedReward(new NendMediationRewardItem(nendAdRewardItem));
}
}
@Override
public void onFailedToPlay(@NonNull NendAdVideo nendAdVideo) {
AdError error = new AdError(ERROR_AD_FAILED_TO_PLAY,
"Nend SDK returned the onFailedToPlay() error callback.", ERROR_DOMAIN);
Log.e(TAG, error.getMessage());
if (mRewardedAdCallback != null) {
mRewardedAdCallback.onAdFailedToShow(error);
}
}
@Override
public void onShown(@NonNull NendAdVideo nendAdVideo) {
if (mRewardedAdCallback != null) {
mRewardedAdCallback.onAdOpened();
mRewardedAdCallback.reportAdImpression();
}
}
@Override
public void onClosed(@NonNull NendAdVideo nendAdVideo) {
if (mRewardedAdCallback != null) {
mRewardedAdCallback.onAdClosed();
}
mRewardedVideo.releaseAd();
}
@Override
public void onAdClicked(@NonNull NendAdVideo nendAdVideo) {
if (mRewardedAdCallback != null) {
mRewardedAdCallback.reportAdClicked();
}
}
@Override
public void onInformationClicked(@NonNull NendAdVideo nendAdVideo) {
// No relevant event to forward to the Google Mobile Ads SDK.
}
}