fix: 绿色通道关闭时状态栏颜色恢复修复

- 修复状态栏默认颜色为0(透明)时无法恢复的问题
- 从主题中获取默认状态栏颜色作为fallback
- 广播接收器统一调用updateStatusBarForGreenChannel()方法
- 消除重复代码,提高可维护性
This commit is contained in:
MiMo
2026-07-20 14:30:39 +08:00
parent f09ab4036f
commit 7f53355005
@@ -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: 反注册完成");
}
}
// ====================== 主题相关函数区 ======================