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

Commit 7a2ffafe authored by Tetiana Meronyk's avatar Tetiana Meronyk Committed by Android (Google) Code Review
Browse files

Merge "Check user type before adding user wakeup" into main

parents 3467edc5 89ed5491
Loading
Loading
Loading
Loading
+22 −2
Original line number Diff line number Diff line
@@ -165,6 +165,7 @@ import com.android.server.SystemService;
import com.android.server.SystemServiceManager;
import com.android.server.SystemTimeZone;
import com.android.server.SystemTimeZone.TimeZoneConfidence;
import com.android.server.pm.UserManagerInternal;
import com.android.server.pm.permission.PermissionManagerService;
import com.android.server.pm.permission.PermissionManagerServiceInternal;
import com.android.server.pm.pkg.AndroidPackage;
@@ -3763,9 +3764,11 @@ public class AlarmManagerService extends SystemService {
            }
            mNextAlarmClockForUser.put(userId, alarmClock);
            if (mStartUserBeforeScheduledAlarms) {
                if (shouldAddWakeupForUser(userId)) {
                    mUserWakeupStore.addUserWakeup(userId, convertToElapsed(
                            mNextAlarmClockForUser.get(userId).getTriggerTime(), RTC));
                }
            }
        } else {
            if (DEBUG_ALARM_CLOCK) {
                Log.v(TAG, "Next AlarmClockInfoForUser(" + userId + "): None");
@@ -3783,6 +3786,23 @@ public class AlarmManagerService extends SystemService {
        mHandler.sendEmptyMessage(AlarmHandler.SEND_NEXT_ALARM_CLOCK_CHANGED);
    }

    /**
     * Checks whether the user is of type that needs to be started before the alarm.
     */
    @VisibleForTesting
    boolean shouldAddWakeupForUser(@UserIdInt int userId) {
        final UserManagerInternal umInternal = LocalServices.getService(UserManagerInternal.class);
        if (umInternal.getUserInfo(userId) == null || umInternal.getUserInfo(userId).isGuest()) {
            // Guest user should not be started in the background.
            return false;
        } else {
            // SYSTEM user is always running, so no need to schedule wakeup for it.
            // Profiles are excluded from the wakeup list because users can explicitly stop them and
            // so starting them in the background would go against the user's intent.
            return userId != UserHandle.USER_SYSTEM && umInternal.getUserInfo(userId).isFull();
        }
    }

    /**
     * Updates NEXT_ALARM_FORMATTED and sends NEXT_ALARM_CLOCK_CHANGED_INTENT for all users
     * for which alarm clocks have changed since the last call to this.
+3 −7
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@ 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;
@@ -119,14 +118,11 @@ public class UserWakeupStore {
     * @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.
+27 −0
Original line number Diff line number Diff line
@@ -132,6 +132,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.PermissionChecker;
import android.content.pm.PackageManagerInternal;
import android.content.pm.UserInfo;
import android.net.Uri;
import android.os.BatteryManager;
import android.os.Bundle;
@@ -149,6 +150,7 @@ import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.SystemProperties;
import android.os.UserHandle;
import android.os.UserManager;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.annotations.Presubmit;
@@ -176,6 +178,7 @@ import com.android.server.DeviceIdleInternal;
import com.android.server.LocalServices;
import com.android.server.SystemClockTime.TimeConfidence;
import com.android.server.SystemService;
import com.android.server.pm.UserManagerInternal;
import com.android.server.pm.permission.PermissionManagerService;
import com.android.server.pm.permission.PermissionManagerServiceInternal;
import com.android.server.pm.pkg.AndroidPackage;
@@ -250,6 +253,8 @@ public final class AlarmManagerServiceTest {
    @Mock
    private ActivityManagerInternal mActivityManagerInternal;
    @Mock
    private UserManagerInternal mUserManagerInternal;
    @Mock
    private ActivityManager mActivityManager;
    @Mock
    private PackageManagerInternal mPackageManagerInternal;
@@ -447,6 +452,8 @@ public final class AlarmManagerServiceTest {
                () -> LocalServices.getService(PermissionManagerServiceInternal.class));
        doReturn(mActivityManagerInternal).when(
                () -> LocalServices.getService(ActivityManagerInternal.class));
        doReturn(mUserManagerInternal).when(
                () -> LocalServices.getService(UserManagerInternal.class));
        doReturn(mPackageManagerInternal).when(
                () -> LocalServices.getService(PackageManagerInternal.class));
        doReturn(mAppStateTracker).when(() -> LocalServices.getService(AppStateTracker.class));
@@ -1251,6 +1258,26 @@ public final class AlarmManagerServiceTest {
        assertEquals(0, mService.mAlarmsPerUid.get(TEST_CALLING_UID));
    }

    @Test
    public void wakeupShouldBeScheduledForFullUsers_skipsGuestSystemAndProfiles() {
        final int systemUserId = 0;
        final int fullUserId = 10;
        final int privateProfileId = 12;
        final int guestUserId = 13;
        when(mUserManagerInternal.getUserInfo(fullUserId)).thenReturn(new UserInfo(fullUserId,
                "TestUser2", UserInfo.FLAG_FULL));
        when(mUserManagerInternal.getUserInfo(privateProfileId)).thenReturn(new UserInfo(
                privateProfileId, "TestUser3", UserInfo.FLAG_PROFILE));
        when(mUserManagerInternal.getUserInfo(guestUserId)).thenReturn(new UserInfo(
                guestUserId, "TestUserGuest", null, 0, UserManager.USER_TYPE_FULL_GUEST));
        when(mUserManagerInternal.getUserInfo(systemUserId)).thenReturn(new UserInfo(
                systemUserId, "TestUserSystem", null, 0, UserManager.USER_TYPE_FULL_SYSTEM));
        assertTrue(mService.shouldAddWakeupForUser(fullUserId));
        assertFalse(mService.shouldAddWakeupForUser(systemUserId));
        assertFalse(mService.shouldAddWakeupForUser(privateProfileId));
        assertFalse(mService.shouldAddWakeupForUser(guestUserId));
    }

    @Test
    public void sendsTimeTickOnInteractive() {
        final ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class);
+0 −10
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ 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;
@@ -52,7 +51,6 @@ 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");
@@ -111,14 +109,6 @@ 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;