Android: adInfoChanged, per-ad impressionData with a capped pre-listener queue, capabilities, segments, dynamic user id, placement capping, getReward
- Every rewarded / interstitial ad object gets the plugin's LevelPlayImpressionDataListener; impressions that arrive before the first JS impressionData listener are queued (max 100, oldest dropped) and flushed by an addListener override. - AdInfo gains ab, segmentName, encryptedCPM, creativeId and adSize. - setSegment validates customParameters against the SDK limits (1-32 letters or digits, at most 5) and rejects INVALID_ARGUMENT. - OverlayMath: pure geometry / validation helpers with JVM unit tests.
This commit is contained in:
parent
e31f019a7c
commit
479c4aadda
5 changed files with 536 additions and 27 deletions
|
|
@ -0,0 +1,20 @@
|
||||||
|
package fyi.crt.capacitor.levelplay;
|
||||||
|
|
||||||
|
/** A promise rejection with one of the plugin's string codes (INVALID_ARGUMENT, NOT_FOUND ...). */
|
||||||
|
final class CallError extends Exception {
|
||||||
|
|
||||||
|
final String code;
|
||||||
|
|
||||||
|
CallError(String code, String message) {
|
||||||
|
super(message);
|
||||||
|
this.code = code;
|
||||||
|
}
|
||||||
|
|
||||||
|
static CallError invalid(String message) {
|
||||||
|
return new CallError("INVALID_ARGUMENT", message);
|
||||||
|
}
|
||||||
|
|
||||||
|
static CallError notFound(String what, String id) {
|
||||||
|
return new CallError("NOT_FOUND", what + " '" + id + "' not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -18,24 +18,34 @@ import com.unity3d.mediation.LevelPlayInitError;
|
||||||
import com.unity3d.mediation.LevelPlayInitListener;
|
import com.unity3d.mediation.LevelPlayInitListener;
|
||||||
import com.unity3d.mediation.LevelPlayInitRequest;
|
import com.unity3d.mediation.LevelPlayInitRequest;
|
||||||
import com.unity3d.mediation.LevelPlayPrivacySettings;
|
import com.unity3d.mediation.LevelPlayPrivacySettings;
|
||||||
|
import com.unity3d.mediation.impression.LevelPlayImpressionDataListener;
|
||||||
import com.unity3d.mediation.interstitial.LevelPlayInterstitialAd;
|
import com.unity3d.mediation.interstitial.LevelPlayInterstitialAd;
|
||||||
import com.unity3d.mediation.interstitial.LevelPlayInterstitialAdListener;
|
import com.unity3d.mediation.interstitial.LevelPlayInterstitialAdListener;
|
||||||
import com.unity3d.mediation.rewarded.LevelPlayReward;
|
import com.unity3d.mediation.rewarded.LevelPlayReward;
|
||||||
import com.unity3d.mediation.rewarded.LevelPlayRewardedAd;
|
import com.unity3d.mediation.rewarded.LevelPlayRewardedAd;
|
||||||
import com.unity3d.mediation.rewarded.LevelPlayRewardedAdListener;
|
import com.unity3d.mediation.rewarded.LevelPlayRewardedAdListener;
|
||||||
|
import com.unity3d.mediation.segment.LevelPlaySegment;
|
||||||
|
import java.util.ArrayDeque;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import org.json.JSONArray;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Capacitor bridge for the Unity LevelPlay SDK (ad-unit API: LevelPlayRewardedAd / LevelPlayInterstitialAd).
|
* Capacitor bridge for the Unity LevelPlay SDK (ad-unit API: LevelPlayRewardedAd / LevelPlayInterstitialAd /
|
||||||
* Rewarded and interstitial only. Every SDK call runs on the main thread; plugin state is only touched there.
|
* LevelPlayBannerAdView, plus LevelPlayNativeAd). Every SDK call and every view operation runs on the main thread;
|
||||||
|
* plugin state is only touched there.
|
||||||
*/
|
*/
|
||||||
@CapacitorPlugin(name = "LevelPlay")
|
@CapacitorPlugin(name = "LevelPlay")
|
||||||
public class LevelPlayPlugin extends Plugin {
|
public class LevelPlayPlugin extends Plugin {
|
||||||
|
|
||||||
private static final long LATE_REWARD_GRACE_MS = 1000;
|
private static final long LATE_REWARD_GRACE_MS = 1000;
|
||||||
|
/** Impressions kept while no JS impressionData listener exists (owner decision Q2); the oldest is dropped. */
|
||||||
|
static final int IMPRESSION_QUEUE_CAP = 100;
|
||||||
|
static final String EVENT_IMPRESSION = "impressionData";
|
||||||
|
|
||||||
private enum InitState {
|
private enum InitState {
|
||||||
IDLE,
|
IDLE,
|
||||||
|
|
@ -48,6 +58,13 @@ public class LevelPlayPlugin extends Plugin {
|
||||||
private final List<PluginCall> pendingInits = new ArrayList<>();
|
private final List<PluginCall> pendingInits = new ArrayList<>();
|
||||||
private final Map<String, RewardedHolder> rewarded = new HashMap<>();
|
private final Map<String, RewardedHolder> rewarded = new HashMap<>();
|
||||||
private final Map<String, InterstitialHolder> interstitials = new HashMap<>();
|
private final Map<String, InterstitialHolder> interstitials = new HashMap<>();
|
||||||
|
private final ArrayDeque<JSObject> impressionQueue = new ArrayDeque<>();
|
||||||
|
|
||||||
|
/** Attached to every rewarded, interstitial and banner object (per-ad listeners, owner decision Q1). */
|
||||||
|
final LevelPlayImpressionDataListener impressionListener = data -> {
|
||||||
|
JSObject o = Mappers.impression(data);
|
||||||
|
main.post(() -> emitImpression(o));
|
||||||
|
};
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------- init / settings
|
// ---------------------------------------------------------------------------------------------- init / settings
|
||||||
|
|
||||||
|
|
@ -267,6 +284,123 @@ public class LevelPlayPlugin extends Plugin {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------------------- 0.3.0: capabilities, user
|
||||||
|
|
||||||
|
@PluginMethod
|
||||||
|
public void getCapabilities(PluginCall call) {
|
||||||
|
JSObject r = new JSObject();
|
||||||
|
r.put("banner", true);
|
||||||
|
r.put("nativeAd", true);
|
||||||
|
r.put("nativeAdTemplates", new JSONArray().put("small").put("medium"));
|
||||||
|
r.put("impressionData", true);
|
||||||
|
r.put("adInfoChanged", true);
|
||||||
|
call.resolve(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PluginMethod
|
||||||
|
public void setSegment(PluginCall call) {
|
||||||
|
JSObject o = call.getData();
|
||||||
|
LevelPlaySegment segment = new LevelPlaySegment();
|
||||||
|
try {
|
||||||
|
String name = o.getString("segmentName");
|
||||||
|
if (name != null) segment.setSegmentName(name);
|
||||||
|
Number level = number(o, "level");
|
||||||
|
if (level != null) {
|
||||||
|
if (level.doubleValue() != Math.rint(level.doubleValue())) throw CallError.invalid("level must be an integer");
|
||||||
|
segment.setLevel(level.intValue());
|
||||||
|
}
|
||||||
|
if (o.has("isPaying")) {
|
||||||
|
Object paying = o.opt("isPaying");
|
||||||
|
if (!(paying instanceof Boolean)) throw CallError.invalid("isPaying must be a boolean");
|
||||||
|
segment.setPaying((Boolean) paying);
|
||||||
|
}
|
||||||
|
Number created = number(o, "userCreationDate");
|
||||||
|
if (created != null) segment.setUserCreationDate(created.longValue());
|
||||||
|
Number iap = number(o, "iapTotal");
|
||||||
|
if (iap != null) segment.setIapTotal(iap.doubleValue());
|
||||||
|
JSObject custom = o.getJSObject("customParameters");
|
||||||
|
if (custom == null && o.has("customParameters") && !o.isNull("customParameters")) {
|
||||||
|
throw CallError.invalid("customParameters must be an object of strings");
|
||||||
|
}
|
||||||
|
if (custom != null) {
|
||||||
|
Map<String, Object> params = new LinkedHashMap<>();
|
||||||
|
Iterator<String> keys = custom.keys();
|
||||||
|
while (keys.hasNext()) {
|
||||||
|
String k = keys.next();
|
||||||
|
params.put(k, custom.opt(k));
|
||||||
|
}
|
||||||
|
String err = OverlayMath.customParamsError(params);
|
||||||
|
if (err != null) throw CallError.invalid(err);
|
||||||
|
for (Map.Entry<String, Object> e : params.entrySet()) segment.setCustom(e.getKey(), (String) e.getValue());
|
||||||
|
}
|
||||||
|
} catch (CallError e) {
|
||||||
|
reject(call, e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
main.post(() -> {
|
||||||
|
LevelPlay.setSegment(segment);
|
||||||
|
call.resolve();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private static Number number(JSObject o, String key) throws CallError {
|
||||||
|
if (!o.has(key) || o.isNull(key)) return null;
|
||||||
|
Object v = o.opt(key);
|
||||||
|
if (!(v instanceof Number)) throw CallError.invalid(key + " must be a number");
|
||||||
|
double d = ((Number) v).doubleValue();
|
||||||
|
if (Double.isNaN(d) || Double.isInfinite(d)) throw CallError.invalid(key + " must be finite");
|
||||||
|
return (Number) v;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PluginMethod
|
||||||
|
public void setDynamicUserId(PluginCall call) {
|
||||||
|
String userId = call.getString("userId");
|
||||||
|
if (userId == null || userId.isEmpty()) {
|
||||||
|
call.reject("userId is required", "INVALID_ARGUMENT");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
main.post(() -> {
|
||||||
|
JSObject r = new JSObject();
|
||||||
|
r.put("valid", LevelPlay.setDynamicUserId(userId));
|
||||||
|
call.resolve(r);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@PluginMethod
|
||||||
|
public void isPlacementCapped(PluginCall call) {
|
||||||
|
String format = call.getString("format");
|
||||||
|
String placement = call.getString("placementName");
|
||||||
|
if (!"rewarded".equals(format) && !"interstitial".equals(format)) {
|
||||||
|
call.reject("format must be 'rewarded' or 'interstitial'", "INVALID_ARGUMENT");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (placement == null || placement.isEmpty()) {
|
||||||
|
call.reject("placementName is required", "INVALID_ARGUMENT");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
main.post(() -> {
|
||||||
|
boolean capped = "interstitial".equals(format)
|
||||||
|
? LevelPlayInterstitialAd.isPlacementCapped(placement)
|
||||||
|
: LevelPlayRewardedAd.isPlacementCapped(placement);
|
||||||
|
JSObject r = new JSObject();
|
||||||
|
r.put("capped", capped);
|
||||||
|
call.resolve(r);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@PluginMethod
|
||||||
|
public void getReward(PluginCall call) {
|
||||||
|
String adUnitId = requireAdUnit(call);
|
||||||
|
if (adUnitId == null) return;
|
||||||
|
String placement = call.getString("placementName");
|
||||||
|
main.post(() -> {
|
||||||
|
LevelPlayRewardedAd ad = rewardedHolder(adUnitId).ad;
|
||||||
|
LevelPlayReward r = placement != null && !placement.isEmpty() ? ad.getReward(placement) : ad.getReward();
|
||||||
|
call.resolve(Mappers.reward(r));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------- helpers
|
// ---------------------------------------------------------------------------------------------- helpers
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|
@ -297,35 +431,45 @@ public class LevelPlayPlugin extends Plugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static JSObject info(@Nullable LevelPlayAdInfo i) {
|
private static JSObject info(@Nullable LevelPlayAdInfo i) {
|
||||||
JSObject o = new JSObject();
|
return Mappers.adInfo(i);
|
||||||
if (i == null) return o;
|
|
||||||
o.put("adId", i.getAdId());
|
|
||||||
o.put("adUnitId", i.getAdUnitId());
|
|
||||||
o.put("adUnitName", i.getAdUnitName());
|
|
||||||
o.put("adFormat", i.getAdFormat());
|
|
||||||
o.put("placementName", i.getPlacementName());
|
|
||||||
o.put("adNetwork", i.getAdNetwork());
|
|
||||||
o.put("instanceName", i.getInstanceName());
|
|
||||||
o.put("instanceId", i.getInstanceId());
|
|
||||||
o.put("country", i.getCountry());
|
|
||||||
o.put("auctionId", i.getAuctionId());
|
|
||||||
o.put("precision", i.getPrecision());
|
|
||||||
o.put("revenue", i.getRevenue());
|
|
||||||
return o;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static JSObject error(int code, String message) {
|
private static JSObject error(int code, String message) {
|
||||||
JSObject e = new JSObject();
|
return Mappers.error(code, message);
|
||||||
e.put("code", code);
|
|
||||||
e.put("message", message);
|
|
||||||
return e;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static JSObject reward(LevelPlayReward r) {
|
private static JSObject reward(LevelPlayReward r) {
|
||||||
JSObject o = new JSObject();
|
return Mappers.reward(r);
|
||||||
o.put("name", r.getName());
|
}
|
||||||
o.put("amount", r.getAmount());
|
|
||||||
return o;
|
static void reject(PluginCall call, CallError e) {
|
||||||
|
call.reject(e.getMessage(), e.code);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------------------- impression data
|
||||||
|
|
||||||
|
/** Main thread. Sends directly when a JS listener exists, otherwise queues (capped). */
|
||||||
|
private void emitImpression(JSObject o) {
|
||||||
|
if (hasListeners(EVENT_IMPRESSION)) {
|
||||||
|
notifyListeners(EVENT_IMPRESSION, o);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
impressionQueue.addLast(o);
|
||||||
|
while (impressionQueue.size() > IMPRESSION_QUEUE_CAP) impressionQueue.removeFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void flushImpressions() {
|
||||||
|
while (!impressionQueue.isEmpty() && hasListeners(EVENT_IMPRESSION)) {
|
||||||
|
notifyListeners(EVENT_IMPRESSION, impressionQueue.removeFirst());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Same as Plugin.addListener, then flushes impressions queued before the first impressionData listener. */
|
||||||
|
@Override
|
||||||
|
@PluginMethod(returnType = PluginMethod.RETURN_NONE)
|
||||||
|
public void addListener(PluginCall call) {
|
||||||
|
super.addListener(call);
|
||||||
|
if (EVENT_IMPRESSION.equals(call.getString("eventName"))) main.post(this::flushImpressions);
|
||||||
}
|
}
|
||||||
|
|
||||||
private JSObject event(String format, String adUnitId, @Nullable LevelPlayAdInfo adInfo) {
|
private JSObject event(String format, String adUnitId, @Nullable LevelPlayAdInfo adInfo) {
|
||||||
|
|
@ -351,6 +495,7 @@ public class LevelPlayPlugin extends Plugin {
|
||||||
this.adUnitId = adUnitId;
|
this.adUnitId = adUnitId;
|
||||||
this.ad = new LevelPlayRewardedAd(adUnitId);
|
this.ad = new LevelPlayRewardedAd(adUnitId);
|
||||||
this.ad.setListener(this);
|
this.ad.setListener(this);
|
||||||
|
this.ad.setImpressionDataListener(impressionListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
void begin(PluginCall c) {
|
void begin(PluginCall c) {
|
||||||
|
|
@ -436,7 +581,9 @@ public class LevelPlayPlugin extends Plugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAdInfoChanged(@NonNull LevelPlayAdInfo adInfo) {}
|
public void onAdInfoChanged(@NonNull LevelPlayAdInfo adInfo) {
|
||||||
|
main.post(() -> notifyListeners("adInfoChanged", event("rewarded", adUnitId, adInfo)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private final class InterstitialHolder implements LevelPlayInterstitialAdListener {
|
private final class InterstitialHolder implements LevelPlayInterstitialAdListener {
|
||||||
|
|
@ -451,6 +598,7 @@ public class LevelPlayPlugin extends Plugin {
|
||||||
this.adUnitId = adUnitId;
|
this.adUnitId = adUnitId;
|
||||||
this.ad = new LevelPlayInterstitialAd(adUnitId);
|
this.ad = new LevelPlayInterstitialAd(adUnitId);
|
||||||
this.ad.setListener(this);
|
this.ad.setListener(this);
|
||||||
|
this.ad.setImpressionDataListener(impressionListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
void begin(PluginCall c) {
|
void begin(PluginCall c) {
|
||||||
|
|
@ -520,6 +668,8 @@ public class LevelPlayPlugin extends Plugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAdInfoChanged(@NonNull LevelPlayAdInfo adInfo) {}
|
public void onAdInfoChanged(@NonNull LevelPlayAdInfo adInfo) {
|
||||||
|
main.post(() -> notifyListeners("adInfoChanged", event("interstitial", adUnitId, adInfo)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
117
android/src/main/java/fyi/crt/capacitor/levelplay/Mappers.java
Normal file
117
android/src/main/java/fyi/crt/capacitor/levelplay/Mappers.java
Normal file
|
|
@ -0,0 +1,117 @@
|
||||||
|
package fyi.crt.capacitor.levelplay;
|
||||||
|
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
import com.getcapacitor.JSObject;
|
||||||
|
import com.ironsource.mediationsdk.adunit.adapter.utility.AdInfo;
|
||||||
|
import com.unity3d.mediation.LevelPlayAdInfo;
|
||||||
|
import com.unity3d.mediation.LevelPlayAdSize;
|
||||||
|
import com.unity3d.mediation.impression.LevelPlayImpressionData;
|
||||||
|
import com.unity3d.mediation.rewarded.LevelPlayReward;
|
||||||
|
|
||||||
|
/** SDK objects to the JSON shapes of src/definitions.ts. Null SDK values are omitted (JSONObject.put(k, null) removes k). */
|
||||||
|
final class Mappers {
|
||||||
|
|
||||||
|
private Mappers() {}
|
||||||
|
|
||||||
|
static JSObject adInfo(@Nullable LevelPlayAdInfo i) {
|
||||||
|
JSObject o = new JSObject();
|
||||||
|
if (i == null) return o;
|
||||||
|
o.put("adId", i.getAdId());
|
||||||
|
o.put("adUnitId", i.getAdUnitId());
|
||||||
|
o.put("adUnitName", i.getAdUnitName());
|
||||||
|
o.put("adFormat", i.getAdFormat());
|
||||||
|
o.put("placementName", i.getPlacementName());
|
||||||
|
o.put("adNetwork", i.getAdNetwork());
|
||||||
|
o.put("instanceName", i.getInstanceName());
|
||||||
|
o.put("instanceId", i.getInstanceId());
|
||||||
|
o.put("country", i.getCountry());
|
||||||
|
o.put("auctionId", i.getAuctionId());
|
||||||
|
o.put("precision", i.getPrecision());
|
||||||
|
o.put("revenue", i.getRevenue());
|
||||||
|
o.put("ab", i.getAb());
|
||||||
|
o.put("segmentName", i.getSegmentName());
|
||||||
|
o.put("encryptedCPM", i.getEncryptedCPM());
|
||||||
|
o.put("creativeId", i.getCreativeId());
|
||||||
|
LevelPlayAdSize size = i.getAdSize();
|
||||||
|
if (size != null) o.put("adSize", adSize(size));
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
static JSObject adSize(LevelPlayAdSize s) {
|
||||||
|
JSObject o = new JSObject();
|
||||||
|
o.put("width", s.getWidth());
|
||||||
|
o.put("height", s.getHeight());
|
||||||
|
o.put("description", s.getDescription());
|
||||||
|
o.put("isAdaptive", s.isAdaptive());
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
static JSObject impression(LevelPlayImpressionData d) {
|
||||||
|
JSObject o = new JSObject();
|
||||||
|
o.put("adFormat", d.getAdFormat());
|
||||||
|
o.put("auctionId", d.getAuctionId());
|
||||||
|
o.put("adUnitId", d.getMediationAdUnitId());
|
||||||
|
o.put("adUnitName", d.getMediationAdUnitName());
|
||||||
|
o.put("adNetwork", d.getAdNetwork());
|
||||||
|
o.put("instanceName", d.getInstanceName());
|
||||||
|
o.put("instanceId", d.getInstanceId());
|
||||||
|
o.put("country", d.getCountry());
|
||||||
|
o.put("placement", d.getPlacement());
|
||||||
|
Double revenue = d.getRevenue();
|
||||||
|
if (revenue != null) o.put("revenue", revenue.doubleValue());
|
||||||
|
o.put("precision", d.getPrecision());
|
||||||
|
o.put("ab", d.getAb());
|
||||||
|
o.put("segmentName", d.getSegmentName());
|
||||||
|
o.put("encryptedCPM", d.getEncryptedCPM());
|
||||||
|
o.put("creativeId", d.getCreativeId());
|
||||||
|
if (d.getAllData() != null) o.put("allData", d.getAllData());
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Native ads use the older ironSource AdInfo (no ad unit, ad id or placement). */
|
||||||
|
static JSObject nativeAdInfo(@Nullable AdInfo i) {
|
||||||
|
JSObject o = new JSObject();
|
||||||
|
if (i == null) return o;
|
||||||
|
o.put("auctionId", i.getAuctionId());
|
||||||
|
o.put("adNetwork", i.getAdNetwork());
|
||||||
|
o.put("instanceName", i.getInstanceName());
|
||||||
|
o.put("instanceId", i.getInstanceId());
|
||||||
|
o.put("country", i.getCountry());
|
||||||
|
o.put("revenue", i.getRevenue());
|
||||||
|
o.put("precision", i.getPrecision());
|
||||||
|
o.put("ab", i.getAb());
|
||||||
|
o.put("segmentName", i.getSegmentName());
|
||||||
|
o.put("encryptedCPM", i.getEncryptedCPM());
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** impressionData synthesised from a native ad impression (no per-ad impression listener for native ads in 9.6.1). */
|
||||||
|
static JSObject nativeImpression(@Nullable AdInfo i) {
|
||||||
|
JSObject o = nativeAdInfo(i);
|
||||||
|
o.put("adFormat", "nativead");
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
static JSObject error(int code, @Nullable String message) {
|
||||||
|
JSObject e = new JSObject();
|
||||||
|
e.put("code", code);
|
||||||
|
e.put("message", message != null ? message : "");
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
static JSObject reward(@Nullable LevelPlayReward r) {
|
||||||
|
JSObject o = new JSObject();
|
||||||
|
o.put("name", r != null && r.getName() != null ? r.getName() : "");
|
||||||
|
o.put("amount", r != null ? r.getAmount() : 0);
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
static JSObject rect(double[] r) {
|
||||||
|
JSObject o = new JSObject();
|
||||||
|
o.put("x", r[0]);
|
||||||
|
o.put("y", r[1]);
|
||||||
|
o.put("width", r[2]);
|
||||||
|
o.put("height", r[3]);
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,120 @@
|
||||||
|
package fyi.crt.capacitor.levelplay;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pure helpers (no Android, no SDK types) for overlay geometry and argument validation, unit tested on the JVM.
|
||||||
|
* Rects are {@code double[]{x, y, width, height}}.
|
||||||
|
*/
|
||||||
|
final class OverlayMath {
|
||||||
|
|
||||||
|
/** LevelPlaySegment.setCustom limits read from the 9.6.1 bytecode: 1-32 chars, letters and digits, at most 5. */
|
||||||
|
static final int MAX_CUSTOM_PARAMS = 5;
|
||||||
|
static final int MAX_CUSTOM_LENGTH = 32;
|
||||||
|
private static final Pattern ALNUM = Pattern.compile("^[a-zA-Z0-9]+$");
|
||||||
|
private static final Pattern HEX_COLOR = Pattern.compile("^#([0-9a-fA-F]{6}|[0-9a-fA-F]{8})$");
|
||||||
|
|
||||||
|
private OverlayMath() {}
|
||||||
|
|
||||||
|
static double[] rect(double x, double y, double w, double h) {
|
||||||
|
return new double[] { x, y, w, h };
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Intersection of two rects; an empty intersection has zero width / height at the clamped origin. */
|
||||||
|
static double[] intersect(double[] a, double[] b) {
|
||||||
|
double l = Math.max(a[0], b[0]);
|
||||||
|
double t = Math.max(a[1], b[1]);
|
||||||
|
double r = Math.min(a[0] + a[2], b[0] + b[2]);
|
||||||
|
double btm = Math.min(a[1] + a[3], b[1] + b[3]);
|
||||||
|
return rect(l, t, Math.max(0, r - l), Math.max(0, btm - t));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Safe area of the WebView in WebView-local CSS px: the WebView's on-screen rect (window px) intersected with the
|
||||||
|
* window minus the system insets (window px), shifted to the WebView origin and divided by the density.
|
||||||
|
*/
|
||||||
|
static double[] safeAreaCss(
|
||||||
|
int wvLeft,
|
||||||
|
int wvTop,
|
||||||
|
int wvWidth,
|
||||||
|
int wvHeight,
|
||||||
|
int windowWidth,
|
||||||
|
int windowHeight,
|
||||||
|
int insetLeft,
|
||||||
|
int insetTop,
|
||||||
|
int insetRight,
|
||||||
|
int insetBottom,
|
||||||
|
float density
|
||||||
|
) {
|
||||||
|
double[] web = rect(wvLeft, wvTop, wvWidth, wvHeight);
|
||||||
|
double[] safe = rect(insetLeft, insetTop, windowWidth - insetLeft - insetRight, windowHeight - insetTop - insetBottom);
|
||||||
|
double[] s = intersect(web, safe);
|
||||||
|
return rect((s[0] - wvLeft) / density, (s[1] - wvTop) / density, s[2] / density, s[3] / density);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Banner frame inside {@code area} (design §6.4): centred horizontally, anchored top / bottom / center. */
|
||||||
|
static double[] bannerFrame(double[] area, double width, double height, String position, double offsetX, double offsetY) {
|
||||||
|
double x = area[0] + (area[2] - width) / 2 + offsetX;
|
||||||
|
double y;
|
||||||
|
switch (position) {
|
||||||
|
case "top":
|
||||||
|
y = area[1] + offsetY;
|
||||||
|
break;
|
||||||
|
case "center":
|
||||||
|
y = area[1] + (area[3] - height) / 2 + offsetY;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
y = area[1] + area[3] - height + offsetY;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return rect(x, y, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
static boolean isPosition(String p) {
|
||||||
|
return "top".equals(p) || "bottom".equals(p) || "center".equals(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
static boolean isBannerSize(String s) {
|
||||||
|
switch (s) {
|
||||||
|
case "BANNER":
|
||||||
|
case "LARGE":
|
||||||
|
case "MEDIUM_RECTANGLE":
|
||||||
|
case "LEADERBOARD":
|
||||||
|
case "ADAPTIVE":
|
||||||
|
case "CUSTOM":
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Error message for segment custom parameters that break the SDK limits, or null when they are fine. */
|
||||||
|
static String customParamsError(Map<String, ?> params) {
|
||||||
|
if (params.size() > MAX_CUSTOM_PARAMS) {
|
||||||
|
return "customParameters: at most " + MAX_CUSTOM_PARAMS + " pairs (got " + params.size() + ")";
|
||||||
|
}
|
||||||
|
for (Map.Entry<String, ?> e : params.entrySet()) {
|
||||||
|
String k = e.getKey();
|
||||||
|
if (!validToken(k)) return "customParameters: key '" + k + "' must be 1-32 letters or digits";
|
||||||
|
Object v = e.getValue();
|
||||||
|
if (!(v instanceof String)) return "customParameters: value of '" + k + "' must be a string";
|
||||||
|
if (!validToken((String) v)) return "customParameters: value of '" + k + "' must be 1-32 letters or digits";
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean validToken(String s) {
|
||||||
|
return s != null && s.length() >= 1 && s.length() <= MAX_CUSTOM_LENGTH && ALNUM.matcher(s).matches();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** CSS {@code #RRGGBB} / {@code #RRGGBBAA} to Android ARGB, or null when malformed. */
|
||||||
|
static Integer parseColor(String css) {
|
||||||
|
if (css == null || !HEX_COLOR.matcher(css).matches()) return null;
|
||||||
|
long v = Long.parseLong(css.substring(1), 16);
|
||||||
|
if (css.length() == 7) return (int) (0xFF000000L | v);
|
||||||
|
long rgb = v >>> 8;
|
||||||
|
long a = v & 0xFF;
|
||||||
|
return (int) ((a << 24) | rgb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,102 @@
|
||||||
|
package fyi.crt.capacitor.levelplay;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class OverlayMathTest {
|
||||||
|
|
||||||
|
private static final double EPS = 1e-9;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void intersectOverlapsAndEmpty() {
|
||||||
|
assertArrayEquals(new double[] { 10, 20, 90, 80 }, OverlayMath.intersect(OverlayMath.rect(0, 0, 100, 100), OverlayMath.rect(10, 20, 200, 200)), EPS);
|
||||||
|
double[] none = OverlayMath.intersect(OverlayMath.rect(0, 0, 10, 10), OverlayMath.rect(20, 20, 5, 5));
|
||||||
|
assertEquals(0, none[2], EPS);
|
||||||
|
assertEquals(0, none[3], EPS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void safeAreaEdgeToEdgeWebView() {
|
||||||
|
// 1080 x 2400 window at density 2.625; WebView covers the window; status bar 63 px, nav bar 126 px.
|
||||||
|
double[] s = OverlayMath.safeAreaCss(0, 0, 1080, 2400, 1080, 2400, 0, 63, 0, 126, 2.625f);
|
||||||
|
assertArrayEquals(new double[] { 0, 24, 1080 / 2.625, (2400 - 63 - 126) / 2.625 }, s, EPS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void safeAreaWebViewAlreadyInset() {
|
||||||
|
// Capacitor native insets handling: the WebView itself sits between the bars, so the safe area is all of it.
|
||||||
|
double[] s = OverlayMath.safeAreaCss(0, 63, 1080, 2211, 1080, 2400, 0, 63, 0, 126, 2.625f);
|
||||||
|
assertArrayEquals(new double[] { 0, 0, 1080 / 2.625, 2211 / 2.625 }, s, EPS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void safeAreaLandscapeCutout() {
|
||||||
|
// Landscape with a 100 px cutout on the left and a 120 px nav bar on the right; density 2.
|
||||||
|
double[] s = OverlayMath.safeAreaCss(0, 0, 2400, 1080, 2400, 1080, 100, 50, 120, 0, 2f);
|
||||||
|
assertArrayEquals(new double[] { 50, 25, (2400 - 220) / 2.0, (1080 - 50) / 2.0 }, s, EPS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void bannerFramePositions() {
|
||||||
|
double[] area = OverlayMath.rect(0, 24, 411, 800);
|
||||||
|
assertArrayEquals(new double[] { 45.5, 24, 320, 50 }, OverlayMath.bannerFrame(area, 320, 50, "top", 0, 0), EPS);
|
||||||
|
assertArrayEquals(new double[] { 45.5, 774, 320, 50 }, OverlayMath.bannerFrame(area, 320, 50, "bottom", 0, 0), EPS);
|
||||||
|
assertArrayEquals(new double[] { 45.5, 399, 320, 50 }, OverlayMath.bannerFrame(area, 320, 50, "center", 0, 0), EPS);
|
||||||
|
assertArrayEquals(new double[] { 55.5, 764, 320, 50 }, OverlayMath.bannerFrame(area, 320, 50, "bottom", 10, -10), EPS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void positionsAndSizes() {
|
||||||
|
assertTrue(OverlayMath.isPosition("top"));
|
||||||
|
assertFalse(OverlayMath.isPosition("left"));
|
||||||
|
assertTrue(OverlayMath.isBannerSize("CUSTOM"));
|
||||||
|
assertTrue(OverlayMath.isBannerSize("ADAPTIVE"));
|
||||||
|
assertFalse(OverlayMath.isBannerSize("SMART"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void customParams() {
|
||||||
|
Map<String, Object> ok = new LinkedHashMap<>();
|
||||||
|
ok.put("tier", "gold");
|
||||||
|
ok.put("ab1", "B");
|
||||||
|
assertNull(OverlayMath.customParamsError(ok));
|
||||||
|
|
||||||
|
Map<String, Object> badChar = new LinkedHashMap<>();
|
||||||
|
badChar.put("my-key", "x");
|
||||||
|
assertNotNull(OverlayMath.customParamsError(badChar));
|
||||||
|
|
||||||
|
Map<String, Object> empty = new LinkedHashMap<>();
|
||||||
|
empty.put("k", "");
|
||||||
|
assertNotNull(OverlayMath.customParamsError(empty));
|
||||||
|
|
||||||
|
Map<String, Object> tooLong = new LinkedHashMap<>();
|
||||||
|
tooLong.put("k", "123456789012345678901234567890123");
|
||||||
|
assertNotNull(OverlayMath.customParamsError(tooLong));
|
||||||
|
|
||||||
|
Map<String, Object> notString = new LinkedHashMap<>();
|
||||||
|
notString.put("k", 5);
|
||||||
|
assertNotNull(OverlayMath.customParamsError(notString));
|
||||||
|
|
||||||
|
Map<String, Object> six = new LinkedHashMap<>();
|
||||||
|
for (int i = 0; i < 6; i++) six.put("k" + i, "v");
|
||||||
|
assertNotNull(OverlayMath.customParamsError(six));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void colors() {
|
||||||
|
assertEquals(Integer.valueOf(0xFF112233), OverlayMath.parseColor("#112233"));
|
||||||
|
assertEquals(Integer.valueOf(0x80112233), OverlayMath.parseColor("#11223380"));
|
||||||
|
assertNull(OverlayMath.parseColor("112233"));
|
||||||
|
assertNull(OverlayMath.parseColor("#12345"));
|
||||||
|
assertNull(OverlayMath.parseColor("red"));
|
||||||
|
assertNull(OverlayMath.parseColor(null));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue