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

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

Merge "Do not schedule alarm wakeups for SYSTEM users." into main

parents b8357d89 d86de302
Loading
Loading
Loading
Loading
+8 −4
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ package com.android.server.alarm;
import android.annotation.Nullable;
import android.os.Environment;
import android.os.SystemClock;
import android.os.UserHandle;
import android.util.AtomicFile;
import android.util.IndentingPrintWriter;
import android.util.Pair;
@@ -113,16 +114,19 @@ public class UserWakeupStore {
    }

    /**
     * Add user wakeup for the alarm.
     * Add user wakeup for the alarm if needed.
     * @param userId Id of the user that scheduled alarm.
     * @param alarmTime time when alarm is expected to trigger.
     */
    public void addUserWakeup(int userId, long alarmTime) {
        // SYSTEM user is always running, so no need to schedule wakeup for it.
        if (userId != UserHandle.USER_SYSTEM) {
            synchronized (mUserWakeupLock) {
                mUserStarts.put(userId, alarmTime - BUFFER_TIME_MS + getUserWakeupOffset());
            }
            updateUserListFile();
        }
    }

    /**
     * Remove wakeup scheduled for the user with given userId if present.
+10 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import static com.android.server.alarm.UserWakeupStore.USER_START_TIME_DEVIATION

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.testng.AssertJUnit.assertFalse;

import android.os.Environment;
import android.os.FileUtils;
@@ -51,6 +52,7 @@ public class UserWakeupStoreTest {
    private static final int USER_ID_1 = 10;
    private static final int USER_ID_2 = 11;
    private static final int USER_ID_3 = 12;
    private static final int USER_ID_SYSTEM = 0;
    private static final long TEST_TIMESTAMP = 150_000;
    private static final File TEST_SYSTEM_DIR = new File(InstrumentationRegistry
            .getInstrumentation().getContext().getDataDir(), "alarmsTestDir");
@@ -109,6 +111,14 @@ public class UserWakeupStoreTest {
        assertTrue(file.exists());
    }

    @Test
    public void testAddWakeupForSystemUser_shouldDoNothing() {
        mUserWakeupStore.addUserWakeup(USER_ID_SYSTEM, TEST_TIMESTAMP - 19_000);
        assertEquals(0, mUserWakeupStore.getUserIdsToWakeup(TEST_TIMESTAMP).length);
        final File file = new File(ROOT_DIR , "usersWithAlarmClocks.xml");
        assertFalse(file.exists());
    }

    @Test
    public void testAddMultipleWakeupsForUser_ensureOnlyLastWakeupRemains() {
        final long finalAlarmTime = TEST_TIMESTAMP - 13_000;