19b256929Sopenharmony_ci// @ts-nocheck 29b256929Sopenharmony_ci/** 39b256929Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 49b256929Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 59b256929Sopenharmony_ci * you may not use this file except in compliance with the License. 69b256929Sopenharmony_ci * You may obtain a copy of the License at 79b256929Sopenharmony_ci * 89b256929Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 99b256929Sopenharmony_ci * 109b256929Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 119b256929Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 129b256929Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139b256929Sopenharmony_ci * See the License for the specific language governing permissions and 149b256929Sopenharmony_ci * limitations under the License. 159b256929Sopenharmony_ci */ 169b256929Sopenharmony_ci 179b256929Sopenharmony_ciimport settings from "@ohos.settings"; 189b256929Sopenharmony_ciimport commonEvent from "@ohos.commonEvent"; 199b256929Sopenharmony_ciimport dataShare from '@ohos.data.dataShare'; 209b256929Sopenharmony_ciimport {DataAbilityHelper} from "ability/dataAbilityHelper"; 219b256929Sopenharmony_ciimport {Log} from "./Log"; 229b256929Sopenharmony_ciimport {sEventManager} from "./event/EventManager"; 239b256929Sopenharmony_ciimport {Constants} from "./Constants"; 249b256929Sopenharmony_ciimport {obtainLocalEvent} from "./event/EventUtil"; 259b256929Sopenharmony_ciimport {CommonEventManager, getCommonEventManager, POLICY } from "./commonEvent/CommonEventManager"; 269b256929Sopenharmony_ci 279b256929Sopenharmony_ciexport const TIME_CHANGE_EVENT = "Time_Change_Event"; 289b256929Sopenharmony_ci 299b256929Sopenharmony_ciexport type TimeEventArgs = { 309b256929Sopenharmony_ci date: Date; 319b256929Sopenharmony_ci timeFormat: boolean; 329b256929Sopenharmony_ci}; 339b256929Sopenharmony_ci 349b256929Sopenharmony_ciconst TAG = "TimeManagerSc"; 359b256929Sopenharmony_ci//const URI_VAR = "dataability:///com.ohos.settingsdata.DataAbility"; 369b256929Sopenharmony_ciconst TIME_FORMAT_KEY = settings.date.TIME_FORMAT; 379b256929Sopenharmony_ciconst TIME_SUBSCRIBE_INFO = { 389b256929Sopenharmony_ci events: [ 399b256929Sopenharmony_ci commonEvent.Support.COMMON_EVENT_TIME_CHANGED, 409b256929Sopenharmony_ci commonEvent.Support.COMMON_EVENT_TIMEZONE_CHANGED, 419b256929Sopenharmony_ci commonEvent.Support.COMMON_EVENT_TIME_TICK, 429b256929Sopenharmony_ci ], 439b256929Sopenharmony_ci}; 449b256929Sopenharmony_ci 459b256929Sopenharmony_cifunction fill(value: number) { 469b256929Sopenharmony_ci return (value > 9 ? "" : "0") + value; 479b256929Sopenharmony_ci} 489b256929Sopenharmony_ci 499b256929Sopenharmony_ciexport function concatTime(h: number, m: number) { 509b256929Sopenharmony_ci return `${fill(h)}:${fill(m)}`; 519b256929Sopenharmony_ci} 529b256929Sopenharmony_ci 539b256929Sopenharmony_ciexport class TimeManager { 549b256929Sopenharmony_ci private mUse24hFormat: boolean = false; 559b256929Sopenharmony_ci private mSettingsHelper?: DataAbilityHelper; 569b256929Sopenharmony_ci private mManager?: CommonEventManager; 579b256929Sopenharmony_ci 589b256929Sopenharmony_ci static getInstance(): TimeManager { 599b256929Sopenharmony_ci if (globalThis.TimeManager == null) { 609b256929Sopenharmony_ci globalThis.TimeManager = new TimeManager(); 619b256929Sopenharmony_ci } 629b256929Sopenharmony_ci return globalThis.TimeManager; 639b256929Sopenharmony_ci } 649b256929Sopenharmony_ci 659b256929Sopenharmony_ci public init(context: any) { 669b256929Sopenharmony_ci this.mManager = getCommonEventManager( 679b256929Sopenharmony_ci TAG, 689b256929Sopenharmony_ci TIME_SUBSCRIBE_INFO, 699b256929Sopenharmony_ci () => this.notifyTimeChange(), 709b256929Sopenharmony_ci (isSubscribe) => isSubscribe && this.notifyTimeChange() 719b256929Sopenharmony_ci ); 729b256929Sopenharmony_ci this.mManager.subscriberCommonEvent(); 739b256929Sopenharmony_ci this.mManager.applyPolicy([POLICY.SCREEN_POLICY]); 749b256929Sopenharmony_ci this.initTimeFormat(context); 759b256929Sopenharmony_ci } 769b256929Sopenharmony_ci 779b256929Sopenharmony_ci public release() { 789b256929Sopenharmony_ci this.mManager?.release(); 799b256929Sopenharmony_ci this.mManager = undefined; 809b256929Sopenharmony_ci this.mSettingsHelper?.off("dataChange", Constants.getUriSync(TIME_FORMAT_KEY)); 819b256929Sopenharmony_ci } 829b256929Sopenharmony_ci 839b256929Sopenharmony_ci public formatTime(date: Date) { 849b256929Sopenharmony_ci return concatTime(date.getHours() % (this.mUse24hFormat ? 24 : 12), date.getMinutes()); 859b256929Sopenharmony_ci } 869b256929Sopenharmony_ci 879b256929Sopenharmony_ci private async initTimeFormat(context: any) { 889b256929Sopenharmony_ci Log.showDebug(TAG, "initTimeFormat"); 899b256929Sopenharmony_ci //this.mSettingsHelper = featureAbility.acquireDataAbilityHelper(context, URI_VAR); 909b256929Sopenharmony_ci this.mSettingsHelper = await dataShare.createDataShareHelper(context, Constants.getUriSync(TIME_FORMAT_KEY)); 919b256929Sopenharmony_ci //Log.showDebug(TAG, "url:"+Constants.getUriSync(TIME_FORMAT_KEY)); 929b256929Sopenharmony_ci //Log.showDebug(TAG, "mSettingsHelper:"+JSON.stringify(this.mSettingsHelper)); 939b256929Sopenharmony_ci try { 949b256929Sopenharmony_ci this.mSettingsHelper.on("dataChange", Constants.getUriSync(TIME_FORMAT_KEY), () => { 959b256929Sopenharmony_ci Log.showDebug(TAG, "mSettingsHelper on"); 969b256929Sopenharmony_ci this.handleTimeFormatChange(context); 979b256929Sopenharmony_ci }); 989b256929Sopenharmony_ci this.handleTimeFormatChange(context); 999b256929Sopenharmony_ci } catch (e) { 1009b256929Sopenharmony_ci Log.showError(TAG, `Can't listen timeformate change.`); 1019b256929Sopenharmony_ci } 1029b256929Sopenharmony_ci } 1039b256929Sopenharmony_ci 1049b256929Sopenharmony_ci private handleTimeFormatChange(context: any) { 1059b256929Sopenharmony_ci Log.showDebug(TAG, "handleTimeFormatChange") 1069b256929Sopenharmony_ci if (!this.mSettingsHelper) { 1079b256929Sopenharmony_ci Log.showError(TAG, `Can't get dataAbility helper.`); 1089b256929Sopenharmony_ci return; 1099b256929Sopenharmony_ci } 1109b256929Sopenharmony_ci let timeString = settings.getValueSync(context, TIME_FORMAT_KEY, "24"); 1119b256929Sopenharmony_ci Log.showDebug(TAG, `timeFormat change: ${timeString}`); 1129b256929Sopenharmony_ci this.mUse24hFormat = timeString == "24"; 1139b256929Sopenharmony_ci this.notifyTimeChange(); 1149b256929Sopenharmony_ci }; 1159b256929Sopenharmony_ci 1169b256929Sopenharmony_ci private notifyTimeChange() { 1179b256929Sopenharmony_ci Log.showDebug(TAG, "notifyTimeChange"); 1189b256929Sopenharmony_ci let args: TimeEventArgs = { 1199b256929Sopenharmony_ci date: new Date(), 1209b256929Sopenharmony_ci timeFormat: this.mUse24hFormat, 1219b256929Sopenharmony_ci }; 1229b256929Sopenharmony_ci sEventManager.publish(obtainLocalEvent(TIME_CHANGE_EVENT, args)); 1239b256929Sopenharmony_ci } 1249b256929Sopenharmony_ci} 1259b256929Sopenharmony_ci 1269b256929Sopenharmony_ciexport let sTimeManager = TimeManager.getInstance(); 127