• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.health.connect;
18 
19 import android.annotation.NonNull;
20 
21 import java.time.Instant;
22 import java.time.LocalDateTime;
23 import java.time.ZoneOffset;
24 
25 /**
26  * A helper class for {@link TimeRangeFilter} to handle possible time filter types.
27  *
28  * @hide
29  */
30 public final class TimeRangeFilterHelper {
31 
32     private static final ZoneOffset LOCAL_TIME_ZERO_OFFSET = ZoneOffset.UTC;
33 
isLocalTimeFilter(@onNull TimeRangeFilter timeRangeFilter)34     public static boolean isLocalTimeFilter(@NonNull TimeRangeFilter timeRangeFilter) {
35         return (timeRangeFilter instanceof LocalTimeRangeFilter);
36     }
37 
38     /**
39      * @return start time epoch milliseconds for Instant time filter and epoch milliseconds using
40      *     system zoneOffset for LocalTime filter
41      */
getFilterStartTimeMillis(@onNull TimeRangeFilter timeRangeFilter)42     public static long getFilterStartTimeMillis(@NonNull TimeRangeFilter timeRangeFilter) {
43         if (isLocalTimeFilter(timeRangeFilter)) {
44             return getMillisOfLocalTime(((LocalTimeRangeFilter) timeRangeFilter).getStartTime());
45         } else if (timeRangeFilter instanceof TimeInstantRangeFilter) {
46             return ((TimeInstantRangeFilter) timeRangeFilter).getStartTime().toEpochMilli();
47         } else {
48             throw new IllegalArgumentException(
49                     "Invalid time filter object. Object should be either "
50                             + "TimeInstantRangeFilter or LocalTimeRangeFilter.");
51         }
52     }
53 
54     /**
55      * @return end time epoch milliseconds for Instant time filter and epoch milliseconds using
56      *     system zoneOffset for LocalTime filter
57      */
getFilterEndTimeMillis(@onNull TimeRangeFilter timeRangeFilter)58     public static long getFilterEndTimeMillis(@NonNull TimeRangeFilter timeRangeFilter) {
59         if (isLocalTimeFilter(timeRangeFilter)) {
60             return getMillisOfLocalTime(((LocalTimeRangeFilter) timeRangeFilter).getEndTime());
61         } else if (timeRangeFilter instanceof TimeInstantRangeFilter) {
62             return ((TimeInstantRangeFilter) timeRangeFilter).getEndTime().toEpochMilli();
63         } else {
64             throw new IllegalArgumentException(
65                     "Invalid time filter object. Object should be either "
66                             + "TimeInstantRangeFilter or LocalTimeRangeFilter.");
67         }
68     }
69 
getLocalTimeFromMillis(Long localDateTimeMillis)70     public static LocalDateTime getLocalTimeFromMillis(Long localDateTimeMillis) {
71         return LocalDateTime.ofInstant(
72                 Instant.ofEpochMilli(localDateTimeMillis), LOCAL_TIME_ZERO_OFFSET);
73     }
74 
getMillisOfLocalTime(LocalDateTime time)75     public static long getMillisOfLocalTime(LocalDateTime time) {
76         return time.toInstant(LOCAL_TIME_ZERO_OFFSET).toEpochMilli();
77     }
78 }
79