Skip to content

Commit 56366fb

Browse files
committed
Preload ad implementation added.
1 parent b377a49 commit 56366fb

File tree

4 files changed

+104
-5
lines changed

4 files changed

+104
-5
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.gd.analytics;
2+
3+
/**
4+
* Created by demiremrece on 21.02.2018.
5+
*/
6+
7+
public abstract class GDPreloadListener {
8+
9+
public void onPreloadedAdCompleted() {
10+
}
11+
public void onAdPreloaded() {
12+
}
13+
public void onPreloadFailed(String msg) {
14+
}
15+
}

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

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
public class GDad {
2929

3030
GDadListener devListener;
31+
GDPreloadListener preloadListener;
3132
private PublisherInterstitialAd mInterstitialAd;
3233
private Activity mContext;
3334
private String mUnitId;
3435
private PublisherAdView publisherAdView;
3536
private FrameLayout rootview;
3637
private RelativeLayout relativeLayoutContainer;
3738
private boolean bannerActive = false;
39+
private boolean isPreloadStream = false;
3840
ArrayList<GDTunnlData> tunnlData;
3941
private int currentRequestInd = -1;
4042
private GDRequestAdHandler gdRequestAdHandler;
@@ -46,6 +48,8 @@ public void init(Activity mContext) {
4648

4749
if (devListener != null) devListener.onAPIReady();
4850

51+
requestPreloadAd();
52+
4953
}
5054

5155
public void init(Activity mContext, boolean isCordovaPlugin) {
@@ -55,6 +59,7 @@ public void init(Activity mContext, boolean isCordovaPlugin) {
5559

5660
if (devListener != null) devListener.onAPIReady();
5761

62+
requestPreloadAd();
5863

5964
} else {
6065
init(mContext);
@@ -201,6 +206,13 @@ public void onAdClosed() {
201206
GDutils.log("Ad closed.");
202207
if (devListener != null)
203208
devListener.onBannerClosed();
209+
210+
if(isPreloadStream){
211+
212+
}
213+
if(preloadListener != null){
214+
preloadListener.onPreloadedAdCompleted();
215+
}
204216
}
205217

206218
@Override
@@ -248,7 +260,9 @@ public void onAdOpened() {
248260
public void onAdLoaded() {
249261
super.onAdLoaded();
250262
GDutils.log("Ad received.");
251-
showInterstitialAd();
263+
264+
if(!isPreloadStream())
265+
showInterstitialAd();
252266

253267
GDEvent gdEvent = new GDEvent();
254268
gdEvent.isInterstitial = true;
@@ -259,6 +273,10 @@ public void onAdLoaded() {
259273

260274
if(!GDstatic.testAds)
261275
gdRequestAdHandler.Succes();
276+
277+
if(preloadListener != null){
278+
preloadListener.onAdPreloaded();
279+
}
262280
}
263281
});
264282
mInterstitialAd.loadAd(adRequest);
@@ -417,7 +435,7 @@ public void onError(VolleyError error) {
417435

418436
@Override
419437
public void Error(String err) {
420-
438+
421439
String url = tunnlData.get(currentRequestInd).getErr().replace("https","http");
422440
GDHttpRequest.sendStringRequest(GDlogger.mContext, url, Request.Method.GET, null, new GDHttpCallback() {
423441
@Override
@@ -454,6 +472,13 @@ public void onError(VolleyError error) {
454472

455473
}
456474

475+
public void requestPreloadAd(){
476+
if(isPreloadStream()){
477+
String args = "{isInterstitial: true}";
478+
showBanner(args);
479+
}
480+
}
481+
457482
public void destroyBanner() {
458483
if (publisherAdView != null && bannerActive) {
459484
publisherAdView.destroy();
@@ -513,4 +538,27 @@ public void setRelativeLayoutContainer(RelativeLayout relativeLayoutContainer) {
513538
this.relativeLayoutContainer = relativeLayoutContainer;
514539
}
515540

541+
public boolean isPreloadStream() {
542+
return isPreloadStream;
543+
}
544+
545+
public void setPreloadStream(boolean preloadStream) {
546+
isPreloadStream = preloadStream;
547+
}
548+
549+
public boolean isPreloadedAdExist(){
550+
return getmInterstitialAd().isLoaded() && isPreloadStream();
551+
}
552+
553+
public boolean isPreloadedAdLoading(){
554+
return getmInterstitialAd().isLoading() && isPreloadStream();
555+
}
556+
557+
public GDPreloadListener getPreloadListener() {
558+
return preloadListener;
559+
}
560+
561+
public void setPreloadListener(GDPreloadListener preloadListener) {
562+
this.preloadListener = preloadListener;
563+
}
516564
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public void onSuccess(JSONObject data) {
3434

3535
GDutils.log(data.toString());
3636
GDlogger.gDad.init(GDlogger.mContext,GDlogger.isCordovaPlugin);
37+
38+
GDlogger.gdPreloadStream.setPreloadStream(true);
39+
GDlogger.gdPreloadStream.init(GDlogger.mContext,GDlogger.isCordovaPlugin);
3740
}
3841
else{
3942
String error = "Something went wrong fetching game data.";
@@ -139,6 +142,23 @@ protected static void ShowBanner(String args) {
139142

140143
}
141144

145+
protected static void ShowPreloadedBanner(){
146+
147+
if(GDlogger.gdPreloadStream.isPreloadedAdExist())
148+
GDlogger.gdPreloadStream.showInterstitialAd();
149+
else if(!GDlogger.gdPreloadStream.isPreloadedAdLoading()){
150+
if(GDlogger.gdPreloadStream.preloadListener != null){
151+
GDlogger.gdPreloadStream.preloadListener.onPreloadFailed("No ads found preloaded.");
152+
}
153+
GDlogger.gdPreloadStream.requestPreloadAd();
154+
}
155+
else{
156+
if(GDlogger.gdPreloadStream.preloadListener != null){
157+
GDlogger.gdPreloadStream.preloadListener.onPreloadFailed("A preload ad is currently being loaded.");
158+
}
159+
}
160+
}
161+
142162
private static void setAdTimer(final boolean isInterstitial) {
143163

144164
if (GDstatic.enable) {

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

Lines changed: 19 additions & 3 deletions
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

@@ -115,11 +113,29 @@ public static void ShowBanner(Boolean isInterstitial) {
115113

116114
}
117115

116+
public static void ShowPreloadedAd(){
117+
if(GDutils.isOnline(mContext)){
118+
if (GDstatic.enable) {
119+
GDbanner.ShowPreloadedBanner();
120+
} else {
121+
Log.i("GDLogger", "GDApi is not initialized!");
122+
}
123+
}
124+
else{
125+
if(GDlogger.gdPreloadStream.preloadListener != null)
126+
GDlogger.gdPreloadStream.preloadListener.onPreloadFailed("API cannot connect to internet. Please check the network connection.");
127+
}
128+
}
129+
118130
public static void setAdListener(GDadListener gDadListener) {
119131
if (gDad != null)
120132
gDad.setAdListener(gDadListener);
121133
}
122134

135+
public static void setPreloadAdListener(GDPreloadListener preloadAdListener){
136+
if(gdPreloadStream != null)
137+
gdPreloadStream.setPreloadListener(preloadAdListener);
138+
}
123139
/**
124140
* GDlogger hides banner
125141
*/

0 commit comments

Comments
 (0)