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

Commit cf376650 authored by Michael Wachenschwanz's avatar Michael Wachenschwanz Committed by Automerger Merge Worker
Browse files

Merge "Don't call AlarmManager while holding the BatteryStatsImpl lock." into...

Merge "Don't call AlarmManager while holding the BatteryStatsImpl lock." into udc-dev am: 5a2e66b0

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/21687820



Change-Id: Ia8cee37c618e8cf8491456c137fec0847cae0b46
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 8bfa9bc9 5a2e66b0
Loading
Loading
Loading
Loading
+0 −39
Original line number Original line Diff line number Diff line
@@ -30,7 +30,6 @@ import static android.os.BatteryStats.POWER_DATA_UNAVAILABLE;
import android.annotation.EnforcePermission;
import android.annotation.EnforcePermission;
import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.RequiresNoPermission;
import android.annotation.RequiresNoPermission;
import android.app.AlarmManager;
import android.app.StatsManager;
import android.app.StatsManager;
import android.app.usage.NetworkStatsManager;
import android.app.usage.NetworkStatsManager;
import android.bluetooth.BluetoothActivityEnergyInfo;
import android.bluetooth.BluetoothActivityEnergyInfo;
@@ -414,18 +413,6 @@ public final class BatteryStatsService extends IBatteryStats.Stub
            Slog.e(TAG, "Could not register INetworkManagement event observer " + e);
            Slog.e(TAG, "Could not register INetworkManagement event observer " + e);
        }
        }


        final AlarmManager am = mContext.getSystemService(AlarmManager.class);
        mHandler.post(() -> {
            synchronized (mStats) {
                mStats.setLongPlugInAlarmInterface(new AlarmInterface(am, () -> {
                    synchronized (mStats) {
                        if (mStats.isOnBattery()) return;
                        mStats.maybeResetWhilePluggedInLocked();
                    }
                }));
            }
        });

        synchronized (mPowerStatsLock) {
        synchronized (mPowerStatsLock) {
            mPowerStatsInternal = LocalServices.getService(PowerStatsInternal.class);
            mPowerStatsInternal = LocalServices.getService(PowerStatsInternal.class);
            if (mPowerStatsInternal != null) {
            if (mPowerStatsInternal != null) {
@@ -2529,32 +2516,6 @@ public final class BatteryStatsService extends IBatteryStats.Stub
        }
        }
    }
    }


    final class AlarmInterface implements BatteryStatsImpl.AlarmInterface,
            AlarmManager.OnAlarmListener {
        private AlarmManager mAm;
        private Runnable mOnAlarm;

        AlarmInterface(AlarmManager am, Runnable onAlarm) {
            mAm = am;
            mOnAlarm = onAlarm;
        }

        @Override
        public void schedule(long rtcTimeMs, long windowLengthMs) {
            mAm.setWindow(AlarmManager.RTC, rtcTimeMs, windowLengthMs, TAG, this, mHandler);
        }

        @Override
        public void cancel() {
            mAm.cancel(this);
        }

        @Override
        public void onAlarm() {
            mOnAlarm.run();
        }
    }

    private static native int nativeWaitWakeup(ByteBuffer outBuffer);
    private static native int nativeWaitWakeup(ByteBuffer outBuffer);


    private void dumpHelp(PrintWriter pw) {
    private void dumpHelp(PrintWriter pw) {
+50 −46
Original line number Original line Diff line number Diff line
@@ -26,6 +26,7 @@ import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.app.ActivityManager;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.usage.NetworkStatsManager;
import android.app.usage.NetworkStatsManager;
import android.bluetooth.BluetoothActivityEnergyInfo;
import android.bluetooth.BluetoothActivityEnergyInfo;
import android.bluetooth.UidTraffic;
import android.bluetooth.UidTraffic;
@@ -500,14 +501,6 @@ public class BatteryStatsImpl extends BatteryStats {
    }
    }
    /** Handles calls to AlarmManager */
    public interface AlarmInterface {
        /** Schedule an RTC alarm */
        void schedule(long rtcTimeMs, long windowLengthMs);
        /** Cancel the previously scheduled alarm */
        void cancel();
    }
    private final PlatformIdleStateCallback mPlatformIdleStateCallback;
    private final PlatformIdleStateCallback mPlatformIdleStateCallback;
    private final Runnable mDeferSetCharging = new Runnable() {
    private final Runnable mDeferSetCharging = new Runnable() {
@@ -1569,8 +1562,15 @@ public class BatteryStatsImpl extends BatteryStats {
    @GuardedBy("this")
    @GuardedBy("this")
    protected BatteryStatsConfig mBatteryStatsConfig = new BatteryStatsConfig.Builder().build();
    protected BatteryStatsConfig mBatteryStatsConfig = new BatteryStatsConfig.Builder().build();
    @VisibleForTesting
    @GuardedBy("this")
    protected AlarmInterface mLongPlugInAlarmInterface = null;
    private AlarmManager mAlarmManager = null;
    private final AlarmManager.OnAlarmListener mLongPlugInAlarmHandler = () ->
            mHandler.post(() -> {
                synchronized (BatteryStatsImpl.this) {
                    maybeResetWhilePluggedInLocked();
                }
            });
    /*
    /*
     * Holds a SamplingTimer associated with each Resource Power Manager state and voter,
     * Holds a SamplingTimer associated with each Resource Power Manager state and voter,
@@ -11060,18 +11060,6 @@ public class BatteryStatsImpl extends BatteryStats {
        }
        }
    }
    }
    /**
     * Injects an AlarmInterface for the long plug in alarm.
     */
    public void setLongPlugInAlarmInterface(AlarmInterface longPlugInAlarmInterface) {
        synchronized (this) {
            mLongPlugInAlarmInterface = longPlugInAlarmInterface;
            if (mBatteryPluggedIn) {
                scheduleNextResetWhilePluggedInCheck();
            }
        }
    }
    /**
    /**
     * Starts tracking CPU time-in-state for threads of the system server process,
     * Starts tracking CPU time-in-state for threads of the system server process,
     * keeping a separate account of threads receiving incoming binder calls.
     * keeping a separate account of threads receiving incoming binder calls.
@@ -14173,6 +14161,7 @@ public class BatteryStatsImpl extends BatteryStats {
    /**
    /**
     * Might reset battery stats if conditions are met. Assumed the device is currently plugged in.
     * Might reset battery stats if conditions are met. Assumed the device is currently plugged in.
     */
     */
    @VisibleForTesting
    @GuardedBy("this")
    @GuardedBy("this")
    public void maybeResetWhilePluggedInLocked() {
    public void maybeResetWhilePluggedInLocked() {
        final long elapsedRealtimeMs = mClock.elapsedRealtime();
        final long elapsedRealtimeMs = mClock.elapsedRealtime();
@@ -14189,7 +14178,7 @@ public class BatteryStatsImpl extends BatteryStats {
    @GuardedBy("this")
    @GuardedBy("this")
    private void scheduleNextResetWhilePluggedInCheck() {
    private void scheduleNextResetWhilePluggedInCheck() {
        if (mLongPlugInAlarmInterface != null) {
        if (mAlarmManager == null) return;
        final long timeoutMs = mClock.currentTimeMillis()
        final long timeoutMs = mClock.currentTimeMillis()
                + mConstants.RESET_WHILE_PLUGGED_IN_MINIMUM_DURATION_HOURS
                + mConstants.RESET_WHILE_PLUGGED_IN_MINIMUM_DURATION_HOURS
                * DateUtils.HOUR_IN_MILLIS;
                * DateUtils.HOUR_IN_MILLIS;
@@ -14204,13 +14193,16 @@ public class BatteryStatsImpl extends BatteryStats {
        nextAlarm.set(Calendar.SECOND, 0);
        nextAlarm.set(Calendar.SECOND, 0);
        nextAlarm.set(Calendar.MINUTE, 0);
        nextAlarm.set(Calendar.MINUTE, 0);
        nextAlarm.set(Calendar.HOUR_OF_DAY, 2);
        nextAlarm.set(Calendar.HOUR_OF_DAY, 2);
            long nextTimeMs = nextAlarm.getTimeInMillis();
        long possibleNextTimeMs = nextAlarm.getTimeInMillis();
            if (nextTimeMs < timeoutMs) {
        if (possibleNextTimeMs < timeoutMs) {
            // The 2AM on the day of the timeout, move on the next day.
            // The 2AM on the day of the timeout, move on the next day.
                nextTimeMs += DateUtils.DAY_IN_MILLIS;
            possibleNextTimeMs += DateUtils.DAY_IN_MILLIS;
            }
            mLongPlugInAlarmInterface.schedule(nextTimeMs, DateUtils.HOUR_IN_MILLIS);
        }
        }
        final long nextTimeMs = possibleNextTimeMs;
        final AlarmManager am = mAlarmManager;
        mHandler.post(() -> am.setWindow(AlarmManager.RTC, nextTimeMs,
                DateUtils.HOUR_IN_MILLIS,
                TAG, mLongPlugInAlarmHandler, mHandler));
    }
    }
@@ -14339,8 +14331,12 @@ public class BatteryStatsImpl extends BatteryStats {
                initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
                initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
            }
            }
            mBatteryPluggedIn = false;
            mBatteryPluggedIn = false;
            if (mLongPlugInAlarmInterface != null) {
            if (mAlarmManager != null) {
                mLongPlugInAlarmInterface.cancel();
                final AlarmManager am = mAlarmManager;
                mHandler.post(() -> {
                    // No longer plugged in. Cancel the long plug in alarm.
                    am.cancel(mLongPlugInAlarmHandler);
                });
            }
            }
            mHistory.recordBatteryState(mSecRealtime, mSecUptime, level, mBatteryPluggedIn);
            mHistory.recordBatteryState(mSecRealtime, mSecUptime, level, mBatteryPluggedIn);
            mDischargeCurrentLevel = mDischargeUnplugLevel = level;
            mDischargeCurrentLevel = mDischargeUnplugLevel = level;
@@ -15178,6 +15174,14 @@ public class BatteryStatsImpl extends BatteryStats {
    public void systemServicesReady(Context context) {
    public void systemServicesReady(Context context) {
        mConstants.startObserving(context.getContentResolver());
        mConstants.startObserving(context.getContentResolver());
        registerUsbStateReceiver(context);
        registerUsbStateReceiver(context);
        synchronized (this) {
            mAlarmManager = context.getSystemService(AlarmManager.class);
            if (mBatteryPluggedIn) {
                // Already plugged in. Schedule the long plug in alarm.
                scheduleNextResetWhilePluggedInCheck();
            }
        }
    }
    }
    /**
    /**