147 lines
4.5 KiB
Dart
147 lines
4.5 KiB
Dart
// Author: fengshengxiong
|
||
// Date: 2024/5/20
|
||
// Description: 插页广告管理
|
||
|
||
import 'dart:async';
|
||
import 'dart:io';
|
||
|
||
import 'package:applovin_max/applovin_max.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:wallpaperx/common/utils/log_print.dart';
|
||
import 'package:wallpaperx/common/utils/num_util.dart';
|
||
import 'package:wallpaperx/common/utils/shared_util.dart';
|
||
import 'package:wallpaperx/firebase/firebase_analytics_manager.dart';
|
||
import 'package:wallpaperx/page/launch/launch_controller.dart';
|
||
|
||
class InterstitialAdManager {
|
||
InterstitialAdManager._();
|
||
|
||
static final InterstitialAdManager _instance = InterstitialAdManager._();
|
||
|
||
factory InterstitialAdManager() => _instance;
|
||
|
||
/// sdkKey
|
||
final String applovinKey = 'HXOh4UBLahW9KzBBrxqwniKOvfD_JEDJgE9y2rv8DlmJaJ6xPEXUmmJyeAg0xipqdH_EiHg5NsnvfmIHubSu1k';
|
||
|
||
/// 开屏广告
|
||
final launchAdId = Platform.isAndroid ? '' : 'f4d33bc86b44eb24';
|
||
|
||
/// 广告单元Id
|
||
final adIds = [
|
||
Platform.isAndroid ? '' : 'e0ac389b7746be38',
|
||
Platform.isAndroid ? '' : 'a8ecc9c457dee7bf',
|
||
Platform.isAndroid ? '' : 'f4231f22c0314229',
|
||
];
|
||
|
||
/// 是否已初始化
|
||
bool isInitialized = false;
|
||
|
||
/// 是否显示
|
||
bool isShowingAd = false;
|
||
|
||
/// 重试计数
|
||
// final _maxExponentialRetryCount = 6;
|
||
// var _interstitialRetryAttempt = 0;
|
||
|
||
Function()? onTap;
|
||
|
||
/// 初始化
|
||
Future<void> init() async {
|
||
MaxConfiguration? configuration = await AppLovinMAX.initialize(applovinKey);
|
||
if (configuration != null) {
|
||
isInitialized = true;
|
||
_attachAdListeners();
|
||
loadAllAd();
|
||
}
|
||
}
|
||
|
||
/// 广告监听
|
||
void _attachAdListeners() {
|
||
AppLovinMAX.setInterstitialListener(InterstitialListener(
|
||
onAdLoadedCallback: (ad) async {
|
||
LogPrint.d('插页广告加载成功:${ad.adUnitId}');
|
||
if (ad.adUnitId == launchAdId) {
|
||
if (Get.isRegistered<LaunchController>()) {
|
||
LaunchController.to.editChangeValue();
|
||
}
|
||
}
|
||
// _interstitialRetryAttempt = 0;
|
||
},
|
||
onAdLoadFailedCallback: (adUnitId, error) {
|
||
LogPrint.d('插页广告加载失败adUnitId:$adUnitId,code:${error.code},message:${error.message}');
|
||
// Applovin建议您以指数级更高的延迟重试,最大延迟可达64秒
|
||
// _interstitialRetryAttempt = _interstitialRetryAttempt + 1;
|
||
// if (_interstitialRetryAttempt > _maxExponentialRetryCount) return;
|
||
// int retryDelay = pow(2, min(_maxExponentialRetryCount, _interstitialRetryAttempt)).toInt();
|
||
|
||
Future.delayed(const Duration(seconds: 10), () {
|
||
AppLovinMAX.loadInterstitial(adUnitId);
|
||
});
|
||
},
|
||
onAdDisplayedCallback: (ad) {
|
||
LogPrint.d('插页广告显示成功:${ad.adUnitId}');
|
||
isShowingAd = true;
|
||
FirebaseAnalyticsManager.logAdsShow();
|
||
},
|
||
onAdDisplayFailedCallback: (ad, error) {
|
||
LogPrint.d('插页广告显示失败adUnitId:${ad.adUnitId},code:${error.code},message:${error.message}');
|
||
isShowingAd = false;
|
||
|
||
if (onTap != null) onTap!();
|
||
},
|
||
onAdClickedCallback: (ad) {},
|
||
onAdHiddenCallback: (ad) {
|
||
LogPrint.d('插页广告关闭:${ad.adUnitId}');
|
||
isShowingAd = false;
|
||
AppLovinMAX.loadInterstitial(ad.adUnitId);
|
||
|
||
if (onTap != null) onTap!();
|
||
},
|
||
));
|
||
}
|
||
|
||
void loadAllAd() {
|
||
bool adSwitch = UPCache.getInstance().get<bool>('adSwitch') ?? false;
|
||
if (!adSwitch) {
|
||
return;
|
||
}
|
||
AppLovinMAX.loadInterstitial(launchAdId);
|
||
for (var id in adIds) {
|
||
AppLovinMAX.loadInterstitial(id);
|
||
}
|
||
LogPrint.d('插页广告正在加载');
|
||
}
|
||
|
||
/// 显示插页广告,如果准备好
|
||
Future<void> showAdIfReady({bool showLaunchAd = false, Function()? onTap}) async {
|
||
if (onTap != null) {
|
||
this.onTap = onTap;
|
||
}
|
||
bool adSwitch = UPCache.getInstance().get<bool>('adSwitch') ?? false;
|
||
if (!adSwitch) {
|
||
if (this.onTap != null) this.onTap!();
|
||
return;
|
||
}
|
||
if (!isInitialized) {
|
||
if (this.onTap != null) this.onTap!();
|
||
return;
|
||
}
|
||
var adId = launchAdId;
|
||
if (!showLaunchAd) {
|
||
int index = NumUtil.getRandomNumber(0, 3);
|
||
adId = adIds[index];
|
||
}
|
||
bool isReady = (await AppLovinMAX.isInterstitialReady(adId))!;
|
||
if (isReady) {
|
||
if (isShowingAd) {
|
||
LogPrint.d('尝试在已显示广告的情况下显示广告');
|
||
if (this.onTap != null) this.onTap!();
|
||
return;
|
||
}
|
||
AppLovinMAX.showInterstitial(adId);
|
||
} else {
|
||
if (this.onTap != null) this.onTap!();
|
||
AppLovinMAX.loadInterstitial(adId);
|
||
}
|
||
}
|
||
} |