diff --git a/interfaces/kits/picker/picker.js b/interfaces/kits/picker/picker.js index e20e011c50f0ac1ea2975895d5009ee2f6683c19..175cd546d717c977b1d11468a9cb030a9e2a0e54 100644 --- a/interfaces/kits/picker/picker.js +++ b/interfaces/kits/picker/picker.js @@ -36,6 +36,7 @@ const DocumentPickerMode = { const ExtTypes = { DOWNLOAD_TYPE: 'filePicker', + AUDIO_PICKER_TYPE: 'audioPicker', }; const PickerDetailType = { @@ -75,6 +76,8 @@ const CREATE_FILE_NAME_LENGTH_LIMIT = 256; const ARGS_ZERO = 0; const ARGS_ONE = 1; const ARGS_TWO = 2; +const RESULT_CODE_ERROR = -1; +const RESULT_CODE_OK = 0; /* * UTF-8字符编码数值对应的存储长度: @@ -264,33 +267,22 @@ function parseDocumentPickerSelectOption(args, action) { return config; } -function getAudioPickerSelectResult(args) { - let selectResult = { - error: undefined, - data: undefined + +function parseAudioPickerSelectOption(args, action) { + let config = { + action: action, + parameters: { + extType: ExtTypes.AUDIO_PICKER_TYPE, + } }; - // 0:success - // -1:Non modal cancel - // 1:Modal cancel - // ResultCode is a non modal return code. - // Result is the return code of the modality. - if ((args.resultCode !== undefined && args.resultCode === 0) || (args.result !== undefined && args.result === 0)) { - if (args.want && args.want.parameters) { - if (args.want.parameters.select_item_list) { - selectResult.data = args.want.parameters.select_item_list; - } else { - selectResult.data = args.want.parameters['ability.params.stream']; - } + if (args.length > ARGS_ZERO && typeof args[ARGS_ZERO] === 'object') { + let option = args[ARGS_ZERO]; + if ((option.maxSelectNumber !== undefined) && option.maxSelectNumber > 0) { + config.parameters.key_pick_num = option.maxSelectNumber; } - } else if ((args.resultCode !== undefined && args.resultCode === -1) || - (args.result !== undefined && args.result === 1)) { - selectResult.data = []; - } else { - selectResult.error = getErr(ErrCode.RESULT_ERROR); } - - console.log('[picker] document select selectResult: ' + JSON.stringify(selectResult)); - return selectResult; + console.log('modal picker: audio select config: ' + JSON.stringify(config)); + return config; } function getDocumentPickerSelectResult(args) { @@ -298,21 +290,20 @@ function getDocumentPickerSelectResult(args) { error: undefined, data: undefined }; - // 0:success - // -1:modal cancel - if (args.resultCode !== undefined && args.resultCode === 0) { + if (args.resultCode === undefined) { + selectResult.error = getErr(ErrCode.RESULT_ERROR); + console.log('[picker] document select selectResult: ' + JSON.stringify(selectResult)); + return selectResult; + } + if (args.resultCode === RESULT_CODE_OK) { if (args.ability_params_stream) { selectResult.data = args.ability_params_stream; selectResult.error = args.resultCode; - } - - } else if (args.resultCode !== undefined && args.resultCode === -1) { + } + } else if (args.resultCode === RESULT_CODE_ERROR) { selectResult.data = []; selectResult.error = args.resultCode; - } else { - selectResult.error = getErr(ErrCode.RESULT_ERROR); } - console.log('[picker] document select selectResult: ' + JSON.stringify(selectResult)); return selectResult; } @@ -390,27 +381,49 @@ function parseDocumentPickerSaveOption(args, action) { return config; } +function getAudioPickerSelectResult(args) { + let selectResult = { + error: undefined, + data: undefined + }; + if (args.resultCode === undefined) { + selectResult.error = getErr(ErrCode.RESULT_ERROR); + console.log('[picker] getAudioPickerSelectResult selectResult: ' + JSON.stringify(selectResult)); + return selectResult; + } + if (args.resultCode === RESULT_CODE_OK) { + if (args.uriArr) { + selectResult.data = args.uriArr; + selectResult.error = args.resultCode; + } + } else if (args.resultCode === RESULT_CODE_ERROR) { + selectResult.data = []; + selectResult.error = args.resultCode; + } + console.log('[picker] getAudioPickerSelectResult selectResult: ' + JSON.stringify(selectResult)); + return selectResult; +} + + function getDocumentPickerSaveResult(args) { let saveResult = { error: undefined, data: undefined }; - - // 0:success - // -1:modal cancel - if (args.resultCode !== undefined && args.resultCode === 0) { + if (args.resultCode === undefined) { + saveResult.error = getErr(ErrCode.RESULT_ERROR); + console.log('[picker] getDocumentPickerSaveResult saveResult: ' + JSON.stringify(saveResult)); + return saveResult; + } + if (args.resultCode === RESULT_CODE_OK) { if (args.ability_params_stream) { saveResult.data = args.ability_params_stream; saveResult.error = args.resultCode; } - - } else if (args.resultCode !== undefined && args.resultCode === -1) { + } else if (args.resultCode === RESULT_CODE_ERROR) { saveResult.data = []; saveResult.error = args.resultCode; - } else { - saveResult.error = getErr(ErrCode.RESULT_ERROR); } - console.log('[picker] getDocumentPickerSaveResult saveResult: ' + JSON.stringify(saveResult)); return saveResult; } @@ -442,6 +455,7 @@ async function modalPicker(args, context, config) { return modalResult; } catch (resultError) { console.error('[picker] modal picker: Result error: ' + resultError); + return undefined; } } @@ -508,7 +522,7 @@ async function audioPickerSelect(...args) { throw checkAudioArgsResult; } - const audioSelectConfig = parseDocumentPickerSelectOption(args, ACTION.SELECT_ACTION); + const audioSelectConfig = parseAudioPickerSelectOption(args, ACTION.SELECT_ACTION); console.log('[picker] audio select config: ' + JSON.stringify(audioSelectConfig)); let audioSelectContext = undefined; @@ -527,22 +541,9 @@ async function audioPickerSelect(...args) { console.error('[picker] audioSelectContext == undefined'); throw getErr(ErrCode.CONTEXT_NO_EXIST); } - let result = await audioSelectContext.startAbilityForResult(audioSelectConfig, {windowMode: 0}); - console.log('[picker] audio select result: ' + JSON.stringify(result)); - const audioSelectResult = getAudioPickerSelectResult(result); - console.log('[picker] documentSelectResult: ' + JSON.stringify(audioSelectResult)); - if (args.length === ARGS_TWO && typeof args[ARGS_ONE] === 'function') { - return args[ARGS_ONE](audioSelectResult.error, audioSelectResult.data); - } else if (args.length === ARGS_ONE && typeof args[ARGS_ZERO] === 'function') { - return args[ARGS_ZERO](audioSelectResult.error, audioSelectResult.data); - } - return new Promise((resolve, reject) => { - if (audioSelectResult.data !== undefined) { - resolve(audioSelectResult.data); - } else { - reject(audioSelectResult.error); - } - }) + let modalSelectResult = await modalPicker(args, audioSelectContext, audioSelectConfig); + let saveResult = getAudioPickerSelectResult(modalSelectResult); + return sendResult(args, saveResult); } catch (error) { console.error('[picker] audio select error: ' + error); } diff --git a/interfaces/kits/picker/src/picker_n_exporter.cpp b/interfaces/kits/picker/src/picker_n_exporter.cpp index afd9b7067caa5030f74f633e6b53b88f1d72fafc..c387e127985f6c8c1aa65426b73fbd1c46345068 100644 --- a/interfaces/kits/picker/src/picker_n_exporter.cpp +++ b/interfaces/kits/picker/src/picker_n_exporter.cpp @@ -55,6 +55,30 @@ static void StartModalPickerExecute(napi_env env, void *data) HILOG_INFO("modal picker: StartModalPickerExecute is ready."); } +static void MakeResultWithArr(napi_env env, std::string key, napi_value &result, + std::shared_ptr pickerCallBack) +{ + napi_value array; + napi_create_array(env, &array); + napi_status status = napi_generic_failure; + if (pickerCallBack->want.GetParams().HasParam(key.c_str())) { + std::vector list = pickerCallBack->want.GetStringArrayParam(key.c_str()); + HILOG_INFO("modal picker: %{public}s size. %{public}zu ", key.c_str(), list.size()); + for (size_t i = 0; i < list.size(); i++) { + napi_value uri = nullptr; + napi_create_string_utf8(env, list[i].c_str(), NAPI_AUTO_LENGTH, &uri); + napi_set_element(env, array, i, uri); + } + if (key == "ability.params.stream") { + key = "ability_params_stream"; + } + status = napi_set_named_property(env, result, key.c_str(), array); + if (status != napi_ok) { + HILOG_ERROR("modal picker: napi_set_named_property uri failed"); + } + } +} + static napi_value MakeResultWithPickerCallBack(napi_env env, std::shared_ptr pickerCallBack) { if (pickerCallBack == nullptr) { @@ -74,24 +98,8 @@ static napi_value MakeResultWithPickerCallBack(napi_env env, std::shared_ptrwant.GetParams().HasParam("ability.params.stream")) { - std::vector list = pickerCallBack->want.GetStringArrayParam("ability.params.stream"); - const uint32_t len = list.size(); - HILOG_INFO("modal picker: ability.params.stream size. %{public}d ", len); - for (uint32_t i = 0; i < len; i++) { - napi_value uri = nullptr; - napi_create_string_utf8(env, list[i].c_str(), NAPI_AUTO_LENGTH, &uri); - napi_set_element(env, array, i, uri); - } - status = napi_set_named_property(env, result, "ability_params_stream", array); - if (status != napi_ok) { - HILOG_ERROR("modal picker: napi_set_named_property uri failed"); - } - } + MakeResultWithArr(env, "ability.params.stream", result, pickerCallBack); + MakeResultWithArr(env, "uriArr", result, pickerCallBack); return result; } @@ -103,10 +111,8 @@ static void StartModalPickerAsyncCallbackComplete(napi_env env, napi_status stat HILOG_ERROR("Async context is null"); return; } - auto jsContext = make_unique(); jsContext->status = false; - status = napi_get_undefined(env, &jsContext->data); if (status != napi_ok) { HILOG_ERROR("modal picker: napi_get_undefined jsContext->data failed");