1/* 2 * Copyright (c) 2023 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 16const PhotoViewMIMETypes = { 17 IMAGE_TYPE: "image/*", 18 VIDEO_TYPE: "video/*", 19 IMAGE_VIDEO_TYPE: "*/*", 20 INVALID_TYPE: "" 21} 22 23const ErrCode = { 24 INVALID_ARGS: 13900020, 25 RESULT_ERROR: 13900042, 26} 27 28const ERRCODE_MAP = new Map([ 29 [ErrCode.INVALID_ARGS, "Invalid argument"], 30 [ErrCode.RESULT_ERROR, "Unknown error"], 31]); 32 33const PHOTO_VIEW_MIME_TYPE_MAP = new Map([ 34 [PhotoViewMIMETypes.IMAGE_TYPE, "FILTER_MEDIA_TYPE_IMAGE"], 35 [PhotoViewMIMETypes.VIDEO_TYPE, "FILTER_MEDIA_TYPE_VIDEO"], 36 [PhotoViewMIMETypes.IMAGE_VIDEO_TYPE, "FILTER_MEDIA_TYPE_ALL"], 37]); 38 39const CREATE_FILE_NAME_LENGTH_LIMIT = 256; 40 41function checkArguments(args) { 42 if (args.length == 2 && typeof args[1] != "function") { 43 return false; 44 } 45 46 if (args.length > 0 && typeof args[0] == 'object') { 47 let option = args[0]; 48 if (option.maxSelectNumber != undefined) { 49 if (option.maxSelectNumber.toString().indexOf(".") != -1) { 50 return false; 51 } 52 } 53 54 if (option.newFileNames != undefined && option.newFileNames.length > 0) { 55 for (let i = 0; i < option.newFileNames.length; i++) { 56 let value = option.newFileNames[i]; 57 if ((value.indexOf(".") == -1) || (value.length > CREATE_FILE_NAME_LENGTH_LIMIT)) { 58 console.log("[picker] checkArguments Invalid name: " + value); 59 return false; 60 } 61 } 62 } 63 } 64 65 return true; 66} 67 68function getErr(errCode) { 69 return {code: errCode, message: ERRCODE_MAP.get(errCode)}; 70} 71 72async function photoPickerSelect() { 73 if (!checkArguments(arguments)) { 74 console.log("[picker] Invalid argument"); 75 throw Error(getErr(ErrCode.INVALID_ARGS)); 76 } 77 78 let config = { 79 "action": "ohos.want.action.photoPicker", 80 "type": "multipleselect", 81 parameters: { 82 'uri': "multipleselect", 83 }, 84 } 85 if (arguments.length > 0 && typeof arguments[0] == 'object') { 86 let option = arguments[0]; 87 if (option.maxSelectNumber > 0) { 88 let select = (option.maxSelectNumber == 1) ? "singleselect" : "multipleselect"; 89 config.type = select; 90 config.parameters.uri = select; 91 config.parameters.maxSelectCount = option.maxSelectNumber; 92 } 93 if (option.MIMEType.length > 0 && PHOTO_VIEW_MIME_TYPE_MAP.has(option.MIMEType)) { 94 config.parameters.filterMediaType = PHOTO_VIEW_MIME_TYPE_MAP.get(option.MIMEType); 95 } 96 } 97 console.log("[picker] config: " + JSON.stringify(config)); 98 99 try { 100 let context = getContext(this); 101 let result = await context.startAbilityForResult(config, {windowMode: 1}); 102 console.log("[picker] result: " + JSON.stringify(result)); 103 let uris = result.want.parameters["select-item-list"]; 104 let isOrigin = result.want.parameters["isOriginal"]; 105 if (result.resultCode == -1) { 106 result.resultCode = 0; 107 uris = []; 108 } 109 let err = (result.resultCode == 0) ? null : getErr(ErrCode.RESULT_ERROR); 110 let selectResult = (result.resultCode == 0) ? new PhotoSelectResult(uris, isOrigin) : null; 111 if (arguments.length == 2 && typeof arguments[1] == "function") { 112 return arguments[1](err, selectResult); 113 } else if (arguments.length == 1 && typeof arguments[0] == "function") { 114 return arguments[0](err, selectResult); 115 } 116 return new Promise((resolve, reject) => { 117 if (result.resultCode == 0) { 118 resolve(selectResult); 119 } else { 120 console.log("[picker] err: " + result.resultCode); 121 reject(result.resultCode); 122 } 123 }) 124 } catch (error) { 125 console.log("[picker] error: " + error); 126 } 127} 128 129async function documentPickerSelect() { 130 if (!checkArguments(arguments)) { 131 console.log("[picker] Invalid argument"); 132 throw Error(getErr(ErrCode.INVALID_ARGS)); 133 } 134 135 let config = { 136 action: "ohos.want.action.OPEN_FILE", 137 parameters: { 138 'startMode': 'choose', 139 } 140 } 141 console.log("[picker] config: " + JSON.stringify(config)); 142 143 try { 144 let context = getContext(this); 145 let result = await context.startAbilityForResult(config, {windowMode: 1}); 146 console.log("[picker] result: " + JSON.stringify(result)); 147 let uris = result.want.parameters.select_item_list; 148 if (result.resultCode == -1) { 149 result.resultCode = 0; 150 uris = []; 151 } 152 let err = (result.resultCode == 0) ? null : getErr(ErrCode.RESULT_ERROR); 153 let uriResult = (result.resultCode == 0) ? uris : null; 154 if (arguments.length == 2 && typeof arguments[1] == "function") { 155 return arguments[1](err, uriResult); 156 } else if (arguments.length == 1 && typeof arguments[0] == "function") { 157 return arguments[0](err, uriResult); 158 } 159 return new Promise((resolve, reject) => { 160 if (result.resultCode == 0) { 161 resolve(uris); 162 } else { 163 console.log("[picker] err: " + result.resultCode); 164 reject(result.resultCode); 165 } 166 }) 167 } catch (error) { 168 console.log("[picker] error: " + error); 169 } 170} 171 172async function documentPickerSave() { 173 if (!checkArguments(arguments)) { 174 console.log("[picker] Invalid argument"); 175 throw Error({code: 13900020, message: "Invalid argument"}); 176 } 177 178 let config = { 179 action: "ohos.want.action.CREATE_FILE", 180 parameters: { 181 'startMode': 'save', 182 } 183 } 184 if (arguments.length > 0 && typeof arguments[0] == 'object') { 185 let option = arguments[0]; 186 if (option.newFileNames.length > 0) { 187 config.parameters.key_pick_file_name = option.newFileNames; 188 config.parameters.saveFile = option.newFileNames[0]; 189 } 190 } 191 console.log("[picker] config: " + JSON.stringify(config)); 192 193 try { 194 let context = getContext(this); 195 let result = await context.startAbilityForResult(config, {windowMode: 1}); 196 console.log("[picker] result: " + JSON.stringify(result)); 197 let uris = result.want["parameters"].pick_path_return; 198 if (result.resultCode == -1) { 199 result.resultCode = 0; 200 uris = []; 201 } 202 let err = (result.resultCode == 0) ? null : getErr(ErrCode.RESULT_ERROR); 203 let uriResult = (result.resultCode == 0) ? uris : null; 204 if (arguments.length == 2 && typeof arguments[1] == "function") { 205 return arguments[1](err, uriResult); 206 } else if (arguments.length == 1 && typeof arguments[0] == "function") { 207 return arguments[0](err, uriResult); 208 } 209 return new Promise((resolve, reject) => { 210 if (result.resultCode == 0) { 211 resolve(uris); 212 } else { 213 console.log("[picker] err: " + result.resultCode); 214 reject(result.resultCode); 215 } 216 }) 217 } catch (error) { 218 console.log("[picker] error: " + error); 219 } 220} 221 222async function audioPickerSelect() { 223 if (!checkArguments(arguments)) { 224 console.log("[picker] Invalid argument"); 225 throw Error({code: 13900020, message: "Invalid argument"}); 226 } 227 228 let config = { 229 action: "ohos.want.action.OPEN_FILE", 230 parameters: { 231 'startMode': 'choose', 232 } 233 } 234 console.log("[picker] config: " + JSON.stringify(config)); 235 236 try { 237 let context = getContext(this); 238 let result = await context.startAbilityForResult(config, {windowMode: 1}); 239 console.log("[picker] result: " + JSON.stringify(result)); 240 let uris = result.want.parameters.select_item_list; 241 if (result.resultCode == -1) { 242 result.resultCode = 0; 243 uris = []; 244 } 245 let err = (result.resultCode == 0) ? null : getErr(ErrCode.RESULT_ERROR); 246 let uriResult = (result.resultCode == 0) ? uris : null; 247 if (arguments.length == 2 && typeof arguments[1] == "function") { 248 return arguments[1](err, uriResult); 249 } else if (arguments.length == 1 && typeof arguments[0] == "function") { 250 return arguments[0](err, uriResult); 251 } 252 return new Promise((resolve, reject) => { 253 if (result.resultCode == 0) { 254 resolve(uris); 255 } else { 256 console.log("[picker] err: " + result.resultCode); 257 reject(result.resultCode); 258 } 259 }) 260 } catch (error) { 261 console.log("[picker] error: " + error); 262 } 263} 264 265function PhotoSelectOptions() { 266 this.MIMEType = PhotoViewMIMETypes.INVALID_TYPE; 267 this.maxSelectNumber = -1; 268} 269 270function PhotoSelectResult(uris, isOriginalPhoto) { 271 this.photoUris = uris; 272 this.isOriginalPhoto = isOriginalPhoto; 273} 274 275function PhotoSaveOptions() { 276 this.newFileNames = []; 277} 278 279function DocumentSelectOptions() {} 280 281function DocumentSaveOptions() { 282 this.newFileNames = []; 283} 284 285function AudioSelectOptions() {} 286 287function AudioSaveOptions() { 288 this.newFileNames = []; 289} 290 291function PhotoViewPicker() { 292 this.select = photoPickerSelect; 293 this.save = documentPickerSave; 294} 295 296function DocumentViewPicker() { 297 this.select = documentPickerSelect; 298 this.save = documentPickerSave; 299} 300 301function AudioViewPicker() { 302 this.select = audioPickerSelect; 303 this.save = documentPickerSave; 304} 305 306export default { 307 PhotoViewMIMETypes : PhotoViewMIMETypes, 308 PhotoSelectOptions : PhotoSelectOptions, 309 PhotoSelectResult : PhotoSelectResult, 310 PhotoSaveOptions : PhotoSaveOptions, 311 DocumentSelectOptions : DocumentSelectOptions, 312 DocumentSaveOptions : DocumentSaveOptions, 313 AudioSelectOptions : AudioSelectOptions, 314 AudioSaveOptions : AudioSaveOptions, 315 PhotoViewPicker : PhotoViewPicker, 316 DocumentViewPicker: DocumentViewPicker, 317 AudioViewPicker : AudioViewPicker, 318}