forked from bidmachine/BidMachine-Android-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestsJavaActivity.java
More file actions
463 lines (382 loc) · 17.9 KB
/
Copy pathRequestsJavaActivity.java
File metadata and controls
463 lines (382 loc) · 17.9 KB
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
package io.bidmachine.examples;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.Arrays;
import java.util.List;
import io.bidmachine.AdPlacementConfig;
import io.bidmachine.BidMachine;
import io.bidmachine.BannerAdSize;
import io.bidmachine.banner.BannerRequest;
import io.bidmachine.banner.BannerView;
import io.bidmachine.banner.SimpleBannerListener;
import io.bidmachine.examples.base.BaseJavaExampleActivity;
import io.bidmachine.examples.base.Status;
import io.bidmachine.examples.databinding.ActivityRequestsBinding;
import io.bidmachine.interstitial.InterstitialAd;
import io.bidmachine.interstitial.InterstitialRequest;
import io.bidmachine.interstitial.SimpleInterstitialListener;
import io.bidmachine.models.AuctionResult;
import io.bidmachine.MediaAssetType;
import io.bidmachine.nativead.NativeAd;
import io.bidmachine.nativead.NativeRequest;
import io.bidmachine.nativead.SimpleNativeListener;
import io.bidmachine.nativead.view.NativeAdContentLayout;
import io.bidmachine.rewarded.RewardedAd;
import io.bidmachine.rewarded.RewardedRequest;
import io.bidmachine.rewarded.SimpleRewardedListener;
import io.bidmachine.utils.BMError;
public class RequestsJavaActivity extends BaseJavaExampleActivity<ActivityRequestsBinding> {
@Nullable
private BannerView bannerView;
@Nullable
private BannerRequest bannerRequest;
@Nullable
private InterstitialAd interstitialAd;
@Nullable
private InterstitialRequest interstitialRequest;
@Nullable
private RewardedAd rewardedAd;
@Nullable
private RewardedRequest rewardedRequest;
@Nullable
private NativeAd nativeAd;
@Nullable
private NativeRequest nativeRequest;
@NonNull
@Override
protected ActivityRequestsBinding inflate(@NonNull LayoutInflater inflater) {
return ActivityRequestsBinding.inflate(inflater);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialise SDK
BidMachine.initialize(this, SOURCE_ID);
// Enable logs
BidMachine.setLoggingEnabled(true);
// Set listener to perform Banner Ads request
binding.bRequestBanner.setOnClickListener(v -> requestBanner());
// Set listener to perform Banner Ads show
binding.bShowRequestedBanner.setOnClickListener(v -> showRequestedBanner());
// Set listener to perform Interstitial Ads request
binding.bRequestInterstitial.setOnClickListener(v -> requestInterstitial());
// Set listener to perform Interstitial Ads show
binding.bShowRequestedInterstitial.setOnClickListener(v -> showRequestedInterstitial());
// Set listener to perform Rewarded Ads request
binding.bRequestRewarded.setOnClickListener(v -> requestRewarded());
// Set listener to perform Rewarded Ads show
binding.bShowRequestedRewarded.setOnClickListener(v -> showRequestedRewarded());
// Set listener to perform Native Ads request
binding.bRequestNative.setOnClickListener(v -> requestNative());
// Set listener to perform Native Ads show
binding.bShowRequestedNative.setOnClickListener(v -> showRequestedNative());
}
@Override
protected void onDestroy() {
super.onDestroy();
// Destroy Ads when you finish with it
destroyCurrentBannerView();
destroyCurrentInterstitialAd();
destroyCurrentRewardedAd();
destroyCurrentNativeAd();
}
private void showAdView(View view) {
binding.adContainer.removeAllViews();
binding.adContainer.addView(view);
}
/**
* Make note, that AdRequest listeners will be notified in background thread
*/
private void requestBanner() {
setDebugState(Status.Requesting);
// Create placement configuration
AdPlacementConfig config = AdPlacementConfig.bannerBuilder(BannerAdSize.Banner).build();
// Create new Banner Ads request
bannerRequest = new BannerRequest.Builder(config)
// Set Banner Ads request listener
.setListener(new BannerRequest.AdRequestListener() {
@Override
public void onRequestSuccess(@NonNull BannerRequest bannerRequest,
@NonNull AuctionResult auctionResult) {
runOnUiThread(() -> setDebugState(Status.Requested, "Banner Ad Requested"));
}
@Override
public void onRequestFailed(@NonNull BannerRequest bannerRequest,
@NonNull final BMError bmError) {
runOnUiThread(() -> setDebugState(Status.RequestFail,
String.format("Banner Request Failed: %s",
bmError.getMessage())));
}
@Override
public void onRequestExpired(@NonNull BannerRequest bannerRequest) {
runOnUiThread(() -> setDebugState(Status.Expired, "Banner Request Expired"));
}
})
.build();
// Perform Banner Ads request
bannerRequest.request(this);
}
private void showRequestedBanner() {
if (bannerRequest == null) {
toast("Please request banner first");
} else if (bannerRequest.isExpired()) {
toast("BannerRequest expired, request new one please");
} else if (bannerRequest.getAuctionResult() == null) {
toast("BannerRequest not requested or requested unsuccessfully");
} else {
// Destroy previous BannerView object
destroyCurrentBannerView();
bannerView = new BannerView(this);
bannerView.setListener(new SimpleBannerListener() {
@Override
public void onAdLoaded(@NonNull BannerView ad) {
setDebugState(Status.Loaded, "Banner Ads loaded");
// Show Banner Ad
showAdView(ad);
}
@Override
public void onAdLoadFailed(@NonNull BannerView ad, @NonNull BMError error) {
setDebugState(Status.LoadFail, String.format("Banner Ads load failed: %s", error.getMessage()));
// Destroy loaded ad since it not required any more
destroyCurrentBannerView();
}
});
// Perform BannerAd load
bannerView.load(bannerRequest);
}
}
private void destroyCurrentBannerView() {
if (bannerView != null) {
bannerView.destroy();
bannerView = null;
}
}
private void requestInterstitial() {
setDebugState(Status.Requesting);
// Create placement configuration
AdPlacementConfig config = AdPlacementConfig.interstitialBuilder().build();
// Create new Interstitial Ads request
interstitialRequest = new InterstitialRequest.Builder(config)
// Set Interstitial Ads request listener
.setListener(new InterstitialRequest.AdRequestListener() {
@Override
public void onRequestSuccess(@NonNull InterstitialRequest interstitialRequest,
@NonNull AuctionResult auctionResult) {
runOnUiThread(() -> setDebugState(Status.Requested, "Interstitial Ad Requested"));
}
@Override
public void onRequestFailed(@NonNull InterstitialRequest interstitialRequest,
@NonNull final BMError bmError) {
runOnUiThread(() -> setDebugState(Status.RequestFail,
String.format("Interstitial Request Failed: %s",
bmError.getMessage())));
}
@Override
public void onRequestExpired(@NonNull InterstitialRequest interstitialRequest) {
runOnUiThread(() -> setDebugState(Status.Expired, "Interstitial Request Expired"));
}
})
.build();
// Perform Interstitial Ads request
interstitialRequest.request(this);
}
private void showRequestedInterstitial() {
if (interstitialRequest == null) {
toast("Please request Interstitial first");
} else if (interstitialRequest.isExpired()) {
toast("InterstitialRequest expired, request new one please");
} else if (interstitialRequest.getAuctionResult() == null) {
toast("InterstitialRequest not requested or requested unsuccessfully");
} else {
// Destroy previous InterstitialAd object
destroyCurrentInterstitialAd();
// Create new InterstitialAd
interstitialAd = new InterstitialAd(this);
interstitialAd.setListener(new SimpleInterstitialListener() {
@Override
public void onAdLoaded(@NonNull InterstitialAd ad) {
setDebugState(Status.Loaded, "Interstitial Ads Loaded");
// Show Interstitial Ads
ad.show();
}
@Override
public void onAdLoadFailed(@NonNull InterstitialAd ad, @NonNull BMError error) {
setDebugState(Status.LoadFail,
String.format("Interstitial Ads load failed: %s", error.getMessage()));
// Destroy current Interstitial ad since we don't need it anymore
destroyCurrentInterstitialAd();
}
@Override
public void onAdClosed(@NonNull InterstitialAd ad, boolean finished) {
setDebugState(Status.Closed);
// Destroy current Interstitial ad since we don't need it anymore
destroyCurrentInterstitialAd();
}
});
// Perform InterstitialAd load
interstitialAd.load(interstitialRequest);
}
}
private void destroyCurrentInterstitialAd() {
if (interstitialAd != null) {
interstitialAd.destroy();
interstitialAd = null;
}
}
private void requestRewarded() {
setDebugState(Status.Requesting);
// Create placement configuration
AdPlacementConfig config = AdPlacementConfig.rewardedBuilder().build();
// Create new Rewarded Ads request
rewardedRequest = new RewardedRequest.Builder(config)
// Set Rewarded Ads request listener
.setListener(new RewardedRequest.AdRequestListener() {
@Override
public void onRequestSuccess(@NonNull RewardedRequest rewardedRequest,
@NonNull AuctionResult auctionResult) {
runOnUiThread(() -> setDebugState(Status.Requested, "Rewarded Ad Requested"));
}
@Override
public void onRequestFailed(@NonNull RewardedRequest rewardedRequest,
@NonNull final BMError bmError) {
runOnUiThread(() -> setDebugState(Status.RequestFail,
String.format("Rewarded Request Failed: %s",
bmError.getMessage())));
}
@Override
public void onRequestExpired(@NonNull RewardedRequest rewardedRequest) {
runOnUiThread(() -> setDebugState(Status.Expired, "Rewarded Request Expired"));
}
})
.build();
// Perform Rewarded Ads request
rewardedRequest.request(this);
}
private void showRequestedRewarded() {
if (rewardedRequest == null) {
toast("Please request Rewarded first");
} else if (rewardedRequest.isExpired()) {
toast("RewardedRequest expired, request new one please");
} else if (rewardedRequest.getAuctionResult() == null) {
toast("RewardedRequest not requested or requested unsuccessfully");
} else {
// Destroy previous RewardedAd object
destroyCurrentRewardedAd();
// Create new RewardedAd
rewardedAd = new RewardedAd(this);
rewardedAd.setListener(new SimpleRewardedListener() {
@Override
public void onAdLoaded(@NonNull RewardedAd ad) {
setDebugState(Status.Loaded, "Rewarded Ads Loaded");
// Show Rewarded Ads
ad.show();
}
@Override
public void onAdLoadFailed(@NonNull RewardedAd ad, @NonNull BMError error) {
setDebugState(Status.LoadFail, String.format("Rewarded Ads load failed: %s", error.getMessage()));
// Destroy current Rewarded ad since we don't need it anymore
destroyCurrentRewardedAd();
}
@Override
public void onAdClosed(@NonNull RewardedAd ad, boolean finished) {
setDebugState(Status.Closed, "Rewarded Ads Closed");
// Destroy current Rewarded ad since we don't need it anymore
destroyCurrentRewardedAd();
}
@Override
public void onAdRewarded(@NonNull RewardedAd ad) {
setDebugState(Status.Rewarded, "Rewarded Ads Rewarded");
// Here you can start you reward process
}
});
// Perform RewardedAd load
rewardedAd.load(rewardedRequest);
}
}
private void destroyCurrentRewardedAd() {
if (rewardedAd != null) {
rewardedAd.destroy();
rewardedAd = null;
}
}
private void requestNative() {
setDebugState(Status.Requesting);
// Create media types list
List<MediaAssetType> mediaTypes = Arrays.asList(
MediaAssetType.Icon,
MediaAssetType.Image
);
// Create placement configuration
AdPlacementConfig config = AdPlacementConfig.nativeBuilder(mediaTypes).build();
// Create new Native Ads request
nativeRequest = new NativeRequest.Builder(config)
// Set Native Ads request listener
.setListener(new NativeRequest.AdRequestListener() {
@Override
public void onRequestSuccess(@NonNull NativeRequest nativeRequest,
@NonNull AuctionResult auctionResult) {
runOnUiThread(() -> setDebugState(Status.Requested, "Native Ad Requested"));
}
@Override
public void onRequestFailed(@NonNull NativeRequest nativeRequest,
@NonNull final BMError bmError) {
runOnUiThread(() -> setDebugState(Status.RequestFail,
String.format("Native Request Failed: %s",
bmError.getMessage())));
}
@Override
public void onRequestExpired(@NonNull NativeRequest nativeRequest) {
runOnUiThread(() -> setDebugState(Status.Expired, "Native Request Expired"));
}
})
.build();
// Perform Native Ads request
nativeRequest.request(this);
}
private void showRequestedNative() {
if (nativeRequest == null) {
toast("Please request Native first");
} else if (nativeRequest.isExpired()) {
toast("NativeRequest expired, request new one please");
} else if (nativeRequest.getAuctionResult() == null) {
toast("NativeRequest not requested or requested unsuccessfully");
} else {
// Destroy previous NativeAd object
destroyCurrentNativeAd();
// Create new NativeAd
nativeAd = new NativeAd(this);
nativeAd.setListener(new SimpleNativeListener() {
@Override
public void onAdLoaded(@NonNull NativeAd ad) {
setDebugState(Status.Loaded, "Native Ads Loaded");
// Show native Ads
View nativeView = createNativeAdView(ad);
showAdView(nativeView);
}
@Override
public void onAdLoadFailed(@NonNull NativeAd ad, @NonNull BMError error) {
setDebugState(Status.LoadFail, String.format("Native Ads load failed: %s", error.getMessage()));
}
});
// Perform NativeAd load
nativeAd.load(nativeRequest);
}
}
private View createNativeAdView(NativeAd nativeAd) {
NativeAdContentLayout nativeView = (NativeAdContentLayout) LayoutInflater.from(this)
.inflate(R.layout.native_ad, binding.adContainer, false);
nativeView.bind(nativeAd);
nativeView.registerViewForInteraction(nativeAd);
nativeView.setVisibility(View.VISIBLE);
return nativeView;
}
private void destroyCurrentNativeAd() {
if (nativeAd != null) {
nativeAd.destroy();
nativeAd = null;
}
}
}