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 16const photoAccessHelper = requireInternal('file.photoAccessHelper'); 17const bundleManager = requireNapi('bundle.bundleManager'); 18 19const ARGS_ZERO = 0; 20const ARGS_ONE = 1; 21const ARGS_TWO = 2; 22const ARGS_THREE = 3; 23 24const WRITE_PERMISSION = 'ohos.permission.WRITE_IMAGEVIDEO'; 25 26const PERMISSION_DENIED = 13900012; 27const ERR_CODE_PARAMERTER_INVALID = 13900020; 28const ERR_CODE_OHOS_PERMISSION_DENIED = 201; 29const ERR_CODE_OHOS_PARAMERTER_INVALID = 401; 30const REQUEST_CODE_SUCCESS = 0; 31const PERMISSION_STATE_ERROR = -1; 32const ERROR_MSG_WRITE_PERMISSION = 'not have ohos.permission.WRITE_IMAGEVIDEO'; 33const ERROR_MSG_USER_DENY = 'user deny'; 34const ERROR_MSG_PARAMERTER_INVALID = 'input parmaeter invalid'; 35const ERROR_MSG_INNER_FAIL = 'System inner fail'; 36const ERROR_MSG_OHOS_INNER_FAIL = 'Internal system error'; 37 38const MAX_DELETE_NUMBER = 300; 39const MIN_DELETE_NUMBER = 1; 40const MAX_CONFIRM_NUMBER = 100; 41const MIN_CONFIRM_NUMBER = 1; 42 43let gContext = undefined; 44 45class BusinessError extends Error { 46 constructor(msg, code) { 47 super(msg); 48 this.code = code || PERMISSION_DENIED; 49 } 50} 51 52function checkArrayAndSize(array, minSize, maxSize) { 53 // check whether input is array 54 if (!Array.isArray(array)) { 55 console.error('photoAccessHelper invalid, array is null.'); 56 return false; 57 } 58 59 // check whether array length is valid 60 let len = array.length; 61 if ((len < minSize) || (len > maxSize)) { 62 console.error('photoAccessHelper invalid, array size invalid.'); 63 return false; 64 } 65 66 return true; 67} 68 69function checkIsUriValid(uri, isAppUri) { 70 if (!uri) { 71 console.error('photoAccessHelper invalid, uri is null.'); 72 return false; 73 } 74 75 if (typeof uri !== 'string') { 76 console.error('photoAccessHelper invalid, uri type is not string.'); 77 return false; 78 } 79 80 // media library uri starts with 'file://media/Photo/', createDeleteReques delete media library resource should check 81 if (!isAppUri) { 82 return uri.includes('file://media/Photo/'); 83 } 84 85 // showAssetsCreationDialog store third part application resource to media library, no need to check it 86 return true; 87} 88 89function checkParams(uriList, asyncCallback) { 90 if (arguments.length > ARGS_TWO) { 91 return false; 92 } 93 if (!checkArrayAndSize(uriList, MIN_DELETE_NUMBER, MAX_DELETE_NUMBER)) { 94 return false; 95 } 96 if (asyncCallback && typeof asyncCallback !== 'function') { 97 return false; 98 } 99 for (let uri of uriList) { 100 if (!checkIsUriValid(uri, false)) { 101 console.info(`photoAccessHelper invalid uri: ${uri}`); 102 return false; 103 } 104 } 105 return true; 106} 107function errorResult(rej, asyncCallback) { 108 if (asyncCallback) { 109 return asyncCallback(rej); 110 } 111 return new Promise((resolve, reject) => { 112 reject(rej); 113 }); 114} 115 116function getAbilityResource(bundleInfo) { 117 console.info('getAbilityResource enter.'); 118 let labelId = 0; 119 for (let hapInfo of bundleInfo.hapModulesInfo) { 120 if (hapInfo.type === bundleManager.ModuleType.ENTRY) { 121 labelId = getLabelId(hapInfo); 122 } 123 } 124 return labelId; 125} 126 127function getLabelId(hapInfo) { 128 let labelId = 0; 129 for (let abilityInfo of hapInfo.abilitiesInfo) { 130 let abilitiesInfoName = ''; 131 if (abilityInfo.name.includes('.')) { 132 let abilitiesInfoLength = abilityInfo.name.split('.').length; 133 abilitiesInfoName = abilityInfo.name.split('.')[abilitiesInfoLength - 1]; 134 } else { 135 abilitiesInfoName = abilityInfo.name; 136 } 137 if (abilitiesInfoName === hapInfo.mainElementName) { 138 labelId = abilityInfo.labelId; 139 } 140 } 141 return labelId; 142} 143 144async function getAppName() { 145 let appName = ''; 146 try { 147 const flags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_ABILITY | bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_HAP_MODULE; 148 const bundleInfo = await bundleManager.getBundleInfoForSelf(flags); 149 console.info(`photoAccessHelper bundleInfo: ${JSON.stringify(bundleInfo)}`); 150 if (bundleInfo === undefined || bundleInfo.hapModulesInfo === undefined || bundleInfo.hapModulesInfo.length === 0) { 151 return appName; 152 } 153 const labelId = getAbilityResource(bundleInfo); 154 const resourceMgr = gContext.resourceManager; 155 appName = await resourceMgr.getStringValue(labelId); 156 console.info(`photoAccessHelper appName: ${appName}`); 157 } catch (error) { 158 console.info(`photoAccessHelper error: ${JSON.stringify(error)}`); 159 } 160 161 return appName; 162} 163 164async function createPhotoDeleteRequestParamsOk(uriList, asyncCallback) { 165 let flags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION; 166 let { reqPermissionDetails, permissionGrantStates } = await bundleManager.getBundleInfoForSelf(flags); 167 let permissionIndex = -1; 168 for (let i = 0; i < reqPermissionDetails.length; i++) { 169 if (reqPermissionDetails[i].name === WRITE_PERMISSION) { 170 permissionIndex = i; 171 } 172 } 173 if (permissionIndex < 0 || permissionGrantStates[permissionIndex] === PERMISSION_STATE_ERROR) { 174 console.info('photoAccessHelper permission error'); 175 return errorResult(new BusinessError(ERROR_MSG_WRITE_PERMISSION), asyncCallback); 176 } 177 const appName = await getAppName(); 178 if (appName.length === 0) { 179 console.info('photoAccessHelper appName not found'); 180 return errorResult(new BusinessError(ERROR_MSG_PARAMERTER_INVALID, ERR_CODE_PARAMERTER_INVALID), asyncCallback); 181 } 182 try { 183 if (asyncCallback) { 184 return photoAccessHelper.createDeleteRequest(getContext(this), appName, uriList, result => { 185 if (result.result === REQUEST_CODE_SUCCESS) { 186 asyncCallback(); 187 } else if (result.result === PERMISSION_DENIED) { 188 asyncCallback(new BusinessError(ERROR_MSG_USER_DENY)); 189 } else { 190 asyncCallback(new BusinessError(ERROR_MSG_INNER_FAIL, result.result)); 191 } 192 }); 193 } else { 194 return new Promise((resolve, reject) => { 195 photoAccessHelper.createDeleteRequest(getContext(this), appName, uriList, result => { 196 if (result.result === REQUEST_CODE_SUCCESS) { 197 resolve(); 198 } else if (result.result === PERMISSION_DENIED) { 199 reject(new BusinessError(ERROR_MSG_USER_DENY)); 200 } else { 201 reject(new BusinessError(ERROR_MSG_INNER_FAIL, result.result)); 202 } 203 }); 204 }); 205 } 206 } catch (error) { 207 return errorResult(new BusinessError(error.message, error.code), asyncCallback); 208 } 209} 210 211function createDeleteRequest(...params) { 212 if (!checkParams(...params)) { 213 throw new BusinessError(ERROR_MSG_PARAMERTER_INVALID, ERR_CODE_PARAMERTER_INVALID); 214 } 215 return createPhotoDeleteRequestParamsOk(...params); 216} 217 218function checkIsPhotoCreationConfigValid(config) { 219 if (!config) { 220 console.error('photoAccessHelper invalid, config is null.'); 221 return false; 222 } 223 224 // check whether input is a object 225 if (typeof config !== 'object') { 226 console.error('photoAccessHelper invalid, config type is not object.'); 227 return false; 228 } 229 230 // check whether title is string if exsit 231 if ((config.title) && (typeof config.title !== 'string')) { 232 console.error('photoAccessHelper invalid, config.title type is not string.'); 233 return false; 234 } 235 236 // check whether fileNameExtension is string 237 if (!config.fileNameExtension) { 238 console.error('photoAccessHelper invalid, config.fileNameExtension is null.'); 239 return false; 240 } 241 if (typeof config.fileNameExtension !== 'string') { 242 console.error('photoAccessHelper invalid, config.fileNameExtension type is not string.'); 243 return false; 244 } 245 246 // check whether photoType is number 247 if (!config.photoType) { 248 console.error('photoAccessHelper invalid, config.photoType is null.'); 249 return false; 250 } 251 if (typeof config.photoType !== 'number') { 252 console.error('photoAccessHelper invalid, config.photoType type is not number.'); 253 return false; 254 } 255 256 // check whether subtype is number if exsit 257 if ((config.subtype) && (typeof config.subtype !== 'number')) { 258 console.error('photoAccessHelper invalid, config.subtype type is not number.'); 259 return false; 260 } 261 262 return true; 263} 264 265function checkConfirmBoxParams(srcFileUris, photoCreationConfigs) { 266 // check param number 267 if (arguments.length > ARGS_TWO) { 268 return false; 269 } 270 271 // check whether input array is valid 272 if (!checkArrayAndSize(srcFileUris, MIN_CONFIRM_NUMBER, MAX_CONFIRM_NUMBER)) { 273 return false; 274 } 275 if (!checkArrayAndSize(photoCreationConfigs, MIN_CONFIRM_NUMBER, MAX_CONFIRM_NUMBER)) { 276 return false; 277 } 278 if (srcFileUris.length !== photoCreationConfigs.length) { 279 return false; 280 } 281 282 // check whether srcFileUris element is valid 283 for (let srcFileUri of srcFileUris) { 284 if (!checkIsUriValid(srcFileUri, true)) { 285 console.error('photoAccessHelper invalid uri: ${srcFileUri}.'); 286 return false; 287 } 288 } 289 290 // check whether photoCreationConfigs element is valid 291 for (let photoCreateConfig of photoCreationConfigs) { 292 if (!checkIsPhotoCreationConfigValid(photoCreateConfig)) { 293 return false; 294 } 295 } 296 297 return true; 298} 299 300function getBundleInfo() { 301 let flags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_ABILITY | // for appName 302 bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_HAP_MODULE | // for appName 303 bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_SIGNATURE_INFO | // for appId 304 bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION; // for appInfo 305 let bundleInfo = bundleManager.getBundleInfoForSelfSync(flags); 306 if (((bundleInfo === undefined) || (bundleInfo.name === undefined)) || 307 ((bundleInfo.hapModulesInfo === undefined) || (bundleInfo.hapModulesInfo.length === 0)) || 308 ((bundleInfo.signatureInfo === undefined) || (bundleInfo.signatureInfo.appId === undefined)) || 309 ((bundleInfo.appInfo === undefined) || (bundleInfo.appInfo.labelId === 0))) { 310 console.error('photoAccessHelper failed to get bundle info.'); 311 return undefined; 312 } 313 314 return bundleInfo; 315} 316 317function showAssetsCreationDialogResult(result, reject, resolve) { 318 if (result.result !== REQUEST_CODE_SUCCESS) { 319 reject(new BusinessError(ERROR_MSG_OHOS_INNER_FAIL, result.result)); 320 } 321 322 if (result.data === undefined) { 323 result.data = []; 324 } 325 326 resolve(result.data); 327} 328 329async function showAssetsCreationDialogParamsOk(srcFileUris, photoCreationConfigs) { 330 let bundleInfo = getBundleInfo(); 331 if (bundleInfo === undefined) { 332 return new Promise((resolve, reject) => { 333 reject(new BusinessError(ERROR_MSG_PARAMERTER_INVALID, ERR_CODE_OHOS_PARAMERTER_INVALID)); 334 }); 335 } 336 337 // get bundleName and appId and appName 338 let bundleName = bundleInfo.name; 339 let appId = bundleInfo.signatureInfo.appId; 340 console.info('photoAccessHelper bundleName is ' + bundleName + '.'); 341 console.info('photoAccessHelper appId is ' + appId + '.'); 342 343 let labelId = bundleInfo.appInfo.labelId; 344 console.info('photoAccessHelper labelId is ' + appId + '.'); 345 let appName = ''; 346 347 try { 348 let modeleName = ''; 349 for (let hapInfo of bundleInfo.hapModulesInfo) { 350 if (labelId === hapInfo.labelId) { 351 modeleName = hapInfo.name; 352 } 353 } 354 console.info('photoAccessHelper modeleName is ' + modeleName + '.'); 355 appName = await gContext.createModuleContext(modeleName).resourceManager.getStringValue(labelId); 356 console.info('photoAccessHelper appName is ' + appName + '.'); 357 358 // only promise type 359 return new Promise((resolve, reject) => { 360 photoAccessHelper.showAssetsCreationDialog(getContext(this), srcFileUris, photoCreationConfigs, bundleName, 361 appName, appId, result => { 362 showAssetsCreationDialogResult(result, reject, resolve); 363 }); 364 }); 365 } catch (error) { 366 return errorResult(new BusinessError(error.message, error.code), null); 367 } 368} 369 370function showAssetsCreationDialog(...params) { 371 if (!checkConfirmBoxParams(...params)) { 372 throw new BusinessError(ERROR_MSG_PARAMERTER_INVALID, ERR_CODE_OHOS_PARAMERTER_INVALID); 373 } 374 return showAssetsCreationDialogParamsOk(...params); 375} 376 377async function createAssetWithShortTermPermissionOk(photoCreationConfig) { 378 let bundleInfo = getBundleInfo(); 379 if (bundleInfo === undefined) { 380 return new Promise((resolve, reject) => { 381 reject(new BusinessError(ERROR_MSG_PARAMERTER_INVALID, ERR_CODE_OHOS_PARAMERTER_INVALID)); 382 }); 383 } 384 385 let bundleName = bundleInfo.name; 386 let appId = bundleInfo.signatureInfo.appId; 387 console.info('photoAccessHelper bundleName is ' + bundleName + '.'); 388 console.info('photoAccessHelper appId is ' + appId + '.'); 389 390 let labelId = bundleInfo.appInfo.labelId; 391 console.info('photoAccessHelper labelId is ' + appId + '.'); 392 let appName = ''; 393 394 try { 395 let modeleName = ''; 396 for (let hapInfo of bundleInfo.hapModulesInfo) { 397 if (labelId === hapInfo.labelId) { 398 modeleName = hapInfo.name; 399 } 400 } 401 console.info('photoAccessHelper modeleName is ' + modeleName + '.'); 402 appName = await gContext.createModuleContext(modeleName).resourceManager.getStringValue(labelId); 403 console.info('photoAccessHelper appName is ' + appName + '.'); 404 405 if (photoAccessHelper.checkShortTermPermission()) { 406 let photoCreationConfigs = [photoCreationConfig]; 407 let desFileUris = await getPhotoAccessHelper(getContext(this)).createAssetsHasPermission(bundleName, appName, appId, 408 photoCreationConfigs); 409 return new Promise((resolve, reject) => { 410 resolve(desFileUris[0]); 411 }); 412 } 413 return new Promise((resolve, reject) => { 414 photoAccessHelper.createAssetWithShortTermPermission(getContext(this), photoCreationConfig, bundleName, appName, 415 appId, result => { 416 showAssetsCreationDialogResult(result, reject, resolve); 417 }); 418 }); 419 } catch (error) { 420 return errorResult(new BusinessError(ERROR_MSG_INNER_FAIL, error.code), null); 421 } 422} 423 424function createAssetWithShortTermPermission(photoCreationConfig) { 425 if (!checkIsPhotoCreationConfigValid(photoCreationConfig)) { 426 throw new BusinessError(ERROR_MSG_PARAMERTER_INVALID, ERR_CODE_OHOS_PARAMERTER_INVALID); 427 } 428 return createAssetWithShortTermPermissionOk(photoCreationConfig); 429} 430 431function getPhotoAccessHelper(context) { 432 if (context === undefined) { 433 console.log('photoAccessHelper gContext undefined'); 434 throw Error('photoAccessHelper gContext undefined'); 435 } 436 gContext = context; 437 let helper = photoAccessHelper.getPhotoAccessHelper(gContext); 438 if (helper !== undefined) { 439 console.log('photoAccessHelper getPhotoAccessHelper inner add createDeleteRequest and showAssetsCreationDialog'); 440 helper.createDeleteRequest = createDeleteRequest; 441 helper.showAssetsCreationDialog = showAssetsCreationDialog; 442 helper.createAssetWithShortTermPermission = createAssetWithShortTermPermission; 443 } 444 return helper; 445} 446 447function startPhotoPicker(context, config) { 448 if (context === undefined) { 449 console.log('photoAccessHelper gContext undefined'); 450 throw Error('photoAccessHelper gContext undefined'); 451 } 452 if (config === undefined) { 453 console.log('photoAccessHelper config undefined'); 454 throw Error('photoAccessHelper config undefined'); 455 } 456 gContext = context; 457 let helper = photoAccessHelper.startPhotoPicker(gContext, config); 458 if (helper !== undefined) { 459 console.log('photoAccessHelper startPhotoPicker inner add createDeleteRequest'); 460 helper.createDeleteRequest = createDeleteRequest; 461 } 462 return helper; 463} 464 465function getPhotoAccessHelperAsync(context, asyncCallback) { 466 if (context === undefined) { 467 console.log('photoAccessHelper gContext undefined'); 468 throw Error('photoAccessHelper gContext undefined'); 469 } 470 gContext = context; 471 if (arguments.length === 1) { 472 return photoAccessHelper.getPhotoAccessHelperAsync(gContext) 473 .then((helper) => { 474 if (helper !== undefined) { 475 console.log('photoAccessHelper getPhotoAccessHelperAsync inner add createDeleteRequest' + 476 ' and showAssetsCreationDialog'); 477 helper.createDeleteRequest = createDeleteRequest; 478 helper.showAssetsCreationDialog = showAssetsCreationDialog; 479 helper.createAssetWithShortTermPermission = createAssetWithShortTermPermission; 480 } 481 return helper; 482 }) 483 .catch((err) => { 484 console.log('photoAccessHelper getPhotoAccessHelperAsync err ' + err); 485 throw Error(err); 486 }); 487 } else if (arguments.length === ARGS_TWO && typeof asyncCallback === 'function') { 488 photoAccessHelper.getPhotoAccessHelperAsync(gContext, (err, helper) => { 489 console.log('photoAccessHelper getPhotoAccessHelperAsync callback ' + err); 490 if (err) { 491 asyncCallback(err); 492 } else { 493 if (helper !== undefined) { 494 console.log('photoAccessHelper getPhotoAccessHelperAsync callback add createDeleteRequest' + 495 ' and showAssetsCreationDialog'); 496 helper.createDeleteRequest = createDeleteRequest; 497 helper.showAssetsCreationDialog = showAssetsCreationDialog; 498 helper.createAssetWithShortTermPermission = createAssetWithShortTermPermission; 499 } 500 asyncCallback(err, helper); 501 } 502 }); 503 } else { 504 console.log('photoAccessHelper getPhotoAccessHelperAsync param invalid'); 505 throw new BusinessError(ERROR_MSG_PARAMERTER_INVALID, ERR_CODE_OHOS_PARAMERTER_INVALID); 506 } 507 return undefined; 508} 509 510const RecommendationType = { 511 // Indicates that QR code or barcode photos can be recommended 512 QR_OR_BAR_CODE: 1, 513 514 // Indicates that QR code photos can be recommended 515 QR_CODE: 2, 516 517 // Indicates that barcode photos can be recommended 518 BAR_CODE: 3, 519 520 // Indicates that QR code or barcode photos can be recommended 521 ID_CARD: 4, 522 523 // Indicates that profile picture photos can be recommended 524 PROFILE_PICTURE: 5, 525 526 // Indicates that passport photos can be recommended 527 PASSPORT: 6, 528 529 // Indicates that bank card photos can be recommended 530 BANK_CARD: 7, 531 532 // Indicates that driver license photos can be recommended 533 DRIVER_LICENSE: 8, 534 535 // Indicates that driving license photos can be recommended 536 DRIVING_LICENSE: 9, 537 538 // Indicates that featured single portrait photos can be recommended 539 FEATURED_SINGLE_PORTRAIT: 10 540}; 541 542const PhotoViewMIMETypes = { 543 IMAGE_TYPE: 'image/*', 544 VIDEO_TYPE: 'video/*', 545 IMAGE_VIDEO_TYPE: '*/*', 546 MOVING_PHOTO_IMAGE_TYPE: 'image/movingPhoto', 547 INVALID_TYPE: '' 548}; 549 550const ErrCode = { 551 INVALID_ARGS: 13900020, 552 RESULT_ERROR: 13900042, 553}; 554 555const CompleteButtonText = { 556 TEXT_DONE: 0, 557 TEXT_SEND: 1, 558 TEXT_ADD: 2, 559}; 560 561const ERRCODE_MAP = new Map([ 562 [ErrCode.INVALID_ARGS, 'Invalid argument'], 563 [ErrCode.RESULT_ERROR, 'Unknown error'], 564]); 565 566const PHOTO_VIEW_MIME_TYPE_MAP = new Map([ 567 [PhotoViewMIMETypes.IMAGE_TYPE, 'FILTER_MEDIA_TYPE_IMAGE'], 568 [PhotoViewMIMETypes.VIDEO_TYPE, 'FILTER_MEDIA_TYPE_VIDEO'], 569 [PhotoViewMIMETypes.IMAGE_VIDEO_TYPE, 'FILTER_MEDIA_TYPE_ALL'], 570 [PhotoViewMIMETypes.MOVING_PHOTO_IMAGE_TYPE, 'FILTER_MEDIA_TYPE_IMAGE_MOVING_PHOTO'], 571]); 572 573function checkArguments(args) { 574 let checkArgumentsResult = undefined; 575 576 if (args.length === ARGS_TWO && typeof args[ARGS_ONE] !== 'function') { 577 checkArgumentsResult = getErr(ErrCode.INVALID_ARGS); 578 } 579 580 if (args.length > 0 && typeof args[ARGS_ZERO] === 'object') { 581 let option = args[ARGS_ZERO]; 582 if (option.maxSelectNumber !== undefined) { 583 if (option.maxSelectNumber.toString().indexOf('.') !== -1) { 584 checkArgumentsResult = getErr(ErrCode.INVALID_ARGS); 585 } 586 } 587 } 588 589 return checkArgumentsResult; 590} 591 592function getErr(errCode) { 593 return { code: errCode, message: ERRCODE_MAP.get(errCode) }; 594} 595 596function parsePhotoPickerSelectOption(args) { 597 let config = { 598 action: 'ohos.want.action.photoPicker', 599 type: 'multipleselect', 600 parameters: { 601 uri: 'multipleselect', 602 }, 603 }; 604 605 if (args.length > ARGS_ZERO && typeof args[ARGS_ZERO] === 'object') { 606 let option = args[ARGS_ZERO]; 607 if (option.maxSelectNumber && option.maxSelectNumber > 0) { 608 let select = (option.maxSelectNumber === 1) ? 'singleselect' : 'multipleselect'; 609 config.type = select; 610 config.parameters.uri = select; 611 config.parameters.maxSelectCount = option.maxSelectNumber; 612 } 613 if (option.MIMEType && PHOTO_VIEW_MIME_TYPE_MAP.has(option.MIMEType)) { 614 config.parameters.filterMediaType = PHOTO_VIEW_MIME_TYPE_MAP.get(option.MIMEType); 615 } 616 config.parameters.isSearchSupported = option.isSearchSupported === undefined || option.isSearchSupported; 617 config.parameters.isPhotoTakingSupported = option.isPhotoTakingSupported === undefined || option.isPhotoTakingSupported; 618 config.parameters.isEditSupported = option.isEditSupported === undefined || option.isEditSupported; 619 config.parameters.recommendationOptions = option.recommendationOptions; 620 config.parameters.preselectedUris = option.preselectedUris; 621 config.parameters.isPreviewForSingleSelectionSupported = option.isPreviewForSingleSelectionSupported; 622 config.parameters.isOriginalSupported = option.isOriginalSupported; 623 config.parameters.subWindowName = option.subWindowName; 624 config.parameters.themeColor = option.themeColor; 625 config.parameters.completeButtonText = option.completeButtonText; 626 } 627 628 return config; 629} 630 631function getPhotoPickerSelectResult(args) { 632 let selectResult = { 633 error: undefined, 634 data: undefined, 635 }; 636 637 if (args.resultCode === 0) { 638 let uris = args.uris; 639 let isOrigin = args.isOrigin; 640 selectResult.data = new PhotoSelectResult(uris, isOrigin); 641 } else if (args.resultCode === -1) { 642 selectResult.data = new PhotoSelectResult([], undefined); 643 } else { 644 selectResult.error = getErr(ErrCode.RESULT_ERROR); 645 } 646 647 return selectResult; 648} 649 650async function photoPickerSelect(...args) { 651 let checkArgsResult = checkArguments(args); 652 if (checkArgsResult !== undefined) { 653 console.log('[picker] Invalid argument'); 654 throw checkArgsResult; 655 } 656 657 const config = parsePhotoPickerSelectOption(args); 658 console.log('[picker] config: ' + JSON.stringify(config)); 659 660 try { 661 let context = getContext(this); 662 let result = await startPhotoPicker(context, config); 663 console.log('[picker] result: ' + JSON.stringify(result)); 664 const selectResult = getPhotoPickerSelectResult(result); 665 console.log('[picker] selectResult: ' + JSON.stringify(selectResult)); 666 if (args.length === ARGS_TWO && typeof args[ARGS_ONE] === 'function') { 667 return args[ARGS_ONE](selectResult.error, selectResult.data); 668 } else if (args.length === ARGS_ONE && typeof args[ARGS_ZERO] === 'function') { 669 return args[ARGS_ZERO](selectResult.error, selectResult.data); 670 } 671 return new Promise((resolve, reject) => { 672 if (selectResult.data !== undefined) { 673 resolve(selectResult.data); 674 } else { 675 reject(selectResult.error); 676 } 677 }); 678 } catch (error) { 679 console.log('[picker] error: ' + error); 680 } 681 return undefined; 682} 683 684function BaseSelectOptions() { 685 this.MIMEType = PhotoViewMIMETypes.INVALID_TYPE; 686 this.maxSelectNumber = -1; 687 this.isSearchSupported = true; 688 this.isPhotoTakingSupported = true; 689 this.isPreviewForSingleSelectionSupported = true; 690} 691 692function PhotoSelectOptions() { 693 this.MIMEType = PhotoViewMIMETypes.INVALID_TYPE; 694 this.maxSelectNumber = -1; 695 this.isSearchSupported = true; 696 this.isPhotoTakingSupported = true; 697 this.isEditSupported = true; 698 this.isOriginalSupported = false; 699 this.completeButtonText = CompleteButtonText.TEXT_DONE; 700} 701 702function PhotoSelectResult(uris, isOriginalPhoto) { 703 this.photoUris = uris; 704 this.isOriginalPhoto = isOriginalPhoto; 705} 706 707function PhotoViewPicker() { 708 this.select = photoPickerSelect; 709} 710 711function RecommendationOptions() { 712} 713 714class MediaAssetChangeRequest extends photoAccessHelper.MediaAssetChangeRequest { 715 static deleteAssets(context, assets, asyncCallback) { 716 if (arguments.length > ARGS_THREE || arguments.length < ARGS_TWO) { 717 throw new BusinessError(ERROR_MSG_PARAMERTER_INVALID, ERR_CODE_OHOS_PARAMERTER_INVALID); 718 } 719 720 try { 721 if (asyncCallback) { 722 return super.deleteAssets(context, result => { 723 if (result.result === REQUEST_CODE_SUCCESS) { 724 asyncCallback(); 725 } else if (result.result === PERMISSION_DENIED) { 726 asyncCallback(new BusinessError(ERROR_MSG_USER_DENY, ERR_CODE_OHOS_PERMISSION_DENIED)); 727 } else { 728 asyncCallback(new BusinessError(ERROR_MSG_INNER_FAIL, result.result)); 729 } 730 }, assets, asyncCallback); 731 } 732 733 return new Promise((resolve, reject) => { 734 super.deleteAssets(context, result => { 735 if (result.result === REQUEST_CODE_SUCCESS) { 736 resolve(); 737 } else if (result.result === PERMISSION_DENIED) { 738 reject(new BusinessError(ERROR_MSG_USER_DENY, ERR_CODE_OHOS_PERMISSION_DENIED)); 739 } else { 740 reject(new BusinessError(ERROR_MSG_INNER_FAIL, result.result)); 741 } 742 }, assets, (err) => { 743 if (err) { 744 reject(err); 745 } else { 746 resolve(); 747 } 748 }); 749 }); 750 } catch (error) { 751 return errorResult(new BusinessError(error.message, error.code), asyncCallback); 752 } 753 } 754} 755 756export default { 757 getPhotoAccessHelper, 758 startPhotoPicker, 759 getPhotoAccessHelperAsync, 760 PhotoType: photoAccessHelper.PhotoType, 761 PhotoCreationConfig: photoAccessHelper.PhotoCreationConfig, 762 PhotoKeys: photoAccessHelper.PhotoKeys, 763 AlbumKeys: photoAccessHelper.AlbumKeys, 764 AlbumType: photoAccessHelper.AlbumType, 765 AlbumSubtype: photoAccessHelper.AlbumSubtype, 766 HighlightAlbum: photoAccessHelper.HighlightAlbum, 767 PositionType: photoAccessHelper.PositionType, 768 PhotoSubtype: photoAccessHelper.PhotoSubtype, 769 PhotoPermissionType: photoAccessHelper.PhotoPermissionType, 770 HideSensitiveType: photoAccessHelper.HideSensitiveType, 771 NotifyType: photoAccessHelper.NotifyType, 772 DefaultChangeUri: photoAccessHelper.DefaultChangeUri, 773 HiddenPhotosDisplayMode: photoAccessHelper.HiddenPhotosDisplayMode, 774 RequestPhotoType: photoAccessHelper.RequestPhotoType, 775 AnalysisType: photoAccessHelper.AnalysisType, 776 HighlightAlbumInfoType: photoAccessHelper.HighlightAlbumInfoType, 777 HighlightUserActionType: photoAccessHelper.HighlightUserActionType, 778 RequestPhotoType: photoAccessHelper.RequestPhotoType, 779 PhotoViewMIMETypes: PhotoViewMIMETypes, 780 DeliveryMode: photoAccessHelper.DeliveryMode, 781 SourceMode: photoAccessHelper.SourceMode, 782 AuthorizationMode: photoAccessHelper.AuthorizationMode, 783 BaseSelectOptions: BaseSelectOptions, 784 PhotoSelectOptions: PhotoSelectOptions, 785 PhotoSelectResult: PhotoSelectResult, 786 PhotoViewPicker: PhotoViewPicker, 787 RecommendationType: RecommendationType, 788 RecommendationOptions: RecommendationOptions, 789 ResourceType: photoAccessHelper.ResourceType, 790 MediaAssetEditData: photoAccessHelper.MediaAssetEditData, 791 MediaAssetChangeRequest: MediaAssetChangeRequest, 792 MediaAssetsChangeRequest: photoAccessHelper.MediaAssetsChangeRequest, 793 MediaAlbumChangeRequest: photoAccessHelper.MediaAlbumChangeRequest, 794 MediaAssetManager: photoAccessHelper.MediaAssetManager, 795 MovingPhoto: photoAccessHelper.MovingPhoto, 796 MovingPhotoEffectMode: photoAccessHelper.MovingPhotoEffectMode, 797 CompleteButtonText: CompleteButtonText, 798 ImageFileType: photoAccessHelper.ImageFileType, 799 CloudEnhancement: photoAccessHelper.CloudEnhancement, 800 CloudEnhancementTaskStage: photoAccessHelper.CloudEnhancementTaskStage, 801 CloudEnhancementState: photoAccessHelper.CloudEnhancementState, 802 CloudEnhancementTaskState: photoAccessHelper.CloudEnhancementTaskState, 803 VideoEnhancementType: photoAccessHelper.VideoEnhancementType, 804}; 805