1bea4f105Sopenharmony_ci/*
2bea4f105Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3bea4f105Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bea4f105Sopenharmony_ci * you may not use this file except in compliance with the License.
5bea4f105Sopenharmony_ci * You may obtain a copy of the License at
6bea4f105Sopenharmony_ci *
7bea4f105Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8bea4f105Sopenharmony_ci *
9bea4f105Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bea4f105Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bea4f105Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bea4f105Sopenharmony_ci * See the License for the specific language governing permissions and
13bea4f105Sopenharmony_ci * limitations under the License.
14bea4f105Sopenharmony_ci */
15bea4f105Sopenharmony_ci
16bea4f105Sopenharmony_ciimport UIAbility from '@ohos.app.ability.UIAbility'
17bea4f105Sopenharmony_ciimport window from '@ohos.window'
18bea4f105Sopenharmony_ciimport AbilityCommonUtil from '../base/utils/AbilityCommonUtil'
19bea4f105Sopenharmony_ciimport Logger from '../base/log/Logger'
20bea4f105Sopenharmony_ciimport { FilePickerUtil } from '../base/utils/FilePickerUtil'
21bea4f105Sopenharmony_ciimport { StartModeOptions } from '../base/model/StartModeOptions'
22bea4f105Sopenharmony_ciimport { PickerWindowType } from '../base/constants/FilePickerItems'
23bea4f105Sopenharmony_ciimport Want from '@ohos.app.ability.Want'
24bea4f105Sopenharmony_ciimport { AbilityConstant } from '@kit.AbilityKit'
25bea4f105Sopenharmony_ci
26bea4f105Sopenharmony_ciconst TAG = 'MainAbility'
27bea4f105Sopenharmony_ci
28bea4f105Sopenharmony_ciexport default class MainAbility extends UIAbility {
29bea4f105Sopenharmony_ci  private storage: LocalStorage = new LocalStorage();
30bea4f105Sopenharmony_ci  private startModeOptions?: StartModeOptions;
31bea4f105Sopenharmony_ci
32bea4f105Sopenharmony_ci  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
33bea4f105Sopenharmony_ci    Logger.i(TAG, 'onCreate')
34bea4f105Sopenharmony_ci    globalThis.abilityContext = this.context;
35bea4f105Sopenharmony_ci    let options: StartModeOptions = FilePickerUtil.getStartModeOptions(want);
36bea4f105Sopenharmony_ci    options.windowType = PickerWindowType.ABILITY;
37bea4f105Sopenharmony_ci    options.uiContext = this.context;
38bea4f105Sopenharmony_ci    options.context = this.context;
39bea4f105Sopenharmony_ci    options.fileSuffixFilters = AbilityCommonUtil.getKeyFileSuffixFilter(options.fileSuffixFilters);
40bea4f105Sopenharmony_ci    if (options.isOpenFileMode()) {
41bea4f105Sopenharmony_ci      options.fileSuffixFilters = AbilityCommonUtil.getKeyFileSuffixFilter(options.fileSuffixFilters);
42bea4f105Sopenharmony_ci      options.phonePickerType = (want.parameters?.key_pick_type as string) || '';
43bea4f105Sopenharmony_ci      options.phonePickerTypeList = AbilityCommonUtil.getKeyPickTypeList(want.parameters?.key_picker_type as object,
44bea4f105Sopenharmony_ci        want.parameters?.key_picker_type_list as object)
45bea4f105Sopenharmony_ci    }
46bea4f105Sopenharmony_ci    if (options.isCreateFileMode()) {
47bea4f105Sopenharmony_ci      options.PhoneFileSuffixChoices = AbilityCommonUtil.getKeyFileSuffixChoices(options.fileSuffixChoices);
48bea4f105Sopenharmony_ci    }
49bea4f105Sopenharmony_ci    this.startModeOptions = options;
50bea4f105Sopenharmony_ci    this.storage.setOrCreate<StartModeOptions>('startModeOptions', options);
51bea4f105Sopenharmony_ci  }
52bea4f105Sopenharmony_ci
53bea4f105Sopenharmony_ci  onDestroy() {
54bea4f105Sopenharmony_ci    Logger.i(TAG, 'onDestroy')
55bea4f105Sopenharmony_ci    AbilityCommonUtil.releasePhotoManageHelper()
56bea4f105Sopenharmony_ci  }
57bea4f105Sopenharmony_ci
58bea4f105Sopenharmony_ci  onWindowStageCreate(windowStage: window.WindowStage) {
59bea4f105Sopenharmony_ci    // Main window is created, set main page for this ability
60bea4f105Sopenharmony_ci    Logger.i(TAG, 'onWindowStageCreate')
61bea4f105Sopenharmony_ci    AbilityCommonUtil.init().then(() => {
62bea4f105Sopenharmony_ci      globalThis.windowClass = windowStage.getMainWindowSync();
63bea4f105Sopenharmony_ci      if (this.startModeOptions?.isOpenFileMode()) {
64bea4f105Sopenharmony_ci        // 文件选择器
65bea4f105Sopenharmony_ci        windowStage.loadContent('pages/browser/storage/MyPhone', this.storage, (err, data) => {
66bea4f105Sopenharmony_ci          if (err.code) {
67bea4f105Sopenharmony_ci            Logger.e(TAG, 'Failed to load the content: ' + JSON.stringify(err));
68bea4f105Sopenharmony_ci            return
69bea4f105Sopenharmony_ci          }
70bea4f105Sopenharmony_ci          Logger.i(TAG, 'data: ' + JSON.stringify(data));
71bea4f105Sopenharmony_ci        })
72bea4f105Sopenharmony_ci      } else {
73bea4f105Sopenharmony_ci        // 路径选择器
74bea4f105Sopenharmony_ci        windowStage.loadContent('pages/PathPicker', this.storage, (err, data) => {
75bea4f105Sopenharmony_ci          if (err.code) {
76bea4f105Sopenharmony_ci            Logger.e(TAG, 'Failed to load the content. Cause: ' + JSON.stringify(err))
77bea4f105Sopenharmony_ci            return;
78bea4f105Sopenharmony_ci          }
79bea4f105Sopenharmony_ci          Logger.i(TAG, 'Succeeded in loading the content. Data: ' + JSON.stringify(data))
80bea4f105Sopenharmony_ci        })
81bea4f105Sopenharmony_ci      }
82bea4f105Sopenharmony_ci    })
83bea4f105Sopenharmony_ci
84bea4f105Sopenharmony_ci  }
85bea4f105Sopenharmony_ci
86bea4f105Sopenharmony_ci  onWindowStageDestroy() {
87bea4f105Sopenharmony_ci    // Main window is destroyed, release UI related resources
88bea4f105Sopenharmony_ci    Logger.i(TAG, 'onWindowStageDestroy')
89bea4f105Sopenharmony_ci  }
90bea4f105Sopenharmony_ci
91bea4f105Sopenharmony_ci  onForeground() {
92bea4f105Sopenharmony_ci    // Ability has brought to foreground
93bea4f105Sopenharmony_ci    Logger.i(TAG, 'onForeground')
94bea4f105Sopenharmony_ci  }
95bea4f105Sopenharmony_ci
96bea4f105Sopenharmony_ci  onBackground() {
97bea4f105Sopenharmony_ci    // Ability has back to background
98bea4f105Sopenharmony_ci    Logger.i(TAG, 'onBackground')
99bea4f105Sopenharmony_ci  }
100bea4f105Sopenharmony_ci}
101