1 . PowerUsageSummary.java
onCreate方法
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
mStatsHelper.create(icicle);
addPreferencesFromResource(R.xml.power_usage_summary);
mAppListGroup = (PreferenceGroup) findPreference(KEY_APP_LIST);
setHasOptionsMenu(true);
// M: power usage extends features
// 电源使用扩展功能
mPowerUsageExts = new PowerUsageExts(getActivity(), mAppListGroup);
}
refreshStats方法
private void refreshStats() {
mAppListGroup.removeAll();
//是否添加该组的“偏好”孩子。
mAppListGroup.setOrderingAsAdded(false);
// M: power usage extends features
//电力使用延伸功能
mPowerUsageExts.initPowerUsageExtItems();
//上次充满后的电量使用情况
mHistPref = new BatteryHistoryPreference(getActivity(), mStatsHelper.getStats(),
mStatsHelper.getBatteryBroadcast());
mHistPref.setOrder(-1);
//现在mHistPref是否在mAppListGroup
mAppListGroup.addPreference(mHistPref);
boolean addedSome = false;
//每个器件的平均耗电
final PowerProfile powerProfile = mStatsHelper.getPowerProfile();
//stats:统计电量
final BatteryStats stats = mStatsHelper.getStats();
//averagePower = powerProfile.getAveragePower()获取消耗电量
final double averagePower = powerProfile.getAveragePower(PowerProfile.POWER_SCREEN_FULL);
if (averagePower >= MIN_AVERAGE_POWER_THRESHOLD_MILLI_AMP) {//如果没有配置原生的power_profile.xml,这里的averagePower 为0.1
final List<UserHandle> profiles = mUm.getUserProfiles();
mStatsHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED, profiles);
final List<BatterySipper> usageList = mStatsHelper.getUsageList();//获取各个耗电
//BatteryStatsService统计各个的时间会在usb线拔掉后清零,dichargeaAmount相当于usb拔掉后这段时间的电量,相当于电池使用的量
//mStatsType的值为BatteryStats.STATS_SINCE_CHARGED,代表了我们的计算规则是从上次充满电后数据,
//还有一种规则是STATS_SINCE_UNPLUGGED是拔掉USB线后的数据
//dischargeAmount:上次充满电后消耗的电量
final int dischargeAmount = stats != null ? stats.getDischargeAmount(mStatsType) : 0;
final int numSippers = usageList.size();
for (int i = 0; i < numSippers; i++) {
final BatterySipper sipper = usageList.get(i);
//MIN_POWER_THRESHOLD_MILLI_AMP:最小功率门槛
if ((sipper.value * SECONDS_IN_HOUR) < MIN_POWER_THRESHOLD_MILLI_AMP) {//BatterySipper 的power都是以每小时计的
continue;
}
//和上面一样(sipper.value / mStatsHelper.getTotalPower()统计的值,代表是该统计占该段时间内总的消耗电量的一个比,
//而该段时间是就是BatteryStats统计的时间,因此这段时间统计的电量还要乘以一个,这段时间内消耗的电量比上总电量的一个值。
//而且这个消耗电量的统计在BatteryStatsImpl中,当充电时,会把该项消耗电量统计下。
final double percentOfTotal =
((sipper.value / mStatsHelper.getTotalPower()) * dischargeAmount);
if (((int) (percentOfTotal + .5)) < 1) {
continue;
}
if (sipper.drainType == BatterySipper.DrainType.OVERCOUNTED) {
// Don't show over-counted unless it is at least 2/3 the size of
// the largest real entry, and its percent of total is more significant
if (sipper.value < ((mStatsHelper.getMaxRealPower()*2)/3)) {
continue;
}
if (percentOfTotal < 10) {
continue;
}
if ("user".equals(Build.TYPE)) {
continue;
}
}
if (sipper.drainType == BatterySipper.DrainType.UNACCOUNTED) {
// Don't show over-counted unless it is at least 1/2 the size of
// the largest real entry, and its percent of total is more significant
if (sipper.value < (mStatsHelper.getMaxRealPower()/2)) {
continue;
}
if (percentOfTotal < 5) {
continue;
}
if ("user".equals(Build.TYPE)) {
continue;
}
}
final UserHandle userHandle = new UserHandle(UserHandle.getUserId(sipper.getUid()));
final BatteryEntry entry = new BatteryEntry(getActivity(), mHandler, mUm, sipper);
final Drawable badgedIcon = mUm.getBadgedIconForUser(entry.getIcon(),
userHandle);
final CharSequence contentDescription = mUm.getBadgedLabelForUser(entry.getLabel(),
userHandle);
final PowerGaugePreference pref = new PowerGaugePreference(getActivity(),
badgedIcon, contentDescription, entry);
final double percentOfMax = (sipper.value * 100) / mStatsHelper.getMaxPower();
sipper.percent = percentOfTotal;
pref.setTitle(entry.getLabel());
pref.setOrder(i + 1);
pref.setPercent(percentOfMax, percentOfTotal);
if (sipper.uidObj != null) {
pref.setKey(Integer.toString(sipper.uidObj.getUid()));
}
addedSome = true;
mAppListGroup.addPreference(pref);
if (mAppListGroup.getPreferenceCount() > (MAX_ITEMS_TO_LIST + 1)) {
break;
}
}
}
if (!addedSome) {//如果走到这里会显示"没有电池数据",要么power_profile.xml没有配置,要么每项都continue
addNotAvailableMessage();
}
BatteryEntry.startRequestQueue();
}
2.PowerUsageExts.java
initPowerUsageExtItems方法
// init power usage extends items
// 初始化电源使用扩展项目
public void initPowerUsageExtItems() {
//add by kelinxi begin 20150320
if (BirdFeatureOption.BIRD_BATTERY_PERCENTAGE) {
mBatterrPercentPrf = new CheckBoxPreference(mContext);
mBatterrPercentPrf.setKey(KEY_BATTERY_PERCENTAGE);
mBatterrPercentPrf.setTitle(mContext.getString(R.string.battery_percent));
//设置此首选项的默认值,如果持久性已关闭或持久性打开,并且在持久存储中未找到首选项,则将设置此偏好。
mBatterrPercentPrf.setDefaultValue(false);
//设置此Preference是否持续。持久化时,它将其值存储到持久的SharedPreferences存储中。
//如果应该将其值存储到SharedPreferences中,请设置为true。
mBatterrPercentPrf.setPersistent(false);
//设置相同级别上其他Preference对象的此Preference设置的顺序。
//如果未指定,则默认行为是按字母顺序排列。
//setOrderingAsAdded(boolean)可以用于根据它们在XML中显示的顺序来排序“首选项”对象。
//这个偏好的顺序。 首先显示较低的值。 使用DEFAULT_ORDER按字母顺序排序或允许从XML排序。
mBatterrPercentPrf.setOrder(-3);
//在状态栏中打开/关闭电池百分比显示
//Settings.Secure.BATTERY_PERCENTAGE = "battery_percentage"
final boolean enable = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.BATTERY_PERCENTAGE, 0) != 0;
mAppListGroup.addPreference(mBatterrPercentPrf);
//把该复选框设置成enable,如果enble是true,则该复选框默认选择
mBatterrPercentPrf.setChecked(enable);//#8816 ningzhiyu, setChecked after addPreference.
}
//add by kelinxi end 20150320
// Power saving mode for op09
mBatteryExt.loadPreference(mContext, mAppListGroup);
// background power saving
if (FeatureOption.MTK_BG_POWER_SAVING_SUPPORT
&& FeatureOption.MTK_BG_POWER_SAVING_UI_SUPPORT) {
mBgPowerSavingPrf = new SwitchPreference(mContext);
mBgPowerSavingPrf.setKey(KEY_BACKGROUND_POWER_SAVING);
mBgPowerSavingPrf.setTitle(R.string.bg_power_saving_title);
mBgPowerSavingPrf.setOrder(-4);
mBgPowerSavingPrf.setChecked(Settings.System.getInt(mContext.getContentResolver(),
Settings.System.BG_POWER_SAVING_ENABLE, 1) != 0);
mAppListGroup.addPreference(mBgPowerSavingPrf);
}
}
3.BirdProviderUtils.java
public void loadCustomSecureSettings(SQLiteStatement stmt) {
//ningzhiyu , add battery percentage default value
//添加电池百分比默认值
loadIntegerSetting(stmt, Settings.Secure.BATTERY_PERCENTAGE, R.integer.battery_percentage_def);
}
4.bird_defaults.xml
<!--
battery_percentage_def
besure the BIRD_BATTERY_PERCENTAGE is define in the mak file.
0 default off
1 default on
battery_percentage_def
所以BIRD_BATTERY_PERCENTAGE是在mak文件中定义的。
0默认关闭
1默认开启
-->
<integer name="battery_percentage_def">0</integer>
</resources>