Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 633a1740 authored by Evan Millar's avatar Evan Millar Committed by The Android Open Source Project
Browse files

AI 144333: Change the way the battery level tracking code works in...

AI 144333: Change the way the battery level tracking code works in BatteryStats. Before we simply kept track of the last
  2 levels as recorded at plug and unplug events. During charge cycles this would be useful because it would tell us
  what the start and end levels were in the last discharge cycle. However during a discharge cycle this information could
  be misleading as it would give you the level at the last unplug event (beginning the the discharge cycle) and last plug
  event (end of the previous discharge cycle).
  Now we are still keeping track of 2 values, but they are "discharge cycle start level" and "discharge cycle current level".
  During a discharge cycle this will give you the level the current discharge cycle started at, and the current level. During
  a charge cycle the same data will be supplied as before (the start/end of the last discharge cycle).
  B=144249

Automated import of CL 144333
parent 65543476
Loading
Loading
Loading
Loading
+13 −10
Original line number Diff line number Diff line
@@ -472,15 +472,16 @@ public abstract class BatteryStats implements Parcelable {
    public abstract long getBatteryRealtime(long curTime);
    
    /**
     * Returns the battery percentage level at the last time the device was unplugged from power, 
     * or the last time it was booted while unplugged.
     * Returns the battery percentage level at the last time the device was unplugged from power, or
     * the last time it booted on battery power. 
     */
    public abstract int getUnpluggedStartLevel();
    public abstract int getDischargeStartLevel();
    
    /**
     * Returns the battery percentage level at the last time the device was plugged into power.
     * Returns the current battery percentage level if we are in a discharge cycle, otherwise
     * returns the level at the last plug event.
     */
    public abstract int getPluggedStartLevel();
    public abstract int getDischargeCurrentLevel();

    /**
     * Returns the total, last, or current battery uptime in microseconds.
@@ -774,8 +775,8 @@ public abstract class BatteryStats implements Parcelable {
        dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
        
        if (which == STATS_UNPLUGGED) {
            dumpLine(pw, 0 /* uid */, category, BATTERY_DATA, getUnpluggedStartLevel(), 
                    getPluggedStartLevel());
            dumpLine(pw, 0 /* uid */, category, BATTERY_DATA, getDischargeStartLevel(), 
                    getDischargeCurrentLevel());
        }
        
        for (int iu = 0; iu < NU; iu++) {
@@ -1059,13 +1060,15 @@ public abstract class BatteryStats implements Parcelable {
            if (getIsOnBattery()) {
                pw.println(prefix + "  Device is currently unplugged");
                pw.println(prefix + "    Discharge cycle start level: " + 
                        getUnpluggedStartLevel());
                        getDischargeStartLevel());
                pw.println(prefix + "    Discharge cycle current level: " +
                        getDischargeCurrentLevel());
            } else {
                pw.println(prefix + "  Device is currently plugged into power");
                pw.println(prefix + "    Last discharge cycle start level: " + 
                        getUnpluggedStartLevel());
                        getDischargeStartLevel());
                pw.println(prefix + "    Last discharge cycle end level: " + 
                        getPluggedStartLevel());
                        getDischargeCurrentLevel());
            }
            pw.println(" ");
        }
