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 util from '@ohos.util';
18import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
19import inputEventClient from '@ohos.multimodalInput.inputEventClient';
20
21@Extend(Button)
22function customizeButton() {
23  .backgroundColor(Color.Transparent)
24  .fontColor('#0D81F2')
25  .fontSize($r('sys.float.ohos_id_text_size_button1'))
26  .height('40vp')
27  .width('160vp')
28}
29
30@CustomDialog
31struct InputCustomDialog {
32  @Prop titleTip: string = '';
33  controller?: CustomDialogController;
34  cancel: () => void = () => {
35  }
36  confirm: () => void = () => {
37  }
38
39  build() {
40    Column() {
41        Row() {
42          Image($r('app.media.icon_notice'))
43            .width('40vp')
44            .height('40vp')
45            .objectFit(ImageFit.Contain)
46            .autoResize(false)
47            .draggable(false)
48        }
49        .height('64vp')
50        .margin({top: '24vp'})
51
52        Row() {
53          Text(this.titleTip)
54            .fontSize($r('sys.float.ohos_id_text_size_headline8'))
55            .fontColor($r('sys.color.ohos_id_color_text_primary'))
56            .textAlign(TextAlign.Center)
57        }
58        .height('56vp')
59        .margin({left: '24vp', right: '24vp'})
60
61        Row() {
62          Text($r('app.string.WARNING_TIP'))
63            .fontSize($r('sys.float.ohos_id_text_size_button1'))
64            .fontColor($r('sys.color.ohos_id_color_text_primary'))
65            .textAlign(TextAlign.Center)
66        }
67        .height('22vp')
68        .margin({left: '24vp', right: '24vp' })
69
70        Row(){
71
72            Button($r('app.string.bn_not_agree'))
73              .onClick(() => {
74                if (this.controller) {
75                  this.controller.close();
76                }
77                this.cancel();
78              }).customizeButton()
79
80            Button($r('app.string.bn_agree'))
81              .onClick(() => {
82                if (this.controller) {
83                  this.controller.close();
84                }
85                this.confirm();
86              }).customizeButton()
87        }
88        .height('64vp')
89        .margin({top: '8vp', left: '16vp', right: '16vp', bottom: '16vp'})
90    }
91    .width('400vp')
92    .backgroundBlurStyle(BlurStyle.COMPONENT_ULTRA_THICK)
93  }
94}
95
96@Entry
97@Component
98struct InputDialog {
99  private mContext?: common.UIExtensionContext;
100  @State titleTip: string = '';
101  dialogController: CustomDialogController = new CustomDialogController({
102    builder: InputCustomDialog({
103      cancel: () => { this.onCancel() },
104      confirm: () => { this.onConfirm() },
105      titleTip: this.titleTip,
106    }),
107    cancel: this.existApp,
108    autoCancel: false,
109    alignment: DialogAlignment.Center,
110    offset: { dx: 0, dy: -20 },
111    customStyle: false,
112    cornerRadius: $r('sys.float.ohos_id_corner_radius_dialog'),
113  });
114
115  aboutToAppear() {
116    console.log('aboutToAppear');
117    let storage = LocalStorage.GetShared();
118    this.mContext = storage.get<common.UIExtensionContext>('context');
119    let tileTipFormat = this.mContext.resourceManager.getStringSync($r('app.string.text_inject_tip_title'));
120    this.titleTip = util.format(tileTipFormat, '');
121    console.log('InjectNotice', `aboutToAppear titelTip:${this.titleTip}`);
122  }
123
124  aboutToDisappear() {
125    console.log('aboutToDisappear');
126  }
127
128  onCancel() {
129    try {
130      console.log('cancel input');
131      let storage = LocalStorage.GetShared()
132      let session = storage.get<UIExtensionContentSession>('session');
133      if (session) {
134        console.log('cancel terminateSelf session end');
135        session.terminateSelf();
136      }
137      console.log('cancel end');
138    } catch (err) {
139      console.error('cancel failed:%{public}s', JSON.stringify(err));
140    }
141  }
142
143  onConfirm() {
144    try {
145      console.info('confirm input');
146      inputEventClient.permitInjection(true);
147      let storage = LocalStorage.GetShared()
148      let session = storage.get<UIExtensionContentSession>('session');
149      if (session) {
150        console.log('confirm terminateSelf session end');
151        session.terminateSelf();
152      }
153      console.log('confirm end');
154    } catch (err) {
155      console.error('confirm failed:%{public}s', JSON.stringify(err));
156    }
157  }
158
159  existApp() {
160    try {
161      console.log('existApp input');
162      let storage = LocalStorage.GetShared()
163      let session = storage.get<UIExtensionContentSession>('session');
164      if (session) {
165        console.log('existApp terminateSelf session');
166        session.terminateSelf();
167      }
168    } catch (err) {
169      console.error('existApp failed:%{public}s', JSON.stringify(err));
170    }
171  }
172
173  build() {
174    Column(this.dialogController.open()) {
175    }
176  }
177}
178