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

Commit e9c37864 authored by Lyn's avatar Lyn Committed by Lyn Han
Browse files

Refactor time math to use Duration

Bug: 303854744
Bug: 290381858
Test: NotificationBitmapJobServiceTest
Change-Id: If6d140098b011ff592076e9f7c4b3654ec70a70c
parent 6cf75e9e
Loading
Loading
Loading
Loading
+17 −13
Original line number Original line Diff line number Diff line
@@ -29,7 +29,12 @@ import android.util.Slog;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.annotations.VisibleForTesting;
import com.android.server.LocalServices;
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.
 * 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.
     * @return Milliseconds until the next time the job should run.
     */
     */
    private static long getRunAfterMs() {
    private static long getRunAfterMs() {
        Calendar cal = Calendar.getInstance();  // Uses local time zone
        ZoneId zoneId = ZoneId.systemDefault();
        final long now = cal.getTimeInMillis();
        ZonedDateTime now = Instant.now().atZone(zoneId);


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


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


        return getTimeUntilRemoval(now, today2AM, tomorrow2AM);
        return getTimeUntilRemoval(now, today2AM, tomorrow2AM);
    }
    }


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


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


import java.lang.reflect.Field;
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)
@RunWith(AndroidTestingRunner.class)
public class NotificationBitmapJobServiceTest extends UiServiceTestCase {
public class NotificationBitmapJobServiceTest extends UiServiceTestCase {
@@ -103,17 +109,39 @@ public class NotificationBitmapJobServiceTest extends UiServiceTestCase {


    @Test
    @Test
    public void testGetTimeUntilRemoval_beforeToday2am_returnTimeUntilToday2am() {
    public void testGetTimeUntilRemoval_beforeToday2am_returnTimeUntilToday2am() {
        final long timeUntilRemoval = mJobService.getTimeUntilRemoval(/* now= */ 1,
        ZoneId zoneId = ZoneId.systemDefault();
                /* today2AM= */ 2, /* tomorrow2AM= */ 26);
        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
    @Test
    public void testGetTimeUntilRemoval_afterToday2am_returnTimeUntilTomorrow2am() {
    public void testGetTimeUntilRemoval_afterToday2am_returnTimeUntilTomorrow2am() {
        final long timeUntilRemoval = mJobService.getTimeUntilRemoval(/* now= */ 3,
        ZoneId zoneId = ZoneId.systemDefault();
                /* today2AM= */ 2, /* tomorrow2AM= */ 26);
        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