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    if (this.context == undefined || this.context == null) {
42      Log.showInfo(TAG, `constructor, this.context is null`);
43      return;
44    }
45    try {
46      this.mAirplaneStatus =
47        settings.getValueSync(this.context, Constants.KEY_AIRPLANE_MODE_STATUS, AIRPLANE_MODE_STATUS.OFF) === AIRPLANE_MODE_STATUS.ON
48    } catch (err) {
49      Log.showError(TAG, `AirplaneVM: ${this.context}, ${JSON.stringify(err)}`);
50    }
51    AppStorage.SetOrCreate('Airplane_Status', this.mAirplaneStatus);
52  }
53
54  startVM() {
55    if (this.mIsStart) return;
56    this.mIsStart = true;
57
58    AirplaneService.registerListener(this);
59    AirplaneService.startService();
60
61    Log.showInfo(TAG, 'startVM')
62  }
63
64  stopVM() {
65    if (!this.mIsStart) return;
66    this.mIsStart = false;
67
68    AirplaneService.stopService();
69  }
70
71  updateState(status: boolean) {
72    Log.showInfo(TAG, `updateState ${status}`)
73    this.mAirplaneStatus = status;
74    AppStorage.Set('Airplane_Status', this.mAirplaneStatus);
75  }
76
77  getAirplaneStatus() {
78    return this.mAirplaneStatus;
79  }
80
81  getTintContentInfo(): TintContentInfo {
82    return this.mTintContentInfo;
83  }
84
85  async handleClick() {
86    Log.showInfo(TAG, `handleClick ${this.mIsToggling}`)
87
88    if (this.mIsToggling) return
89    this.mIsToggling = true
90
91    await AirplaneService[this.mAirplaneStatus ? "disableAirplaneMode" : "enableAirplaneMode"]();
92    Log.showInfo(TAG, 'await over')
93    this.mIsToggling = false
94  }
95}
96
97const sAirplaneVM = createOrGet(AirplaneVM, TAG);
98
99export default sAirplaneVM;
100