package cc.winboll.studio.powerbell;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewStub;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;
import androidx.appcompat.widget.Toolbar;
import cc.winboll.studio.libaes.activitys.AboutActivity;
import cc.winboll.studio.libaes.dialogs.YesNoAlertDialog;
import cc.winboll.studio.libaes.models.APPInfo;
import cc.winboll.studio.libaes.utils.AESThemeUtil;
import cc.winboll.studio.libaes.utils.DevelopUtils;
import cc.winboll.studio.libaes.utils.WinBoLLActivityManager;
import cc.winboll.studio.libaes.views.ADsBannerView;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.powerbell.activities.BackgroundSettingsActivity;
import cc.winboll.studio.powerbell.activities.BatteryReportActivity;
import cc.winboll.studio.powerbell.activities.ClearRecordActivity;
import cc.winboll.studio.powerbell.activities.SettingsActivity;
import cc.winboll.studio.powerbell.activities.WinBoLLActivity;
import cc.winboll.studio.powerbell.fragments.MainViewFragment;
import cc.winboll.studio.powerbell.models.BackgroundBean;
import cc.winboll.studio.powerbell.services.ControlCenterService;
import cc.winboll.studio.powerbell.unittest.MainUnitTestActivity;
import cc.winboll.studio.powerbell.utils.AppConfigUtils;
import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils;
import cc.winboll.studio.powerbell.utils.PermissionUtils;
import cc.winboll.studio.powerbell.utils.ServiceUtils;
import cc.winboll.studio.powerbell.views.BackgroundView;
import cc.winboll.studio.powerbell.views.BatteryDrawable;
import cc.winboll.studio.powerbell.views.VerticalSeekBar;

/**
 * 主活动类（Java 7 兼容 | 移除窗体缓存）
 * 核心优化点：
 * 1. ViewHolder 缓存控件 + ViewStub 延迟加载
 * 2. 资源异步加载 + 非关键任务异步化
 */
public class MainActivity extends WinBoLLActivity {

    // ======================== 静态常量（移除缓存相关键）========================
    public static final String TAG = "MainActivity";
    private static final int REQUEST_READ_MEDIA_IMAGES = 1001;
    public static final int MSG_RELOAD_APPCONFIG = 0;
    public static final int MSG_CURRENTVALUEBATTERY = 1;
    public static final int MSG_LOAD_BACKGROUND = 2;
    private static final int DELAY_LOAD_NON_CRITICAL = 500;

    // ======================== 成员属性（移除缓存标识）========================
    public static MainActivity _mMainActivity;
    static MainViewFragment _mMainViewFragment;
    static Handler _mHandler;
	PermissionUtils permissionUtils = PermissionUtils.getInstance();

    private App mApplication;
    private AppConfigUtils mAppConfigUtils;
    private BackgroundSourceUtils mBgSourceUtils;

    private Toolbar mToolbar;
    private ViewStub mAdsViewStub;
    private ADsBannerView mADsBannerView;

    private ViewHolder mViewHolder;
    private Menu mMenu;
    private Fragment mCurrentShowFragment;
    private MainViewFragment mMainViewFragment;
    private Drawable mDrawableFrame;
    private BatteryDrawable mCurrentValueBatteryDrawable;
    private BatteryDrawable mChargeReminderValueBatteryDrawable;
    private BatteryDrawable mUsegeReminderValueBatteryDrawable;

    // ======================== 视图缓存容器（ViewHolder 不变）========================
    private static class ViewHolder {
        BackgroundView backgroundView;
        RelativeLayout mainLayout;
        LinearLayout llLeftSeekBar, llRightSeekBar;
        CheckBox cbIsEnableChargeReminder, cbIsEnableUsegeReminder;
        Switch swIsEnableService;
        TextView tvTips, tvChargeReminderValue, tvUsegeReminderValue, tvCurrentValue;
        VerticalSeekBar chargeReminderSeekBar, usegeReminderSeekBar;
        ImageView ivCurrentBattery, ivChargeReminderBattery, ivUsegeReminderBattery;
    }

