Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions modules/ads/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dependencies {
implementation project(":util")
}

ext.moduleName = 'com.gluonhq.attach.ads'
ext.description = 'Common API to access ad features'
51 changes: 51 additions & 0 deletions modules/ads/src/main/java/com/gluonhq/attach/ads/Ad.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.gluonhq.attach.ads;

/**
* The base class for all ad types.
*
* @param <T> the type of service the ad uses
*/
public abstract class Ad<T extends Ad.Service> {

/**
* The unique id of the ad.
*/
protected final long id;

/**
* The service the ad uses.
*/
protected final T service;

/**
* Constructs an ad.
*
* @param id the unique id of the ad
* @param service the service the ad uses
*/
public Ad(long id, T service) {
this.id = id;
this.service = service;
}

/**
* Disposes the ad.
*/
public void dispose() {
service.dispose(this);
}

/**
* Returns the unique id of the ad.
*
* @return the unique id of the ad
*/
public long getId() {
return id;
}

public interface Service {

void dispose(Ad<?> ad);
}
}
88 changes: 88 additions & 0 deletions modules/ads/src/main/java/com/gluonhq/attach/ads/AdListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.gluonhq.attach.ads;

/**
* A listener for receiving notifications during the lifecycle of an ad.
*/
public abstract class AdListener implements Callback {

/**
* Called when a click is recorded for an ad.
*/
public void onAdClicked() {
// empty
}

/**
* Called when the user is about to return to the application after clicking on an ad.
*/
public void onAdClosed() {
// empty
}

/**
* Called when an ad request failed.
*/
public void onAdFailedToLoad() {
// empty
}

/**
* Called when an impression is recorded for an ad.
*/
public void onAdImpression() {
// empty
}

/**
* Called when an ad is received.
*/
public void onAdLoaded() {
// empty
}

/**
* Called when an ad opens an overlay that covers the screen.
*/
public void onAdOpened() {
// empty
}

/**
* Called when a swipe gesture on an ad is recorded as a click.
*/
public void onAdSwipeGestureClicked() {
// empty
}

static class Adapter implements CallbackAdapter {

@Override
public void invoke(Ad<?> ad, Callback callback, String method, String[] params) {
AdListener c = (AdListener) callback;

switch (method) {
case "onAdClicked":
c.onAdClicked();
break;
case "onAdClosed":
c.onAdClosed();
break;
case "onAdFailedToLoad":
c.onAdFailedToLoad();
break;
case "onAdImpression":
c.onAdImpression();
break;
case "onAdLoaded":
c.onAdLoaded();
break;
case "onAdOpened":
c.onAdOpened();
break;
case "onAdSwipeGestureClicked":
c.onAdSwipeGestureClicked();
break;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.gluonhq.attach.ads;

/**
* Callback to be invoked when an ad finishes loading.
*
* @param <T> the type of ad the callback uses
*/
public abstract class AdLoadCallback<T> implements Callback {

/**
* Called when an ad fails to load.
*/
public void onAdFailedToLoad() {
// empty
}

/**
* Called when an ad successfully loads.
*
* @param ad the loaded ad
*/
public void onAdLoaded(T ad) {
// empty
}

static class Adapter<T> implements CallbackAdapter {

@Override
@SuppressWarnings("unchecked")
public void invoke(Ad<?> ad, Callback callback, String method, String[] params) {
AdLoadCallback<T> c = (AdLoadCallback<T>) callback;

switch (method) {
case "onAdFailedToLoad":
c.onAdFailedToLoad();
break;
case "onAdLoaded":
c.onAdLoaded((T) ad);
break;
}
}
}
}
68 changes: 68 additions & 0 deletions modules/ads/src/main/java/com/gluonhq/attach/ads/AdRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.gluonhq.attach.ads;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdRegistry {

private long id;

private final Map<String, CallbackAdapter> handlers;

private final Map<Long, Ad<?>> ads;

private final Map<Long, Map<String, Callback>> callbacks;

public AdRegistry() {
handlers = new HashMap<>();
ads = new HashMap<>();
callbacks = new HashMap<>();

handlers.put(AdListener.class.getSimpleName(), new AdListener.Adapter());
handlers.put(InterstitialAdLoadCallback.class.getSimpleName(), new AdLoadCallback.Adapter<InterstitialAd>());
handlers.put(RewardedAdLoadCallback.class.getSimpleName(), new AdLoadCallback.Adapter<RewardedAd>());
handlers.put(OnUserEarnedRewardListener.class.getSimpleName(), new OnUserEarnedRewardListener.Adapter());
handlers.put(FullScreenContentCallback.class.getSimpleName(), new FullScreenContentCallback.Adapter());
}

public void addAd(Ad<?> ad) {
ads.put(ad.getId(), ad);
callbacks.put(ad.getId(), new HashMap<>());
}

public void removeAd(long id) {
ads.remove(id);
callbacks.remove(id);
}

@SuppressWarnings("unchecked")
public <T extends Ad<?>> T getAd(long id) {
return (T) ads.get(id);
}

public <T extends Callback> void setCallback(long id, Class<T> callbackClass, T callback) {
Map<String, Callback> values = callbacks.get(id);
String name = callbackClass.getSimpleName();

values.remove(name);

if (callback != null) {
values.put(name, callback);
}
}

public void invokeCallback(long id, String callback, String method, String[] params) {
CallbackAdapter handler = handlers.get(callback);
Callback value = callbacks.get(id).get(callback);

if (value != null) {
handler.invoke(ads.get(id), value, method, params);
}
}

public long nextId() {
return id++;
}
}
41 changes: 41 additions & 0 deletions modules/ads/src/main/java/com/gluonhq/attach/ads/AdRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.gluonhq.attach.ads;

/**
* An AdRequest contains targeting information used to fetch an ad. Ad requests are created using AdRequest.Builder.
*/
public class AdRequest {

/**
* Constructs an AdRequest.
*/
private AdRequest() {
// empty
}

/**
* Builds an AdRequest.
*/
public static class Builder {

/**
* The AdRequest for this builder.
*/
private final AdRequest request;

/**
* Constructs a Builder.
*/
public Builder() {
request = new AdRequest();
}

/**
* Constructs an AdRequest with the specified attributes.
*
* @return the constructed ad request
*/
public AdRequest build() {
return request;
}
}
}
80 changes: 80 additions & 0 deletions modules/ads/src/main/java/com/gluonhq/attach/ads/AdsService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.gluonhq.attach.ads;

import com.gluonhq.attach.util.Services;

import java.util.Optional;

/**
* The ads service provides the ability to show ads with the Google Mobile Ads SDK on Android and iOS.
*
* <p><b>Example</b></p>
* <pre>
* {@code AdsService.create().ifPresent(service -> {
* service.initialize(() -> {
* BannerAd ad = service.newBannerAd();
*
* ad.setAdUnitId(BannerAd.TEST_AD_UNIT_ID);
* ad.setAdLayout(BannerAd.Layout.BOTTOM);
* ad.setAdSize(BannerAd.Size.BANNER);
* ad.load(new AdRequest.Builder().build());
* ad.show();
* });
* });}</pre>
*
* <p><b>Android Configuration</b>: none</p>
* <p><b>iOS Configuration</b>: none</p>
*
* @since 4.0.24
*/
public interface AdsService {

/**
* Creates an AdsService.
*
* @return the created ad service
*/
static Optional<AdsService> create() {
return Services.get(AdsService.class);
}

/**
* Initializes the Google Mobile Ads SDK. Call this method as early as possible after the app launches to reduce
* latency on the session's first ad request. If this method is not called, the first ad request automatically
* initializes the Google Mobile Ads SDK.
*
* @param listener a callback to be invoked upon initialization completion
*/
void initialize(OnInitializationCompleteListener listener);

/**
* Constructs a new BannerAd.
*
* @return the constructed banner ad
*/
BannerAd newBannerAd();

/**
* Loads an InterstitialAd.
*
* @param adUnitId the ad unit ID
* @param adRequest an ad request with targeting information
* @param callback a callback to be invoked when an interstitial ad finishes loading
*/
void loadInterstitialAd(String adUnitId, AdRequest adRequest, InterstitialAdLoadCallback callback);

/**
* Loads a RewardedAd.
*
* @param adUnitId the ad unit ID
* @param adRequest an ad request with targeting information
* @param callback a callback to be invoked when a rewarded ad finishes loading
*/
void loadRewardedAd(String adUnitId, AdRequest adRequest, RewardedAdLoadCallback callback);

/**
* Sets the global RequestConfiguration that will be used for every AdRequest during the app's session.
*
* @param requestConfiguration the request configuration
*/
void setRequestConfiguration(RequestConfiguration requestConfiguration);
}
Loading