diff --git a/aes/build.properties b/aes/build.properties index 94fd5a5f..b2db752c 100644 --- a/aes/build.properties +++ b/aes/build.properties @@ -1,8 +1,8 @@ #Created by .winboll/winboll_app_build.gradle -#Fri Nov 21 13:48:53 HKT 2025 +#Wed Nov 26 13:05:31 GMT 2025 stageCount=7 libraryProject=libaes baseVersion=15.11 publishVersion=15.11.6 -buildCount=0 +buildCount=27 baseBetaVersion=15.11.7 diff --git a/aes/src/main/AndroidManifest.xml b/aes/src/main/AndroidManifest.xml index 9396685d..40eb6740 100644 --- a/aes/src/main/AndroidManifest.xml +++ b/aes/src/main/AndroidManifest.xml @@ -35,6 +35,8 @@ + + \ No newline at end of file diff --git a/aes/src/main/java/cc/winboll/studio/aes/MainActivity.java b/aes/src/main/java/cc/winboll/studio/aes/MainActivity.java index 53968fd8..0733873c 100644 --- a/aes/src/main/java/cc/winboll/studio/aes/MainActivity.java +++ b/aes/src/main/java/cc/winboll/studio/aes/MainActivity.java @@ -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; diff --git a/aes/src/main/java/cc/winboll/studio/aes/SettingsActivity.java b/aes/src/main/java/cc/winboll/studio/aes/SettingsActivity.java new file mode 100644 index 00000000..2962ba01 --- /dev/null +++ b/aes/src/main/java/cc/winboll/studio/aes/SettingsActivity.java @@ -0,0 +1,37 @@ +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&豆包大模型 + * @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"); + } + } + }); + } + +} diff --git a/aes/src/main/res/layout/activity_settings.xml b/aes/src/main/res/layout/activity_settings.xml new file mode 100644 index 00000000..c2df8381 --- /dev/null +++ b/aes/src/main/res/layout/activity_settings.xml @@ -0,0 +1,12 @@ + + + + diff --git a/aes/src/main/res/menu/toolbar_library.xml b/aes/src/main/res/menu/toolbar_main.xml similarity index 92% rename from aes/src/main/res/menu/toolbar_library.xml rename to aes/src/main/res/menu/toolbar_main.xml index 7c1a41cc..7f2f26ba 100644 --- a/aes/src/main/res/menu/toolbar_library.xml +++ b/aes/src/main/res/menu/toolbar_main.xml @@ -32,4 +32,7 @@ + diff --git a/libaes/build.properties b/libaes/build.properties index 273b33ec..b2db752c 100644 --- a/libaes/build.properties +++ b/libaes/build.properties @@ -1,8 +1,8 @@ #Created by .winboll/winboll_app_build.gradle -#Fri Nov 21 13:48:41 HKT 2025 +#Wed Nov 26 13:05:31 GMT 2025 stageCount=7 libraryProject=libaes baseVersion=15.11 publishVersion=15.11.6 -buildCount=0 +buildCount=27 baseBetaVersion=15.11.7 diff --git a/libaes/src/main/java/cc/winboll/studio/libaes/activitys/DrawerFragmentActivity.java b/libaes/src/main/java/cc/winboll/studio/libaes/activitys/DrawerFragmentActivity.java index b6dfba12..1a967dec 100644 --- a/libaes/src/main/java/cc/winboll/studio/libaes/activitys/DrawerFragmentActivity.java +++ b/libaes/src/main/java/cc/winboll/studio/libaes/activitys/DrawerFragmentActivity.java @@ -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); } } diff --git a/libaes/src/main/java/cc/winboll/studio/libaes/enums/ADsMode.java b/libaes/src/main/java/cc/winboll/studio/libaes/enums/ADsMode.java new file mode 100644 index 00000000..81343c5e --- /dev/null +++ b/libaes/src/main/java/cc/winboll/studio/libaes/enums/ADsMode.java @@ -0,0 +1,31 @@ +package cc.winboll.studio.libaes.enums; + +/** + * @Author ZhanGSKen&豆包大模型 + * @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; + } + } +} diff --git a/libaes/src/main/java/cc/winboll/studio/libaes/views/ADsBannerView.java b/libaes/src/main/java/cc/winboll/studio/libaes/views/ADsBannerView.java index 41eee91f..90813439 100644 --- a/libaes/src/main/java/cc/winboll/studio/libaes/views/ADsBannerView.java +++ b/libaes/src/main/java/cc/winboll/studio/libaes/views/ADsBannerView.java @@ -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&豆包大模型 @@ -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 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 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; +// } } diff --git a/libaes/src/main/java/cc/winboll/studio/libaes/views/ADsControlView.java b/libaes/src/main/java/cc/winboll/studio/libaes/views/ADsControlView.java new file mode 100644 index 00000000..facb44fb --- /dev/null +++ b/libaes/src/main/java/cc/winboll/studio/libaes/views/ADsControlView.java @@ -0,0 +1,479 @@ +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&豆包大模型 + * @Date 2025/11/26 17:51 + * @Describe 广告模式控制控件(Java 7 兼容) + * 支持:SP持久化、外部静态方法更新、Handler视图同步、外部静态方法读取 + */ +public class ADsControlView extends LinearLayout { + public static final String TAG = "ADsControlView"; + + private static final String PRIVACY_FILE = "privacy_pfs"; + private static final String PRIVACY_VALUE = "privacy_value";//0: 拒绝,1:赞同 + + // SP存储配置 + private static final String SP_NAME = "ads_control_config"; + private static final String KEY_SELECTED_MODE = "selected_ads_mode"; + + // 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 sControlViews = new java.util.ArrayList(); + + // 构造方法(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); + } + + /** + * 初始化视图、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读取初始模式并设置 + String savedModeStr = sharedPreferences.getString(KEY_SELECTED_MODE, ADsMode.STANDALONE.name()); + ADsMode savedMode = ADsMode.fromValue(savedModeStr); + setSelectedMode(savedMode); + + // 单选组选择事件监听 + rgAdsMode.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(RadioGroup group, int checkedId) { + if (checkedId == R.id.rb_standalone) { + cleanPrivacyStatus(context); + } else if (checkedId == R.id.rb_mimo_sdk) { + verifyPrivacyPolicy(context, ADsMode.MIMO_SDK); + } + } + }); + } + + void verifyPrivacyPolicy(final Context context, ADsMode selectedMode) { + // 检查设定值,再调用隐私协议签署对话框。 + // 下面监听对话框会话结果,再最终确定选择单机还是广告SDK。 + if (selectedMode == ADsMode.MIMO_SDK) { + showPrivacy(context, new OnPrivacyChangeListener(){ + @Override + public void onAgreePrivacy() { + // 保存到SP + sharedPreferences.edit().putString(KEY_SELECTED_MODE, ADsMode.MIMO_SDK.name()).apply(); + // 触发外部监听 + if (listener != null) { + listener.onModeSelected(ADsMode.MIMO_SDK); + } + } + + @Override + public void onDisagreePrivacy() { + cleanPrivacyStatus(context); + // 触发外部监听 + if (listener != null) { + listener.onModeSelected(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; + } + // 读取隐私协议状态并处理逻辑 + SharedPreferences 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(); + } +} + diff --git a/libaes/src/main/res/layout/view_adscontrol.xml b/libaes/src/main/res/layout/view_adscontrol.xml new file mode 100644 index 00000000..b5e6d957 --- /dev/null +++ b/libaes/src/main/res/layout/view_adscontrol.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + +