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