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