Skip to content

Commit

Permalink
Add SDK implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kentran-hw committed Oct 25, 2022
1 parent ae2eff8 commit 6b36f77
Show file tree
Hide file tree
Showing 22 changed files with 2,719 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Binaries/
Intermediate/
.vscode/
21 changes: 21 additions & 0 deletions HuaweiAds.uplugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"FileVersion": 3,
"Version": 1,
"VersionName": "1.0",
"FriendlyName": "HuaweiAds",
"Description": "Ads plugin for Unreal Game Engine to Huawei App Gallery",
"Category": "Service",
"CreatedBy": "Huawei Japan DTSE",
"CreatedByURL": "",
"DocsURL": "",
"MarketplaceURL": "",
"EnabledByDefault": true,
"SupportURL": "",
"Modules": [
{
"Name": "HuaweiAds",
"Type": "Runtime",
"LoadingPhase": "PreLoadingScreen"
}
]
}
102 changes: 102 additions & 0 deletions Source/HuaweiAds/External/com/huawei/adplugin/Const.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.huawei.adplugin;

public class Const {
public static final class BannerAdSize {
public static final String USER_DEFINED = "USER_DEFINED";

public static final String BANNER_SIZE_320_50 = "BANNER_SIZE_320_50";

public static final String BANNER_SIZE_320_100 = "BANNER_SIZE_320_100";

public static final String BANNER_SIZE_468_60 = "BANNER_SIZE_468_60";

public static final String BANNER_SIZE_DYNAMIC = "BANNER_SIZE_DYNAMIC";

public static final String BANNER_SIZE_728_90 = "BANNER_SIZE_728_90";

public static final String BANNER_SIZE_300_250 = "BANNER_SIZE_300_250";

public static final String BANNER_SIZE_SMART = "BANNER_SIZE_SMART";

public static final String BANNER_SIZE_160_600 = "BANNER_SIZE_160_600";

public static final String BANNER_SIZE_360_57 = "BANNER_SIZE_360_57";

public static final String BANNER_SIZE_360_144 = "BANNER_SIZE_360_144";
}

public static final class BannerAdPositionCode {
/**
* Position constant for a position with a custom offset.
*/
public static final int POSITION_CUSTOM = -1;

/**
* Position constant for top of the screen.
*/
public static final int POSITION_TOP = 0;

/**
* Position constant for bottom of the screen.
*/
public static final int POSITION_BOTTOM = 1;

/**
* Position constant for top-left of the screen.
*/
public static final int POSITION_TOP_LEFT = 2;

/**
* Position constant for top-right of the screen.
*/
public static final int POSITION_TOP_RIGHT = 3;

/**
* Position constant for bottom-left of the screen.
*/
public static final int POSITION_BOTTOM_LEFT = 4;

/**
* Position constant bottom-right of the screen.
*/
public static final int POSITION_BOTTOM_RIGHT = 5;

/**
* Position constant center of the screen.
*/
public static final int POSITION_CENTER = 6;

public static int getLayoutGravityForPositionCode(int positionCode) {
int gravity;
switch (positionCode) {
case POSITION_TOP:
gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
break;
case POSITION_BOTTOM:
gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
break;
case POSITION_TOP_LEFT:
gravity = Gravity.TOP | Gravity.LEFT;
break;
case POSITION_TOP_RIGHT:
gravity = Gravity.TOP | Gravity.RIGHT;
break;
case POSITION_BOTTOM_LEFT:
gravity = Gravity.BOTTOM | Gravity.LEFT;
break;
case POSITION_BOTTOM_RIGHT:
gravity = Gravity.BOTTOM | Gravity.RIGHT;
break;
case POSITION_CENTER:
gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
break;
case POSITION_CUSTOM:
gravity = Gravity.TOP | Gravity.LEFT;
break;
default:
throw new IllegalArgumentException("Attempted to position ad with invalid ad " + "position.");
}
return gravity;
}
}
}
204 changes: 204 additions & 0 deletions Source/HuaweiAds/External/com/huawei/adplugin/HuaweiAdsPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package com.huawei.adplugin;

import com.huawei.adplugin.*;
import com.huawei.hms.ads.*;

import android.app.Activity;

