19b256929Sopenharmony_ci/*
29b256929Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
39b256929Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
49b256929Sopenharmony_ci * you may not use this file except in compliance with the License.
59b256929Sopenharmony_ci * You may obtain a copy of the License at
69b256929Sopenharmony_ci *
79b256929Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
89b256929Sopenharmony_ci *
99b256929Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
109b256929Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
119b256929Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129b256929Sopenharmony_ci * See the License for the specific language governing permissions and
139b256929Sopenharmony_ci * limitations under the License.
149b256929Sopenharmony_ci */
159b256929Sopenharmony_ci
169b256929Sopenharmony_ciimport commonEvent from "@ohos.commonEvent";
179b256929Sopenharmony_ciimport {CommonEventData} from "commonEvent/commonEventData";
189b256929Sopenharmony_ciimport {sEventManager} from "../event/EventManager";
199b256929Sopenharmony_ciimport {Log} from "../Log";
209b256929Sopenharmony_ciimport {SCREEN_CHANGE_EVENT} from "../ScreenLockManager";
219b256929Sopenharmony_ci
229b256929Sopenharmony_ciexport type CommonEventManager = {
239b256929Sopenharmony_ci  subscriberCommonEvent: () => Promise<void>;
249b256929Sopenharmony_ci  unSubscriberCommonEvent: () => void;
259b256929Sopenharmony_ci  applyPolicy: (policys: Array<POLICY>) => void;
269b256929Sopenharmony_ci  release: () => void;
279b256929Sopenharmony_ci};
289b256929Sopenharmony_ci
299b256929Sopenharmony_ciexport enum POLICY {
309b256929Sopenharmony_ci  SCREEN_POLICY = "screenOnOffPolicy",
319b256929Sopenharmony_ci}
329b256929Sopenharmony_ci
339b256929Sopenharmony_citype ClearPolicy = () => void;
349b256929Sopenharmony_citype InnerManager = { subscriberCommonEvent: () => void; unSubscriberCommonEvent: () => void };
359b256929Sopenharmony_ciconst policyMap: { [key in POLICY]: (manager: InnerManager) => ClearPolicy } = {
369b256929Sopenharmony_ci  screenOnOffPolicy: ScreenPolicy,
379b256929Sopenharmony_ci};
389b256929Sopenharmony_ci
399b256929Sopenharmony_ciexport function getCommonEventManager(
409b256929Sopenharmony_ci  tag: string,
419b256929Sopenharmony_ci  subscribeInfos: { events: Array<string> },
429b256929Sopenharmony_ci  commonEventCallback: (data: CommonEventData) => void,
439b256929Sopenharmony_ci  subscribeStateChange?: (isSubscribe: boolean) => void
449b256929Sopenharmony_ci): CommonEventManager {
459b256929Sopenharmony_ci  const TAG = `CommonEvent_${tag}`;
469b256929Sopenharmony_ci  const SUBSCRIBE_INFOS = subscribeInfos;
479b256929Sopenharmony_ci  let unSubcribers: Array<() => void> = [];
489b256929Sopenharmony_ci  let policyClearCb: Map<POLICY, ClearPolicy> | undefined = undefined;
499b256929Sopenharmony_ci
509b256929Sopenharmony_ci  async function subscriberCommonEvent() {
519b256929Sopenharmony_ci    Log.showDebug(TAG, "registerSubscriber start");
529b256929Sopenharmony_ci    let subscriber = await commonEvent.createSubscriber(SUBSCRIBE_INFOS);
539b256929Sopenharmony_ci    commonEvent.subscribe(subscriber, (err, data) => {
549b256929Sopenharmony_ci      if (err.code != 0) {
559b256929Sopenharmony_ci        Log.showError(TAG, `Can't handle common event, err: ${JSON.stringify(err)}`);
569b256929Sopenharmony_ci        return;
579b256929Sopenharmony_ci      }
589b256929Sopenharmony_ci      Log.showInfo(TAG, `handle common event: ${data.event}`);
599b256929Sopenharmony_ci      commonEventCallback(data);
609b256929Sopenharmony_ci    });
619b256929Sopenharmony_ci    unSubcribers.push(() => commonEvent.unsubscribe(subscriber));
629b256929Sopenharmony_ci    subscribeStateChange && subscribeStateChange(true);
639b256929Sopenharmony_ci    Log.showInfo(TAG, `registerSubscriber success, size: ${unSubcribers.length}`);
649b256929Sopenharmony_ci  }
659b256929Sopenharmony_ci
669b256929Sopenharmony_ci  function unSubscriberCommonEvent() {
679b256929Sopenharmony_ci    Log.showDebug(TAG, `UnSubcribers size: ${unSubcribers.length}`);
689b256929Sopenharmony_ci    unSubcribers.forEach((unsubscribe) => unsubscribe());
699b256929Sopenharmony_ci    unSubcribers.length = 0;
709b256929Sopenharmony_ci    subscribeStateChange && subscribeStateChange(false);
719b256929Sopenharmony_ci  }
729b256929Sopenharmony_ci
739b256929Sopenharmony_ci  function applyPolicy(policys: Array<POLICY>) {
749b256929Sopenharmony_ci    const innerManager = { subscriberCommonEvent, unSubscriberCommonEvent };
759b256929Sopenharmony_ci    policyClearCb = policyClearCb ?? new Map();
769b256929Sopenharmony_ci    policys.forEach((policy) => {
779b256929Sopenharmony_ci      if (policyClearCb) {
789b256929Sopenharmony_ci        !policyClearCb.has(policy) && policyClearCb.set(policy, policyMap[policy](innerManager));
799b256929Sopenharmony_ci        Log.showDebug(TAG, `apply policy: ${policy}`);
809b256929Sopenharmony_ci      }
819b256929Sopenharmony_ci    });
829b256929Sopenharmony_ci  }
839b256929Sopenharmony_ci
849b256929Sopenharmony_ci  function release() {
859b256929Sopenharmony_ci    policyClearCb?.forEach((cb) => cb());
869b256929Sopenharmony_ci    policyClearCb?.clear();
879b256929Sopenharmony_ci    unSubscriberCommonEvent();
889b256929Sopenharmony_ci  }
899b256929Sopenharmony_ci
909b256929Sopenharmony_ci  return { subscriberCommonEvent, unSubscriberCommonEvent, applyPolicy, release };
919b256929Sopenharmony_ci}
929b256929Sopenharmony_ci
939b256929Sopenharmony_cifunction ScreenPolicy(manager: InnerManager): ClearPolicy {
949b256929Sopenharmony_ci  return sEventManager.subscribe(SCREEN_CHANGE_EVENT, (isScreenOn: boolean) => {
959b256929Sopenharmony_ci    isScreenOn ? manager.subscriberCommonEvent() : manager.unSubscriberCommonEvent();
969b256929Sopenharmony_ci  });
979b256929Sopenharmony_ci}
98