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