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

Commit d86de302 authored by Tetiana Meronyk's avatar Tetiana Meronyk
Browse files

Do not schedule alarm wakeups for SYSTEM users.

SYSTEM users can never be stopped, so waking them up is not necessary. On non-HSUM devices, this also means Main user.

On HSUM SYSTEM doesn't schedule user facing alarms so no need to schedule wake

Bug: 352696527
Test: atest UserWakeupStore
Flag: com.android.server.alarm.start_user_before_scheduled_alarms
Change-Id: Ib5709ff2c55ed756478e4e74a49a4681bd332d8d
parent f53437f6
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;