16
16
import com .google .android .gms .ads .doubleclick .PublisherAdView ;
17
17
import com .google .android .gms .ads .doubleclick .PublisherInterstitialAd ;
18
18
import com .google .gson .Gson ;
19
-
20
19
import org .json .JSONArray ;
21
20
import org .json .JSONException ;
22
21
import org .json .JSONObject ;
23
-
24
- import java .security .KeyManagementException ;
25
- import java .security .NoSuchAlgorithmException ;
26
- import java .security .cert .CertificateException ;
27
- import java .security .cert .X509Certificate ;
28
22
import java .util .ArrayList ;
29
- import java .util .List ;
30
-
31
- import javax .net .ssl .HostnameVerifier ;
32
- import javax .net .ssl .HttpsURLConnection ;
33
- import javax .net .ssl .SSLContext ;
34
- import javax .net .ssl .SSLSession ;
35
- import javax .net .ssl .TrustManager ;
36
- import javax .net .ssl .X509TrustManager ;
37
23
38
24
39
25
/**
@@ -51,33 +37,32 @@ public class GDad {
51
37
private RelativeLayout relativeLayoutContainer ;
52
38
private boolean bannerActive = false ;
53
39
ArrayList <GDTunnlData > tunnlData ;
54
- int currentRequestInd = -1 ;
55
- GDRequestAdHandler gdRequestAdHandler ;
40
+ private int currentRequestInd = -1 ;
41
+ private GDRequestAdHandler gdRequestAdHandler ;
56
42
57
43
public void init (Activity mContext ) {
58
44
setmContext (mContext );
59
45
setRootview ((FrameLayout ) mContext .findViewById (android .R .id .content ));
60
46
initBannerObject ();
61
47
48
+ if (devListener != null ) devListener .onAPIReady ();
49
+
62
50
if (GDGameData .preRoll ){
63
51
showBanner ("{isInterstitial:" + true + "}" );
64
52
}
65
-
66
- disableSSLCertificateChecking ();
67
-
68
53
}
69
54
70
55
public void init (Activity mContext , boolean isCordovaPlugin ) {
71
56
if (isCordovaPlugin ) {
72
57
setmContext (mContext );
73
58
// in cordova plugin, there will be no banner ads.
74
59
60
+ if (devListener != null ) devListener .onAPIReady ();
61
+
75
62
if (GDGameData .preRoll ){
76
63
showBanner ("{isInterstitial:" + true + "}" );
77
64
}
78
65
79
- disableSSLCertificateChecking ();
80
-
81
66
} else {
82
67
init (mContext );
83
68
}
@@ -94,7 +79,6 @@ private void initBannerObject() {
94
79
setRelativeLayoutContainer (rl );
95
80
96
81
publisherAdView = new PublisherAdView (getmContext ());
97
- publisherAdView .setAdUnitId (getmUnitId ());
98
82
publisherAdView .setLayoutParams (new RelativeLayout .LayoutParams (
99
83
RelativeLayout .LayoutParams .WRAP_CONTENT ,
100
84
RelativeLayout .LayoutParams .WRAP_CONTENT
@@ -113,6 +97,19 @@ private void requestBanner(String size, String alignment, String position, Bundl
113
97
114
98
if (getRelativeLayoutContainer () != null ){
115
99
100
+ if (publisherAdView != null ){
101
+
102
+ getRelativeLayoutContainer ().removeView (publisherAdView );
103
+ publisherAdView = new PublisherAdView (getmContext ());
104
+ publisherAdView .setLayoutParams (new RelativeLayout .LayoutParams (
105
+ RelativeLayout .LayoutParams .WRAP_CONTENT ,
106
+ RelativeLayout .LayoutParams .WRAP_CONTENT
107
+ ));
108
+ setPublisherAdView (publisherAdView );
109
+ // add adcontainer into rootview
110
+ getRelativeLayoutContainer ().addView (publisherAdView );
111
+ }
112
+
116
113
PublisherAdRequest adRequest ;
117
114
PublisherAdRequest .Builder builder = new PublisherAdRequest .Builder ();
118
115
builder .addNetworkExtrasBundle (AdMobAdapter .class , customParams );
@@ -122,7 +119,6 @@ private void requestBanner(String size, String alignment, String position, Bundl
122
119
123
120
publisherAdView .setAdUnitId (unitId );
124
121
125
-
126
122
publisherAdView .loadAd (adRequest );
127
123
publisherAdView .setAdListener (new AdListener () {
128
124
@@ -152,13 +148,15 @@ public void onAdFailedToLoad(int errorCode) {
152
148
error += "No fill.\n The ad request was successful, but no ad was returned due to lack of ad inventory.\n Constant Value: " + errorCode ;
153
149
break ;
154
150
}
151
+
155
152
GDutils .log ("Ad failed to load: " + error );
156
153
GDutils .log ("For more details: https://developers.google.com/android/reference/com/google/android/gms/ads/AdRequest" );
157
154
158
- if (devListener != null )
155
+ if (devListener != null && GDstatic . testAds ){
159
156
devListener .onBannerFailed (error );
160
-
161
- gdRequestAdHandler .Error (error );
157
+ }
158
+ else if (devListener != null && gdRequestAdHandler != null )
159
+ gdRequestAdHandler .Error (error );
162
160
163
161
}
164
162
@@ -186,7 +184,8 @@ public void onAdLoaded() {
186
184
if (devListener != null )
187
185
devListener .onBannerRecieved (gdEvent );
188
186
189
- gdRequestAdHandler .Succes ();
187
+ if (!GDstatic .testAds )
188
+ gdRequestAdHandler .Succes ();
190
189
191
190
}
192
191
});
@@ -199,7 +198,6 @@ private void requestInterstitial(Bundle customParams, String unitId) {
199
198
200
199
PublisherAdRequest .Builder builder = new PublisherAdRequest .Builder ();
201
200
builder .addNetworkExtrasBundle (AdMobAdapter .class , customParams );
202
-
203
201
adRequest = builder .build ();
204
202
205
203
mInterstitialAd .setAdUnitId (unitId );
@@ -233,11 +231,11 @@ public void onAdFailedToLoad(int errorCode) {
233
231
GDutils .log ("Ad failed to load: " + error );
234
232
GDutils .log ("For more details: https://developers.google.com/android/reference/com/google/android/gms/ads/AdRequest" );
235
233
236
- if (devListener != null )
234
+ if (devListener != null && GDstatic . testAds ){
237
235
devListener .onBannerFailed (error );
238
-
239
- gdRequestAdHandler . Error ( error );
240
-
236
+ }
237
+ else if ( devListener != null && gdRequestAdHandler != null )
238
+ gdRequestAdHandler . Error ( error );
241
239
242
240
}
243
241
@@ -266,7 +264,8 @@ public void onAdLoaded() {
266
264
if (devListener != null )
267
265
devListener .onBannerRecieved (gdEvent );
268
266
269
- gdRequestAdHandler .Succes ();
267
+ if (!GDstatic .testAds )
268
+ gdRequestAdHandler .Succes ();
270
269
}
271
270
});
272
271
mInterstitialAd .loadAd (adRequest );
@@ -333,17 +332,22 @@ public void showBanner(String args) {
333
332
gDshowObj = gson .fromJson (args , GDshowObj .class );
334
333
335
334
if (GDstatic .testAds ){
336
- if (gDshowObj .isInterstitial )
335
+ if (gDshowObj .isInterstitial ){
337
336
this .setmUnitId (GDstatic .testInterUnitId );
338
- else
337
+ requestInterstitial (null , GDstatic .testInterUnitId );
338
+ }
339
+ else {
339
340
this .setmUnitId (GDstatic .testBannerUnitId );
341
+ requestBanner (gDshowObj .size , gDshowObj .alignment , gDshowObj .position ,null , GDstatic .testBannerUnitId );
342
+ }
340
343
}
341
344
else {
342
345
343
- String bundleId = "bundle.test.1" ;
344
- String dimension = "640x480" ;
345
- if (!gDshowObj .isInterstitial ) dimension = gDshowObj .size ;
346
- String url = "http://pub.tunnl.com/oppm?bundleid=" +bundleId +"&dnumber=" +dimension ;
346
+ currentRequestInd = -1 ;
347
+ String bundleId = GDGameData .bundleId ;
348
+ String msize = "interstitial" ;
349
+ if (!gDshowObj .isInterstitial ) msize = gDshowObj .size ;
350
+ String url = "http://pub.tunnl.com/oppm?bundleid=" +bundleId +"&msize=" +msize ;
347
351
348
352
// getting unit id from tunnl for ad request
349
353
GDHttpRequest .sendHttpRequest (GDlogger .mContext , url , Request .Method .GET , null , new GDHttpCallback () {
@@ -371,11 +375,16 @@ public void onSuccess(JSONObject data) {
371
375
tunnlData .add (gdTunnlData );
372
376
}
373
377
374
- requestHandler (gDshowObj );
378
+ if (items .length () > 0 )
379
+ requestHandler (gDshowObj );
380
+ else {
381
+ if (devListener != null )
382
+ devListener .onBannerFailed ("Something went wrong fetching advertisement settings. Please contact with support team." );
383
+ }
375
384
376
385
} catch (JSONException e ) {
377
386
e .printStackTrace ();
378
- GDutils .log ("Something went wrong parsing json game data.\n Data:\n " +data .toString ());
387
+ GDutils .log ("Something went wrong parsing json tunnl data.\n Data:\n " +data .toString ());
379
388
}
380
389
}
381
390
@ Override
@@ -440,42 +449,16 @@ public void onError(VolleyError error) {
440
449
else {
441
450
currentRequestInd = -1 ;
442
451
tunnlData = null ;
443
- }
444
452
445
- }
446
- };
447
-
448
-
449
- }
450
-
451
- private static void disableSSLCertificateChecking () {
452
- TrustManager [] trustAllCerts = new TrustManager [] { new X509TrustManager () {
453
- public X509Certificate [] getAcceptedIssuers () {
454
- return null ;
455
- }
453
+ if (devListener != null )
454
+ devListener .onBannerFailed (err );
456
455
457
- @ Override
458
- public void checkClientTrusted (X509Certificate [] arg0 , String arg1 ) throws CertificateException {
459
- // Not implemented
460
- }
456
+ }
461
457
462
- @ Override
463
- public void checkServerTrusted (X509Certificate [] arg0 , String arg1 ) throws CertificateException {
464
- // Not implemented
465
458
}
466
- } };
467
-
468
- try {
469
- SSLContext sc = SSLContext .getInstance ("TLS" );
459
+ };
470
460
471
- sc .init (null , trustAllCerts , new java .security .SecureRandom ());
472
461
473
- HttpsURLConnection .setDefaultSSLSocketFactory (sc .getSocketFactory ());
474
- } catch (KeyManagementException e ) {
475
- e .printStackTrace ();
476
- } catch (NoSuchAlgorithmException e ) {
477
- e .printStackTrace ();
478
- }
479
462
}
480
463
481
464
public void destroyBanner () {
0 commit comments