1/* 2 * Copyright (c) 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 */ 15import bundleManager from '@ohos.bundle.bundleManager'; 16import Constants from '../common/utils/constant'; 17import window from '@ohos.window'; 18import common from '@ohos.app.ability.common'; 19import display from '@ohos.display'; 20import deviceInfo from '@ohos.deviceInfo'; 21import { BusinessError } from '@ohos.base'; 22import { Log } from '../common/utils/utils'; 23import { param, wantInfo } from '../common/utils/typedef'; 24 25let bottomPopoverTypes = ['default', 'phone']; 26let storage = LocalStorage.getShared(); 27let want: wantInfo; 28let win: window.Window; 29 30@Entry(storage) 31@Component 32struct SecurityDialog { 33 private context = getContext(this) as common.ServiceExtensionContext; 34 @LocalStorageLink('want') want: wantInfo = new wantInfo([]); 35 @LocalStorageLink('win') win: window.Window = {} as window.Window; 36 37 securityDialogController: CustomDialogController = new CustomDialogController({ 38 builder: CustomSecurityDialog(), 39 alignment: DialogAlignment.Bottom, 40 customStyle: true, 41 cancel: () => { 42 win.destroyWindow(); 43 this.context.terminateSelf(); 44 } 45 }) 46 47 build() {} 48 49 aboutToAppear() { 50 this.securityDialogController.open(); 51 want = this.want; 52 win = this.win; 53 } 54} 55 56@CustomDialog 57struct CustomSecurityDialog { 58 private context = getContext(this) as common.ServiceExtensionContext; 59 @State isBottomPopover: boolean = true; 60 controller?: CustomDialogController; 61 descriptor: string = '' 62 @State appName: string = 'ToBeInstead'; 63 @State index: number = 0; 64 @State naviHeight: number = 0 65 66 securityParams : Array<param> = [ 67 new param($r('app.media.ic_public_gps'), $r('app.string.location_desc')), 68 new param($r('app.media.ic_public_folder'), $r('app.string.media_files_desc')) 69 ] 70 71 GetAppName() { 72 let bundleName: string = want.parameters['ohos.aafwk.param.callerBundleName']; 73 bundleManager.getApplicationInfo(bundleName, bundleManager.ApplicationFlag.GET_APPLICATION_INFO_DEFAULT) 74 .then(data => { 75 this.context.resourceManager.getStringValue(data.labelResource) 76 .then(data => { 77 this.appName = data; 78 }) 79 .catch((error: BusinessError) => { 80 Log.error('getStringValue failed. err is ' + JSON.stringify(error)); 81 }); 82 }) 83 .catch((error: BusinessError) => { 84 Log.error('getApplicationInfo failed. err is ' + JSON.stringify(error)); 85 }); 86 } 87 88 destruction() { 89 win.destroyWindow(); 90 this.context.terminateSelf(); 91 } 92 93 getAvoidWindow() { 94 let type = window.AvoidAreaType.TYPE_SYSTEM; 95 try { 96 let avoidArea = win.getWindowAvoidArea(type); 97 Log.info('avoidArea: ' + JSON.stringify(avoidArea)); 98 this.naviHeight = avoidArea.bottomRect.height; 99 } catch (exception) { 100 Log.error('Failed to obtain the area. Cause:' + JSON.stringify(exception)); 101 } 102 } 103 104 getPopupPosition() { 105 try { 106 let dis = display.getDefaultDisplaySync(); 107 let isVertical = dis.width > dis.height ? false : true; 108 this.isBottomPopover = (bottomPopoverTypes.includes(deviceInfo.deviceType) && isVertical) ? true : false; 109 } catch (exception) { 110 Log.error('Failed to obtain the default display object. Code: ' + JSON.stringify(exception)); 111 }; 112 } 113 114 build() { 115 GridRow({ 116 columns: { 117 xs: Constants.XS_COLUMNS, sm: Constants.SM_COLUMNS, md: Constants.MD_COLUMNS, lg: Constants.LG_COLUMNS 118 }, 119 gutter: Constants.DIALOG_GUTTER 120 }) { 121 GridCol({ 122 span: { 123 xs: Constants.XS_SPAN, sm: Constants.SM_SPAN, md: Constants.DIALOG_MD_SPAN, lg: Constants.DIALOG_LG_SPAN 124 }, 125 offset: { 126 xs: Constants.XS_OFFSET, 127 sm: Constants.SM_OFFSET, 128 md: Constants.DIALOG_MD_OFFSET, 129 lg: Constants.DIALOG_LG_OFFSET 130 } 131 }) { 132 Flex({ 133 justifyContent: FlexAlign.Center, 134 alignItems: this.isBottomPopover ? ItemAlign.End : ItemAlign.Center 135 }) { 136 Row() { 137 Column() { 138 Image(this.securityParams[this.index].icon) // icon 139 .width(Constants.SECURITY_ICON_WIDTH) 140 .height(Constants.SECURITY_ICON_HEIGHT) 141 .fillColor($r('sys.color.ohos_id_color_text_primary')) 142 .margin({ 143 top: Constants.SECURITY_ICON_MARGIN_TOP, 144 bottom: Constants.SECURITY_ICON_MARGIN_BOTTOM 145 }) 146 Column() { // content 147 Text() { 148 Span($r('app.string.Temporarily_authorize')) 149 Span(this.appName) 150 Span($r('app.string.To_access')) 151 Span(this.securityParams[this.index].label) 152 } 153 .textAlign(TextAlign.Center) 154 .fontColor($r('sys.color.ohos_id_color_text_primary')) 155 .fontSize($r('sys.float.ohos_id_text_size_body1')) 156 .fontWeight(FontWeight.Medium) 157 158 Text() { 159 Span($r('app.string.location_button_desc')) 160 Span($r('app.string.only_can_desc')) 161 Span(this.securityParams[this.index].label) 162 Span($r('app.string.only_after_desc')) 163 Span($r('app.string.prevent_desc')) 164 } 165 .textAlign(TextAlign.Center) 166 .fontColor($r('sys.color.ohos_id_color_text_secondary')) 167 .fontSize($r('sys.float.ohos_id_text_size_body2')) 168 .margin({ 169 top: Constants.SECURITY_DESCRIPTOR_DEVIDER_MARGIN_TOP, 170 }) 171 } 172 Column() { 173 Button($r('app.string.Got_it')) // button 174 .fontSize($r('sys.float.ohos_id_text_size_button1')) 175 .fontWeight(FontWeight.Medium) 176 .fontColor($r('sys.color.ohos_id_color_text_primary_activated')) 177 .backgroundColor($r('sys.color.ohos_id_color_dialog_bg')) 178 .height(Constants.SECURITY_BUTTON_HEIGHT) 179 .width(Constants.FULL_WIDTH) 180 .onClick(() => { 181 if (this.controller !== undefined) { 182 this.controller.close(); 183 } 184 this.destruction(); 185 }) 186 } 187 .margin({ top: Constants.SECURITY_BUTTON_MARGIN_TOP }) 188 .height(Constants.SECURITY_BUTTON_ROW_HEIGHT) 189 }.margin({ left: Constants.SECURITY_TOTAL_MARGIN_LEFT, right: Constants.SECURITY_TOTAL_MARGIN_RIGHT }) 190 } 191 .margin({ bottom: $r('sys.float.ohos_id_dialog_margin_bottom') }) 192 .backgroundColor($r('sys.color.ohos_id_color_dialog_bg')) 193 .borderRadius(Constants.DIALOG_PRIVACY_BORDER_RADIUS) 194 }.width(Constants.FULL_WIDTH) 195 .height(Constants.FULL_HEIGHT) 196 } 197 }.margin({ left: this.isBottomPopover ? Constants.DIALOG_MARGIN_VERTICAL : Constants.DIALOG_MARGIN, 198 right: this.isBottomPopover ? Constants.DIALOG_MARGIN_VERTICAL : Constants.DIALOG_MARGIN, 199 bottom: this.isBottomPopover ? this.naviHeight : 0}) 200 } 201 202 aboutToAppear() { 203 Log.info('onAboutToAppear.'); 204 this.getAvoidWindow(); 205 this.GetAppName(); 206 this.index = want.parameters['ohos.user.security.type']; 207 this.getPopupPosition(); 208 } 209} 210 211