1/* 2 * Copyright (c) 2021-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 connection from '@ohos.net.connection' 18import { CommonEventSubscriber } from 'commonEvent/commonEventSubscriber'; 19import { CommonEventData } from 'commonEvent/commonEventData'; 20 21import createOrGet from '../../../../../../../common/src/main/ets/default/SingleInstanceHelper'; 22import Log from '../../../../../../../common/src/main/ets/default/Log'; 23import type { AirplaneServiceListener } from '../common/Constants'; 24import { AIRPLANE_MODE_STATUS } from '../common/Constants'; 25 26const TAG = 'AirplaneModel'; 27 28const AIRPLANE_SUBSCRIBE_INFO = { 29 events: [CommonEvent.Support.COMMON_EVENT_AIRPLANE_MODE_CHANGED] 30}; 31 32class AirplaneService { 33 mIsStart = false; 34 mListener: AirplaneServiceListener; 35 mSubscriber: CommonEventSubscriber; 36 37 startService() { 38 if (this.mIsStart) return; 39 this.mIsStart = true; 40 41 this.subscribe(); 42 } 43 44 stopService() { 45 if (!this.mIsStart) return; 46 this.mIsStart = false; 47 48 this.mListener = null; 49 this.unsubscribe(); 50 this.unregisterListener(); 51 } 52 53 async subscribe() { 54 this.unsubscribe(); 55 56 this.mSubscriber = await CommonEvent.createSubscriber(AIRPLANE_SUBSCRIBE_INFO); 57 CommonEvent.subscribe(this.mSubscriber, (err, data: CommonEventData) => { 58 Log.showInfo(TAG, `subscribe cb -> err:${JSON.stringify(err || {})} data:${JSON.stringify(data || {})}`); 59 if (err && err.code !== 0) { 60 Log.showInfo(TAG, `subscribe error ${JSON.stringify(err)}`); 61 return; 62 } 63 if (data && data.event === CommonEvent.Support.COMMON_EVENT_AIRPLANE_MODE_CHANGED) { 64 const status = String(data.code) === AIRPLANE_MODE_STATUS.ON; 65 this.mListener?.updateState(status); 66 } 67 }) 68 } 69 70 unsubscribe() { 71 if (!this.mSubscriber) return; 72 73 CommonEvent.unsubscribe(this.mSubscriber); 74 this.mSubscriber = null; 75 } 76 77 registerListener(listener: AirplaneServiceListener) { 78 this.mListener = listener; 79 } 80 81 unregisterListener() { 82 this.mListener = null; 83 } 84 85 enableAirplaneMode() { 86 Log.showInfo(TAG, 'open airplane'); 87 return connection.enableAirplaneMode(); 88 } 89 90 disableAirplaneMode() { 91 Log.showInfo(TAG, 'close airplane'); 92 return connection.disableAirplaneMode(); 93 } 94} 95 96const sAirplaneService = createOrGet(AirplaneService, TAG); 97 98export default sAirplaneService; 99