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 package com.android.internal.widget.remotecompose.core; 17 18 import android.annotation.NonNull; 19 20 import java.time.LocalDateTime; 21 import java.time.OffsetDateTime; 22 import java.time.ZoneId; 23 import java.time.ZoneOffset; 24 25 /** This generates the standard system variables for time. */ 26 public class TimeVariables { 27 28 /** 29 * This class populates all time variables in the system 30 * 31 * @param context 32 */ updateTime(@onNull RemoteContext context, ZoneId zoneId, LocalDateTime dateTime)33 public void updateTime(@NonNull RemoteContext context, ZoneId zoneId, LocalDateTime dateTime) { 34 // This define the time in the format 35 // seconds run from Midnight=0 quantized to seconds hour 0..3599 36 // minutes run from Midnight=0 quantized to minutes 0..1439 37 // hours run from Midnight=0 quantized to Hours 0-23 38 // CONTINUOUS_SEC is seconds from midnight looping every hour 0-3600 39 // CONTINUOUS_SEC is accurate to milliseconds due to float precession 40 // ID_OFFSET_TO_UTC is the offset from UTC in sec (typically / 3600f) 41 int month = dateTime.getMonth().getValue(); 42 int hour = dateTime.getHour(); 43 int minute = dateTime.getMinute(); 44 int seconds = dateTime.getSecond(); 45 int currentMinute = hour * 60 + minute; 46 int currentSeconds = minute * 60 + seconds; 47 float sec = currentSeconds + dateTime.getNano() * 1E-9f; 48 int day_week = dateTime.getDayOfWeek().getValue(); 49 50 OffsetDateTime offsetDateTime = dateTime.atZone(zoneId).toOffsetDateTime(); 51 ZoneOffset offset = offsetDateTime.getOffset(); 52 long epochSec = dateTime.toEpochSecond(offset); 53 54 context.loadFloat(RemoteContext.ID_OFFSET_TO_UTC, offset.getTotalSeconds()); 55 context.loadFloat(RemoteContext.ID_CONTINUOUS_SEC, sec); 56 // This will overflow in 2038. 57 context.loadInteger(RemoteContext.ID_EPOCH_SECOND, (int) epochSec); 58 context.loadFloat(RemoteContext.ID_TIME_IN_SEC, currentSeconds); 59 context.loadFloat(RemoteContext.ID_TIME_IN_MIN, currentMinute); 60 context.loadFloat(RemoteContext.ID_TIME_IN_HR, hour); 61 context.loadFloat(RemoteContext.ID_CALENDAR_MONTH, month); 62 context.loadFloat(RemoteContext.ID_DAY_OF_MONTH, month); 63 context.loadFloat(RemoteContext.ID_WEEK_DAY, day_week); 64 context.loadFloat( 65 RemoteContext.ID_API_LEVEL, 66 CoreDocument.getDocumentApiLevel() + CoreDocument.BUILD); 67 } 68 69 /** 70 * This class populates all time variables in the system 71 * 72 * @param context 73 */ updateTime(@onNull RemoteContext context)74 public void updateTime(@NonNull RemoteContext context) { 75 ZoneId zone = ZoneId.systemDefault(); 76 LocalDateTime dateTime = LocalDateTime.now(zone); 77 78 updateTime(context, zone, dateTime); 79 } 80 } 81