• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, PickerColorMode } 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            isAutoRefreshSupported: this.recentPhotoOptions?.isAutoRefreshSupported as boolean,
43            colorMode: this.recentPhotoOptions?.colorMode as PickerColorMode,
44            isFromPickerView: true,
45            isRecentPhotoCheckResultSet: this.onRecentPhotoCheckResult ? true : false
46          }
47        })
48          .height('100%')
49          .width('100%')
50          .onRemoteReady(() => {
51            console.info('RecentPhotoComponent onRemoteReady');
52          })
53          .onReceive((data) => {
54            let wantParam: Record<string, Object> = data as Record<string, Object>;
55            this.handleOnReceive(wantParam);
56          })
57          .onError(() => {
58            console.info('RecentPhotoComponent onError');
59          })
60      }
61      .width('100%')
62    }
63    .height('100%')
64  }
65
66  private handleOnReceive(wantParam: Record<string, Object>): void {
67    console.info('RecentPhotoComponent OnReceive:' + this.encrypt(JSON.stringify(wantParam)));
68    let dataType: string = wantParam['dataType'] as string;
69    if (dataType === 'checkResult') {
70      if (this.onRecentPhotoCheckResult) {
71        this.onRecentPhotoCheckResult(wantParam['isExist'] as boolean);
72      }
73    } else if (dataType === 'select') {
74      if (this.onRecentPhotoClick) {
75        let baseItemInfo: BaseItemInfo = new BaseItemInfo();
76        baseItemInfo.uri = wantParam['uri'] as string;
77        baseItemInfo.mimeType = wantParam['mimeType'] as string;
78        baseItemInfo.width = wantParam['width'] as number;
79        baseItemInfo.height = wantParam['height'] as number;
80        baseItemInfo.size = wantParam['size'] as number;
81        baseItemInfo.duration = wantParam['duration'] as number;
82        this.onRecentPhotoClick(baseItemInfo);
83      } else {
84        console.warn('RecentPhotoComponent onReceive data type is invalid.');
85      }
86    } else if (dataType === 'checkInfo') {
87      if (this.onRecentPhotoCheckInfo) {
88        let info: RecentPhotoInfo = new RecentPhotoInfo();
89        info.identifier = wantParam.identifier as string;
90        info.dateTaken = wantParam.dateTaken as number;
91        this.onRecentPhotoCheckInfo(wantParam.isExist as boolean, info);
92      }
93    }
94  }
95
96  private convertMIMETypeToFilterType(mimeType: photoAccessHelper.PhotoViewMIMETypes | undefined): string {
97    let filterType: string;
98    if (mimeType === photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE) {
99      filterType = FILTER_MEDIA_TYPE_IMAGE;
100    } else if (mimeType === photoAccessHelper.PhotoViewMIMETypes.VIDEO_TYPE) {
101      filterType = FILTER_MEDIA_TYPE_VIDEO;
102    } else if (mimeType === photoAccessHelper.PhotoViewMIMETypes.MOVING_PHOTO_IMAGE_TYPE) {
103      filterType = FILTER_MEDIA_TYPE_IMAGE_MOVING_PHOTO;
104    } else {
105      filterType = FILTER_MEDIA_TYPE_ALL;
106    }
107    console.info('RecentPhotoComponent convertMIMETypeToFilterType: ' + JSON.stringify(filterType));
108    return filterType;
109  }
110
111  private encrypt(data: string): string {
112    if (!data || data?.indexOf('file:///data/storage/') !== -1) {
113      return '';
114    }
115    return data.replace(/(\/\w+)\./g, '/******.');
116  }
117}
118
119export type RecentPhotoCheckResultCallback = (recentPhotoExists: boolean) => void;
120
121export type RecentPhotoClickCallback = (recentPhotoInfo: BaseItemInfo) => boolean;
122
123export type RecentPhotoCheckInfoCallback = (recentPhotoExists: boolean, info: RecentPhotoInfo) => void;
124
125export class RecentPhotoOptions {
126  period?: number;
127  MIMEType?: photoAccessHelper.PhotoViewMIMETypes;
128  photoSource?: PhotoSource;
129  isAutoRefreshSupported?: boolean;
130  colorMode?: PickerColorMode;
131}
132
133export class RecentPhotoInfo {
134  dateTaken?: number;
135  identifier?: string;
136}
137
138export enum PhotoSource {
139  ALL = 0,
140  CAMERA = 1,
141  SCREENSHOT = 2
142}
143