mirror of
https://gitea.winboll.cc/ZhanGSKen/MyWinBoLL.git
synced 2026-08-14 13:37:11 +08:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3fde2551f9 | |||
| 3a8d509f4e | |||
| da529c737b | |||
| b1be995092 | |||
| 4247934a20 | |||
| bf5fe296ff | |||
| f6bc48d5d2 | |||
| e2c9f362fc | |||
| 7f53355005 | |||
| f09ab4036f | |||
| 405ebe41db | |||
| 54497d8adb |
@@ -34,7 +34,7 @@ android {
|
||||
// versionName 更新后需要手动设置
|
||||
// .winboll/winbollBuildProps.properties 文件的 stageCount=0
|
||||
// Gradle编译环境下合起来的 versionName 就是 "${versionName}.0"
|
||||
versionName "15.20"
|
||||
versionName "15.21"
|
||||
if(true) {
|
||||
versionName = genVersionName("${versionName}")
|
||||
}
|
||||
@@ -75,8 +75,10 @@ dependencies {
|
||||
//annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
|
||||
|
||||
// WinBoLL库 nexus.winboll.cc 地址
|
||||
api 'cc.winboll.studio:libaes:15.20.21'
|
||||
api 'cc.winboll.studio:libappbase:15.20.34'
|
||||
//api 'cc.winboll.studio:libaes:15.20.21'
|
||||
//api 'cc.winboll.studio:libappbase:15.20.34'
|
||||
api 'cc.winboll.studio:libaes:15.21.+'
|
||||
api 'cc.winboll.studio:libappbase:15.21.+'
|
||||
|
||||
api fileTree(dir: 'libs', include: ['*.jar'])
|
||||
}
|
||||
|
||||
@@ -1,8 +1,18 @@
|
||||
#Created by .winboll/winboll_app_build.gradle
|
||||
#Sun Jul 19 06:16:39 HKT 2026
|
||||
<<<<<<< HEAD
|
||||
#Tue Jul 21 11:13:12 CST 2026
|
||||
stageCount=12
|
||||
libraryProject=
|
||||
baseVersion=15.20
|
||||
baseVersion=15.21
|
||||
publishVersion=15.20.11
|
||||
buildCount=0
|
||||
buildCount=22
|
||||
baseBetaVersion=15.20.12
|
||||
=======
|
||||
#Tue Jul 21 02:54:31 GMT 2026
|
||||
stageCount=0
|
||||
libraryProject=
|
||||
baseVersion=15.21
|
||||
publishVersion=15.21.0
|
||||
buildCount=1
|
||||
baseBetaVersion=15.21.1
|
||||
>>>>>>> origin/contacts
|
||||
|
||||
@@ -254,7 +254,7 @@
|
||||
|
||||
<activity android:name="cc.winboll.studio.contacts.activities.AboutActivity"/>
|
||||
|
||||
<service android:name="cc.winboll.studio.contacts.services.LimitedTimeSpecialChannelService"/>
|
||||
<service android:name="cc.winboll.studio.contacts.services.LimitedTimeGreenChannelService"/>
|
||||
|
||||
<activity android:name="cc.winboll.studio.contacts.unittest.LayoutDraftPreViewActivity"/>
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import androidx.appcompat.app.AppCompatActivity;
|
||||
import cc.winboll.studio.contacts.R;
|
||||
import cc.winboll.studio.contacts.activities.SettingsActivity;
|
||||
import cc.winboll.studio.contacts.dun.Rules;
|
||||
import cc.winboll.studio.contacts.services.LimitedTimeSpecialChannelService;
|
||||
import cc.winboll.studio.contacts.services.LimitedTimeGreenChannelService;
|
||||
import cc.winboll.studio.contacts.utils.IntUtils;
|
||||
import cc.winboll.studio.libaes.interfaces.IWinBoLLActivity;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
@@ -122,7 +122,7 @@ public class UnitTestActivity extends WinBollActivity implements IWinBoLLActivit
|
||||
new Thread(new Runnable(){
|
||||
@Override
|
||||
public void run() {
|
||||
LimitedTimeSpecialChannelService.unitTest(UnitTestActivity.this);
|
||||
LimitedTimeGreenChannelService.unitTest(UnitTestActivity.this);
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
@@ -1,9 +1,16 @@
|
||||
package cc.winboll.studio.contacts.activities;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.view.MenuItem;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
||||
import cc.winboll.studio.contacts.dun.Rules;
|
||||
import cc.winboll.studio.contacts.model.SettingsBean;
|
||||
import cc.winboll.studio.contacts.services.LimitedTimeGreenChannelService;
|
||||
import cc.winboll.studio.libaes.interfaces.IWinBoLLActivity;
|
||||
import cc.winboll.studio.libaes.models.AESThemeBean;
|
||||
import cc.winboll.studio.libaes.utils.AESThemeUtil;
|
||||
@@ -21,6 +28,8 @@ public class WinBollActivity extends AppCompatActivity implements IWinBoLLActivi
|
||||
|
||||
// ====================== 成员变量区 ======================
|
||||
protected volatile AESThemeBean.ThemeType mThemeType;
|
||||
private android.content.BroadcastReceiver mGreenChannelReceiver;
|
||||
private int mDefaultStatusBarColor = 0; // 默认状态栏颜色
|
||||
|
||||
// ====================== 接口实现区 ======================
|
||||
@Override
|
||||
@@ -42,6 +51,114 @@ public class WinBollActivity extends AppCompatActivity implements IWinBoLLActivi
|
||||
// setThemeStyle();
|
||||
super.onCreate(savedInstanceState);
|
||||
//LogUtils.d(TAG, "onCreate: 基类主题设置完成,当前主题类型=" + mThemeType);
|
||||
|
||||
// 保存默认状态栏颜色
|
||||
saveDefaultStatusBarColor();
|
||||
|
||||
// 检测绿色通道状态,设置顶部状态栏背景色
|
||||
updateStatusBarForGreenChannel();
|
||||
|
||||
// 注册绿色通道状态变更广播接收器
|
||||
registerGreenChannelReceiver();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
// 反注册绿色通道状态变更广播接收器
|
||||
unregisterGreenChannelReceiver();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据绿色通道状态更新顶部状态栏背景色
|
||||
* 绿色通道开启时状态栏背景为绿色,其他情况不改变
|
||||
*/
|
||||
private void updateStatusBarForGreenChannel() {
|
||||
// API 21+ 才支持设置状态栏颜色
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
boolean isGreenChannelEnabled = false;
|
||||
|
||||
Rules rules = Rules.getInstance(getApplicationContext());
|
||||
if (rules != null) {
|
||||
SettingsBean settings = rules.getSettingsModel();
|
||||
if (settings != null) {
|
||||
isGreenChannelEnabled = settings.isEnableGreenChannel();
|
||||
}
|
||||
}
|
||||
|
||||
if (isGreenChannelEnabled) {
|
||||
// 绿色通道开启,设置状态栏为绿色
|
||||
getWindow().setStatusBarColor(android.graphics.Color.GREEN);
|
||||
LogUtils.d(TAG, "updateStatusBarForGreenChannel: 绿色通道已开启,状态栏背景设为绿色");
|
||||
} else {
|
||||
// 绿色通道关闭,恢复默认状态栏颜色
|
||||
// 如果默认颜色为0(透明),则获取主题默认颜色
|
||||
int restoreColor = mDefaultStatusBarColor;
|
||||
if (restoreColor == 0) {
|
||||
// 从主题中获取默认状态栏颜色
|
||||
android.util.TypedValue typedValue = new android.util.TypedValue();
|
||||
if (getTheme().resolveAttribute(android.R.attr.statusBarColor, typedValue, true)) {
|
||||
restoreColor = typedValue.data;
|
||||
}
|
||||
}
|
||||
getWindow().setStatusBarColor(restoreColor);
|
||||
LogUtils.d(TAG, "updateStatusBarForGreenChannel: 绿色通道已关闭,状态栏背景已恢复, color=0x" + Integer.toHexString(restoreColor));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存默认状态栏颜色(在设置绿色通道颜色之前调用)
|
||||
*/
|
||||
private void saveDefaultStatusBarColor() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
mDefaultStatusBarColor = getWindow().getStatusBarColor();
|
||||
|
||||
// 如果获取到的颜色为0(透明),尝试从主题获取默认颜色
|
||||
if (mDefaultStatusBarColor == 0) {
|
||||
android.util.TypedValue typedValue = new android.util.TypedValue();
|
||||
if (getTheme().resolveAttribute(android.R.attr.statusBarColor, typedValue, true)) {
|
||||
mDefaultStatusBarColor = typedValue.data;
|
||||
}
|
||||
}
|
||||
|
||||
LogUtils.d(TAG, "saveDefaultStatusBarColor: 保存默认状态栏颜色=0x" + Integer.toHexString(mDefaultStatusBarColor));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册绿色通道状态变更本地广播接收器
|
||||
*/
|
||||
private void registerGreenChannelReceiver() {
|
||||
LogUtils.d(TAG, "registerGreenChannelReceiver: 注册绿色通道状态变更广播接收器");
|
||||
mGreenChannelReceiver = new android.content.BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(android.content.Context context, Intent intent) {
|
||||
if (LimitedTimeGreenChannelService.ACTION_GREEN_CHANNEL_STATE_CHANGED.equals(intent.getAction())) {
|
||||
boolean isEnabled = intent.getBooleanExtra(LimitedTimeGreenChannelService.EXTRA_GREEN_CHANNEL_ENABLED, false);
|
||||
LogUtils.d(TAG, "registerGreenChannelReceiver: 收到绿色通道状态变更广播, isEnabled=" + isEnabled);
|
||||
|
||||
// 调用统一方法更新状态栏颜色
|
||||
updateStatusBarForGreenChannel();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
IntentFilter filter = new IntentFilter(LimitedTimeGreenChannelService.ACTION_GREEN_CHANNEL_STATE_CHANGED);
|
||||
LocalBroadcastManager.getInstance(this).registerReceiver(mGreenChannelReceiver, filter);
|
||||
LogUtils.d(TAG, "registerGreenChannelReceiver: 广播接收器注册完成");
|
||||
}
|
||||
|
||||
/**
|
||||
* 反注册绿色通道状态变更本地广播接收器
|
||||
*/
|
||||
private void unregisterGreenChannelReceiver() {
|
||||
if (mGreenChannelReceiver != null) {
|
||||
LogUtils.d(TAG, "unregisterGreenChannelReceiver: 反注册绿色通道状态变更广播接收器");
|
||||
LocalBroadcastManager.getInstance(this).unregisterReceiver(mGreenChannelReceiver);
|
||||
mGreenChannelReceiver = null;
|
||||
LogUtils.d(TAG, "unregisterGreenChannelReceiver: 反注册完成");
|
||||
}
|
||||
}
|
||||
|
||||
// ====================== 主题相关函数区 ======================
|
||||
|
||||
@@ -5,7 +5,7 @@ import cc.winboll.studio.contacts.activities.SettingsActivity;
|
||||
import cc.winboll.studio.contacts.bobulltoon.TomCat;
|
||||
import cc.winboll.studio.contacts.model.PhoneConnectRuleBean;
|
||||
import cc.winboll.studio.contacts.model.SettingsBean;
|
||||
import cc.winboll.studio.contacts.services.LimitedTimeSpecialChannelService;
|
||||
import cc.winboll.studio.contacts.services.LimitedTimeGreenChannelService;
|
||||
import cc.winboll.studio.contacts.services.MainService;
|
||||
import cc.winboll.studio.contacts.utils.ContactUtils;
|
||||
import cc.winboll.studio.contacts.utils.IntUtils;
|
||||
@@ -172,9 +172,9 @@ public class Rules {
|
||||
LogUtils.d(TAG, String.format("isDefend == %s\nisConnect == %s", isDefend, isConnect));
|
||||
}
|
||||
|
||||
// 限时特殊通道打开时返回连接
|
||||
if (!isDefend && LimitedTimeSpecialChannelService.isServiceRunning()) {
|
||||
LogUtils.d(TAG, String.format("PhoneNumber %s\n and Limited Time Special Channel Service Is Running.", phoneNumber));
|
||||
// 限时绿色通道打开时返回连接
|
||||
if (!isDefend && LimitedTimeGreenChannelService.isServiceRunning()) {
|
||||
LogUtils.d(TAG, String.format("PhoneNumber %s\n and Limited Time Green Channel Service Is Running.", phoneNumber));
|
||||
isDefend = true;
|
||||
isConnect = true;
|
||||
LogUtils.d(TAG, String.format("isDefend == %s\nisConnect == %s", isDefend, isConnect));
|
||||
|
||||
+284
@@ -0,0 +1,284 @@
|
||||
package cc.winboll.studio.contacts.floatingwindow;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.graphics.PixelFormat;
|
||||
import android.os.Build;
|
||||
import android.view.Gravity;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.FrameLayout;
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
||||
import cc.winboll.studio.contacts.R;
|
||||
import cc.winboll.studio.contacts.services.LimitedTimeGreenChannelService;
|
||||
import cc.winboll.studio.contacts.utils.PermissionUtils;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2026/07/20 15:00:00
|
||||
* @Describe 绿色通道悬浮窗管理类
|
||||
* 负责悬浮窗的创建、显示、隐藏和销毁
|
||||
* 适配 Android API 26+ Java 7 语法规范
|
||||
*/
|
||||
public class GreenChannelFloatingWindow {
|
||||
|
||||
// ====================== 常量定义区 ======================
|
||||
public static final String TAG = "GreenChannelFloatingWindow";
|
||||
|
||||
// Android版本常量(适配Java7)
|
||||
private static final int ANDROID_8_API = 26;
|
||||
private static final int ANDROID_10_API = 29;
|
||||
private static final int ANDROID_19_API = 19;
|
||||
|
||||
// ====================== 成员变量区 ======================
|
||||
private Context mContext;
|
||||
private WindowManager mWindowManager;
|
||||
private WindowManager.LayoutParams mWindowParams;
|
||||
private View mFloatingView;
|
||||
private boolean mIsShowing;
|
||||
private android.content.BroadcastReceiver mGreenChannelReceiver;
|
||||
|
||||
// ====================== 构造函数区 ======================
|
||||
/**
|
||||
* 构造函数
|
||||
* @param context 上下文
|
||||
*/
|
||||
public GreenChannelFloatingWindow(Context context) {
|
||||
this.mContext = context;
|
||||
this.mIsShowing = false;
|
||||
LogUtils.d(TAG, "GreenChannelFloatingWindow: 构造函数调用完成");
|
||||
}
|
||||
|
||||
// ====================== 公共方法区 ======================
|
||||
/**
|
||||
* 显示悬浮窗
|
||||
* @return true=显示成功,false=显示失败(权限不足或已显示)
|
||||
*/
|
||||
public boolean show() {
|
||||
LogUtils.d(TAG, "show: 尝试显示悬浮窗");
|
||||
|
||||
// 检查是否已显示
|
||||
if (mIsShowing) {
|
||||
LogUtils.w(TAG, "show: 悬浮窗已显示,跳过重复显示");
|
||||
return true;
|
||||
}
|
||||
|
||||
// 检查悬浮窗权限
|
||||
if (!PermissionUtils.isOverlayPermissionGranted(mContext)) {
|
||||
LogUtils.e(TAG, "show: 悬浮窗权限未授予,无法显示");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 初始化窗口管理器
|
||||
if (!initWindowManager()) {
|
||||
LogUtils.e(TAG, "show: 窗口管理器初始化失败");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 初始化悬浮窗视图
|
||||
if (!initFloatingView()) {
|
||||
LogUtils.e(TAG, "show: 悬浮窗视图初始化失败");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 显示悬浮窗
|
||||
try {
|
||||
mWindowManager.addView(mFloatingView, mWindowParams);
|
||||
mIsShowing = true;
|
||||
// 注册绿色通道状态变更广播接收器
|
||||
registerGreenChannelReceiver();
|
||||
LogUtils.d(TAG, "show: 悬浮窗显示成功");
|
||||
return true;
|
||||
} catch (SecurityException e) {
|
||||
LogUtils.e(TAG, "show: 悬浮窗显示失败(安全异常)", e);
|
||||
return false;
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "show: 悬浮窗显示异常", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 隐藏悬浮窗
|
||||
*/
|
||||
public void dismiss() {
|
||||
LogUtils.d(TAG, "dismiss: 尝试隐藏悬浮窗");
|
||||
|
||||
if (!mIsShowing) {
|
||||
LogUtils.d(TAG, "dismiss: 悬浮窗未显示,跳过隐藏操作");
|
||||
return;
|
||||
}
|
||||
|
||||
// 反注册绿色通道状态变更广播接收器
|
||||
unregisterGreenChannelReceiver();
|
||||
|
||||
if (mFloatingView != null && mWindowManager != null) {
|
||||
try {
|
||||
mWindowManager.removeView(mFloatingView);
|
||||
mIsShowing = false;
|
||||
LogUtils.d(TAG, "dismiss: 悬浮窗隐藏成功");
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "dismiss: 悬浮窗隐藏异常", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查悬浮窗是否正在显示
|
||||
* @return true=正在显示,false=未显示
|
||||
*/
|
||||
public boolean isShowing() {
|
||||
return mIsShowing;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取悬浮窗视图
|
||||
* @return 悬浮窗视图,未初始化时返回null
|
||||
*/
|
||||
public View getFloatingView() {
|
||||
return mFloatingView;
|
||||
}
|
||||
|
||||
// ====================== 私有方法区 ======================
|
||||
/**
|
||||
* 初始化窗口管理器
|
||||
* @return true=初始化成功,false=初始化失败
|
||||
*/
|
||||
private boolean initWindowManager() {
|
||||
if (mWindowManager == null) {
|
||||
mWindowManager = (WindowManager) mContext.getApplicationContext()
|
||||
.getSystemService(Context.WINDOW_SERVICE);
|
||||
if (mWindowManager == null) {
|
||||
LogUtils.e(TAG, "initWindowManager: WindowManager获取失败");
|
||||
return false;
|
||||
}
|
||||
LogUtils.d(TAG, "initWindowManager: WindowManager初始化成功");
|
||||
}
|
||||
|
||||
// 初始化窗口参数
|
||||
initWindowParams();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化窗口参数(适配API26+窗口类型)
|
||||
*/
|
||||
private void initWindowParams() {
|
||||
mWindowParams = new WindowManager.LayoutParams();
|
||||
|
||||
// 窗口位置:顶部居中
|
||||
mWindowParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
|
||||
|
||||
// 窗口大小:宽度全屏,高度自适应
|
||||
mWindowParams.width = WindowManager.LayoutParams.MATCH_PARENT;
|
||||
mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
|
||||
|
||||
// 窗口格式:半透明
|
||||
mWindowParams.format = PixelFormat.TRANSLUCENT;
|
||||
|
||||
// 窗口类型(适配API26+)
|
||||
if (Build.VERSION.SDK_INT >= ANDROID_10_API) {
|
||||
mWindowParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
|
||||
LogUtils.d(TAG, "initWindowParams: API29+ 悬浮窗类型=TYPE_APPLICATION_OVERLAY");
|
||||
} else if (Build.VERSION.SDK_INT >= ANDROID_8_API) {
|
||||
mWindowParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
|
||||
LogUtils.d(TAG, "initWindowParams: API26-28 悬浮窗类型=TYPE_APPLICATION_OVERLAY");
|
||||
} else {
|
||||
mWindowParams.type = WindowManager.LayoutParams.TYPE_PHONE;
|
||||
LogUtils.d(TAG, "initWindowParams: API26以下 悬浮窗类型=TYPE_PHONE");
|
||||
}
|
||||
|
||||
// 窗口标志:无焦点(不抢占输入)、全屏布局
|
||||
mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|
||||
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
|
||||
|
||||
// API19+ 添加透明状态栏/导航栏标志
|
||||
if (Build.VERSION.SDK_INT >= ANDROID_19_API) {
|
||||
mWindowParams.flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
|
||||
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
|
||||
}
|
||||
|
||||
LogUtils.d(TAG, "initWindowParams: 窗口参数初始化完成");
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化悬浮窗视图
|
||||
* @return true=初始化成功,false=初始化失败
|
||||
*/
|
||||
private boolean initFloatingView() {
|
||||
if (mFloatingView != null) {
|
||||
LogUtils.d(TAG, "initFloatingView: 视图已初始化,跳过重复创建");
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
// 创建FrameLayout拦截返回键
|
||||
FrameLayout keyInterceptorLayout = new FrameLayout(mContext) {
|
||||
@Override
|
||||
public boolean dispatchKeyEvent(KeyEvent event) {
|
||||
// 拦截返回键,避免误关闭悬浮窗
|
||||
if (event.getAction() == KeyEvent.ACTION_DOWN
|
||||
&& event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
|
||||
LogUtils.d(TAG, "dispatchKeyEvent: 拦截悬浮窗返回键事件");
|
||||
return true;
|
||||
}
|
||||
return super.dispatchKeyEvent(event);
|
||||
}
|
||||
};
|
||||
|
||||
// 加载悬浮窗布局
|
||||
mFloatingView = LayoutInflater.from(mContext)
|
||||
.inflate(R.layout.view_green_channel_floating, keyInterceptorLayout);
|
||||
|
||||
LogUtils.d(TAG, "initFloatingView: 悬浮窗视图初始化完成");
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "initFloatingView: 悬浮窗视图初始化异常", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册绿色通道状态变更广播接收器
|
||||
* 接收 LimitedTimeGreenChannelView 发出的通道开启与关闭消息
|
||||
*/
|
||||
private void registerGreenChannelReceiver() {
|
||||
LogUtils.d(TAG, "registerGreenChannelReceiver: 注册绿色通道状态变更广播接收器");
|
||||
mGreenChannelReceiver = new android.content.BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (LimitedTimeGreenChannelService.ACTION_GREEN_CHANNEL_STATE_CHANGED.equals(intent.getAction())) {
|
||||
boolean isEnabled = intent.getBooleanExtra(
|
||||
LimitedTimeGreenChannelService.EXTRA_GREEN_CHANNEL_ENABLED, false);
|
||||
LogUtils.d(TAG, "registerGreenChannelReceiver: 收到绿色通道状态变更广播, isEnabled=" + isEnabled);
|
||||
|
||||
// 如果绿色通道关闭,关闭悬浮窗
|
||||
if (!isEnabled) {
|
||||
LogUtils.d(TAG, "registerGreenChannelReceiver: 绿色通道已关闭,准备关闭悬浮窗");
|
||||
dismiss();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
IntentFilter filter = new IntentFilter(LimitedTimeGreenChannelService.ACTION_GREEN_CHANNEL_STATE_CHANGED);
|
||||
LocalBroadcastManager.getInstance(mContext).registerReceiver(mGreenChannelReceiver, filter);
|
||||
LogUtils.d(TAG, "registerGreenChannelReceiver: 广播接收器注册完成");
|
||||
}
|
||||
|
||||
/**
|
||||
* 反注册绿色通道状态变更广播接收器
|
||||
*/
|
||||
private void unregisterGreenChannelReceiver() {
|
||||
if (mGreenChannelReceiver != null) {
|
||||
LogUtils.d(TAG, "unregisterGreenChannelReceiver: 反注册绿色通道状态变更广播接收器");
|
||||
LocalBroadcastManager.getInstance(mContext).unregisterReceiver(mGreenChannelReceiver);
|
||||
mGreenChannelReceiver = null;
|
||||
LogUtils.d(TAG, "unregisterGreenChannelReceiver: 反注册完成");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@ public class SettingsBean extends BaseBean {
|
||||
private static final String JSON_KEY_DUN_RESUME_COUNT = "dunResumeCount";
|
||||
private static final String JSON_KEY_DUN_ENABLE = "isEnableDun";
|
||||
private static final String JSON_KEY_URL = "szBoBullToon_URL";
|
||||
private static final String JSON_KEY_GREEN_CHANNEL_ENABLE = "isEnableGreenChannel";
|
||||
|
||||
// ====================== 成员变量区 ======================
|
||||
// 云盾防御层数量
|
||||
@@ -40,6 +41,8 @@ public class SettingsBean extends BaseBean {
|
||||
private int dunResumeCount;
|
||||
// 是否启用云盾
|
||||
private boolean isEnableDun;
|
||||
// 是否启用绿色通道
|
||||
private boolean isEnableGreenChannel;
|
||||
// BoBullToon 应用模块数据请求地址
|
||||
private String szBoBullToon_URL;
|
||||
|
||||
@@ -53,6 +56,7 @@ public class SettingsBean extends BaseBean {
|
||||
this.dunResumeSecondCount = 60;
|
||||
this.dunResumeCount = 1;
|
||||
this.isEnableDun = false;
|
||||
this.isEnableGreenChannel = false;
|
||||
this.szBoBullToon_URL = "";
|
||||
LogUtils.d(TAG, "SettingsModel: 默认构造初始化完成 | 云盾默认配置加载完毕");
|
||||
}
|
||||
@@ -61,17 +65,20 @@ public class SettingsBean extends BaseBean {
|
||||
* 带参构造,初始化自定义配置并校验数值范围
|
||||
*/
|
||||
public SettingsBean(int dunTotalCount, int dunCurrentCount, int dunResumeSecondCount,
|
||||
int dunResumeCount, boolean isEnableDun, String szBoBullToon_URL) {
|
||||
int dunResumeCount, boolean isEnableDun, boolean isEnableGreenChannel,
|
||||
String szBoBullToon_URL) {
|
||||
this.dunTotalCount = getSettingsModelRangeInt(dunTotalCount);
|
||||
this.dunCurrentCount = getSettingsModelRangeInt(dunCurrentCount);
|
||||
this.dunResumeSecondCount = getSettingsModelRangeInt(dunResumeSecondCount);
|
||||
this.dunResumeCount = getSettingsModelRangeInt(dunResumeCount);
|
||||
this.isEnableDun = isEnableDun;
|
||||
this.isEnableGreenChannel = isEnableGreenChannel;
|
||||
this.szBoBullToon_URL = szBoBullToon_URL == null ? "" : szBoBullToon_URL;
|
||||
|
||||
LogUtils.d(TAG, "SettingsModel: 带参构造初始化完成 | 总层数=" + this.dunTotalCount
|
||||
+ " | 当前层数=" + this.dunCurrentCount + " | 恢复间隔=" + this.dunResumeSecondCount
|
||||
+ " | 恢复层数=" + this.dunResumeCount + " | 云盾启用=" + this.isEnableDun);
|
||||
+ " | 恢复层数=" + this.dunResumeCount + " | 云盾启用=" + this.isEnableDun
|
||||
+ " | 绿色通道启用=" + this.isEnableGreenChannel);
|
||||
}
|
||||
|
||||
// ====================== 私有工具方法区 ======================
|
||||
@@ -136,6 +143,15 @@ public class SettingsBean extends BaseBean {
|
||||
this.isEnableDun = isEnableDun;
|
||||
}
|
||||
|
||||
public boolean isEnableGreenChannel() {
|
||||
return isEnableGreenChannel;
|
||||
}
|
||||
|
||||
public void setIsEnableGreenChannel(boolean isEnableGreenChannel) {
|
||||
LogUtils.d(TAG, "setIsEnableGreenChannel: 绿色通道启用状态更新为" + isEnableGreenChannel);
|
||||
this.isEnableGreenChannel = isEnableGreenChannel;
|
||||
}
|
||||
|
||||
public String getBoBullToon_URL() {
|
||||
return szBoBullToon_URL;
|
||||
}
|
||||
@@ -164,6 +180,7 @@ public class SettingsBean extends BaseBean {
|
||||
jsonWriter.name(JSON_KEY_DUN_RESUME_SECOND).value(getDunResumeSecondCount());
|
||||
jsonWriter.name(JSON_KEY_DUN_RESUME_COUNT).value(getDunResumeCount());
|
||||
jsonWriter.name(JSON_KEY_DUN_ENABLE).value(isEnableDun());
|
||||
jsonWriter.name(JSON_KEY_GREEN_CHANNEL_ENABLE).value(isEnableGreenChannel());
|
||||
jsonWriter.name(JSON_KEY_URL).value(getBoBullToon_URL());
|
||||
LogUtils.d(TAG, "writeThisToJsonWriter: JSON序列化完成");
|
||||
}
|
||||
@@ -187,6 +204,8 @@ public class SettingsBean extends BaseBean {
|
||||
setDunResumeCount(getSettingsModelRangeInt(jsonReader.nextInt()));
|
||||
} else if (JSON_KEY_DUN_ENABLE.equals(name)) {
|
||||
setIsEnableDun(jsonReader.nextBoolean());
|
||||
} else if (JSON_KEY_GREEN_CHANNEL_ENABLE.equals(name)) {
|
||||
setIsEnableGreenChannel(jsonReader.nextBoolean());
|
||||
} else if (JSON_KEY_URL.equals(name)) {
|
||||
setBoBullToon_URL(jsonReader.nextString());
|
||||
} else {
|
||||
|
||||
+22
-6
@@ -13,17 +13,17 @@ import cc.winboll.studio.libappbase.LogUtils;
|
||||
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
|
||||
* @Date 2026/04/18 15:00:00 (GMT+8)
|
||||
* @LastEditTime 2026/04/22 17:20:00 (GMT+8)
|
||||
* @Describe 限时特殊通道服务
|
||||
* @Describe 限时绿色通道服务
|
||||
* 提供安全的服务启动、令牌校验及循环任务管理
|
||||
* 新增功能:
|
||||
* 1. 计时过程中实时输出剩余秒数
|
||||
* 2. 服务销毁前,发送本地广播通知应用内其他组件,并携带剩余秒数信息
|
||||
* 3. 每秒发送一次倒计时状态的本地广播
|
||||
*/
|
||||
public class LimitedTimeSpecialChannelService extends Service {
|
||||
public class LimitedTimeGreenChannelService extends Service {
|
||||
|
||||
// ========================= 常量定义 =========================
|
||||
public static final String TAG = "LimitedTimeSpecialChannelService";
|
||||
public static final String TAG = "LimitedTimeGreenChannelService";
|
||||
public static final String EXTRA_DELAY_MILLIS = "EXTRA_DELAY_MILLIS";
|
||||
private static final String EXTRA_SECURITY_TOKEN = "EXTRA_SECURITY_TOKEN";
|
||||
|
||||
@@ -32,6 +32,10 @@ public class LimitedTimeSpecialChannelService extends Service {
|
||||
|
||||
// 倒计时心跳广播 Action
|
||||
public static final String ACTION_COUNTDOWN_TICK = "cc.winboll.studio.contacts.services.ACTION_COUNTDOWN_TICK";
|
||||
// 绿色通道状态变更广播 Action
|
||||
public static final String ACTION_GREEN_CHANNEL_STATE_CHANGED = "cc.winboll.studio.contacts.services.ACTION_GREEN_CHANNEL_STATE_CHANGED";
|
||||
// 广播携带的额外参数:绿色通道是否开启
|
||||
public static final String EXTRA_GREEN_CHANNEL_ENABLED = "EXTRA_GREEN_CHANNEL_ENABLED";
|
||||
// 广播携带的额外参数:剩余秒数
|
||||
public static final String EXTRA_REMAINING_SECONDS = "EXTRA_REMAINING_SECONDS";
|
||||
// 广播携带的额外参数:总定时秒数
|
||||
@@ -44,7 +48,7 @@ public class LimitedTimeSpecialChannelService extends Service {
|
||||
*/
|
||||
public static final String mValidToken = "VALID_TOKEN_" + System.currentTimeMillis();
|
||||
|
||||
private static volatile LimitedTimeSpecialChannelService sInstance = null;
|
||||
private static volatile LimitedTimeGreenChannelService sInstance = null;
|
||||
private static volatile boolean sIsServiceRunning = false;
|
||||
|
||||
// ========================= 成员变量 =========================
|
||||
@@ -73,7 +77,7 @@ public class LimitedTimeSpecialChannelService extends Service {
|
||||
}
|
||||
|
||||
// 构建Intent传递参数
|
||||
Intent intent = new Intent(context, LimitedTimeSpecialChannelService.class);
|
||||
Intent intent = new Intent(context, LimitedTimeGreenChannelService.class);
|
||||
intent.putExtra(EXTRA_SECURITY_TOKEN, mValidToken);
|
||||
intent.putExtra(EXTRA_DELAY_MILLIS, delayMillis);
|
||||
|
||||
@@ -93,7 +97,7 @@ public class LimitedTimeSpecialChannelService extends Service {
|
||||
LogUtils.w(TAG, "停止失败,上下文为null");
|
||||
return;
|
||||
}
|
||||
Intent intent = new Intent(context, LimitedTimeSpecialChannelService.class);
|
||||
Intent intent = new Intent(context, LimitedTimeGreenChannelService.class);
|
||||
context.stopService(intent);
|
||||
LogUtils.i(TAG, "服务停止命令已发出");
|
||||
}
|
||||
@@ -106,6 +110,18 @@ public class LimitedTimeSpecialChannelService extends Service {
|
||||
return sIsServiceRunning;
|
||||
}
|
||||
|
||||
/**
|
||||
* 公共静态方法:发送绿色通道状态变更广播
|
||||
* @param context 上下文
|
||||
* @param isEnabled 绿色通道是否开启
|
||||
*/
|
||||
public static void sendGreenChannelStateChangedBroadcast(Context context, boolean isEnabled) {
|
||||
LogUtils.i(TAG, "sendGreenChannelStateChangedBroadcast: 发送绿色通道状态变更广播, isEnabled=" + isEnabled);
|
||||
Intent intent = new Intent(ACTION_GREEN_CHANNEL_STATE_CHANGED);
|
||||
intent.putExtra(EXTRA_GREEN_CHANNEL_ENABLED, isEnabled);
|
||||
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
||||
}
|
||||
|
||||
/**
|
||||
* 【核心单元测试方法】
|
||||
* 执行完整的单元测试流程
|
||||
@@ -8,6 +8,7 @@ import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.LinearLayout;
|
||||
import cc.winboll.studio.contacts.R;
|
||||
import cc.winboll.studio.contacts.floatingwindow.GreenChannelFloatingWindow;
|
||||
import cc.winboll.studio.libappbase.LogActivity;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
|
||||
@@ -21,6 +22,7 @@ public class DebugToolButtonsView extends LinearLayout {
|
||||
private static final String TAG = "DebugToolButtonsView";
|
||||
|
||||
private Context mContext;
|
||||
private GreenChannelFloatingWindow mGreenChannelFloatingWindow;
|
||||
|
||||
public DebugToolButtonsView(Context context) {
|
||||
super(context);
|
||||
@@ -51,6 +53,7 @@ public class DebugToolButtonsView extends LinearLayout {
|
||||
Button btnLayoutPreview = (Button) findViewById(R.id.btn_debug_layout_preview);
|
||||
Button btnUnitTest = (Button) findViewById(R.id.btn_debug_unit_test);
|
||||
Button btnLogView = (Button) findViewById(R.id.btn_debug_logview);
|
||||
Button btnGreenChannel = (Button) findViewById(R.id.btn_debug_green_channel);
|
||||
|
||||
btnCrash.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
@@ -79,6 +82,13 @@ public class DebugToolButtonsView extends LinearLayout {
|
||||
onLogView();
|
||||
}
|
||||
});
|
||||
|
||||
btnGreenChannel.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
onOpenGreenChannel();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void onTestCrash() {
|
||||
@@ -121,4 +131,24 @@ public class DebugToolButtonsView extends LinearLayout {
|
||||
LogUtils.d(TAG, "onLogView: 打开日志窗口");
|
||||
LogActivity.startLogActivity(getContext(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开绿色通道悬浮窗
|
||||
*/
|
||||
private void onOpenGreenChannel() {
|
||||
LogUtils.d(TAG, "onOpenGreenChannel: 打开绿色通道悬浮窗");
|
||||
if (mGreenChannelFloatingWindow == null) {
|
||||
mGreenChannelFloatingWindow = new GreenChannelFloatingWindow(mContext);
|
||||
}
|
||||
if (mGreenChannelFloatingWindow.isShowing()) {
|
||||
LogUtils.d(TAG, "onOpenGreenChannel: 悬浮窗已显示,跳过");
|
||||
return;
|
||||
}
|
||||
boolean result = mGreenChannelFloatingWindow.show();
|
||||
if (result) {
|
||||
LogUtils.d(TAG, "onOpenGreenChannel: 悬浮窗显示成功");
|
||||
} else {
|
||||
LogUtils.e(TAG, "onOpenGreenChannel: 悬浮窗显示失败,可能缺少悬浮窗权限");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,79 +13,79 @@ import android.os.Message;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.TypedValue;
|
||||
import android.view.View;
|
||||
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/03/19 14:04:20
|
||||
* @Author 豆包&Big Pickle&MiMo&ZhanGSKen<zhangsken@qq.com>
|
||||
* @CreateDate 2025/03/19 14:04:20
|
||||
* @LastEdit 2026/07/21 11:40:00
|
||||
* @Describe 云盾华氏度热力视图,垂直盾值温度视图控件(带颜色渐变+静态Handler更新)
|
||||
* 采用绘图方式展示盾值温度,填充色随盾值比例渐变,支持设置文本在温度条左侧/右侧,底部对齐竖排显示
|
||||
* 温度条宽度=5dp,文本区宽度固定=5dp,整体左右边距=0,无任何多余空白间距
|
||||
* 1. 原生Canvas绘图渲染盾值温度条,填充色随盾值比例动态渐变
|
||||
* 2. 支持竖排数值文本切换至温度条左侧/右侧,文本底部对齐展示
|
||||
* 3. 固定尺寸规范:温度条宽度5dp、文本区域宽度5dp,控件左右无外边距
|
||||
* 4. 全局静态Handler统一分发盾值更新,WeakHashMap弱引用缓存控件防止内存泄漏
|
||||
* 5. 代码完全兼容Java7语法,无Lambda、无try-with-resources、无高版本API
|
||||
*/
|
||||
public class DunTemperatureView extends View {
|
||||
// ====================== 常量定义区 ======================
|
||||
public static final String TAG = "DunTemperatureView";
|
||||
// 控件默认高度
|
||||
private static final int DEFAULT_HEIGHT = 200;
|
||||
// 温度条宽度(5dp)、文本区宽度(固定5dp)
|
||||
private static final int THERMOMETER_WIDTH_DP = 5;
|
||||
private static final int TEXT_AREA_WIDTH_DP = 5;
|
||||
// 填充区域内边距(左右=0,上下=2避免贴边)
|
||||
private static final int FILL_PADDING_HORIZONTAL = 0;
|
||||
private static final int FILL_PADDING_VERTICAL = 2;
|
||||
// 竖排文本字间距
|
||||
private static final float TEXT_CHAR_SPACING = 8f;
|
||||
// Handler消息标识
|
||||
public static final int MSG_UPDATE_DUN_VALUE = 0x01;
|
||||
// 消息参数Key
|
||||
public static final String KEY_MAX_VALUE = "max_value";
|
||||
public static final String KEY_CURRENT_VALUE = "current_value";
|
||||
|
||||
private static final int DEFAULT_HEIGHT = 200;
|
||||
private static final int THERMOMETER_WIDTH_DP = 5;
|
||||
private static final int TEXT_AREA_WIDTH_DP = 5;
|
||||
private static final int FILL_PADDING_HORIZONTAL = 0;
|
||||
private static final int FILL_PADDING_VERTICAL = 2;
|
||||
private static final float TEXT_CHAR_SPACING = 8f;
|
||||
|
||||
private static final int COLOR_GRADIENT_LOW = Color.GREEN;
|
||||
private static final int COLOR_GRADIENT_MID = Color.YELLOW;
|
||||
private static final int COLOR_GRADIENT_HIGH = Color.RED;
|
||||
private static final int COLOR_BORDER = Color.parseColor("#FF444444");
|
||||
private static final int COLOR_TEXT = Color.parseColor("#FF000000");
|
||||
|
||||
// ====================== 静态成员区 ======================
|
||||
// 弱引用缓存控件实例,避免内存泄漏
|
||||
private static WeakHashMap<DunTemperatureView, Object> sViewCache = new WeakHashMap<>();
|
||||
// 静态Handler,处理跨线程更新消息
|
||||
private static final WeakHashMap<DunTemperatureView, Object> sViewCache = new WeakHashMap<DunTemperatureView, Object>();
|
||||
private static Handler sStaticHandler;
|
||||
|
||||
// ====================== 成员变量区 ======================
|
||||
// 画笔相关
|
||||
// ====================== 实例成员变量区 ======================
|
||||
private Paint mThermometerPaint;
|
||||
private Paint mFillPaint;
|
||||
private Paint mTextPaint;
|
||||
// 尺寸参数(dp转px后的值)
|
||||
|
||||
private int mThermometerWidth;
|
||||
private int mTextAreaWidth;
|
||||
private int mMaxValue = 100; // 最高盾值
|
||||
private int mCurrentValue = 0; // 当前盾值
|
||||
private RectF mThermometerRect; // 温度条矩形区域
|
||||
// 渐变颜色配置(低→中→高 对应绿→黄→红)
|
||||
private int[] mGradientColors = {Color.GREEN, Color.YELLOW, Color.RED};
|
||||
private float[] mGradientPositions = {0.0f, 0.5f, 1.0f};
|
||||
// 布局配置:true=文本在温度条右侧(默认),false=文本在温度条左侧
|
||||
private int mMaxValue = 100;
|
||||
private int mCurrentValue = 0;
|
||||
|
||||
private RectF mThermometerRect;
|
||||
private int[] mGradientColors = new int[]{COLOR_GRADIENT_LOW, COLOR_GRADIENT_MID, COLOR_GRADIENT_HIGH};
|
||||
private float[] mGradientPositions = new float[]{0.0f, 0.5f, 1.0f};
|
||||
private boolean isTextOnRight = true;
|
||||
// 其他颜色配置
|
||||
private int mBorderColor = Color.parseColor("#FF444444");
|
||||
private int mTextColor = Color.parseColor("#FF000000");
|
||||
private int mBorderColor = COLOR_BORDER;
|
||||
private int mTextColor = COLOR_TEXT;
|
||||
|
||||
// ====================== 静态代码块 ======================
|
||||
static {
|
||||
// 初始化静态Handler,绑定主线程Looper
|
||||
sStaticHandler = new Handler(Looper.getMainLooper()) {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
public void handleMessage(final Message msg) {
|
||||
super.handleMessage(msg);
|
||||
if (msg.what == MSG_UPDATE_DUN_VALUE) {
|
||||
// 获取消息中的盾值参数
|
||||
int maxValue = msg.getData().getInt(KEY_MAX_VALUE, 100);
|
||||
int currentValue = msg.getData().getInt(KEY_CURRENT_VALUE, 0);
|
||||
LogUtils.d(TAG, "sStaticHandler: 收到更新消息,max=" + maxValue + ", current=" + currentValue);
|
||||
final int targetMsgWhat = msg.what;
|
||||
if (targetMsgWhat == MSG_UPDATE_DUN_VALUE) {
|
||||
final int maxValue = msg.getData().getInt(KEY_MAX_VALUE, 100);
|
||||
final int currentValue = msg.getData().getInt(KEY_CURRENT_VALUE, 0);
|
||||
LogUtils.d(TAG, "handleMessage receive update params, max=" + maxValue + ", current=" + currentValue);
|
||||
|
||||
// 遍历缓存的控件实例,更新所有实例
|
||||
for (DunTemperatureView view : sViewCache.keySet()) {
|
||||
if (view != null && view.isShown()) {
|
||||
view.setMaxValue(maxValue);
|
||||
view.setCurrentValue(currentValue);
|
||||
final DunTemperatureView targetView = view;
|
||||
if (targetView != null && targetView.isShown()) {
|
||||
targetView.setMaxValue(maxValue);
|
||||
targetView.setCurrentValue(currentValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -94,203 +94,194 @@ public class DunTemperatureView extends View {
|
||||
}
|
||||
|
||||
// ====================== 构造函数区 ======================
|
||||
public DunTemperatureView(Context context) {
|
||||
public DunTemperatureView(final Context context) {
|
||||
super(context);
|
||||
init();
|
||||
}
|
||||
|
||||
public DunTemperatureView(Context context, AttributeSet attrs) {
|
||||
public DunTemperatureView(final Context context, final AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init();
|
||||
}
|
||||
|
||||
public DunTemperatureView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
public DunTemperatureView(final Context context, final AttributeSet attrs, final int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
init();
|
||||
}
|
||||
|
||||
// ====================== 初始化方法区 ======================
|
||||
// ====================== 初始化私有方法 ======================
|
||||
/**
|
||||
* 初始化画笔和参数
|
||||
* 初始化画笔、尺寸参数并将实例存入全局缓存
|
||||
*/
|
||||
private void init() {
|
||||
LogUtils.d(TAG, "init: 开始初始化云盾温度视图控件");
|
||||
// dp转px(适配不同分辨率)
|
||||
mThermometerWidth = dp2px(getContext(), THERMOMETER_WIDTH_DP);
|
||||
mTextAreaWidth = dp2px(getContext(), TEXT_AREA_WIDTH_DP);
|
||||
LogUtils.d(TAG, "init: 温度条宽度5dp转px=" + mThermometerWidth + ",文本区宽度5dp转px=" + mTextAreaWidth);
|
||||
final Context context = getContext();
|
||||
LogUtils.d(TAG, "init start");
|
||||
|
||||
mThermometerWidth = dp2px(context, THERMOMETER_WIDTH_DP);
|
||||
mTextAreaWidth = dp2px(context, TEXT_AREA_WIDTH_DP);
|
||||
LogUtils.d(TAG, "init dp convert result, thermometerWidth=" + mThermometerWidth + ", textAreaWidth=" + mTextAreaWidth);
|
||||
|
||||
// 温度条边框画笔(宽度1px,适配5dp宽度)
|
||||
mThermometerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
mThermometerPaint.setColor(mBorderColor);
|
||||
mThermometerPaint.setStyle(Paint.Style.STROKE);
|
||||
mThermometerPaint.setStrokeWidth(1);
|
||||
|
||||
// 温度条填充画笔(支持渐变)
|
||||
mFillPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
mFillPaint.setStyle(Paint.Style.FILL);
|
||||
|
||||
// 文本画笔(适配5dp窄文本区,文字居中绘制)
|
||||
mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
mTextPaint.setColor(mTextColor);
|
||||
mTextPaint.setTextSize(18); // 缩小字号适配5dp窄文本区(避免文字超出)
|
||||
mTextPaint.setTextSize(18);
|
||||
mTextPaint.setTextAlign(Paint.Align.CENTER);
|
||||
mTextPaint.setFakeBoldText(true); // 文字加粗,提升窄区域可读性
|
||||
mTextPaint.setFakeBoldText(true);
|
||||
|
||||
// 初始化温度条矩形
|
||||
mThermometerRect = new RectF();
|
||||
|
||||
// 将当前实例加入静态缓存
|
||||
sViewCache.put(this, null);
|
||||
LogUtils.d(TAG, "init: 云盾温度视图控件初始化完成,实例已加入缓存");
|
||||
LogUtils.d(TAG, "init complete, add view to static cache");
|
||||
}
|
||||
|
||||
// ====================== 工具方法区 ======================
|
||||
/**
|
||||
* dp 转 px(适配不同屏幕分辨率)
|
||||
* dp单位转换px,多分辨率屏幕适配
|
||||
* @param context 上下文
|
||||
* @param dpValue dp数值
|
||||
* @return 对应px像素值
|
||||
*/
|
||||
private int dp2px(Context context, float dpValue) {
|
||||
return (int) TypedValue.applyDimension(
|
||||
TypedValue.COMPLEX_UNIT_DIP,
|
||||
dpValue,
|
||||
context.getResources().getDisplayMetrics()
|
||||
);
|
||||
private int dp2px(final Context context, final float dpValue) {
|
||||
final float density = context.getResources().getDisplayMetrics().density;
|
||||
return (int) (dpValue * density + 0.5f);
|
||||
}
|
||||
|
||||
// ====================== 对外控制方法区 ======================
|
||||
// ====================== UI布局控制方法 ======================
|
||||
/**
|
||||
* 设置文本相对于温度条的位置
|
||||
* @param isOnRight true=文本在温度条右侧(默认),false=文本在温度条左侧
|
||||
* 设置文本在温度条的左右位置
|
||||
* @param isOnRight true=右侧,false=左侧
|
||||
*/
|
||||
public void setTextPosition(boolean isOnRight) {
|
||||
public void setTextPosition(final boolean isOnRight) {
|
||||
this.isTextOnRight = isOnRight;
|
||||
invalidate(); // 刷新布局绘制
|
||||
LogUtils.d(TAG, "setTextPosition input param=" + isOnRight);
|
||||
invalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前文本位置配置
|
||||
* @return true=右侧,false=左侧
|
||||
*/
|
||||
public boolean isTextOnRight() {
|
||||
return isTextOnRight;
|
||||
}
|
||||
|
||||
// ====================== 对外静态方法区 ======================
|
||||
// ====================== 全局静态更新入口 ======================
|
||||
/**
|
||||
* 静态外部方法:发送消息更新所有 DunTemperatureView 实例的盾值
|
||||
* 可在子线程中调用
|
||||
* @param maxValue 最高盾值
|
||||
* 全局静态方法,统一更新所有控件盾值,支持子线程调用
|
||||
* @param maxValue 最大盾值
|
||||
* @param currentValue 当前盾值
|
||||
*/
|
||||
public static void updateDunValue(int maxValue, int currentValue) {
|
||||
public static void updateDunValue(final int maxValue, final int currentValue) {
|
||||
if (sStaticHandler == null) {
|
||||
LogUtils.w(TAG, "updateDunValue: 静态Handler未初始化");
|
||||
LogUtils.w(TAG, "updateDunValue static handler uninitialized");
|
||||
return;
|
||||
}
|
||||
// 封装参数到消息
|
||||
Message msg = sStaticHandler.obtainMessage(MSG_UPDATE_DUN_VALUE);
|
||||
LogUtils.d(TAG, "static updateDunValue call, input max=" + maxValue + ", current=" + currentValue);
|
||||
final Message msg = sStaticHandler.obtainMessage(MSG_UPDATE_DUN_VALUE);
|
||||
msg.getData().putInt(KEY_MAX_VALUE, maxValue);
|
||||
msg.getData().putInt(KEY_CURRENT_VALUE, currentValue);
|
||||
// 发送消息
|
||||
sStaticHandler.sendMessage(msg);
|
||||
}
|
||||
|
||||
// ====================== 对外实例方法区 ======================
|
||||
// ====================== 盾值读写实例方法 ======================
|
||||
/**
|
||||
* 设置最高盾值
|
||||
* @param maxValue 最高盾值(需大于0)
|
||||
* 设置最大盾值,非法值会拦截并打印日志
|
||||
* @param maxValue 目标最大值,必须大于0
|
||||
*/
|
||||
public void setMaxValue(int maxValue) {
|
||||
public void setMaxValue(final int maxValue) {
|
||||
if (maxValue <= 0) {
|
||||
LogUtils.w(TAG, "setMaxValue: 最高盾值必须大于0,当前值=" + maxValue);
|
||||
LogUtils.w(TAG, "setMaxValue invalid input value=" + maxValue);
|
||||
return;
|
||||
}
|
||||
this.mMaxValue = maxValue;
|
||||
// 限制当前值不超过最大值
|
||||
mCurrentValue = Math.min(mCurrentValue, maxValue);
|
||||
invalidate(); // 重绘控件
|
||||
LogUtils.d(TAG, "setMaxValue update max=" + maxValue + ", clamp current=" + mCurrentValue);
|
||||
invalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置当前盾值
|
||||
* @param currentValue 当前盾值(范围 0~maxValue)
|
||||
* 设置当前盾值,自动限制0~maxValue区间
|
||||
* @param currentValue 目标当前盾值
|
||||
*/
|
||||
public void setCurrentValue(int currentValue) {
|
||||
int oldValue = this.mCurrentValue;
|
||||
public void setCurrentValue(final int currentValue) {
|
||||
final int oldVal = this.mCurrentValue;
|
||||
this.mCurrentValue = Math.max(0, Math.min(currentValue, mMaxValue));
|
||||
if (oldValue != this.mCurrentValue) {
|
||||
LogUtils.d(TAG, "setCurrentValue: 当前盾值从" + oldValue + "更新为" + mCurrentValue);
|
||||
invalidate(); // 重绘控件
|
||||
if (oldVal != this.mCurrentValue) {
|
||||
LogUtils.d(TAG, "setCurrentValue change value from " + oldVal + " to " + mCurrentValue);
|
||||
invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前盾值
|
||||
*/
|
||||
public int getCurrentValue() {
|
||||
return mCurrentValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最高盾值
|
||||
*/
|
||||
public int getMaxValue() {
|
||||
return mMaxValue;
|
||||
}
|
||||
|
||||
// ====================== 渐变、颜色自定义配置 ======================
|
||||
/**
|
||||
* 设置自定义渐变颜色
|
||||
* @param colors 渐变颜色数组(至少2种颜色)
|
||||
* @param positions 颜色位置数组(与colors长度一致,0.0~1.0)
|
||||
* 自定义渐变颜色数组与位置节点
|
||||
* @param colors 颜色数组,长度≥2
|
||||
* @param positions 渐变节点数组,长度与colors一致
|
||||
*/
|
||||
public void setGradientColors(int[] colors, float[] positions) {
|
||||
public void setGradientColors(final int[] colors, final float[] positions) {
|
||||
if (colors == null || colors.length < 2 || positions == null || positions.length != colors.length) {
|
||||
LogUtils.w(TAG, "setGradientColors: 渐变颜色参数不合法,颜色数组长度=" + (colors == null ? "null" : colors.length));
|
||||
LogUtils.w(TAG, "setGradientColors invalid param, color array length=" + (colors == null ? "null" : colors.length));
|
||||
return;
|
||||
}
|
||||
this.mGradientColors = colors;
|
||||
this.mGradientPositions = positions;
|
||||
LogUtils.d(TAG, "setGradientColors: 自定义渐变颜色已设置");
|
||||
LogUtils.d(TAG, "setGradientColors custom gradient config applied");
|
||||
invalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置温度条边框颜色
|
||||
* 更新温度条边框颜色
|
||||
* @param color 目标色值
|
||||
*/
|
||||
public void setBorderColor(int color) {
|
||||
public void setBorderColor(final int color) {
|
||||
this.mBorderColor = color;
|
||||
mThermometerPaint.setColor(color);
|
||||
LogUtils.d(TAG, "setBorderColor: 边框颜色已更新");
|
||||
LogUtils.d(TAG, "setBorderColor update border color");
|
||||
invalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置文本颜色
|
||||
* 更新竖排文本颜色
|
||||
* @param color 目标色值
|
||||
*/
|
||||
public void setTextColor(int color) {
|
||||
public void setTextColor(final int color) {
|
||||
this.mTextColor = color;
|
||||
mTextPaint.setColor(color);
|
||||
LogUtils.d(TAG, "setTextColor: 文本颜色已更新");
|
||||
LogUtils.d(TAG, "setTextColor update text color");
|
||||
invalidate();
|
||||
}
|
||||
|
||||
// ====================== 生命周期方法 ======================
|
||||
// ====================== 视图生命周期回调 ======================
|
||||
@Override
|
||||
protected void onDetachedFromWindow() {
|
||||
super.onDetachedFromWindow();
|
||||
// 控件从窗口移除时,从缓存中清除,避免内存泄漏
|
||||
sViewCache.remove(this);
|
||||
LogUtils.d(TAG, "onDetachedFromWindow: 控件实例已从缓存移除");
|
||||
LogUtils.d(TAG, "onDetachedFromWindow remove view from static cache");
|
||||
}
|
||||
|
||||
// ====================== 测量与绘制区 ======================
|
||||
// ====================== 测量绘制工具私有方法 ======================
|
||||
/**
|
||||
* 测量辅助函数
|
||||
* 测量尺寸计算工具
|
||||
* @param defaultSize 默认尺寸
|
||||
* @param measureSpec 测量规格
|
||||
* @return 最终计算尺寸
|
||||
*/
|
||||
private int measureSize(int defaultSize, int measureSpec) {
|
||||
private int measureSize(final int defaultSize, final int measureSpec) {
|
||||
int result = defaultSize;
|
||||
int specMode = MeasureSpec.getMode(measureSpec);
|
||||
int specSize = MeasureSpec.getSize(measureSpec);
|
||||
final int specMode = MeasureSpec.getMode(measureSpec);
|
||||
final int specSize = MeasureSpec.getSize(measureSpec);
|
||||
if (specMode == MeasureSpec.EXACTLY) {
|
||||
result = specSize;
|
||||
} else if (specMode == MeasureSpec.AT_MOST) {
|
||||
@@ -302,90 +293,83 @@ public class DunTemperatureView extends View {
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||
// 强制控件整体左右内边距=0,彻底消除外部边距
|
||||
setPadding(0, getPaddingTop(), 0, getPaddingBottom());
|
||||
|
||||
// 控件宽度=温度条宽度 + 文本区宽度(均为5dp转px,无额外空白)
|
||||
int defaultWidth = mThermometerWidth + mTextAreaWidth;
|
||||
int width = measureSize(defaultWidth, widthMeasureSpec);
|
||||
int height = measureSize(DEFAULT_HEIGHT, heightMeasureSpec);
|
||||
final int defaultWidth = mThermometerWidth + mTextAreaWidth;
|
||||
final int width = measureSize(defaultWidth, widthMeasureSpec);
|
||||
final int height = measureSize(DEFAULT_HEIGHT, heightMeasureSpec);
|
||||
setMeasuredDimension(width, height);
|
||||
|
||||
// 根据文本位置配置,计算温度条矩形坐标
|
||||
float thermometerLeft, thermometerRight;
|
||||
if (isTextOnRight) {
|
||||
// 文本在右侧:温度条靠左,右接文本区
|
||||
float thermometerLeft;
|
||||
float thermometerRight;
|
||||
final boolean textRightFlag = isTextOnRight;
|
||||
if (textRightFlag) {
|
||||
thermometerLeft = 0;
|
||||
thermometerRight = thermometerLeft + mThermometerWidth;
|
||||
} else {
|
||||
// 文本在左侧:温度条靠右,左接文本区
|
||||
thermometerLeft = width - mThermometerWidth;
|
||||
thermometerRight = width;
|
||||
}
|
||||
float thermometerTop = getPaddingTop();
|
||||
float thermometerBottom = height - getPaddingBottom();
|
||||
final float thermometerTop = getPaddingTop();
|
||||
final float thermometerBottom = height - getPaddingBottom();
|
||||
mThermometerRect.set(thermometerLeft, thermometerTop, thermometerRight, thermometerBottom);
|
||||
|
||||
LogUtils.v(TAG, "onMeasure: 文本位置=" + (isTextOnRight ? "右侧" : "左侧") + ",控件尺寸=" + width + "x" + height + ",温度条区域=" + mThermometerRect.toShortString());
|
||||
LogUtils.v(TAG, "onMeasure textPos=" + (textRightFlag ? "RIGHT" : "LEFT") + ", view size=" + width + "x" + height);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
protected void onDraw(final Canvas canvas) {
|
||||
super.onDraw(canvas);
|
||||
// 1. 绘制温度条边框(5dp宽度,小圆角适配)
|
||||
// 绘制温度条外边框
|
||||
canvas.drawRoundRect(mThermometerRect, 3, 3, mThermometerPaint);
|
||||
|
||||
// 2. 计算填充高度(根据当前值占最大值的比例)
|
||||
float fillRatio = (float) mCurrentValue / mMaxValue;
|
||||
float fillHeight = mThermometerRect.height() * fillRatio;
|
||||
float fillTop = mThermometerRect.bottom - fillHeight;
|
||||
// 计算渐变填充区域高度比例
|
||||
final float fillRatio = (float) mCurrentValue / mMaxValue;
|
||||
final float fillHeight = mThermometerRect.height() * fillRatio;
|
||||
final float fillTop = mThermometerRect.bottom - fillHeight;
|
||||
|
||||
// 3. 绘制渐变填充部分(左右贴边框,无间距)
|
||||
// 绘制渐变填充色块
|
||||
if (fillHeight > 0) {
|
||||
RectF fillRect = new RectF(
|
||||
mThermometerRect.left + FILL_PADDING_HORIZONTAL,
|
||||
fillTop + FILL_PADDING_VERTICAL,
|
||||
mThermometerRect.right - FILL_PADDING_HORIZONTAL,
|
||||
mThermometerRect.bottom - FILL_PADDING_VERTICAL
|
||||
final RectF fillRect = new RectF(
|
||||
mThermometerRect.left + FILL_PADDING_HORIZONTAL,
|
||||
fillTop + FILL_PADDING_VERTICAL,
|
||||
mThermometerRect.right - FILL_PADDING_HORIZONTAL,
|
||||
mThermometerRect.bottom - FILL_PADDING_VERTICAL
|
||||
);
|
||||
LinearGradient gradient = new LinearGradient(
|
||||
fillRect.centerX(), fillRect.bottom,
|
||||
fillRect.centerX(), fillRect.top,
|
||||
mGradientColors,
|
||||
mGradientPositions,
|
||||
Shader.TileMode.CLAMP
|
||||
final LinearGradient gradient = new LinearGradient(
|
||||
fillRect.centerX(), fillRect.bottom,
|
||||
fillRect.centerX(), fillRect.top,
|
||||
mGradientColors,
|
||||
mGradientPositions,
|
||||
Shader.TileMode.CLAMP
|
||||
);
|
||||
mFillPaint.setShader(gradient);
|
||||
canvas.drawRoundRect(fillRect, 2, 2, mFillPaint);
|
||||
mFillPaint.setShader(null);
|
||||
}
|
||||
|
||||
// 4. 绘制文本(5dp固定宽度文本区,底部对齐竖排,文字居中)
|
||||
String text = String.format("%d/%d", mCurrentValue, mMaxValue);
|
||||
if (text.isEmpty()) return;
|
||||
// 竖排文本绘制逻辑
|
||||
final String text = String.format("%d/%d", mCurrentValue, mMaxValue);
|
||||
if (text.length() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
float textBaseX;
|
||||
if (isTextOnRight) {
|
||||
// 文本在右侧:X=温度条右边缘 + 文本区宽度的一半(紧贴温度条)
|
||||
final boolean textRightFlag = isTextOnRight;
|
||||
if (textRightFlag) {
|
||||
textBaseX = mThermometerRect.right + (mTextAreaWidth / 2f);
|
||||
} else {
|
||||
// 文本在左侧:X=文本区宽度的一半(紧贴控件左边缘)
|
||||
textBaseX = mTextAreaWidth / 2f;
|
||||
}
|
||||
|
||||
// 文本绘制参数(适配5dp窄区域,缩小字间距提升紧凑度)
|
||||
float singleCharHeight = mTextPaint.getTextSize() + (TEXT_CHAR_SPACING - 2f); // 字间距减为6f
|
||||
float totalTextHeight = (singleCharHeight * text.length()) - (TEXT_CHAR_SPACING - 2f);
|
||||
Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
|
||||
float charBottomOffset = fontMetrics.bottom;
|
||||
final float singleCharHeight = mTextPaint.getTextSize() + (TEXT_CHAR_SPACING - 2f);
|
||||
final Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
|
||||
final float charBottomOffset = fontMetrics.bottom;
|
||||
final float startTextY = getHeight() - getPaddingBottom() - charBottomOffset;
|
||||
|
||||
// 文本起始Y(底部对齐控件底部,无间距)
|
||||
float startTextY = getHeight() - getPaddingBottom() - charBottomOffset;
|
||||
|
||||
// 逐字竖排绘制(文字居中于5dp文本区,无超出)
|
||||
for (int i = 0; i < text.length(); i++) {
|
||||
char singleChar = text.charAt(i);
|
||||
float currentTextY = startTextY - (i * singleCharHeight);
|
||||
final char singleChar = text.charAt(i);
|
||||
final float currentTextY = startTextY - (i * singleCharHeight);
|
||||
canvas.drawText(String.valueOf(singleChar), textBaseX, currentTextY, mTextPaint);
|
||||
}
|
||||
}
|
||||
|
||||
+90
-21
@@ -13,7 +13,9 @@ import android.widget.LinearLayout;
|
||||
import android.widget.Switch;
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
||||
import cc.winboll.studio.contacts.R;
|
||||
import cc.winboll.studio.contacts.services.LimitedTimeSpecialChannelService;
|
||||
import cc.winboll.studio.contacts.dun.Rules;
|
||||
import cc.winboll.studio.contacts.model.SettingsBean;
|
||||
import cc.winboll.studio.contacts.services.LimitedTimeGreenChannelService;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
|
||||
@@ -21,7 +23,7 @@ import cc.winboll.studio.libappbase.ToastUtils;
|
||||
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
|
||||
* @Date 2026/04/18 15:05:00 (GMT+8)
|
||||
* @LastEditTime 2026/04/22 17:30:00 (GMT+8)
|
||||
* @Describe 限时特殊通道视图
|
||||
* @Describe 限时绿色通道视图
|
||||
* 功能说明:
|
||||
* 1. Switch 打开时,EditText 不可编辑,布局背景变为绿色,加减按钮不可用
|
||||
* 2. Switch 关闭时,EditText 可编辑,布局背景恢复为白色,加减按钮恢复可用
|
||||
@@ -31,10 +33,10 @@ import cc.winboll.studio.libappbase.ToastUtils;
|
||||
* 6. 新增快速设置按钮(btn_thisSeconds),点击直接设置为当前量级3600秒
|
||||
* 7. [新增] 服务销毁时,恢复EditText显示为倒计时剩余秒数
|
||||
*/
|
||||
public class LimitedTimeSpecialChannelView extends LinearLayout {
|
||||
public class LimitedTimeGreenChannelView extends LinearLayout {
|
||||
|
||||
// ====================== 常量定义 =========================
|
||||
public static final String TAG = "LimitedTimeSpecialChannelView";
|
||||
public static final String TAG = "LimitedTimeGreenChannelView";
|
||||
// 定义秒数增量的量级
|
||||
private static final long SECONDS_INCREMENT = 3600;
|
||||
|
||||
@@ -50,17 +52,17 @@ public class LimitedTimeSpecialChannelView extends LinearLayout {
|
||||
private android.content.BroadcastReceiver mCountdownReceiver;
|
||||
|
||||
// ====================== 构造函数 =========================
|
||||
public LimitedTimeSpecialChannelView(Context context) {
|
||||
public LimitedTimeGreenChannelView(Context context) {
|
||||
super(context);
|
||||
initView(context);
|
||||
}
|
||||
|
||||
public LimitedTimeSpecialChannelView(Context context, AttributeSet attrs) {
|
||||
public LimitedTimeGreenChannelView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
initView(context);
|
||||
}
|
||||
|
||||
public LimitedTimeSpecialChannelView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
public LimitedTimeGreenChannelView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
initView(context);
|
||||
}
|
||||
@@ -71,19 +73,22 @@ public class LimitedTimeSpecialChannelView extends LinearLayout {
|
||||
this.mContext = context;
|
||||
|
||||
// 加载布局文件
|
||||
LayoutInflater.from(context).inflate(R.layout.view_limitedtimespecialchannel, this, true);
|
||||
LayoutInflater.from(context).inflate(R.layout.view_limitedtimegreenchannel, this, true);
|
||||
LogUtils.i(TAG, "布局文件加载完成");
|
||||
|
||||
// 绑定控件ID
|
||||
bindViews();
|
||||
// 初始化所有按钮点击事件
|
||||
initButtons();
|
||||
// 初始化开关状态
|
||||
// 初始化开关状态(从持久化存储加载)
|
||||
initSwitchState();
|
||||
// 注册广播监听
|
||||
registerServiceDestroyedListener();
|
||||
registerCountdownTickListener();
|
||||
|
||||
// 加载保存的绿色通道启用状态并设置Switch
|
||||
loadSavedGreenChannelState();
|
||||
|
||||
LogUtils.i(TAG, "视图初始化流程结束");
|
||||
}
|
||||
|
||||
@@ -191,15 +196,75 @@ public class LimitedTimeSpecialChannelView extends LinearLayout {
|
||||
// 根据开关状态更新UI可操作状态
|
||||
updateUIState(!isChecked);
|
||||
|
||||
// 保存绿色通道启用状态到持久化存储
|
||||
saveGreenChannelState(isChecked);
|
||||
|
||||
// 发送绿色通道状态变更广播
|
||||
LimitedTimeGreenChannelService.sendGreenChannelStateChangedBroadcast(mContext, isChecked);
|
||||
|
||||
if (isChecked) {
|
||||
startLimitedTimeSpecialChannelService();
|
||||
startLimitedTimeGreenChannelService();
|
||||
} else {
|
||||
LimitedTimeSpecialChannelService.stopService(mContext);
|
||||
LimitedTimeGreenChannelService.stopService(mContext);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 从持久化存储加载绿色通道启用状态并设置Switch
|
||||
*/
|
||||
private void loadSavedGreenChannelState() {
|
||||
Rules rules = Rules.getInstance(mContext);
|
||||
if (rules == null) {
|
||||
LogUtils.e(TAG, "loadSavedGreenChannelState: Rules实例获取失败");
|
||||
return;
|
||||
}
|
||||
SettingsBean settings = rules.getSettingsModel();
|
||||
if (settings == null) {
|
||||
LogUtils.e(TAG, "loadSavedGreenChannelState: SettingsBean获取失败");
|
||||
return;
|
||||
}
|
||||
|
||||
boolean savedState = settings.isEnableGreenChannel();
|
||||
LogUtils.i(TAG, "loadSavedGreenChannelState: 加载保存的绿色通道状态=" + savedState);
|
||||
|
||||
// 设置Switch状态(不会触发监听器,因为监听器在initSwitchState中已设置)
|
||||
if (mSwEnable != null && mSwEnable.isChecked() != savedState) {
|
||||
mSwEnable.setChecked(savedState);
|
||||
// 手动更新UI状态
|
||||
updateUIState(!savedState);
|
||||
LogUtils.i(TAG, "loadSavedGreenChannelState: Switch状态已恢复为" + (savedState ? "开启" : "关闭"));
|
||||
}
|
||||
|
||||
// 如果保存的状态是开启,启动服务
|
||||
if (savedState && !LimitedTimeGreenChannelService.isServiceRunning()) {
|
||||
LogUtils.i(TAG, "loadSavedGreenChannelState: 启动绿色通道服务");
|
||||
startLimitedTimeGreenChannelService();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存绿色通道启用状态到持久化存储
|
||||
* @param isEnabled 是否启用
|
||||
*/
|
||||
private void saveGreenChannelState(boolean isEnabled) {
|
||||
Rules rules = Rules.getInstance(mContext);
|
||||
if (rules == null) {
|
||||
LogUtils.e(TAG, "saveGreenChannelState: Rules实例获取失败,保存失败");
|
||||
return;
|
||||
}
|
||||
SettingsBean settings = rules.getSettingsModel();
|
||||
if (settings == null) {
|
||||
LogUtils.e(TAG, "saveGreenChannelState: SettingsBean获取失败,保存失败");
|
||||
return;
|
||||
}
|
||||
|
||||
settings.setIsEnableGreenChannel(isEnabled);
|
||||
rules.saveDun();
|
||||
LogUtils.i(TAG, "saveGreenChannelState: 绿色通道启用状态已保存为" + isEnabled);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一更新UI可操作状态
|
||||
* @param enabled 可用状态
|
||||
@@ -222,11 +287,11 @@ public class LimitedTimeSpecialChannelView extends LinearLayout {
|
||||
*/
|
||||
private void registerServiceDestroyedListener() {
|
||||
LogUtils.i(TAG, "注册服务销毁广播监听");
|
||||
IntentFilter filter = new IntentFilter(LimitedTimeSpecialChannelService.ACTION_SERVICE_DESTROYED);
|
||||
IntentFilter filter = new IntentFilter(LimitedTimeGreenChannelService.ACTION_SERVICE_DESTROYED);
|
||||
LocalBroadcastManager.getInstance(mContext).registerReceiver(new android.content.BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (LimitedTimeSpecialChannelService.ACTION_SERVICE_DESTROYED.equals(intent.getAction())) {
|
||||
if (LimitedTimeGreenChannelService.ACTION_SERVICE_DESTROYED.equals(intent.getAction())) {
|
||||
LogUtils.i(TAG, "收到服务销毁广播,执行恢复操作");
|
||||
|
||||
// 1. 恢复开关状态
|
||||
@@ -236,7 +301,7 @@ public class LimitedTimeSpecialChannelView extends LinearLayout {
|
||||
|
||||
// 2. [核心修改] 检索并设置倒计时剩余秒数参数
|
||||
// 从广播附加数据中获取之前服务发送的剩余秒数
|
||||
long remainingSeconds = intent.getLongExtra(LimitedTimeSpecialChannelService.EXTRA_REMAINING_SECONDS, 0);
|
||||
long remainingSeconds = intent.getLongExtra(LimitedTimeGreenChannelService.EXTRA_REMAINING_SECONDS, 0);
|
||||
// 实际倒计时消耗冲正
|
||||
remainingSeconds -= 1;
|
||||
// 倒计时秒数0值保底
|
||||
@@ -252,6 +317,10 @@ public class LimitedTimeSpecialChannelView extends LinearLayout {
|
||||
LogUtils.i(TAG, "已将EditText设置为剩余秒数: " + remainingSeconds);
|
||||
}
|
||||
|
||||
// 5. 保存绿色通道关闭状态并发送广播
|
||||
saveGreenChannelState(false);
|
||||
LimitedTimeGreenChannelService.sendGreenChannelStateChangedBroadcast(mContext, false);
|
||||
|
||||
//ToastUtils.show("服务已销毁,恢复倒计时剩余状态");
|
||||
}
|
||||
}
|
||||
@@ -266,8 +335,8 @@ public class LimitedTimeSpecialChannelView extends LinearLayout {
|
||||
mCountdownReceiver = new android.content.BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (LimitedTimeSpecialChannelService.ACTION_COUNTDOWN_TICK.equals(intent.getAction())) {
|
||||
long remainingSeconds = intent.getLongExtra(LimitedTimeSpecialChannelService.EXTRA_REMAINING_SECONDS, 0);
|
||||
if (LimitedTimeGreenChannelService.ACTION_COUNTDOWN_TICK.equals(intent.getAction())) {
|
||||
long remainingSeconds = intent.getLongExtra(LimitedTimeGreenChannelService.EXTRA_REMAINING_SECONDS, 0);
|
||||
if (mEtSeconds != null) {
|
||||
mEtSeconds.setText(String.valueOf(remainingSeconds));
|
||||
}
|
||||
@@ -278,7 +347,7 @@ public class LimitedTimeSpecialChannelView extends LinearLayout {
|
||||
}
|
||||
};
|
||||
|
||||
IntentFilter filter = new IntentFilter(LimitedTimeSpecialChannelService.ACTION_COUNTDOWN_TICK);
|
||||
IntentFilter filter = new IntentFilter(LimitedTimeGreenChannelService.ACTION_COUNTDOWN_TICK);
|
||||
LocalBroadcastManager.getInstance(mContext).registerReceiver(mCountdownReceiver, filter);
|
||||
LogUtils.i(TAG, "倒计时广播注册成功");
|
||||
}
|
||||
@@ -286,8 +355,8 @@ public class LimitedTimeSpecialChannelView extends LinearLayout {
|
||||
/**
|
||||
* 启动限时特殊通道服务
|
||||
*/
|
||||
private void startLimitedTimeSpecialChannelService() {
|
||||
LogUtils.i(TAG, "准备启动限时特殊通道服务");
|
||||
private void startLimitedTimeGreenChannelService() {
|
||||
LogUtils.i(TAG, "准备启动限时绿色通道服务");
|
||||
String secondsStr = mEtSeconds.getText().toString().trim();
|
||||
if (secondsStr.isEmpty()) {
|
||||
LogUtils.w(TAG, "输入框为空,使用默认值 3600 秒");
|
||||
@@ -299,8 +368,8 @@ public class LimitedTimeSpecialChannelView extends LinearLayout {
|
||||
long delayMillis = seconds * 1000;
|
||||
|
||||
LogUtils.i(TAG, "启动参数 - 输入秒数: " + seconds + ", 转换毫秒: " + delayMillis);
|
||||
//ToastUtils.show("调用 LimitedTimeSpecialChannelService 服务");
|
||||
LimitedTimeSpecialChannelService.startService(mContext, delayMillis);
|
||||
//ToastUtils.show("调用 LimitedTimeGreenChannelService 服务");
|
||||
LimitedTimeGreenChannelService.startService(mContext, delayMillis);
|
||||
} catch (NumberFormatException e) {
|
||||
LogUtils.e(TAG, "输入的秒数格式错误: " + secondsStr, e);
|
||||
}
|
||||
@@ -67,12 +67,12 @@
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="特殊通道设置:"/>
|
||||
android:text="绿色通道设置:"/>
|
||||
|
||||
<cc.winboll.studio.contacts.views.LimitedTimeSpecialChannelView
|
||||
<cc.winboll.studio.contacts.views.LimitedTimeGreenChannelView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/view_LimitedTimeSpecialChannel"/>
|
||||
android:id="@+id/view_LimitedTimeGreenChannel"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
@@ -28,6 +28,14 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="right">
|
||||
|
||||
<Button
|
||||
android:id="@+id/btn_debug_green_channel"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="绿色通道"
|
||||
android:textSize="12sp"
|
||||
android:layout_margin="2dp"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/btn_debug_logview"
|
||||
android:layout_width="wrap_content"
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/ll_green_channel_floating"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:background="@android:color/white"
|
||||
android:elevation="4dp">
|
||||
|
||||
<cc.winboll.studio.contacts.views.LimitedTimeGreenChannelView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/view_LimitedTimeGreenChannel"/>
|
||||
|
||||
</LinearLayout>
|
||||
Reference in New Issue
Block a user