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 window from '@ohos.window'; 18import { Log } from '@ohos/common'; 19import { WindowErrorCode } from '@ohos/common'; 20import router from '@ohos.router'; 21import { GlobalThisHelper, GlobalThisStorageKey} from '@ohos/common'; 22import { AppStorageKeyName} from '@ohos/common'; 23import common from '@ohos.app.ability.common'; 24import AppStorageHelper from '../Common/Adapter/AppStorageHelper'; 25import {CancelButton, ClickableImage} from './component/BaseComponent'; 26 27const TAG = 'PrivacyStatementWebPage'; 28 29@Entry 30@Component 31struct PrivacyStatementWebPage { 32 @State @Watch('languageChange') language: string = AppStorageHelper.getValue(AppStorageKeyName.CONFIG_LANGUAGE); 33 private abilityContext: common.UIAbilityContext = GlobalThisHelper 34 .getValue<common.UIAbilityContext>(GlobalThisStorageKey.KEY_MAIN_ABILITY_CONTEXT); 35 private webController: webview.WebviewController = new webview.WebviewController(); 36 private privacyStatementFlag: boolean = false; 37 private baseUrl: string; 38 private url: string; 39 40 aboutToAppear() { 41 const params = router.getParams(); 42 const hideCancel = params['info']; 43 if (hideCancel) { 44 this.privacyStatementFlag = hideCancel; 45 } 46 Log.info(TAG, 'privacyStatementFlag: ' + this.privacyStatementFlag); 47 this.languageToWebUrl(); 48 this.setLanguageChangeCallback(); 49 } 50 51 build() { 52 Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center, direction: FlexDirection.Column }) { 53 Column() { 54 ClickableImage({ 55 imageSrc: $r('app.media.ic_back'), 56 imageHeight: $r('app.float.privacy_statement_text_headline_height'), 57 imageWidth: $r('app.float.privacy_statement_text_headline_height'), 58 setting: {hoverAble: true}, 59 clickEvent: () => { 60 this.onBack(); 61 } 62 }).margin({left: $r('app.float.shield_width_height')}) 63 } 64 .margin({top: $r('app.float.privacy_statement_text_headline_height')}) 65 .alignItems(HorizontalAlign.Start) 66 .width('100%') 67 .height($r('app.float.privacy_statement_text_margin_left_right')) 68 Column() { 69 Column() { 70 Web({src: this.url, controller: this.webController}) 71 .width('100%') 72 .height('100%') 73 } 74 .height(600) 75 .width('100%') 76 if (!this.privacyStatementFlag) { 77 Column() { 78 CancelButton({ 79 cancelLabel: $r('app.string.Cancel'), 80 cancelWidth: $r('app.float.about_button_width'), 81 cancelHeight: $r('app.float.privacy_statement_button_height'), 82 cancelClick: () => { 83 router.back({ url: 'pages/PrintPage' }); 84 } 85 }) 86 .margin({ 87 top: $r('app.float.privacy_statement_button_to_text_margin_top') 88 }) 89 } 90 .width('100%') 91 .alignItems(HorizontalAlign.Center) 92 } 93 } 94 .margin({ 95 bottom: this.privacyStatementFlag ? 0 96 : $r('app.float.privacy_statement_button_margin_bottom') 97 }) 98 .width('100%') 99 .height('100%') 100 } 101 } 102 103 104 private onBack() { 105 try { 106 let result = this.webController.accessBackward(); 107 if (result) { 108 this.webController.backward(); 109 } else { 110 let page = router.getState(); 111 Log.info(TAG, 'page index = ' + page.index); 112 Log.info(TAG, 'page name = ' + page.name); 113 Log.info(TAG, 'page path = ' + page.path); 114 Log.info(TAG, "onBack parseInt(router.getLength()): " + parseInt(router.getLength())); 115 if (parseInt(router.getLength()) > 1) { 116 router.back({url: 'pages/AboutPage'}) 117 } else { 118 router.replaceUrl({ url: 'pages/PrivacyStatementPage' }) 119 } 120 } 121 } catch (error) { 122 console.error(`Errorcode: ${error.code}, Message: ${error.message}`); 123 } 124 } 125 126 private closeWindow() { 127 try { 128 var windowClass = window.findWindow('PrivacyStatementWebPage'); 129 } catch (exception) { 130 Log.info(TAG, 'findWindow exception: ' + JSON.stringify(exception)); 131 if (WindowErrorCode.WINDOW_STATE_ABNORMAL === exception.code) { 132 windowClass = window.findWindow('AboutPage'); 133 } 134 } 135 if (windowClass) { 136 windowClass.destroyWindow(() => { 137 Log.info(TAG, 'Succeeded in destroying the window'); 138 }) 139 } 140 } 141 142 private languageChange():void { 143 Log.info(TAG, 'languageChange language: ' + this.language); 144 this.languageToWebUrl(); 145 Log.info(TAG, 'languageChange url: ' + this.url); 146 this.webController.loadUrl(this.url); 147 Log.info(TAG, 'web url: ' + this.webController.getUrl()); 148 } 149 150 private languageToWebUrl() { 151 let languageCode = 'en-US'; 152 if (this.language.substr(0, 2) === 'zh') { 153 languageCode = 'zh-CN'; 154 } 155 this.baseUrl = this.abilityContext.resourceManager.getStringByNameSync('privacy_statement_web_base_url'); 156 Log.info(TAG, "languageToWebUrl baseUrl: " + this.baseUrl); 157 this.url = this.baseUrl + languageCode; 158 } 159 160 private setLanguageChangeCallback() { 161 let envCallback = { 162 bindThis(that) { 163 this.that = that; 164 }, 165 onConfigurationUpdated(config) { 166 this.that.language = config.language.substr(0, 2); 167 Log.info(TAG, 'onConfigurationUpdated language: ' + JSON.stringify(this.that.language)); 168 }, 169 onMemoryLevel() {} 170 }; 171 envCallback.bindThis(this); 172 try { 173 if (this.abilityContext) { 174 let applicationContext = this.abilityContext.getApplicationContext(); 175 applicationContext.on('environment', envCallback); 176 } 177 } catch (paramError) { 178 console.error('error: ${paramError.code}, ${paramError.message}'); 179 } 180 } 181}