1/* 2 * Copyright (c) 2021-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 '../utils/constant'; 17import audio from '@ohos.multimedia.audio'; 18import camera from '@ohos.multimedia.camera'; 19import deviceInfo from '@ohos.deviceInfo'; 20import display from '@ohos.display'; 21import common from '@ohos.app.ability.common'; 22import { GlobalContext } from "../utils/globalContext"; 23 24let bottomPopoverTypes = ['default', 'phone']; 25 26@Extend(Button) function customizeButton() { 27 .backgroundColor($r('sys.color.ohos_id_color_dialog_bg')) 28 .fontColor($r('sys.color.ohos_id_color_text_primary_activated')) 29 .fontSize(Constants.TEXT_MIDDLE_FONT_SIZE) 30 .fontWeight(FontWeight.Medium) 31 .height(Constants.BUTTON_HEIGHT) 32 .flexGrow(Constants.FLEX_GROW) 33} 34 35@CustomDialog 36export struct globalDialog { 37 private context = getContext(this) as common.UIAbilityContext; 38 @State currentGroup: string = GlobalContext.load('currentPermissionGroup'); 39 @Link globalIsOn: boolean; 40 @State isBottomPopover: boolean = true; 41 controller?: CustomDialogController; 42 43 build() { 44 GridRow({ columns: { xs: Constants.XS_COLUMNS, sm: Constants.SM_COLUMNS, md: Constants.MD_COLUMNS, lg: Constants.LG_COLUMNS }, gutter: Constants.DIALOG_GUTTER }) { 45 GridCol({ span: { xs: Constants.XS_SPAN, sm: Constants.SM_SPAN, md: Constants.DIALOG_MD_SPAN, lg: Constants.DIALOG_LG_SPAN }, 46 offset: {xs: Constants.XS_OFFSET, sm: Constants.SM_OFFSET, md: Constants.DIALOG_MD_OFFSET, lg: Constants.DIALOG_LG_OFFSET} }) { 47 Flex({ justifyContent: FlexAlign.Center, alignItems: this.isBottomPopover ? ItemAlign.End : ItemAlign.Center }) { 48 Column() { 49 Text(this.currentGroup == 'CAMERA' ? $r('app.string.close_camera') : $r('app.string.close_microphone')) 50 .fontSize(Constants.TEXT_BIG_FONT_SIZE) 51 .fontColor($r('sys.color.ohos_id_color_text_primary')) 52 .fontWeight(FontWeight.Medium) 53 .lineHeight(Constants.TEXT_BIG_LINE_HEIGHT) 54 .height(Constants.ROW_HEIGHT) 55 .width(Constants.FULL_WIDTH) 56 .padding({ left: Constants.DIALOG_DESP_MARGIN_LEFT, right: Constants.DIALOG_DESP_MARGIN_RIGHT }) 57 Text(this.currentGroup == 'CAMERA' ? $r('app.string.close_camera_desc') : $r('app.string.close_microphone_desc')) 58 .fontSize(Constants.TEXT_MIDDLE_FONT_SIZE) 59 .fontColor($r('sys.color.ohos_id_color_text_primary')) 60 .lineHeight(Constants.TEXT_LINE_HEIGHT) 61 .width(Constants.FULL_WIDTH) 62 .padding({ left: Constants.DIALOG_DESP_MARGIN_LEFT, right: Constants.DIALOG_DESP_MARGIN_RIGHT }) 63 .margin({ bottom: Constants.DIALOG_DESP_MARGIN_BOTTOM }) 64 Row() { 65 Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Start }) { 66 Button($r('app.string.cancel')) 67 .onClick(() => { 68 this.cancel() 69 }).customizeButton() 70 Divider() 71 .color($r('sys.color.ohos_id_color_list_separator')) 72 .vertical(true) 73 .height(Constants.DIVIDER_HEIGHT) 74 .margin({ top: Constants.BUTTON_MARGIN_TOP }) 75 .opacity(.2) 76 Button($r('app.string.close')) 77 .onClick(() => { 78 this.confirm() 79 }).customizeButton() 80 }.height(Constants.ROW_HEIGHT) 81 .margin({ left: Constants.BUTTON_MARGIN_LEFT, right: Constants.BUTTON_MARGIN_RIGHT }) 82 } 83 } 84 .backgroundColor($r('sys.color.ohos_id_color_dialog_bg')) 85 .borderRadius(Constants.DIALOG_PRIVACY_BORDER_RADIUS) 86 .width(Constants.FULL_WIDTH) 87 .margin({ bottom: $r('sys.float.ohos_id_dialog_margin_bottom') }) 88 .clip(true) 89 }.width(Constants.FULL_WIDTH) 90 .height(Constants.FULL_HEIGHT) 91 } 92 }.margin({ left: this.isBottomPopover ? Constants.DIALOG_MARGIN_VERTICAL : Constants.DIALOG_MARGIN, 93 right: this.isBottomPopover ? Constants.DIALOG_MARGIN_VERTICAL : Constants.DIALOG_MARGIN }) 94 } 95 96 confirm() { 97 if (this.currentGroup === 'CAMERA') { 98 let cameraManager = camera.getCameraManager(GlobalContext.load('context')); 99 cameraManager.muteCamera(true); 100 if (this.controller !== undefined) { 101 this.controller.close(); 102 } 103 } else { 104 let audioManager = audio.getAudioManager(); 105 let audioVolumeManager = audioManager.getVolumeManager(); 106 let groupid = audio.DEFAULT_VOLUME_GROUP_ID; 107 audioVolumeManager.getVolumeGroupManager(groupid).then(audioVolumeGroupManager => { 108 audioVolumeGroupManager.setMicrophoneMute(true).then(() => { 109 if (this.controller !== undefined) { 110 this.controller.close(); 111 } 112 }) 113 }) 114 } 115 } 116 117 cancel() { 118 if (this.controller !== undefined) { 119 this.controller.close(); 120 } 121 } 122 123 aboutToAppear() { 124 try { 125 let dis = display.getDefaultDisplaySync(); 126 let isVertical = dis.width > dis.height ? false : true; 127 this.isBottomPopover = bottomPopoverTypes.includes(deviceInfo.deviceType) && isVertical; 128 } catch (exception) { 129 console.error('Failed to obtain the default display object. Code: ' + JSON.stringify(exception)); 130 }; 131 } 132}