1bea4f105Sopenharmony_ci/*
2bea4f105Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
3bea4f105Sopenharmony_ci */
4bea4f105Sopenharmony_ci
5bea4f105Sopenharmony_ciimport UIExtensionAbility from '@ohos.app.ability.UIExtensionAbility';
6bea4f105Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
7bea4f105Sopenharmony_ciimport type Want from '@ohos.app.ability.Want';
8bea4f105Sopenharmony_ciimport dataPreferences from '@ohos.data.preferences';
9bea4f105Sopenharmony_ciimport Logger from '../base/log/Logger';
10bea4f105Sopenharmony_ciimport { StartModeOptions } from '../base/model/StartModeOptions';
11bea4f105Sopenharmony_ciimport { PickerWindowType } from '../base/constants/FilePickerItems';
12bea4f105Sopenharmony_ciimport { UIExtensionContentSession } from '@kit.AbilityKit';
13bea4f105Sopenharmony_ciimport AbilityCommonUtil from '../base/utils/AbilityCommonUtil';
14bea4f105Sopenharmony_ciimport { FilePickerUtil } from '../base/utils/FilePickerUtil';
15bea4f105Sopenharmony_ciimport bundleResourceManager from '@ohos.bundle.bundleResourceManager';
16bea4f105Sopenharmony_ci
17bea4f105Sopenharmony_ciconst TRANSPARENT_COLOR = '#00000000';
18bea4f105Sopenharmony_ci
19bea4f105Sopenharmony_ciconst TAG: string = 'FilePickerUIExtAbility';
20bea4f105Sopenharmony_ci
21bea4f105Sopenharmony_ciexport default class FilePickerUIExtAbility extends UIExtensionAbility {
22bea4f105Sopenharmony_ci  private abilityKey: string = '';
23bea4f105Sopenharmony_ci  private securityPreferences?: dataPreferences.Preferences;
24bea4f105Sopenharmony_ci  private storage: LocalStorage = new LocalStorage();
25bea4f105Sopenharmony_ci
26bea4f105Sopenharmony_ci  onCreate(): void {
27bea4f105Sopenharmony_ci    Logger.i(TAG, 'FilePickerUIExtAbility onCreate');
28bea4f105Sopenharmony_ci  }
29bea4f105Sopenharmony_ci
30bea4f105Sopenharmony_ci  async onSessionCreate(want: Want, session: UIExtensionContentSession): Promise<void> {
31bea4f105Sopenharmony_ci    Logger.i(TAG, 'FilePickerUIExtAbility onSessionCreate, want: ' + JSON.stringify(want));
32bea4f105Sopenharmony_ci    globalThis.abilityContext = this.context;
33bea4f105Sopenharmony_ci    let options = this.initParam(want, session);
34bea4f105Sopenharmony_ci    this.getAppResourceInfo(options.callerBundleName, this.storage);
35bea4f105Sopenharmony_ci    if (options.isDownloadMode()) {
36bea4f105Sopenharmony_ci      this.initSessionDownloadAuth(session);
37bea4f105Sopenharmony_ci      return;
38bea4f105Sopenharmony_ci    }
39bea4f105Sopenharmony_ci
40bea4f105Sopenharmony_ci    this.abilityKey = `${TAG}+${Date.now()}`;
41bea4f105Sopenharmony_ci    await AbilityCommonUtil.init();
42bea4f105Sopenharmony_ci    if (options.isOpenFileMode()) {
43bea4f105Sopenharmony_ci      this.initSessionFilePicker(session);
44bea4f105Sopenharmony_ci      return;
45bea4f105Sopenharmony_ci    }
46bea4f105Sopenharmony_ci
47bea4f105Sopenharmony_ci    if (options.isCreateFileMode()) {
48bea4f105Sopenharmony_ci      this.initSessionPathPicker(session);
49bea4f105Sopenharmony_ci      return;
50bea4f105Sopenharmony_ci    }
51bea4f105Sopenharmony_ci  }
52bea4f105Sopenharmony_ci
53bea4f105Sopenharmony_ci  onSessionDestroy(session: UIExtensionContentSession): void {
54bea4f105Sopenharmony_ci    Logger.i(TAG, 'FilePickerUIExtAbility onSessionDestroy');
55bea4f105Sopenharmony_ci  }
56bea4f105Sopenharmony_ci
57bea4f105Sopenharmony_ci  onDestroy(): void {
58bea4f105Sopenharmony_ci    Logger.i(TAG, 'FilePickerUIExtAbility onDestroy');
59bea4f105Sopenharmony_ci  }
60bea4f105Sopenharmony_ci
61bea4f105Sopenharmony_ci  private getAppResourceInfo(bundleName: string, storage: LocalStorage): void {
62bea4f105Sopenharmony_ci    Logger.i(TAG, `getAppResourceInfo start`)
63bea4f105Sopenharmony_ci    const bundleFlags = bundleResourceManager.ResourceFlag.GET_RESOURCE_INFO_ALL;
64bea4f105Sopenharmony_ci    try {
65bea4f105Sopenharmony_ci      const resourceInfo = bundleResourceManager.getBundleResourceInfo(bundleName, bundleFlags);
66bea4f105Sopenharmony_ci      storage.setOrCreate<string>('appName', resourceInfo.label);
67bea4f105Sopenharmony_ci      storage.setOrCreate<string>('appIcon', resourceInfo.icon);
68bea4f105Sopenharmony_ci    } catch (err) {
69bea4f105Sopenharmony_ci      const message = (err as BusinessError).message;
70bea4f105Sopenharmony_ci      Logger.e(TAG, 'getBundleResourceInfo failed: %{public}s' + message);
71bea4f105Sopenharmony_ci    }
72bea4f105Sopenharmony_ci  }
73bea4f105Sopenharmony_ci
74bea4f105Sopenharmony_ci  initParam(want: Want, session: UIExtensionContentSession): StartModeOptions {
75bea4f105Sopenharmony_ci    let options: StartModeOptions = FilePickerUtil.getStartModeOptions(want);
76bea4f105Sopenharmony_ci    options.windowType = PickerWindowType.UI;
77bea4f105Sopenharmony_ci    options.setUiExtContext(this.context);
78bea4f105Sopenharmony_ci    options.context = this.context;
79bea4f105Sopenharmony_ci    options.session = session;
80bea4f105Sopenharmony_ci    options.fileSuffixFilters = AbilityCommonUtil.getKeyFileSuffixFilter(options.fileSuffixFilters);
81bea4f105Sopenharmony_ci    if (options.isOpenFileMode()) {
82bea4f105Sopenharmony_ci      options.fileSuffixFilters = AbilityCommonUtil.getKeyFileSuffixFilter(options.fileSuffixFilters);
83bea4f105Sopenharmony_ci      options.phonePickerType = (want.parameters?.key_pick_type as string) || '';
84bea4f105Sopenharmony_ci      options.phonePickerTypeList = AbilityCommonUtil.getKeyPickTypeList(want.parameters?.key_picker_type as object,
85bea4f105Sopenharmony_ci        want.parameters?.key_picker_type_list as object)
86bea4f105Sopenharmony_ci    }
87bea4f105Sopenharmony_ci    if (options.isCreateFileMode()) {
88bea4f105Sopenharmony_ci      options.PhoneFileSuffixChoices = AbilityCommonUtil.getKeyFileSuffixChoices(options.fileSuffixChoices);
89bea4f105Sopenharmony_ci    }
90bea4f105Sopenharmony_ci    this.storage.setOrCreate<StartModeOptions>('startModeOptions', options);
91bea4f105Sopenharmony_ci    return options;
92bea4f105Sopenharmony_ci  }
93bea4f105Sopenharmony_ci
94bea4f105Sopenharmony_ci  private initSessionDownloadAuth(session: UIExtensionContentSession) {
95bea4f105Sopenharmony_ci    Logger.i(TAG, `initSessionDownloadAuth start`)
96bea4f105Sopenharmony_ci    session.loadContent('pages/DownloadAuth', this.storage);
97bea4f105Sopenharmony_ci    session.setWindowBackgroundColor(TRANSPARENT_COLOR);
98bea4f105Sopenharmony_ci  }
99bea4f105Sopenharmony_ci
100bea4f105Sopenharmony_ci  private initSessionFilePicker(session: UIExtensionContentSession) {
101bea4f105Sopenharmony_ci    Logger.i(TAG, `initSessionFilePicker start`)
102bea4f105Sopenharmony_ci    try {
103bea4f105Sopenharmony_ci      const promise = dataPreferences.getPreferences(this.context, 'securityWarning');
104bea4f105Sopenharmony_ci      promise.then(async (object) => {
105bea4f105Sopenharmony_ci        this.securityPreferences = object;
106bea4f105Sopenharmony_ci        Logger.i(TAG, 'Succeeded in getting preferences.');
107bea4f105Sopenharmony_ci        const hasSecurityWarning = this.securityPreferences.hasSync('securityWarning');
108bea4f105Sopenharmony_ci        if (!hasSecurityWarning) {
109bea4f105Sopenharmony_ci          await this.securityPreferences.put('securityWarning', '1');
110bea4f105Sopenharmony_ci          await this.securityPreferences.flush();
111bea4f105Sopenharmony_ci          this.storage?.setOrCreate('securityWarning', '1');
112bea4f105Sopenharmony_ci          Logger.i(TAG, 'dataPreferences.flush');
113bea4f105Sopenharmony_ci        }
114bea4f105Sopenharmony_ci      }).catch((err: BusinessError) => {
115bea4f105Sopenharmony_ci        console.error('Failed to get preferences. code =' + err.code + ', message =' + err.message);
116bea4f105Sopenharmony_ci      })
117bea4f105Sopenharmony_ci    } catch (err) {
118bea4f105Sopenharmony_ci      console.error('Failed to get preferences. code =' + err.code + ', message =' + err.message);
119bea4f105Sopenharmony_ci    }
120bea4f105Sopenharmony_ci    AbilityCommonUtil.init().then(() => {
121bea4f105Sopenharmony_ci      session.loadContent('pages/browser/storage/MyPhone', this.storage);
122bea4f105Sopenharmony_ci      session.setWindowBackgroundColor(TRANSPARENT_COLOR);
123bea4f105Sopenharmony_ci    });
124bea4f105Sopenharmony_ci  }
125bea4f105Sopenharmony_ci
126bea4f105Sopenharmony_ci  private initSessionPathPicker(session: UIExtensionContentSession) {
127bea4f105Sopenharmony_ci    Logger.i(TAG, `initSessionPathPicker start`)
128bea4f105Sopenharmony_ci    AbilityCommonUtil.init().then(() => {
129bea4f105Sopenharmony_ci      session.loadContent('pages/PathPicker', this.storage);
130bea4f105Sopenharmony_ci      session.setWindowBackgroundColor(TRANSPARENT_COLOR);
131bea4f105Sopenharmony_ci    });
132bea4f105Sopenharmony_ci  }
133bea4f105Sopenharmony_ci};