Merge branch 'aes' into winboll
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
# AES
|
||||
|
||||
#### 介绍
|
||||
安卓视图元素类库
|
||||
WinBoLL 安卓可视化元素类库测试应用。
|
||||
|
||||
#### 软件架构
|
||||
适配安卓应用 [AIDE Pro] 的 Gradle 编译结构。
|
||||
@@ -32,4 +32,4 @@
|
||||
5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help)
|
||||
6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)
|
||||
|
||||
#### 参考文档
|
||||
#### 参考文档
|
||||
|
||||
@@ -38,12 +38,10 @@ android {
|
||||
versionName = genVersionName("${versionName}")
|
||||
}
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
|
||||
// 米盟 SDK
|
||||
packagingOptions {
|
||||
doNotStrip "*/*/libmimo_1011.so"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#Created by .winboll/winboll_app_build.gradle
|
||||
#Wed Nov 19 09:04:33 HKT 2025
|
||||
stageCount=5
|
||||
#Wed Nov 26 15:54:26 GMT 2025
|
||||
stageCount=7
|
||||
libraryProject=libaes
|
||||
baseVersion=15.11
|
||||
publishVersion=15.11.4
|
||||
buildCount=0
|
||||
baseBetaVersion=15.11.5
|
||||
publishVersion=15.11.6
|
||||
buildCount=32
|
||||
baseBetaVersion=15.11.7
|
||||
|
||||
132
aes/proguard-rules.pro
vendored
132
aes/proguard-rules.pro
vendored
@@ -9,9 +9,129 @@
|
||||
|
||||
# Add any project specific keep options here:
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
# ============================== 基础通用规则 ==============================
|
||||
# 保留系统组件
|
||||
-keep public class * extends android.app.Activity
|
||||
-keep public class * extends android.app.Service
|
||||
-keep public class * extends android.content.BroadcastReceiver
|
||||
-keep public class * extends android.content.ContentProvider
|
||||
-keep public class * extends android.app.backup.BackupAgentHelper
|
||||
-keep public class * extends android.preference.Preference
|
||||
|
||||
# 保留 WinBoLL 核心包及子类(合并简化规则)
|
||||
-keep class cc.winboll.studio.** { *; }
|
||||
-keepclassmembers class cc.winboll.studio.** { *; }
|
||||
|
||||
# 保留所有类中的 public static final String TAG 字段(便于日志定位)
|
||||
-keepclassmembers class * {
|
||||
public static final java.lang.String TAG;
|
||||
}
|
||||
|
||||
# 保留序列化类(避免Parcelable/Gson解析异常)
|
||||
-keep class * implements android.os.Parcelable {
|
||||
public static final android.os.Parcelable$Creator *;
|
||||
}
|
||||
-keepclassmembers class * implements java.io.Serializable {
|
||||
static final long serialVersionUID;
|
||||
private static final java.io.ObjectStreamField[] serialPersistentFields;
|
||||
private void writeObject(java.io.ObjectOutputStream);
|
||||
private void readObject(java.io.ObjectInputStream);
|
||||
java.lang.Object writeReplace();
|
||||
java.lang.Object readResolve();
|
||||
}
|
||||
|
||||
# 保留 R 文件(避免资源ID混淆)
|
||||
-keepclassmembers class **.R$* {
|
||||
public static <fields>;
|
||||
}
|
||||
|
||||
# 保留 native 方法(避免JNI调用失败)
|
||||
-keepclasseswithmembernames class * {
|
||||
native <methods>;
|
||||
}
|
||||
|
||||
# 保留注解和泛型(避免反射/序列化异常)
|
||||
-keepattributes *Annotation*
|
||||
-keepattributes Signature
|
||||
|
||||
# 屏蔽 Java 8+ 警告(适配 Java 7 语法)
|
||||
-dontwarn java.lang.invoke.*
|
||||
-dontwarn android.support.v8.renderscript.*
|
||||
-dontwarn java.util.function.**
|
||||
|
||||
# ============================== 第三方框架专项规则 ==============================
|
||||
# OkHttp 4.4.1(米盟广告请求依赖,完善Lambda兼容)
|
||||
-keep class okhttp3.** { *; }
|
||||
-keep interface okhttp3.** { *; }
|
||||
-keep class okhttp3.internal.** { *; }
|
||||
-keep class okio.** { *; }
|
||||
-dontwarn okhttp3.internal.platform.**
|
||||
-dontwarn okio.**
|
||||
|
||||
# Glide 4.9.0(米盟广告图片加载依赖)
|
||||
-keep public class * implements com.bumptech.glide.module.GlideModule
|
||||
-keep public class * extends com.bumptech.glide.module.AppGlideModule
|
||||
-keep public enum com.bumptech.glide.load.ImageHeaderParser$ImageType {
|
||||
**[] $VALUES;
|
||||
public *;
|
||||
}
|
||||
-keepclassmembers class * implements com.bumptech.glide.module.AppGlideModule {
|
||||
<init>();
|
||||
}
|
||||
-dontwarn com.bumptech.glide.**
|
||||
|
||||
# Gson 2.8.5(米盟广告数据序列化依赖)
|
||||
-keep class com.google.gson.** { *; }
|
||||
-keep interface com.google.gson.** { *; }
|
||||
-keepclassmembers class * {
|
||||
@com.google.gson.annotations.SerializedName <fields>;
|
||||
}
|
||||
|
||||
# 米盟 SDK(核心广告组件,完整保留避免加载失败)
|
||||
-keep class com.miui.zeus.** { *; }
|
||||
-keep interface com.miui.zeus.** { *; }
|
||||
# 保留米盟日志字段(便于广告加载失败排查)
|
||||
-keepclassmembers class com.miui.zeus.mimo.sdk.** {
|
||||
public static final java.lang.String TAG;
|
||||
}
|
||||
|
||||
# RecyclerView 1.0.0(米盟广告布局渲染依赖)
|
||||
-keep class androidx.recyclerview.** { *; }
|
||||
-keep interface androidx.recyclerview.** { *; }
|
||||
-keepclassmembers class androidx.recyclerview.widget.RecyclerView$Adapter {
|
||||
public *;
|
||||
}
|
||||
|
||||
# 其他第三方框架(按引入依赖保留,无则可删除)
|
||||
# XXPermissions 18.63
|
||||
-keep class com.hjq.permissions.** { *; }
|
||||
-keep interface com.hjq.permissions.** { *; }
|
||||
|
||||
# ZXing 二维码(核心解析组件)
|
||||
-keep class com.google.zxing.** { *; }
|
||||
-keep class com.journeyapps.zxing.** { *; }
|
||||
|
||||
# Jsoup HTML解析
|
||||
-keep class org.jsoup.** { *; }
|
||||
|
||||
# Pinyin4j 拼音搜索
|
||||
-keep class net.sourceforge.pinyin4j.** { *; }
|
||||
|
||||
# JSch SSH组件
|
||||
-keep class com.jcraft.jsch.** { *; }
|
||||
|
||||
# AndroidX 基础组件
|
||||
-keep class androidx.appcompat.** { *; }
|
||||
-keep interface androidx.appcompat.** { *; }
|
||||
|
||||
# ============================== 优化与调试配置 ==============================
|
||||
# 优化级别(平衡混淆效果与性能)
|
||||
-optimizationpasses 5
|
||||
-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*
|
||||
|
||||
# 调试辅助(保留行号便于崩溃定位)
|
||||
-verbose
|
||||
-dontpreverify
|
||||
-dontusemixedcaseclassnames
|
||||
-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
|
||||
@@ -35,6 +35,8 @@
|
||||
|
||||
<activity android:name=".TestActivityManagerActivity"/>
|
||||
|
||||
<activity android:name=".SettingsActivity"/>
|
||||
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@@ -83,11 +83,11 @@ public class AboutActivity extends WinBoLLActivity implements IWinBoLLActivity {
|
||||
appInfo.setAppGitOwner("Studio");
|
||||
appInfo.setAppGitAPPBranch(szBranchName);
|
||||
appInfo.setAppGitAPPSubProjectFolder(szBranchName);
|
||||
appInfo.setAppHomePage("https://discuz.winboll.cc/forum.php?mod=viewthread&tid=3&extra=page%3D1");
|
||||
appInfo.setAppHomePage("https://www.winboll.cc/apks/index.php?project=AES");
|
||||
appInfo.setAppAPKName("AES");
|
||||
appInfo.setAppAPKFolderName("AES");
|
||||
//appInfo.setIsAddDebugTools(false);
|
||||
appInfo.setIsAddDebugTools(BuildConfig.DEBUG);
|
||||
//appInfo.setIsAddDebugTools(BuildConfig.DEBUG);
|
||||
return new AboutView(mContext, appInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ public class MainActivity extends DrawerFragmentActivity implements IWinBoLLActi
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.toolbar_library, menu);
|
||||
getMenuInflater().inflate(R.menu.toolbar_main, menu);
|
||||
if(App.isDebugging()) {
|
||||
getMenuInflater().inflate(cc.winboll.studio.libaes.R.menu.toolbar_studio_debug, menu);
|
||||
}
|
||||
@@ -185,8 +185,10 @@ public class MainActivity extends DrawerFragmentActivity implements IWinBoLLActi
|
||||
} else if (nItemId == R.id.item_drawerfragmentactivity) {
|
||||
Intent intent = new Intent(this, TestDrawerFragmentActivity.class);
|
||||
startActivity(intent);
|
||||
}
|
||||
else if (nItemId == R.id.item_about) {
|
||||
} else if (nItemId == R.id.item_settings) {
|
||||
Intent intent = new Intent(this, SettingsActivity.class);
|
||||
startActivity(intent);
|
||||
} else if (nItemId == R.id.item_about) {
|
||||
Intent intent = new Intent(this, AboutActivity.class);
|
||||
startActivity(intent);
|
||||
return true;
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package cc.winboll.studio.aes;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import cc.winboll.studio.libaes.enums.ADsMode;
|
||||
import cc.winboll.studio.libaes.views.ADsControlView;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/11/26 18:01
|
||||
* @Describe SettingsActivity
|
||||
*/
|
||||
public class SettingsActivity extends Activity {
|
||||
|
||||
public static final String TAG = "SettingsActivity";
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_settings);
|
||||
ADsControlView adsControlView = (ADsControlView) findViewById(R.id.ads_control_view);
|
||||
|
||||
// adsControlView.setOnAdsModeSelectedListener(new ADsControlView.OnAdsModeSelectedListener() {
|
||||
// @Override
|
||||
// public void onModeSelected(ADsMode selectedMode) {
|
||||
// if (selectedMode == ADsMode.STANDALONE) {
|
||||
// // 处理单机模式逻辑(如释放米盟资源)
|
||||
// ToastUtils.show("STANDALONE");
|
||||
// } else if (selectedMode == ADsMode.MIMO_SDK) {
|
||||
// // 处理米盟SDK模式逻辑(如初始化SDK)
|
||||
// ToastUtils.show("MIMO_SDK");
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
}
|
||||
|
||||
}
|
||||
12
aes/src/main/res/layout/activity_settings.xml
Normal file
12
aes/src/main/res/layout/activity_settings.xml
Normal file
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
<cc.winboll.studio.libaes.views.ADsControlView
|
||||
android:id="@+id/ads_control_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
</LinearLayout>
|
||||
@@ -32,4 +32,7 @@
|
||||
<item
|
||||
android:id="@+id/item_drawerfragmentactivity"
|
||||
android:title="Test DrawerFragmentActivity"/>
|
||||
<item
|
||||
android:id="@+id/item_settings"
|
||||
android:title="Settings"/>
|
||||
</menu>
|
||||
@@ -15,12 +15,18 @@ android {
|
||||
minSdkVersion 23
|
||||
targetSdkVersion 30
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
|
||||
// 米盟 SDK
|
||||
packagingOptions {
|
||||
doNotStrip "*/*/libmimo_1011.so"
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
@@ -51,12 +57,12 @@ dependencies {
|
||||
//api 'androidx.fragment:fragment:1.1.0'
|
||||
|
||||
// 米盟
|
||||
implementation 'com.miui.zeus:mimo-ad-sdk:5.3.+'//请使用最新版sdk
|
||||
api 'com.miui.zeus:mimo-ad-sdk:5.3.+'//请使用最新版sdk
|
||||
//注意:以下5个库必须要引入
|
||||
//implementation 'androidx.appcompat:appcompat:1.4.1'
|
||||
implementation 'androidx.recyclerview:recyclerview:1.0.0'
|
||||
implementation 'com.google.code.gson:gson:2.8.5'
|
||||
implementation 'com.github.bumptech.glide:glide:4.9.0'
|
||||
api 'androidx.recyclerview:recyclerview:1.0.0'
|
||||
api 'com.google.code.gson:gson:2.8.5'
|
||||
api 'com.github.bumptech.glide:glide:4.9.0'
|
||||
//annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
|
||||
|
||||
api 'cc.winboll.studio:libappbase:15.11.0'
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#Created by .winboll/winboll_app_build.gradle
|
||||
#Wed Nov 19 09:04:27 HKT 2025
|
||||
stageCount=5
|
||||
#Wed Nov 26 15:54:26 GMT 2025
|
||||
stageCount=7
|
||||
libraryProject=libaes
|
||||
baseVersion=15.11
|
||||
publishVersion=15.11.4
|
||||
buildCount=0
|
||||
baseBetaVersion=15.11.5
|
||||
publishVersion=15.11.6
|
||||
buildCount=32
|
||||
baseBetaVersion=15.11.7
|
||||
|
||||
@@ -203,7 +203,7 @@ public abstract class DrawerFragmentActivity extends AppCompatActivity implement
|
||||
|
||||
ADsBannerView adsBannerView = findViewById(R.id.adsbanner);
|
||||
if (adsBannerView != null) {
|
||||
adsBannerView.resumeADs();
|
||||
adsBannerView.resumeADs(DrawerFragmentActivity.this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package cc.winboll.studio.libaes.enums;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/11/26 17:49
|
||||
* @Describe 广告控制模式枚举
|
||||
*/
|
||||
public enum ADsMode {
|
||||
STANDALONE("单机模式"), // 单机模式(默认)
|
||||
MIMO_SDK("米盟广告SDK支持模式"); // 米盟广告SDK模式
|
||||
|
||||
private final String modeName;
|
||||
|
||||
ADsMode(String modeName) {
|
||||
this.modeName = modeName;
|
||||
}
|
||||
|
||||
public String getModeName() {
|
||||
return modeName;
|
||||
}
|
||||
|
||||
// 根据保存的字符串值解析枚举(SP读取时使用)
|
||||
public static ADsMode fromValue(String value) {
|
||||
if (value == null) return STANDALONE;
|
||||
try {
|
||||
return ADsMode.valueOf(value);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return STANDALONE;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,7 @@ 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>
|
||||
@@ -41,8 +42,6 @@ public class ADsBannerView extends LinearLayout {
|
||||
|
||||
public static final String TAG = "ADsBannerView";
|
||||
|
||||
private static final String PRIVACY_FILE = "privacy_pfs";
|
||||
private static final String PRIVACY_VALUE = "privacy_value";//0: 拒绝,1:赞同
|
||||
|
||||
private String BANNER_POS_ID = "802e356f1726f9ff39c69308bfd6f06a";
|
||||
private String BANNER_POS_ID_WINBOLL_BETA = "d129ee5a263911f981a6dc7a9802e3e7";
|
||||
@@ -65,63 +64,53 @@ public class ADsBannerView extends LinearLayout {
|
||||
|
||||
public ADsBannerView(Context context) {
|
||||
super(context);
|
||||
this.mContext = context;
|
||||
initView();
|
||||
initView(context);
|
||||
}
|
||||
|
||||
public ADsBannerView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
this.mContext = context;
|
||||
initView();
|
||||
initView(context);
|
||||
}
|
||||
|
||||
public ADsBannerView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
this.mContext = context;
|
||||
initView();
|
||||
initView(context);
|
||||
}
|
||||
|
||||
public ADsBannerView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
|
||||
super(context, attrs, defStyleAttr, defStyleRes);
|
||||
this.mContext = context;
|
||||
initView();
|
||||
initView(context);
|
||||
}
|
||||
|
||||
void initView() {
|
||||
|
||||
// 初始化主线程Handler(关键:确保广告操作在主线程执行)
|
||||
mMainHandler = new Handler(Looper.getMainLooper());
|
||||
|
||||
// 米盟模块:隐私协议弹窗
|
||||
showPrivacy();
|
||||
void initView(Context context) {
|
||||
this.mContext = context;
|
||||
|
||||
initMimoSdk(this.mContext);
|
||||
|
||||
// 初始化主线程Handler(关键:确保广告操作在主线程执行)
|
||||
mMainHandler = new Handler(Looper.getMainLooper());
|
||||
|
||||
this.mMianView = inflate(this.mContext, R.layout.view_adsbanner, null);
|
||||
mContainer = this.mMianView.findViewById(R.id.ads_container);
|
||||
addView(this.mMianView);
|
||||
}
|
||||
|
||||
Activity getActivity() {
|
||||
try {
|
||||
Activity activity = (Activity)this.mContext;
|
||||
return activity;
|
||||
} catch (Exception ex) {
|
||||
public void resumeADs(final Activity activity) {
|
||||
// 没有设置米盟广告支持就退出
|
||||
if (ADsControlView.getAdsModeFromStatic(this.mContext) != ADsMode.MIMO_SDK) {
|
||||
return;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void resumeADs() {
|
||||
|
||||
// 修复:优化广告请求逻辑(添加生命周期判断 + 主线程执行)
|
||||
if (getActivity() != null && !getActivity().isFinishing() && !getActivity().isDestroyed()) {
|
||||
String privacyAgreeValue = getSharedPreferences().getString(PRIVACY_VALUE, null);
|
||||
if (TextUtils.equals(privacyAgreeValue, String.valueOf(1))) {
|
||||
LogUtils.i(TAG, "已同意隐私协议,开始播放米盟广告...");
|
||||
if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
|
||||
if (ADsControlView.getAdsModeFromStatic(this.mContext) == ADsMode.MIMO_SDK) {
|
||||
LogUtils.i(TAG, "已设置播放米盟广告,正在播放...");
|
||||
mMainHandler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
//ToastUtils.show("ADs run");
|
||||
// 再次校验生命周期,避免延迟执行时Activity已销毁
|
||||
if (getActivity() != null && !getActivity().isFinishing() && !getActivity().isDestroyed()) {
|
||||
fetchAd();
|
||||
if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
|
||||
fetchAd(activity);
|
||||
}
|
||||
}
|
||||
}, 1000); // 延迟1秒请求广告,提升页面加载体验
|
||||
@@ -133,6 +122,11 @@ public class ADsBannerView extends LinearLayout {
|
||||
* 释放广告资源(关键:避免内存泄漏和空Context调用)
|
||||
*/
|
||||
public void releaseAdResources() {
|
||||
// 没有设置米盟广告支持就退出
|
||||
if (ADsControlView.getAdsModeFromStatic(this.mContext) != ADsMode.MIMO_SDK) {
|
||||
return;
|
||||
}
|
||||
|
||||
LogUtils.d(TAG, "releaseAdResources()");
|
||||
|
||||
// 移除Handler回调
|
||||
@@ -160,10 +154,15 @@ public class ADsBannerView extends LinearLayout {
|
||||
/**
|
||||
* 显示广告(核心修复:传递安全的Context + 生命周期校验)
|
||||
*/
|
||||
private void showAd() {
|
||||
private void showAd(final Activity activity) {
|
||||
// 没有设置米盟广告支持就退出
|
||||
if (ADsControlView.getAdsModeFromStatic(this.mContext) != ADsMode.MIMO_SDK) {
|
||||
return;
|
||||
}
|
||||
|
||||
LogUtils.d(TAG, "showAd()");
|
||||
// 1. 生命周期校验:避免Activity已销毁时操作UI
|
||||
if (getActivity() == null || getActivity().isFinishing() || getActivity().isDestroyed()) {
|
||||
if (activity == null || activity.isFinishing() || activity.isDestroyed()) {
|
||||
LogUtils.e(TAG, "showAd: Activity is finishing or destroyed");
|
||||
return;
|
||||
}
|
||||
@@ -173,8 +172,8 @@ public class ADsBannerView extends LinearLayout {
|
||||
return;
|
||||
}
|
||||
// 3. 创建广告容器(使用ApplicationContext避免内存泄漏)
|
||||
final FrameLayout container = new FrameLayout(getActivity().getApplicationContext());
|
||||
container.setPadding(0, 0, 0, MimoUtils.dpToPx(getActivity(), 10));
|
||||
final FrameLayout container = new FrameLayout(activity.getApplicationContext());
|
||||
container.setPadding(0, 0, 0, MimoUtils.dpToPx(activity, 10));
|
||||
mContainer.addView(container, new FrameLayout.LayoutParams(
|
||||
FrameLayout.LayoutParams.MATCH_PARENT,
|
||||
FrameLayout.LayoutParams.WRAP_CONTENT
|
||||
@@ -184,7 +183,7 @@ public class ADsBannerView extends LinearLayout {
|
||||
// mBannerAd.setPrice(getPrice());
|
||||
// }
|
||||
// 4. 显示广告:传递ApplicationContext,避免Activity Context失效
|
||||
mBannerAd.showAd(getActivity(), container, new BannerAd.BannerInteractionListener() {
|
||||
mBannerAd.showAd(activity, container, new BannerAd.BannerInteractionListener() {
|
||||
@Override
|
||||
public void onAdClick() {
|
||||
LogUtils.d(TAG, "onAdClick");
|
||||
@@ -199,7 +198,7 @@ public class ADsBannerView extends LinearLayout {
|
||||
public void onAdDismiss() {
|
||||
LogUtils.d(TAG, "onAdDismiss");
|
||||
// 修复:移除容器时校验Activity状态
|
||||
if (getActivity() != null && !getActivity().isFinishing() && !getActivity().isDestroyed() && mContainer != null) {
|
||||
if (activity != null && !activity.isFinishing() && !activity.isDestroyed() && mContainer != null) {
|
||||
mContainer.removeView(container);
|
||||
}
|
||||
}
|
||||
@@ -213,7 +212,7 @@ public class ADsBannerView extends LinearLayout {
|
||||
public void onRenderFail(int code, String msg) {
|
||||
LogUtils.e(TAG, "onRenderFail errorCode " + code + " errorMsg " + msg);
|
||||
// 修复:渲染失败时移除容器
|
||||
if (getActivity() != null && !getActivity().isFinishing() && !getActivity().isDestroyed() && mContainer != null) {
|
||||
if (activity != null && !activity.isFinishing() && !activity.isDestroyed() && mContainer != null) {
|
||||
mContainer.removeView(container);
|
||||
}
|
||||
}
|
||||
@@ -223,10 +222,15 @@ public class ADsBannerView extends LinearLayout {
|
||||
/**
|
||||
* 请求广告(核心修复:Context安全校验 + 异常捕获 + 资源管理)
|
||||
*/
|
||||
private void fetchAd() {
|
||||
private void fetchAd(final Activity activity) {
|
||||
// 没有设置米盟广告支持就退出
|
||||
if (ADsControlView.getAdsModeFromStatic(this.mContext) != ADsMode.MIMO_SDK) {
|
||||
return;
|
||||
}
|
||||
|
||||
LogUtils.d(TAG, "fetchAd()");
|
||||
// 1. 双重校验:Activity未销毁 + Context非空
|
||||
if (getActivity() == null || getActivity().isFinishing() || getActivity().isDestroyed() || getActivity().getApplicationContext() == null) {
|
||||
if (activity == null || activity.isFinishing() || activity.isDestroyed() || activity.getApplicationContext() == null) {
|
||||
LogUtils.e(TAG, "fetchAd: Invalid Context or Activity state");
|
||||
return;
|
||||
}
|
||||
@@ -301,8 +305,9 @@ public class ADsBannerView extends LinearLayout {
|
||||
public void onBannerAdLoadSuccess() {
|
||||
LogUtils.d(TAG, "onBannerAdLoadSuccess()");
|
||||
// 修复:广告加载成功后校验Activity状态
|
||||
if (getActivity() != null && !getActivity().isFinishing() && !getActivity().isDestroyed()) {
|
||||
showAd();
|
||||
if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
|
||||
showAd(activity);
|
||||
//ToastUtils.show("showAd()");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -316,6 +321,11 @@ public class ADsBannerView extends LinearLayout {
|
||||
}
|
||||
|
||||
void removeAllBanners() {
|
||||
// 没有设置米盟广告支持就退出
|
||||
if (ADsControlView.getAdsModeFromStatic(this.mContext) != ADsMode.MIMO_SDK) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 修复:加载失败时移除当前广告实例
|
||||
if (mAllBanners.contains(mBannerAd)) {
|
||||
mAllBanners.remove(mBannerAd);
|
||||
@@ -336,91 +346,94 @@ public class ADsBannerView extends LinearLayout {
|
||||
/**
|
||||
* 获取广告价格(原逻辑保留,添加空指针校验)
|
||||
*/
|
||||
private long getPrice() {
|
||||
if (mBannerAd == null) {
|
||||
return 0;
|
||||
}
|
||||
Map<String, Object> map = mBannerAd.getMediaExtraInfo();
|
||||
if (map == null || map.isEmpty() || !map.containsKey("price")) {
|
||||
LogUtils.w(TAG, "getPrice: media extra info is null or no price key");
|
||||
return 0;
|
||||
}
|
||||
Object priceObj = map.get("price");
|
||||
if (priceObj instanceof Long) {
|
||||
return (Long) priceObj;
|
||||
} else if (priceObj instanceof Integer) {
|
||||
return ((Integer) priceObj).longValue();
|
||||
} else {
|
||||
LogUtils.e(TAG, "getPrice: price type is invalid");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
// private long getPrice() {
|
||||
// if (mBannerAd == null) {
|
||||
// return 0;
|
||||
// }
|
||||
// Map<String, Object> map = mBannerAd.getMediaExtraInfo();
|
||||
// if (map == null || map.isEmpty() || !map.containsKey("price")) {
|
||||
// LogUtils.w(TAG, "getPrice: media extra info is null or no price key");
|
||||
// return 0;
|
||||
// }
|
||||
// Object priceObj = map.get("price");
|
||||
// if (priceObj instanceof Long) {
|
||||
// return (Long) priceObj;
|
||||
// } else if (priceObj instanceof Integer) {
|
||||
// return ((Integer) priceObj).longValue();
|
||||
// } else {
|
||||
// LogUtils.e(TAG, "getPrice: price type is invalid");
|
||||
// return 0;
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* 显示隐私协议弹窗(原逻辑保留,优化Context使用)
|
||||
*/
|
||||
private void showPrivacy() {
|
||||
// 校验Activity状态,避免弹窗泄露
|
||||
if (getActivity() == null || getActivity().isFinishing() || getActivity().isDestroyed()) {
|
||||
return;
|
||||
}
|
||||
String privacyAgreeValue = getSharedPreferences().getString(PRIVACY_VALUE, null);
|
||||
if (TextUtils.equals(privacyAgreeValue, String.valueOf(0))) {
|
||||
LogUtils.i(TAG, "已拒绝隐私协议,广告已处于不可用状态...");
|
||||
Toast.makeText(getActivity().getApplicationContext(), "已拒绝隐私协议,广告已处于不可用状态", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
if (TextUtils.equals(privacyAgreeValue, String.valueOf(1))) {
|
||||
LogUtils.i(TAG, "已同意隐私协议,开始初始化米盟SDK...");
|
||||
initMimoSdk();
|
||||
return;
|
||||
}
|
||||
LogUtils.i(TAG, "开始弹出隐私协议...");
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
||||
builder.setTitle("用户须知");
|
||||
builder.setMessage("小米广告SDK隐私政策: https://dev.mi.com/distribute/doc/details?pId=1688, 请复制到浏览器查看");
|
||||
builder.setIcon(R.drawable.ic_launcher);
|
||||
builder.setCancelable(false); // 点击对话框以外的区域不消失
|
||||
builder.setPositiveButton("同意", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
getSharedPreferences().edit()
|
||||
.putString(PRIVACY_VALUE, String.valueOf(1))
|
||||
.apply();
|
||||
initMimoSdk();
|
||||
dialog.dismiss();
|
||||
}
|
||||
});
|
||||
builder.setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
getSharedPreferences().edit()
|
||||
.putString(PRIVACY_VALUE, String.valueOf(0))
|
||||
.apply();
|
||||
dialog.dismiss();
|
||||
}
|
||||
});
|
||||
AlertDialog dialog = builder.create();
|
||||
|
||||
// 配置弹窗位置(底部全屏)
|
||||
Window window = dialog.getWindow();
|
||||
if (window != null) {
|
||||
window.setGravity(Gravity.BOTTOM);
|
||||
WindowManager m = getActivity().getWindowManager();
|
||||
Display d = m.getDefaultDisplay();
|
||||
WindowManager.LayoutParams p = window.getAttributes();
|
||||
p.width = d.getWidth();
|
||||
window.setAttributes(p);
|
||||
}
|
||||
dialog.show();
|
||||
}
|
||||
// private void showPrivacy() {
|
||||
// // 校验Activity状态,避免弹窗泄露
|
||||
// if (getActivity() == null || getActivity().isFinishing() || getActivity().isDestroyed()) {
|
||||
// return;
|
||||
// }
|
||||
// ADsMode adsMode = ADsControlView.getAdsModeFromStatic(this.mContext);
|
||||
// if (adsMode == ADsMode.STANDALONE) {
|
||||
// ADsControlView.updateAdsModeByStatic(this.mContext, ADsMode.STANDALONE);
|
||||
// LogUtils.i(TAG, "单机模式,广告已处于不可用状态...");
|
||||
// Toast.makeText(getActivity().getApplicationContext(), "单机模式,广告已处于不可用状态...", Toast.LENGTH_SHORT).show();
|
||||
// return;
|
||||
// } else if (adsMode == ADsMode.MIMO_SDK) {
|
||||
// ADsControlView.updateAdsModeByStatic(this.mContext, ADsMode.MIMO_SDK);
|
||||
// LogUtils.i(TAG, "米盟广告SDK支持模式,现在初始化SDK...");
|
||||
// initMimoSdk();
|
||||
// return;
|
||||
// }
|
||||
// else {
|
||||
// LogUtils.i(TAG, "开始弹出隐私协议...");
|
||||
// AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
||||
// builder.setTitle("用户须知");
|
||||
// builder.setMessage("小米广告SDK隐私政策: https://dev.mi.com/distribute/doc/details?pId=1688, 请复制到浏览器查看");
|
||||
// builder.setIcon(R.drawable.ic_launcher);
|
||||
// builder.setCancelable(false); // 点击对话框以外的区域不消失
|
||||
// builder.setPositiveButton("同意", new DialogInterface.OnClickListener() {
|
||||
// @Override
|
||||
// public void onClick(DialogInterface dialog, int which) {
|
||||
// getSharedPreferences().edit()
|
||||
// .putString(PRIVACY_VALUE, String.valueOf(1))
|
||||
// .apply();
|
||||
// initMimoSdk();
|
||||
// dialog.dismiss();
|
||||
// }
|
||||
// });
|
||||
// builder.setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
|
||||
// @Override
|
||||
// public void onClick(DialogInterface dialog, int which) {
|
||||
// getSharedPreferences().edit()
|
||||
// .putString(PRIVACY_VALUE, String.valueOf(0))
|
||||
// .apply();
|
||||
// dialog.dismiss();
|
||||
// }
|
||||
// });
|
||||
// AlertDialog dialog = builder.create();
|
||||
//
|
||||
// // 配置弹窗位置(底部全屏)
|
||||
// Window window = dialog.getWindow();
|
||||
// if (window != null) {
|
||||
// window.setGravity(Gravity.BOTTOM);
|
||||
// WindowManager m = getActivity().getWindowManager();
|
||||
// Display d = m.getDefaultDisplay();
|
||||
// WindowManager.LayoutParams p = window.getAttributes();
|
||||
// p.width = d.getWidth();
|
||||
// window.setAttributes(p);
|
||||
// }
|
||||
// dialog.show();
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* 初始化米盟SDK(核心修复:传递ApplicationContext + 异常捕获)
|
||||
*/
|
||||
private void initMimoSdk() {
|
||||
private void initMimoSdk(Context context) {
|
||||
// 1. 安全获取ApplicationContext,避免Activity Context失效
|
||||
Context appContext = getActivity().getApplicationContext();
|
||||
Context appContext = context.getApplicationContext();
|
||||
if (appContext == null) {
|
||||
LogUtils.e(TAG, "initMimoSdk: ApplicationContext is null");
|
||||
return;
|
||||
@@ -468,18 +481,18 @@ public class ADsBannerView extends LinearLayout {
|
||||
/**
|
||||
* 获取SharedPreferences实例(原逻辑保留,添加空指针校验)
|
||||
*/
|
||||
SharedPreferences getSharedPreferences() {
|
||||
if (mSharedPreferences == null) {
|
||||
// 修复:使用ApplicationContext获取SharedPreferences,避免Activity Context泄露
|
||||
Context appContext = getActivity().getApplicationContext();
|
||||
if (appContext != null) {
|
||||
mSharedPreferences = appContext.getSharedPreferences(PRIVACY_FILE, Context.MODE_PRIVATE);
|
||||
} else {
|
||||
LogUtils.e(TAG, "getSharedPreferences: ApplicationContext is null");
|
||||
// 降级方案:若ApplicationContext为空,使用Activity Context(仅作兼容)
|
||||
mSharedPreferences = getActivity().getSharedPreferences(PRIVACY_FILE, Context.MODE_PRIVATE);
|
||||
}
|
||||
}
|
||||
return mSharedPreferences;
|
||||
}
|
||||
// SharedPreferences getSharedPreferences() {
|
||||
//// if (mSharedPreferences == null) {
|
||||
//// // 修复:使用ApplicationContext获取SharedPreferences,避免Activity Context泄露
|
||||
//// Context appContext = getActivity().getApplicationContext();
|
||||
//// if (appContext != null) {
|
||||
//// mSharedPreferences = appContext.getSharedPreferences(PRIVACY_FILE, Context.MODE_PRIVATE);
|
||||
//// } else {
|
||||
//// LogUtils.e(TAG, "getSharedPreferences: ApplicationContext is null");
|
||||
//// // 降级方案:若ApplicationContext为空,使用Activity Context(仅作兼容)
|
||||
//// mSharedPreferences = getActivity().getSharedPreferences(PRIVACY_FILE, Context.MODE_PRIVATE);
|
||||
//// }
|
||||
//// }
|
||||
// return mSharedPreferences;
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,484 @@
|
||||
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.os.Message;
|
||||
import android.text.TextUtils;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.Display;
|
||||
import android.view.Gravity;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.RadioButton;
|
||||
import android.widget.RadioGroup;
|
||||
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.views.ADsControlView;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/11/26 17:51
|
||||
* @Describe 广告模式控制控件(Java 7 兼容)
|
||||
* 支持:SP持久化、外部静态方法更新、Handler视图同步、外部静态方法读取
|
||||
*/
|
||||
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";
|
||||
// 单机模式与米盟模式标志位
|
||||
ADsMode mADsMode;
|
||||
private static final String PRIVACY_VALUE = "privacy_value";
|
||||
// 隐私协议签约结果 0: 拒绝,1:赞同 2: 未签约
|
||||
String privacyAgreeValue;
|
||||
|
||||
// Handler消息标识
|
||||
private static final int MSG_UPDATE_MODE = 1001;
|
||||
|
||||
// 控件引用
|
||||
private RadioGroup rgAdsMode;
|
||||
private RadioButton rbStandalone;
|
||||
private RadioButton rbMimoSdk;
|
||||
|
||||
// 外部监听、SP实例、Handler实例
|
||||
private OnAdsModeSelectedListener listener;
|
||||
private SharedPreferences sharedPreferences;
|
||||
private InternalHandler mHandler;
|
||||
private Context mContext;
|
||||
|
||||
// 静态列表:存储所有已创建的控件实例(Java 7 标准集合)
|
||||
private static final java.util.List<ADsControlView> sControlViews = new java.util.ArrayList<ADsControlView>();
|
||||
|
||||
// 构造方法(Java 7 兼容)
|
||||
public ADsControlView(Context context) {
|
||||
super(context);
|
||||
initView(context);
|
||||
}
|
||||
|
||||
public ADsControlView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
initView(context);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public ADsControlView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
initView(context);
|
||||
}
|
||||
|
||||
public void setPrivacyAgreeValue(String privacyAgreeValue) {
|
||||
this.privacyAgreeValue = privacyAgreeValue;
|
||||
}
|
||||
|
||||
public String getPrivacyAgreeValue() {
|
||||
String privacyAgreeValue = sharedPreferences.getString(PRIVACY_VALUE, "0");
|
||||
return privacyAgreeValue;
|
||||
}
|
||||
|
||||
public void setADsMode(ADsMode mADsMode) {
|
||||
this.mADsMode = mADsMode;
|
||||
sharedPreferences.edit().putString(KEY_SELECTED_MODE, this.mADsMode.name()).apply();
|
||||
}
|
||||
|
||||
public ADsMode getADsMode() {
|
||||
String savedModeStr = sharedPreferences.getString(KEY_SELECTED_MODE, ADsMode.STANDALONE.name());
|
||||
mADsMode = ADsMode.fromValue(savedModeStr);
|
||||
return mADsMode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化视图、SP、Handler
|
||||
*/
|
||||
private void initView(final Context context) {
|
||||
this.mContext = context;
|
||||
// 加载布局
|
||||
LayoutInflater.from(context).inflate(R.layout.view_adscontrol, this, true);
|
||||
|
||||
// 初始化SP
|
||||
sharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
|
||||
|
||||
// 绑定控件
|
||||
rgAdsMode = (RadioGroup) findViewById(R.id.rg_ads_mode);
|
||||
rbStandalone = (RadioButton) findViewById(R.id.rb_standalone);
|
||||
rbMimoSdk = (RadioButton) findViewById(R.id.rb_mimo_sdk);
|
||||
|
||||
// 初始化Handler(主线程Looper)
|
||||
mHandler = new InternalHandler(Looper.getMainLooper());
|
||||
|
||||
// 注册控件实例到静态列表(线程安全)
|
||||
registerControlView(this);
|
||||
|
||||
// 从SP读取初始模式并设置
|
||||
ToastUtils.show(String.format("savedMode : %s", getADsMode().name()));
|
||||
setSelectedMode(getADsMode());
|
||||
|
||||
// 单选组选择事件监听
|
||||
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(){
|
||||
@Override
|
||||
public void onAgreePrivacy() {
|
||||
setADsMode(ADsMode.MIMO_SDK);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisagreePrivacy() {
|
||||
setADsMode(ADsMode.STANDALONE);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 【静态】显示隐私协议弹窗(供外部调用,带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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 【静态】清理SP中存储的隐私协议状态(PRIVACY_VALUE)
|
||||
* 函数名:cleanprivacystatus(按要求命名)
|
||||
* @param context 上下文(建议使用ApplicationContext)
|
||||
*/
|
||||
public static void cleanPrivacyStatus(Context context) {
|
||||
if (context == null) {
|
||||
LogUtils.e(TAG, "cleanPrivacyStatus: Context is null, cannot clean privacy status");
|
||||
return;
|
||||
}
|
||||
// 清空PRIVACY_VALUE值(移除该键,恢复初始未选择状态)
|
||||
SharedPreferences sp = getPrivacySharedPreferences(context);
|
||||
sp.edit().remove(PRIVACY_VALUE).apply();
|
||||
LogUtils.i(TAG, "cleanPrivacyStatus: Privacy status cleaned successfully");
|
||||
ToastUtils.show("cleanPrivacyStatus: Privacy status cleaned successfully");
|
||||
// 清理后同步更新广告模式为单机模式(避免隐私状态为空时仍加载广告)
|
||||
//ADsControlView.updateAdsModeByStatic(context, ADsMode.STANDALONE);
|
||||
}
|
||||
|
||||
// 【配套静态工具方法】获取隐私协议SP实例(供上述两个静态方法调用,需一并添加)
|
||||
private static SharedPreferences getPrivacySharedPreferences(Context context) {
|
||||
// 使用ApplicationContext获取SP,避免内存泄漏
|
||||
Context appContext = context.getApplicationContext();
|
||||
if (appContext != null) {
|
||||
return appContext.getSharedPreferences(PRIVACY_FILE, Context.MODE_PRIVATE);
|
||||
}
|
||||
// 降级方案:若ApplicationContext为空,使用传入的Context
|
||||
return context.getSharedPreferences(PRIVACY_FILE, 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);
|
||||
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);
|
||||
LogUtils.i(TAG, "已同意隐私协议,开始初始化米盟SDK...");
|
||||
initMimoSdkStatic(activity.getApplicationContext());
|
||||
return;
|
||||
} else {
|
||||
LogUtils.i(TAG, "开始弹出隐私协议...");
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
|
||||
builder.setTitle("用户须知");
|
||||
builder.setMessage("小米广告SDK隐私政策: https://dev.mi.com/distribute/doc/details?pId=1688, 请复制到浏览器查看");
|
||||
builder.setIcon(R.drawable.ic_launcher);
|
||||
builder.setCancelable(false); // 点击对话框以外的区域不消失
|
||||
builder.setPositiveButton("同意", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
SharedPreferences sp = getPrivacySharedPreferences(activity);
|
||||
sp.edit().putString(PRIVACY_VALUE, String.valueOf(1)).apply();
|
||||
initMimoSdkStatic(activity.getApplicationContext());
|
||||
dialog.dismiss();
|
||||
onPrivacyChangeListener.onAgreePrivacy();
|
||||
}
|
||||
});
|
||||
builder.setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
SharedPreferences sp = getPrivacySharedPreferences(activity);
|
||||
sp.edit().putString(PRIVACY_VALUE, String.valueOf(0)).apply();
|
||||
ADsControlView.updateAdsModeByStatic(activity.getApplicationContext(), ADsMode.STANDALONE);
|
||||
dialog.dismiss();
|
||||
onPrivacyChangeListener.onDisagreePrivacy();
|
||||
}
|
||||
});
|
||||
AlertDialog dialog = builder.create();
|
||||
|
||||
// 配置弹窗位置(底部全屏)
|
||||
Window window = dialog.getWindow();
|
||||
if (window != null) {
|
||||
window.setGravity(Gravity.BOTTOM);
|
||||
WindowManager m = activity.getWindowManager();
|
||||
Display d = m.getDefaultDisplay();
|
||||
WindowManager.LayoutParams p = window.getAttributes();
|
||||
p.width = d.getWidth();
|
||||
window.setAttributes(p);
|
||||
}
|
||||
dialog.show();
|
||||
}
|
||||
}
|
||||
|
||||
// 【配套静态工具方法】静态初始化米盟SDK(供上述静态方法调用,需一并添加)
|
||||
private static void initMimoSdkStatic(Context appContext) {
|
||||
if (appContext == null) {
|
||||
LogUtils.e(TAG, "initMimoSdkStatic: ApplicationContext is null");
|
||||
return;
|
||||
}
|
||||
// 初始化SDK,捕获异常避免崩溃
|
||||
try {
|
||||
MimoSdk.init(appContext, new MimoCustomController() {
|
||||
@Override
|
||||
public boolean isCanUseLocation() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MimoLocation getMimoLocation() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCanUseWifiState() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean alist() {
|
||||
return true;
|
||||
}
|
||||
}, new MimoSdk.InitCallback() {
|
||||
@Override
|
||||
public void success() {
|
||||
LogUtils.d(TAG, "MimoSdk init success (static)");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fail(int code, String msg) {
|
||||
LogUtils.e(TAG, "MimoSdk init fail (static), code=" + code + ",msg=" + msg);
|
||||
}
|
||||
});
|
||||
MimoSdk.setDebugOn(true);
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "initMimoSdkStatic: init failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 静态方法:外部调用更新SP中的模式,并发送消息通知控件更新
|
||||
* @param context 上下文(建议使用ApplicationContext)
|
||||
* @param mode 要设置的广告模式
|
||||
*/
|
||||
public static void updateAdsModeByStatic(Context context, ADsMode mode) {
|
||||
if (context == null || mode == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. 更新SP数据
|
||||
SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
|
||||
sp.edit().putString(KEY_SELECTED_MODE, mode.name()).apply();
|
||||
|
||||
// 2. 发送Handler消息,通知所有控件更新
|
||||
InternalHandler.sendUpdateModeMessage(mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增静态方法:外部调用读取SP中存储的广告模式(Java 7 兼容)
|
||||
* @param context 上下文(建议使用ApplicationContext)
|
||||
* @return 存储的AdsMode,默认返回单机模式(STANDALONE)
|
||||
*/
|
||||
public static ADsMode getAdsModeFromStatic(Context context) {
|
||||
// 空指针校验:上下文为空时返回默认模式
|
||||
if (context == null) {
|
||||
return ADsMode.STANDALONE;
|
||||
}
|
||||
|
||||
// 读取SP数据
|
||||
SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
|
||||
String savedModeStr = sp.getString(KEY_SELECTED_MODE, ADsMode.STANDALONE.name());
|
||||
|
||||
// 解析枚举并返回(兼容SP中数据异常的情况)
|
||||
return ADsMode.fromValue(savedModeStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册控件实例到静态列表(线程安全)
|
||||
*/
|
||||
private static void registerControlView(ADsControlView view) {
|
||||
synchronized (sControlViews) {
|
||||
if (!sControlViews.contains(view)) {
|
||||
sControlViews.add(view);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除控件实例(线程安全)
|
||||
*/
|
||||
private static void unregisterControlView(ADsControlView view) {
|
||||
synchronized (sControlViews) {
|
||||
sControlViews.remove(view);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置选中模式(内部使用,更新UI)
|
||||
*/
|
||||
private void setSelectedMode(final ADsMode mode) {
|
||||
final ADsMode mode2;
|
||||
if (mode == null) {
|
||||
mode2 = ADsMode.STANDALONE;
|
||||
} else {
|
||||
mode2 = mode;
|
||||
}
|
||||
// 确保UI操作在主线程
|
||||
if (Looper.myLooper() == Looper.getMainLooper()) {
|
||||
if (mode2 == ADsMode.STANDALONE) {
|
||||
rbStandalone.setChecked(true);
|
||||
} else if (mode2 == ADsMode.MIMO_SDK) {
|
||||
rbMimoSdk.setChecked(true);
|
||||
}
|
||||
} else {
|
||||
mHandler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
setSelectedMode(mode2);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前选中模式
|
||||
*/
|
||||
public ADsMode getSelectedMode() {
|
||||
int checkedId = rgAdsMode.getCheckedRadioButtonId();
|
||||
return checkedId == R.id.rb_mimo_sdk ? ADsMode.MIMO_SDK : ADsMode.STANDALONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置外部监听
|
||||
*/
|
||||
public void setOnAdsModeSelectedListener(OnAdsModeSelectedListener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
/**
|
||||
* 内部Handler类(Java 7 静态内部类,无隐藏API依赖)
|
||||
*/
|
||||
private static class InternalHandler extends Handler {
|
||||
static volatile InternalHandler _InternalHandler;
|
||||
public InternalHandler(Looper looper) {
|
||||
super(looper);
|
||||
_InternalHandler = this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 静态方法:发送模式更新消息
|
||||
*/
|
||||
public static void sendUpdateModeMessage(ADsMode mode) {
|
||||
if (mode == null || _InternalHandler == null) {
|
||||
return;
|
||||
}
|
||||
Message msg = _InternalHandler.obtainMessage();
|
||||
msg.what = MSG_UPDATE_MODE;
|
||||
msg.obj = mode;
|
||||
_InternalHandler.sendMessage(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
super.handleMessage(msg);
|
||||
if (msg.what == MSG_UPDATE_MODE) {
|
||||
ADsMode mode = (ADsMode) msg.obj;
|
||||
if (mode == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 修复:替换isDetachedFromWindow()为isAttachedToWindow()(API 1兼容)
|
||||
// 逻辑调整:view.isAttachedToWindow() → 控件已附加到窗口(活跃状态)
|
||||
synchronized (sControlViews) {
|
||||
for (ADsControlView view : sControlViews) {
|
||||
// 三重校验:非空 + 可见 + 已附加到窗口(避免操作销毁/未初始化的控件)
|
||||
if (view != null && view.isShown() && view.isAttachedToWindow()) {
|
||||
view.setSelectedMode(mode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生命周期方法:控件销毁时解除注册,避免内存泄漏
|
||||
*/
|
||||
@Override
|
||||
protected void onDetachedFromWindow() {
|
||||
super.onDetachedFromWindow();
|
||||
// 移除Handler回调
|
||||
if (mHandler != null) {
|
||||
mHandler.removeCallbacksAndMessages(null);
|
||||
}
|
||||
// 从静态列表中移除当前控件
|
||||
unregisterControlView(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 外部监听接口
|
||||
*/
|
||||
public interface OnAdsModeSelectedListener {
|
||||
void onModeSelected(ADsMode selectedMode);
|
||||
}
|
||||
|
||||
public interface OnPrivacyChangeListener {
|
||||
void onAgreePrivacy();
|
||||
void onDisagreePrivacy();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,8 +193,9 @@ public class AboutView extends LinearLayout {
|
||||
elementGitWeb.setOnClickListener(mGitWebOnClickListener);
|
||||
// 定义检查更新按钮
|
||||
//
|
||||
Element elementAppUpdate = new Element(_mContext.getString(R.string.app_update), R.drawable.ic_winboll);
|
||||
/*Element elementAppUpdate = new Element(_mContext.getString(R.string.app_update), R.drawable.ic_winboll);
|
||||
elementAppUpdate.setOnClickListener(mAppUpdateOnClickListener);
|
||||
*/
|
||||
|
||||
String szAppInfo = "";
|
||||
try {
|
||||
@@ -214,8 +215,8 @@ public class AboutView extends LinearLayout {
|
||||
//.addGroup("Connect with us")
|
||||
.addEmail("ZhanGSKen<zhangsken@qq.com>")
|
||||
.addWebsite(mszHomePage)
|
||||
.addItem(elementGitWeb)
|
||||
.addItem(elementAppUpdate);
|
||||
.addItem(elementGitWeb);
|
||||
//.addItem(elementAppUpdate);
|
||||
//.addFacebook("the.medy")
|
||||
//.addTwitter("medyo80")
|
||||
//.addYoutube("UCdPQtdWIsg7_pi4mrRu46vA")
|
||||
@@ -224,7 +225,7 @@ public class AboutView extends LinearLayout {
|
||||
//.addInstagram("medyo80")
|
||||
//.create();
|
||||
|
||||
if (mAPPInfo.isAddDebugTools()) {
|
||||
/*if (mAPPInfo.isAddDebugTools()) {
|
||||
// 定义应用调试按钮
|
||||
//
|
||||
Element elementAppMode;
|
||||
@@ -236,7 +237,7 @@ public class AboutView extends LinearLayout {
|
||||
elementAppMode.setOnClickListener(mAppDebugOnClickListener);
|
||||
}
|
||||
aboutPage.addItem(elementAppMode);
|
||||
}
|
||||
}*/
|
||||
|
||||
return aboutPage.create();
|
||||
}
|
||||
|
||||
46
libaes/src/main/res/layout/view_adscontrol.xml
Normal file
46
libaes/src/main/res/layout/view_adscontrol.xml
Normal file
@@ -0,0 +1,46 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:padding="16dp"
|
||||
android:background="@android:color/white">
|
||||
|
||||
<!-- 标题提示 -->
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="运行模式选择"
|
||||
android:textSize="16sp"
|
||||
android:textColor="@android:color/black"
|
||||
android:layout_marginBottom="12dp" />
|
||||
|
||||
<!-- 单选组 -->
|
||||
<RadioGroup
|
||||
android:id="@+id/rg_ads_mode"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
<!-- 单机模式单选框 -->
|
||||
<RadioButton
|
||||
android:id="@+id/rb_standalone"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="单机模式"
|
||||
android:textSize="14sp"
|
||||
android:textColor="@android:color/darker_gray"
|
||||
android:layout_marginBottom="8dp" />
|
||||
|
||||
<!-- 米盟广告SDK模式单选框 -->
|
||||
<RadioButton
|
||||
android:id="@+id/rb_mimo_sdk"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="米盟广告SDK支持模式"
|
||||
android:textSize="14sp"
|
||||
android:textColor="@android:color/darker_gray" />
|
||||
|
||||
</RadioGroup>
|
||||
|
||||
</LinearLayout>
|
||||
Reference in New Issue
Block a user