+1 −0
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ interface IBatteryStats {
    void noteScanWifiLockAcquired(int uid);
    void noteScanWifiLockReleased(int uid);
    void setOnBattery(boolean onBattery, int level);
    void recordCurrentLevel(int level);
    long getAwakeTimeBattery();
    long getAwakeTimePlugged();
}
+26 −22
Original line number Diff line number Diff line
@@ -131,8 +131,8 @@ public final class BatteryStatsImpl extends BatteryStats {
    /*
     * These keep track of battery levels (1-100) at the last plug event and the last unplug event.
     */
    int mUnpluggedStartLevel;
    int mPluggedStartLevel;
    int mDischargeStartLevel;
    int mDischargeCurrentLevel;
    
    long mLastWriteTime = 0; // Milliseconds

@@ -1947,8 +1947,8 @@ public final class BatteryStatsImpl extends BatteryStats {
        mRealtimeStart = mTrackBatteryRealtimeStart = SystemClock.elapsedRealtime() * 1000;
        mUnpluggedBatteryUptime = getBatteryUptimeLocked(mUptimeStart);
        mUnpluggedBatteryRealtime = getBatteryRealtimeLocked(mRealtimeStart);
        mUnpluggedStartLevel = 0;
        mPluggedStartLevel = 0;
        mDischargeStartLevel = 0;
        mDischargeCurrentLevel = 0;
    }

    public BatteryStatsImpl(Parcel p) {
@@ -1978,12 +1978,12 @@ public final class BatteryStatsImpl extends BatteryStats {
                    mTrackBatteryRealtimeStart = realtime;
                    mUnpluggedBatteryUptime = getBatteryUptimeLocked(uptime);
                    mUnpluggedBatteryRealtime = getBatteryRealtimeLocked(realtime);
                    mUnpluggedStartLevel = level;
                    mDischargeCurrentLevel = mDischargeStartLevel = level;
                    doUnplug(mUnpluggedBatteryUptime, mUnpluggedBatteryRealtime);
                } else {
                    mTrackBatteryPastUptime += uptime - mTrackBatteryUptimeStart;
                    mTrackBatteryPastRealtime += realtime - mTrackBatteryRealtimeStart;
                    mPluggedStartLevel = level;
                    mDischargeCurrentLevel = level;
                    doPlug(getBatteryUptimeLocked(uptime), getBatteryRealtimeLocked(realtime));
                }
                if ((mLastWriteTime + (60 * 1000)) < mSecRealtime) {
@@ -1995,6 +1995,10 @@ public final class BatteryStatsImpl extends BatteryStats {
        }
    }
    
    public void recordCurrentLevel(int level) {
        mDischargeCurrentLevel = level;
    }

    public long getAwakeTimeBattery() {
        return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
    }
@@ -2086,25 +2090,25 @@ public final class BatteryStatsImpl extends BatteryStats {
    }
    
    @Override
    public int getUnpluggedStartLevel() {
    public int getDischargeStartLevel() {
        synchronized(this) {
            return getUnluggedStartLevelLocked();
            return getDischargeStartLevelLocked();
        }
    }
    
    public int getUnluggedStartLevelLocked() {
            return mUnpluggedStartLevel;
    public int getDischargeStartLevelLocked() {
            return mDischargeStartLevel;
    }
    
    @Override
    public int getPluggedStartLevel() {
    public int getDischargeCurrentLevel() {
        synchronized(this) {
            return getPluggedStartLevelLocked();
            return getDischargeCurrentLevelLocked();
        }
    }
    
    public int getPluggedStartLevelLocked() {
            return mPluggedStartLevel;
    public int getDischargeCurrentLevelLocked() {
            return mDischargeCurrentLevel;
    }

    /**
@@ -2266,8 +2270,8 @@ public final class BatteryStatsImpl extends BatteryStats {
        mLastUptime = in.readLong();
        mRealtime = in.readLong();
        mLastRealtime = in.readLong();
        mUnpluggedStartLevel = in.readInt();
        mPluggedStartLevel = in.readInt();
        mDischargeStartLevel = in.readInt();
        mDischargeCurrentLevel = in.readInt();
        
        mStartCount++;
        
@@ -2396,8 +2400,8 @@ public final class BatteryStatsImpl extends BatteryStats {
        out.writeLong(computeUptime(NOW_SYS, STATS_CURRENT));
        out.writeLong(computeRealtime(NOWREAL_SYS, STATS_TOTAL));
        out.writeLong(computeRealtime(NOWREAL_SYS, STATS_CURRENT));
        out.writeInt(mUnpluggedStartLevel);
        out.writeInt(mPluggedStartLevel);
        out.writeInt(mDischargeStartLevel);
        out.writeInt(mDischargeCurrentLevel);
        
        
        mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL);
@@ -2577,8 +2581,8 @@ public final class BatteryStatsImpl extends BatteryStats {
        mTrackBatteryRealtimeStart = in.readLong();
        mUnpluggedBatteryUptime = in.readLong();
        mUnpluggedBatteryRealtime = in.readLong();
        mUnpluggedStartLevel = in.readInt();
        mPluggedStartLevel = in.readInt();
        mDischargeStartLevel = in.readInt();
        mDischargeCurrentLevel = in.readInt();
        mLastWriteTime = in.readLong();

        mPartialTimers.clear();
@@ -2640,8 +2644,8 @@ public final class BatteryStatsImpl extends BatteryStats {
        out.writeLong(mTrackBatteryRealtimeStart);
        out.writeLong(mUnpluggedBatteryUptime);
        out.writeLong(mUnpluggedBatteryRealtime);
        out.writeInt(mUnpluggedStartLevel);
        out.writeInt(mPluggedStartLevel);
        out.writeInt(mDischargeStartLevel);
        out.writeInt(mDischargeCurrentLevel);
        out.writeLong(mLastWriteTime);

        int size = mUidStats.size();
+10 −0
Original line number Diff line number Diff line
@@ -229,6 +229,16 @@ class BatteryService extends Binder {
                EventLog.writeEvent(LOG_BATTERY_LEVEL,
                        mBatteryLevel, mBatteryVoltage, mBatteryTemperature);
            }
            if (mBatteryLevel != mLastBatteryLevel && mPlugType == BATTERY_PLUGGED_NONE) {
                // If the battery level has changed and we are on battery, update the current level.
                // This is used for discharge cycle tracking so this shouldn't be updated while the 
                // battery is charging.
                try {
                    mBatteryStats.recordCurrentLevel(mBatteryLevel);
                } catch (RemoteException e) {
                    // Should never happen.
                }
            }
            if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
                    mPlugType == BATTERY_PLUGGED_NONE) {
                // We want to make sure we log discharge cycle outliers
+5 −0
Original line number Diff line number Diff line
@@ -264,6 +264,11 @@ public final class BatteryStatsService extends IBatteryStats.Stub {
        mStats.setOnBattery(onBattery, level);
    }
    
    public void recordCurrentLevel(int level) {
        enforceCallingPermission();
        mStats.recordCurrentLevel(level);
    }
    
    public long getAwakeTimeBattery() {
        mContext.enforceCallingOrSelfPermission(
                android.Manifest.permission.BATTERY_STATS, null);