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 */
15import Log from '../../../../../../common/src/main/ets/default/Log';
16import Want from '@ohos.app.ability.Want';
17import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
18import measure from '@ohos.measure';
19import display from '@ohos.display'
20
21const TAG = 'AVScreenCapture-DiaLogPage';
22
23@CustomDialog
24struct PrivacyWindowDialog {
25  cancel?: () => void;
26  confirm?: () => void;
27  controller: CustomDialogController;
28  appLabel: string = '';
29  overflow: boolean = false;
30
31  build() {
32    Column() {
33      if (this.overflow) {
34        Row() {
35          Text($r('app.string.avscreencapture_privacy_window_title'))
36            .fontWeight(FontWeight.Bold)
37            .textAlign(TextAlign.Start)
38            .width('100%')
39            .textOverflow({ overflow: TextOverflow.Ellipsis})
40            .maxLines(2)
41            .fontSize(16)
42        }
43        .alignItems(VerticalAlign.Center)
44        .padding({ top: 16, bottom: 16 })
45        .margin({ left: 24, right: 24})
46      } else {
47        Row() {
48          Text($r('app.string.avscreencapture_privacy_window_title'))
49            .fontWeight(FontWeight.Bold)
50            .textAlign(TextAlign.Start)
51            .width('100%')
52            .fontSize($r('sys.float.ohos_id_text_size_headline8'))
53        }
54        .alignItems(VerticalAlign.Center)
55        .margin({ left: 24, right: 24 })
56        .height(56)
57      }
58
59      Column() {
60        Text($r('app.string.avscreencapture_privacy_window_content', this.appLabel))
61          .fontSize($r('sys.float.ohos_id_text_size_body1'))
62          .fontColor($r('sys.color.ohos_id_color_text_primary'))
63          .width('100%')
64      }
65      .margin({ left: 24, right: 24 })
66
67      Flex({ justifyContent: FlexAlign.SpaceAround}) {
68        Column() {
69          Button($r('app.string.avscreencapture_privacy_window_cancel'), { type: ButtonType.Capsule })
70            .onClick(() => {
71              this.controller.close();
72              if (this.cancel) {
73                this.cancel();
74              }
75            })
76            .backgroundColor(Color.Transparent)
77            .fontColor($r('sys.color.ohos_id_color_text_primary_activated'))
78            .fontSize($r('sys.float.ohos_id_text_size_button1'))
79            .width('100%')
80            .height('100%')
81        }
82        .width('100%')
83
84        Column() {
85          Column()
86            .backgroundColor($r('sys.color.ohos_id_color_list_separator'))
87            .height(24)
88            .width(1)
89        }
90        .margin({ left: 2, right: 2, top: 8 })
91        .justifyContent(FlexAlign.Center)
92
93        Column() {
94          Button($r('app.string.avscreencapture_privacy_window_agree'), { type: ButtonType.Capsule })
95            .onClick(() => {
96              this.controller.close();
97              if (this.confirm) {
98                this.confirm();
99              }
100            })
101            .backgroundColor(Color.Transparent)
102            .fontColor($r('sys.color.ohos_id_color_text_primary_activated'))
103            .fontSize($r('sys.float.ohos_id_text_size_button1'))
104            .width('100%')
105            .height('100%')
106        }
107        .width('100%')
108      }
109      .height(40)
110      .margin({ top: 8, bottom: 16, left: 16, right: 16 })
111    }
112  }
113}
114
115const MARGIN_DISTANCE = 24;
116const BORDER_DISTANCE = 16;
117
118@Entry
119@Component
120export struct DialogPage {
121  private want = globalThis.dialogWant as Want;
122  private session = globalThis.dialogSession as UIExtensionContentSession;
123  private appLabel: string = '';
124  private overflow: boolean = false;
125
126  dialogController: CustomDialogController = new CustomDialogController({
127    builder: PrivacyWindowDialog({
128      cancel: () => { this.onCancel() },
129      confirm: () => { this.onConfirm() },
130      appLabel: this.appLabel,
131      overflow: this.overflow
132    }),
133    alignment: DialogAlignment.Center,
134    autoCancel: false,
135    backgroundColor: Color.White
136  })
137
138  getTextLength(text: Resource, fontSize: Resource): void {
139    let textLength: number = measure.measureText({
140      textContent: text,
141      fontSize: fontSize
142    });
143    let windowWidth = display.getDefaultDisplaySync().width;
144    let textMaxLength = px2vp(windowWidth) - BORDER_DISTANCE - MARGIN_DISTANCE;
145    if (px2vp(textLength) > textMaxLength) {
146      Log.showInfo(TAG, 'title overflow, need change line');
147      this.overflow = true;
148    } else {
149      this.overflow = false;
150    }
151  }
152
153  async onCancel(): Promise<void> {
154    Log.showInfo(TAG, 'Cancel is clicked');
155    globalThis.userChoice = 'false';
156    await this.session.terminateSelf();
157  }
158
159  async onConfirm(): Promise<void> {
160    Log.showInfo(TAG, 'Agree is clicked');
161    globalThis.userChoice = 'true';
162    await this.session.terminateSelf();
163  }
164
165  async onPageShow(): Promise<void> {
166    this.getTextLength($r('app.string.avscreencapture_privacy_window_title'),
167      $r('sys.float.ohos_id_text_size_headline8'));
168    this.getAppName();
169    Log.showInfo(TAG, 'onPageShow');
170    this.dialogController.open();
171  }
172
173  onPageHide(): void {
174    Log.showInfo(TAG, 'onPageHide')
175  }
176
177  getAppName(): void {
178    if (this.want.parameters?.callingLabel) {
179      this.appLabel = (this.want.parameters?.appLabel).toString();
180      Log.showInfo(TAG, `appLabel: ${this.appLabel}`);
181    }
182  }
183
184  build() {}
185}