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 */
15
16import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
17import ability from '@ohos.ability.ability';
18import Constants from '../common/constant';
19import GlobalContext from '../common/GlobalContext';
20import { directionStatus } from '../common/utils';
21import common from '@ohos.app.ability.common';
22import { HiLog } from '../common/HiLog';
23
24const TAG = 'PermStat';
25
26let abilityResult: ability.AbilityResult = {
27  'resultCode': 0,
28  'want': {}
29};
30
31class StatusContent {
32  public label: Resource | undefined = undefined
33  public value: boolean = false
34  public date?: boolean
35}
36
37class TitleContent {
38  public title: Resource | undefined = undefined
39  public content: StatusContent[] = []
40}
41
42let storage = LocalStorage.getShared();
43@Entry(storage)
44@Component
45struct PermissionStatus {
46  @State session: UIExtensionContentSession | undefined = storage === undefined ? undefined : storage.get<UIExtensionContentSession>('session');
47  @State directionStatus: number = 0;
48  @State authPerm: number = Constants.PP_IMAGE_PAD;
49  @State authPermFlag: boolean = false;
50  @State accountId: string = '';
51  @State validity: number = 0;
52  private array1: TitleContent[] = [
53    {
54      title: ($r('app.string.perm_list_title_read_only')),
55      content: [
56        {
57          label: ($r('app.string.perm_list_title_read_only')), value: true
58        }
59      ]
60    },
61    {
62      title: ($r('app.string.header_title_edit')),
63      content: [
64        {
65          label: ($r('app.string.save')), value: false
66        },
67        {
68          label: ($r('app.string.save_as')), value: false
69        },
70        {
71          label: ($r('app.string.header_title_edit_content')), value: false
72        },
73        {
74          label: ($r('app.string.screenshot')), value: false
75        },
76        {
77          label: ($r('app.string.share_the_screen')), value: false
78        },
79        {
80          label: ($r('app.string.screen_recording')), value: false
81        },
82        {
83          label: ($r('app.string.duplicate')), value: false
84        },
85        {
86          label: ($r('app.string.print')), value: false
87        },
88      ]
89    },
90    {
91      title: ($r('app.string.Document_valid')),
92      content: [
93        {
94          label: ($r('app.string.Document_valid_until')), value: false, date: true
95        }
96      ]
97    }
98  ];
99  private array2: TitleContent[] = [
100    {
101      title: ($r('app.string.perm_list_title_read_only')),
102      content: [
103        {
104          label: ($r('app.string.perm_list_title_read_only')), value: true
105        }
106      ]
107    },
108    {
109      title: ($r('app.string.header_title_edit')),
110      content: [
111        {
112          label: ($r('app.string.save')), value: true
113        },
114        {
115          label: ($r('app.string.save_as')), value: true
116        },
117        {
118          label: ($r('app.string.header_title_edit_content')), value: true
119        },
120        {
121          label: ($r('app.string.screenshot')), value: true
122        },
123        {
124          label: ($r('app.string.share_the_screen')), value: true
125        },
126        {
127          label: ($r('app.string.screen_recording')), value: true
128        },
129        {
130          label: ($r('app.string.duplicate')), value: true
131        },
132        {
133          label: ($r('app.string.print')), value: true
134        },
135      ]
136    },
137    {
138      title: ($r('app.string.Document_valid')),
139      content: [
140        {
141          label: ($r('app.string.Document_valid_until')), value: false, date: true
142        }
143      ]
144    }
145  ];
146
147  aboutToAppear() {
148    HiLog.info(TAG, `aboutToAppear`);
149    this.authPerm = AppStorage.get('authPerm') ?? 1;
150    this.accountId = AppStorage.get('contactAccount') ?? '';
151    this.validity = AppStorage.get('validity') ?? 0;
152    this.directionStatus = (getContext(this) as common.UIAbilityContext).config.direction ?? -1;
153    directionStatus((counter) => {
154      this.directionStatus = counter;
155    })
156  }
157
158  getTime(validity: number) {
159    if (validity === 0) {
160      return $r('app.string.permanently');
161    }
162    let date: Date = new Date(validity);
163    let year = date.getFullYear();
164    let month = date.getMonth() + 1;
165    let day = date.getDate();
166    let hour = date.getHours();
167    let minute = date.getMinutes();
168    return `${year}/${month}/${day} ${hour}:${minute}`;
169  }
170
171  @Builder
172  itemHead(text?: string | Resource) {
173    Text(text)
174      .fontSize($r('sys.float.ohos_id_text_size_sub_title3'))
175      .fontWeight(FontWeight.Medium)
176      .fontColor($r('sys.color.ohos_id_color_text_secondary'))
177      .width(Constants.HEADER_COLUMN_WIDTH)
178      .constraintSize({ minHeight: Constants.SUB_HEADER_HEIGHT })
179  }
180
181  build() {
182    GridRow({
183      columns: {
184        xs: Constants.XS_COLUMNS,
185        sm: Constants.SM_COLUMNS,
186        md: Constants.MD_COLUMNS,
187        lg: Constants.LG_COLUMNS
188      },
189      gutter: Constants.DIALOG_GUTTER
190    }) {
191      GridCol({
192        span: {
193          xs: Constants.XS_SPAN,
194          sm: Constants.SM_SPAN,
195          md: Constants.SM_SPAN,
196          lg: Constants.SM_SPAN
197        },
198        offset: {
199          xs: Constants.XS_OFFSET,
200          sm: Constants.SM_OFFSET,
201          md: Constants.LG_OFFSET,
202          lg: Constants.SM_SPAN
203        }
204      }) {
205        Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center,
206          direction: FlexDirection.Column }) {
207          Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
208            Column() {
209              Row() {
210                Text($r('app.string.header_title'))
211                  .fontWeight(FontWeight.Bold)
212                  .fontColor($r('sys.color.ohos_id_color_text_primary'))
213                  .fontSize($r('sys.float.ohos_id_text_size_dialog_tittle'))
214                  .width(Constants.HEADER_TEXT_WIDTH)
215                  .align(Alignment.Start)
216              }
217              .width(Constants.HEADER_COLUMN_WIDTH)
218              .height(Constants.HEADER_COLUMN_HEIGHT)
219              .padding({
220                left: Constants.HEADER_COLUMN_PADDING_LEFT,
221                right: Constants.HEADER_COLUMN_PADDING_RIGHT
222              })
223              .margin({ bottom: Constants.HEADER_COLUMN_MARGIN_BOTTOM });
224
225              Scroll() {
226                Column() {
227                  Row() {
228                    Text(this.authPerm === Constants.FOOTER_OPACITY_ONE ?
229                      $r('app.string.permission_status_readOnly') : $r('app.string.permission_status_title'))
230                      .fontWeight(FontWeight.Regular)
231                      .fontColor($r('sys.color.ohos_id_color_text_secondary'))
232                      .fontSize($r('sys.float.ohos_id_text_size_body1'))
233                      .width(Constants.HEADER_TEXT_WIDTH)
234                      .align(Alignment.Start)
235                  }
236                  .width(Constants.HEADER_COLUMN_WIDTH)
237                  .margin({ bottom: Constants.DU_LINE_MARGIN_TOP2 })
238
239                  List() {
240                    ForEach(this.authPerm === Constants.FOOTER_OPACITY_ONE ?
241                      this.array1 : this.array2, (item: TitleContent) => {
242                      ListItemGroup({ header: this.itemHead(item.title) }) {
243                        ForEach(item.content, (item: StatusContent) => {
244                          ListItem() {
245                            Column() {
246                              Row() {
247                                Text(item.label)
248                                  .constraintSize({
249                                    minHeight: Constants.SUB_HEADER_HEIGHT,
250                                    maxWidth: Constants.SUB_HEADER_WIDTH
251                                  })
252                                  .fontSize($r('sys.float.ohos_id_text_size_body1'))
253                                  .textAlign(TextAlign.Start)
254                                  .fontWeight(FontWeight.Medium)
255                                Blank()
256                                if (item.date) {
257                                  Text(this.getTime(this.validity))
258                                    .constraintSize({ minHeight: Constants.SUB_HEADER_HEIGHT })
259                                    .fontSize($r('sys.float.ohos_id_text_size_body2'))
260                                    .textAlign(TextAlign.Start)
261                                    .fontWeight(FontWeight.Regular)
262                                    .opacity(Constants.ENCRYPTION_STATUS_VALUE_COLOR)
263                                } else {
264                                  SymbolGlyph(item.value ? $r('sys.symbol.checkmark') : $r('sys.symbol.multiply'))
265                                    .fontSize(`${Constants.PP_IMAGE_WIDTH}vp`)
266                                    .fontColor([$r('sys.color.ohos_id_color_text_primary')])
267                                    .opacity(Constants.ENCRYPTION_STATUS_VALUE_COLOR)
268                                }
269                              }
270                              .width(Constants.HEADER_TEXT_WIDTH);
271                              Divider()
272                                .width(Constants.HEADER_TEXT_WIDTH)
273                                .height(Constants.FOOTER_OPACITY_ONE)
274                                .color($r('sys.color.comp_divider'));
275                            }
276                          }
277                        }, (item: StatusContent) => JSON.stringify(item))
278                      }
279                    })
280                  }
281
282                  Row() {
283                    Text() {
284                      Span($r('app.string.apply_for_the_subject'))
285                      if (this.authPerm === Constants.FOOTER_OPACITY_ONE) {
286                        Span($r('app.string.apply_for_the_permission', this.accountId))
287                      }
288                    }
289                    .fontColor($r('sys.color.ohos_id_color_text_secondary'))
290                    .fontSize($r('sys.float.ohos_id_text_size_body1'))
291                    .align(Alignment.Start)
292                    .fontWeight(FontWeight.Regular)
293                  }
294                  .width(Constants.HEADER_COLUMN_WIDTH)
295                  .margin({ top: Constants.DA_MARGIN_TOP });
296                }
297              }.constraintSize({
298                maxHeight: this.directionStatus ===
299                  0 ? Constants.CHANGE_MAX_HEIGHT : Constants.ENCRYPTION_SUCCESS_MAX_HEIGHT
300              })
301              .padding({
302                left: Constants.HEADER_COLUMN_PADDING_LEFT,
303                right: Constants.HEADER_COLUMN_PADDING_RIGHT
304              })
305
306              Row() {
307                Button($r('app.string.da_button'), { type: ButtonType.Capsule, stateEffect: true })
308                  .backgroundColor($r('sys.color.ohos_id_color_button_normal'))
309                  .fontColor($r('sys.color.ohos_id_color_text_primary_activated'))
310                  .width(Constants.ENCRYPTION_PERMISSION_STATUS_WIDTH)
311                  .controlSize(ControlSize.NORMAL)
312                  .onClick(() => {
313                    if (this.session !== undefined) {
314                      this.session.terminateSelfWithResult({
315                        'resultCode': 0,
316                        'want': {
317                          'bundleName': Constants.DLP_MANAGER_BUNDLE_NAME,
318                        },
319                      });
320                    } else {
321                      if (GlobalContext.load('fileOpenHistoryFromMain')) {
322                        (GlobalContext.load('fileOpenHistoryFromMain') as Map<string, Object>).delete(GlobalContext.load('uri') as string)
323                      }
324                      abilityResult.resultCode = 0;
325                      (getContext(this) as common.UIAbilityContext).terminateSelfWithResult(abilityResult);
326                    }
327                  })
328              }
329              .justifyContent(FlexAlign.Center)
330              .padding({
331                top: Constants.FOOTER_ROW_PAD_LEFT,
332                bottom: Constants.HEADER_COLUMN_PADDING_BOTTOM,
333                left: Constants.FOOTER_ROW_PAD_LEFT,
334                right: Constants.FOOTER_ROW_PAD_RIGHT
335              })
336            }
337            .width(Constants.ENCRYPTION_PC_FIXING_STATUS_WIDTH)
338            .backgroundColor($r('sys.color.ohos_id_color_dialog_bg'))
339            .borderRadius($r('sys.float.ohos_id_corner_radius_dialog'))
340            .shadow(ShadowStyle.OUTER_DEFAULT_MD)
341            .constraintSize({ minWidth: Constants.ENCRYPTION_PC_FIXING_STATUS_WIDTH })
342            .backgroundBlurStyle(BlurStyle.COMPONENT_ULTRA_THICK);
343          }
344        }
345      }
346    }
347  }
348}
349