1/* 2 * Copyright (c) 2022 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 bundle from '@ohos.bundle'; 20import abilityAccessCtrl from '@ohos.abilityAccessCtrl'; 21import { Log } from '../common/utils/utils' 22 23const MICROPHONE = 'microphone' 24const CAMERA = 'camera' 25 26@Extend(Button) function customizeButton() { 27 .backgroundColor($r('app.color.default_background_color')) 28 .fontColor($r('app.color.button_color')) 29 .fontWeight(FontWeight.Medium) 30 .height(Constants.BUTTON_HEIGHT) 31 .width(Constants.BUTTON_WIDTH) 32} 33 34@Entry 35@Component 36struct globalSwitch { 37 privacyDialogController: CustomDialogController = new CustomDialogController({ 38 builder: globalDialog(), 39 autoCancel: false, 40 alignment: DialogAlignment.Center, 41 customStyle: true 42 }) 43 44 build() {} 45 46 aboutToAppear() { 47 this.privacyDialogController.open() 48 } 49} 50 51@CustomDialog 52struct globalDialog { 53 controller: CustomDialogController 54 55 build() { 56 GridRow({ columns: { xs: Constants.XS_COLUMNS, sm: Constants.SM_COLUMNS, md: Constants.MD_COLUMNS, lg: Constants.LG_COLUMNS }, gutter: Constants.DIALOG_GUTTER }) { 57 GridCol({ span: { xs: Constants.XS_SPAN, sm: Constants.SM_SPAN, md: Constants.DIALOG_MD_SPAN, lg: Constants.DIALOG_LG_SPAN }, 58 offset: {xs: Constants.XS_OFFSET, sm: Constants.SM_OFFSET, md: Constants.DIALOG_MD_OFFSET, lg: Constants.DIALOG_LG_OFFSET} }) { 59 Flex({ justifyContent: FlexAlign.Center, alignItems: globalThis.isBottomPopover ? ItemAlign.End : ItemAlign.Center }) { 60 Column() { 61 Text(globalThis.globalState == MICROPHONE ? $r('app.string.global_title_microphone') : 62 globalThis.globalState == CAMERA ? $r('app.string.global_title_camera') : 63 $r('app.string.global_title_camera_and_microphone')) 64 .fontSize(Constants.TEXT_BIG_FONT_SIZE) 65 .fontColor($r('app.color.label_color')) 66 .fontWeight(FontWeight.Medium) 67 .lineHeight(Constants.TEXT_BIG_LINE_HEIGHT) 68 .width(Constants.FULL_WIDTH) 69 .padding({ left: Constants.DIALOG_DESP_MARGIN_LEFT, right: Constants.DIALOG_DESP_MARGIN_RIGHT, 70 top: Constants.ROW_PADDING_TOP, bottom: Constants.ROW_PADDING_BOTTOM}) 71 Text(globalThis.globalState == MICROPHONE ? $r('app.string.global_desc_microphone') : 72 globalThis.globalState == CAMERA ? $r('app.string.global_desc_camera') : 73 $r('app.string.global_desc_camera_and_microphone')) 74 .fontSize(Constants.TEXT_MIDDLE_FONT_SIZE) 75 .fontColor($r('app.color.label_color')) 76 .lineHeight(Constants.TEXT_LINE_HEIGHT) 77 .width(Constants.FULL_WIDTH) 78 .padding({ left: Constants.DIALOG_DESP_MARGIN_LEFT, right: Constants.DIALOG_DESP_MARGIN_RIGHT }) 79 .margin({ bottom: Constants.DIALOG_DESP_MARGIN_BOTTOM }) 80 Row() { 81 Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Start }) { 82 Button($r('app.string.cancel')) 83 .fontSize(Constants.BUTTON_FONT_SIZE) 84 .onClick(() => { 85 this.cancel() 86 }).customizeButton().margin({ left: Constants.BUTTON_MARGIN_LEFT }) 87 Text('|').fontSize(Constants.BUTTON_DIVIDER_FONT_SIZE).fontColor($r('app.color.divider_color')) 88 .margin({ top: Constants.BUTTON_MARGIN_TOP }) 89 Button($r('app.string.open')) 90 .fontSize(Constants.BUTTON_FONT_SIZE) 91 .onClick(() => { 92 this.accept() 93 }).customizeButton().margin({ right: Constants.BUTTON_MARGIN_RIGHT }) 94 }.height(Constants.ROW_HEIGHT) 95 } 96 } 97 .backgroundColor($r('app.color.default_background_color')) 98 .borderRadius(Constants.DIALOG_PRIVACY_BORDER_RADIUS) 99 .width(Constants.FULL_WIDTH) 100 .margin({ bottom: Constants.DIALOG_MARGIN_BOTTOM }) 101 }.width(Constants.FULL_WIDTH) 102 .height(Constants.FULL_HEIGHT) 103 } 104 }.margin({ left: globalThis.isBottomPopover ? Constants.DIALOG_MARGIN_VERTICAL : Constants.DIALOG_MARGIN, 105 right: globalThis.isBottomPopover ? Constants.DIALOG_MARGIN_VERTICAL : Constants.DIALOG_MARGIN }) 106 } 107 108 accept() { 109 Log.info('global accept') 110 if(globalThis.globalState == MICROPHONE) { 111 var audioManager = audio.getAudioManager(); 112 audioManager.setMicrophoneMute(false).then(() => { 113 globalThis.globalContext.terminateSelf() 114 }) 115 }else if(globalThis.globalState == CAMERA) { 116 let cameraManager = camera.getCameraManager(globalThis.globalContext); 117 cameraManager.muteCamera(false); 118 globalThis.globalContext.terminateSelf(); 119 }else { 120 let cameraManager = camera.getCameraManager(globalThis.globalContext); 121 cameraManager.muteCamera(false) 122 var audioManager = audio.getAudioManager(); 123 audioManager.setMicrophoneMute(false).then(() => { 124 globalThis.globalContext.terminateSelf() 125 }) 126 } 127 128 } 129 130 cancel() { 131 Log.info('global cancel') 132 globalThis.globalContext.terminateSelf() 133 } 134 135 aboutToAppear() { 136 setTimeout(function() { 137 globalThis.globalContext.terminateSelf() 138 }, 1000 * 60) 139 var acManager = abilityAccessCtrl.createAtManager() 140 bundle.getApplicationInfo(Constants.BUNDLE_NAME, 0).then(data => { 141 acManager.grantUserGrantedPermission(data.accessTokenId, "ohos.permission.MICROPHONE", 2) 142 }) 143 } 144 145}