• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
17
18import { Log } from '../../utils/Log'
19import { EventBus } from '../../worker/eventbus/EventBus'
20import { EventBusManager } from '../../worker/eventbus/EventBusManager'
21
22const SCREEN_COMMON_EVENT_INFO = {
23    events: [commonEvent.Support.COMMON_EVENT_SCREEN_OFF],
24};
25
26
27export class ScreenLockManager {
28    private TAG: string = '[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(`${this.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(`${this.TAG} Can't handle screen change, err: ${JSON.stringify(err)}`);
39                return;
40            }
41            Log.info(`${this.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(`${this.TAG} COMMON_EVENT_SCREEN_OFF`)
45                    this.notifyScreenEvent(false);
46                    break;
47                default:
48                    Log.log(`${this.TAG} unknow event`);
49            }
50        });
51    }
52
53    close() {
54        commonEvent.unsubscribe(this.mSubscriber)
55    }
56
57    notifyScreenEvent(isScreenOn: boolean) {
58        this.appEventBus.emit(ScreenLockManager.SCREEN_CHANGE_EVENT, [isScreenOn]);
59        Log.log(`${this.TAG} publish ${ScreenLockManager.SCREEN_CHANGE_EVENT} screenState: ${isScreenOn}`);
60    }
61}
62
63export default ScreenLockManager;