电量百分比实现

packages/apps/Settings/src/com/android/settings/fuelgauge/PowerUsageSummary.java中实现checkbox UI及数据值设置

//add by fangbo.xiong for Task 901010 Battery percentage bgn
import android.app.ActivityManagerNative;
import android.preference.CheckBoxPreference;
import android.provider.Settings;
//add by fangbo.xiong for Task 901010 Battery percentage end

public class PowerUsageSummary extends PreferenceFragment {
    ...    
    //add by fangbo.xiong for Task 901010 Battery percentage bgn
    private static final String KEY_BATTERY_STATUS_PERCENTAGE = "battery_status_percentage";
    ...
    //add by fangbo.xiong for Task 901010 Battery percentage bgn
    private CheckBoxPreference mBatteryStatusPercentagePref;
    //add by fangbo.xiong for Task 901010 Battery percentage end
    ...
    public void onCreate(Bundle icicle) {
        //add by fangbo.xiong for Task 901010 Battery percentage bgn
        mBatteryStatusPercentagePref = (CheckBoxPreference)((PreferenceScreen)mAppListGroup)
            .findPreference(KEY_BATTERY_STATUS_PERCENTAGE);
        mBatteryStatusPercentagePref.setEnabled(true);
        //add by fangbo.xiong for Task 901010 Battery percentage end
    }
    ...

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        //add by fangbo.xiong for Task 901010 Battery percentage bgn
        if (preference instanceof CheckBoxPreference && preference.getKey().equals(KEY_BATTERY_STATUS_PERCENTAGE)) {
            // save the value
            boolean isShowBatteryPercentage = Settings.System.getInt(this.getActivity()
                    .getContentResolver(), "show_statusbar_battery_percentage", 0) == 1;
            mBatteryStatusPercentagePref.setChecked(!isShowBatteryPercentage);
            int show_or_not = 0;
            // !isShowBatteryPercentage; isShowBatteryPercentage true,next value is false ,means 0;
            if (isShowBatteryPercentage) {
                show_or_not = 0;
            } else {
                show_or_not = 1;
            }
            Settings.System.putInt(this.getActivity().getContentResolver(),
                    "show_statusbar_battery_percentage", show_or_not);

            return super.onPreferenceTreeClick(preferenceScreen, preference);
        }
    }
    ...
    private void refreshStats() {

        //add by fangbo.xiong for Task 901010 Battery percentage bgn
        mBatteryStatusPercentagePref.setOrder(-2);
        mAppListGroup.addPreference(mBatteryStatusPercentagePref);
        boolean isShowBatteryPercentage = Settings.System.getInt(this.getActivity().getContentResolver(),
                "show_statusbar_battery_percentage", 0) == 1;
        mBatteryStatusPercentagePref.setChecked(isShowBatteryPercentage);
        //add by fangbo.xiong for Task 901010 Battery percentage end
    }
    ...
}

packages/apps/Settings/res/xml/power_usage_summary.xml 中定义UI元素

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
        android:title="@string/power_usage_summary_title"
        settings:keywords="@string/keywords_battery"
        android:key="app_list">
    <!--add by fangbo.xiong for Task 901010 Battery percentage bgn -->
    <CheckBoxPreference
        android:key="battery_status_percentage"
        android:persistent="false"
        android:title="@string/battery_status_percentage"/>
    <!--add by fangbo.xiong for Task 901010 Battery percentage end -->
</PreferenceScreen>


frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java


    private void loadSystemSettings(SQLiteDatabase db) {

        SQLiteStatement stmt = null;

            //add by fangbo.xiong for Task 901010 Battery percentage bgn
            loadBooleanSetting(stmt, "show_statusbar_battery_percentage", 
                        R.bool.def_SettingsProvider_battery_percentage);
            //add by fangbo.xiong for Task 901010 Battery percentage end
    }


