1/*
2 * Copyright (c) 2023-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 router from '@ohos.router';
17import CheckEmptyUtils, { Constants, Log } from '@ohos/common';
18import common from '@ohos.app.ability.common';
19import { GlobalThisHelper, GlobalThisStorageKey} from '@ohos/common';
20import { PreferencesKey} from '@ohos/common';
21import PreferencesAdapter from '../Common/Adapter/PreferencesAdapter';
22import {PrivacyStatementDialog} from './PrivacyStatementDialog';
23import {CancelButton} from './component/BaseComponent';
24import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
25
26const TAG = 'PrivacyStatementPage';
27
28let storage = LocalStorage.getShared();
29@Entry(storage)
30@Component
31struct PrivacyStatementPage {
32  private readonly PRIVACY_STATEMENT_STORE: string = 'privacyStatementStore';
33  private abilityContext: common.UIExtensionContext | undefined = undefined;
34  private session?: UIExtensionContentSession = undefined;
35  private dialogController: CustomDialogController = new CustomDialogController({
36    builder: PrivacyStatementDialog(),
37    autoCancel: false,
38    customStyle: true,
39  })
40
41  aboutToAppear() {
42    this.abilityContext = GlobalThisHelper.getValue<common.UIExtensionContext>(
43      GlobalThisStorageKey.KEY_MAIN_ABILITY_CONTEXT);
44    this.session = storage.get<UIExtensionContentSession>(Constants.SESSION);
45  }
46
47  build() {
48    Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
49      Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) {
50        Column() {
51          Image($r('app.media.logo'))
52            .width($r('app.float.privacy_statement_print_log_width_height'))
53            .height($r("app.float.privacy_statement_print_log_width_height"))
54            .margin({top: $r('app.float.print_log_margin_top')})
55          Text($r('app.string.welcome'))
56            .margin({ top: $r('app.float.privacy_statement_text_relative_img_margin_top')})
57            .fontColor($r('sys.color.ohos_id_color_text_tertiary'))
58            .fontSize($r('sys.float.ohos_id_text_size_over_line'))
59            .fontWeight(FontWeight.Regular)
60            .height($r('app.float.privacy_statement_text_tertiary_height'))
61          Text($r('app.string.MainAbility_label'))
62            .fontSize($r('sys.float.ohos_id_text_size_headline7'))
63            .fontColor($r('sys.color.ohos_id_color_text_primary'))
64            .fontWeight(FontWeight.Bolder)
65            .height($r('app.float.privacy_statement_text_headline_height'))
66            .margin({top: $r('app.float.privacy_statement_text_relative_text_margin_top')})
67          Text($r('app.string.print_slogan'))
68            .fontColor($r('sys.color.ohos_id_color_text_tertiary'))
69            .fontSize($r('sys.float.ohos_id_text_size_over_line'))
70            .fontWeight(FontWeight.Regular)
71            .margin({top: $r('app.float.privacy_statement_text_relative_text_margin_top')})
72            .height($r('app.float.privacy_statement_text_tertiary_height'))
73        }
74
75        Column() {
76          Image($r('app.media.ic_public_shield'))
77            .width($r('app.float.shield_width_height'))
78            .height($r('app.float.shield_width_height'))
79          Text() {
80            ForEach(this.getPrivacyStatementText(), (privacyStatementText: string) => {
81              if (privacyStatementText === this.abilityContext?.resourceManager.getStringByNameSync('location_information') ||
82                privacyStatementText === this.abilityContext?.resourceManager.getStringByNameSync('print_permission_network')) {
83                Span(privacyStatementText).fontWeight(FontWeight.Bolder)
84              } else {
85                Span(privacyStatementText)
86              }
87            })
88            Span($r('app.string.about_privacy_statement_text'))
89              .onClick(() => {
90                router.replaceUrl({
91                  url: 'pages/PrivacyStatementWebPage',
92                  params: {
93                    info: true
94                  }
95                });
96              })
97              .fontColor($r('sys.color.ohos_id_color_text_hyperlink'))
98              .fontSize($r('sys.float.ohos_id_text_size_body3'))
99            Span('、')
100            Span($r('app.string.permissions_notice'))
101              .fontColor($r('sys.color.ohos_id_color_text_hyperlink'))
102              .fontSize($r('sys.float.ohos_id_text_size_body3'))
103              .onClick(() => {
104                this.dialogController.open();
105              })
106            Span('。')
107          }
108          .fontColor($r('sys.color.ohos_id_color_text_primary'))
109          .fontSize($r('sys.float.ohos_id_text_size_body3'))
110          .margin({
111            top: $r('app.float.privacy_statement_text_relative_img_margin_top'),
112            left: $r('app.float.privacy_statement_text_margin_left_right'),
113            right: $r('app.float.privacy_statement_text_margin_left_right')
114          })
115          Row() {
116            CancelButton({
117              cancelLabel: $r('app.string.Cancel'),
118              cancelWidth: $r('app.float.privacy_statement_button_width'),
119              cancelHeight: $r('app.float.privacy_statement_button_height'),
120              cancelClick: () => {
121                this.session?.terminateSelf();
122              }
123            })
124            .margin({right: $r('app.float.privacy_statement_button_space')})
125            Button($r('app.string.Agree'))
126              .onClick(() => {
127                this.agreePrivacyStatement();
128                router.replaceUrl({url: 'pages/PrintPage', params: storage});
129              })
130              .width($r('app.float.privacy_statement_button_width'))
131              .height($r('app.float.privacy_statement_button_height'))
132          }
133          .margin({
134              top: $r('app.float.privacy_statement_button_to_text_margin_top'),
135              bottom: $r('app.float.privacy_statement_button_margin_bottom'),
136          })
137        }
138      }
139    }
140    .width('100%')
141    .height('100%')
142  }
143
144  async agreePrivacyStatement() {
145    Log.info(TAG, "agreePrivacyStatement");
146    PreferencesAdapter.getInstance().getOrCreatePreferencesSync(this.PRIVACY_STATEMENT_STORE).then((successGet) => {
147      Log.info(TAG, 'agreePrivacyStatement getOrCreatePreferencesSync successGet: ' + successGet);
148      if (successGet) {
149        PreferencesAdapter.getInstance().putValue(PreferencesKey.KEY_PRIVACY_STATEMENT_PREFERENCES, true).then((successPut) => {
150          Log.info(TAG, 'agreePrivacyStatement putValue successPut: ' + successPut);
151          if (successPut) {
152            PreferencesAdapter.getInstance().flush();
153          }
154        })
155      }
156    });
157  }
158
159  private getPrivacyStatementText() : Array<string> {
160    let privacyStatementTextList = new Array<string>();
161    if (CheckEmptyUtils.isEmpty(this.abilityContext)) {
162      Log.warn(TAG, 'getPrivacyStatementText abilityContext is invalid.');
163      return privacyStatementTextList;
164    }
165    let privacyStatementText = this.abilityContext!.resourceManager.getStringByNameSync('privacy_statement_text');
166    let printPermissionNetwork = this.abilityContext!.resourceManager.getStringByNameSync('print_permission_network');
167    let locationInformation = this.abilityContext!.resourceManager.getStringByNameSync('location_information');
168
169    let privacyStatementTexts = privacyStatementText.split(printPermissionNetwork);
170    privacyStatementTextList.push(privacyStatementTexts[0]);
171    privacyStatementTextList.push(printPermissionNetwork);
172    privacyStatementTexts = privacyStatementTexts[1].split(locationInformation);
173    privacyStatementTextList.push(privacyStatementTexts[0]);
174    privacyStatementTextList.push(locationInformation);
175    privacyStatementTextList.push(privacyStatementTexts[1]);
176    return privacyStatementTextList;
177  }
178}