Android: LevelPlay ad-unit bridge (mediation-sdk 9.6.1), consent, consumer ProGuard rules
This commit is contained in:
parent
28886bf2c8
commit
1cb01e024e
4 changed files with 611 additions and 0 deletions
5
android/src/main/AndroidManifest.xml
Normal file
5
android/src/main/AndroidManifest.xml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<!-- INTERNET / ACCESS_NETWORK_STATE and the SDK activities + providers come from the LevelPlay AAR manifest. -->
|
||||
<!-- Advertising ID (Android 13+ / API 33). Child-directed apps can strip it with tools:node="remove". -->
|
||||
<uses-permission android:name="com.google.android.gms.permission.AD_ID" />
|
||||
</manifest>
|
||||
|
|
@ -0,0 +1,525 @@
|
|||
package com.voxgames.capacitor.levelplay;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import com.getcapacitor.JSObject;
|
||||
import com.getcapacitor.Plugin;
|
||||
import com.getcapacitor.PluginCall;
|
||||
import com.getcapacitor.PluginMethod;
|
||||
import com.getcapacitor.annotation.CapacitorPlugin;
|
||||
import com.unity3d.mediation.LevelPlay;
|
||||
import com.unity3d.mediation.LevelPlayAdError;
|
||||
import com.unity3d.mediation.LevelPlayAdInfo;
|
||||
import com.unity3d.mediation.LevelPlayConfiguration;
|
||||
import com.unity3d.mediation.LevelPlayInitError;
|
||||
import com.unity3d.mediation.LevelPlayInitListener;
|
||||
import com.unity3d.mediation.LevelPlayInitRequest;
|
||||
import com.unity3d.mediation.LevelPlayPrivacySettings;
|
||||
import com.unity3d.mediation.interstitial.LevelPlayInterstitialAd;
|
||||
import com.unity3d.mediation.interstitial.LevelPlayInterstitialAdListener;
|
||||
import com.unity3d.mediation.rewarded.LevelPlayReward;
|
||||
import com.unity3d.mediation.rewarded.LevelPlayRewardedAd;
|
||||
import com.unity3d.mediation.rewarded.LevelPlayRewardedAdListener;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
@CapacitorPlugin(name = "LevelPlay")
|
||||
public class LevelPlayPlugin extends Plugin {
|
||||
|
||||
private static final long LATE_REWARD_GRACE_MS = 1000;
|
||||
|
||||
private enum InitState {
|
||||
IDLE,
|
||||
RUNNING,
|
||||
DONE
|
||||
}
|
||||
|
||||
private final Handler main = new Handler(Looper.getMainLooper());
|
||||
private InitState initState = InitState.IDLE;
|
||||
private final List<PluginCall> pendingInits = new ArrayList<>();
|
||||
private final Map<String, RewardedHolder> rewarded = new HashMap<>();
|
||||
private final Map<String, InterstitialHolder> interstitials = new HashMap<>();
|
||||
|
||||
// ---------------------------------------------------------------------------------------------- init / settings
|
||||
|
||||
@PluginMethod
|
||||
public void initialize(PluginCall call) {
|
||||
String appKey = call.getString("appKey");
|
||||
if (appKey == null || appKey.isEmpty()) {
|
||||
call.reject("appKey is required", "INVALID_ARGUMENT");
|
||||
return;
|
||||
}
|
||||
main.post(() -> {
|
||||
if (initState == InitState.DONE) {
|
||||
call.resolve(version());
|
||||
return;
|
||||
}
|
||||
pendingInits.add(call);
|
||||
if (initState == InitState.RUNNING) return;
|
||||
initState = InitState.RUNNING;
|
||||
|
||||
JSObject consent = call.getObject("consent");
|
||||
if (consent != null) applyConsent(consent);
|
||||
if (Boolean.TRUE.equals(call.getBoolean("testSuite", false))) LevelPlay.setMetaData("is_test_suite", "enable");
|
||||
if (Boolean.TRUE.equals(call.getBoolean("adaptersDebug", false))) LevelPlay.setAdaptersDebug(true);
|
||||
|
||||
LevelPlayInitRequest.Builder builder = new LevelPlayInitRequest.Builder(appKey);
|
||||
String userId = call.getString("userId");
|
||||
if (userId != null && !userId.isEmpty()) builder.withUserId(userId);
|
||||
|
||||
LevelPlay.init(
|
||||
getContext().getApplicationContext(),
|
||||
builder.build(),
|
||||
new LevelPlayInitListener() {
|
||||
@Override
|
||||
public void onInitSuccess(@NonNull LevelPlayConfiguration configuration) {
|
||||
main.post(() -> {
|
||||
initState = InitState.DONE;
|
||||
List<PluginCall> calls = new ArrayList<>(pendingInits);
|
||||
pendingInits.clear();
|
||||
for (PluginCall c : calls) c.resolve(version());
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInitFailed(@NonNull LevelPlayInitError error) {
|
||||
main.post(() -> {
|
||||
initState = InitState.IDLE;
|
||||
List<PluginCall> calls = new ArrayList<>(pendingInits);
|
||||
pendingInits.clear();
|
||||
for (PluginCall c : calls) c.reject(error.getErrorMessage(), String.valueOf(error.getErrorCode()));
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
public void getSdkVersion(PluginCall call) {
|
||||
call.resolve(version());
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
public void setConsent(PluginCall call) {
|
||||
JSObject options = call.getData();
|
||||
main.post(() -> {
|
||||
applyConsent(options);
|
||||
call.resolve();
|
||||
});
|
||||
}
|
||||
|
||||
private static void applyConsent(JSObject o) {
|
||||
if (o.has("gdprConsent")) LevelPlayPrivacySettings.setGDPRConsent(o.optBoolean("gdprConsent"));
|
||||
if (o.has("ccpaDoNotSell")) LevelPlayPrivacySettings.setCCPA(o.optBoolean("ccpaDoNotSell"));
|
||||
if (o.has("coppa")) LevelPlayPrivacySettings.setCOPPA(o.optBoolean("coppa"));
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
public void setMetaData(PluginCall call) {
|
||||
String key = call.getString("key");
|
||||
String value = call.getString("value");
|
||||
if (key == null || value == null) {
|
||||
call.reject("key and value are required", "INVALID_ARGUMENT");
|
||||
return;
|
||||
}
|
||||
main.post(() -> {
|
||||
LevelPlay.setMetaData(key, value);
|
||||
call.resolve();
|
||||
});
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
public void requestTrackingAuthorization(PluginCall call) {
|
||||
JSObject r = new JSObject();
|
||||
r.put("status", "unavailable");
|
||||
call.resolve(r);
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
public void getTrackingAuthorizationStatus(PluginCall call) {
|
||||
requestTrackingAuthorization(call);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------- rewarded
|
||||
|
||||
@PluginMethod
|
||||
public void loadRewarded(PluginCall call) {
|
||||
String adUnitId = requireAdUnit(call);
|
||||
if (adUnitId == null) return;
|
||||
main.post(() -> {
|
||||
rewardedHolder(adUnitId).ad.loadAd();
|
||||
call.resolve();
|
||||
});
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
public void showRewarded(PluginCall call) {
|
||||
String adUnitId = requireAdUnit(call);
|
||||
if (adUnitId == null) return;
|
||||
String placement = call.getString("placementName");
|
||||
main.post(() -> {
|
||||
RewardedHolder h = rewardedHolder(adUnitId);
|
||||
Activity activity = getActivity();
|
||||
if (activity == null || !h.ad.isAdReady()) {
|
||||
call.resolve(notReady());
|
||||
return;
|
||||
}
|
||||
h.begin(call);
|
||||
if (placement != null && !placement.isEmpty()) h.ad.showAd(activity, placement);
|
||||
else h.ad.showAd(activity);
|
||||
});
|
||||
}
|
||||
|
||||
private RewardedHolder rewardedHolder(String adUnitId) {
|
||||
RewardedHolder h = rewarded.get(adUnitId);
|
||||
if (h == null) {
|
||||
h = new RewardedHolder(adUnitId);
|
||||
rewarded.put(adUnitId, h);
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------- interstitial
|
||||
|
||||
@PluginMethod
|
||||
public void loadInterstitial(PluginCall call) {
|
||||
String adUnitId = requireAdUnit(call);
|
||||
if (adUnitId == null) return;
|
||||
main.post(() -> {
|
||||
interstitialHolder(adUnitId).ad.loadAd();
|
||||
call.resolve();
|
||||
});
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
public void showInterstitial(PluginCall call) {
|
||||
String adUnitId = requireAdUnit(call);
|
||||
if (adUnitId == null) return;
|
||||
String placement = call.getString("placementName");
|
||||
main.post(() -> {
|
||||
InterstitialHolder h = interstitialHolder(adUnitId);
|
||||
Activity activity = getActivity();
|
||||
if (activity == null || !h.ad.isAdReady()) {
|
||||
call.resolve(notReady());
|
||||
return;
|
||||
}
|
||||
h.begin(call);
|
||||
if (placement != null && !placement.isEmpty()) h.ad.showAd(activity, placement);
|
||||
else h.ad.showAd(activity);
|
||||
});
|
||||
}
|
||||
|
||||
private InterstitialHolder interstitialHolder(String adUnitId) {
|
||||
InterstitialHolder h = interstitials.get(adUnitId);
|
||||
if (h == null) {
|
||||
h = new InterstitialHolder(adUnitId);
|
||||
interstitials.put(adUnitId, h);
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------- misc
|
||||
|
||||
@PluginMethod
|
||||
public void isReady(PluginCall call) {
|
||||
String adUnitId = requireAdUnit(call);
|
||||
if (adUnitId == null) return;
|
||||
String format = call.getString("format", "rewarded");
|
||||
main.post(() -> {
|
||||
boolean ready;
|
||||
if ("interstitial".equals(format)) {
|
||||
InterstitialHolder h = interstitials.get(adUnitId);
|
||||
ready = h != null && h.ad.isAdReady();
|
||||
} else {
|
||||
RewardedHolder h = rewarded.get(adUnitId);
|
||||
ready = h != null && h.ad.isAdReady();
|
||||
}
|
||||
JSObject r = new JSObject();
|
||||
r.put("ready", ready);
|
||||
call.resolve(r);
|
||||
});
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
public void launchTestSuite(PluginCall call) {
|
||||
main.post(() -> {
|
||||
Activity activity = getActivity();
|
||||
LevelPlay.launchTestSuite(activity != null ? activity : getContext());
|
||||
call.resolve();
|
||||
});
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
public void validateIntegration(PluginCall call) {
|
||||
main.post(() -> {
|
||||
LevelPlay.validateIntegration(getContext());
|
||||
call.resolve();
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------- helpers
|
||||
|
||||
@Nullable
|
||||
private static String requireAdUnit(PluginCall call) {
|
||||
String adUnitId = call.getString("adUnitId");
|
||||
if (adUnitId == null || adUnitId.isEmpty()) {
|
||||
call.reject("adUnitId is required", "INVALID_ARGUMENT");
|
||||
return null;
|
||||
}
|
||||
return adUnitId;
|
||||
}
|
||||
|
||||
private static JSObject version() {
|
||||
JSObject r = new JSObject();
|
||||
r.put("sdkVersion", LevelPlay.getSdkVersion());
|
||||
return r;
|
||||
}
|
||||
|
||||
private static JSObject notReady() {
|
||||
JSObject r = new JSObject();
|
||||
r.put("shown", false);
|
||||
r.put("rewarded", false);
|
||||
JSObject e = new JSObject();
|
||||
e.put("code", -1);
|
||||
e.put("message", "Ad not ready");
|
||||
r.put("error", e);
|
||||
return r;
|
||||
}
|
||||
|
||||
private static JSObject info(@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());
|
||||
return o;
|
||||
}
|
||||
|
||||
private static JSObject error(int code, String message) {
|
||||
JSObject e = new JSObject();
|
||||
e.put("code", code);
|
||||
e.put("message", message);
|
||||
return e;
|
||||
}
|
||||
|
||||
private static JSObject reward(LevelPlayReward r) {
|
||||
JSObject o = new JSObject();
|
||||
o.put("name", r.getName());
|
||||
o.put("amount", r.getAmount());
|
||||
return o;
|
||||
}
|
||||
|
||||
private JSObject event(String format, String adUnitId, @Nullable LevelPlayAdInfo adInfo) {
|
||||
JSObject o = new JSObject();
|
||||
o.put("format", format);
|
||||
o.put("adUnitId", adUnitId);
|
||||
if (adInfo != null) o.put("adInfo", info(adInfo));
|
||||
return o;
|
||||
}
|
||||
|
||||
/** One LevelPlayRewardedAd per ad unit plus the pending show call. Reward and close may arrive in either order. */
|
||||
private final class RewardedHolder implements LevelPlayRewardedAdListener {
|
||||
|
||||
final LevelPlayRewardedAd ad;
|
||||
private final String adUnitId;
|
||||
private PluginCall call;
|
||||
private boolean shown;
|
||||
private boolean closed;
|
||||
private LevelPlayReward reward;
|
||||
private LevelPlayAdInfo lastInfo;
|
||||
|
||||
RewardedHolder(String adUnitId) {
|
||||
this.adUnitId = adUnitId;
|
||||
this.ad = new LevelPlayRewardedAd(adUnitId);
|
||||
this.ad.setListener(this);
|
||||
}
|
||||
|
||||
void begin(PluginCall c) {
|
||||
call = c;
|
||||
shown = false;
|
||||
closed = false;
|
||||
reward = null;
|
||||
lastInfo = null;
|
||||
}
|
||||
|
||||
private void finish(@Nullable JSObject err) {
|
||||
if (call == null) return;
|
||||
PluginCall c = call;
|
||||
call = null;
|
||||
JSObject r = new JSObject();
|
||||
r.put("shown", shown);
|
||||
r.put("rewarded", reward != null);
|
||||
if (reward != null) r.put("reward", LevelPlayPlugin.reward(reward));
|
||||
if (err != null) r.put("error", err);
|
||||
if (lastInfo != null) r.put("adInfo", info(lastInfo));
|
||||
c.resolve(r);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAdLoaded(@NonNull LevelPlayAdInfo adInfo) {
|
||||
main.post(() -> notifyListeners("loaded", event("rewarded", adUnitId, adInfo)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAdLoadFailed(@NonNull LevelPlayAdError e) {
|
||||
main.post(() -> {
|
||||
JSObject o = event("rewarded", adUnitId, null);
|
||||
o.put("error", error(e.getErrorCode(), e.getErrorMessage()));
|
||||
notifyListeners("loadFailed", o);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAdDisplayed(@NonNull LevelPlayAdInfo adInfo) {
|
||||
main.post(() -> {
|
||||
shown = true;
|
||||
lastInfo = adInfo;
|
||||
notifyListeners("displayed", event("rewarded", adUnitId, adInfo));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAdDisplayFailed(@NonNull LevelPlayAdError e, @NonNull LevelPlayAdInfo adInfo) {
|
||||
main.post(() -> {
|
||||
JSObject err = error(e.getErrorCode(), e.getErrorMessage());
|
||||
JSObject o = event("rewarded", adUnitId, adInfo);
|
||||
o.put("error", err);
|
||||
notifyListeners("displayFailed", o);
|
||||
finish(err);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAdRewarded(@NonNull LevelPlayReward r, @NonNull LevelPlayAdInfo adInfo) {
|
||||
main.post(() -> {
|
||||
reward = r;
|
||||
JSObject o = event("rewarded", adUnitId, adInfo);
|
||||
o.put("reward", LevelPlayPlugin.reward(r));
|
||||
notifyListeners("rewarded", o);
|
||||
if (closed) finish(null);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAdClicked(@NonNull LevelPlayAdInfo adInfo) {
|
||||
main.post(() -> notifyListeners("clicked", event("rewarded", adUnitId, adInfo)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAdClosed(@NonNull LevelPlayAdInfo adInfo) {
|
||||
main.post(() -> {
|
||||
closed = true;
|
||||
lastInfo = adInfo;
|
||||
notifyListeners("closed", event("rewarded", adUnitId, adInfo));
|
||||
if (reward != null) finish(null);
|
||||
else main.postDelayed(() -> finish(null), LATE_REWARD_GRACE_MS);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAdInfoChanged(@NonNull LevelPlayAdInfo adInfo) {}
|
||||
}
|
||||
|
||||
private final class InterstitialHolder implements LevelPlayInterstitialAdListener {
|
||||
|
||||
final LevelPlayInterstitialAd ad;
|
||||
private final String adUnitId;
|
||||
private PluginCall call;
|
||||
private boolean shown;
|
||||
private LevelPlayAdInfo lastInfo;
|
||||
|
||||
InterstitialHolder(String adUnitId) {
|
||||
this.adUnitId = adUnitId;
|
||||
this.ad = new LevelPlayInterstitialAd(adUnitId);
|
||||
this.ad.setListener(this);
|
||||
}
|
||||
|
||||
void begin(PluginCall c) {
|
||||
call = c;
|
||||
shown = false;
|
||||
lastInfo = null;
|
||||
}
|
||||
|
||||
private void finish(@Nullable JSObject err) {
|
||||
if (call == null) return;
|
||||
PluginCall c = call;
|
||||
call = null;
|
||||
JSObject r = new JSObject();
|
||||
r.put("shown", shown);
|
||||
r.put("rewarded", false);
|
||||
if (err != null) r.put("error", err);
|
||||
if (lastInfo != null) r.put("adInfo", info(lastInfo));
|
||||
c.resolve(r);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAdLoaded(@NonNull LevelPlayAdInfo adInfo) {
|
||||
main.post(() -> notifyListeners("loaded", event("interstitial", adUnitId, adInfo)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAdLoadFailed(@NonNull LevelPlayAdError e) {
|
||||
main.post(() -> {
|
||||
JSObject o = event("interstitial", adUnitId, null);
|
||||
o.put("error", error(e.getErrorCode(), e.getErrorMessage()));
|
||||
notifyListeners("loadFailed", o);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAdDisplayed(@NonNull LevelPlayAdInfo adInfo) {
|
||||
main.post(() -> {
|
||||
shown = true;
|
||||
lastInfo = adInfo;
|
||||
notifyListeners("displayed", event("interstitial", adUnitId, adInfo));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAdDisplayFailed(@NonNull LevelPlayAdError e, @NonNull LevelPlayAdInfo adInfo) {
|
||||
main.post(() -> {
|
||||
JSObject err = error(e.getErrorCode(), e.getErrorMessage());
|
||||
JSObject o = event("interstitial", adUnitId, adInfo);
|
||||
o.put("error", err);
|
||||
notifyListeners("displayFailed", o);
|
||||
finish(err);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAdClicked(@NonNull LevelPlayAdInfo adInfo) {
|
||||
main.post(() -> notifyListeners("clicked", event("interstitial", adUnitId, adInfo)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAdClosed(@NonNull LevelPlayAdInfo adInfo) {
|
||||
main.post(() -> {
|
||||
lastInfo = adInfo;
|
||||
notifyListeners("closed", event("interstitial", adUnitId, adInfo));
|
||||
finish(null);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAdInfoChanged(@NonNull LevelPlayAdInfo adInfo) {}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue