@@ -51,15 +51,14 @@ import cc.winboll.studio.powerbell.views.BatteryDrawable;
import cc.winboll.studio.powerbell.views.VerticalSeekBar ;
/**
* 主活动类( Java 7 兼容 | 首屏加速+ 窗体缓存)
* 主活动类( Java 7 兼容 | 移除 窗体缓存)
* 核心优化点:
* 1. ViewHolder 缓存控件 + ViewStub 延迟加载(原有)
* 2. savedInstanceState 窗体缓存(新增):缓存视图状态/数据,快速重建窗体
* 3. 资源异步加载 + 非关键任务异步化(原有)
* 1. ViewHolder 缓存控件 + ViewStub 延迟加载
* 2. 资源异步加载 + 非关键任务异步化
*/
public class MainActivity extends WinBoLLActivity {
// ======================== 静态常量(新增: savedInstanceState 缓存 键)========================
// ======================== 静态常量(移除缓存相关 键)========================
public static final String TAG = " MainActivity " ;
private static final int REQUEST_WRITE_STORAGE_PERMISSION = 1001 ;
public static final int MSG_RELOAD_APPCONFIG = 0 ;
@@ -67,17 +66,7 @@ public class MainActivity extends WinBoLLActivity {
public static final int MSG_LOAD_BACKGROUND = 2 ;
private static final int DELAY_LOAD_NON_CRITICAL = 500 ;
// 缓存键(统一管理,避免键名重复)
private static final String KEY_VIEW_STATE = " key_view_state " ; // 视图状态缓存键
private static final String KEY_CHARGE_REMINDER = " key_charge_reminder " ; // 充电提醒值
private static final String KEY_USEGE_REMINDER = " key_usege_reminder " ; // 耗电提醒值
private static final String KEY_CURRENT_VALUE = " key_current_value " ; // 当前电量值
private static final String KEY_IS_SERVICE_ENABLE = " key_is_service_enable " ; // 服务开关状态
private static final String KEY_IS_CHARGE_ENABLE = " key_is_charge_enable " ; // 充电提醒开关
private static final String KEY_IS_USEGE_ENABLE = " key_is_usege_enable " ; // 耗电提醒开关
private static final String KEY_BACKGROUND_BEAN = " key_background_bean " ; // 背景配置Bean
// ======================== 成员属性(保持原有,新增缓存标识)========================
// ======================== 成员属性(移除缓存标识)========================
public static MainActivity _mMainActivity ;
static MainViewFragment _mMainViewFragment ;
static Handler _mHandler ;
@@ -99,10 +88,6 @@ public class MainActivity extends WinBoLLActivity {
private BatteryDrawable mChargeReminderValueBatteryDrawable ;
private BatteryDrawable mUsegeReminderValueBatteryDrawable ;
// 新增: 缓存标识( 判断是否从savedInstanceState恢复, 避免重复初始化)
private boolean isRestoredFromCache = false ;
// ======================== 视图缓存容器( ViewHolder 不变)========================
private static class ViewHolder {
BackgroundView backgroundView ;
@@ -115,8 +100,7 @@ public class MainActivity extends WinBoLLActivity {
ImageView ivCurrentBattery , ivChargeReminderBattery , ivUsegeReminderBattery ;
}
// ======================== 重写生命周期( 核心: savedInstanceState 缓存/恢复)========================
// ======================== 重写生命周期(移除缓存恢复/保存逻辑)========================
@Override
public Activity getActivity ( ) {
return this ;
@@ -129,80 +113,27 @@ public class MainActivity extends WinBoLLActivity {
@Override
protected void onCreate ( Bundle savedInstanceState ) {
LogUtils . d ( TAG , " onCreate(...) - 窗体缓存优化: savedInstanceState = " + ( savedInstanceState ! = null ) ) ;
LogUtils . d ( TAG , " onCreate(...) - 移除 窗体缓存" ) ;
super . onCreate ( savedInstanceState ) ;
initGlobalHandler ( ) ;
// 第一步: 优先从savedInstanceState恢复窗体( 核心优化)
if ( savedInstanceState ! = null ) {
restoreFromCache ( savedInstanceState ) ; // 从缓存恢复视图/数据
isRestoredFromCache = true ; // 标记为缓存恢复,跳过重复初始化
}
// 恢复原始初始化流程:直接加载布局+初始化
setContentView ( R . layout . activity_main ) ;
initCoreUtilsAsync ( ) ;
initViewHolder ( ) ;
initCriticalView ( ) ;
loadNonCriticalViewDelayed ( ) ;
// 第二步:无缓存时,正常初始化(保持原有逻辑,新增判断)
if ( ! isRestoredFromCache ) {
setContentView ( R . layout . activity_main ) ; // 加载布局(仅首次/无缓存时执行)
initCoreUtilsAsync ( ) ; // 异步初始化工具类
initViewHolder ( ) ; // 绑定控件(仅首次/无缓存时执行)
initCriticalView ( ) ; // 初始化核心视图
loadNonCriticalViewDelayed ( ) ; // 延迟加载非核心视图
}
// 第三步:权限申请(无论是否缓存,都需确保权限有效)
// 权限申请
PermissionUtils . getInstance ( ) . checkAndRequestStoragePermission ( this ) ;
}
/**
* 重写 onSaveInstanceState: 保存视图状态/数据到 saved InstanceState(关键)
* 当Activity销毁前( 如旋转屏幕、后台回收) , 自动调用此方法缓存数据
*/
@Override
protected void onSaveInstanceState ( Bundle outState ) {
super . onSaveInstanceState ( outState ) ;
LogUtils . d ( TAG , " onSaveInstanceState - 缓存窗体状态/数据 " ) ;
// 1. 缓存控件数据(避免重建后重复读取配置)
if ( mViewHolder ! = null & & mAppConfigUtils ! = null ) {
outState . putInt ( KEY_CHARGE_REMINDER , mAppConfigUtils . getChargeReminderValue ( ) ) ;
outState . putInt ( KEY_USEGE_REMINDER , mAppConfigUtils . getUsegeReminderValue ( ) ) ;
outState . putInt ( KEY_CURRENT_VALUE , mAppConfigUtils . getCurrentValue ( ) ) ;
outState . putBoolean ( KEY_IS_SERVICE_ENABLE , mAppConfigUtils . getIsEnableService ( ) ) ;
outState . putBoolean ( KEY_IS_CHARGE_ENABLE , mViewHolder . cbIsEnableChargeReminder . isChecked ( ) ) ;
outState . putBoolean ( KEY_IS_USEGE_ENABLE , mViewHolder . cbIsEnableUsegeReminder . isChecked ( ) ) ;
}
// 2. 缓存背景配置(避免重建后重复加载背景图)
if ( mBgSourceUtils ! = null ) {
BackgroundBean backgroundBean = mBgSourceUtils . getCurrentBackgroundBean ( ) ;
if ( backgroundBean ! = null ) {
outState . putSerializable ( KEY_BACKGROUND_BEAN , backgroundBean ) ; // 需BackgroundBean实现Serializable
}
}
// 3. 缓存视图状态( 可选: 如SeekBar进度、控件可见性等)
if ( mViewHolder ! = null ) {
outState . putInt ( KEY_VIEW_STATE + " _charge_seek " , mViewHolder . chargeReminderSeekBar . getProgress ( ) ) ;
outState . putInt ( KEY_VIEW_STATE + " _usege_seek " , mViewHolder . usegeReminderSeekBar . getProgress ( ) ) ;
}
}
/**
* 重写 onRestoreInstanceState: 从 savedInstanceState 恢复数据( 可选, onCreate中已处理)
* 确保在onStart后、onResume前恢复, 避免视图状态覆盖
*/
@Override
protected void onRestoreInstanceState ( Bundle savedInstanceState ) {
super . onRestoreInstanceState ( savedInstanceState ) ;
if ( savedInstanceState ! = null & & ! isRestoredFromCache ) {
restoreFromCache ( savedInstanceState ) ;
isRestoredFromCache = true ;
}
}
// 移除 onSaveInstanceState 方法
// 移除 onRestore InstanceState 方法
@Override
protected void onDestroy ( ) {
super . onDestroy ( ) ;
// 释放资源(保持原有)
if ( mADsBannerView ! = null ) {
mADsBannerView . releaseAdResources ( ) ;
}
@@ -211,23 +142,15 @@ public class MainActivity extends WinBoLLActivity {
if ( _mHandler ! = null ) {
_mHandler . removeCallbacksAndMessages ( null ) ;
}
// 重置缓存标识
isRestoredFromCache = false ;
}
@Override
protected void onResume ( ) {
super . onResume ( ) ;
// 缓存恢复后,无需重复发送加载消息(仅首次/无缓存时执行)
if ( ! isRestoredFromCache ) {
if ( _mHandler ! = null ) {
_mHandler . sendEmptyMessage ( MSG_LOAD_BACKGROUND ) ;
}
} else {
// 缓存恢复后,快速刷新视图(避免状态不一致)
refreshViewFromCache ( ) ;
// 移除 缓存恢复后的刷新逻辑,直接发送加载背景消息
if ( _mHandler ! = null ) {
_mHandler . sendEmptyMessage ( MSG_LOAD_BACKGROUND ) ;
}
// 恢复广告(保持原有)
if ( mADsBannerView ! = null ) {
mADsBannerView . resumeADs ( MainActivity . this ) ;
}
@@ -312,94 +235,10 @@ public class MainActivity extends WinBoLLActivity {
}
}
// ======================== 新增:窗体缓存核心方法(恢复+刷新)========================
/**
* 从 savedInstanceState 恢复视图和数据(核心)
* 避免重复加载布局、绑定控件、读取配置,加快窗体重建速度
*/
private void restoreFromCache ( Bundle savedInstanceState ) {
LogUtils . d ( TAG , " restoreFromCache - 从缓存恢复窗体 " ) ;
// 1. 恢复工具类(无需重新初始化,直接获取实例)
mApplication = ( App ) getApplication ( ) ;
mAppConfigUtils = App . getAppConfigUtils ( getActivity ( ) ) ;
mBgSourceUtils = BackgroundSourceUtils . getInstance ( getActivity ( ) ) ;
// 2. 恢复ViewHolder( 仅绑定控件, 不重新inflate布局)
if ( mViewHolder = = null ) {
mViewHolder = new ViewHolder ( ) ;
initViewHolder ( ) ; // 复用原有绑定逻辑,避免重复代码
}
// 3. 恢复背景配置( 直接设置缓存的BackgroundBean, 避免重复读取)
BackgroundBean cachedBean = ( BackgroundBean ) savedInstanceState . getSerializable ( KEY_BACKGROUND_BEAN ) ;
if ( cachedBean ! = null & & mViewHolder . backgroundView ! = null ) {
mViewHolder . backgroundView . loadBackgroundBean ( cachedBean ) ;
//mBgSourceUtils.setCurrentBackgroundBean(cachedBean); // 同步更新工具类缓存
}
// 4. 恢复控件数据( 从缓存读取, 避免重复调用AppConfigUtils)
int chargeReminder = savedInstanceState . getInt ( KEY_CHARGE_REMINDER , 80 ) ; // 默认值80
int usegeReminder = savedInstanceState . getInt ( KEY_USEGE_REMINDER , 20 ) ; // 默认值20
int currentValue = savedInstanceState . getInt ( KEY_CURRENT_VALUE , 50 ) ; // 默认值50
boolean isServiceEnable = savedInstanceState . getBoolean ( KEY_IS_SERVICE_ENABLE , false ) ;
boolean isChargeEnable = savedInstanceState . getBoolean ( KEY_IS_CHARGE_ENABLE , false ) ;
boolean isUsegeEnable = savedInstanceState . getBoolean ( KEY_IS_USEGE_ENABLE , false ) ;
// 5. 恢复电量图标(复用缓存实例,避免重新创建)
initBatteryDrawables ( ) ;
mCurrentValueBatteryDrawable . setValue ( currentValue ) ;
mChargeReminderValueBatteryDrawable . setValue ( chargeReminder ) ;
mUsegeReminderValueBatteryDrawable . setValue ( usegeReminder ) ;
// 6. 恢复控件状态(直接设置,避免重复绑定数据)
if ( mViewHolder ! = null ) {
mViewHolder . cbIsEnableChargeReminder . setChecked ( isChargeEnable ) ;
mViewHolder . cbIsEnableUsegeReminder . setChecked ( isUsegeEnable ) ;
mViewHolder . swIsEnableService . setChecked ( isServiceEnable ) ;
mViewHolder . chargeReminderSeekBar . setProgress ( savedInstanceState . getInt ( KEY_VIEW_STATE + " _charge_seek " , chargeReminder ) ) ;
mViewHolder . usegeReminderSeekBar . setProgress ( savedInstanceState . getInt ( KEY_VIEW_STATE + " _usege_seek " , usegeReminder ) ) ;
mViewHolder . tvChargeReminderValue . setText ( String . valueOf ( chargeReminder ) + " % " ) ;
mViewHolder . tvUsegeReminderValue . setText ( String . valueOf ( usegeReminder ) + " % " ) ;
mViewHolder . tvCurrentValue . setText ( String . valueOf ( currentValue ) + " % " ) ;
}
// 7. 恢复非核心视图(广告)
loadAdsView ( ) ;
}
// ======================== 移除缓存相关核心方法( restoreFromCache/refreshViewFromCache) ========================
/**
* 缓存恢复后,快速刷新视图(确保状态一致 )
*/
private void refreshViewFromCache ( ) {
if ( mViewHolder = = null ) return ;
// 快速设置背景色( 从缓存Bean获取)
if ( mBgSourceUtils ! = null & & mViewHolder . mainLayout ! = null ) {
BackgroundBean cachedBean = mBgSourceUtils . getCurrentBackgroundBean ( ) ;
if ( cachedBean ! = null ) {
mViewHolder . mainLayout . setBackgroundColor ( cachedBean . getPixelColor ( ) ) ;
}
}
// 快速设置电量图标(避免图标不显示)
if ( mViewHolder . ivCurrentBattery ! = null ) {
mViewHolder . ivCurrentBattery . setImageDrawable ( mCurrentValueBatteryDrawable ) ;
}
if ( mViewHolder . ivChargeReminderBattery ! = null ) {
mViewHolder . ivChargeReminderBattery . setImageDrawable ( mChargeReminderValueBatteryDrawable ) ;
}
if ( mViewHolder . ivUsegeReminderBattery ! = null ) {
mViewHolder . ivUsegeReminderBattery . setImageDrawable ( mUsegeReminderValueBatteryDrawable ) ;
}
// 重新绑定监听(确保交互正常,避免监听失效)
setViewListener ( ) ;
}
/**
* 初始化电量图标(复用逻辑,避免重复代码)
* 初始化电量图标(复用逻辑 )
*/
private void initBatteryDrawables ( ) {
if ( mCurrentValueBatteryDrawable = = null ) {
@@ -413,13 +252,11 @@ public class MainActivity extends WinBoLLActivity {
}
}
// ======================== 原有核心方法(微调:新增缓存判断,避免重复执行)========================
// ======================== 原有核心方法(移除缓存判断逻辑)========================
/**
* 刷新背景(新增 缓存判断,缓存恢复后无需重复执行 )
* 刷新背景(移除 缓存判断)
*/
public void reloadBackground ( ) {
if ( isRestoredFromCache ) return ; // 缓存恢复后,跳过重复加载
if ( mViewHolder = = null | | mBgSourceUtils = = null | | mViewHolder . backgroundView = = null ) {
LogUtils . e ( TAG , " reloadBackground: 背景加载失败(控件/工具类未初始化) " ) ;
return ;
@@ -434,10 +271,9 @@ public class MainActivity extends WinBoLLActivity {
}
/**
* 设置主页面背景颜色(新增 缓存判断)
* 设置主页面背景颜色(移除 缓存判断)
*/
void setBackgroundColor ( ) {
if ( isRestoredFromCache ) return ;
if ( isFinishing ( ) | | isDestroyed ( ) | | mViewHolder = = null | | mBgSourceUtils = = null | | mViewHolder . mainLayout = = null ) {
return ;
}
@@ -449,10 +285,9 @@ public class MainActivity extends WinBoLLActivity {
}
/**
* 设置视图数据(新增 缓存判断,缓存恢复后无需重复执行 )
* 设置视图数据(移除 缓存判断)
*/
void setViewData ( ) {
if ( isRestoredFromCache ) return ;
if ( mViewHolder = = null | | mAppConfigUtils = = null ) return ;
int nChargeReminderValue = mAppConfigUtils . getChargeReminderValue ( ) ;
@@ -464,7 +299,7 @@ public class MainActivity extends WinBoLLActivity {
mViewHolder . llRightSeekBar . setBackground ( mDrawableFrame ) ;
}
initBatteryDrawables ( ) ; // 复用初始化逻辑
initBatteryDrawables ( ) ;
if ( mViewHolder . ivCurrentBattery ! = null ) {
mCurrentValueBatteryDrawable . setValue ( nCurrentValue ) ;
mViewHolder . ivCurrentBattery . setImageDrawable ( mCurrentValueBatteryDrawable ) ;
@@ -478,7 +313,6 @@ public class MainActivity extends WinBoLLActivity {
mViewHolder . ivUsegeReminderBattery . setImageDrawable ( mUsegeReminderValueBatteryDrawable ) ;
}
// 控件数据绑定(保持原有)
if ( mViewHolder . tvChargeReminderValue ! = null ) {
mViewHolder . tvChargeReminderValue . setTextColor ( getActivity ( ) . getColor ( R . color . colorCharge ) ) ;
mViewHolder . tvChargeReminderValue . setText ( String . valueOf ( nChargeReminderValue ) + " % " ) ;
@@ -515,7 +349,6 @@ public class MainActivity extends WinBoLLActivity {
}
}
// 其他原有方法( setViewListener、setCurrentValueBattery、initCoreUtilsAsync等保持不变)
/**
* 设置视图监听(保持原有)
*/