1/* 2 * Copyright (c) 2025 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 { HiLog } from './HiLog'; 17import commonEventManager from '@ohos.commonEventManager'; 18import { BusinessError, emitter } from '@kit.BasicServicesKit'; 19import Constants from './constant'; 20 21const TAG = 'CommonEventManager'; 22 23export default class CommonEventManager { 24 private static instance: CommonEventManager; 25 private subscribe: commonEventManager.CommonEventSubscriber | undefined; 26 private subscribeInfo: commonEventManager.CommonEventSubscribeInfo = { 27 events: [commonEventManager.Support.COMMON_EVENT_SCREEN_LOCKED] 28 }; 29 30 private constructor() { 31 } 32 33 static getInstance(): CommonEventManager { 34 if (!CommonEventManager.instance) { 35 CommonEventManager.instance = new CommonEventManager(); 36 } 37 return CommonEventManager.instance; 38 } 39 40 public async init(): Promise<void> { 41 HiLog.info(TAG, 'CommonEventManager init'); 42 let subscribeCB = (err: BusinessError, data: commonEventManager.CommonEventData): void => { 43 if (err) { 44 HiLog.wrapError(TAG, err, 'subscribeCB error'); 45 } else { 46 switch (data.event) { 47 case commonEventManager.Support.COMMON_EVENT_SCREEN_LOCKED: 48 HiLog.info(TAG, 'subscribeCB COMMON_EVENT_SCREEN_LOCKED'); 49 emitter.emit(Constants.SCREEN_OFF_EVENT); 50 break; 51 default: 52 HiLog.error(TAG, 'unKnow event'); 53 } 54 } 55 }; 56 57 let createCB = (err: BusinessError, commonEventSubscriber: commonEventManager.CommonEventSubscriber): void => { 58 if (err) { 59 HiLog.wrapError(TAG, err, 'createCB failed'); 60 } else { 61 HiLog.info(TAG, 'createCB data'); 62 this.subscribe = commonEventSubscriber; 63 try { 64 commonEventManager.subscribe(this.subscribe, subscribeCB); 65 HiLog.info(TAG, 'subscribe success'); 66 } catch (err) { 67 HiLog.wrapError(TAG, err, 'subscribe failed'); 68 } 69 } 70 }; 71 72 try { 73 commonEventManager.createSubscriber(this.subscribeInfo, createCB); 74 HiLog.info(TAG, 'createSubscriber success'); 75 } catch (err) { 76 HiLog.wrapError(TAG, err, 'createSubscriber failed'); 77 } 78 } 79 80 public unsubscribe(): void { 81 try { 82 commonEventManager.unsubscribe(this.subscribe, (err: BusinessError) => { 83 if (err) { 84 HiLog.wrapError(TAG, err, 'unsubscribe error'); 85 return; 86 } 87 this.subscribe = undefined; 88 HiLog.info(TAG, 'subscribe unsubscribe'); 89 }); 90 } catch (error) { 91 HiLog.wrapError(TAG, error, 'Failed to unsubscribe'); 92 } 93 } 94}