Skip to content

Commit

Permalink
Merge pull request #9 from m0skit0/ads
Browse files Browse the repository at this point in the history
Wrapper for Ads kit enhancement
  • Loading branch information
m0skit0 authored Apr 16, 2020
2 parents 1ce5855 + 830c9ea commit 3a117a0
Show file tree
Hide file tree
Showing 28 changed files with 723 additions and 2 deletions.
1 change: 1 addition & 0 deletions android-wrapper/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ dependencies {

// HMS
compileOnly 'com.huawei.hms:iap:4.0.2.300'
compileOnly 'com.huawei.hms:ads-lite:13.4.28.305'
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import android.content.Intent
interface GenericBridgeCallback {
fun onSuccess(data: Intent)
fun onFailure(error: Exception)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.m0skit0.android.hms.unity.ads

interface OnMetadataChangedListener {
fun onMetadataChanged()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.m0skit0.android.hms.unity.ads

class OnMetadataChangedListenerWrapper(private val listener: OnMetadataChangedListener) : com.huawei.hms.ads.reward.OnMetadataChangedListener() {

override fun onMetadataChanged() {
listener.onMetadataChanged()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.m0skit0.android.hms.unity.ads

interface RewardAdLoadListener {
fun onRewardedLoaded()
fun onRewardAdFailedToLoad(errorCode: Int)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.m0skit0.android.hms.unity.ads

class RewardAdLoadListenerWrapper(private val listener: RewardAdLoadListener) : com.huawei.hms.ads.reward.RewardAdLoadListener() {

override fun onRewardedLoaded() {
listener.onRewardedLoaded()
}

override fun onRewardAdFailedToLoad(errorCode: Int) {
listener.onRewardAdFailedToLoad(errorCode)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.m0skit0.android.hms.unity.ads

import com.huawei.hms.ads.reward.Reward

interface RewardAdStatusListener {
fun onRewardAdClosed()
fun onRewardAdFailedToShow(errorCode: Int)
fun onRewardAdOpened()
fun onRewarded(reward: Reward)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.m0skit0.android.hms.unity.ads

import com.huawei.hms.ads.reward.Reward

class RewardAdStatusListenerWrapper(private val listener: RewardAdStatusListener) : com.huawei.hms.ads.reward.RewardAdStatusListener() {

override fun onRewardAdClosed() {
listener.onRewardAdClosed()
}

override fun onRewardAdFailedToShow(errorCode: Int) {
listener.onRewardAdFailedToShow(errorCode)
}

override fun onRewardAdOpened() {
listener.onRewardAdOpened()
}

override fun onRewarded(reward: Reward) {
listener.onRewarded(reward)
}
}
51 changes: 51 additions & 0 deletions hms-sdk-unity/HuaweiMobileServices/Ads/AdListenerWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace HuaweiMobileServices.Ads
{
using UnityEngine;

internal class AdListenerWrapper : AndroidJavaProxy
{

private readonly IAdListener mListener;

public AdListenerWrapper(IAdListener listener) : base("com.huawei.hms.ads.AdListener")
{
mListener = listener;
}

public void onAdClosed()
{
mListener.OnAdClosed();
}

public void onAdFailed(int paramInt)
{
mListener.OnAdFailed(paramInt);
}

public void onAdLeave()
{
mListener.OnAdLeave();
}

public void onAdOpened()
{
mListener.OnAdOpened();
}

public void onAdLoaded()
{
mListener.OnAdLoaded();
}

public void onAdClicked()
{
mListener.OnAdClicked();
}

public void onAdImpression()
{
mListener.OnAdImpression();
}

}
}
117 changes: 117 additions & 0 deletions hms-sdk-unity/HuaweiMobileServices/Ads/AdParam.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using System.Collections.Generic;

namespace HuaweiMobileServices.Ads
{
using HuaweiMobileServices.Utils;
using UnityEngine;

// Wrapper for com.huawei.hms.ads.Adparam
public class AdParam : JavaObjectWrapper
{

public AdParam(AndroidJavaObject javaObject) : base(javaObject) { }

public virtual string TargetingContentUrl => CallAsString("getTargetingContentUrl");

public int Gender => Call<int>("getGender");

public ISet<string> Keywords => Call<AndroidJavaObject>("getKeywords").AsStringSet();

// Wrapper for com.huawei.hms.ads.Adparam.Builder
public class Builder : JavaObjectWrapper
{

public Builder(AndroidJavaObject javaObject) : base(javaObject) { }

public Builder() : base("com.huawei.hms.ads.Adparam.Builder") { }

public Builder AddKeyword(string param1String)
{
JavaObject = Call<AndroidJavaObject>("addKeyword", param1String);
return this;
}

public AdParam Build() => CallAsWrapper<AdParam>("build");

public Builder SetTargetingContentUrl(string param1String)
{
JavaObject = Call<AndroidJavaObject>("setTargetingContentUrl", param1String);
return this;
}

public Builder SetGender(int param1Int)
{
JavaObject = Call<AndroidJavaObject>("setGender", param1Int);
return this;
}

public Builder SetRequestOrigin(string param1String)
{
JavaObject = Call<AndroidJavaObject>("setRequestOrigin", param1String);
return this;
}

public Builder SetBelongCountryCode(string param1String)
{
JavaObject = Call<AndroidJavaObject>("setBelongCountryCode", param1String);
return this;
}

public Builder SetTagForChildProtection(int? param1Integer)
{
JavaObject = Call<AndroidJavaObject>("setTagForChildProtection", param1Integer);
return this;
}

public Builder SetNonPersonalizedAd(int? param1Integer)
{
JavaObject = Call<AndroidJavaObject>("setNonPersonalizedAd", param1Integer);
return this;
}

public Builder SetAppLang(string param1String)
{
JavaObject = Call<AndroidJavaObject>("setAppLang", param1String);
return this;
}

public Builder SetAppCountry(string param1String)
{
JavaObject = Call<AndroidJavaObject>("setAppCountry", param1String);
return this;
}

public Builder SetTagForUnderAgeOfPromise(int? param1Integer)
{
JavaObject = Call<AndroidJavaObject>("setTagForUnderAgeOfPromise", param1Integer);
return this;
}

public Builder SetAdContentClassification(string param1String)
{
JavaObject = Call<AndroidJavaObject>("setAdContentClassification", param1String);
return this;
}
}

public static class ErrorCode
{
public static int INNER = 0;

public static int INVALID_REQUEST = 1;

public static int NETWORK_ERROR = 2;

public static int NO_AD = 3;

public static int AD_LOADING = 4;

public static int LOW_API = 5;

public static int BANNER_AD_EXPIRE = 6;

public static int BANNER_AD_CANCEL = 7;
}
}

}
53 changes: 53 additions & 0 deletions hms-sdk-unity/HuaweiMobileServices/Ads/BannerAdSize.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace HuaweiMobileServices.Ads
{
using HuaweiMobileServices.Utils;
using UnityEngine;

// Wrapper for com.huawei.hms.ads.BannerAdSize
public class BannerAdSize : JavaObjectWrapper
{

private const string CLASS_NAME = "com.huawei.hms.ads.BannerAdSize";

private static readonly AndroidJavaClass sJavaClass = new AndroidJavaClass(CLASS_NAME);

public static BannerAdSize BANNER_SIZE_360_57 => sJavaClass.GetStaticAsWrapper<BannerAdSize>("BANNER_SIZE_360_57");
public static BannerAdSize BANNER_SIZE_360_144 => sJavaClass.GetStaticAsWrapper<BannerAdSize>("BANNER_SIZE_360_144");
public static BannerAdSize BANNER_SIZE_320_50 => sJavaClass.GetStaticAsWrapper<BannerAdSize>("BANNER_SIZE_320_50");
public static BannerAdSize BANNER_SIZE_DYNAMIC => sJavaClass.GetStaticAsWrapper<BannerAdSize>("BANNER_SIZE_DYNAMIC");
public static BannerAdSize BANNER_SIZE_468_60 => sJavaClass.GetStaticAsWrapper<BannerAdSize>("BANNER_SIZE_468_60");
public static BannerAdSize BANNER_SIZE_INVALID => sJavaClass.GetStaticAsWrapper<BannerAdSize>("BANNER_SIZE_INVALID");
public static BannerAdSize BANNER_SIZE_320_100 => sJavaClass.GetStaticAsWrapper<BannerAdSize>("BANNER_SIZE_320_100");
public static BannerAdSize BANNER_SIZE_728_90 => sJavaClass.GetStaticAsWrapper<BannerAdSize>("BANNER_SIZE_728_90");
public static BannerAdSize BANNER_SIZE_300_250 => sJavaClass.GetStaticAsWrapper<BannerAdSize>("BANNER_SIZE_300_250");
public static BannerAdSize BANNER_SIZE_SMART => sJavaClass.GetStaticAsWrapper<BannerAdSize>("BANNER_SIZE_SMART");
public static BannerAdSize BANNER_SIZE_160_600 => sJavaClass.GetStaticAsWrapper<BannerAdSize>("BANNER_SIZE_160_600");

public static BannerAdSize GetCurrentDirectionBannerSize(int paramInt) =>
sJavaClass.CallStaticAsWrapper<BannerAdSize>("getCurrentDirectionBannerSize", AndroidContext.GetActivityContext(), paramInt);

public static BannerAdSize GetLandscapeBannerSize(int paramInt) =>
sJavaClass.CallStaticAsWrapper<BannerAdSize>("getLandscapeBannerSize", AndroidContext.GetActivityContext(), paramInt);

public static BannerAdSize GetPortraitBannerSize(int paramInt) =>
sJavaClass.CallStaticAsWrapper<BannerAdSize>("getPortraitBannerSize", AndroidContext.GetActivityContext(), paramInt);

public BannerAdSize(int paramInt1, int paramInt2) : base(CLASS_NAME, paramInt1, paramInt2) { }

public BannerAdSize(AndroidJavaObject javaObject) : base(javaObject) { }

public int Height => Call<int>("getHeight");

public int Width => Call<int>("getWidth");

public int HeightPx => Call<int>("getHeightPx", AndroidContext.GetActivityContext());

public int WidthPx => Call<int>("getWidthPx", AndroidContext.GetActivityContext());

public bool AutoHeightSize => Call<bool>("isAutoHeightSize");

public bool DynamicSize => Call<bool>("isDynamicSize");

public bool FullWidthSize => Call<bool>("isFullWidthSize");
}
}
47 changes: 47 additions & 0 deletions hms-sdk-unity/HuaweiMobileServices/Ads/BannerView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;

namespace HuaweiMobileServices.Ads
{
using HuaweiMobileServices.Utils;
using UnityEngine;

// Wrapper for com.huawei.hms.ads.banner.BannerView
public class BannerView : JavaObjectWrapper
{

public BannerView(AndroidJavaObject javaObject) : base(javaObject) { }

public virtual void Destroy() => Call("destroy");

public virtual IAdListener AdListener
{
set => Call("setAdListener", new AdListenerWrapper(value));
}

public virtual BannerAdSize BannerAdSize
{
get => CallAsWrapper<BannerAdSize>("getBannerAdSize");
set => Call("setBannerAdSize", value);
}

public virtual string AdId => CallAsString("getAdId");

public virtual bool Loading => Call<bool>("isLoading");

public virtual void LoadAd(AdParam paramAdParam) => Call("loadAd", paramAdParam);

public virtual void Pause() => Call("pause");

public virtual void Resume() => Call("resume");

public virtual long BannerRefresh
{
set
{
Call("setBannerRefresh", value);
}
}

}

}
19 changes: 19 additions & 0 deletions hms-sdk-unity/HuaweiMobileServices/Ads/HwAds.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace HuaweiMobileServices.Ads
{
using UnityEngine;
using HuaweiMobileServices.Utils;

// Wrapper for com.huawei.hms.ads.HwAds
public static class HwAds
{

private static readonly AndroidJavaClass sJavaClass = new AndroidJavaClass("com.huawei.hms.ads.HwAds");

public static void Init() => sJavaClass.CallStatic("init", AndroidContext.GetActivityContext());

}
}
17 changes: 17 additions & 0 deletions hms-sdk-unity/HuaweiMobileServices/Ads/IAdListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace HuaweiMobileServices.Ads
{
public interface IAdListener
{
void OnAdClosed();
void OnAdFailed(int reason);
void OnAdLeave();
void OnAdOpened();
void OnAdLoaded();
void OnAdClicked();
void OnAdImpression();
}
}
Loading

0 comments on commit 3a117a0

Please sign in to comment.