1e75ebbc8Sopenharmony_ci//@ts-nocheck
2e75ebbc8Sopenharmony_ci/**
3e75ebbc8Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
4e75ebbc8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
5e75ebbc8Sopenharmony_ci * you may not use this file except in compliance with the License.
6e75ebbc8Sopenharmony_ci * You may obtain a copy of the License at
7e75ebbc8Sopenharmony_ci *
8e75ebbc8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
9e75ebbc8Sopenharmony_ci *
10e75ebbc8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
11e75ebbc8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
12e75ebbc8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e75ebbc8Sopenharmony_ci * See the License for the specific language governing permissions and
14e75ebbc8Sopenharmony_ci * limitations under the License.
15e75ebbc8Sopenharmony_ci */
16e75ebbc8Sopenharmony_ci
17e75ebbc8Sopenharmony_ciimport settings from "@ohos.settings";
18e75ebbc8Sopenharmony_ciimport commonEvent from "@ohos.commonEvent";
19e75ebbc8Sopenharmony_ciimport dataShare from '@ohos.data.dataShare';
20e75ebbc8Sopenharmony_ciimport Log from "./Log";
21e75ebbc8Sopenharmony_ciimport EventManager from "./event/EventManager";
22e75ebbc8Sopenharmony_ciimport createOrGet from "./SingleInstanceHelper";
23e75ebbc8Sopenharmony_ciimport { obtainLocalEvent } from "./event/EventUtil";
24e75ebbc8Sopenharmony_ciimport Constants from "./Constants";
25e75ebbc8Sopenharmony_ciimport { CommonEventManager, getCommonEventManager, POLICY } from "./commonEvent/CommonEventManager";
26e75ebbc8Sopenharmony_ciimport i18n from '@ohos.i18n';
27e75ebbc8Sopenharmony_ci
28e75ebbc8Sopenharmony_ciexport const TIME_CHANGE_EVENT = "Time_Change_Event";
29e75ebbc8Sopenharmony_ci
30e75ebbc8Sopenharmony_ciexport type TimeEventArgs = {
31e75ebbc8Sopenharmony_ci  date: Date;
32e75ebbc8Sopenharmony_ci  timeFormat: boolean;
33e75ebbc8Sopenharmony_ci};
34e75ebbc8Sopenharmony_ci
35e75ebbc8Sopenharmony_ciconst TAG = "TimeManager";
36e75ebbc8Sopenharmony_ciconst TIME_FORMAT_KEY = settings.date.TIME_FORMAT;
37e75ebbc8Sopenharmony_ciconst TIME_SUBSCRIBE_INFO = {
38e75ebbc8Sopenharmony_ci  events: [
39e75ebbc8Sopenharmony_ci    commonEvent.Support.COMMON_EVENT_TIME_CHANGED,
40e75ebbc8Sopenharmony_ci    commonEvent.Support.COMMON_EVENT_TIMEZONE_CHANGED,
41e75ebbc8Sopenharmony_ci    commonEvent.Support.COMMON_EVENT_TIME_TICK,
42e75ebbc8Sopenharmony_ci  ],
43e75ebbc8Sopenharmony_ci};
44e75ebbc8Sopenharmony_ci
45e75ebbc8Sopenharmony_cifunction fill(value: number) {
46e75ebbc8Sopenharmony_ci  return (value > 9 ? "" : "0") + value;
47e75ebbc8Sopenharmony_ci}
48e75ebbc8Sopenharmony_ci
49e75ebbc8Sopenharmony_ciexport function concatTime(h: number, m: number) {
50e75ebbc8Sopenharmony_ci  return `${fill(h)}:${fill(m)}`;
51e75ebbc8Sopenharmony_ci}
52e75ebbc8Sopenharmony_ci
53e75ebbc8Sopenharmony_ciclass TimeManager {
54e75ebbc8Sopenharmony_ci  private mUse24hFormat: boolean = false;
55e75ebbc8Sopenharmony_ci  private mSettingsHelper?: dataShare.DataShareHelper;
56e75ebbc8Sopenharmony_ci  private mManager?: CommonEventManager;
57e75ebbc8Sopenharmony_ci
58e75ebbc8Sopenharmony_ci  public init(context: any) {
59e75ebbc8Sopenharmony_ci    this.mManager = getCommonEventManager(
60e75ebbc8Sopenharmony_ci      TAG,
61e75ebbc8Sopenharmony_ci      TIME_SUBSCRIBE_INFO, 
62e75ebbc8Sopenharmony_ci      () => this.notifyTimeChange(),
63e75ebbc8Sopenharmony_ci      (isSubscribe) => isSubscribe && this.notifyTimeChange()
64e75ebbc8Sopenharmony_ci    );
65e75ebbc8Sopenharmony_ci    this.mManager.subscriberCommonEvent();
66e75ebbc8Sopenharmony_ci    this.mManager.applyPolicy([POLICY.SCREEN_POLICY]);
67e75ebbc8Sopenharmony_ci    this.initTimeFormat(context);
68e75ebbc8Sopenharmony_ci  }
69e75ebbc8Sopenharmony_ci
70e75ebbc8Sopenharmony_ci  public release() {
71e75ebbc8Sopenharmony_ci    this.mManager?.release();
72e75ebbc8Sopenharmony_ci    this.mManager = undefined;
73e75ebbc8Sopenharmony_ci    this.mSettingsHelper?.off("dataChange", Constants.getUriSync(Constants.KEY_TIME_FORMAT));
74e75ebbc8Sopenharmony_ci  }
75e75ebbc8Sopenharmony_ci
76e75ebbc8Sopenharmony_ci  public formatTime(date: Date, as24Hour: boolean = false) {
77e75ebbc8Sopenharmony_ci    this.mUse24hFormat = as24Hour ? true : i18n.System.is24HourClock();
78e75ebbc8Sopenharmony_ci    return concatTime(date.getHours() % (this.mUse24hFormat ? 24 : 12), date.getMinutes());
79e75ebbc8Sopenharmony_ci  }
80e75ebbc8Sopenharmony_ci
81e75ebbc8Sopenharmony_ci  private async initTimeFormat(context: any): Promise<void> {
82e75ebbc8Sopenharmony_ci    Log.showDebug(TAG, "initTimeFormat");
83e75ebbc8Sopenharmony_ci    if (context == undefined || context == null) {
84e75ebbc8Sopenharmony_ci      Log.showInfo(TAG, `initTimeFormat: ${context}`);
85e75ebbc8Sopenharmony_ci      return;
86e75ebbc8Sopenharmony_ci    }
87e75ebbc8Sopenharmony_ci    try {
88e75ebbc8Sopenharmony_ci      settings.getValueSync(context, TIME_FORMAT_KEY, "24");
89e75ebbc8Sopenharmony_ci    } catch (err) {
90e75ebbc8Sopenharmony_ci      Log.showError(TAG, `initTimeFormat: ${context},${JSON.stringify(err)}`)
91e75ebbc8Sopenharmony_ci    }
92e75ebbc8Sopenharmony_ci    this.mSettingsHelper = await dataShare.createDataShareHelper(context, Constants.getUriSync(Constants.KEY_TIME_FORMAT));
93e75ebbc8Sopenharmony_ci
94e75ebbc8Sopenharmony_ci    const handleTimeFormatChange = () => {
95e75ebbc8Sopenharmony_ci      if (!this.mSettingsHelper) {
96e75ebbc8Sopenharmony_ci        Log.showError(TAG, `Can't get dataAbility helper.`);
97e75ebbc8Sopenharmony_ci        return;
98e75ebbc8Sopenharmony_ci      }
99e75ebbc8Sopenharmony_ci      if (context == undefined || context == null) {
100e75ebbc8Sopenharmony_ci         Log.showInfo(TAG, `handleTimeFormatChange: ${context}`);
101e75ebbc8Sopenharmony_ci        return;
102e75ebbc8Sopenharmony_ci      }
103e75ebbc8Sopenharmony_ci      try {
104e75ebbc8Sopenharmony_ci        let timeString = settings.getValueSync(context, TIME_FORMAT_KEY, "24");
105e75ebbc8Sopenharmony_ci        Log.showDebug(TAG, `timeFormat change: ${timeString}`);
106e75ebbc8Sopenharmony_ci        this.mUse24hFormat = timeString == "24";
107e75ebbc8Sopenharmony_ci      } catch (err) {
108e75ebbc8Sopenharmony_ci        Log.showError(TAG, `handleTimeFormatChange: ${context},${JSON.stringify(err)}`)
109e75ebbc8Sopenharmony_ci      }
110e75ebbc8Sopenharmony_ci      this.notifyTimeChange();
111e75ebbc8Sopenharmony_ci    };
112e75ebbc8Sopenharmony_ci
113e75ebbc8Sopenharmony_ci    try {
114e75ebbc8Sopenharmony_ci      this.mSettingsHelper?.on("dataChange", Constants.getUriSync(Constants.KEY_TIME_FORMAT), () => {
115e75ebbc8Sopenharmony_ci        handleTimeFormatChange();
116e75ebbc8Sopenharmony_ci      });
117e75ebbc8Sopenharmony_ci    } catch (e) {
118e75ebbc8Sopenharmony_ci      Log.showError(TAG, `Can't listen timeformate change.`);
119e75ebbc8Sopenharmony_ci    }
120e75ebbc8Sopenharmony_ci    handleTimeFormatChange();
121e75ebbc8Sopenharmony_ci  }
122e75ebbc8Sopenharmony_ci
123e75ebbc8Sopenharmony_ci  private notifyTimeChange() {
124e75ebbc8Sopenharmony_ci    Log.showDebug(TAG, "notifyTimeChange");
125e75ebbc8Sopenharmony_ci    let args: TimeEventArgs = {
126e75ebbc8Sopenharmony_ci      date: new Date(),
127e75ebbc8Sopenharmony_ci      timeFormat: this.mUse24hFormat,
128e75ebbc8Sopenharmony_ci    };
129e75ebbc8Sopenharmony_ci    EventManager.publish(obtainLocalEvent(TIME_CHANGE_EVENT, args));
130e75ebbc8Sopenharmony_ci  }
131e75ebbc8Sopenharmony_ci}
132e75ebbc8Sopenharmony_ci
133e75ebbc8Sopenharmony_cilet sTimeManager = createOrGet(TimeManager, TAG);
134e75ebbc8Sopenharmony_ci
135e75ebbc8Sopenharmony_ciexport default sTimeManager as TimeManager;
136