-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBidMachineAdMobActivity.java
510 lines (442 loc) · 17.1 KB
/
BidMachineAdMobActivity.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
package io.bidmachine.examples;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Toast;
import com.google.ads.mediation.bidmachine.BidMachineBundleBuilder;
import com.google.ads.mediation.bidmachine.BidMachineCustomEventBanner;
import com.google.ads.mediation.bidmachine.BidMachineCustomEventInterstitial;
import com.google.ads.mediation.bidmachine.BidMachineMediationRewardedAdAdapter;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.reward.RewardItem;
import com.google.android.gms.ads.reward.RewardedVideoAd;
import com.google.android.gms.ads.reward.RewardedVideoAdListener;
import org.json.JSONArray;
import org.json.JSONObject;
import io.bidmachine.AdContentType;
public class BidMachineAdMobActivity extends Activity {
private static final String TAG = "MainActivity";
private static final String BANNER_ID = "ca-app-pub-1405929557079197/8614249475";
private static final String INTERSTITIAL_ID = "ca-app-pub-1405929557079197/1600418448";
private static final String REWARDED_ID = "ca-app-pub-1405929557079197/6263418305";
private FrameLayout bannerContainer;
private AdView adView;
private InterstitialAd interstitialAd;
private RewardedVideoAd rewardedVideoAd;
private JSONArray mediationConfig;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bannerContainer = findViewById(R.id.banner_container);
findViewById(R.id.load_banner).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadBanner();
}
});
findViewById(R.id.show_banner).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showBanner();
}
});
findViewById(R.id.load_interstitial).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadInterstitial();
}
});
findViewById(R.id.show_interstitial).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showInterstitial();
}
});
findViewById(R.id.load_rvideo).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadRewardedVideo();
}
});
findViewById(R.id.show_rvideo).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showRewardedVideo();
}
});
try {
JSONObject myTargetJSON = new JSONObject("" +
"{" +
" \"network\": \"my_target\"," +
" \"ad_units\": [{" +
" \"format\": \"banner\"," +
" \"slot_id\": \"437933\"" +
" }, {" +
" \"format\": \"banner_320x50\"," +
" \"slot_id\": \"437933\"" +
" }, {" +
" \"format\": \"banner_300x250\"," +
" \"slot_id\": \"64526\"" +
" }, {" +
" \"format\": \"banner_728x90\"," +
" \"slot_id\": \"81620\"" +
" }, {" +
" \"format\": \"interstitial_static\"," +
" \"slot_id\": \"365991\"" +
" }, {" +
" \"format\": \"rewarded_video\"," +
" \"slot_id\": \"482205\"" +
" }]" +
"}");
JSONObject adColonyJSON = new JSONObject("" +
"{" +
" \"network\": \"adcolony\"," +
" \"network_config\": {" +
" \"app_id\": \"app185a7e71e1714831a49ec7\"" +
" }," +
" \"ad_units\": [{" +
" \"format\": \"interstitial_video\"," +
" \"app_id\": \"app185a7e71e1714831a49ec7\"," +
" \"zone_id\": \"vz06e8c32a037749699e7050\"," +
" \"store_id\": \"google\"" +
" }, {" +
" \"format\": \"rewarded_video\"," +
" \"app_id\": \"app185a7e71e1714831a49ec7\"," +
" \"zone_id\": \"vz1fd5a8b2bf6841a0a4b826\"," +
" \"store_id\": \"google\"" +
" }]" +
"}");
JSONObject facebookJSON = new JSONObject("" +
"{" +
" \"network\": \"facebook\"," +
" \"app_id\": \"1525692904128549\"," +
" \"ad_units\": [{" +
" \"format\": \"banner\"," +
" \"facebook_key\": \"1525692904128549_2386746951356469\"" +
" }, {" +
" \"format\": \"banner_320x50\"," +
" \"facebook_key\": \"1525692904128549_2386746951356469\"" +
" }, {" +
" \"format\": \"banner_300x250\"," +
" \"facebook_key\": \"1525692904128549_2386746951356469\"" +
" }, {" +
" \"format\": \"interstitial_static\"," +
" \"facebook_key\": \"1525692904128549_2386743441356820\"" +
" }, {" +
" \"format\": \"rewarded_video\"," +
" \"facebook_key\": \"1525692904128549_2386753464689151\"" +
" }]" +
"}");
JSONObject tapjoyJSON = new JSONObject("" +
"{" +
" \"network\": \"tapjoy\"," +
" \"sdk_key\": \"tmyN5ZcXTMyjeJNJmUD5ggECAbnEGtJREmLDd0fvqKBXcIr7e1dvboNKZI4y\"," +
" \"ad_units\": [{" +
" \"format\": \"interstitial_video\"," +
" \"sdk_key\": \"tmyN5ZcXTMyjeJNJmUD5ggECAbnEGtJREmLDd0fvqKBXcIr7e1dvboNKZI4y\"," +
" \"placement_name\": \"video_without_cap_pb\"" +
" }, {" +
" \"format\": \"rewarded_video\"," +
" \"sdk_key\": \"tmyN5ZcXTMyjeJNJmUD5ggECAbnEGtJREmLDd0fvqKBXcIr7e1dvboNKZI4y\"," +
" \"placement_name\": \"rewarded_video_without_cap_pb\"" +
" }]" +
"}");
mediationConfig = new JSONArray();
mediationConfig.put(myTargetJSON);
mediationConfig.put(adColonyJSON);
mediationConfig.put(facebookJSON);
mediationConfig.put(tapjoyJSON);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
destroyBanner();
destroyInterstitial();
}
/**
* Method for load banner from AdMob
*/
private void loadBanner() {
//Destroy previous AdView
destroyBanner();
Log.d(TAG, "AdMob loadBanner");
//Prepare bundle for set to AdRequest
Bundle bundle = new BidMachineBundleBuilder()
.setSellerId("1")
.setCoppa(true)
.setLoggingEnabled(true)
.setTestMode(true)
.setMediationConfig(mediationConfig)
.build();
//Set bundle to custom event banner
AdRequest adRequest = new AdRequest.Builder()
.addCustomEventExtrasBundle(BidMachineCustomEventBanner.class, bundle)
.build();
//Create new AdView instance and load
adView = new AdView(this);
adView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
adView.setAdUnitId(BANNER_ID);
adView.setAdSize(AdSize.BANNER);
adView.setAdListener(new BannerViewListener());
adView.loadAd(adRequest);
}
/**
* Method for show banner from AdMob
*/
private void showBanner() {
if (adView != null && adView.getParent() == null) {
Log.d(TAG, "AdView showBanner");
//Add AdView for show
bannerContainer.addView(adView);
} else {
Log.d(TAG, "AdView null, load banner first");
}
}
/**
* Method for destroy AdView
*/
private void destroyBanner() {
if (adView != null) {
Log.d(TAG, "AdView destroyBanner");
bannerContainer.removeAllViews();
adView.setAdListener(null);
adView.destroy();
adView = null;
}
}
/**
* Method for load interstitial from AdMob
*/
private void loadInterstitial() {
//Destroy previous InterstitialAd
destroyInterstitial();
Log.d(TAG, "InterstitialAd loadInterstitial");
//Prepare bundle for set to AdRequest
Bundle bundle = new BidMachineBundleBuilder()
.setSellerId("1")
.setCoppa(true)
.setLoggingEnabled(true)
.setTestMode(true)
.setAdContentType(AdContentType.All)
.setMediationConfig(mediationConfig)
.build();
//Set bundle to custom event interstitial
AdRequest adRequest = new AdRequest.Builder()
.addCustomEventExtrasBundle(BidMachineCustomEventInterstitial.class, bundle)
.build();
//Create new InterstitialAd instance and load
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(INTERSTITIAL_ID);
interstitialAd.setAdListener(new InterstitialListener());
interstitialAd.loadAd(adRequest);
}
/**
* Method for show interstitial from AdMob
*/
private void showInterstitial() {
if (interstitialAd != null && interstitialAd.isLoaded()) {
Log.d(TAG, "InterstitialAd showInterstitial");
interstitialAd.show();
} else {
Log.d(TAG, "InterstitialAd null, load interstitial first");
}
}
/**
* Method for destroy InterstitialAd
*/
private void destroyInterstitial() {
if (interstitialAd != null) {
Log.d(TAG, "InterstitialAd destroyInterstitial");
interstitialAd.setAdListener(null);
interstitialAd = null;
}
}
/**
* Method for load rewarded video from AdMob
*/
private void loadRewardedVideo() {
//Destroy previous RewardedVideoAd
destroyRewardedVideo();
Log.d(TAG, "RewardedVideoAd loadRewardedVideo");
//Prepare bundle for set to AdRequest
Bundle bundle = new BidMachineBundleBuilder()
.setSellerId("1")
.setCoppa(false)
.setLoggingEnabled(true)
.setTestMode(true)
.setMediationConfig(mediationConfig)
.build();
//Set bundle to mediation rewarded video ad adapter
AdRequest adRequest = new AdRequest.Builder()
.addNetworkExtrasBundle(BidMachineMediationRewardedAdAdapter.class, bundle)
.build();
//Create new RewardedVideoAd instance and load
rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
rewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoListener());
rewardedVideoAd.loadAd(REWARDED_ID, adRequest);
}
/**
* Method for show rewarded video from AdMob
*/
private void showRewardedVideo() {
if (rewardedVideoAd != null && rewardedVideoAd.isLoaded()) {
Log.d(TAG, "RewardedVideoAd showRewardedVideo");
rewardedVideoAd.show();
} else {
Log.d(TAG, "RewardedVideo not loaded");
}
}
/**
* Method for destroy InterstitialAd
*/
private void destroyRewardedVideo() {
if (rewardedVideoAd != null) {
Log.d(TAG, "InterstitialAd destroyInterstitial");
rewardedVideoAd.setRewardedVideoAdListener(null);
rewardedVideoAd.destroy(this);
rewardedVideoAd = null;
}
}
/**
* Class for definition behavior AdView
*/
private class BannerViewListener extends AdListener {
@Override
public void onAdLoaded() {
Log.d(TAG, "AdView onBannerLoaded");
Toast.makeText(
BidMachineAdMobActivity.this,
"BannerLoaded",
Toast.LENGTH_SHORT).show();
}
@Override
public void onAdFailedToLoad(int i) {
Log.d(TAG, "AdView onBannerFailedToLoad with errorCode - " + i + ")");
Toast.makeText(
BidMachineAdMobActivity.this,
"BannerFailedToLoad",
Toast.LENGTH_SHORT).show();
}
@Override
public void onAdOpened() {
Log.d(TAG, "AdView onBannerOpened");
}
@Override
public void onAdImpression() {
Log.d(TAG, "AdView onBannerImpression");
}
@Override
public void onAdClicked() {
Log.d(TAG, "AdView onBannerClicked");
}
@Override
public void onAdClosed() {
Log.d(TAG, "AdView onBannerClosed");
}
@Override
public void onAdLeftApplication() {
Log.d(TAG, "AdView onBannerLeftApplication");
}
}
/**
* Class for definition behavior InterstitialAd
*/
private class InterstitialListener extends AdListener {
@Override
public void onAdLoaded() {
Log.d(TAG, "InterstitialAd onInterstitialLoaded");
Toast.makeText(
BidMachineAdMobActivity.this,
"InterstitialLoaded",
Toast.LENGTH_SHORT).show();
}
@Override
public void onAdFailedToLoad(int i) {
Log.d(TAG, "InterstitialAd onInterstitialFailedToLoad with errorCode - " + i + ")");
Toast.makeText(
BidMachineAdMobActivity.this,
"InterstitialFailedToLoad",
Toast.LENGTH_SHORT).show();
}
@Override
public void onAdOpened() {
Log.d(TAG, "InterstitialAd onInterstitialOpened");
}
@Override
public void onAdImpression() {
Log.d(TAG, "InterstitialAd onInterstitialImpression");
}
@Override
public void onAdClicked() {
Log.d(TAG, "InterstitialAd onInterstitialClicked");
}
@Override
public void onAdClosed() {
Log.d(TAG, "InterstitialAd onInterstitialClosed");
}
@Override
public void onAdLeftApplication() {
Log.d(TAG, "InterstitialAd onInterstitialLeftApplication");
}
}
/**
* Class for definition behavior RewardedVideoAd
*/
private class RewardedVideoListener implements RewardedVideoAdListener {
@Override
public void onRewardedVideoAdLoaded() {
Log.d(TAG, "RewardedVideoAd onRewardedVideoAdLoaded");
Toast.makeText(
BidMachineAdMobActivity.this,
"RewardedVideoAdLoaded",
Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdFailedToLoad(int i) {
Log.d(TAG, "RewardedVideoAd onRewardedVideoAdFailedToLoad with errorCode - " + i + ")");
Toast.makeText(
BidMachineAdMobActivity.this,
"RewardedVideoAdFailedToLoad",
Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdOpened() {
Log.d(TAG, "RewardedVideoAd onRewardedVideoAdOpened");
}
@Override
public void onRewardedVideoStarted() {
Log.d(TAG, "RewardedVideoAd onRewardedVideoStarted");
}
@Override
public void onRewardedVideoCompleted() {
Log.d(TAG, "RewardedVideoAd onRewardedVideoCompleted");
}
@Override
public void onRewardedVideoAdClosed() {
Log.d(TAG, "RewardedVideoAd onRewardedVideoAdClosed");
}
@Override
public void onRewarded(RewardItem rewardItem) {
Log.d(TAG, "RewardedVideoAd onRewarded");
}
@Override
public void onRewardedVideoAdLeftApplication() {
Log.d(TAG, "RewardedVideoAd onRewardedVideoAdLeftApplication");
}
}
}