• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    settings.getValueSync(context, TIME_FORMAT_KEY, "24");
82    this.mSettingsHelper = await dataShare.createDataShareHelper(context, Constants.getUriSync(Constants.KEY_TIME_FORMAT));
83
84    const handleTimeFormatChange = () => {
85      if (!this.mSettingsHelper) {
86        Log.showError(TAG, `Can't get dataAbility helper.`);
87        return;
88      }
89      let timeString = settings.getValueSync(context, TIME_FORMAT_KEY, "24");
90      Log.showDebug(TAG, `timeFormat change: ${timeString}`);
91      this.mUse24hFormat = timeString == "24";
92      this.notifyTimeChange();
93    };
94
95    try {
96      this.mSettingsHelper?.on("dataChange", Constants.getUriSync(Constants.KEY_TIME_FORMAT), () => {
97        handleTimeFormatChange();
98      });
99    } catch (e) {
100      Log.showError(TAG, `Can't listen timeformate change.`);
101    }
102    handleTimeFormatChange();
103  }
104
105  private notifyTimeChange() {
106    Log.showDebug(TAG, "notifyTimeChange");
107    let args: TimeEventArgs = {
108      date: new Date(),
109      timeFormat: this.mUse24hFormat,
110    };
111    EventManager.publish(obtainLocalEvent(TIME_CHANGE_EVENT, args));
112  }
113}
114
115let sTimeManager = createOrGet(TimeManager, TAG);
116
117export default sTimeManager as TimeManager;
118