1e75ebbc8Sopenharmony_ci/*
2e75ebbc8Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3e75ebbc8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e75ebbc8Sopenharmony_ci * you may not use this file except in compliance with the License.
5e75ebbc8Sopenharmony_ci * You may obtain a copy of the License at
6e75ebbc8Sopenharmony_ci *
7e75ebbc8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e75ebbc8Sopenharmony_ci *
9e75ebbc8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e75ebbc8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e75ebbc8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e75ebbc8Sopenharmony_ci * See the License for the specific language governing permissions and
13e75ebbc8Sopenharmony_ci * limitations under the License.
14e75ebbc8Sopenharmony_ci */
15e75ebbc8Sopenharmony_ci
16e75ebbc8Sopenharmony_ciimport commonEvent from '@ohos.commonEvent';
17e75ebbc8Sopenharmony_ciimport { CommonEventSubscriber } from 'commonEvent/commonEventSubscriber';
18e75ebbc8Sopenharmony_ciimport createOrGet from './SingleInstanceHelper';
19e75ebbc8Sopenharmony_ciimport EventManager from './event/EventManager';
20e75ebbc8Sopenharmony_ciimport Log from './Log';
21e75ebbc8Sopenharmony_ciimport { obtainLocalEvent } from './event/EventUtil';
22e75ebbc8Sopenharmony_ciimport { debounce } from './Decorators';
23e75ebbc8Sopenharmony_ci
24e75ebbc8Sopenharmony_ciexport const SCREEN_CHANGE_EVENT = 'screenChangeEvent';
25e75ebbc8Sopenharmony_ci
26e75ebbc8Sopenharmony_ciconst TAG = 'ScreenLockManager';
27e75ebbc8Sopenharmony_ciconst SCREEN_COMMON_EVENT_INFO = {
28e75ebbc8Sopenharmony_ci  events: [commonEvent.Support.COMMON_EVENT_SCREEN_OFF, commonEvent.Support.COMMON_EVENT_SCREEN_ON],
29e75ebbc8Sopenharmony_ci};
30e75ebbc8Sopenharmony_ciconst debounceTimeout = 500;
31e75ebbc8Sopenharmony_ci
32e75ebbc8Sopenharmony_ciclass ScreenLockManager {
33e75ebbc8Sopenharmony_ci  mSubscriber: CommonEventSubscriber | undefined;
34e75ebbc8Sopenharmony_ci
35e75ebbc8Sopenharmony_ci  async init(): Promise<void> {
36e75ebbc8Sopenharmony_ci    this.mSubscriber = await commonEvent.createSubscriber(SCREEN_COMMON_EVENT_INFO);
37e75ebbc8Sopenharmony_ci    commonEvent.subscribe(this.mSubscriber, (err, data) => {
38e75ebbc8Sopenharmony_ci      if (err.code != 0) {
39e75ebbc8Sopenharmony_ci        Log.showError(TAG, `Can't handle screen change, err: ${JSON.stringify(err)}`);
40e75ebbc8Sopenharmony_ci        return;
41e75ebbc8Sopenharmony_ci      }
42e75ebbc8Sopenharmony_ci      Log.showDebug(TAG, `screenChange, err: ${JSON.stringify(err)} data: ${JSON.stringify(data)}`);
43e75ebbc8Sopenharmony_ci      switch (data.event) {
44e75ebbc8Sopenharmony_ci        case commonEvent.Support.COMMON_EVENT_SCREEN_OFF:
45e75ebbc8Sopenharmony_ci          this.notifyScreenEvent(false);
46e75ebbc8Sopenharmony_ci          break;
47e75ebbc8Sopenharmony_ci        case commonEvent.Support.COMMON_EVENT_SCREEN_ON:
48e75ebbc8Sopenharmony_ci          this.notifyScreenEvent(true);
49e75ebbc8Sopenharmony_ci          break;
50e75ebbc8Sopenharmony_ci        default:
51e75ebbc8Sopenharmony_ci          Log.showError(TAG, 'unknow event');
52e75ebbc8Sopenharmony_ci      }
53e75ebbc8Sopenharmony_ci    });
54e75ebbc8Sopenharmony_ci  }
55e75ebbc8Sopenharmony_ci
56e75ebbc8Sopenharmony_ci  @debounce(debounceTimeout)
57e75ebbc8Sopenharmony_ci  notifyScreenEvent(isScreenOn: boolean): void {
58e75ebbc8Sopenharmony_ci    EventManager.publish(obtainLocalEvent(SCREEN_CHANGE_EVENT, isScreenOn));
59e75ebbc8Sopenharmony_ci    Log.showDebug(TAG, `publish ${SCREEN_CHANGE_EVENT} screenState: ${isScreenOn ? 'true' : 'false'}`);
60e75ebbc8Sopenharmony_ci  }
61e75ebbc8Sopenharmony_ci}
62e75ebbc8Sopenharmony_ci
63e75ebbc8Sopenharmony_cilet sScreenLockManager = createOrGet(ScreenLockManager, TAG);
64e75ebbc8Sopenharmony_ci
65e75ebbc8Sopenharmony_ciexport default sScreenLockManager;