Skip to content

Commit

Permalink
Merge pull request #149 from zhangruiyu/ohos
Browse files Browse the repository at this point in the history
Ohos
  • Loading branch information
gstory0404 authored Jan 15, 2025
2 parents 5b0dc7e + 4d279e2 commit 3914c33
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 13 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ import 'package:flutter_unionad/flutter_unionad.dart';
<true/>
```

#### 4、鸿蒙next
##### 需要把下面这个文件拷贝到你的鸿蒙项目根目录下
[.ohpmrc](example/ohos/.ohpmrc)



## 使用

Expand Down
1 change: 1 addition & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ class _IndexPageState extends State<IndexPage> {
androidCodeId: "102735530",
//ios 全屏广告id 必填
iosCodeId: "102735530",
ohosCodeId: "963135369",
//视屏方向 选填
orientation: FlutterUnionadOrientation.VERTICAL,
);
Expand Down
12 changes: 0 additions & 12 deletions example/ohos/build-profile.json5
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,6 @@
]
}
]
},
{
"name": "flutter_unionad",
"srcPath": "../../ohos",
"targets": [
{
"name": "default",
"applyToProducts": [
"default"
]
}
]
}
]
}
8 changes: 8 additions & 0 deletions example/ohos/entry/src/main/resources/rawfile/buildinfo.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"string": [
{
"name": "enable_impeller",
"value": "true"
}
]
}
2 changes: 1 addition & 1 deletion example/ohos/oh-package.json5
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"overrides": {
"@ohos/flutter_ohos": "file:./har/flutter.har",
"flutter_unionad": "../../ohos",
"flutter_unionad": "file:./har/flutter_unionad.har",
"@ohos/flutter_module": "file:./entry"
}
}
2 changes: 2 additions & 0 deletions lib/flutter_unionad.dart
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,13 @@ class FlutterUnionad {
static Future<bool> loadFullScreenVideoAdInteraction({
required String androidCodeId,
required String iosCodeId,
String? ohosCodeId,
int? orientation,
}) async {
return await _channel.invokeMethod("loadFullScreenVideoAdInteraction", {
"androidCodeId": androidCodeId,
"iosCodeId": iosCodeId,
"ohosCodeId": ohosCodeId,
"orientation": orientation ?? FlutterUnionadOrientation.VERTICAL,
});
}
Expand Down
15 changes: 15 additions & 0 deletions ohos/src/main/ets/components/plugin/FlutterUnionadPlugin.ets
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from '@csj/openadsdk';
import { UIAbility } from '@kit.AbilityKit';
import FlutterUnionadEventPlugin from './FlutterUnionadEventPlugin';
import FullScreenVideoAdInteraction from './FullScreenVideoAdInteraction';

/** FlutterUnionadPlugin **/
export default class FlutterUnionadPlugin implements FlutterPlugin, MethodCallHandler, AbilityAware {
Expand Down Expand Up @@ -168,6 +169,20 @@ export default class FlutterUnionadPlugin implements FlutterPlugin, MethodCallHa
this.rewardAd?.showRewardAd(this.uiAbility.context.windowStage)
}
result.success(true)
} else if (call.method == "loadFullScreenVideoAdInteraction") {
let ohosCodeId: string = call.args.get("ohosCodeId");
let orientation: number = call.args.get("orientation");
FullScreenVideoAdInteraction.init(
this.applicationContext!,
this.uiAbility!.context.windowStage,
ohosCodeId,
orientation!!,
)
result.success(true)
//显示插屏广告 全屏插屏二合一
} else if (call.method == "showFullScreenVideoAdInteraction") {
FullScreenVideoAdInteraction.showAd()
result.success(true)
} else {
result.notImplemented()
}
Expand Down
126 changes: 126 additions & 0 deletions ohos/src/main/ets/components/plugin/FullScreenVideoAdInteraction.ets
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { Any, Log } from '@ohos/flutter_ohos';
import {
AdSlotBuilder,
CSJAdCreator,
CSJAdSdk,
CSJFullAd,
FullAdInteractionListener,
FullAdLoadListener
} from '@csj/openadsdk';
import FlutterUnionadEventPlugin from './FlutterUnionadEventPlugin';
import { window } from '@kit.ArkUI';

/**
* @Description:
* @Author: gstory0404@gmail
* @CreateDate: 2020/8/21 15:48
*/
export default class FullScreenVideoAdInteraction {
private static TAG = "FullScreenVideoExpressAd"
static mContext: Context | null = null
static mActivity: window.WindowStage | null = null
private static mttFullVideoAd: CSJFullAd | null = null
//广告所需参数
private static mCodeId: string | null = null
private static orientation: number = 1
private static mAdCreator: CSJAdCreator | null

static init(
context: Context,
mActivity: window.WindowStage | null,
mCodeId: string | null,
orientation: number | null,
): void {
FullScreenVideoAdInteraction.mContext = context
FullScreenVideoAdInteraction.mActivity = mActivity
FullScreenVideoAdInteraction.mCodeId = mCodeId
FullScreenVideoAdInteraction.orientation = orientation!!
FullScreenVideoAdInteraction.loadFullScreenVideoAd()
}

private static loadFullScreenVideoAd() {
Log.e(FullScreenVideoAdInteraction.TAG, "广告位id $mCodeId")
FullScreenVideoAdInteraction.mAdCreator = CSJAdSdk.getAdCreator()
//设置广告参数
let adSlot = new AdSlotBuilder().setCodeId(FullScreenVideoAdInteraction.mCodeId)//广告位id
.setOrientation(FullScreenVideoAdInteraction.orientation).build()
let mLoadListener: FullAdLoadListener = {
onAdLoaded: (fullAd: CSJFullAd) => {
// 广告基础信息加载完成
Log.e(FullScreenVideoAdInteraction.TAG, "fullScreenVideoAdInteraction loaded")
FullScreenVideoAdInteraction.mttFullVideoAd = fullAd
let map: Record<string, Any> = {
"adType": "fullScreenVideoAdInteraction", "onAdMethod": "onReady"
}
FlutterUnionadEventPlugin.sendContent(map)
},
onAdCached: (fullAd: CSJFullAd) => {
// 广告基础信息与素材缓存完成
Log.e(FullScreenVideoAdInteraction.TAG, "fullScreenVideoAdInteraction video cached2")
},
onError: (code: number, message: string) => {
Log.e(FullScreenVideoAdInteraction.TAG, "fullScreenVideoAd加载失败 $code === > $message")
let map: Record<string, Any> = {
"adType": "fullScreenVideoAdInteraction",
"onAdMethod": "onFail",
"error": code + message
}
FlutterUnionadEventPlugin.sendContent(map)
}
}
//加载全屏视频
FullScreenVideoAdInteraction.mAdCreator.loadFullAd(adSlot, mLoadListener)

}

static showAd() {
if (FullScreenVideoAdInteraction.mttFullVideoAd == null) {
let map: Record<string, Any> = {
"adType": "fullScreenVideoAdInteraction",
"onAdMethod": "onUnReady",
"error": "广告预加载未完成"
}
FlutterUnionadEventPlugin.sendContent(map)
return
}
let adInteractionListener: FullAdInteractionListener = {
onShow: () => {
Log.e(FullScreenVideoAdInteraction.TAG, "fullScreenVideoAdInteraction show")
let map: Record<string, Any> = {
"adType": "fullScreenVideoAdInteraction", "onAdMethod": "onShow"
}
FlutterUnionadEventPlugin.sendContent(map)
},
onSkip: () => {
Log.e(FullScreenVideoAdInteraction.TAG, "fullScreenVideoAd skipped")
let map: Record<string, Any> = {
"adType": "fullScreenVideoAdInteraction", "onAdMethod": "onSkip"
}
FlutterUnionadEventPlugin.sendContent(map)
},
onClick: () => {
Log.e(FullScreenVideoAdInteraction.TAG, "fullScreenVideoAd click")
let map: Record<string, Any> = {
"adType": "fullScreenVideoAdInteraction", "onAdMethod": "onClick"
}
FlutterUnionadEventPlugin.sendContent(map)
},
onClose: () => {
Log.e(FullScreenVideoAdInteraction.TAG, "fullScreenVideoAd close")
let map: Record<string, Any> = {
"adType": "fullScreenVideoAdInteraction", "onAdMethod": "onClose"
}
FlutterUnionadEventPlugin.sendContent(map)
},
onComplete: () => {
Log.e(FullScreenVideoAdInteraction.TAG, "fullScreenVideoAd complete")
let map: Record<string, Any> = {
"adType": "fullScreenVideoAdInteraction", "onAdMethod": "onFinish"
}
FlutterUnionadEventPlugin.sendContent(map)
}
}
FullScreenVideoAdInteraction.mttFullVideoAd?.setInteractionListener(adInteractionListener)
FullScreenVideoAdInteraction.mttFullVideoAd?.showFullAd(FullScreenVideoAdInteraction.mActivity)
}
}

0 comments on commit 3914c33

Please sign in to comment.