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

Commit b9b693d0 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Reduce battery level reporting delay in battery history" into main

parents a9a7d69c d142114b
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);
                }
                    });
                });
            });
        }
    }