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 display from '@ohos.display'; 21import deviceInfo from '@ohos.deviceInfo'; 22import { Log } from '../common/utils/utils'; 23import { GlobalContext } from '../common/utils/globalContext'; 24 25const MICROPHONE = 'microphone'; 26const CAMERA = 'camera'; 27let bottomPopoverTypes = ['default', 'phone']; 28 29@Extend(Button) function customizeButton() { 30 .backgroundColor($r('sys.color.ohos_id_color_dialog_bg')) 31 .fontColor($r('sys.color.ohos_id_color_text_primary_activated')) 32 .fontSize(Constants.TEXT_MIDDLE_FONT_SIZE) 33 .fontWeight(FontWeight.Medium) 34 .height(Constants.BUTTON_HEIGHT) 35 .width(Constants.HALF_LENGTH) 36} 37 38@Entry 39@Component 40struct globalSwitch { 41 private context = getContext(this) as common.ServiceExtensionContext; 42 43 privacyDialogController: CustomDialogController | null = new CustomDialogController({ 44 builder: globalDialog(), 45 autoCancel: false, 46 alignment: DialogAlignment.Center, 47 customStyle: true, 48 cancel: () => { this.context.terminateSelf() } 49 }) 50 51 build() {} 52 53 aboutToAppear() { 54 if (this.privacyDialogController !== null) { 55 this.privacyDialogController.open(); 56 } 57 } 58 59 aboutToDisappear() { 60 this.privacyDialogController = null; 61 } 62} 63 64@CustomDialog 65struct globalDialog { 66 private context = getContext(this) as common.ServiceExtensionContext; 67 @State isBottomPopover: boolean = true; 68 @State globalState: string = GlobalContext.load('globalState'); 69 controller?: CustomDialogController 70 71 build() { 72 GridRow({ columns: { xs: Constants.XS_COLUMNS, sm: Constants.SM_COLUMNS, md: Constants.MD_COLUMNS, lg: Constants.LG_COLUMNS }, gutter: Constants.DIALOG_GUTTER }) { 73 GridCol({ span: { xs: Constants.XS_SPAN, sm: Constants.SM_SPAN, md: Constants.DIALOG_MD_SPAN, lg: Constants.DIALOG_LG_SPAN }, 74 offset: {xs: Constants.XS_OFFSET, sm: Constants.SM_OFFSET, md: Constants.DIALOG_MD_OFFSET, lg: Constants.DIALOG_LG_OFFSET} }) { 75 Flex({ justifyContent: FlexAlign.Center, alignItems: this.isBottomPopover ? ItemAlign.End : ItemAlign.Center }) { 76 Column() { 77 Text(this.globalState == MICROPHONE ? $r('app.string.global_title_microphone') : 78 this.globalState == CAMERA ? $r('app.string.global_title_camera') : 79 $r('app.string.global_title_camera_and_microphone')) 80 .fontSize(Constants.TEXT_BIG_FONT_SIZE) 81 .fontColor($r('sys.color.ohos_id_color_text_primary')) 82 .fontWeight(FontWeight.Medium) 83 .lineHeight(Constants.TEXT_BIG_LINE_HEIGHT) 84 .width(Constants.FULL_WIDTH) 85 .padding({ left: Constants.DIALOG_DESP_MARGIN_LEFT, right: Constants.DIALOG_DESP_MARGIN_RIGHT, 86 top: Constants.ROW_PADDING_TOP, bottom: Constants.ROW_PADDING_BOTTOM}) 87 Text(this.globalState == MICROPHONE ? $r('app.string.global_desc_microphone') : 88 this.globalState == CAMERA ? $r('app.string.global_desc_camera') : 89 $r('app.string.global_desc_camera_and_microphone')) 90 .fontSize(Constants.TEXT_MIDDLE_FONT_SIZE) 91 .fontColor($r('sys.color.ohos_id_color_text_primary')) 92 .lineHeight(Constants.TEXT_LINE_HEIGHT) 93 .width(Constants.FULL_WIDTH) 94 .padding({ left: Constants.DIALOG_DESP_MARGIN_LEFT, right: Constants.DIALOG_DESP_MARGIN_RIGHT }) 95 .margin({ bottom: Constants.DIALOG_DESP_MARGIN_BOTTOM }) 96 Row() { 97 Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Start }) { 98 Button($r('app.string.cancel')) 99 .onClick(() => { 100 this.cancel() 101 }).customizeButton() 102 Divider() 103 .color($r('sys.color.ohos_id_color_list_separator')) 104 .vertical(true) 105 .height(Constants.DIVIDER_HEIGHT) 106 .margin({ top: Constants.BUTTON_MARGIN_TOP }) 107 .opacity(.2) 108 Button($r('app.string.open')) 109 .onClick(() => { 110 this.accept() 111 }).customizeButton() 112 }.height(Constants.ROW_HEIGHT) 113 .margin({ left: Constants.BUTTON_MARGIN_LEFT, right: Constants.BUTTON_MARGIN_RIGHT }) 114 } 115 } 116 .backgroundColor($r('sys.color.ohos_id_color_dialog_bg')) 117 .borderRadius(Constants.DIALOG_PRIVACY_BORDER_RADIUS) 118 .width(Constants.FULL_WIDTH) 119 .margin({ bottom: $r('sys.float.ohos_id_dialog_margin_bottom') }) 120 .clip(true) 121 }.width(Constants.FULL_WIDTH) 122 .height(Constants.FULL_HEIGHT) 123 } 124 }.margin({ left: this.isBottomPopover ? Constants.DIALOG_MARGIN_VERTICAL : Constants.DIALOG_MARGIN, 125 right: this.isBottomPopover ? Constants.DIALOG_MARGIN_VERTICAL : Constants.DIALOG_MARGIN }) 126 } 127 128 accept() { 129 Log.info('global accept'); 130 if (this.globalState == MICROPHONE) { 131 let audioManager = audio.getAudioManager(); 132 let audioVolumeManager = audioManager.getVolumeManager(); 133 let groupid = audio.DEFAULT_VOLUME_GROUP_ID; 134 audioVolumeManager.getVolumeGroupManager(groupid).then(audioVolumeGroupManager => { 135 audioVolumeGroupManager.setMicrophoneMute(false).then(() => { 136 this.context.terminateSelf(); 137 }) 138 }) 139 } else if (this.globalState == CAMERA) { 140 let cameraManager = camera.getCameraManager(GlobalContext.load('context')); 141 cameraManager.muteCamera(false); 142 this.context.terminateSelf(); 143 } else { 144 let cameraManager = camera.getCameraManager(GlobalContext.load('context')); 145 cameraManager.muteCamera(false); 146 let audioManager = audio.getAudioManager(); 147 let audioVolumeManager = audioManager.getVolumeManager(); 148 let groupid = audio.DEFAULT_VOLUME_GROUP_ID; 149 audioVolumeManager.getVolumeGroupManager(groupid).then(audioVolumeGroupManager => { 150 audioVolumeGroupManager.setMicrophoneMute(false).then(() => { 151 this.context.terminateSelf(); 152 }) 153 }) 154 } 155 156 } 157 158 cancel() { 159 Log.info('global cancel'); 160 this.context.terminateSelf(); 161 } 162 163 aboutToAppear() { 164 try { 165 let dis = display.getDefaultDisplaySync(); 166 let isVertical = dis.width > dis.height ? false : true; 167 this.isBottomPopover = (bottomPopoverTypes.includes(deviceInfo.deviceType) && isVertical) ? true : false; 168 } catch (exception) { 169 Log.error('Failed to obtain the default display object. Code: ' + JSON.stringify(exception)); 170 }; 171 } 172 173}