-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eaa2cc6
commit ab37677
Showing
5 changed files
with
112 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 105 additions & 68 deletions
173
ohos/src/main/ets/components/plugin/FlutterUnionadPlugin.ets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,123 @@ | ||
import { | ||
FlutterPlugin, | ||
FlutterPluginBinding, | ||
MethodCall, | ||
MethodCallHandler, | ||
MethodChannel, | ||
MethodResult, | ||
AbilityAware, | ||
AbilityPluginBinding, | ||
FlutterPlugin, | ||
FlutterPluginBinding, | ||
MethodCall, | ||
MethodCallHandler, | ||
MethodChannel, | ||
MethodResult, | ||
} from '@ohos/flutter_ohos'; | ||
import { | ||
AdLoadType, | ||
AdSlotBuilder, CSJAdSdk, | ||
CSJRewardAd, | ||
RewardAdLoadListener, SDKConfig, | ||
SDKConfigBuilder | ||
AdLoadType, | ||
AdSlotBuilder, | ||
CSJAdSdk, | ||
CSJRewardAd, | ||
RewardAdInteractionListener, | ||
RewardAdLoadListener, | ||
SDKConfig, | ||
SDKConfigBuilder | ||
} from '@csj/openadsdk'; | ||
import { UIAbility } from '@kit.AbilityKit'; | ||
|
||
/** FlutterUnionadPlugin **/ | ||
export default class FlutterUnionadPlugin implements FlutterPlugin, MethodCallHandler { | ||
private channel: MethodChannel | null = null; | ||
private applicationContext: Context | null = null; | ||
private adCreator = CSJAdSdk.getAdCreator(); | ||
constructor() { | ||
} | ||
export default class FlutterUnionadPlugin implements FlutterPlugin, MethodCallHandler, AbilityAware { | ||
private channel: MethodChannel | null = null; | ||
private applicationContext: Context | null = null; | ||
private uiAbility: UIAbility | null = null; | ||
private adCreator = CSJAdSdk.getAdCreator(); | ||
|
||
getUniqueClassName(): string { | ||
return "FlutterUnionadPlugin" | ||
} | ||
constructor() { | ||
} | ||
|
||
onAttachedToEngine(binding: FlutterPluginBinding): void { | ||
this.channel = new MethodChannel(binding.getBinaryMessenger(), "flutter_unionad"); | ||
this.channel.setMethodCallHandler(this) | ||
this.applicationContext = binding.getApplicationContext(); | ||
} | ||
getUniqueClassName(): string { | ||
return "FlutterUnionadPlugin" | ||
} | ||
|
||
onDetachedFromEngine(binding: FlutterPluginBinding): void { | ||
if (this.channel != null) { | ||
this.channel.setMethodCallHandler(null) | ||
} | ||
onAttachedToEngine(binding: FlutterPluginBinding): void { | ||
this.channel = new MethodChannel(binding.getBinaryMessenger(), "flutter_unionad"); | ||
this.channel.setMethodCallHandler(this) | ||
this.applicationContext = binding.getApplicationContext(); | ||
} | ||
|
||
onDetachedFromEngine(binding: FlutterPluginBinding): void { | ||
if (this.channel != null) { | ||
this.channel.setMethodCallHandler(null) | ||
} | ||
} | ||
|
||
onMethodCall(call: MethodCall, result: MethodResult): void { | ||
if (call.method == "register") { | ||
onAttachedToAbility(binding: AbilityPluginBinding): void { | ||
this.uiAbility = binding.getAbility() | ||
} | ||
|
||
// 创建SDKConfig对象 | ||
let adConfigBuilder = new SDKConfigBuilder() | ||
let config: SDKConfig = adConfigBuilder | ||
.appId("5638354") | ||
.appName("穿山甲Demo") | ||
.allowShowNotify(true) | ||
.debug(true) | ||
.build() | ||
onDetachedFromAbility(): void { | ||
this.uiAbility = null | ||
} | ||
|
||
// 初始化SDK | ||
CSJAdSdk.init(this.applicationContext, config) //context类型必须为UIAbility,否则可能存在展示异常场景 | ||
onMethodCall(call: MethodCall, result: MethodResult): void { | ||
if (call.method == "register") { | ||
let ohosAppId: string = call.args.get("ohosAppId"); | ||
let appName: string = call.args.get("appName"); | ||
let debug: boolean = call.args.get("debug"); | ||
// 创建SDKConfig对象 | ||
let adConfigBuilder = new SDKConfigBuilder() | ||
let config: SDKConfig = adConfigBuilder | ||
.appId(ohosAppId) | ||
.appName(appName) | ||
.allowShowNotify(true) | ||
.debug(debug) | ||
.build() | ||
|
||
// 启动SDK | ||
CSJAdSdk.start(); | ||
result.success(true) | ||
} else if (call.method == "getSDKVersion") { | ||
result.success(CSJAdSdk.getSDKVersion()) | ||
} else if (call.method == "getThemeStatus") { | ||
result.success(0) | ||
} else if (call.method == "loadRewardVideoAd") { | ||
// 初始化SDK | ||
CSJAdSdk.init(this.applicationContext, config) //context类型必须为UIAbility,否则可能存在展示异常场景 | ||
|
||
let adSlot = new AdSlotBuilder() | ||
.setCodeId("codeId") | ||
.setAdLoadType(AdLoadType.LOAD) | ||
// .setMediaExtra(mediaExtra) | ||
.build() | ||
let mLoadListener: RewardAdLoadListener = { | ||
onAdLoaded: (rewardAd: CSJRewardAd) => { | ||
// 广告基础信息加载完成 | ||
}, | ||
onAdCached: (rewardAd: CSJRewardAd) => { | ||
// 广告基础信息与素材缓存完成 | ||
}, | ||
onError: (code: number, message: string) => { | ||
} | ||
} | ||
this.adCreator.loadRewardAd(adSlot, mLoadListener) | ||
result.success(true) | ||
} else { | ||
result.notImplemented() | ||
// 启动SDK | ||
CSJAdSdk.start(); | ||
result.success(true) | ||
} else if (call.method == "getSDKVersion") { | ||
result.success(CSJAdSdk.getSDKVersion()) | ||
} else if (call.method == "getThemeStatus") { | ||
//todo 没做 | ||
result.success(0) | ||
} else if (call.method == "loadRewardVideoAd") { | ||
let ohosCodeId: string = call.args.get("ohosCodeId"); | ||
let adSlot = new AdSlotBuilder() | ||
.setCodeId(ohosCodeId) | ||
.setAdLoadType(AdLoadType.LOAD)// .setMediaExtra(mediaExtra) | ||
.build() | ||
let adInteractionListener: RewardAdInteractionListener = { | ||
onShow: () => { | ||
}, | ||
onSkip: () => { | ||
}, | ||
onClick: () => { | ||
}, | ||
onComplete: () => { | ||
}, | ||
onClose: () => { | ||
}, | ||
onRewardArrived: (isVerify, rewardType, extraInfo) => { | ||
} | ||
} | ||
let mLoadListener: RewardAdLoadListener = { | ||
onAdLoaded: (rewardAd: CSJRewardAd) => { | ||
// 广告基础信息加载完成 | ||
if (this.uiAbility != null) { | ||
rewardAd.setInteractionListener(adInteractionListener) | ||
rewardAd.showRewardAd(this.uiAbility.context.windowStage) | ||
} | ||
}, | ||
onAdCached: (rewardAd: CSJRewardAd) => { | ||
// 广告基础信息与素材缓存完成 | ||
}, | ||
onError: (code: number, message: string) => { | ||
console.log("onError,code:"+code+",message:"+message) | ||
} | ||
} | ||
this.adCreator.loadRewardAd(adSlot, mLoadListener) | ||
result.success(true) | ||
} else { | ||
result.notImplemented() | ||
} | ||
} | ||
} |