1/* 2 * Copyright (c) 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 photoAccessHelper from '@ohos.file.photoAccessHelper'; 17import { BaseItemInfo } from '@ohos.file.PhotoPickerComponent'; 18 19const FILTER_MEDIA_TYPE_ALL = 'FILTER_MEDIA_TYPE_ALL'; 20const FILTER_MEDIA_TYPE_IMAGE = 'FILTER_MEDIA_TYPE_IMAGE'; 21const FILTER_MEDIA_TYPE_VIDEO = 'FILTER_MEDIA_TYPE_VIDEO'; 22const FILTER_MEDIA_TYPE_IMAGE_MOVING_PHOTO = 'FILTER_MEDIA_TYPE_IMAGE_MOVING_PHOTO'; 23 24@Component 25export struct RecentPhotoComponent { 26 public recentPhotoOptions: RecentPhotoOptions | undefined; 27 public onRecentPhotoCheckResult?: RecentPhotoCheckResultCallback; 28 public onRecentPhotoClick?: RecentPhotoClickCallback; 29 public onRecentPhotoCheckInfo?: RecentPhotoCheckInfoCallback; 30 31 build() { 32 Row() { 33 Column() { 34 SecurityUIExtensionComponent({ 35 bundleName: 'com.ohos.photos', 36 abilityName: 'RecentUIExtensionAbility', 37 parameters: { 38 'ability.want.params.uiExtensionType': 'recentPhoto', 39 filterMediaType: this.convertMIMETypeToFilterType(this.recentPhotoOptions?.MIMEType), 40 period: this.recentPhotoOptions?.period as number, 41 photoSource: this.recentPhotoOptions?.photoSource as PhotoSource, 42 isFromPickerView: true, 43 isRecentPhotoCheckResultSet: this.onRecentPhotoCheckResult ? true : false 44 } 45 }) 46 .height('100%') 47 .width('100%') 48 .onRemoteReady(() => { 49 console.info('RecentPhotoComponent onRemoteReady'); 50 }) 51 .onReceive((data) => { 52 let wantParam: Record<string, Object> = data as Record<string, Object>; 53 this.handleOnReceive(wantParam); 54 }) 55 .onError(() => { 56 console.info('RecentPhotoComponent onError'); 57 }) 58 } 59 .width('100%') 60 } 61 .height('100%') 62 } 63 64 private handleOnReceive(wantParam: Record<string, Object>): void { 65 console.info('RecentPhotoComponent OnReceive:' + this.encrypt(JSON.stringify(wantParam))); 66 let dataType: string = wantParam['dataType'] as string; 67 if (dataType === 'checkResult') { 68 if (this.onRecentPhotoCheckResult) { 69 this.onRecentPhotoCheckResult(wantParam['isExist'] as boolean); 70 } 71 } else if (dataType === 'select') { 72 if (this.onRecentPhotoClick) { 73 let baseItemInfo: BaseItemInfo = new BaseItemInfo(); 74 baseItemInfo.uri = wantParam['uri'] as string; 75 baseItemInfo.mimeType = wantParam['mimeType'] as string; 76 baseItemInfo.width = wantParam['width'] as number; 77 baseItemInfo.height = wantParam['height'] as number; 78 baseItemInfo.size = wantParam['size'] as number; 79 baseItemInfo.duration = wantParam['duration'] as number; 80 this.onRecentPhotoClick(baseItemInfo); 81 } else { 82 console.warn('RecentPhotoComponent onReceive data type is invalid.'); 83 } 84 } else if (dataType === 'checkInfo') { 85 if (this.onRecentPhotoCheckInfo) { 86 let info: RecentPhotoInfo = new RecentPhotoInfo(); 87 info.identifier = wantParam.identifier as string; 88 info.dateTaken = wantParam.dateTaken as number; 89 this.onRecentPhotoCheckInfo(wantParam.isExist as boolean, info); 90 } 91 } 92 } 93 94 private convertMIMETypeToFilterType(mimeType: photoAccessHelper.PhotoViewMIMETypes | undefined): string { 95 let filterType: string; 96 if (mimeType === photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE) { 97 filterType = FILTER_MEDIA_TYPE_IMAGE; 98 } else if (mimeType === photoAccessHelper.PhotoViewMIMETypes.VIDEO_TYPE) { 99 filterType = FILTER_MEDIA_TYPE_VIDEO; 100 } else if (mimeType === photoAccessHelper.PhotoViewMIMETypes.MOVING_PHOTO_IMAGE_TYPE) { 101 filterType = FILTER_MEDIA_TYPE_IMAGE_MOVING_PHOTO; 102 } else { 103 filterType = FILTER_MEDIA_TYPE_ALL; 104 } 105 console.info('RecentPhotoComponent convertMIMETypeToFilterType : ' + JSON.stringify(filterType)); 106 return filterType; 107 } 108 109 private encrypt(data: string): string { 110 if (!data || data?.indexOf('file:///data/storage/') !== -1) { 111 return ''; 112 } 113 return data.replace(/(\/\w+)\./g, '/******.'); 114 } 115} 116 117export type RecentPhotoCheckResultCallback = (recentPhotoExists: boolean) => void; 118 119export type RecentPhotoClickCallback = (recentPhotoInfo: BaseItemInfo) => boolean; 120 121export type RecentPhotoCheckInfoCallback = (recentPhotoExists: boolean, info: RecentPhotoInfo) => void; 122 123export class RecentPhotoOptions { 124 period?: number; 125 MIMEType?: photoAccessHelper.PhotoViewMIMETypes; 126 photoSource?: PhotoSource 127} 128 129export class RecentPhotoInfo { 130 dateTaken?: number; 131 identifier?: string; 132} 133 134export enum PhotoSource { 135 ALL = 0, 136 CAMERA = 1, 137 SCREENSHOT = 2 138} 139