1//@ts-nocheck 2/* 3 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import settings from '@ohos.settings'; 18import Context from 'application/ServiceExtensionContext'; 19import Log from '../../../../../../../common/src/main/ets/default/Log'; 20import createOrGet from '../../../../../../../common/src/main/ets/default/SingleInstanceHelper'; 21import { TintContentInfo, getOrCreateTintContentInfo 22} from '../../../../../../../common/src/main/ets/default/TintStateManager'; 23import { FASlotName } from '../../../../../../../common/src/main/ets/default/Constants'; 24import AbilityManager from '../../../../../../../common/src/main/ets/default/abilitymanager/abilityManager'; 25import Constants from '../../../../../../../common/src/main/ets/default/Constants'; 26import AirplaneService from '../model/AirplaneService'; 27import type { AirplaneServiceListener } from '../common/Constants'; 28import { AIRPLANE_MODE_STATUS } from '../common/Constants'; 29 30const TAG = 'AirplaneVM'; 31 32class AirplaneVM implements AirplaneServiceListener { 33 mIsStart: boolean = false; 34 mAirplaneStatus: boolean = false; 35 mTintContentInfo: TintContentInfo = getOrCreateTintContentInfo(FASlotName.AIR_PLANE); 36 mIsToggling: boolean = false; 37 context: Context; 38 39 constructor() { 40 this.context = AbilityManager.getContext(AbilityManager.getContextName(AbilityManager.ABILITY_NAME_CONTROL_PANEL)); 41 42 this.mAirplaneStatus = 43 settings.getValueSync(this.context, Constants.KEY_AIRPLANE_MODE_STATUS, AIRPLANE_MODE_STATUS.OFF) === AIRPLANE_MODE_STATUS.ON 44 45 AppStorage.SetOrCreate('Airplane_Status', this.mAirplaneStatus); 46 } 47 48 startVM() { 49 if (this.mIsStart) return; 50 this.mIsStart = true; 51 52 AirplaneService.registerListener(this); 53 AirplaneService.startService(); 54 55 Log.showInfo(TAG, 'startVM') 56 } 57 58 stopVM() { 59 if (!this.mIsStart) return; 60 this.mIsStart = false; 61 62 AirplaneService.stopService(); 63 } 64 65 updateState(status: boolean) { 66 Log.showInfo(TAG, `updateState ${status}`) 67 this.mAirplaneStatus = status; 68 AppStorage.Set('Airplane_Status', this.mAirplaneStatus); 69 } 70 71 getAirplaneStatus() { 72 return this.mAirplaneStatus; 73 } 74 75 getTintContentInfo(): TintContentInfo { 76 return this.mTintContentInfo; 77 } 78 79 async handleClick() { 80 Log.showInfo(TAG, `handleClick ${this.mIsToggling}`) 81 82 if (this.mIsToggling) return 83 this.mIsToggling = true 84 85 await AirplaneService[this.mAirplaneStatus ? "disableAirplaneMode" : "enableAirplaneMode"](); 86 Log.showInfo(TAG, 'await over') 87 this.mIsToggling = false 88 } 89} 90 91const sAirplaneVM = createOrGet(AirplaneVM, TAG); 92 93export default sAirplaneVM; 94