-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBidMachineMoPubActivity.java
More file actions
387 lines (321 loc) · 13 KB
/
BidMachineMoPubActivity.java
File metadata and controls
387 lines (321 loc) · 13 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
package io.bidmachine.examples;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;
import com.mopub.common.MoPub;
import com.mopub.common.MoPubReward;
import com.mopub.common.SdkConfiguration;
import com.mopub.common.SdkInitializationListener;
import com.mopub.common.logging.MoPubLog;
import com.mopub.mobileads.BidMachineAdapterConfiguration;
import com.mopub.mobileads.MoPubErrorCode;
import com.mopub.mobileads.MoPubInterstitial;
import com.mopub.mobileads.MoPubRewardedVideoListener;
import com.mopub.mobileads.MoPubRewardedVideos;
import com.mopub.mobileads.MoPubView;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class BidMachineMoPubActivity extends Activity {
private static final String TAG = "MainActivity";
private static final String AD_UNIT_ID = "4068bca9a3a44977917d68338b75df64";
private static final String BANNER_KEY = "4068bca9a3a44977917d68338b75df64";
private static final String INTERSTITIAL_KEY = "6173ac5e48de4a8b9741571f93d9c04e";
private static final String REWARDED_KEY = "e746b899b7d54a5d980d627626422c25";
private Button btnLoadBanner;
private Button btnShowBanner;
private Button btnLoadInterstitial;
private Button btnShowInterstitial;
private Button btnLoadRewardedVideo;
private Button btnShowRewardedVideo;
private FrameLayout bannerContainer;
private MoPubView moPubView;
private MoPubInterstitial moPubInterstitial;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bannerContainer = findViewById(R.id.banner_container);
btnLoadBanner = findViewById(R.id.load_banner);
btnLoadBanner.setOnClickListener(v -> loadBanner());
btnShowBanner = findViewById(R.id.show_banner);
btnShowBanner.setOnClickListener(v -> showBanner());
btnLoadInterstitial = findViewById(R.id.load_interstitial);
btnLoadInterstitial.setOnClickListener(v -> loadInterstitial());
btnShowInterstitial = findViewById(R.id.show_interstitial);
btnShowInterstitial.setOnClickListener(v -> showInterstitial());
btnLoadRewardedVideo = findViewById(R.id.load_rvideo);
btnLoadRewardedVideo.setOnClickListener(v -> loadRewardedVideo());
btnShowRewardedVideo = findViewById(R.id.show_rvideo);
btnShowRewardedVideo.setOnClickListener(v -> showRewardedVideo());
initialize();
}
@Override
protected void onDestroy() {
super.onDestroy();
destroyBanner();
destroyInterstitial();
}
/**
* Initialize MoPub SDK with BidMachineAdapterConfiguration
*/
private void initialize() {
//Check initialized MoPub or not
if (!MoPub.isSdkInitialized()) {
Log.d(TAG, "MoPub initialize");
//Set DEBUG log from MoPub
MoPubLog.setLogLevel(MoPubLog.LogLevel.DEBUG);
//Prepare configuration map for BidMachineAdapterConfiguration
Map<String, String> configuration = new HashMap<>();
configuration.put("seller_id", "1");
configuration.put("coppa", "true");
configuration.put("logging_enabled", "true");
configuration.put("test_mode", "true");
//Prepare SdkConfiguration for initialize MoPub with BidMachineAdapterConfiguration
SdkConfiguration sdkConfiguration = new SdkConfiguration.Builder(AD_UNIT_ID)
.withAdditionalNetwork(BidMachineAdapterConfiguration.class.getName())
.withMediatedNetworkConfiguration(
BidMachineAdapterConfiguration.class.getName(),
configuration)
.build();
//Initialize MoPub SDK
MoPub.initializeSdk(this, sdkConfiguration, new InitializationListener());
} else {
enableButton();
}
}
/**
* Enable buttons for user interaction
*/
private void enableButton() {
btnLoadBanner.setEnabled(true);
btnShowBanner.setEnabled(true);
btnLoadInterstitial.setEnabled(true);
btnShowInterstitial.setEnabled(true);
btnLoadRewardedVideo.setEnabled(true);
btnShowRewardedVideo.setEnabled(true);
}
/**
* Method for load banner from MoPub
*/
private void loadBanner() {
//Destroy previous MoPubView
destroyBanner();
Log.d(TAG, "MoPubView loadBanner");
//Prepare localExtras for set to MoPubView
Map<String, Object> localExtras = new HashMap<>();
localExtras.put("banner_width", 320);
//Create new MoPubView instance and load
moPubView = new MoPubView(this);
moPubView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
moPubView.setLocalExtras(localExtras);
moPubView.setAutorefreshEnabled(false);
moPubView.setAdUnitId(BANNER_KEY);
moPubView.setBannerAdListener(new BannerViewListener());
moPubView.loadAd();
}
/**
* Method for show banner from MoPub
*/
private void showBanner() {
if (moPubView != null && moPubView.getParent() == null) {
Log.d(TAG, "MoPubView showBanner");
//Add MoPubView for show
bannerContainer.addView(moPubView);
} else {
Log.d(TAG, "MoPubView null, load banner first");
}
}
/**
* Method for destroy MoPubView
*/
private void destroyBanner() {
if (moPubView != null) {
Log.d(TAG, "MoPubView destroyBanner");
bannerContainer.removeAllViews();
moPubView.setBannerAdListener(null);
moPubView.destroy();
}
}
/**
* Method for load interstitial from MoPub
*/
private void loadInterstitial() {
//Destroy previous MoPubInterstitial
destroyInterstitial();
Log.d(TAG, "MoPubInterstitial loadInterstitial");
//Prepare localExtras for set to MoPubInterstitial
Map<String, Object> localExtras = new HashMap<>();
localExtras.put("ad_content_type", "All");
//Create new MoPubInterstitial instance and load
moPubInterstitial = new MoPubInterstitial(this, INTERSTITIAL_KEY);
moPubInterstitial.setLocalExtras(localExtras);
moPubInterstitial.setInterstitialAdListener(new InterstitialListener());
moPubInterstitial.load();
}
/**
* Method for show interstitial from MoPub
*/
private void showInterstitial() {
if (moPubInterstitial != null && moPubInterstitial.isReady()) {
Log.d(TAG, "MoPubInterstitial showInterstitial");
moPubInterstitial.show();
} else {
Log.d(TAG, "MoPubInterstitial null, load interstitial first");
}
}
/**
* Method for destroy MoPubInterstitial
*/
private void destroyInterstitial() {
if (moPubInterstitial != null) {
Log.d(TAG, "MoPubInterstitial destroyInterstitial");
moPubInterstitial.setInterstitialAdListener(null);
moPubInterstitial.destroy();
}
}
/**
* Method for load rewarded video from MoPub
*/
private void loadRewardedVideo() {
Log.d(TAG, "MoPubRewardedVideos loadRewardedVideo");
MoPubRewardedVideos.setRewardedVideoListener(new RewardedVideoListener());
MoPubRewardedVideos.loadRewardedVideo(REWARDED_KEY);
}
/**
* Method for show rewarded video from MoPub
*/
private void showRewardedVideo() {
if (MoPubRewardedVideos.hasRewardedVideo(REWARDED_KEY)) {
Log.d(TAG, "MoPubRewardedVideos showRewardedVideo");
MoPubRewardedVideos.showRewardedVideo(REWARDED_KEY);
} else {
Log.d(TAG, "RewardedVideo not loaded");
}
}
/**
* Class for definition behavior after initialize finished
*/
private class InitializationListener implements SdkInitializationListener {
@Override
public void onInitializationFinished() {
Log.d(TAG, "MoPub onInitializationFinished");
enableButton();
}
}
/**
* Class for definition behavior MoPubView
*/
private class BannerViewListener implements MoPubView.BannerAdListener {
@Override
public void onBannerLoaded(MoPubView banner) {
Log.d(TAG, "MoPubView onBannerLoaded");
Toast.makeText(
BidMachineMoPubActivity.this,
"BannerLoaded",
Toast.LENGTH_SHORT).show();
}
@Override
public void onBannerFailed(MoPubView banner, MoPubErrorCode errorCode) {
Log.d(TAG, "MoPubView onBannerFailed with errorCode - " + errorCode.getIntCode() + " (" + errorCode.toString() + ")");
Toast.makeText(
BidMachineMoPubActivity.this,
"BannerFailedToLoad",
Toast.LENGTH_SHORT).show();
}
@Override
public void onBannerClicked(MoPubView banner) {
Log.d(TAG, "MoPubView onBannerClicked");
}
@Override
public void onBannerExpanded(MoPubView banner) {
Log.d(TAG, "MoPubView onBannerExpanded");
}
@Override
public void onBannerCollapsed(MoPubView banner) {
Log.d(TAG, "MoPubView onBannerCollapsed");
}
}
/**
* Class for definition behavior MoPubInterstitial
*/
private class InterstitialListener implements MoPubInterstitial.InterstitialAdListener {
@Override
public void onInterstitialLoaded(MoPubInterstitial interstitial) {
Log.d(TAG, "MoPubInterstitial onInterstitialLoaded");
Toast.makeText(
BidMachineMoPubActivity.this,
"InterstitialLoaded",
Toast.LENGTH_SHORT).show();
}
@Override
public void onInterstitialFailed(MoPubInterstitial interstitial, MoPubErrorCode errorCode) {
Log.d(TAG, "MoPubInterstitial onInterstitialFailed with errorCode - " + errorCode.getIntCode() + " (" + errorCode.toString() + ")");
Toast.makeText(
BidMachineMoPubActivity.this,
"InterstitialFailedToLoad",
Toast.LENGTH_SHORT).show();
}
@Override
public void onInterstitialShown(MoPubInterstitial interstitial) {
Log.d(TAG, "MoPubInterstitial onInterstitialShown");
}
@Override
public void onInterstitialClicked(MoPubInterstitial interstitial) {
Log.d(TAG, "MoPubInterstitial onInterstitialClicked");
}
@Override
public void onInterstitialDismissed(MoPubInterstitial interstitial) {
Log.d(TAG, "MoPubInterstitial onInterstitialDismissed");
}
}
/**
* Class for definition behavior MoPubRewardedVideos
*/
private class RewardedVideoListener implements MoPubRewardedVideoListener {
@Override
public void onRewardedVideoLoadSuccess(@NonNull String adUnitId) {
Log.d(TAG, "MoPubRewardedVideos onRewardedVideoLoadSuccess");
Toast.makeText(
BidMachineMoPubActivity.this,
"RewardedVideoLoaded",
Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoLoadFailure(@NonNull String adUnitId, @NonNull MoPubErrorCode errorCode) {
Log.d(TAG, "MoPubRewardedVideos onRewardedVideoLoadFailure with errorCode - " + errorCode.getIntCode() + " (" + errorCode.toString() + ")");
Toast.makeText(
BidMachineMoPubActivity.this,
"RewardedVideoFailedToLoad",
Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoStarted(@NonNull String adUnitId) {
Log.d(TAG, "MoPubRewardedVideos onRewardedVideoStarted");
}
@Override
public void onRewardedVideoPlaybackError(@NonNull String adUnitId, @NonNull MoPubErrorCode errorCode) {
Log.d(TAG, "MoPubRewardedVideos onRewardedVideoPlaybackError with errorCode - " + errorCode.getIntCode() + " (" + errorCode.toString() + ")");
}
@Override
public void onRewardedVideoClicked(@NonNull String adUnitId) {
Log.d(TAG, "MoPubRewardedVideos onRewardedVideoClicked");
}
@Override
public void onRewardedVideoClosed(@NonNull String adUnitId) {
Log.d(TAG, "MoPubRewardedVideos onRewardedVideoClosed");
}
@Override
public void onRewardedVideoCompleted(@NonNull Set<String> adUnitIds, @NonNull MoPubReward reward) {
Log.d(TAG, "MoPubRewardedVideos onRewardedVideoCompleted");
}
}
}