1//@ts-nocheck 2/** 3 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 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 17import settings from "@ohos.settings"; 18import commonEvent from "@ohos.commonEvent"; 19import dataShare from '@ohos.data.dataShare'; 20import Log from "./Log"; 21import EventManager from "./event/EventManager"; 22import createOrGet from "./SingleInstanceHelper"; 23import { obtainLocalEvent } from "./event/EventUtil"; 24import Constants from "./Constants"; 25import { CommonEventManager, getCommonEventManager, POLICY } from "./commonEvent/CommonEventManager"; 26import i18n from '@ohos.i18n'; 27 28export const TIME_CHANGE_EVENT = "Time_Change_Event"; 29 30export type TimeEventArgs = { 31 date: Date; 32 timeFormat: boolean; 33}; 34 35const TAG = "TimeManager_SysUI"; 36const TIME_FORMAT_KEY = settings.date.TIME_FORMAT; 37const TIME_SUBSCRIBE_INFO = { 38 events: [ 39 commonEvent.Support.COMMON_EVENT_TIME_CHANGED, 40 commonEvent.Support.COMMON_EVENT_TIMEZONE_CHANGED, 41 commonEvent.Support.COMMON_EVENT_TIME_TICK, 42 ], 43}; 44 45function fill(value: number) { 46 return (value > 9 ? "" : "0") + value; 47} 48 49export function concatTime(h: number, m: number) { 50 return `${fill(h)}:${fill(m)}`; 51} 52 53class TimeManager { 54 private mUse24hFormat: boolean = false; 55 private mSettingsHelper?: dataShare.DataShareHelper; 56 private mManager?: CommonEventManager; 57 58 public init(context: any) { 59 this.mManager = getCommonEventManager( 60 TAG, 61 TIME_SUBSCRIBE_INFO, 62 () => this.notifyTimeChange(), 63 (isSubscribe) => isSubscribe && this.notifyTimeChange() 64 ); 65 this.mManager.subscriberCommonEvent(); 66 this.mManager.applyPolicy([POLICY.SCREEN_POLICY]); 67 this.initTimeFormat(context); 68 } 69 70 public release() { 71 this.mManager?.release(); 72 this.mManager = undefined; 73 this.mSettingsHelper?.off("dataChange", Constants.getUriSync(Constants.KEY_TIME_FORMAT)); 74 } 75 76 public formatTime(date: Date, as24Hour: boolean = false) { 77 this.mUse24hFormat = i18n.System.is24HourClock(); 78 let format = as24Hour ? 24 : this.mUse24hFormat; 79 return concatTime(date.getHours() % (format ? 24 : 12), date.getMinutes()); 80 } 81 82 private async initTimeFormat(context: any): Promise<void> { 83 Log.showDebug(TAG, "initTimeFormat"); 84 if (context == undefined || context == null) { 85 Log.showInfo(TAG, `initTimeFormat: ${context}`); 86 return; 87 } 88 try { 89 settings.getValueSync(context, TIME_FORMAT_KEY, "24"); 90 } catch (err) { 91 Log.showError(TAG, `initTimeFormat: ${context},${JSON.stringify(err)}`) 92 } 93 this.mSettingsHelper = await dataShare.createDataShareHelper(context, Constants.getUriSync(Constants.KEY_TIME_FORMAT)); 94 95 const handleTimeFormatChange = () => { 96 if (!this.mSettingsHelper) { 97 Log.showError(TAG, `Can't get dataAbility helper.`); 98 return; 99 } 100 if (context == undefined || context == null) { 101 Log.showInfo(TAG, `handleTimeFormatChange: ${context}`); 102 return; 103 } 104 try { 105 let timeString = settings.getValueSync(context, TIME_FORMAT_KEY, "24"); 106 Log.showDebug(TAG, `timeFormat change: ${timeString}`); 107 this.mUse24hFormat = timeString == "24"; 108 } catch (err) { 109 Log.showError(TAG, `handleTimeFormatChange: ${context},${JSON.stringify(err)}`) 110 } 111 this.notifyTimeChange(); 112 }; 113 114 try { 115 this.mSettingsHelper?.on("dataChange", Constants.getUriSync(Constants.KEY_TIME_FORMAT), () => { 116 handleTimeFormatChange(); 117 }); 118 } catch (e) { 119 Log.showError(TAG, `Can't listen timeformate change.`); 120 } 121 handleTimeFormatChange(); 122 } 123 124 private notifyTimeChange() { 125 Log.showDebug(TAG, "notifyTimeChange"); 126 let args: TimeEventArgs = { 127 date: new Date(), 128 timeFormat: this.mUse24hFormat, 129 }; 130 EventManager.publish(obtainLocalEvent(TIME_CHANGE_EVENT, args)); 131 } 132} 133 134let sTimeManager = createOrGet(TimeManager, TAG); 135 136export default sTimeManager as TimeManager; 137