1/*
2 * Copyright (c) 2023-2024 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 { common, Context, UIExtensionContentSession } from '@kit.AbilityKit';
17import { PickerWindowType, SelectMode, UnityStartMode } from '../constants/FilePickerItems';
18
19export class StartModeOptions {
20  /****************************    picker  *********************************/
21  /**************    common  *******************/
22  /**
23   * picker场景下,界面模式
24   */
25  public windowType = PickerWindowType.ABILITY;
26
27  public session: UIExtensionContentSession;
28
29  /**
30   * want对应的action
31   */
32  public action: string = '';
33
34  /**
35   * 拉起picker的应用ability名
36   */
37  public callerAbilityName: string = '';
38
39  /**
40   * 拉起picker的包名
41   */
42  public callerBundleName: string = '';
43
44  /**
45   * 选择器callerId
46   */
47  public callerUid: number = 0;
48
49  /**
50   * 拉起picker的默认文件或者指定目录
51   */
52  public defaultFilePathUri: string = '';
53
54  /**
55   * 代表拉起sysPicker/filePicker类型的ExtensionAbility
56   */
57  public extType: string = '';
58
59  /**
60   * UIExtensionContext
61   */
62  public uiExtContext: common.UIExtensionContext;
63
64  /**
65   * UIExtensionContext
66   */
67  public uiContext: common.UIAbilityContext;
68
69  /**
70   * context
71   */
72  public context: Context;
73
74
75  /**
76   * 用来区分选择,保存还是下载模式
77   * 当pickerType设置为downloadAuth时,用户配置的参数newFileNames、defaultFilePathUri和fileSuffixChoices将不会生效
78   */
79  public pickerType: string = 'DEFAULT';
80
81  /**************    select  *******************/
82  /**
83   * 选择文件的后缀类型
84   */
85  public fileSuffixFilters: string[] = [];
86
87  /**
88   * 选择文件最大个数,上限500, 默认为1
89   */
90  public maxSelectNumber: number = 1;
91
92  /**
93   * 支持选择的资源类型,比如:文件、文件夹和二者混合
94   */
95  public selectMode: SelectMode = SelectMode.FILE;
96
97  /**
98   * 当为授权模式,defaultFilePathUri必填,表明待授权uri
99   */
100  public isAuthMode: boolean = false;
101
102  /**
103   * 调用方传入文件类型(兼容双框架action)
104   */
105  public phonePickerType: string = '';
106
107  /**
108   * 调用方传入文件类型列表
109   */
110  public phonePickerTypeList: string[] = [];
111
112
113  /**************    save  *******************/
114  /**
115   * 进行保存的文件名列表
116   */
117  public newFileNames: string[] = [];
118
119  /**
120   * 保存文件的后缀类型
121   */
122  public fileSuffixChoices: string[] = [];
123
124  /**
125   * 手机保存文件的后缀类型
126   */
127  public PhoneFileSuffixChoices: string = '';
128
129
130  /****************************    主界面模式  *********************************/
131  /**
132   * 文管启动模式,只允许初始化过程修改一次,默认为主界面模式
133   */
134  public startMode: UnityStartMode = UnityStartMode.NORMAL;
135
136  public getFileSuffixFilterList(): string[] {
137    if (this.fileSuffixFilters.length === 0) {
138      this.fileSuffixFilters.push('.*');
139    }
140    return this.fileSuffixFilters;
141  }
142
143  public setSelectMode(mode: number | undefined): void {
144    if (mode === undefined) {
145      this.selectMode = SelectMode.FILE;
146      return;
147    }
148    if ((mode < SelectMode.FILE) || (mode > SelectMode.MIXED)) {
149      this.selectMode = SelectMode.FILE;
150      return;
151    }
152    this.selectMode = mode;
153  }
154
155  public setNewFileNames(names: string[] | undefined): void {
156    if (names === undefined) {
157      this.newFileNames = [];
158      return;
159    }
160    if (names.length === 0) {
161      this.newFileNames = [''];
162      return;
163    }
164    this.newFileNames = names;
165  }
166
167  public isDownloadMode(): boolean {
168    return this.pickerType === 'downloadAuth';
169  }
170
171  public isGrantPermissionMode(): boolean {
172    return this.isAuthMode;
173  }
174
175  public isSelectFolderMode(): boolean {
176    return this.startMode === UnityStartMode.FILE_PICKER_OPEN_FOLDER;
177  }
178
179  public isOpenFileMode(): boolean {
180    // 新方案需要通过pickerType区分
181    return this.action === 'ohos.want.action.OPEN_FILE_SERVICE' || this.action === 'ohos.want.action.OPEN_FILE';
182  }
183
184  public isCreateFileMode(): boolean {
185    // 新方案需要通过pickerType区分
186    return this.action === 'ohos.want.action.CREATE_FILE_SERVICE' || this.action === 'ohos.want.action.CREATE_FILE';
187  }
188
189  public isUxt(): boolean {
190    return this.windowType === PickerWindowType.UI;
191  }
192
193  public setUiExtContext(context: common.UIExtensionContext): void {
194    this.uiExtContext = context;
195  }
196}