19b256929Sopenharmony_ci/*
29b256929Sopenharmony_ci * Copyright (c) 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 {CommonEventSubscriber} from "commonEvent/commonEventSubscriber";
189b256929Sopenharmony_ciimport {createOrGet} from "./SingleInstanceHelper";
199b256929Sopenharmony_ciimport {sEventManager} from "./event/EventManager";
209b256929Sopenharmony_ciimport {Log} from "./Log";
219b256929Sopenharmony_ciimport {obtainLocalEvent} from "./event/EventUtil";
229b256929Sopenharmony_ciimport {debounce} from "./Decorators";
239b256929Sopenharmony_ciexport const SCREEN_CHANGE_EVENT = "screenChangeEvent";
249b256929Sopenharmony_ci
259b256929Sopenharmony_ciconst TAG = "ScreenLockManager";
269b256929Sopenharmony_ciconst SCREEN_COMMON_EVENT_INFO = {
279b256929Sopenharmony_ci  events: [commonEvent.Support.COMMON_EVENT_SCREEN_OFF, commonEvent.Support.COMMON_EVENT_SCREEN_ON],
289b256929Sopenharmony_ci};
299b256929Sopenharmony_ciconst debounceTimeout = 500;
309b256929Sopenharmony_ci
319b256929Sopenharmony_ciclass ScreenLockManager {
329b256929Sopenharmony_ci  mSubscriber: CommonEventSubscriber | undefined;
339b256929Sopenharmony_ci
349b256929Sopenharmony_ci  async init() {
359b256929Sopenharmony_ci    this.mSubscriber = await commonEvent.createSubscriber(SCREEN_COMMON_EVENT_INFO);
369b256929Sopenharmony_ci    commonEvent.subscribe(this.mSubscriber, (err, data) => {
379b256929Sopenharmony_ci      if (err.code != 0) {
389b256929Sopenharmony_ci        Log.showError(TAG, `Can't handle screen change, err: ${JSON.stringify(err)}`);
399b256929Sopenharmony_ci        return;
409b256929Sopenharmony_ci      }
419b256929Sopenharmony_ci      Log.showDebug(TAG, `screenChange, err: ${JSON.stringify(err)} data: ${JSON.stringify(data)}`);
429b256929Sopenharmony_ci      switch (data.event) {
439b256929Sopenharmony_ci        case commonEvent.Support.COMMON_EVENT_SCREEN_OFF:
449b256929Sopenharmony_ci          this.notifyScreenEvent(false);
459b256929Sopenharmony_ci          break;
469b256929Sopenharmony_ci        case commonEvent.Support.COMMON_EVENT_SCREEN_ON:
479b256929Sopenharmony_ci          this.notifyScreenEvent(true);
489b256929Sopenharmony_ci          break;
499b256929Sopenharmony_ci        default:
509b256929Sopenharmony_ci          Log.showError(TAG, `unknow event`);
519b256929Sopenharmony_ci      }
529b256929Sopenharmony_ci    });
539b256929Sopenharmony_ci  }
549b256929Sopenharmony_ci
559b256929Sopenharmony_ci  @debounce(debounceTimeout)
569b256929Sopenharmony_ci  notifyScreenEvent(isScreenOn: boolean) {
579b256929Sopenharmony_ci    sEventManager.publish(obtainLocalEvent(SCREEN_CHANGE_EVENT, isScreenOn));
589b256929Sopenharmony_ci    Log.showDebug(TAG, `publish ${SCREEN_CHANGE_EVENT} screenState: ${isScreenOn}`);
599b256929Sopenharmony_ci  }
609b256929Sopenharmony_ci}
619b256929Sopenharmony_ci
629b256929Sopenharmony_cilet sScreenLockManager = createOrGet(ScreenLockManager, TAG);
639b256929Sopenharmony_ciexport default sScreenLockManager as ScreenLockManager;