Skip to content

Commit 95b505b

Browse files
authored
Merge pull request #3 from GameDistribution/preload-ads
Preload ads
2 parents b377a49 + 66c69bc commit 95b505b

File tree

5 files changed

+97
-30
lines changed

5 files changed

+97
-30
lines changed

Source/GDApi/gdapi/libs/gdapi.jar

-23.1 KB
Binary file not shown.

Source/GDApi/gdapi/src/main/java/com/gd/analytics/GDad.java

+45-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class GDad {
3535
private FrameLayout rootview;
3636
private RelativeLayout relativeLayoutContainer;
3737
private boolean bannerActive = false;
38+
private boolean isPreloadStream = false;
3839
ArrayList<GDTunnlData> tunnlData;
3940
private int currentRequestInd = -1;
4041
private GDRequestAdHandler gdRequestAdHandler;
@@ -46,6 +47,8 @@ public void init(Activity mContext) {
4647

4748
if (devListener != null) devListener.onAPIReady();
4849

50+
requestPreloadAd();
51+
4952
}
5053

5154
public void init(Activity mContext, boolean isCordovaPlugin) {
@@ -55,6 +58,7 @@ public void init(Activity mContext, boolean isCordovaPlugin) {
5558

5659
if (devListener != null) devListener.onAPIReady();
5760

61+
requestPreloadAd();
5862

5963
} else {
6064
init(mContext);
@@ -148,7 +152,7 @@ public void onAdFailedToLoad(int errorCode) {
148152
if (devListener != null && GDstatic.testAds){
149153
devListener.onBannerFailed(error);
150154
}
151-
else if(devListener != null && gdRequestAdHandler != null)
155+
else if(!GDstatic.testAds && gdRequestAdHandler != null)
152156
gdRequestAdHandler.Error(error);
153157

154158
}
@@ -201,6 +205,13 @@ public void onAdClosed() {
201205
GDutils.log("Ad closed.");
202206
if (devListener != null)
203207
devListener.onBannerClosed();
208+
209+
if(isPreloadStream){
210+
requestPreloadAd();
211+
}
212+
if(isPreloadStream && GDlogger.gDad.devListener != null){
213+
GDlogger.gDad.devListener.onPreloadedAdCompleted();
214+
}
204215
}
205216

206217
@Override
@@ -227,7 +238,7 @@ public void onAdFailedToLoad(int errorCode) {
227238
if (devListener != null && GDstatic.testAds){
228239
devListener.onBannerFailed(error);
229240
}
230-
else if(devListener != null && gdRequestAdHandler != null)
241+
else if(!GDstatic.testAds && gdRequestAdHandler != null)
231242
gdRequestAdHandler.Error(error);
232243

233244
}
@@ -248,7 +259,9 @@ public void onAdOpened() {
248259
public void onAdLoaded() {
249260
super.onAdLoaded();
250261
GDutils.log("Ad received.");
251-
showInterstitialAd();
262+
263+
if(!isPreloadStream())
264+
showInterstitialAd();
252265

253266
GDEvent gdEvent = new GDEvent();
254267
gdEvent.isInterstitial = true;
@@ -259,6 +272,10 @@ public void onAdLoaded() {
259272

260273
if(!GDstatic.testAds)
261274
gdRequestAdHandler.Succes();
275+
276+
if(isPreloadStream && GDlogger.gDad.devListener != null){
277+
GDlogger.gDad.devListener.onAdPreloaded();
278+
}
262279
}
263280
});
264281
mInterstitialAd.loadAd(adRequest);
@@ -417,7 +434,7 @@ public void onError(VolleyError error) {
417434

418435
@Override
419436
public void Error(String err) {
420-
437+
421438
String url = tunnlData.get(currentRequestInd).getErr().replace("https","http");
422439
GDHttpRequest.sendStringRequest(GDlogger.mContext, url, Request.Method.GET, null, new GDHttpCallback() {
423440
@Override
@@ -454,6 +471,13 @@ public void onError(VolleyError error) {
454471

455472
}
456473

474+
public void requestPreloadAd(){
475+
if(isPreloadStream()){
476+
String args = "{isInterstitial: true}";
477+
showBanner(args);
478+
}
479+
}
480+
457481
public void destroyBanner() {
458482
if (publisherAdView != null && bannerActive) {
459483
publisherAdView.destroy();
@@ -513,4 +537,21 @@ public void setRelativeLayoutContainer(RelativeLayout relativeLayoutContainer) {
513537
this.relativeLayoutContainer = relativeLayoutContainer;
514538
}
515539

540+
public boolean isPreloadStream() {
541+
return isPreloadStream;
542+
}
543+
544+
public void setPreloadStream(boolean preloadStream) {
545+
isPreloadStream = preloadStream;
546+
}
547+
548+
public boolean isPreloadedAdExist(){
549+
if (getmInterstitialAd() == null) return false;
550+
return getmInterstitialAd().isLoaded() && isPreloadStream();
551+
}
552+
553+
public boolean isPreloadedAdLoading(){
554+
if (getmInterstitialAd() == null) return false;
555+
return getmInterstitialAd().isLoading() && isPreloadStream();
556+
}
516557
}

Source/GDApi/gdapi/src/main/java/com/gd/analytics/GDadListener.java

+6
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,11 @@ public void onAPIReady(){
1818
}
1919
public void onAPINotReady(String error){
2020
}
21+
public void onPreloadedAdCompleted() {
22+
}
23+
public void onAdPreloaded() {
24+
}
25+
public void onPreloadFailed(String msg) {
26+
}
2127

2228
}

Source/GDApi/gdapi/src/main/java/com/gd/analytics/GDbanner.java

+45-21
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ class GDbanner {
1313

1414
protected static void init() {
1515

16-
if(GDstatic.enable && !GDstatic.testAds) {
17-
String url = GDstatic.GAME_API_URL+ '/' + GDstatic.gameId;
16+
if (GDstatic.enable && !GDstatic.testAds) {
17+
String url = GDstatic.GAME_API_URL + '/' + GDstatic.gameId;
1818
GDHttpRequest.sendHttpRequest(GDlogger.mContext, url, Request.Method.GET, null, new GDHttpCallback() {
1919
@Override
2020
public void onSuccess(JSONObject data) {
2121

2222
try {
2323
boolean success = data.getBoolean("success");
24-
if(success) {
24+
if (success) {
2525
JSONObject result = data.getJSONObject("result");
2626
JSONObject game = result.getJSONObject("game");
2727

@@ -33,13 +33,15 @@ public void onSuccess(JSONObject data) {
3333
GDGameData.bundleId = game.getString("androidBundleId");
3434

3535
GDutils.log(data.toString());
36-
GDlogger.gDad.init(GDlogger.mContext,GDlogger.isCordovaPlugin);
37-
}
38-
else{
36+
GDlogger.gDad.init(GDlogger.mContext, GDlogger.isCordovaPlugin);
37+
38+
GDlogger.gdPreloadStream.setPreloadStream(true);
39+
GDlogger.gdPreloadStream.init(GDlogger.mContext, GDlogger.isCordovaPlugin);
40+
} else {
3941
String error = "Something went wrong fetching game data.";
4042
GDutils.log(error);
4143

42-
if(GDlogger.gDad.devListener != null)
44+
if (GDlogger.gDad.devListener != null)
4345
GDlogger.gDad.devListener.onAPINotReady(error);
4446
GDstatic.enable = false;
4547

@@ -48,7 +50,7 @@ public void onSuccess(JSONObject data) {
4850
} catch (JSONException e) {
4951
e.printStackTrace();
5052
GDutils.log("Something went wrong parsing json game data.");
51-
if(GDlogger.gDad.devListener != null)
53+
if (GDlogger.gDad.devListener != null)
5254
GDlogger.gDad.devListener.onAPINotReady("Something went wrong parsing json game data.");
5355
GDstatic.enable = false;
5456

@@ -58,15 +60,16 @@ public void onSuccess(JSONObject data) {
5860
@Override
5961
public void onError(VolleyError error) {
6062
GDutils.log("Something went wrong fetching json game data.");
61-
if(GDlogger.gDad.devListener != null)
63+
if (GDlogger.gDad.devListener != null)
6264
GDlogger.gDad.devListener.onAPINotReady("Something went wrong fetching json game data.");
6365
GDstatic.enable = false;
6466

6567
}
6668
});
67-
}
68-
else if(GDstatic.testAds){
69-
GDlogger.gDad.init(GDlogger.mContext,GDlogger.isCordovaPlugin);
69+
} else if (GDstatic.testAds) {
70+
GDlogger.gDad.init(GDlogger.mContext, GDlogger.isCordovaPlugin);
71+
GDlogger.gdPreloadStream.setPreloadStream(true);
72+
GDlogger.gdPreloadStream.init(GDlogger.mContext, GDlogger.isCordovaPlugin);
7073
}
7174
}
7275

@@ -76,11 +79,12 @@ protected static void ShowBanner(String args) {
7679
final GDshowObj gDshowObj;
7780
gDshowObj = gson.fromJson(args, GDshowObj.class);
7881

79-
8082
if ((GDGameData.enableAds || GDstatic.testAds) && gDshowObj._key != null && gDshowObj._key.equals("preroll") && GDlogger.gDad != null) {
8183

8284
if (GDGameData.preRoll) {
83-
GDlogger.gDad.showBanner(args);
85+
86+
ShowPreloadedBanner(args);
87+
8488
} else {
8589
if (GDlogger.gDad.devListener != null) {
8690
GDlogger.gDad.devListener.onBannerFailed("Banner request failed: 'Preroll is disabled.'");
@@ -93,14 +97,13 @@ protected static void ShowBanner(String args) {
9397
if (gDshowObj.isInterstitial) {
9498
if (GDstatic.reqInterstitialEnabled) {
9599

96-
GDlogger.gDad.showBanner(args);
100+
ShowPreloadedBanner(args);
97101
adInterstitialTimer = null;
98102

99-
if(!GDstatic.testAds){
103+
if (!GDstatic.testAds) {
100104
setAdTimer(true); // inter timer
101105
GDstatic.reqInterstitialEnabled = false;
102-
}
103-
else{
106+
} else {
104107
GDstatic.reqInterstitialEnabled = true;
105108
}
106109

@@ -110,12 +113,11 @@ protected static void ShowBanner(String args) {
110113
GDlogger.gDad.devListener.onBannerFailed("You can not invoke 'ShowBanner()' within " + GDGameData.timeAds + " min(s).");
111114
}
112115
}
113-
}
114-
else {
116+
} else {
115117
if (GDstatic.reqBannerEnabled) {
116118
GDlogger.gDad.showBanner(args);
117119
adBannerTimer = null;
118-
if(!GDstatic.testAds){
120+
if (!GDstatic.testAds) {
119121
setAdTimer(false); // banner timer
120122
GDstatic.reqBannerEnabled = false;
121123
}
@@ -132,13 +134,35 @@ protected static void ShowBanner(String args) {
132134
if (GDlogger.gDad.devListener != null) {
133135
GDlogger.gDad.devListener.onBannerFailed("Banner request failed: 'Midroll is disabled.'");
134136
}
137+
135138
}
136139

137140
}
138141

139142

140143
}
141144

145+
protected static void ShowPreloadedBanner(String args) {
146+
// If preloaded ad exists, then show it.
147+
// Otherwise, make sure you request preload and also request new ad to show.
148+
if (GDlogger.gdPreloadStream.isPreloadedAdExist()){
149+
GDlogger.gdPreloadStream.showInterstitialAd();
150+
}
151+
else{
152+
if (!GDlogger.gdPreloadStream.isPreloadedAdLoading()) {
153+
if (GDlogger.gDad.devListener != null) {
154+
GDlogger.gDad.devListener.onPreloadFailed("No ads found preloaded.");
155+
}
156+
GDlogger.gdPreloadStream.requestPreloadAd();
157+
} else {
158+
if (GDlogger.gDad.devListener != null) {
159+
GDlogger.gDad.devListener.onPreloadFailed("A preload ad is currently being loaded.");
160+
}
161+
}
162+
GDlogger.gDad.showBanner(args);
163+
}
164+
}
165+
142166
private static void setAdTimer(final boolean isInterstitial) {
143167

144168
if (GDstatic.enable) {

Source/GDApi/gdapi/src/main/java/com/gd/analytics/GDlogger.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package com.gd.analytics;
22

33
import android.app.Activity;
4-
import android.content.Context;
5-
import android.net.ConnectivityManager;
6-
import android.net.NetworkInfo;
74
import android.util.Log;
85

96
public class GDlogger {
107

118
static Activity mContext;
129
static GDad gDad = new GDad();
10+
static GDad gdPreloadStream = new GDad();
1311

1412
static boolean isCordovaPlugin = false;
1513

@@ -99,7 +97,6 @@ public static void ShowBanner(String size, String alignment, String position) {
9997
}
10098

10199
public static void ShowBanner(Boolean isInterstitial) {
102-
103100
if(GDutils.isOnline(mContext)){
104101
String args = "{isInterstitial:" + isInterstitial + "}";
105102
if (GDstatic.enable) {
@@ -112,7 +109,6 @@ public static void ShowBanner(Boolean isInterstitial) {
112109
if(GDlogger.gDad.devListener != null)
113110
GDlogger.gDad.devListener.onBannerFailed("API cannot connect to internet. Please check the network connection.");
114111
}
115-
116112
}
117113

118114
public static void setAdListener(GDadListener gDadListener) {

0 commit comments

Comments
 (0)