1/*
2 * Copyright (c) 2021-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 window from '@ohos.window'
18import AbilityCommonUtil from '../base/utils/AbilityCommonUtil'
19import Logger from '../base/log/Logger'
20import { FilePickerUtil } from '../base/utils/FilePickerUtil'
21import { StartModeOptions } from '../base/model/StartModeOptions'
22import { PickerWindowType } from '../base/constants/FilePickerItems'
23import Want from '@ohos.app.ability.Want'
24import { AbilityConstant } from '@kit.AbilityKit'
25
26const TAG = 'MainAbility'
27
28export default class MainAbility extends UIAbility {
29  private storage: LocalStorage = new LocalStorage();
30  private startModeOptions?: StartModeOptions;
31
32  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
33    Logger.i(TAG, 'onCreate')
34    globalThis.abilityContext = this.context;
35    let options: StartModeOptions = FilePickerUtil.getStartModeOptions(want);
36    options.windowType = PickerWindowType.ABILITY;
37    options.uiContext = this.context;
38    options.context = this.context;
39    options.fileSuffixFilters = AbilityCommonUtil.getKeyFileSuffixFilter(options.fileSuffixFilters);
40    if (options.isOpenFileMode()) {
41      options.fileSuffixFilters = AbilityCommonUtil.getKeyFileSuffixFilter(options.fileSuffixFilters);
42      options.phonePickerType = (want.parameters?.key_pick_type as string) || '';
43      options.phonePickerTypeList = AbilityCommonUtil.getKeyPickTypeList(want.parameters?.key_picker_type as object,
44        want.parameters?.key_picker_type_list as object)
45    }
46    if (options.isCreateFileMode()) {
47      options.PhoneFileSuffixChoices = AbilityCommonUtil.getKeyFileSuffixChoices(options.fileSuffixChoices);
48    }
49    this.startModeOptions = options;
50    this.storage.setOrCreate<StartModeOptions>('startModeOptions', options);
51  }
52
53  onDestroy() {
54    Logger.i(TAG, 'onDestroy')
55    AbilityCommonUtil.releasePhotoManageHelper()
56  }
57
58  onWindowStageCreate(windowStage: window.WindowStage) {
59    // Main window is created, set main page for this ability
60    Logger.i(TAG, 'onWindowStageCreate')
61    AbilityCommonUtil.init().then(() => {
62      globalThis.windowClass = windowStage.getMainWindowSync();
63      if (this.startModeOptions?.isOpenFileMode()) {
64        // 文件选择器
65        windowStage.loadContent('pages/browser/storage/MyPhone', this.storage, (err, data) => {
66          if (err.code) {
67            Logger.e(TAG, 'Failed to load the content: ' + JSON.stringify(err));
68            return
69          }
70          Logger.i(TAG, 'data: ' + JSON.stringify(data));
71        })
72      } else {
73        // 路径选择器
74        windowStage.loadContent('pages/PathPicker', this.storage, (err, data) => {
75          if (err.code) {
76            Logger.e(TAG, 'Failed to load the content. Cause: ' + JSON.stringify(err))
77            return;
78          }
79          Logger.i(TAG, 'Succeeded in loading the content. Data: ' + JSON.stringify(data))
80        })
81      }
82    })
83
84  }
85
86  onWindowStageDestroy() {
87    // Main window is destroyed, release UI related resources
88    Logger.i(TAG, 'onWindowStageDestroy')
89  }
90
91  onForeground() {
92    // Ability has brought to foreground
93    Logger.i(TAG, 'onForeground')
94  }
95
96  onBackground() {
97    // Ability has back to background
98    Logger.i(TAG, 'onBackground')
99  }
100}
101