1c36cf2e9Sopenharmony_ci/*
2c36cf2e9Sopenharmony_ci * Copyright (c) 2023-2023 Huawei Device Co., Ltd.
3c36cf2e9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c36cf2e9Sopenharmony_ci * you may not use this file except in compliance with the License.
5c36cf2e9Sopenharmony_ci * You may obtain a copy of the License at
6c36cf2e9Sopenharmony_ci *
7c36cf2e9Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8c36cf2e9Sopenharmony_ci *
9c36cf2e9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c36cf2e9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c36cf2e9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c36cf2e9Sopenharmony_ci * See the License for the specific language governing permissions and
13c36cf2e9Sopenharmony_ci * limitations under the License.
14c36cf2e9Sopenharmony_ci */
15c36cf2e9Sopenharmony_ci
16c36cf2e9Sopenharmony_ciimport webview from '@ohos.web.webview';
17c36cf2e9Sopenharmony_ciimport CheckEmptyUtils, { configMgr, Constants, Log } from '@ohos/common';
18c36cf2e9Sopenharmony_ciimport router from '@ohos.router';
19c36cf2e9Sopenharmony_ciimport { GlobalThisHelper, GlobalThisStorageKey} from '@ohos/common';
20c36cf2e9Sopenharmony_ciimport common from '@ohos.app.ability.common';
21c36cf2e9Sopenharmony_ciimport {CancelButton, ClickableImage} from './component/BaseComponent';
22c36cf2e9Sopenharmony_ciimport { Configuration } from '@ohos.app.ability.Configuration';
23c36cf2e9Sopenharmony_ci
24c36cf2e9Sopenharmony_ciconst TAG = 'PrivacyStatementWebPage';
25c36cf2e9Sopenharmony_ci
26c36cf2e9Sopenharmony_ci@Entry
27c36cf2e9Sopenharmony_ci@Component
28c36cf2e9Sopenharmony_cistruct PrivacyStatementWebPage {
29c36cf2e9Sopenharmony_ci  @State @Watch('languageChange') language: string = configMgr.getConfiguration().language ?? '';
30c36cf2e9Sopenharmony_ci  private abilityContext: common.UIExtensionContext = GlobalThisHelper
31c36cf2e9Sopenharmony_ci    .getValue<common.UIExtensionContext>(GlobalThisStorageKey.KEY_MAIN_ABILITY_CONTEXT);
32c36cf2e9Sopenharmony_ci  private webController: webview.WebviewController = new webview.WebviewController();
33c36cf2e9Sopenharmony_ci  private privacyStatementFlag: boolean = false;
34c36cf2e9Sopenharmony_ci  private baseUrl: string = Constants.STRING_NONE;
35c36cf2e9Sopenharmony_ci  private url: string = Constants.STRING_NONE;
36c36cf2e9Sopenharmony_ci
37c36cf2e9Sopenharmony_ci  aboutToAppear() {
38c36cf2e9Sopenharmony_ci    const hideCancel = router.getParams() as boolean;
39c36cf2e9Sopenharmony_ci    if (hideCancel) {
40c36cf2e9Sopenharmony_ci      this.privacyStatementFlag = hideCancel;
41c36cf2e9Sopenharmony_ci    }
42c36cf2e9Sopenharmony_ci    Log.info(TAG, 'privacyStatementFlag: ' + this.privacyStatementFlag);
43c36cf2e9Sopenharmony_ci    this.languageToWebUrl();
44c36cf2e9Sopenharmony_ci    configMgr.registerConfigManager(TAG, this);
45c36cf2e9Sopenharmony_ci  }
46c36cf2e9Sopenharmony_ci
47c36cf2e9Sopenharmony_ci  aboutToDisappear() {
48c36cf2e9Sopenharmony_ci    configMgr.unregisterConfigManager(TAG);
49c36cf2e9Sopenharmony_ci  }
50c36cf2e9Sopenharmony_ci
51c36cf2e9Sopenharmony_ci  notifyConfigurationChanged(config: Configuration): void {
52c36cf2e9Sopenharmony_ci    this.language = config.language?.substr(0, 2) ?? '';
53c36cf2e9Sopenharmony_ci    Log.info(TAG, 'notifyConfigurationChanged language: ' + JSON.stringify((this.language)));
54c36cf2e9Sopenharmony_ci  }
55c36cf2e9Sopenharmony_ci
56c36cf2e9Sopenharmony_ci  build() {
57c36cf2e9Sopenharmony_ci    Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center, direction: FlexDirection.Column }) {
58c36cf2e9Sopenharmony_ci      Column() {
59c36cf2e9Sopenharmony_ci        ClickableImage({
60c36cf2e9Sopenharmony_ci          imageSrc: $r('app.media.ic_back'),
61c36cf2e9Sopenharmony_ci          imageHeight: $r('app.float.privacy_statement_text_headline_height'),
62c36cf2e9Sopenharmony_ci          imageWidth: $r('app.float.privacy_statement_text_headline_height'),
63c36cf2e9Sopenharmony_ci          isHoverEnable: true,
64c36cf2e9Sopenharmony_ci          clickEvent: () => {
65c36cf2e9Sopenharmony_ci            this.onBack();
66c36cf2e9Sopenharmony_ci          }
67c36cf2e9Sopenharmony_ci        }).margin({left: $r('app.float.shield_width_height')})
68c36cf2e9Sopenharmony_ci      }
69c36cf2e9Sopenharmony_ci      .margin({top: $r('app.float.privacy_statement_text_headline_height')})
70c36cf2e9Sopenharmony_ci      .alignItems(HorizontalAlign.Start)
71c36cf2e9Sopenharmony_ci      .width('100%')
72c36cf2e9Sopenharmony_ci      .height($r('app.float.privacy_statement_text_margin_left_right'))
73c36cf2e9Sopenharmony_ci      Column() {
74c36cf2e9Sopenharmony_ci        Column() {
75c36cf2e9Sopenharmony_ci          Web({src: this.url, controller: this.webController})
76c36cf2e9Sopenharmony_ci            .width('100%')
77c36cf2e9Sopenharmony_ci            .height('100%')
78c36cf2e9Sopenharmony_ci        }
79c36cf2e9Sopenharmony_ci        .height(600)
80c36cf2e9Sopenharmony_ci        .width('100%')
81c36cf2e9Sopenharmony_ci        if (!this.privacyStatementFlag) {
82c36cf2e9Sopenharmony_ci          Column() {
83c36cf2e9Sopenharmony_ci            CancelButton({
84c36cf2e9Sopenharmony_ci              cancelLabel: $r('app.string.Cancel'),
85c36cf2e9Sopenharmony_ci              cancelWidth: $r('app.float.about_button_width'),
86c36cf2e9Sopenharmony_ci              cancelHeight: $r('app.float.privacy_statement_button_height'),
87c36cf2e9Sopenharmony_ci              cancelClick: () => {
88c36cf2e9Sopenharmony_ci                router.back({ url: 'pages/PrintPage' });
89c36cf2e9Sopenharmony_ci              }
90c36cf2e9Sopenharmony_ci            })
91c36cf2e9Sopenharmony_ci            .margin({
92c36cf2e9Sopenharmony_ci              top: $r('app.float.privacy_statement_button_to_text_margin_top')
93c36cf2e9Sopenharmony_ci            })
94c36cf2e9Sopenharmony_ci          }
95c36cf2e9Sopenharmony_ci          .width('100%')
96c36cf2e9Sopenharmony_ci          .alignItems(HorizontalAlign.Center)
97c36cf2e9Sopenharmony_ci        }
98c36cf2e9Sopenharmony_ci      }
99c36cf2e9Sopenharmony_ci      .margin({
100c36cf2e9Sopenharmony_ci        bottom: this.privacyStatementFlag ? 0
101c36cf2e9Sopenharmony_ci                                          : $r('app.float.privacy_statement_button_margin_bottom')
102c36cf2e9Sopenharmony_ci      })
103c36cf2e9Sopenharmony_ci      .width('100%')
104c36cf2e9Sopenharmony_ci      .height('100%')
105c36cf2e9Sopenharmony_ci    }
106c36cf2e9Sopenharmony_ci  }
107c36cf2e9Sopenharmony_ci
108c36cf2e9Sopenharmony_ci
109c36cf2e9Sopenharmony_ci  private onBack() {
110c36cf2e9Sopenharmony_ci    try {
111c36cf2e9Sopenharmony_ci      let result = this.webController.accessBackward();
112c36cf2e9Sopenharmony_ci      if (result) {
113c36cf2e9Sopenharmony_ci        this.webController.backward();
114c36cf2e9Sopenharmony_ci      } else {
115c36cf2e9Sopenharmony_ci        let page = router.getState();
116c36cf2e9Sopenharmony_ci        Log.info(TAG, 'page index = ' + page.index);
117c36cf2e9Sopenharmony_ci        Log.info(TAG, 'page name = ' + page.name);
118c36cf2e9Sopenharmony_ci        Log.info(TAG, 'page path = ' + page.path);
119c36cf2e9Sopenharmony_ci        Log.info(TAG, "onBack parseInt(router.getLength()): " + parseInt(router.getLength()));
120c36cf2e9Sopenharmony_ci        if (parseInt(router.getLength()) > 1) {
121c36cf2e9Sopenharmony_ci          router.back({url: 'pages/AboutPage'})
122c36cf2e9Sopenharmony_ci        } else {
123c36cf2e9Sopenharmony_ci          router.replaceUrl({ url: 'pages/PrivacyStatementPage' })
124c36cf2e9Sopenharmony_ci        }
125c36cf2e9Sopenharmony_ci      }
126c36cf2e9Sopenharmony_ci    } catch (error) {
127c36cf2e9Sopenharmony_ci      console.error(`Errorcode: ${error.code}, Message: ${error.message}`);
128c36cf2e9Sopenharmony_ci    }
129c36cf2e9Sopenharmony_ci  }
130c36cf2e9Sopenharmony_ci
131c36cf2e9Sopenharmony_ci  private languageChange():void {
132c36cf2e9Sopenharmony_ci    Log.info(TAG, 'languageChange language: ' + this.language);
133c36cf2e9Sopenharmony_ci    this.languageToWebUrl();
134c36cf2e9Sopenharmony_ci    Log.info(TAG, 'languageChange url: ' + this.url);
135c36cf2e9Sopenharmony_ci    this.webController.loadUrl(this.url);
136c36cf2e9Sopenharmony_ci    Log.info(TAG, 'web url: ' + this.webController.getUrl());
137c36cf2e9Sopenharmony_ci  }
138c36cf2e9Sopenharmony_ci
139c36cf2e9Sopenharmony_ci  private languageToWebUrl() {
140c36cf2e9Sopenharmony_ci    let languageCode = 'en-US';
141c36cf2e9Sopenharmony_ci    if (CheckEmptyUtils.checkStrIsEmpty(this.language) || this.language.substr(0, 2) === 'zh') {
142c36cf2e9Sopenharmony_ci      languageCode = 'zh-CN';
143c36cf2e9Sopenharmony_ci    }
144c36cf2e9Sopenharmony_ci    this.baseUrl = this.abilityContext.resourceManager.getStringByNameSync('privacy_statement_web_base_url');
145c36cf2e9Sopenharmony_ci    Log.info(TAG, "languageToWebUrl baseUrl: " + this.baseUrl);
146c36cf2e9Sopenharmony_ci    this.url = this.baseUrl + languageCode;
147c36cf2e9Sopenharmony_ci  }
148c36cf2e9Sopenharmony_ci}