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