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

Commit 3f03d2ab authored by Dmitri Plotnikov's avatar Dmitri Plotnikov Committed by Android (Google) Code Review
Browse files

Revert "Reduce battery level reporting delay in battery history"

This reverts commit d142114b.

Reason for revert: Causes a regression, see b/408215413

Change-Id: I5869cccef4714ed42b7fa8bc691aabfe4ad13000
parent d142114b
Loading
Loading
Loading
Loading
+30 −12
Original line number Diff line number Diff line
@@ -2908,19 +2908,37 @@ public final class BatteryStatsService extends IBatteryStats.Stub
            final long elapsedRealtime = SystemClock.elapsedRealtime();
            final long uptime = SystemClock.uptimeMillis();
            final long currentTime = System.currentTimeMillis();
            final boolean onBattery = BatteryStatsImpl.isOnBattery(plugType, status);
            final boolean batteryStateChanged;
            // 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) {
                batteryStateChanged = mStats.isOnBattery() != onBattery;
                        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;
                        }
            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);
                    }

                    // 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(() -> {
                        synchronized (mStats) {
                            mStats.setBatteryStateLocked(status, health, plugType, level, temp,
                                    volt, chargeUAh, chargeFullUAh, chargeTimeToFullSeconds,
                                    elapsedRealtime, uptime, currentTime);
                        }
                    });
                });
            });
        }
    }