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"; 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 = as24Hour ? true : i18n.System.is24HourClock(); 78 return concatTime(date.getHours() % (this.mUse24hFormat ? 24 : 12), date.getMinutes()); 79 } 80 81 private async initTimeFormat(context: any): Promise<void> { 82 Log.showDebug(TAG, "initTimeFormat"); 83 if (context == undefined || context == null) { 84 Log.showInfo(TAG, `initTimeFormat: ${context}`); 85 return; 86 } 87 try { 88 settings.getValueSync(context, TIME_FORMAT_KEY, "24"); 89 } catch (err) { 90 Log.showError(TAG, `initTimeFormat: ${context},${JSON.stringify(err)}`) 91 } 92 this.mSettingsHelper = await dataShare.createDataShareHelper(context, Constants.getUriSync(Constants.KEY_TIME_FORMAT)); 93 94 const handleTimeFormatChange = () => { 95 if (!this.mSettingsHelper) { 96 Log.showError(TAG, `Can't get dataAbility helper.`); 97 return; 98 } 99 if (context == undefined || context == null) { 100 Log.showInfo(TAG, `handleTimeFormatChange: ${context}`); 101 return; 102 } 103 try { 104 let timeString = settings.getValueSync(context, TIME_FORMAT_KEY, "24"); 105 Log.showDebug(TAG, `timeFormat change: ${timeString}`); 106 this.mUse24hFormat = timeString == "24"; 107 } catch (err) { 108 Log.showError(TAG, `handleTimeFormatChange: ${context},${JSON.stringify(err)}`) 109 } 110 this.notifyTimeChange(); 111 }; 112 113 try { 114 this.mSettingsHelper?.on("dataChange", Constants.getUriSync(Constants.KEY_TIME_FORMAT), () => { 115 handleTimeFormatChange(); 116 }); 117 } catch (e) { 118 Log.showError(TAG, `Can't listen timeformate change.`); 119 } 120 handleTimeFormatChange(); 121 } 122 123 private notifyTimeChange() { 124 Log.showDebug(TAG, "notifyTimeChange"); 125 let args: TimeEventArgs = { 126 date: new Date(), 127 timeFormat: this.mUse24hFormat, 128 }; 129 EventManager.publish(obtainLocalEvent(TIME_CHANGE_EVENT, args)); 130 } 131} 132 133let sTimeManager = createOrGet(TimeManager, TAG); 134 135export default sTimeManager as TimeManager; 136