package com.unityuqn.ads;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.PropertyCategory;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.annotations.UsesActivities;
import com.google.appinventor.components.annotations.UsesLibraries;
import com.google.appinventor.components.annotations.UsesPermissions;
import com.google.appinventor.components.annotations.androidmanifest.ActivityElement;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.Component;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.runtime.HVArrangement;
import com.unity3d.ads.IUnityAdsListener;
import com.unity3d.ads.UnityAds;
import com.unity3d.services.banners.UnityBanners;
import com.unity3d.services.banners.IUnityBannerListener;
@DesignerComponent(category = ComponentCategory.EXTENSION, description = "This extension was created with love by Anmol Developer and Assisted by Salman_Dev", iconName = "https://img.icons8.com/ios-filled/16/000000/unity.png", nonVisible = true, version = 1)
@UsesLibraries(libraries = "unity-ads-3.6.0.jar")
@SimpleObject(external = true)
@UsesActivities(activities = {@ActivityElement(configChanges = "fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen", hardwareAccelerated = "true", name = "com.unity3d.services.ads.adunit.AdUnitActivity", theme = "@android:style/Theme.NoTitleBar.Fullscreen"), @ActivityElement(configChanges = "fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen", hardwareAccelerated = "true", name = "com.unity3d.services.ads.adunit.AdUnitTransparentActivity", theme = "@android:style/Theme.Translucent.NoTitleBar.Fullscreen"), @ActivityElement(configChanges = "fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen", hardwareAccelerated = "true", name = "com.unity3d.services.ads.adunit.AdUnitTransparentSoftwareActivity", theme = "@android:style/Theme.Translucent.NoTitleBar.Fullscreen"), @ActivityElement(configChanges = "fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen", hardwareAccelerated = "true", name = "com.unity3d.services.ads.adunit.AdUnitSoftwareActivity", theme = "@android:style/Theme.NoTitleBar.Fullscreen")})
@UsesPermissions(permissionNames = "android.permission.INTERNET,android.permission.ACCESS_NETWORK_STATE")
public class UnityAdsUtils extends AndroidNonvisibleComponent implements Component {
HVArrangement Harrangment;
private final Activity activity;
private ComponentContainer container;
private Context context;
public int flag = 1;
final UnityAdsListener myAdsListener = new UnityAdsListener();
final IUnityBannerListener myBannerListener = new UnityBannerListener();
private boolean testMode = false;
private View view;
ViewGroup viewGroup;
public UnityAdsUtils(ComponentContainer container) {
super(container.$form());
this.container = container;
this.context = container.$context();
this.activity = container.$context();
}
@SimpleProperty(category = PropertyCategory.BEHAVIOR, description = "")
public boolean TestMode() {
return this.testMode;
}
@DesignerProperty(defaultValue = "false", editorType = "boolean")
@SimpleProperty
public void TestMode(boolean testMode2) {
this.testMode = testMode2;
}
@SimpleFunction(description = "")
public void Initialize(String gameId) {
UnityBanners.setBannerListener(this.myBannerListener);
UnityAds.initialize(this.activity, gameId, (IUnityAdsListener) this.myAdsListener, this.testMode);
}
@SimpleFunction(description = "")
public void LoadAdBanner(HVArrangement horizontalArrangement, String placementId) {
this.Harrangment = horizontalArrangement;
UnityBanners.loadBanner(this.activity, placementId);
}
@SimpleFunction(description = "")
public void ShowInterstitial(String placementId) {
UnityAds.show(this.activity, placementId);
}
@SimpleFunction(description = "")
public void ShowRewarded(String placementId) {
UnityAds.show(this.activity, placementId);
}
@SimpleFunction(description = "")
public boolean IsAdReady(String placementId) {
if (UnityAds.isReady(placementId)) {
return true;
}
return false;
}
private class UnityBannerListener implements IUnityBannerListener {
private UnityBannerListener() {
}
public void onUnityBannerLoaded(String s, View view) {
UnityAdsUtils.this.OnBannerAdLoaded();
ViewGroup fm = (ViewGroup) UnityAdsUtils.this.Harrangment.getView();
fm.removeView(view);
fm.addView(view);
}
public void onUnityBannerUnloaded(String s) {
UnityAdsUtils.this.OnBannerAdUnLoaded();
}
public void onUnityBannerShow(String s) {
UnityAdsUtils.this.OnBannerAdShow();
}
public void onUnityBannerClick(String s) {
UnityAdsUtils.this.OnBannerAdClicked();
}
public void onUnityBannerHide(String s) {
UnityAdsUtils.this.OnBannerAdHide();
}
public void onUnityBannerError(String s) {
UnityAdsUtils.this.OnBannerAdError(s);
}
}
@SimpleEvent(description = "")
public void OnBannerAdLoaded() {
EventDispatcher.dispatchEvent(this, "OnBannerAdLoaded");
}
@SimpleEvent(description = "")
public void OnBannerAdUnLoaded() {
EventDispatcher.dispatchEvent(this, "OnBannerAdUnLoaded");
}
@SimpleEvent(description = "")
public void OnBannerAdShow() {
EventDispatcher.dispatchEvent(this, "OnBannerAdShow");
}
@SimpleEvent(description = "")
public void OnBannerAdClicked() {
EventDispatcher.dispatchEvent(this, "OnBannerAdClicked");
}
@SimpleEvent(description = "")
public void OnBannerAdError(String message) {
EventDispatcher.dispatchEvent(this, "OnBannerAdError", message);
}
@SimpleEvent(description = "")
public void OnBannerAdHide() {
EventDispatcher.dispatchEvent(this, "OnBannerAdHide");
}
@SimpleEvent(description = "")
public void OnInitialize() {
EventDispatcher.dispatchEvent(this, "OnInitialize");
}
private class UnityAdsListener implements IUnityAdsListener {
private UnityAdsListener() {
}
public void onUnityAdsReady(String placementId) {
if (UnityAdsUtils.this.flag == 1) {
int unused = UnityAdsUtils.this.flag = 0;
UnityAdsUtils.this.OnInitialize();
}
UnityAdsUtils.this.OnUnityAdReady(placementId);
}
public void onUnityAdsStart(String placementId) {
UnityAdsUtils.this.OnUnityAdStarted(placementId);
}
public void onUnityAdsFinish(String placementId, UnityAds.FinishState finishState) {
UnityAdsUtils.this.OnUnityAdFinished(placementId);
if (finishState.equals(UnityAds.FinishState.COMPLETED)) {
UnityAdsUtils.this.OnUnityAdRewarded(placementId);
} else if (finishState.equals(UnityAds.FinishState.SKIPPED)) {
UnityAdsUtils.this.OnUnityAdSkipped(placementId);
} else if (finishState.equals(UnityAds.FinishState.ERROR)) {
UnityAdsUtils.this.OnUnityAdError(placementId);
}
}
public void onUnityAdsError(UnityAds.UnityAdsError error, String message) {
UnityAdsUtils.this.OnUnityAdError(message);
}
}
@SimpleEvent(description = "")
public void OnUnityAdReady(String placementId) {
EventDispatcher.dispatchEvent(this, "OnUnityAdReady", placementId);
}
@SimpleEvent(description = "")
public void OnUnityAdStarted(String placementId) {
EventDispatcher.dispatchEvent(this, "OnUnityAdStarted", placementId);
}
@SimpleEvent(description = "")
public void OnUnityAdFinished(String placementId) {
EventDispatcher.dispatchEvent(this, "OnUnityAdFinished", placementId);
}
@SimpleEvent(description = "")
public void OnUnityAdRewarded(String placementId) {
EventDispatcher.dispatchEvent(this, "OnUnityAdRewarded", placementId);
}
@SimpleEvent(description = "")
public void OnUnityAdSkipped(String placementId) {
EventDispatcher.dispatchEvent(this, "OnUnityAdSkipped", placementId);
}
@SimpleEvent(description = "")
public void OnUnityVideoAdError(String placementId) {
EventDispatcher.dispatchEvent(this, "OnUnityVideoAdError", placementId);
}
@SimpleEvent(description = "")
public void OnUnityAdError(String message) {
EventDispatcher.dispatchEvent(this, "OnUnityAdError", message);
}
}