1/*
2 * Copyright (c) 2022-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 Constants from '../common/utils/constant';
17import audio from '@ohos.multimedia.audio';
18import camera from '@ohos.multimedia.camera';
19import common from '@ohos.app.ability.common';
20import { CustomContentDialog } from '@ohos.arkui.advanced.Dialog';
21import { Log } from '../common/utils/utils';
22import { GlobalContext } from '../common/utils/globalContext';
23
24const MICROPHONE = 'microphone';
25const CAMERA = 'camera';
26
27@Entry
28@Component
29struct globalSwitch {
30  private context = getContext(this) as common.ServiceExtensionContext;
31  @State globalState: string = GlobalContext.load('globalState');
32
33  dialogController: CustomDialogController | null = new CustomDialogController({
34    builder: CustomContentDialog({
35      contentBuilder: () => {
36        this.buildContent();
37      },
38      contentAreaPadding: { left: Constants.PADDING_24, right: Constants.PADDING_24 },
39      buttons: [
40        {
41          value: $r('app.string.cancel'),
42          buttonStyle: ButtonStyleMode.TEXTUAL,
43          action: () => {
44            Log.info('global cancel');
45            this.context.terminateSelf();
46          }
47        },
48        {
49          value: $r('app.string.open'),
50          buttonStyle: ButtonStyleMode.TEXTUAL,
51          action: () => {
52            Log.info('global accept');
53            if (this.globalState == MICROPHONE) {
54              let audioManager = audio.getAudioManager();
55              let audioVolumeManager = audioManager.getVolumeManager();
56              audioVolumeManager.getVolumeGroupManager(audio.DEFAULT_VOLUME_GROUP_ID).then(audioVolumeGroupManager => {
57                audioVolumeGroupManager.setMicMutePersistent(false, audio.PolicyType.PRIVACY).then(() => {
58                  this.context.terminateSelf();
59                })
60              })
61            } else if (this.globalState == CAMERA) {
62              let cameraManager = camera.getCameraManager(GlobalContext.load('context'));
63              cameraManager.muteCameraPersistent(false, camera.PolicyType.PRIVACY);
64              this.context.terminateSelf();
65            } else {
66              let cameraManager = camera.getCameraManager(GlobalContext.load('context'));
67              cameraManager.muteCameraPersistent(false, camera.PolicyType.PRIVACY);
68              let audioManager = audio.getAudioManager();
69              let audioVolumeManager = audioManager.getVolumeManager();
70              audioVolumeManager.getVolumeGroupManager(audio.DEFAULT_VOLUME_GROUP_ID).then(audioVolumeGroupManager => {
71                audioVolumeGroupManager.setMicMutePersistent(false, audio.PolicyType.PRIVACY).then(() => {
72                  this.context.terminateSelf();
73                })
74              })
75            }
76          }
77        }
78      ],
79    }),
80    autoCancel: false,
81    cancel: () => {
82      this.context.terminateSelf();
83    }
84  });
85
86  @Builder
87  buildContent(): void {
88    Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
89      Column() {
90        Text(this.globalState == MICROPHONE ? $r('app.string.global_title_microphone') :
91          this.globalState == CAMERA ? $r('app.string.global_title_camera') :
92          $r('app.string.global_title_camera_and_microphone'))
93          .fontSize(Constants.TEXT_BIG_FONT_SIZE)
94          .fontColor($r('sys.color.font_primary'))
95          .fontWeight(FontWeight.Medium)
96          .lineHeight(Constants.TEXT_BIG_LINE_HEIGHT)
97          .width(Constants.FULL_WIDTH)
98          .padding({ top: Constants.PADDING_14, bottom: Constants.PADDING_14 })
99        Text(this.globalState == MICROPHONE ? $r('app.string.global_desc_microphone') :
100          this.globalState == CAMERA ? $r('app.string.global_desc_camera') :
101          $r('app.string.global_desc_camera_and_microphone'))
102          .fontSize(Constants.TEXT_MIDDLE_FONT_SIZE)
103          .fontColor($r('sys.color.font_primary'))
104          .lineHeight(Constants.TEXT_LINE_HEIGHT)
105      }
106      .clip(true)
107    }
108  }
109
110  build() {}
111
112  aboutToAppear() {
113    Log.info('global aboutToAppear');
114    this.dialogController?.open();
115  }
116
117  aboutToDisappear() {
118    this.dialogController = null;
119  }
120}