[电池]设置-电池-上次充满电时间计算
生活随笔
收集整理的這篇文章主要介紹了
[电池]设置-电池-上次充满电时间计算
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1. 現(xiàn)象
為什么第一次開機或者格式化后電池顯示上次充滿電是xxx天或者xxx分鐘前,不管之前電池電量數(shù)值
實際操作:
2. 原因
因為上次充滿電接口getStartClockTime中 mStartClockTime = currentTime - (mClocks.elapsedRealtime()/當(dāng)前自開機后,經(jīng)過的時間,包括深度休眠的時間/-(mRealtimeStart/1000))決定,其中elapsedRealtime和mRealtimeStart的數(shù)值刷新邏輯BatteryStatsImpl->setBatteryStateLocked->setOnBatteryLocked->resetAllStatsLocked->initTimes改變
故這里顯示上次充滿電是xxx天或者xxx分鐘前,是因為沒有實際充電的時刻時,取的上次充滿電時間為系統(tǒng)當(dāng)前時間.故會有明明沒充滿電,顯示上次充滿電是xxx天或者xxx分鐘前的邏輯顯示。除非充滿電且拔除充電線則為正常顯示了。
Google的這個邏輯確實存在不精確
3. 源碼
3.1 設(shè)置->電池
package com.android.settings.fuelgauge;public class PowerUsageSummary extends PowerUsageBase implements OnLongClickListener,BatteryTipPreferenceController.BatteryTipListener {@VisibleForTestingvoid updateLastFullChargePreference() {if (mBatteryInfo != null && mBatteryInfo.averageTimeToDischarge!= Estimate.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN) {// 電池在充滿電后的預(yù)估使用時間} else {final long lastFullChargeTime = mBatteryUtils.calculateLastFullChargeTime(mStatsHelper,System.currentTimeMillis());mLastFullChargePref.setTitle(R.string.battery_last_full_charge); // 上次充滿電mLastFullChargePref.setSubtitle(StringUtil.formatRelativeTime(getContext(), lastFullChargeTime,false /* withSeconds */));}}3.2 BatteryUtils.calculateLastFullChargeTime
package com.android.settings.fuelgauge;/*** Utils for battery operation*/ public class BatteryUtils {/*** Calculate the time since last full charge, including the device off time* 計算上次充電電時間,包含設(shè)備關(guān)機時間** @param batteryStatsHelper utility class that contains the data 電池信息類* @param currentTimeMs current wall time 當(dāng)前時間* @return time in millis 返回時長*/public long calculateLastFullChargeTime(BatteryStatsHelper batteryStatsHelper,long currentTimeMs) {return currentTimeMs - batteryStatsHelper.getStats().getStartClockTime();}3.3 BatteryStatsHelper.getStats().getStartClockTime()
- frameworks/base/core/java/com/android/internal/os/BatteryStatsHelper.java
3.3.1 BatteryStats.getStartClockTime()
- frameworks/base/core/java/android/os/BatteryStats.java
3.3.2 BatteryStatsImpl.getStartClockTime()
- frameworks/base/core/java/com/android/internal/os/BatteryStatsImpl.java
3.4 BatteryStatsImpl.getStartClockTime
3.4.1 BatteryStatsImpl.getStartClockTime.ensureStartClockTime
package com.android.internal.os;public class BatteryStatsImpl extends BatteryStats {long mStartClockTime;void initTimes(long uptime, long realtime) {...mStartClockTime = System.currentTimeMillis();...}// 從電池詳情數(shù)據(jù)庫中 batterystats.bin 更新StartClockTimepublic void readSummaryFromParcel(Parcel in) throws ParcelFormatException {...// 從 batterystats.bin 獲取上次充滿電時間mStartClockTime = in.readLong();mRealtimeStart = in.readLong();...}public void writeSummaryToParcel(Parcel out, boolean inclHistory) {...// Pull the clock time. This may update the time and make a new history entry// if we had originally pulled a time before the RTC was set.long startClockTime = getStartClockTime();out.writeLong(startClockTime);out.writeLong(mRealtimeStart);...}// 是否需要重新校準(zhǔn)上次充滿電時間boolean ensureStartClockTime(final long currentTime) {final long ABOUT_ONE_YEAR = 365*24*60*60*1000L; // 1 年時間 31536000000// 是否滿足以下條件// 1. 當(dāng)前時間毫秒值 大于 1年,例如這里是100%滿足,2018-12-22 10:41:46,即當(dāng)前時間毫秒值 1545446651000// 2. 上次充滿電時間StartClockTime 小于 當(dāng)前時間毫秒值與1年的差, 即這里即記錄最長上次充滿電1年內(nèi),即我手動調(diào)整時間,最長只能記錄上次充滿電365天內(nèi)// 或者滿足下面條件// 1. 上次充滿電時間StartClockTime 大于 當(dāng)前時間毫秒值,這個條件可以將系統(tǒng)時間調(diào)整為過去的時間if ((currentTime > ABOUT_ONE_YEAR && mStartClockTime < (currentTime-ABOUT_ONE_YEAR))|| (mStartClockTime > currentTime)) {// If the start clock time has changed by more than a year, then presumably// the previous time was completely bogus. So we are going to figure out a// new time based on how much time has elapsed since we started counting.mStartClockTime = currentTime - (mClocks.elapsedRealtime()/*獲取從設(shè)備boot后經(jīng)歷的時間值*/-(mRealtimeStart/1000));return true;}return false;}3.4.2 查看 mStartClockTime 的賦值情況
3.5 BatteryStatsImpl.setBatteryStateLocked.setOnBatteryLocked.resetAllStatsLocked
@GuardedBy("this")public void setBatteryStateLocked(final int status, final int health, final int plugType,final int level, /* not final */ int temp, final int volt, final int chargeUAh,final int chargeFullUAh) {if (onBattery != mOnBattery) {...setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level, chargeUAh);@GuardedBy("this")protected void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime,final boolean onBattery, final int oldStatus, final int level, final int chargeUAh) {if (onBattery) {// We will reset our status if we are unplugging after the// battery was last full, or the level is at 100, or// we have gone through a significant charge (from a very low// level to a now very high level).boolean reset = false;if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL|| level >= 90|| (mDischargeCurrentLevel < 20 && level >= 80)|| (getHighDischargeAmountSinceCharge() >= 200&& mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER))) {...resetAllStatsLocked();總結(jié)
以上是生活随笔為你收集整理的[电池]设置-电池-上次充满电时间计算的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2020 双 11 京东全民营业一键自动
- 下一篇: 怎样制作刷机包,让你的rom更个性