diff --git a/android/build.gradle b/android/build.gradle new file mode 100644 index 0000000..16083da --- /dev/null +++ b/android/build.gradle @@ -0,0 +1,66 @@ +ext { + junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2' + androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.1' + // Unity LevelPlay (Maven Central: com.unity3d.ads-mediation:mediation-sdk). Override from the app's variables.gradle + // with `levelPlayVersion = '9.x.y'` to pin a different SDK. + levelPlayVersion = project.hasProperty('levelPlayVersion') ? rootProject.ext.levelPlayVersion : '9.6.1' + playServicesAdsIdentifierVersion = project.hasProperty('playServicesAdsIdentifierVersion') ? rootProject.ext.playServicesAdsIdentifierVersion : '18.1.0' + playServicesAppSetVersion = project.hasProperty('playServicesAppSetVersion') ? rootProject.ext.playServicesAppSetVersion : '16.0.0' + playServicesBasementVersion = project.hasProperty('playServicesBasementVersion') ? rootProject.ext.playServicesBasementVersion : '18.1.0' +} + +buildscript { + repositories { + google() + mavenCentral() + } + dependencies { + classpath 'com.android.tools.build:gradle:8.13.0' + } +} + +apply plugin: 'com.android.library' + +android { + namespace = "com.voxgames.capacitor.levelplay" + compileSdk = project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 36 + defaultConfig { + minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 24 + targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 36 + versionCode 1 + versionName "1.0" + consumerProguardFiles 'proguard-rules.pro' + } + buildTypes { + release { + minifyEnabled false + } + } + lintOptions { + abortOnError = false + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_21 + targetCompatibility JavaVersion.VERSION_21 + } +} + +repositories { + google() + mavenCentral() +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation project(':capacitor-android') + implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion" + + // LevelPlay SDK (ironSource Ads network built in; add mediation adapters in the app module). + implementation "com.unity3d.ads-mediation:mediation-sdk:$levelPlayVersion" + // GAID + App Set ID, as required by the LevelPlay Android integration guide. + implementation "com.google.android.gms:play-services-ads-identifier:$playServicesAdsIdentifierVersion" + implementation "com.google.android.gms:play-services-appset:$playServicesAppSetVersion" + implementation "com.google.android.gms:play-services-basement:$playServicesBasementVersion" + + testImplementation "junit:junit:$junitVersion" +} diff --git a/android/proguard-rules.pro b/android/proguard-rules.pro new file mode 100644 index 0000000..cdd2f94 --- /dev/null +++ b/android/proguard-rules.pro @@ -0,0 +1,15 @@ +# Consumer rules for apps that minify. From the LevelPlay Android integration guide (core SDK part); add the per-network +# rules from https://docs.unity.com/en-us/grow/levelplay/sdk/android/sdk-integration for each mediation adapter you use. +-keepclassmembers class * implements android.os.Parcelable { + public static final android.os.Parcelable$Creator *; +} +-keep class com.google.android.gms.appset.** { *; } +-keep class com.google.android.gms.tasks.** { *; } +-keep class com.ironsource.adapters.** { *; } +-dontwarn com.ironsource.** +-keepclassmembers class com.ironsource.** { public *; } +-keep public class com.ironsource.** +-dontwarn com.iab.omid.** +-keep class com.iab.omid.** {*;} +-keepattributes JavascriptInterface +-keep class com.voxgames.capacitor.levelplay.** { *; } diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml new file mode 100644 index 0000000..7cb2ee0 --- /dev/null +++ b/android/src/main/AndroidManifest.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/android/src/main/java/com/voxgames/capacitor/levelplay/LevelPlayPlugin.java b/android/src/main/java/com/voxgames/capacitor/levelplay/LevelPlayPlugin.java new file mode 100644 index 0000000..ba47509 --- /dev/null +++ b/android/src/main/java/com/voxgames/capacitor/levelplay/LevelPlayPlugin.java @@ -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 pendingInits = new ArrayList<>(); + private final Map rewarded = new HashMap<>(); + private final Map 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 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 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) {} + } +}