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

Commit 4790920b authored by Dmitri Plotnikov's avatar Dmitri Plotnikov Committed by Gerrit Code Review
Browse files

Merge "Fix AlarmManager high CPU issue" into main

parents f5d3064c d90dd503
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ import static android.app.AlarmManager.ELAPSED_REALTIME_WAKEUP;
import static android.app.AlarmManager.RTC;
import static android.app.AlarmManager.RTC_WAKEUP;

import static com.android.server.alarm.AlarmManagerService.clampPositive;
import static com.android.server.alarm.AlarmManagerService.addClampPositive;

import android.app.AlarmManager;
import android.app.IAlarmListener;
@@ -146,7 +146,7 @@ class Alarm {
        mPolicyWhenElapsed[REQUESTER_POLICY_INDEX] = requestedWhenElapsed;
        mWhenElapsed = requestedWhenElapsed;
        this.windowLength = windowLength;
        mMaxWhenElapsed = clampPositive(requestedWhenElapsed + windowLength);
        mMaxWhenElapsed = addClampPositive(requestedWhenElapsed, windowLength);
        repeatInterval = interval;
        operation = op;
        listener = rec;
@@ -241,8 +241,8 @@ class Alarm {

        final long oldMaxWhenElapsed = mMaxWhenElapsed;
        // windowLength should always be >= 0 here.
        final long maxRequestedElapsed = clampPositive(
                mPolicyWhenElapsed[REQUESTER_POLICY_INDEX] + windowLength);
        final long maxRequestedElapsed = addClampPositive(
                mPolicyWhenElapsed[REQUESTER_POLICY_INDEX], windowLength);
        mMaxWhenElapsed = Math.max(maxRequestedElapsed, mWhenElapsed);

        return (oldWhenElapsed != mWhenElapsed) || (oldMaxWhenElapsed != mMaxWhenElapsed);
+15 −3
Original line number Diff line number Diff line
@@ -1498,15 +1498,15 @@ public class AlarmManagerService extends SystemService {
        if (futurity < MIN_FUZZABLE_INTERVAL) {
            futurity = 0;
        }
        long maxElapsed = triggerAtTime + (long) (0.75 * futurity);
        long maxElapsed = addClampPositive(triggerAtTime, (long) (0.75 * futurity));
        // For non-repeating alarms, window is capped at a maximum of one hour from the requested
        // delivery time. This allows for inexact-while-idle alarms to be slightly more reliable.
        // In practice, the delivery window should generally be much smaller than that
        // when the device is not idling.
        if (interval == 0) {
            maxElapsed = Math.min(maxElapsed, triggerAtTime + INTERVAL_HOUR);
            maxElapsed = Math.min(maxElapsed, addClampPositive(triggerAtTime, INTERVAL_HOUR));
        }
        return clampPositive(maxElapsed);
        return maxElapsed;
    }

    // The RTC clock has moved arbitrarily, so we need to recalculate all the RTC alarm deliveries.
@@ -1593,6 +1593,18 @@ public class AlarmManagerService extends SystemService {
        return (val >= 0) ? val : Long.MAX_VALUE;
    }

    static long addClampPositive(long val1, long val2) {
        long val = val1 + val2;
        if (val >= 0) {
            return val;
        } else if (val1 >= 0 && val2 >= 0) {
            /* Both are +ve, so overflow happened. */
            return Long.MAX_VALUE;
        } else {
            return 0;
        }
    }

    /**
     * Sends alarms that were blocked due to user applied background restrictions - either because
     * the user lifted those or the uid came to foreground.