1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import commonEvent from '@ohos.commonEvent';
17import { Log } from '../../utils/Log';
18import type { EventBus } from '../../worker/eventbus/EventBus';
19import { EventBusManager } from '../../worker/eventbus/EventBusManager';
20
21const TAG = '[ScreenLockManager]:';
22
23const SCREEN_COMMON_EVENT_INFO = {
24  events: [commonEvent.Support.COMMON_EVENT_SCREEN_OFF]
25};
26
27
28export class ScreenLockManager {
29  public static readonly SCREEN_CHANGE_EVENT = 'SCREEN_CHANGE_EVENT';
30  mSubscriber: any;
31  appEventBus: EventBus = EventBusManager.getInstance().getEventBus();
32
33  async init() {
34    Log.log(`${TAG} init`);
35    this.mSubscriber = await commonEvent.createSubscriber(SCREEN_COMMON_EVENT_INFO);
36    commonEvent.subscribe(this.mSubscriber, (err, data) => {
37      if (err.code != 0) {
38        Log.error(`${TAG} Can't handle screen change, err: ${JSON.stringify(err)}`);
39        return;
40      }
41      Log.info(`${TAG} screenChange, err: ${JSON.stringify(err)} data: ${JSON.stringify(data)}`);
42      switch (data.event) {
43        case commonEvent.Support.COMMON_EVENT_SCREEN_OFF:
44          Log.log(`${TAG} COMMON_EVENT_SCREEN_OFF`);
45          this.notifyScreenEvent(false);
46          break;
47        default:
48          Log.log(`${TAG} unknow event`);
49      }
50    });
51  }
52
53  close(): void {
54    commonEvent.unsubscribe(this.mSubscriber);
55  }
56
57  notifyScreenEvent(isScreenOn: boolean): void {
58    this.appEventBus.emit(ScreenLockManager.SCREEN_CHANGE_EVENT, [isScreenOn]);
59    Log.log(`${TAG} publish ${ScreenLockManager.SCREEN_CHANGE_EVENT} screenState: ${isScreenOn}`);
60  }
61}
62
63export default ScreenLockManager;