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

Commit d142114b authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Reduce battery level reporting delay in battery history

Bug: 407566585
Test: PowerStatsTests
Flag: EXEMPT bugfix

Change-Id: I427a56762861ffd51479074e32b0b1211358696e
parent 52f8f787
Loading
Loading
Loading
Loading
+12 −30
Original line number Diff line number Diff line
@@ -2908,37 +2908,19 @@ public final class BatteryStatsService extends IBatteryStats.Stub
            final long elapsedRealtime = SystemClock.elapsedRealtime();
            final long uptime = SystemClock.uptimeMillis();
            final long currentTime = System.currentTimeMillis();
            // We still schedule this task over the handler thread to make sure we've had
            // all existing pending work handled before setting the battery state
            mHandler.post(() -> {
                // BatteryService calls us here and we may update external state. It would be wrong
                // to block such a low level service like BatteryService on external stats like WiFi
                mWorker.scheduleRunnable(() -> {
                    synchronized (mStats) {
            final boolean onBattery = BatteryStatsImpl.isOnBattery(plugType, status);
                        if (mStats.isOnBattery() == onBattery) {
                            // The battery state has not changed, so we don't need to sync external
                            // stats immediately.
                            mStats.setBatteryStateLocked(status, health, plugType, level, temp,
                                    volt, chargeUAh, chargeFullUAh, chargeTimeToFullSeconds,
                                    elapsedRealtime, uptime, currentTime);
                            return;
                        }
                    }

                    // Sync external stats first as the battery has changed states. If we don't sync
                    // before changing the state, we may not collect the relevant data later.
                    // Order here is guaranteed since we're scheduling from the same thread and we
                    // are using a single threaded executor.
                    mWorker.scheduleSync("battery-state", BatteryExternalStatsWorker.UPDATE_ALL);
                    mWorker.scheduleRunnable(() -> {
            final boolean batteryStateChanged;
            synchronized (mStats) {
                            mStats.setBatteryStateLocked(status, health, plugType, level, temp,
                                    volt, chargeUAh, chargeFullUAh, chargeTimeToFullSeconds,
                                    elapsedRealtime, uptime, currentTime);
                batteryStateChanged = mStats.isOnBattery() != onBattery;
            }
            mHandler.post(() -> {
                mStats.setBatteryStateLocked(status, health, plugType, level, temp, volt, chargeUAh,
                        chargeFullUAh, chargeTimeToFullSeconds, elapsedRealtime, uptime,
                        currentTime);
                if (batteryStateChanged) {
                    mWorker.scheduleSync("battery-state",
                            BatteryExternalStatsWorker.UPDATE_ALL);
                }
                    });
                });
            });
        }
    }