1/* 2 * Copyright (c) 2024 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 common from '@ohos.app.ability.common'; 17import ConfigurationConstant from '@ohos.app.ability.ConfigurationConstant'; 18import util from '@ohos.util'; 19import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 20import inputEventClient from '@ohos.multimodalInput.inputEventClient'; 21 22const TAG: string = 'InjectNotice'; 23const BN_WIDTH: Length = '176vp'; 24 25@Extend(Button) 26function customizeButton(backcolor?: Color | Resource, titleColor?:Color | Resource) { 27 .backgroundColor(backcolor === undefined ? Color.Transparent : backcolor) 28 .fontColor(titleColor === undefined ? '#0D81F2' : titleColor) 29 .fontSize($r('sys.float.Body_L')) 30 .height('40vp') 31 .width(BN_WIDTH) 32 .fontWeight(FontWeight.Medium) 33} 34 35@CustomDialog 36struct InputCustomDialog { 37 @Prop titleTip: string = ''; 38 @Prop innerLineColor: ResourceColor = 0; 39 controller?: CustomDialogController; 40 cancel: () => void = () => { 41 } 42 confirm: () => void = () => { 43 } 44 45 @Builder buildIcon() { 46 Row() { 47 Image($r('app.media.icon_notice')) 48 .width('40vp').height('40vp') 49 .objectFit(ImageFit.Contain) 50 .autoResize(false) 51 .draggable(false) 52 .borderRadius('20vp') 53 } 54 .height('64vp') 55 .padding({top: '24vp'}) 56 } 57 58 @Builder buildTitle() { 59 Text(this.titleTip) 60 .fontSize($r('sys.float.Title_S')) 61 .fontColor($r('sys.color.font_primary')) 62 .textAlign(TextAlign.Center) 63 .fontWeight(FontWeight.Bold) 64 .lineHeight('27vp') 65 .padding({ left: '8vp', right: '8vp' }) 66 .constraintSize({minHeight:'56vp'}) 67 } 68 69 @Builder buildContext() { 70 Text($r('app.string.WARNING_TIP')) 71 .fontSize($r('sys.float.Body_L')) 72 .fontColor($r('sys.color.font_primary')) 73 .textAlign(TextAlign.Center) 74 .fontWeight(FontWeight.Medium) 75 .padding({ left: '8vp', right: '8vp' }) 76 } 77 78 @Builder buildBnDeny() { 79 Button($r('app.string.bn_not_agree')) 80 .type(ButtonType.Normal) 81 .borderRadius($r('sys.float.padding_level4')) 82 .onClick(() => { 83 if (this.controller) { 84 this.controller.close(); 85 } 86 this.cancel(); 87 }).customizeButton($r('sys.color.comp_background_tertiary'), $r('sys.color.font_emphasize')) 88 } 89 90 @Builder buildBnAgree() { 91 Button($r('app.string.bn_agree'), { type: ButtonType.Normal, stateEffect: true }) 92 .borderRadius($r('sys.float.padding_level4')) 93 .fontColor($r('sys.color.font_on_primary')) 94 .onClick(() => { 95 if (this.controller) { 96 this.controller.close(); 97 } 98 this.confirm(); 99 }) 100 .width(BN_WIDTH) 101 } 102 103 build() { 104 Column() { 105 this.buildIcon(); 106 this.buildTitle(); 107 this.buildContext(); 108 Row({ space: '16vp' }) { 109 this.buildBnDeny(); 110 this.buildBnAgree(); 111 } 112 .margin({top:'16vp', bottom:'16vp'}) 113 .height('40vp') 114 } 115 .padding({ left: '15vp', right: '15vp' }) 116 .margin(1) 117 .outlineWidth(1) 118 .outlineColor(this.innerLineColor) 119 .borderRadius($r('sys.float.ohos_id_corner_radius_dialog')) 120 .backgroundBlurStyle(BlurStyle.COMPONENT_ULTRA_THIN) 121 } 122} 123 124@Entry 125@Component 126struct InputDialog { 127 private mContext: common.UIExtensionContext | undefined = undefined; 128 @State titleTip: string = ''; 129 @State outBorderColor : ResourceColor = 0; 130 @State innerBorderColor : ResourceColor = 0; 131 @StorageProp('currentColorMode') @Watch('onColorModeChange') 132 currentMode: number = AppStorage.get('currentColorMode') as number; 133 dialogController: CustomDialogController = new CustomDialogController({ 134 builder: InputCustomDialog({ 135 cancel: () => { this.onCancel() }, 136 confirm: () => { this.onConfirm() }, 137 titleTip: this.titleTip, 138 innerLineColor: this.innerBorderColor 139 }), 140 cancel: this.existApp, 141 autoCancel: false, 142 alignment: DialogAlignment.Center, 143 customStyle: false, 144 shadow: ShadowStyle.OUTER_FLOATING_MD, 145 borderWidth: '1vp', 146 borderColor: this.outBorderColor, 147 cornerRadius: $r('sys.float.ohos_id_corner_radius_dialog'), 148 backgroundColor: Color.Transparent, 149 width: '400vp' 150 }); 151 152 aboutToAppear() { 153 console.log(TAG, `aboutToAppear currentMode:${this.currentMode}`); 154 let storage = LocalStorage.getShared(); 155 this.mContext = storage.get<common.UIExtensionContext>('context'); 156 let tileTipFormat = this.mContext!.resourceManager.getStringSync($r('app.string.text_inject_tip_title')); 157 this.titleTip = util.format(tileTipFormat, ''); 158 this.onColorModeChange(); 159 console.log(TAG, `aboutToAppear titelTip:${this.titleTip}`); 160 } 161 162 aboutToDisappear() { 163 console.log(TAG, 'aboutToDisappear'); 164 } 165 166 onCancel() { 167 try { 168 console.log(TAG, 'cancel input'); 169 inputEventClient.permitInjection(false); 170 let storage = LocalStorage.getShared(); 171 let session = storage.get<UIExtensionContentSession>('session'); 172 if (session) { 173 console.log(TAG, 'cancel terminateSelf session end'); 174 session.terminateSelf(); 175 } 176 console.log(TAG, 'cancel end'); 177 } catch (err) { 178 console.error(TAG, 'cancel failed:%{public}s', JSON.stringify(err)); 179 } 180 } 181 182 onConfirm() { 183 try { 184 console.info(TAG, 'confirm input'); 185 inputEventClient.permitInjection(true); 186 let storage = LocalStorage.getShared(); 187 let session = storage.get<UIExtensionContentSession>('session'); 188 if (session) { 189 console.log(TAG, 'confirm terminateSelf session end'); 190 session.terminateSelf(); 191 } 192 console.log(TAG, 'confirm end'); 193 } catch (err) { 194 console.error(TAG, 'confirm failed:%{public}s', JSON.stringify(err)); 195 } 196 } 197 198 existApp() { 199 try { 200 console.log(TAG, 'existApp input'); 201 let storage = LocalStorage.getShared(); 202 let session = storage.get<UIExtensionContentSession>('session'); 203 if (session) { 204 console.log(TAG, 'existApp terminateSelf session'); 205 session.terminateSelf(); 206 } 207 } catch (err) { 208 console.error(TAG, 'existApp failed:%{public}s', JSON.stringify(err)); 209 } 210 } 211 212 build() { 213 Column(this.dialogController.open()) { 214 } 215 } 216 217 onColorModeChange(): void { 218 if (this.currentMode == ConfigurationConstant.ColorMode.COLOR_MODE_DARK) { 219 console.info(TAG, 'color mode is dark,curMode:' + this.currentMode); 220 this.outBorderColor = 'rgba(0, 0, 0, 0.4)'; 221 this.innerBorderColor = 'rgba(255, 255, 255, 0.2)'; 222 } else { 223 console.info(TAG, 'color mode is not dark curMode:' + this.currentMode); 224 this.outBorderColor = 'rgba(0, 0, 0, 0.1)'; 225 this.innerBorderColor = 'rgba(255, 255, 255, 0.2)'; 226 } 227 } 228} 229