• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023-2025 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 */
15const pickerHelper = requireInternal('file.picker');
16
17let gContext = undefined;
18
19const PhotoViewMIMETypes = {
20  IMAGE_TYPE: 'image/*',
21  VIDEO_TYPE: 'video/*',
22  IMAGE_VIDEO_TYPE: '*/*',
23  INVALID_TYPE: ''
24};
25
26const DocumentSelectMode = {
27  FILE: 0,
28  FOLDER: 1,
29  MIXED: 2,
30};
31
32const MergeTypeMode = {
33  DEFAULT: 0,
34  AUDIO: 1,
35  VIDEO: 2,
36  DOCUMENT: 3,
37  PICTURE: 4,
38};
39
40const DocumentPickerMode = {
41  DEFAULT: 0,
42  DOWNLOAD: 1,
43};
44
45const ExtTypes = {
46  DOWNLOAD_TYPE: 'filePicker',
47  AUDIO_PICKER_TYPE: 'audioPicker',
48  PHOTO_PICKER_TYPE: 'photoPicker',
49};
50
51const PickerDetailType = {
52  FILE_MGR_AUTH: 'downloadAuth',
53  FILE_MGR_SELECT:'select',
54  FILE_MGR_SAVE:'save',
55};
56
57const ErrCode = {
58  INVALID_ARGS: 13900020,
59  RESULT_ERROR: 13900042,
60  NAME_TOO_LONG: 13900030,
61  CONTEXT_NO_EXIST: 16000011,
62};
63
64const ERRCODE_MAP = new Map([
65  [ErrCode.INVALID_ARGS, 'Invalid argument'],
66  [ErrCode.RESULT_ERROR, 'Unknown error'],
67  [ErrCode.NAME_TOO_LONG, 'File name too long'],
68  [ErrCode.CONTEXT_NO_EXIST, 'Current ability failed to obtain context'],
69]);
70
71const PHOTO_VIEW_MIME_TYPE_MAP = new Map([
72  [PhotoViewMIMETypes.IMAGE_TYPE, 'FILTER_MEDIA_TYPE_IMAGE'],
73  [PhotoViewMIMETypes.VIDEO_TYPE, 'FILTER_MEDIA_TYPE_VIDEO'],
74  [PhotoViewMIMETypes.IMAGE_VIDEO_TYPE, 'FILTER_MEDIA_TYPE_ALL'],
75]);
76
77const ACTION = {
78  SELECT_ACTION: 'ohos.want.action.OPEN_FILE',
79  SELECT_ACTION_MODAL: 'ohos.want.action.OPEN_FILE_SERVICE',
80  SAVE_ACTION: 'ohos.want.action.CREATE_FILE',
81  SAVE_ACTION_MODAL: 'ohos.want.action.CREATE_FILE_SERVICE',
82};
83
84const CREATE_FILE_NAME_LENGTH_LIMIT = 256;
85const ARGS_ZERO = 0;
86const ARGS_ONE = 1;
87const ARGS_TWO = 2;
88const RESULT_CODE_ERROR = -1;
89const RESULT_CODE_OK = 0;
90const LENGTH_TWO = 2;
91const LENGTH_THREE = 3;
92let suffixIndex = -1;
93
94/*
95* UTF-8字符编码数值对应的存储长度:
96* 0000 - 0x007F (eg: a~z A~Z 0~9)
97* 0080 - 0x07FF (eg: 希腊字母)
98* 0800 - 0xFFFF (eg: 中文)
99* 其他 (eg: 平面符号)
100*/
101function strSizeUTF8(str) {
102  let strLen = str.length;
103  let bytesLen = 0;
104  let greeceLen = 2;
105  let chineseLen = 3;
106  let othersLen = 4;
107  for (let i = 0; i < strLen; i++) {
108    let charCode = str.charCodeAt(i);
109    if (charCode <= 0x007f) {
110      bytesLen++;
111    } else if (charCode <= 0x07ff) {
112      bytesLen += greeceLen;
113    } else if (charCode <= 0xffff) {
114      bytesLen += chineseLen;
115    } else {
116      bytesLen += othersLen;
117    }
118  }
119  return bytesLen;
120}
121
122function checkArguments(args) {
123  let checkArgumentsResult = undefined;
124  if (args.length === ARGS_TWO && typeof args[ARGS_ONE] !== 'function') {
125    checkArgumentsResult = getErr(ErrCode.INVALID_ARGS);
126  }
127
128  if (args.length > 0 && typeof args[ARGS_ZERO] === 'object') {
129    let option = args[ARGS_ZERO];
130    if (option.maxSelectNumber !== undefined) {
131      if (option.maxSelectNumber.toString().indexOf('.') !== -1) {
132        checkArgumentsResult = getErr(ErrCode.INVALID_ARGS);
133      }
134    }
135
136    if (option.newFileNames === undefined || option.newFileNames.length <= 0) {
137      return checkArgumentsResult;
138    }
139
140    for (let i = 0; i < option.newFileNames.length; i++) {
141      let value = option.newFileNames[i];
142      if (strSizeUTF8(value) >= CREATE_FILE_NAME_LENGTH_LIMIT) {
143        console.log('[picker] checkArguments Invalid name: ' + value);
144        checkArgumentsResult = getErr(ErrCode.NAME_TOO_LONG);
145      }
146    }
147  }
148
149  return checkArgumentsResult;
150}
151
152function getErr(errCode) {
153  return {code: errCode, message: ERRCODE_MAP.get(errCode)};
154}
155
156function parsePhotoPickerSelectOption(args) {
157  let config = {
158    action: 'ohos.want.action.photoPicker',
159    type: 'multipleselect',
160    parameters: {
161      uri: 'multipleselect',
162      extType: ExtTypes.PHOTO_PICKER_TYPE,
163    },
164  };
165  console.log('[picker] parse Photo SelectOption start ');
166  if (args.length > ARGS_ZERO && typeof args[ARGS_ZERO] === 'object') {
167    let option = args[ARGS_ZERO];
168    if (option.maxSelectNumber && option.maxSelectNumber > 0) {
169      let select = (option.maxSelectNumber === 1) ? 'singleselect' : 'multipleselect';
170      config.type = select;
171      config.parameters.uri = select;
172      config.parameters.maxSelectCount = option.maxSelectNumber;
173    }
174    if (option.MIMEType && PHOTO_VIEW_MIME_TYPE_MAP.has(option.MIMEType)) {
175      config.parameters.filterMediaType = PHOTO_VIEW_MIME_TYPE_MAP.get(option.MIMEType);
176    }
177  }
178  console.log('[picker] parse Photo SelectOption end ');
179  return config;
180}
181
182function anonymousPathArray(geturi) {
183  let anonymousPathArrays = [];
184  let anonymousPath = '';
185  if (geturi === undefined) {
186    return anonymousPathArrays;
187  }
188  for (let i = 0; i < geturi.length; ++i) {
189    let lastSlashIndex = geturi[i].lastIndexOf('/');
190    if (lastSlashIndex === -1) {
191      anonymousPathArrays.push(geturi[i]);
192    } else {
193      let dirPath = geturi[i].substring(0, lastSlashIndex + 1);
194      let fileName = geturi[i].substring(lastSlashIndex + 1);
195      if (fileName.length <= 0) {
196        anonymousPath = '******';
197      } else {
198        let lastLetter = fileName.slice(-1);
199        let maskedName = '******' + lastLetter;
200        anonymousPath = dirPath + maskedName;
201      }
202      anonymousPathArrays.push(anonymousPath);
203    }
204    }
205  return anonymousPathArrays;
206}
207
208function getPhotoPickerSelectResult(args) {
209  let selectResult = {
210    error: undefined,
211    data: undefined,
212  };
213
214  if (args.resultCode === 0) {
215    let uris = args.photoUris;
216    if (uris === undefined) {
217      console.log('[picker] Photo uris is undefined');
218      uris = [];
219    }
220    let isOriginal = args.isOriginal;
221    selectResult.data = new PhotoSelectResult(uris, isOriginal);
222  } else if (args.resultCode === -1) {
223    selectResult.data = new PhotoSelectResult([], undefined);
224  } else {
225    selectResult.error = getErr(ErrCode.RESULT_ERROR);
226  }
227  console.log('[picker] photo selectResult end');
228  return selectResult;
229}
230
231async function photoPickerSelect(...args) {
232  if (args.length !== LENGTH_TWO) {
233    console.error('[picker] args error: ' + args.length);
234    return undefined;
235  }
236  let checkPhotoArgsResult = checkArguments(args[ARGS_ZERO]);
237  if (checkPhotoArgsResult !== undefined) {
238    console.log('[picker] Photo Invalid argument');
239    throw checkPhotoArgsResult;
240  }
241
242  const config = parsePhotoPickerSelectOption(args[ARGS_ZERO]);
243
244  let photoSelectContext = undefined;
245  let photoSelectWindow = undefined;
246  try {
247    if (args[ARGS_ONE] !== undefined) {
248      photoSelectContext = args[ARGS_ONE];
249    } else {
250      photoSelectContext = getContext(this);
251    }
252  } catch (getContextError) {
253    console.error('[picker] getContext error: ' + getContextError);
254    throw getErr(ErrCode.CONTEXT_NO_EXIST);
255  }
256  try {
257    if (photoSelectContext === undefined) {
258      console.error('[picker] photoSelectContext == undefined');
259      throw getErr(ErrCode.CONTEXT_NO_EXIST);
260    }
261    let modalSelectResult = await modalPicker(photoSelectContext, config, photoSelectWindow);
262    console.log('[picker] photo modalSelectResult is ready');
263    const photoSelectResult = getPhotoPickerSelectResult(modalSelectResult);
264    let inputArgs = args[ARGS_ZERO];
265    if (inputArgs.length === ARGS_TWO && typeof inputArgs[ARGS_ONE] === 'function') {
266      return inputArgs[ARGS_ONE](photoSelectResult.error, photoSelectResult.data);
267    } else if (inputArgs.length === ARGS_ONE && typeof inputArgs[ARGS_ZERO] === 'function') {
268      return inputArgs[ARGS_ZERO](photoSelectResult.error, photoSelectResult.data);
269    }
270    return new Promise((resolve, reject) => {
271      if (photoSelectResult.data !== undefined) {
272        resolve(photoSelectResult.data);
273      } else {
274        reject(photoSelectResult.error);
275      }
276    });
277  } catch (error) {
278    console.error('[picker] photo select error: ' + error);
279  }
280  return undefined;
281}
282
283function parseDocumentPickerSelectOption(args, action) {
284  let config = {
285    action: action,
286    parameters: {
287      startMode: 'choose',
288      extType: ExtTypes.DOWNLOAD_TYPE,
289      pickerType: PickerDetailType.FILE_MGR_SELECT,
290    }
291  };
292
293  if (args.length > ARGS_ZERO && typeof args[ARGS_ZERO] === 'object') {
294    let option = args[ARGS_ZERO];
295    console.log('[picker] parseDocumentPickerSelectOption start');
296    config.parameters.key_select_mode = option.selectMode;
297    config.parameters.key_merge_type_mode = option.mergeMode;
298    config.parameters.key_is_encryption_supported = option.isEncryptionSupported;
299    config.parameters.key_theme_color_mode = option.themeColor;
300    if ((option.maxSelectNumber !== undefined) && option.maxSelectNumber > 0) {
301      config.parameters.key_pick_num = option.maxSelectNumber;
302    }
303    if (option.defaultFilePathUri !== undefined) {
304      config.parameters.key_pick_dir_path = option.defaultFilePathUri;
305    }
306    if ((option.fileSuffixFilters !== undefined) && option.fileSuffixFilters.length > 0) {
307      config.parameters.key_file_suffix_filter = option.fileSuffixFilters;
308    }
309    if (option.authMode !== undefined) {
310      config.parameters.key_auth_mode = option.authMode;
311    }
312    config.parameters.key_mult_auth_mode = option.multiAuthMode;
313    if (option.multiUriArray !== undefined) {
314      config.parameters.key_mult_uri_arr = option.multiUriArray;
315      console.log('[picker] parseDocumentPickerSelectOption multiUriArray length: ' + option.multiUriArray.length);
316    }
317  }
318  console.log('[picker] parseDocumentPickerSelectOption end');
319  return config;
320}
321
322function parseAudioPickerSelectOption(args, action) {
323  let config = {
324    action: action,
325    parameters: {
326      extType: ExtTypes.AUDIO_PICKER_TYPE,
327    }
328  };
329  if (args.length > ARGS_ZERO && typeof args[ARGS_ZERO] === 'object') {
330    let option = args[ARGS_ZERO];
331    if ((option.maxSelectNumber !== undefined) && option.maxSelectNumber > 0) {
332      config.parameters.key_pick_num = option.maxSelectNumber;
333    }
334  }
335  console.log('[picker] audio select config: ' + JSON.stringify(config));
336  return config;
337}
338
339function getDocumentPickerSelectResult(args) {
340  let selectResult = {
341    error: undefined,
342    data: undefined
343  };
344  if (args === undefined || args.resultCode === undefined) {
345    selectResult.error = getErr(ErrCode.RESULT_ERROR);
346    console.log('[picker] document select selectResult: ' + JSON.stringify(selectResult));
347    return selectResult;
348  }
349  if (args.resultCode === RESULT_CODE_OK) {
350    if (args.ability_params_stream) {
351      selectResult.data = args.ability_params_stream;
352      selectResult.error = args.resultCode;
353    } else if (args.ability_params_udkey) {
354      selectResult.data = args.ability_params_udkey;
355      selectResult.error = args.resultCode;
356    } else {
357      selectResult.data = [];
358      selectResult.error = RESULT_CODE_ERROR;
359    }
360  } else if (args.resultCode === RESULT_CODE_ERROR) {
361    selectResult.data = [];
362    selectResult.error = args.resultCode;
363  }
364
365  console.log('[picker] document select selectResult: : errorcode is = ' + selectResult.error +
366              ', selecturi is = ' + anonymousPathArray(selectResult.data));
367  return selectResult;
368}
369
370async function documentPickerSelect(...args) {
371  if (args.length !== LENGTH_THREE) {
372    console.error('[picker] args error: ' + args.length);
373    return undefined;
374  }
375  let checkDocumentSelectArgsResult = checkArguments(args[ARGS_ZERO]);
376  if (checkDocumentSelectArgsResult !== undefined) {
377    console.log('[picker] Document Select Invalid argument');
378    throw checkDocumentSelectArgsResult;
379  }
380
381  let documentSelectContext = undefined;
382  let documentSelectConfig = undefined;
383  let documentSelectResult = undefined;
384  let selectResult = undefined;
385  let documentSelectWindow = undefined;
386
387  try {
388    if (args[ARGS_ONE] !== undefined) {
389      documentSelectContext = args[ARGS_ONE];
390    } else {
391      documentSelectContext = getContext(this);
392    }
393  } catch (getContextError) {
394    console.error('[picker] getContext error: ' + getContextError);
395    throw getErr(ErrCode.CONTEXT_NO_EXIST);
396  }
397  try {
398    if (documentSelectContext === undefined) {
399      console.error('[picker] documentSelectContext == undefined');
400      throw getErr(ErrCode.CONTEXT_NO_EXIST);
401    }
402    if (args[ARGS_TWO] !== undefined) {
403        documentSelectWindow = args[ARGS_TWO];
404    }
405    documentSelectConfig = parseDocumentPickerSelectOption(args[ARGS_ZERO], ACTION.SELECT_ACTION_MODAL);
406    documentSelectResult = await modalPicker(documentSelectContext, documentSelectConfig, documentSelectWindow);
407  } catch (paramError) {
408    console.error('[picker] DocumentSelect paramError: ' + JSON.stringify(paramError));
409  }
410  selectResult = getDocumentPickerSelectResult(documentSelectResult);
411  return sendResult(args[ARGS_ZERO], selectResult);
412}
413
414function parseDocumentPickerSaveOption(args, action) {
415  let config = {
416    action: action,
417    parameters: {
418      startMode: 'save',
419      pickerMode: DocumentPickerMode.DEFAULT,
420      extType: ExtTypes.DOWNLOAD_TYPE,
421      pickerType: PickerDetailType.FILE_MGR_SAVE,
422    }
423  };
424  console.log('[picker] parse document save option start');
425  if (args.length > ARGS_ZERO && typeof args[ARGS_ZERO] === 'object') {
426    let option = args[ARGS_ZERO];
427    config.parameters.key_theme_color_mode = option.themeColor;
428    if ((option.newFileNames !== undefined) && option.newFileNames.length > 0) {
429      config.parameters.key_pick_file_name = option.newFileNames;
430      config.parameters.saveFile = option.newFileNames[0];
431    }
432
433    if (option.defaultFilePathUri !== undefined) {
434      config.parameters.key_pick_dir_path = option.defaultFilePathUri;
435    }
436    if ((option.fileSuffixChoices !== undefined) && option.fileSuffixChoices.length > 0) {
437      config.parameters.key_file_suffix_choices = option.fileSuffixChoices;
438    }
439    if (option.pickerMode === DocumentPickerMode.DOWNLOAD) {
440      config.parameters.pickerMode = option.pickerMode;
441      config.parameters.pickerType = PickerDetailType.FILE_MGR_AUTH;
442    }
443  }
444  console.log('[picker] document save config pickerMode is ' + config.parameters.pickerMode +
445    ',pickerType is ' + config.parameters.pickerType);
446  return config;
447}
448
449function getAudioPickerSelectResult(args) {
450  let selectResult = {
451    error: undefined,
452    data: undefined
453  };
454  if (args === undefined || args.resultCode === undefined) {
455    selectResult.error = getErr(ErrCode.RESULT_ERROR);
456    console.log('[picker] getAudioPickerSelectResult selectResult: ' + JSON.stringify(selectResult));
457    return selectResult;
458  }
459  if (args.resultCode === RESULT_CODE_OK) {
460    if (args.uriArr) {
461      selectResult.data = args.uriArr;
462      selectResult.error = args.resultCode;
463    } else {
464      selectResult.data = [];
465      selectResult.error = args.resultCode;
466    }
467  } else if (args.resultCode === RESULT_CODE_ERROR) {
468    selectResult.data = [];
469    selectResult.error = args.resultCode;
470  }
471
472  console.log('[picker] getAudioPickerSelectResult selectResult: errorcode is = ' + selectResult.error +
473              ', selecturi is = ' + anonymousPathArray(selectResult.data));
474  return selectResult;
475}
476
477
478function getDocumentPickerSaveResult(args) {
479  let saveResult = {
480    error: undefined,
481    data: undefined,
482    suffix: -1
483  };
484  if (args === undefined || args.resultCode === undefined) {
485    saveResult.error = getErr(ErrCode.RESULT_ERROR);
486    console.log('[picker] getDocumentPickerSaveResult saveResult: ' + JSON.stringify(saveResult));
487    return saveResult;
488  }
489  if (args.resultCode === RESULT_CODE_OK) {
490    if (args.ability_params_stream) {
491      saveResult.data = args.ability_params_stream;
492      saveResult.error = args.resultCode;
493      if (args.userSuffixIndex >= 0) {
494        saveResult.suffix = args.userSuffixIndex;
495      }
496    } else {
497      saveResult.data = [];
498      saveResult.error = RESULT_CODE_ERROR;
499    }
500  } else if (args.resultCode === RESULT_CODE_ERROR) {
501    saveResult.data = [];
502    saveResult.error = args.resultCode;
503  }
504
505  console.log('[picker] getDocumentPickerSaveResult saveResult: errorcode is = ' + saveResult.error +
506              ', selecturi is = ' + anonymousPathArray(saveResult.data) + ', usersavesuffix = ' + saveResult.suffix);
507  return saveResult;
508}
509
510function startModalPicker(context, config, window) {
511  if (context === undefined) {
512    throw Error('[picker] Context undefined.');
513  }
514  if (config === undefined) {
515    throw Error('[picker] Config undefined.');
516  }
517  gContext = context;
518  if (pickerHelper === undefined) {
519    throw Error('[picker] PickerHelper undefined.');
520  }
521  let helper;
522  if (window !== undefined) {
523    helper = pickerHelper.startModalPicker(gContext, config, window);
524  } else {
525    helper = pickerHelper.startModalPicker(gContext, config);
526  }
527  if (helper === undefined) {
528    throw Error('[picker] Please check the parameter you entered.');
529  }
530  return helper;
531}
532
533async function modalPicker(context, config, window) {
534  try {
535    console.log('[picker] modalPicker start ');
536    let modalResult = await startModalPicker(context, config, window);
537    return modalResult;
538  } catch (resultError) {
539    console.error('[picker] Result error: ' + resultError);
540    return undefined;
541  }
542}
543
544async function documentPickerSave(...args) {
545  if (args.length < LENGTH_TWO || args.length > LENGTH_THREE) {
546    console.error('[picker] args error: ' + args.length);
547    return undefined;
548  }
549  let checkDocumentSaveArgsResult = checkArguments(args[ARGS_ZERO]);
550  if (checkDocumentSaveArgsResult !== undefined) {
551    console.log('[picker] Document Save Invalid argument');
552    throw checkDocumentSaveArgsResult;
553  }
554
555  let documentSaveContext = undefined;
556  let documentSaveConfig = undefined;
557  let documentSaveResult = undefined;
558  let saveResult = undefined;
559  let documentSaveWindow = undefined;
560
561  try {
562    if (args[ARGS_ONE] !== undefined) {
563      documentSaveContext = args[ARGS_ONE];
564    } else {
565      documentSaveContext = getContext(this);
566    }
567  } catch (getContextError) {
568    console.error('[picker] getContext error: ' + getContextError);
569    throw getErr(ErrCode.CONTEXT_NO_EXIST);
570  }
571  if (args.length === LENGTH_THREE && args[ARGS_TWO] !== undefined) {
572      documentSaveWindow = args[ARGS_TWO];
573  }
574
575  documentSaveConfig = parseDocumentPickerSaveOption(args[ARGS_ZERO], ACTION.SAVE_ACTION_MODAL);
576  console.log('[picker] document save start');
577
578  documentSaveResult = await modalPicker(documentSaveContext, documentSaveConfig, documentSaveWindow);
579  saveResult = getDocumentPickerSaveResult(documentSaveResult);
580  suffixIndex = saveResult.suffix;
581  return sendResult(args[ARGS_ZERO], saveResult);
582}
583
584function getSelectedSuffixIndex() {
585  console.log('[picker] Get Selected Suffix Index start');
586  let index = suffixIndex;
587  suffixIndex = -1;
588  console.log('[picker] Get Selected Suffix Index end: ' + index);
589  return index;
590}
591
592async function sendResult(args, result) {
593  try {
594    if (result === undefined) {
595      console.log('[picker] result is undefined.');
596      return undefined;
597    }
598    if (args.length === ARGS_TWO && typeof args[ARGS_ONE] === 'function') {
599      console.log('[picker] sendresult is callback.');
600      return args[ARGS_ONE](result.error, result.data);
601    } else if (args.length === ARGS_ONE && typeof args[ARGS_ZERO] === 'function') {
602      console.log('[picker] sendresult is callback without options');
603      return args[ARGS_ZERO](result.error, result.data);
604    }
605    return new Promise((resolve, reject) => {
606      if (result.data !== undefined) {
607        resolve(result.data);
608      } else {
609        reject(result.error);
610      }
611    });
612  } catch (resultError) {
613    console.error('[picker] Result error: ' + resultError);
614  }
615  return undefined;
616}
617
618async function audioPickerSelect(...args) {
619  if (args.length !== LENGTH_TWO) {
620    console.error('[picker] args error: ' + args.length);
621    return undefined;
622  }
623  let checkAudioArgsResult = checkArguments(args[ARGS_ZERO]);
624  if (checkAudioArgsResult !== undefined) {
625    console.log('[picker] Audio Invalid argument');
626    throw checkAudioArgsResult;
627  }
628
629  const audioSelectConfig = parseAudioPickerSelectOption(args[ARGS_ZERO], ACTION.SELECT_ACTION);
630  console.log('[picker] audio select config: ' + JSON.stringify(audioSelectConfig));
631
632  let audioSelectContext = undefined;
633  let audipSelectWindow = undefined;
634  try {
635    if (args[ARGS_ONE] !== undefined) {
636      audioSelectContext = args[ARGS_ONE];
637    } else {
638      audioSelectContext = getContext(this);
639    }
640  } catch (getContextError) {
641    console.error('[picker] getContext error: ' + getContextError);
642    throw getErr(ErrCode.CONTEXT_NO_EXIST);
643  }
644  try {
645    if (audioSelectContext === undefined) {
646      console.error('[picker] audioSelectContext == undefined');
647      throw getErr(ErrCode.CONTEXT_NO_EXIST);
648    }
649    let modalSelectResult = await modalPicker(audioSelectContext, audioSelectConfig, audipSelectWindow);
650    let saveResult = getAudioPickerSelectResult(modalSelectResult);
651    return sendResult(args[ARGS_ZERO], saveResult);
652  } catch (error) {
653    console.error('[picker] audio select error: ' + error);
654  }
655  return undefined;
656}
657
658class PhotoSelectOptions {
659  constructor() {
660    this.MIMEType = PhotoViewMIMETypes.INVALID_TYPE;
661    this.maxSelectNumber = -1;
662  }
663}
664
665class PhotoSelectResult {
666  constructor(uris, isOriginalPhoto) {
667    this.photoUris = uris;
668    this.isOriginalPhoto = isOriginalPhoto;
669  }
670}
671
672class PhotoSaveOptions {
673  constructor() {
674    this.newFileNames = undefined;
675  }
676}
677
678class DocumentSelectOptions {
679  constructor() {
680    this.defaultFilePathUri = undefined;
681    this.fileSuffixFilters = undefined;
682    this.maxSelectNumber = undefined;
683    this.authMode = false;
684    this.selectMode = DocumentSelectMode.FILE;
685    this.mergeMode = MergeTypeMode.DEFAULT;
686    this.multiAuthMode = false;
687    this.multiUriArray = undefined;
688    this.isEncryptionSupported = false;
689    this.themeColor = undefined;
690  }
691}
692
693class DocumentSaveOptions {
694  constructor() {
695    this.newFileNames = undefined;
696    this.defaultFilePathUri = undefined;
697    this.fileSuffixChoices = undefined;
698    this.pickerMode = DocumentPickerMode.DEFAULT;
699    this.themeColor = undefined;
700  }
701}
702
703class AudioSelectOptions {
704  constructor() {}
705}
706
707class AudioSaveOptions {
708  constructor() {
709    this.newFileNames = undefined;
710  }
711}
712
713function ParseContext(args)
714{
715  if (args.length > ARGS_TWO || args.length < ARGS_ZERO || typeof args[ARGS_ZERO] !== 'object') {
716    return undefined;
717  }
718  return args[ARGS_ZERO];
719}
720
721function parseWindow(args)
722{
723  if (args.length !== ARGS_TWO) {
724    console.log('[picker] ParseWindow: not window mode.');
725    return undefined;
726  }
727  if (args.length === ARGS_TWO && typeof args[ARGS_ONE] !== 'object') {
728    console.log('[picker] ParseWindow: not window mode or type err.');
729    return undefined;
730  }
731  console.log('[picker] ParseWindow: window mode.');
732  return args[ARGS_ONE];
733}
734
735class PhotoViewPicker {
736  constructor(...args) {
737    this.context = ParseContext(args);
738  }
739  select(...args) {
740    return photoPickerSelect(args, this.context);
741  }
742  save(...args) {
743    return documentPickerSave(args, this.context);
744  }
745}
746
747class DocumentViewPicker {
748  constructor(...args) {
749    this.context = ParseContext(args);
750    this.window = parseWindow(args);
751  }
752  select(...args) {
753    return documentPickerSelect(args, this.context, this.window);
754  }
755  save(...args) {
756    return documentPickerSave(args, this.context, this.window);
757  }
758  getSelectedIndex() {
759    return getSelectedSuffixIndex();
760  }
761}
762
763class AudioViewPicker {
764  constructor(...args) {
765    this.context = ParseContext(args);
766  }
767  select(...args) {
768    return audioPickerSelect(args, this.context);
769  }
770  save(...args) {
771    return documentPickerSave(args, this.context);
772  }
773}
774
775export default {
776  getSelectedSuffixIndex,
777  startModalPicker,
778  ExtTypes : ExtTypes,
779  PickerDetailType: PickerDetailType,
780  PhotoViewMIMETypes : PhotoViewMIMETypes,
781  PhotoSelectOptions : PhotoSelectOptions,
782  PhotoSelectResult : PhotoSelectResult,
783  PhotoSaveOptions : PhotoSaveOptions,
784  DocumentSelectMode : DocumentSelectMode,
785  DocumentPickerMode : DocumentPickerMode,
786  MergeTypeMode : MergeTypeMode,
787  DocumentSelectOptions : DocumentSelectOptions,
788  DocumentSaveOptions : DocumentSaveOptions,
789  AudioSelectOptions : AudioSelectOptions,
790  AudioSaveOptions : AudioSaveOptions,
791  PhotoViewPicker : PhotoViewPicker,
792  DocumentViewPicker: DocumentViewPicker,
793  AudioViewPicker : AudioViewPicker,
794};
795