    // ======================== 重写生命周期（移除缓存恢复/保存逻辑）========================
    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public String getTag() {
        return TAG;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        LogUtils.d(TAG, "onCreate(...) - 移除窗体缓存");
        super.onCreate(savedInstanceState);
        initGlobalHandler();

        // 恢复原始初始化流程：直接加载布局+初始化
        setContentView(R.layout.activity_main);
        initCoreUtilsAsync();
        initViewHolder();
        initCriticalView();
        loadNonCriticalViewDelayed();
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		permissionUtils.startPermissionRequest(this);
	}

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mADsBannerView != null) {
            mADsBannerView.releaseAdResources();
        }
        _mMainActivity = null;
        _mMainViewFragment = null;
        if (_mHandler != null) {
            _mHandler.removeCallbacksAndMessages(null);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 移除缓存恢复后的刷新逻辑，直接发送加载背景消息
        if (_mHandler != null) {
            _mHandler.sendEmptyMessage(MSG_LOAD_BACKGROUND);
        }
        if (mADsBannerView != null) {
            mADsBannerView.resumeADs(MainActivity.this);
        }
    }

    // 其他生命周期方法（onCreateOptionsMenu、onOptionsItemSelected等保持原有不变）
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        mMenu = menu;
        AESThemeUtil.inflateMenu(this, menu);
        if (App.isDebugging()) {
            DevelopUtils.inflateMenu(this, menu);
			getMenuInflater().inflate(R.menu.toolbar_unittest, mMenu);
        }
        getMenuInflater().inflate(R.menu.toolbar_main, mMenu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int menuItemId = item.getItemId();
        if (AESThemeUtil.onAppThemeItemSelected(this, item)) {
            recreate();
            return true;
        }
        if (DevelopUtils.onDevelopItemSelected(this, item)) {
            LogUtils.d(TAG, String.format("onOptionsItemSelected item.getItemId() %d ", item.getItemId()));
            return true;
        }
        switch (menuItemId) {
            case R.id.action_settings:
                startActivity(new Intent(this, SettingsActivity.class));
                break;
            case R.id.action_battery_report:
                startActivity(new Intent(this, BatteryReportActivity.class));
                break;
            case R.id.action_clearrecord:
                startActivity(new Intent(this, ClearRecordActivity.class));
                break;
            case R.id.action_changepicture:
                startActivityForResult(new Intent(this, BackgroundSettingsActivity.class), REQUEST_READ_MEDIA_IMAGES);
                break;
            case R.id.action_unittestactivity:
                startActivity(new Intent(this, MainUnitTestActivity.class));
                break;
            case R.id.action_about:
                Intent intent = new Intent(getApplicationContext(), AboutActivity.class);
                APPInfo appInfo = genDefaultAPPInfo();
                intent.putExtra(AboutActivity.EXTRA_APPINFO, appInfo);
                WinBoLLActivityManager.getInstance().startWinBoLLActivity(getApplicationContext(), intent, AboutActivity.class);
                break;
            default:
                return super.onOptionsItemSelected(item);
        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

		permissionUtils.handlePermissionRequest(this, requestCode, resultCode, data);

		if (requestCode == REQUEST_READ_MEDIA_IMAGES) {
            if (_mHandler != null) {
                _mHandler.sendEmptyMessage(MSG_LOAD_BACKGROUND);
            }
        }
    }


    @Override
    public void onBackPressed() {
        moveTaskToBack(true);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        return super.dispatchKeyEvent(event);
    }

    @Override
    public void setupToolbar() {
        super.setupToolbar();
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        }
    }

    // ======================== 移除缓存相关核心方法（restoreFromCache/refreshViewFromCache）========================

    /**
     * 初始化电量图标（复用逻辑）
     */
    private void initBatteryDrawables() {
        if (mCurrentValueBatteryDrawable == null) {
            mCurrentValueBatteryDrawable = new BatteryDrawable(getActivity().getColor(R.color.colorCurrent));
        }
        if (mChargeReminderValueBatteryDrawable == null) {
            mChargeReminderValueBatteryDrawable = new BatteryDrawable(getActivity().getColor(R.color.colorCharge));
        }
        if (mUsegeReminderValueBatteryDrawable == null) {
            mUsegeReminderValueBatteryDrawable = new BatteryDrawable(getActivity().getColor(R.color.colorUsege));
        }
    }

    // ======================== 原有核心方法（移除缓存判断逻辑）========================
    /**
     * 刷新背景（移除缓存判断）
     */
    public void reloadBackground() {
        if (mViewHolder == null || mBgSourceUtils == null || mViewHolder.backgroundView == null) {
            LogUtils.e(TAG, "reloadBackground: 背景加载失败（控件/工具类未初始化）");
            return;
        }
        BackgroundBean bean = mBgSourceUtils.getCurrentBackgroundBean();
        if (bean != null) {
            mViewHolder.backgroundView.loadBackgroundBean(bean);
        } else {
            LogUtils.e(TAG, "reloadBackground: 背景Bean为空（图片路径异常）");
            mViewHolder.backgroundView.setBackgroundResource(R.drawable.default_background);
        }
    }

    /**
     * 设置主页面背景颜色（移除缓存判断）
     */
    void setBackgroundColor() {
        if (isFinishing() || isDestroyed() || mViewHolder == null || mBgSourceUtils == null || mViewHolder.mainLayout == null) {
            return;
        }
        BackgroundBean bean = mBgSourceUtils.getCurrentBackgroundBean();
        if (bean != null) {
            int nPixelColor = bean.getPixelColor();
            mViewHolder.mainLayout.setBackgroundColor(nPixelColor);
        }
    }

    /**
     * 设置视图数据（移除缓存判断）
     */
    void setViewData() {
        if (mViewHolder == null || mAppConfigUtils == null) return;

        int nChargeReminderValue = mAppConfigUtils.getChargeReminderValue();
        int nUsegeReminderValue = mAppConfigUtils.getUsegeReminderValue();
        int nCurrentValue = mAppConfigUtils.getCurrentValue();

        if (mViewHolder.llLeftSeekBar != null && mViewHolder.llRightSeekBar != null && mDrawableFrame != null) {
            mViewHolder.llLeftSeekBar.setBackground(mDrawableFrame);
            mViewHolder.llRightSeekBar.setBackground(mDrawableFrame);
        }

        initBatteryDrawables();
        if (mViewHolder.ivCurrentBattery != null) {
            mCurrentValueBatteryDrawable.setValue(nCurrentValue);
            mViewHolder.ivCurrentBattery.setImageDrawable(mCurrentValueBatteryDrawable);
        }
        if (mViewHolder.ivChargeReminderBattery != null) {
            mChargeReminderValueBatteryDrawable.setValue(nChargeReminderValue);
            mViewHolder.ivChargeReminderBattery.setImageDrawable(mChargeReminderValueBatteryDrawable);
        }
        if (mViewHolder.ivUsegeReminderBattery != null) {
            mUsegeReminderValueBatteryDrawable.setValue(nUsegeReminderValue);
            mViewHolder.ivUsegeReminderBattery.setImageDrawable(mUsegeReminderValueBatteryDrawable);
        }

        if (mViewHolder.tvChargeReminderValue != null) {
            mViewHolder.tvChargeReminderValue.setTextColor(getActivity().getColor(R.color.colorCharge));
            mViewHolder.tvChargeReminderValue.setText(String.valueOf(nChargeReminderValue) + "%");
        }
        if (mViewHolder.chargeReminderSeekBar != null) {
            mViewHolder.chargeReminderSeekBar.setProgress(nChargeReminderValue);
        }
        if (mViewHolder.cbIsEnableChargeReminder != null) {
            mViewHolder.cbIsEnableChargeReminder.setChecked(mAppConfigUtils.getIsEnableChargeReminder());
        }

        if (mViewHolder.tvUsegeReminderValue != null) {
            mViewHolder.tvUsegeReminderValue.setTextColor(getActivity().getColor(R.color.colorUsege));
            mViewHolder.tvUsegeReminderValue.setText(String.valueOf(nUsegeReminderValue) + "%");
        }
        if (mViewHolder.usegeReminderSeekBar != null) {
            mViewHolder.usegeReminderSeekBar.setProgress(nUsegeReminderValue);
        }
        if (mViewHolder.cbIsEnableUsegeReminder != null) {
            mViewHolder.cbIsEnableUsegeReminder.setChecked(mAppConfigUtils.getIsEnableUsegeReminder());
        }

        if (mViewHolder.tvCurrentValue != null) {
            mViewHolder.tvCurrentValue.setTextColor(getActivity().getColor(R.color.colorCurrent));
            mViewHolder.tvCurrentValue.setText(String.valueOf(nCurrentValue) + "%");
        }

        if (mViewHolder.swIsEnableService != null) {
            mViewHolder.swIsEnableService.setChecked(mAppConfigUtils.getIsEnableService());
            mViewHolder.swIsEnableService.setText(getString(R.string.txt_aboveswitch));
        }
        if (mViewHolder.tvTips != null) {
            mViewHolder.tvTips.setText(getString(R.string.txt_aboveswitchtips));
        }
    }

    /**
     * 设置视图监听（保持原有）
     */
    void setViewListener() {
        if (mViewHolder == null || mAppConfigUtils == null) return;

        if (mViewHolder.chargeReminderSeekBar != null) {
            mViewHolder.chargeReminderSeekBar.setOnSeekBarChangeListener(new ChargeReminderSeekBarChangeListener());
        }
        if (mViewHolder.cbIsEnableChargeReminder != null) {
            mViewHolder.cbIsEnableChargeReminder.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						mAppConfigUtils.setIsEnableChargeReminder(mViewHolder.cbIsEnableChargeReminder.isChecked());
					}
				});
        }

        if (mViewHolder.usegeReminderSeekBar != null) {
            mViewHolder.usegeReminderSeekBar.setOnSeekBarChangeListener(new UsegeReminderSeekBarChangeListener());
        }
        if (mViewHolder.cbIsEnableUsegeReminder != null) {
            mViewHolder.cbIsEnableUsegeReminder.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						mAppConfigUtils.setIsEnableUsegeReminder(mViewHolder.cbIsEnableUsegeReminder.isChecked());
					}
				});
        }

        if (mViewHolder.swIsEnableService != null) {
            mViewHolder.swIsEnableService.setOnClickListener(new CompoundButton.OnClickListener() {
					@Override
					public void onClick(View view) {
						mAppConfigUtils.setIsEnableService(getActivity(), mViewHolder.swIsEnableService.isChecked());
					}
				});
        }
    }

    /**
     * 更新当前电量显示（保持原有）
     */
    void setCurrentValueBattery(int value) {
        if (mViewHolder == null || mCurrentValueBatteryDrawable == null || mViewHolder.tvCurrentValue == null) return;
        mViewHolder.tvCurrentValue.setText(String.valueOf(value) + "%");
        mCurrentValueBatteryDrawable.setValue(value);
        mCurrentValueBatteryDrawable.invalidateSelf();
    }

    /**
     * 初始化ViewHolder（保持原有）
     */
    private void initViewHolder() {
        mViewHolder = new ViewHolder();
        mViewHolder.mainLayout = (RelativeLayout) findViewById(R.id.activitymainRelativeLayout1);
        if (mViewHolder.mainLayout == null) LogUtils.e(TAG, "initViewHolder: mainLayout绑定失败（ID不匹配）");

        mViewHolder.backgroundView = (BackgroundView) findViewById(R.id.fragmentmainviewBackgroundView1);
        if (mViewHolder.backgroundView == null) LogUtils.e(TAG, "initViewHolder: backgroundView绑定失败（ID不匹配）");

        mViewHolder.llLeftSeekBar = (LinearLayout) findViewById(R.id.fragmentmainviewLinearLayout1);
        mViewHolder.llRightSeekBar = (LinearLayout) findViewById(R.id.fragmentmainviewLinearLayout2);
        mViewHolder.cbIsEnableChargeReminder = (CheckBox) findViewById(R.id.fragmentmainviewCheckBox1);
        mViewHolder.cbIsEnableUsegeReminder = (CheckBox) findViewById(R.id.fragmentmainviewCheckBox2);
        mViewHolder.swIsEnableService = (Switch) findViewById(R.id.fragmentandroidviewSwitch1);
        mViewHolder.tvTips = (TextView) findViewById(R.id.fragmentandroidviewTextView1);
        mViewHolder.tvChargeReminderValue = (TextView) findViewById(R.id.fragmentandroidviewTextView2);
        mViewHolder.tvUsegeReminderValue = (TextView) findViewById(R.id.fragmentandroidviewTextView3);
        mViewHolder.tvCurrentValue = (TextView) findViewById(R.id.fragmentandroidviewTextView4);
        mViewHolder.chargeReminderSeekBar = (VerticalSeekBar) findViewById(R.id.fragmentandroidviewVerticalSeekBar1);
        mViewHolder.usegeReminderSeekBar = (VerticalSeekBar) findViewById(R.id.fragmentandroidviewVerticalSeekBar2);
        mViewHolder.ivCurrentBattery = (ImageView) findViewById(R.id.fragmentandroidviewImageView1);
        mViewHolder.ivChargeReminderBattery = (ImageView) findViewById(R.id.fragmentandroidviewImageView3);
        mViewHolder.ivUsegeReminderBattery = (ImageView) findViewById(R.id.fragmentandroidviewImageView2);
        mAdsViewStub = (ViewStub) findViewById(R.id.stub_ads_banner);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
    }

    /**
     * 初始化核心工具类（保持原有）
     */
    private void initCoreUtilsAsync() {
        new Thread(new Runnable() {
				@Override
				public void run() {
					mApplication = (App) getApplication();
					mAppConfigUtils = App.getAppConfigUtils(getActivity());
					mBgSourceUtils = BackgroundSourceUtils.getInstance(getActivity());
					runOnUiThread(new Runnable() {
							@Override
							public void run() {
								if (getActivity() != null) {
									mDrawableFrame = getActivity().getDrawable(R.drawable.bg_frame);
								}
								setViewData();
								setViewListener();
								checkServiceAsync();
								if (_mHandler != null) {
									_mHandler.sendEmptyMessage(MSG_LOAD_BACKGROUND);
								}
							}
						});
				}
			}).start();
    }

    /**
     * 初始化首屏核心视图（保持原有）
     */
    private void initCriticalView() {
        _mMainActivity = this;
        setSupportActionBar(mToolbar);
        if (mToolbar != null) {
            mToolbar.setTitleTextAppearance(this, R.style.Toolbar_TitleText);
        }
    }

    /**
     * 延迟加载非首屏核心视图（保持原有）
     */
    private void loadNonCriticalViewDelayed() {
        new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					if (isFinishing() || isDestroyed()) return;
					loadAdsView();
				}
			}, DELAY_LOAD_NON_CRITICAL);
    }

    /**
     * 加载广告视图（保持原有）
     */
    private void loadAdsView() {
        if (mAdsViewStub == null) return;
        if (mADsBannerView == null) {
            View adsView = mAdsViewStub.inflate();
            mADsBannerView = (ADsBannerView) adsView.findViewById(R.id.adsbanner);
        }
    }

    /**
     * 检查服务状态（保持原有）
     */
    private void checkServiceAsync() {
        new Thread(new Runnable() {
				@Override
				public void run() {
					if (mAppConfigUtils == null || isFinishing() || isDestroyed()) return;
					if (mAppConfigUtils.getIsEnableService()
                        && !ServiceUtils.isServiceAlive(getActivity(), ControlCenterService.class.getName())) {
						runOnUiThread(new Runnable() {
								@Override
								public void run() {
									Intent intent = new Intent(getActivity(), ControlCenterService.class);
									getActivity().startForegroundService(intent);
								}
							});
					}
				}
			}).start();
    }

    /**
     * 初始化全局Handler（保持原有）
     */
    private void initGlobalHandler() {
        if (_mHandler == null) {
            _mHandler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    switch (msg.what) {
                        case MSG_RELOAD_APPCONFIG:
                            if (_mMainActivity != null && _mMainActivity.mViewHolder != null) {
                                _mMainActivity.setViewData();
                            }
                            break;
                        case MSG_CURRENTVALUEBATTERY:
                            if (_mMainActivity != null && _mMainActivity.mViewHolder != null) {
                                _mMainActivity.setCurrentValueBattery(msg.arg1);
                            }
                            break;
                        case MSG_LOAD_BACKGROUND:
                            if (_mMainActivity != null) {
                                _mMainActivity.reloadBackground();
                                _mMainActivity.setBackgroundColor();
                            }
                            break;
                    }
                    super.handleMessage(msg);
                }
            };
        }
    }

    // 内部监听类（保持原有）
    class ChargeReminderSeekBarChangeListener implements SeekBar.OnSeekBarChangeListener {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if (mViewHolder == null || mChargeReminderValueBatteryDrawable == null || mViewHolder.tvChargeReminderValue == null) return;
            mViewHolder.tvChargeReminderValue.setText(String.valueOf(progress) + "%");
            mChargeReminderValueBatteryDrawable.setValue(progress);
            mChargeReminderValueBatteryDrawable.invalidateSelf();
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {}

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            if (mAppConfigUtils == null || mViewHolder == null || mViewHolder.tvChargeReminderValue == null) return;
            int nChargeReminderValue = ((VerticalSeekBar) seekBar)._mnProgress;
            mAppConfigUtils.setChargeReminderValue(nChargeReminderValue);
            mViewHolder.tvChargeReminderValue.setText(String.valueOf(nChargeReminderValue) + "%");
        }
    }

    class UsegeReminderSeekBarChangeListener implements SeekBar.OnSeekBarChangeListener {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if (mViewHolder == null || mUsegeReminderValueBatteryDrawable == null || mViewHolder.tvUsegeReminderValue == null) return;
            mViewHolder.tvUsegeReminderValue.setText(String.valueOf(progress) + "%");
            mUsegeReminderValueBatteryDrawable.setValue(progress);
            mUsegeReminderValueBatteryDrawable.invalidateSelf();
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {}

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            if (mAppConfigUtils == null || mViewHolder == null || mViewHolder.tvUsegeReminderValue == null) return;
            int nUsegeReminderValue = ((VerticalSeekBar) seekBar)._mnProgress;
            mAppConfigUtils.setUsegeReminderValue(nUsegeReminderValue);
            mViewHolder.tvUsegeReminderValue.setText(String.valueOf(nUsegeReminderValue) + "%");
        }
    }

    // 静态工具方法（保持原有）
    public static void relaodAppConfigs() {
        if (_mHandler != null) {
            _mHandler.sendMessage(_mHandler.obtainMessage(MSG_RELOAD_APPCONFIG));
        }
    }

    public static void sendMsgCurrentValueBattery(int value) {
        if (_mHandler != null) {
            Message msg = _mHandler.obtainMessage(MSG_CURRENTVALUEBATTERY);
            msg.arg1 = value;
            _mHandler.sendMessage(msg);
        }
    }

    private String getRealPathFromURI(Uri contentUri) {
        String[] proj = {MediaStore.MediaColumns.DATA};
        Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
        if (cursor != null && cursor.moveToNext()) {
            int nColumnIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
            if (nColumnIndex > -1) {
                String path = cursor.getString(nColumnIndex);
                cursor.close();
                return path;
            }
            cursor.close();
        }
        return null;
    }

    APPInfo genDefaultAPPInfo() {
        String szBranchName = "powerbell";
        APPInfo appInfo = new APPInfo();
        appInfo.setAppName(getString(R.string.app_name));
        appInfo.setAppIcon(R.drawable.ic_launcher);
        appInfo.setAppDescription(getString(R.string.app_description));
        appInfo.setAppGitName("WinBoLL");
        appInfo.setAppGitOwner("Studio");
        appInfo.setAppGitAPPBranch(szBranchName);
        appInfo.setAppGitAPPSubProjectFolder(szBranchName);
        appInfo.setAppHomePage("https://www.winboll.cc/apks/index.php?project=PowerBell");
        appInfo.setAppAPKName("PowerBell");
        appInfo.setAppAPKFolderName("PowerBell");
        return appInfo;
    }
}