public class HuaweiAdsPlugin {
private static boolean isInit = false;
private static Activity mActivity = null;
private static BannerAdProxy bannerAdProxy = null;
private static InterstitialAdProxy interstitialAdProxy = null;
private static RewardAdProxy rewardAdProxy = null;

public static void initialize(Activity activity) {
if (!isInit) {
isInit = true;
mActivity = activity;
}
}

public static void loadBannerAd(String adId, int position, String size, IAdStatusListener adStatusListener) {
if (mActivity == null) {
return;
}
if (bannerAdProxy == null) {
bannerAdProxy = new BannerAdProxy(mActivity, new IAdStatusListener() {
@Override
public void onAdClosed() {
if (adStatusListener != null) {
adStatusListener.onAdClosed();
}
}

@Override
public void onAdFailed(int errorCode) {
if (adStatusListener != null) {
adStatusListener.onAdFailed(errorCode);
}
}

@Override
public void onAdLeftApp() {
if (adStatusListener != null) {
adStatusListener.onAdLeftApp();
}
}

@Override
public void onAdOpened() {
if (adStatusListener != null) {
adStatusListener.onAdOpened();
}
}

@Override
public void onAdLoaded() {
showBannerAd();
if (adStatusListener != null) {
adStatusListener.onAdLoaded();
}
}

@Override
public void onAdClicked() {
if (adStatusListener != null) {
adStatusListener.onAdClicked();
}
}

@Override
public void onAdImpression() {
if (adStatusListener != null) {
adStatusListener.onAdImpression();
}
}
});
}
bannerAdProxy.setAdId(adId);
bannerAdProxy.setBannerAdPosition(position);
bannerAdProxy.setAdSizeType(size);
AdParam adParam = new AdParam.Builder().build();
bannerAdProxy.loadAd(adParam);
}

public static void showBannerAd(){
if (bannerAdProxy != null) {
bannerAdProxy.show();
}
}

public static void hideBannerAd() {
if (bannerAdProxy != null) {
bannerAdProxy.hide();
}
}

public static void destroyBannerAd() {
if (bannerAdProxy != null) {
bannerAdProxy.destroy();
}
}

public static void loadInterstitialAd(String adId, IAdStatusListener adStatusListener) {
if (mActivity == null) {
return;
}
if (interstitialAdProxy == null) {
interstitialAdProxy = new InterstitialAdProxy(mActivity);
}
interstitialAdProxy.setAdId(adId);
interstitialAdProxy.setAdListener(new IAdStatusListener() {
@Override
public void onAdClosed() {
if (adStatusListener != null) {
adStatusListener.onAdClosed();
}
}

@Override
public void onAdFailed(int errorCode) {
if (adStatusListener != null) {
adStatusListener.onAdFailed(errorCode);
}
}

@Override
public void onAdLeftApp() {
if (adStatusListener != null) {
adStatusListener.onAdLeftApp();
}
}

@Override
public void onAdOpened() {
if (adStatusListener != null) {
adStatusListener.onAdOpened();
}
}

@Override
public void onAdLoaded() {
showInterstitialAd();
if (adStatusListener != null) {
adStatusListener.onAdLoaded();
}
}

@Override
public void onAdClicked() {
if (adStatusListener != null) {
adStatusListener.onAdClicked();
}
}

@Override
public void onAdImpression() {
if (adStatusListener != null) {
adStatusListener.onAdImpression();
}
}
});
AdParam adParam = new AdParam.Builder().build();
interstitialAdProxy.loadAd(adParam);
}

public static void showInterstitialAd() {
if (interstitialAdProxy != null && interstitialAdProxy.isLoaded()) {
interstitialAdProxy.show();
}
}

public static void loadRewardAd(String adId, IRewardAdLoadListener rewardLoadListener, IRewardAdStatusListener rewardStatusListener) {
if (mActivity == null) {
return;
}
if (rewardAdProxy == null) {
rewardAdProxy = new RewardAdProxy(adId, mActivity);
}
AdParam adParam = new AdParam.Builder().build();
rewardAdProxy.loadAd(adParam, new IRewardAdLoadListener() {
@Override
public void onRewardAdFailedToLoad(final int errorCode) {
if (rewardLoadListener != null) {
rewardLoadListener.onRewardAdFailedToLoad(errorCode);
}
}

@Override
public void onRewardedLoaded() {
showRewardAd(rewardStatusListener);
if (rewardLoadListener != null) {
rewardLoadListener.onRewardedLoaded();
}
}
});
}

public static void showRewardAd(IRewardAdStatusListener adStatusListener) {
if (rewardAdProxy != null && rewardAdProxy.isLoaded() && mActivity != null) {
rewardAdProxy.show(mActivity, adStatusListener);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.huawei.adplugin.adlistener;

public interface IAdStatusListener {
void onAdClosed();

void onAdFailed(int errorCode);

void onAdLeftApp();

void onAdOpened();

void onAdLoaded();

void onAdClicked();

void onAdImpression();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

package com.huawei.adplugin.adlistener;

public interface IRewardAdLoadListener {
void onRewardAdFailedToLoad(int errorCode);

void onRewardedLoaded();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

package com.huawei.adplugin.adlistener;

public interface IRewardAdStatusListener {
void onRewardAdFailedToLoad(int errorCode);

void onRewardedLoaded();

void onRewardAdClosed();

void onRewardAdFailedToShow(int errorCode);

void onRewardAdOpened();

void onRewarded(String type, int amount);
}
Loading

0 comments on commit 6b36f77

Please sign in to comment.