Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ads module. #390

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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'
39 changes: 39 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,39 @@
package com.gluonhq.attach.ads;

/**
* Base class for all ads.
*
* @param <T> the service type the ad expects
*/
public abstract class Ad<T> {

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

/**
* The service that provides the methods for this ad.
*/
protected final T service;

/**
* Constructs a new ad with the given id and service.
*
* @param id the unique id of this ad
* @param service the service of this ad
*/
public Ad(long id, T service) {
this.id = id;
this.service = service;
}

/**
* Get the unique id of this ad.
*
* @return the unique id
*/
public long getId() {
return id;
}
}
81 changes: 81 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,81 @@
package com.gluonhq.attach.ads;

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

/**
* Invokes callbacks that are received from the native code.
*/
static class Handler implements AdRegistry.CallbackHandler {

/**
* Match the callback from native code to a method defined
* in this class.
*
* @param ad the ad the callback is intended for
* @param callback the class the callback method belongs to
* @param callbackMethod the callback method to call
* @param params the params for the callback method
*/
@Override
public void handle(Ad<?> ad, Object callback, String callbackMethod, String[] params) {
AdListener c = (AdListener) callback;

switch (callbackMethod) {
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;
}
}
}

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

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

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

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

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

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

/**
* Called when a swipe gesture on an ad is recorded as a click.
*/
public void onAdSwipeGestureClicked() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.gluonhq.attach.ads;

/**
* Callback to be invoked when an ad finishes loading.
*
* @param <T> the exact type of the ad that is loaded
*/
public abstract class AdLoadCallback<T> {

/**
* Invokes callbacks that are received from the native code.
*
* @param <T> the exact type of the ad that is received when
* it is successfully loaded
*/
static class Handler<T> implements AdRegistry.CallbackHandler {

/**
* Match the callback from native code to a method defined
* in this class.
*
* @param ad the ad the callback is intended for
* @param callback the class the callback method belongs to
* @param callbackMethod the callback method to call
* @param params the params for the callback method
*/
@SuppressWarnings("unchecked")
@Override
public void handle(Ad<?> ad, Object callback, String callbackMethod, String[] params) {
AdLoadCallback<T> c = (AdLoadCallback<T>) callback;

switch (callbackMethod) {
case "onAdFailedToLoad": c.onAdFailedToLoad(); break;
case "onAdLoaded": c.onAdLoaded((T) ad); break;
}
}
}

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

/**
* Called when an ad successfully loads.
*
* @param ad the loaded ad
*/
public void onAdLoaded(T ad) {
}
}
174 changes: 174 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,174 @@
package com.gluonhq.attach.ads;

import com.gluonhq.attach.ads.impl.AndroidAdsService;

import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Stores all ads with their unique ids.
*/
public class AdRegistry {

/**
* Logger used for logging.
*/
private static final Logger LOG = Logger.getLogger(AdRegistry.class.getName());


/**
* Handles the conversion from a callback from the native code
* to a method from the java side.
*/
public interface CallbackHandler {

/**
* Converts a callback from native code to java.
*
* @param ad the ad the callback is intended for
* @param callback the callback class
* @param callbackMethod the callback method inside the
* callback class
* @param params the parameters for the method call
*/
void handle(Ad<?> ad, Object callback, String callbackMethod, String[] params);
}

/**
* The unique id of the next created ad. This is always
* incremented by one after an ad got created.
*/
private long id;

/**
* Contains the callback handlers for the ads.
*/
private final Map<String, CallbackHandler> handlers;

/**
* Contains all ads accessible by their ids.
*/
private final Map<Long, Ad<?>> ads;

/**
* Contains all registered callbacks for ads.
*/
private final Map<Long, List<Callback>> callbacks;

/**
* Constructs a new instance and initializes the callback
* handlers.
*/
public AdRegistry() {
handlers = new HashMap<>() {{
put(AdListener.class.getSimpleName(), new AdListener.Handler());
put(InterstitialAdLoadCallback.class.getSimpleName(), new AdLoadCallback.Handler<InterstitialAd>());
put(RewardedAdLoadCallback.class.getSimpleName(), new AdLoadCallback.Handler<RewardedAd>());
put(OnUserEarnedRewardListener.class.getSimpleName(), new OnUserEarnedRewardListener.Handler());
put(FullScreenContentCallback.class.getSimpleName(), new FullScreenContentCallback.Handler());
}};

ads = new HashMap<>();
callbacks = new HashMap<>();

LOG.log(Level.INFO, "AdRegistry(): " + this);
}

/**
* Add an ad to the registry and assign it a unique id.
*
* @param ad the ad to add
*/
public void addAd(Ad<?> ad) {
if (ads.containsKey(ad.getId())) {
throw new IllegalArgumentException("Id '" + ad.getId() + "' is already inside the ad registry");
}

ads.put(ad.getId(), ad);
LOG.log(Level.INFO, "addAd(): " + this);
}

/**
* Remove the ad with the provided id.
*
* @param id the id of the ad to remove
*/
public void removeAd(long id) {
ads.remove(id);
callbacks.remove(id);
LOG.log(Level.INFO, "removeAd(): " + this);
}

/**
* Get the ad with the specified id.
*
* @param id the id of the ad
* @return the ad with the specified id
* @param <T> the exact type of the ad
*/
@SuppressWarnings("unchecked")
public <T extends Ad<?>> T getAd(long id) {
return (T) ads.get(id);
}

/**
* Add a callback to the ad with the specified id.
*
* @param id the id of the ad
* @param callbackObject the callback that is added to the ad
*/
public void addCallback(long id, Object callbackObject) {
Callback callback = new Callback(callbackObject);
List<Callback> callbackList;

if (callbacks.containsKey(id)) {
callbackList = callbacks.get(id);
} else {
callbackList = new ArrayList<>();
callbacks.put(id, callbackList);
}

// remove same callbacks, then add
callbackList.removeIf(c -> c.getName().equals(callback.getName()));
callbackList.add(callback);
LOG.log(Level.INFO, "addCallback(): " + this);
}

/**
* Called from the native side when a callback occurs.
*
* @param id the id of the ad the callback is intended for
* @param callbackClass the name of the callback class
* @param callbackMethod the name of the callback method
* @param params the params for the callback method
*/
public void invokeCallback(long id, String callbackClass, String callbackMethod, String[] params) {
LOG.log(Level.INFO, "Registry invokeCallback");
CallbackHandler handler = handlers.get(callbackClass);
Callback callback = callbacks.get(id).stream()
.filter(c -> c.getName().equals(callbackClass))
.findFirst()
.orElseThrow();

LOG.log(Level.INFO, "c: " + callback);

handler.handle(getAd(id), callback.getCallback(), callbackMethod, params);
LOG.log(Level.INFO, "invokeCallback(): " + this);
}

/**
* Get the next unique id.
*
* @return the next unique id
*/
public long getId() {
return id++;
}

@Override
public String toString() {
return "{id=" + id + ", handlers=" + handlers + ", ads=" + ads + ", callbacks:" + callbacks + "}";
}
}
39 changes: 39 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,39 @@
package com.gluonhq.attach.ads;

/**
* Configures the request details for a single ad.
*/
public class AdRequest {

/**
* Builds a new {@code AdRequest}.
*/
public static class Builder {

/**
* The final {@code AdRequest}.
*/
private final AdRequest request;

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

/**
* Returns the final {@code AdRequest}.
*
* @return the final {@code AdRequest}
*/
public AdRequest build() {
return request;
}
}

/**
* Default constructor.
*/
private AdRequest() {}
}
Loading