电量百分比实现

原创 2015年11月18日 16:05:54

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

}



相关文章推荐

关机充电百分比字体大小修改过程记录

前面都用到了".fnt"文件,但是没讲述".fnt"是怎么产生的怎么用的... 今天就将这个非常有用的位图处理工具,来回答上面的问题... 1、下载地址:http://www.angelcode.co...

iphone3g电量百分比

  • 2016年02月04日 09:01
  • 24KB
  • 下载

增加电量百分比功能

差分包: --- a/idh.code/frameworks/base/core/java/android/provider/Settings.java +++ b/idh.code/framewo...

状态栏电量百分比默认关闭

1 . PowerUsageSummary.java onCreate方法     @Override     public void onCreate(Bundle icicle) { ...

获取手机电量百分比

获取手机电量百分比。import android.os.Bundle; import android.app.Activity; import android.content.BroadcastRec...

Android 怎样控制电量百分比是否显示

我的android版本是5.1。 我是这样去实现的: 首先-----》我在priv-app/Settings里面的battery里面加一条SwitchPreference,这个是是否显示的开关。 ...

android6.0 添加电量百分比

要完成这个功能需要修改SystemUI和Settings两部分 一、Settings添加开关——控制是否打开此功能 我选择把开关添加到display模块下diff --git a/pack...

自定义viewgroup实现百分比布局

  • 2015年07月17日 13:53
  • 8.63MB
  • 下载

Android电量图标实现

最近实现了一个显示系统电量View,外部调用不需要任何控制。简单添加View就可使用。 思路很简单,通过监听Intent.ACTION_BATTERY_CHANGED)获取系统电量。用Canva...
  • tanjinc
  • tanjinc
  • 2016年10月16日 15:59
  • 137
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:电量百分比实现
举报原因:
原因补充:

(最多只允许输入30个字)