添加米盟广告控制模块
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
#Created by .winboll/winboll_app_build.gradle
|
||||
#Wed Nov 26 15:54:26 GMT 2025
|
||||
#Thu Nov 27 05:15:57 GMT 2025
|
||||
stageCount=7
|
||||
libraryProject=libaes
|
||||
baseVersion=15.11
|
||||
publishVersion=15.11.6
|
||||
buildCount=32
|
||||
buildCount=42
|
||||
baseBetaVersion=15.11.7
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#Created by .winboll/winboll_app_build.gradle
|
||||
#Wed Nov 26 15:54:26 GMT 2025
|
||||
#Thu Nov 27 05:15:57 GMT 2025
|
||||
stageCount=7
|
||||
libraryProject=libaes
|
||||
baseVersion=15.11
|
||||
publishVersion=15.11.6
|
||||
buildCount=32
|
||||
buildCount=42
|
||||
baseBetaVersion=15.11.7
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package cc.winboll.studio.libaes.enums;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/11/27 12:35
|
||||
* @Describe 隐私协议签约状态枚举
|
||||
* 对应值:0-拒绝,1-赞同,2-未签约(默认)
|
||||
*/
|
||||
public enum PrivacyAgreeStatus {
|
||||
REJECTED(0, "拒绝"), // 0: 拒绝隐私协议
|
||||
AGREED(1, "赞同"), // 1: 赞同隐私协议
|
||||
UN_SIGNED(2, "未签约"); // 2: 未签约(初始默认状态)
|
||||
|
||||
private final int statusCode; // 对应存储的int值
|
||||
private final String statusDesc; // 状态描述(可选,便于日志/UI显示)
|
||||
|
||||
// Java 7 枚举构造方法(必须private)
|
||||
private PrivacyAgreeStatus(int statusCode, String statusDesc) {
|
||||
this.statusCode = statusCode;
|
||||
this.statusDesc = statusDesc;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据int值获取枚举(SP读取时使用,兼容Java 7)
|
||||
* @param code 存储的int值(0/1/2)
|
||||
* @return 对应枚举,默认返回UN_SIGNED(未签约)
|
||||
*/
|
||||
public static PrivacyAgreeStatus fromCode(int code) {
|
||||
// Java 7 不支持switch(String),用if-else兼容
|
||||
if (code == REJECTED.statusCode) {
|
||||
return REJECTED;
|
||||
} else if (code == AGREED.statusCode) {
|
||||
return AGREED;
|
||||
} else {
|
||||
return UN_SIGNED; // 默认未签约
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据SP存储的字符串值获取枚举(兼容原逻辑中String类型存储)
|
||||
* @param codeStr 存储的字符串值("0"/"1"/"2")
|
||||
* @return 对应枚举,默认返回UN_SIGNED(未签约)
|
||||
*/
|
||||
public static PrivacyAgreeStatus fromString(String codeStr) {
|
||||
if (codeStr == null) {
|
||||
return UN_SIGNED;
|
||||
}
|
||||
try {
|
||||
int code = Integer.parseInt(codeStr);
|
||||
return fromCode(code);
|
||||
} catch (NumberFormatException e) {
|
||||
// 字符串格式异常时,默认返回未签约
|
||||
return UN_SIGNED;
|
||||
}
|
||||
}
|
||||
|
||||
// 获取状态码(用于存储到SP)
|
||||
public int getStatusCode() {
|
||||
return statusCode;
|
||||
}
|
||||
|
||||
// 获取状态描述(用于日志/UI显示,可选)
|
||||
public String getStatusDesc() {
|
||||
return statusDesc;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,27 +2,19 @@ package cc.winboll.studio.libaes.views;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.text.TextUtils;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.Display;
|
||||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.Toast;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import cc.winboll.studio.libaes.R;
|
||||
import cc.winboll.studio.libaes.enums.ADsMode;
|
||||
import cc.winboll.studio.libaes.utils.MimoUtils;
|
||||
import cc.winboll.studio.libappbase.GlobalApplication;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import com.miui.zeus.mimo.sdk.ADParams;
|
||||
import com.miui.zeus.mimo.sdk.BannerAd;
|
||||
import com.miui.zeus.mimo.sdk.MimoCustomController;
|
||||
@@ -30,8 +22,6 @@ import com.miui.zeus.mimo.sdk.MimoLocation;
|
||||
import com.miui.zeus.mimo.sdk.MimoSdk;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import cc.winboll.studio.libaes.enums.ADsMode;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
@@ -84,9 +74,8 @@ public class ADsBannerView extends LinearLayout {
|
||||
|
||||
void initView(Context context) {
|
||||
this.mContext = context;
|
||||
|
||||
initMimoSdk(this.mContext);
|
||||
|
||||
|
||||
// 初始化主线程Handler(关键:确保广告操作在主线程执行)
|
||||
mMainHandler = new Handler(Looper.getMainLooper());
|
||||
|
||||
@@ -98,9 +87,13 @@ public class ADsBannerView extends LinearLayout {
|
||||
public void resumeADs(final Activity activity) {
|
||||
// 没有设置米盟广告支持就退出
|
||||
if (ADsControlView.getAdsModeFromStatic(this.mContext) != ADsMode.MIMO_SDK) {
|
||||
// 2. 释放之前的广告资源
|
||||
if (mBannerAd != null) {
|
||||
mBannerAd.destroy();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// 修复:优化广告请求逻辑(添加生命周期判断 + 主线程执行)
|
||||
if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
|
||||
if (ADsControlView.getAdsModeFromStatic(this.mContext) == ADsMode.MIMO_SDK) {
|
||||
@@ -126,7 +119,7 @@ public class ADsBannerView extends LinearLayout {
|
||||
if (ADsControlView.getAdsModeFromStatic(this.mContext) != ADsMode.MIMO_SDK) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
LogUtils.d(TAG, "releaseAdResources()");
|
||||
|
||||
// 移除Handler回调
|
||||
@@ -159,7 +152,7 @@ public class ADsBannerView extends LinearLayout {
|
||||
if (ADsControlView.getAdsModeFromStatic(this.mContext) != ADsMode.MIMO_SDK) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
LogUtils.d(TAG, "showAd()");
|
||||
// 1. 生命周期校验:避免Activity已销毁时操作UI
|
||||
if (activity == null || activity.isFinishing() || activity.isDestroyed()) {
|
||||
@@ -227,7 +220,7 @@ public class ADsBannerView extends LinearLayout {
|
||||
if (ADsControlView.getAdsModeFromStatic(this.mContext) != ADsMode.MIMO_SDK) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
LogUtils.d(TAG, "fetchAd()");
|
||||
// 1. 双重校验:Activity未销毁 + Context非空
|
||||
if (activity == null || activity.isFinishing() || activity.isDestroyed() || activity.getApplicationContext() == null) {
|
||||
@@ -325,7 +318,7 @@ public class ADsBannerView extends LinearLayout {
|
||||
if (ADsControlView.getAdsModeFromStatic(this.mContext) != ADsMode.MIMO_SDK) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// 修复:加载失败时移除当前广告实例
|
||||
if (mAllBanners.contains(mBannerAd)) {
|
||||
mAllBanners.remove(mBannerAd);
|
||||
|
||||
@@ -27,6 +27,7 @@ import com.miui.zeus.mimo.sdk.MimoCustomController;
|
||||
import com.miui.zeus.mimo.sdk.MimoLocation;
|
||||
import com.miui.zeus.mimo.sdk.MimoSdk;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.libaes.enums.PrivacyAgreeStatus;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
@@ -37,7 +38,7 @@ import cc.winboll.studio.libappbase.ToastUtils;
|
||||
public class ADsControlView extends LinearLayout {
|
||||
public static final String TAG = "ADsControlView";
|
||||
|
||||
|
||||
|
||||
// SP存储配置
|
||||
private static final String SP_NAME = "ads_control_config";
|
||||
private static final String KEY_SELECTED_MODE = "selected_ads_mode";
|
||||
@@ -45,15 +46,15 @@ public class ADsControlView extends LinearLayout {
|
||||
ADsMode mADsMode;
|
||||
private static final String PRIVACY_VALUE = "privacy_value";
|
||||
// 隐私协议签约结果 0: 拒绝,1:赞同 2: 未签约
|
||||
String privacyAgreeValue;
|
||||
PrivacyAgreeStatus mPrivacyAgreeStatus;
|
||||
|
||||
// Handler消息标识
|
||||
private static final int MSG_UPDATE_MODE = 1001;
|
||||
|
||||
// 控件引用
|
||||
private RadioGroup rgAdsMode;
|
||||
private RadioGroup rgADsMode;
|
||||
private RadioButton rbStandalone;
|
||||
private RadioButton rbMimoSdk;
|
||||
private RadioButton rbMimoSDK;
|
||||
|
||||
// 外部监听、SP实例、Handler实例
|
||||
private OnAdsModeSelectedListener listener;
|
||||
@@ -81,13 +82,15 @@ public class ADsControlView extends LinearLayout {
|
||||
initView(context);
|
||||
}
|
||||
|
||||
public void setPrivacyAgreeValue(String privacyAgreeValue) {
|
||||
this.privacyAgreeValue = privacyAgreeValue;
|
||||
public void setPrivacyAgreeStatus(PrivacyAgreeStatus privacyAgreeStatus) {
|
||||
this.mPrivacyAgreeStatus = privacyAgreeStatus;
|
||||
sharedPreferences.edit().putString(PRIVACY_VALUE, this.mPrivacyAgreeStatus.name()).apply();
|
||||
}
|
||||
|
||||
public String getPrivacyAgreeValue() {
|
||||
String privacyAgreeValue = sharedPreferences.getString(PRIVACY_VALUE, "0");
|
||||
return privacyAgreeValue;
|
||||
public PrivacyAgreeStatus getPrivacyAgreeStatus() {
|
||||
String privacyAgreeStatusStr = sharedPreferences.getString(PRIVACY_VALUE, PrivacyAgreeStatus.UN_SIGNED.name());
|
||||
PrivacyAgreeStatus privacyAgreeStatus = PrivacyAgreeStatus.fromString(privacyAgreeStatusStr);
|
||||
return privacyAgreeStatus;
|
||||
}
|
||||
|
||||
public void setADsMode(ADsMode mADsMode) {
|
||||
@@ -113,9 +116,9 @@ public class ADsControlView extends LinearLayout {
|
||||
sharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
|
||||
|
||||
// 绑定控件
|
||||
rgAdsMode = (RadioGroup) findViewById(R.id.rg_ads_mode);
|
||||
rgADsMode = (RadioGroup) findViewById(R.id.rg_ads_mode);
|
||||
rbStandalone = (RadioButton) findViewById(R.id.rb_standalone);
|
||||
rbMimoSdk = (RadioButton) findViewById(R.id.rb_mimo_sdk);
|
||||
rbMimoSDK = (RadioButton) findViewById(R.id.rb_mimo_sdk);
|
||||
|
||||
// 初始化Handler(主线程Looper)
|
||||
mHandler = new InternalHandler(Looper.getMainLooper());
|
||||
@@ -124,17 +127,17 @@ public class ADsControlView extends LinearLayout {
|
||||
registerControlView(this);
|
||||
|
||||
// 从SP读取初始模式并设置
|
||||
ToastUtils.show(String.format("savedMode : %s", getADsMode().name()));
|
||||
//ToastUtils.show(String.format("savedMode : %s", getADsMode().name()));
|
||||
setSelectedMode(getADsMode());
|
||||
|
||||
// 单选组选择事件监听
|
||||
rgAdsMode.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
|
||||
rgADsMode.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(RadioGroup group, int checkedId) {
|
||||
if (checkedId == R.id.rb_standalone) {
|
||||
setADsMode(ADsMode.STANDALONE);
|
||||
} else if (checkedId == R.id.rb_mimo_sdk) {
|
||||
showPrivacy(context, new OnPrivacyChangeListener(){
|
||||
handlePrivacyLogic((Activity)context, PrivacyAgreeStatus.UN_SIGNED , new OnPrivacyChangeListener(){
|
||||
@Override
|
||||
public void onAgreePrivacy() {
|
||||
setADsMode(ADsMode.MIMO_SDK);
|
||||
@@ -154,31 +157,29 @@ public class ADsControlView extends LinearLayout {
|
||||
* 【静态】显示隐私协议弹窗(供外部调用,带Context参数)
|
||||
* @param context 上下文(需传入Activity Context,用于弹窗显示)
|
||||
*/
|
||||
public static void showPrivacy(Context context, OnPrivacyChangeListener onPrivacyChangeListener) {
|
||||
if (context == null) {
|
||||
LogUtils.e(TAG, "showPrivacy: Context is null, cannot show privacy dialog");
|
||||
return;
|
||||
}
|
||||
// 校验是否为Activity Context(弹窗必须依附Activity)
|
||||
Activity activity = null;
|
||||
try {
|
||||
activity = (Activity) context;
|
||||
} catch (ClassCastException e) {
|
||||
LogUtils.e(TAG, "showPrivacy: Context is not Activity Context", e);
|
||||
Toast.makeText(context.getApplicationContext(), "请传入Activity上下文以显示隐私协议", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
// 校验Activity状态
|
||||
if (activity.isFinishing() || activity.isDestroyed()) {
|
||||
LogUtils.e(TAG, "showPrivacy: Activity is finishing or destroyed");
|
||||
return;
|
||||
}
|
||||
|
||||
// 读取隐私协议状态并处理逻辑
|
||||
SbhhharedPreferences sp = getPrivacySharedPreferences(context);
|
||||
String privacyAgreeValue = sp.getString(PRIVACY_VALUE, null);
|
||||
handlePrivacyLogic(activity, privacyAgreeValue, onPrivacyChangeListener);
|
||||
}
|
||||
// public void showPrivacy(OnPrivacyChangeListener onPrivacyChangeListener) {
|
||||
// if (context == null) {
|
||||
// LogUtils.e(TAG, "showPrivacy: Context is null, cannot show privacy dialog");
|
||||
// return;
|
||||
// }
|
||||
// // 校验是否为Activity Context(弹窗必须依附Activity)
|
||||
// Activity activity = null;
|
||||
// try {
|
||||
// activity = (Activity) context;
|
||||
// } catch (ClassCastException e) {
|
||||
// LogUtils.e(TAG, "showPrivacy: Context is not Activity Context", e);
|
||||
// Toast.makeText(context.getApplicationContext(), "请传入Activity上下文以显示隐私协议", Toast.LENGTH_SHORT).show();
|
||||
// return;
|
||||
// }
|
||||
// // 校验Activity状态
|
||||
// if (activity.isFinishing() || activity.isDestroyed()) {
|
||||
// LogUtils.e(TAG, "showPrivacy: Activity is finishing or destroyed");
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// // 读取隐私协议状态并处理逻辑
|
||||
// handlePrivacyLogic(activity, getPrivacyAgreeStatus(), onPrivacyChangeListener);
|
||||
// }
|
||||
|
||||
/**
|
||||
* 【静态】清理SP中存储的隐私协议状态(PRIVACY_VALUE)
|
||||
@@ -204,21 +205,21 @@ public class ADsControlView extends LinearLayout {
|
||||
// 使用ApplicationContext获取SP,避免内存泄漏
|
||||
Context appContext = context.getApplicationContext();
|
||||
if (appContext != null) {
|
||||
return appContext.getSharedPreferences(PRIVACY_FILE, Context.MODE_PRIVATE);
|
||||
return appContext.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
|
||||
}
|
||||
// 降级方案:若ApplicationContext为空,使用传入的Context
|
||||
return context.getSharedPreferences(PRIVACY_FILE, Context.MODE_PRIVATE);
|
||||
return context.getSharedPreferences(PRIVACY_VALUE, Context.MODE_PRIVATE);
|
||||
}
|
||||
|
||||
// 【配套静态工具方法】隐私协议逻辑处理(供上述两个静态方法调用,需一并添加)
|
||||
private static void handlePrivacyLogic(final Activity activity, String privacyAgreeValue, final OnPrivacyChangeListener onPrivacyChangeListener) {
|
||||
if (TextUtils.equals(privacyAgreeValue, String.valueOf(0))) {
|
||||
ADsControlView.updateAdsModeByStatic(activity.getApplicationContext(), ADsMode.STANDALONE);
|
||||
private static void handlePrivacyLogic(final Activity activity, PrivacyAgreeStatus privacyAgreeStatus, final OnPrivacyChangeListener onPrivacyChangeListener) {
|
||||
if (privacyAgreeStatus == PrivacyAgreeStatus.REJECTED) {
|
||||
//ADsControlView.updateAdsModeByStatic(activity.getApplicationContext(), ADsMode.STANDALONE);
|
||||
LogUtils.i(TAG, "已拒绝隐私协议,广告已处于不可用状态...");
|
||||
Toast.makeText(activity.getApplicationContext(), "已拒绝隐私协议,广告已处于不可用状态", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
} else if (TextUtils.equals(privacyAgreeValue, String.valueOf(1))) {
|
||||
ADsControlView.updateAdsModeByStatic(activity.getApplicationContext(), ADsMode.MIMO_SDK);
|
||||
} else if (privacyAgreeStatus == PrivacyAgreeStatus.AGREED) {
|
||||
//ADsControlView.updateAdsModeByStatic(activity.getApplicationContext(), ADsMode.MIMO_SDK);
|
||||
LogUtils.i(TAG, "已同意隐私协议,开始初始化米盟SDK...");
|
||||
initMimoSdkStatic(activity.getApplicationContext());
|
||||
return;
|
||||
@@ -382,7 +383,7 @@ public class ADsControlView extends LinearLayout {
|
||||
if (mode2 == ADsMode.STANDALONE) {
|
||||
rbStandalone.setChecked(true);
|
||||
} else if (mode2 == ADsMode.MIMO_SDK) {
|
||||
rbMimoSdk.setChecked(true);
|
||||
rbMimoSDK.setChecked(true);
|
||||
}
|
||||
} else {
|
||||
mHandler.post(new Runnable() {
|
||||
@@ -398,7 +399,7 @@ public class ADsControlView extends LinearLayout {
|
||||
* 获取当前选中模式
|
||||
*/
|
||||
public ADsMode getSelectedMode() {
|
||||
int checkedId = rgAdsMode.getCheckedRadioButtonId();
|
||||
int checkedId = rgADsMode.getCheckedRadioButtonId();
|
||||
return checkedId == R.id.rb_mimo_sdk ? ADsMode.MIMO_SDK : ADsMode.STANDALONE;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user