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