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 webview from '@ohos.web.webview';
17import CheckEmptyUtils, { configMgr, Constants, Log } from '@ohos/common';
18import router from '@ohos.router';
19import { GlobalThisHelper, GlobalThisStorageKey} from '@ohos/common';
20import common from '@ohos.app.ability.common';
21import {CancelButton, ClickableImage} from './component/BaseComponent';
22import { Configuration } from '@ohos.app.ability.Configuration';
23
24const TAG = 'PrivacyStatementWebPage';
25
26@Entry
27@Component
28struct PrivacyStatementWebPage {
29  @State @Watch('languageChange') language: string = configMgr.getConfiguration().language ?? '';
30  private abilityContext: common.UIExtensionContext = GlobalThisHelper
31    .getValue<common.UIExtensionContext>(GlobalThisStorageKey.KEY_MAIN_ABILITY_CONTEXT);
32  private webController: webview.WebviewController = new webview.WebviewController();
33  private privacyStatementFlag: boolean = false;
34  private baseUrl: string = Constants.STRING_NONE;
35  private url: string = Constants.STRING_NONE;
36
37  aboutToAppear() {
38    const hideCancel = router.getParams() as boolean;
39    if (hideCancel) {
40      this.privacyStatementFlag = hideCancel;
41    }
42    Log.info(TAG, 'privacyStatementFlag: ' + this.privacyStatementFlag);
43    this.languageToWebUrl();
44    configMgr.registerConfigManager(TAG, this);
45  }
46
47  aboutToDisappear() {
48    configMgr.unregisterConfigManager(TAG);
49  }
50
51  notifyConfigurationChanged(config: Configuration): void {
52    this.language = config.language?.substr(0, 2) ?? '';
53    Log.info(TAG, 'notifyConfigurationChanged language: ' + JSON.stringify((this.language)));
54  }
55
56  build() {
57    Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center, direction: FlexDirection.Column }) {
58      Column() {
59        ClickableImage({
60          imageSrc: $r('app.media.ic_back'),
61          imageHeight: $r('app.float.privacy_statement_text_headline_height'),
62          imageWidth: $r('app.float.privacy_statement_text_headline_height'),
63          isHoverEnable: true,
64          clickEvent: () => {
65            this.onBack();
66          }
67        }).margin({left: $r('app.float.shield_width_height')})
68      }
69      .margin({top: $r('app.float.privacy_statement_text_headline_height')})
70      .alignItems(HorizontalAlign.Start)
71      .width('100%')
72      .height($r('app.float.privacy_statement_text_margin_left_right'))
73      Column() {
74        Column() {
75          Web({src: this.url, controller: this.webController})
76            .width('100%')
77            .height('100%')
78        }
79        .height(600)
80        .width('100%')
81        if (!this.privacyStatementFlag) {
82          Column() {
83            CancelButton({
84              cancelLabel: $r('app.string.Cancel'),
85              cancelWidth: $r('app.float.about_button_width'),
86              cancelHeight: $r('app.float.privacy_statement_button_height'),
87              cancelClick: () => {
88                router.back({ url: 'pages/PrintPage' });
89              }
90            })
91            .margin({
92              top: $r('app.float.privacy_statement_button_to_text_margin_top')
93            })
94          }
95          .width('100%')
96          .alignItems(HorizontalAlign.Center)
97        }
98      }
99      .margin({
100        bottom: this.privacyStatementFlag ? 0
101                                          : $r('app.float.privacy_statement_button_margin_bottom')
102      })
103      .width('100%')
104      .height('100%')
105    }
106  }
107
108
109  private onBack() {
110    try {
111      let result = this.webController.accessBackward();
112      if (result) {
113        this.webController.backward();
114      } else {
115        let page = router.getState();
116        Log.info(TAG, 'page index = ' + page.index);
117        Log.info(TAG, 'page name = ' + page.name);
118        Log.info(TAG, 'page path = ' + page.path);
119        Log.info(TAG, "onBack parseInt(router.getLength()): " + parseInt(router.getLength()));
120        if (parseInt(router.getLength()) > 1) {
121          router.back({url: 'pages/AboutPage'})
122        } else {
123          router.replaceUrl({ url: 'pages/PrivacyStatementPage' })
124        }
125      }
126    } catch (error) {
127      console.error(`Errorcode: ${error.code}, Message: ${error.message}`);
128    }
129  }
130
131  private languageChange():void {
132    Log.info(TAG, 'languageChange language: ' + this.language);
133    this.languageToWebUrl();
134    Log.info(TAG, 'languageChange url: ' + this.url);
135    this.webController.loadUrl(this.url);
136    Log.info(TAG, 'web url: ' + this.webController.getUrl());
137  }
138
139  private languageToWebUrl() {
140    let languageCode = 'en-US';
141    if (CheckEmptyUtils.checkStrIsEmpty(this.language) || this.language.substr(0, 2) === 'zh') {
142      languageCode = 'zh-CN';
143    }
144    this.baseUrl = this.abilityContext.resourceManager.getStringByNameSync('privacy_statement_web_base_url');
145    Log.info(TAG, "languageToWebUrl baseUrl: " + this.baseUrl);
146    this.url = this.baseUrl + languageCode;
147  }
148}