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