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

Commit a29e2c32 authored by Lyn Han's avatar Lyn Han Committed by Android (Google) Code Review
Browse files

Merge "Refactor time math to use Duration" into main

parents 147bd6c4 e9c37864
Loading
Loading
Loading
Loading
+17 −13
Original line number Diff line number Diff line
@@ -29,7 +29,12 @@ import android.util.Slog;
import com.android.internal.annotations.VisibleForTesting;
import com.android.server.LocalServices;

import java.util.Calendar;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.ZoneId;

/**
 * This service runs everyday at 2am local time to remove expired bitmaps.
@@ -69,26 +74,25 @@ public class NotificationBitmapJobService extends JobService {
     * @return Milliseconds until the next time the job should run.
     */
    private static long getRunAfterMs() {
        Calendar cal = Calendar.getInstance();  // Uses local time zone
        final long now = cal.getTimeInMillis();
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime now = Instant.now().atZone(zoneId);

        cal.set(Calendar.HOUR_OF_DAY, 2);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        final long today2AM = cal.getTimeInMillis();
        LocalDate today = now.toLocalDate();
        LocalTime twoAM = LocalTime.of(/* hour= */ 2, /* minute= */ 0);

        cal.add(Calendar.DAY_OF_YEAR, 1);
        final long tomorrow2AM = cal.getTimeInMillis();
        ZonedDateTime today2AM = ZonedDateTime.of(today, twoAM, zoneId);
        ZonedDateTime tomorrow2AM = today2AM.plusDays(1);

        return getTimeUntilRemoval(now, today2AM, tomorrow2AM);
    }

    @VisibleForTesting
    static long getTimeUntilRemoval(long now, long today2AM, long tomorrow2AM) {
        if (now < today2AM) {
            return today2AM - now;
    static long getTimeUntilRemoval(ZonedDateTime now, ZonedDateTime today2AM,
                                    ZonedDateTime tomorrow2AM) {
        if (Duration.between(now, today2AM).isNegative()) {
            return Duration.between(now, tomorrow2AM).toMillis();
        }
        return tomorrow2AM - now;
        return Duration.between(now, today2AM).toMillis();
    }

    @Override
+34 −6
Original line number Diff line number Diff line
@@ -44,6 +44,12 @@ import org.mockito.Captor;
import org.mockito.Mock;

import java.lang.reflect.Field;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.ZoneId;

@RunWith(AndroidTestingRunner.class)
public class NotificationBitmapJobServiceTest extends UiServiceTestCase {
@@ -103,17 +109,39 @@ public class NotificationBitmapJobServiceTest extends UiServiceTestCase {

    @Test
    public void testGetTimeUntilRemoval_beforeToday2am_returnTimeUntilToday2am() {
        final long timeUntilRemoval = mJobService.getTimeUntilRemoval(/* now= */ 1,
                /* today2AM= */ 2, /* tomorrow2AM= */ 26);
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime now = Instant.now().atZone(zoneId);
        LocalDate today = now.toLocalDate();

        assertThat(timeUntilRemoval).isEqualTo(1);
        LocalTime oneAM = LocalTime.of(/* hour= */ 1, /* minute= */ 0);
        LocalTime twoAM = LocalTime.of(/* hour= */ 2, /* minute= */ 0);

        ZonedDateTime today1AM = ZonedDateTime.of(today, oneAM, zoneId);
        ZonedDateTime today2AM = ZonedDateTime.of(today, twoAM, zoneId);
        ZonedDateTime tomorrow2AM = today2AM.plusDays(1);

        final long msUntilRemoval = mJobService.getTimeUntilRemoval(
                /* now= */ today1AM, today2AM, tomorrow2AM);

        assertThat(msUntilRemoval).isEqualTo(Duration.ofHours(1).toMillis());
    }

    @Test
    public void testGetTimeUntilRemoval_afterToday2am_returnTimeUntilTomorrow2am() {
        final long timeUntilRemoval = mJobService.getTimeUntilRemoval(/* now= */ 3,
                /* today2AM= */ 2, /* tomorrow2AM= */ 26);
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime now = Instant.now().atZone(zoneId);
        LocalDate today = now.toLocalDate();

        LocalTime threeAM = LocalTime.of(/* hour= */ 3, /* minute= */ 0);
        LocalTime twoAM = LocalTime.of(/* hour= */ 2, /* minute= */ 0);

        ZonedDateTime today3AM = ZonedDateTime.of(today, threeAM, zoneId);
        ZonedDateTime today2AM = ZonedDateTime.of(today, twoAM, zoneId);
        ZonedDateTime tomorrow2AM = today2AM.plusDays(1);

        final long msUntilRemoval = mJobService.getTimeUntilRemoval(/* now= */ today3AM,
                today2AM, tomorrow2AM);

        assertThat(timeUntilRemoval).isEqualTo(23);
        assertThat(msUntilRemoval).isEqualTo(Duration.ofHours(23).toMillis());
    }
}
 No newline at end of file