1/* 2 * Copyright (c) 2022 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 { CommonEventSubscriber } from "commonEvent/commonEventSubscriber"; 18import createOrGet from "./SingleInstanceHelper"; 19import EventManager from "./event/EventManager"; 20import Log from "./Log"; 21import { obtainLocalEvent } from "./event/EventUtil"; 22import { debounce } from "./Decorators"; 23export const SCREEN_CHANGE_EVENT = "screenChangeEvent"; 24 25const TAG = "ScreenLockManager"; 26const SCREEN_COMMON_EVENT_INFO = { 27 events: [commonEvent.Support.COMMON_EVENT_SCREEN_OFF, commonEvent.Support.COMMON_EVENT_SCREEN_ON], 28}; 29const debounceTimeout = 500; 30 31class ScreenLockManager { 32 mSubscriber: CommonEventSubscriber | undefined; 33 34 async 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.showError(TAG, `Can't handle screen change, err: ${JSON.stringify(err)}`); 39 return; 40 } 41 Log.showDebug(TAG, `screenChange, err: ${JSON.stringify(err)} data: ${JSON.stringify(data)}`); 42 switch (data.event) { 43 case commonEvent.Support.COMMON_EVENT_SCREEN_OFF: 44 this.notifyScreenEvent(false); 45 break; 46 case commonEvent.Support.COMMON_EVENT_SCREEN_ON: 47 this.notifyScreenEvent(true); 48 break; 49 default: 50 Log.showError(TAG, `unknow event`); 51 } 52 }); 53 } 54 55 @debounce(debounceTimeout) 56 notifyScreenEvent(isScreenOn: boolean) { 57 EventManager.publish(obtainLocalEvent(SCREEN_CHANGE_EVENT, isScreenOn)); 58 Log.showDebug(TAG, `publish ${SCREEN_CHANGE_EVENT} screenState: ${isScreenOn}`); 59 } 60} 61 62let sScreenLockManager = createOrGet(ScreenLockManager, TAG); 63export default sScreenLockManager as ScreenLockManager;