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