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 UIAbility from '@ohos.app.ability.UIAbility'; 17import { configMgr, GlobalThisHelper, GlobalThisStorageKey, MediaSizeHelper } from '@ohos/common'; 18import AppStorageHelper from '../Common/Adapter/AppStorageHelper'; 19import { Log } from '@ohos/common'; 20import { Constants, AppStorageKeyName, PreferencesKey } from '@ohos/common'; 21import type common from '@ohos.app.ability.common'; 22import PreferencesAdapter from '../Common/Adapter/PreferencesAdapter'; 23import bundleManager from '@ohos.bundle.bundleManager'; 24import PrintAdapter from '../Common/Adapter/PrintAdapter'; 25import { Configuration } from '@ohos.app.ability.Configuration'; 26import image from '@ohos.multimedia.image'; 27import Want from '@ohos.app.ability.Want'; 28import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 29import window from '@ohos.window'; 30 31const TAG = '[MainAbility]:'; 32 33export default class MainAbility extends UIAbility { 34 private readonly PRIVACY_STATEMENT_STORE: string = 'privacyStatementStore'; 35 private storage: LocalStorage = new LocalStorage(); 36 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 37 Log.info(TAG, 'onCreate: ' + JSON.stringify(want) + ' launchParam : ' + JSON.stringify(launchParam)); 38 let jobId = want.parameters[Constants.WANT_JOB_ID_KEY] as string; 39 let fileList = want.parameters[Constants.WANT_FILE_LIST_KEY] as Array<string>; 40 let pkgName = want.parameters[Constants.WANT_PKG_NAME_KEY] as string; 41 Log.info(TAG, 'fileList = ' + JSON.stringify(fileList)); 42 GlobalThisHelper.createValue<common.UIAbilityContext>(this.context, GlobalThisStorageKey.KEY_MAIN_ABILITY_CONTEXT, true); 43 MediaSizeHelper.init(this.context); 44 this.storage.setOrCreate<string>(Constants.WANT_JOB_ID_KEY, jobId); 45 this.storage.setOrCreate<Array<string>>(Constants.WANT_FILE_LIST_KEY, fileList); 46 this.context.resourceManager.getConfiguration((error, value) => { 47 AppStorageHelper.createValue<string>(<string> value.locale, AppStorageKeyName.CONFIG_LANGUAGE); 48 }); 49 AppStorageHelper.createValue<string>(pkgName, AppStorageKeyName.INGRESS_PACKAGE); 50 51 bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT) 52 .then((bundleInfo) => { 53 AppStorageHelper.createValue<string>(<string> bundleInfo.versionName, AppStorageKeyName.APP_VERSION); 54 }); 55 configMgr.onStart(this.context); 56 } 57 58 onDestroy() { 59 Log.info(TAG, 'onDestroy'); 60 } 61 62 onWindowStageCreate(windowStage: window.WindowStage) { 63 // Main window is created, set main page for this ability 64 Log.info(TAG, 'onWindowStageCreate'); 65 66 this.isFirstUsePrint().then((flag) => { 67 windowStage.getMainWindow().then((window) => { 68 window.resetSize(vp2px(Constants.MAIN_WINDOW_WIDTH), vp2px(Constants.MAIN_WINDOW_HEIGHT)); 69 }).catch((err) => { 70 71 }); 72 73 Log.info(TAG, 'onWindowStageCreate flag: ' + JSON.stringify(flag)); 74 if (flag) { 75 windowStage.loadContent('pages/PrivacyStatementPage', this.storage); 76 } else { 77 windowStage.loadContent('pages/PrintPage', this.storage); 78 } 79 }); 80 81 GlobalThisHelper.createValue(windowStage, GlobalThisStorageKey.KEY_MAIN_ABILITY_WINDOW_STAGE, true); 82 } 83 84 onWindowStageDestroy() { 85 // Main window is destroyed, release UI related resources 86 Log.info(TAG, 'onWindowStageDestroy'); 87 let adapter = GlobalThisHelper.getValue<PrintAdapter>(GlobalThisStorageKey.KEY_PRINT_ADAPTER); 88 adapter.getPrinterDiscCtl().stopDiscovery(''); 89 let pixelMap = GlobalThisHelper.getValue<image.PixelMap>(GlobalThisStorageKey.KEY_CURRENT_PIXELMAP); 90 if (pixelMap !== undefined) { 91 pixelMap.release().then(() => { 92 Log.info(TAG, 'onWindowStageDestroy currentPixelMap release success'); 93 }); 94 } 95 this.storage.clear(); 96 configMgr.onStop(); 97 } 98 99 onConfigurationUpdated(config: Configuration): void { 100 Log.info(TAG, 'onConfigurationUpdated, language:' + config.language); 101 AppStorageHelper.createValue<string>(<string> config.language, AppStorageKeyName.CONFIG_LANGUAGE); 102 } 103 104 onForeground() { 105 // Ability has brought to foreground 106 Log.info(TAG, 'onForeground'); 107 } 108 109 onBackground() { 110 // Ability has back to background 111 Log.info(TAG, 'onBackground'); 112 } 113 114 async isFirstUsePrint(): Promise<boolean> { 115 Log.info(TAG, 'isFirstUsePrint start'); 116 const success = await PreferencesAdapter.getInstance().getOrCreatePreferencesSync(this.PRIVACY_STATEMENT_STORE); 117 Log.info(TAG, 'isFirstUsePrint getOrCreatePreferencesSync success: ' + success); 118 if (success) { 119 const agreePrivacyStatement = await PreferencesAdapter.getInstance().getValue(PreferencesKey.KEY_PRIVACY_STATEMENT_PREFERENCES); 120 Log.info(TAG, 'isFirstUsePrint getValue agreePrivacyStatement: ' + agreePrivacyStatement); 121 if (agreePrivacyStatement) { 122 return false; 123 } else { 124 return true; 125 } 126 } else { 127 Log.info(TAG, 'isFirstUsePrint success is not'); 128 return true; 129 } 130 } 131}; 132