frameworks/base/packages/SettingsProvider/res/values/defaults.xml

    <!-- add by fangbo.xiong for Task 901010 Battery percentage bgn-->
    <bool name="def_SettingsProvider_battery_percentage">false</bool>
    <!-- add by fangbo.xiong for Task 901010 Battery percentage end-->

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardStatusBarView.java
public class KeyguardStatusBarView extends RelativeLayout
        implements BatteryController.BatteryStateChangeCallback {
    @Override
    protected void onFinishInflate() {
        //add by fangbo.xiong for Task 901010 Battery percentage bgn
        boolean isShowBatteryPercentage = Settings.System.getInt(getContext()
                    .getContentResolver(), "show_statusbar_battery_percentage", 0) == 1;
        updateBatteryLevelVisibilities(isShowBatteryPercentage);
        //add by fangbo.xiong for Task 901010 Battery percentage end

    }

    private void updateVisibilities() {

        //add by fangbo.xiong for Task 901010 Battery percentage bgn
        //mBatteryLevel.setVisibility((mBatteryCharging ? View.VISIBLE : View.GONE);
        boolean isShowBatteryPercentage = Settings.System.getInt(getContext()
                    .getContentResolver(), "show_statusbar_battery_percentage", 0) == 1;
        updateBatteryLevelVisibilities(isShowBatteryPercentage);
        //add by fangbo.xiong for Task 901010 Battery percentage end

    }
    //add by fangbo.xiong for Task 901010 Battery percentage bgn
    public void updateBatteryLevelVisibilities(boolean aIsShowBatteryPercentage) {
        mBatteryLevel.setVisibility((mBatteryCharging || aIsShowBatteryPercentage) ? View.VISIBLE : View.GONE);
    }
    //add by fangbo.xiong for Task 901010 Battery percentage end
} 

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java


//add by fangbo.xiong for Task 901010 Battery percentage bgn
import android.provider.Settings;
//add by fangbo.xiong for Task 901010 Battery percentage end

public class PhoneStatusBar{
    //add by fangbo.xiong for Task 901010 Battery percentage bgn
    private static final String SHOW_BATTERY_PERCENTAGE = "show_statusbar_battery_percentage";

    TextView mBatterPercentageText;

    private final ContentObserver mBatteryPercentageObserver = new ContentObserver(mHandler) {
        @Override
        public void onChange(boolean selfChange) {
            updateBatteryLevelVisibilities();
        }
    };
    //add by fangbo.xiong for Task 901010 Battery percentage end

        //add by fangbo.xiong for Task 901010 Battery percentage bgn
        mBatterPercentageText = (TextView) mStatusBarView.findViewById(R.id.battery_level);
        boolean isShowBatteryPercentage = Settings.System.getInt(mContext.getContentResolver(),
                SHOW_BATTERY_PERCENTAGE, 0) == 1;
        if (isShowBatteryPercentage) {
            mBatterPercentageText.setVisibility(View.VISIBLE);
        } else {
            mBatterPercentageText.setVisibility(View.GONE);
        }
        mContext.getContentResolver().registerContentObserver(Settings.System.getUriFor(
                SHOW_BATTERY_PERCENTAGE),
                false, 
                mBatteryPercentageObserver, 
                UserHandle.USER_ALL);
        //add by fangbo.xiong for Task 901010 Battery percentage end

            @Override
            public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
                // noop
                //add by fangbo.xiong for Task 901010 Battery percentage bgn
                String percentage = java.text.NumberFormat.getPercentInstance().format((double) level / 100.0);
                mBatterPercentageText.setText(percentage);
                //add by fangbo.xiong for Task 901010 Battery percentage end

            }

     //add by fangbo.xiong for Task 901010 Battery percentage bgn
     private void updateBatteryLevelVisibilities() {
          if (null == mBatterPercentageText) {
                return ;
          }
          boolean isShowBatteryPercentage = Settings.System.getInt(
                                mContext.getContentResolver(), SHOW_BATTERY_PERCENTAGE, 0) == 1;
          if (isShowBatteryPercentage) {
                mBatterPercentageText.setVisibility(View.VISIBLE);
          } else {
                mBatterPercentageText.setVisibility(View.GONE);
          }
          mKeyguardStatusBar.updateBatteryLevelVisibilities(isShowBatteryPercentage);
     }
     //add by fangbo.xiong for Task 901010 Battery percentage end

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值