• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
15import type { BrowserDataInterface } from '../../interface/BrowserDataInterface';
16import { AlbumDefine } from './AlbumDefine';
17import { Log } from '../../utils/Log';
18import type { FileAsset, FileAssetEx } from '../../access/UserFileManagerAccess';
19import { Album, UserFileManagerAccess } from '../../access/UserFileManagerAccess';
20import type { AsyncCallback } from '../common/AsyncCallback';
21import { Constants } from '../common/Constants';
22
23const TAG: string = 'BrowserDataImpl';
24
25export type QueryParam = {
26  albumUri: string,
27  start: number,
28  count: number,
29  filterMediaType?: string
30};
31
32export abstract class BrowserDataImpl implements BrowserDataInterface {
33  static readonly THUMBNAIL_WIDTH = 256;
34
35  abstract getData(callback: AsyncCallback<unknown> | Function, param: QueryParam): void;
36
37  abstract getDataCount(callback: AsyncCallback<unknown> | Function, param: unknown): void;
38
39  abstract getDataByUri(uri: unknown): unknown;
40
41  abstract getDataByName(name: string, albumUri: string): unknown;
42
43  abstract getDataIndexByUri(callback: AsyncCallback<unknown> | Function, param: QueryParam, uri: string): void;
44
45  async getAllObject(fetchOpt): Promise<Array<FileAsset>> {
46    Log.debug(TAG, `getAllObject ${fetchOpt}`);
47    let allObject = await UserFileManagerAccess.getInstance().getAllObject(fetchOpt);
48    return allObject;
49  }
50
51  async getCount(fetchOpt): Promise<number> {
52    let count = await UserFileManagerAccess.getInstance().getCount(fetchOpt);
53    return count;
54  }
55
56  async getFirstObject(fetchOpt): Promise<FileAssetEx> {
57    Log.debug(TAG, 'getFirstObject');
58    let firstObject: FileAssetEx = await UserFileManagerAccess.getInstance().getFirstObject(fetchOpt);
59    return firstObject;
60  }
61
62  async getObject(fetchOpt): Promise<FileAsset> {
63    Log.debug(TAG, 'getFirstObject');
64    let object: FileAsset = await UserFileManagerAccess.getInstance().getObject(fetchOpt);
65    return object;
66  }
67
68  async getItems(albumUri?: string, startIndex?: number, count?: number, filterMediaType?: string): Promise<Array<FileAsset>> {
69    let result: Array<FileAsset> = null;
70
71    // albumUri不为空,则从目标相册中获取;否则默认从所有媒体资源中获取
72    if (albumUri) {
73      let album: Album = await UserFileManagerAccess.getInstance().getAlbumByUri(albumUri);
74      let fetchOpt = AlbumDefine.getFileFetchOpt(startIndex, count, filterMediaType);
75      if (album) {
76        let fetchResult = await album.getPhotoAssets(fetchOpt);
77        result = await fetchResult.getAllObject();
78        fetchResult.close();
79      }
80    } else {
81      let fetchOpt = AlbumDefine.getFileFetchOpt(startIndex, count, filterMediaType);
82      Log.debug(TAG, `getMediaItem start ${JSON.stringify(fetchOpt)}`);
83      result = await UserFileManagerAccess.getInstance().getAllObject(fetchOpt);
84    }
85    return result;
86  }
87
88  async getItemsCount(albumUri?: string, filterMediaType?: string): Promise<number> {
89    let count = 0;
90    if (albumUri) {
91      let album: Album = await UserFileManagerAccess.getInstance().getAlbumByUri(albumUri);
92      // 当前相册count始终为0,先通过查全部图片获取count
93      let fetchOpt = AlbumDefine.getFileFetchOpt(Constants.INVALID, Constants.INVALID, filterMediaType);
94      let fetchResult = await album.getPhotoAssets(fetchOpt);
95      count = fetchResult.getCount();
96      fetchResult.close();
97    } else {
98      let fetchOpt = AlbumDefine.getFileFetchOpt(undefined, undefined, filterMediaType);
99      count = await this.getCount(fetchOpt);
100    }
101    return count;
102  }
103
104  async getItemIndexByUri(uri: string, albumUri?: string): Promise<number> {
105    let index: number = Constants.INVALID;
106    let realUri: string = uri;
107    let realAlbumUri: string = albumUri;
108    let allObject: Array<FileAsset> = null;
109    Log.debug(TAG, `getItemIndexByUri uri: ${uri}, albumUri: ${albumUri}`);
110    if (!uri.startsWith(UserFileManagerAccess.REGISTER_TYPE_ALL_PHOTOS)) {
111      let targetObject: FileAsset = await this.getItemByUri(uri);
112      if (targetObject) {
113        Log.debug(TAG, `find photo for uri: ${realUri}=>${targetObject.uri}`);
114        realUri = targetObject.uri;
115      }
116    }
117    Log.debug(TAG, `getItemIndexByUri real uri: ${realUri}`);
118    if (albumUri && albumUri.length > 0) {
119      if (!albumUri.startsWith(UserFileManagerAccess.REGISTER_TYPE_ALL_ALBUMS)) {
120        let targetAlbum: Album = await UserFileManagerAccess.getInstance().getAlbumByUri(albumUri);
121        if (targetAlbum) {
122          Log.debug(TAG, `find album for uri: ${realAlbumUri}=>${targetAlbum.albumUri}`);
123          realAlbumUri = targetAlbum.albumUri;
124        }
125      }
126    } else {
127      realAlbumUri = "";
128    }
129    Log.debug(TAG, `getItemIndexByUri real album uri: ${realAlbumUri}`);
130    allObject = await this.getItems(realAlbumUri);
131    if (allObject) {
132      Log.debug(TAG, `getItemIndexByUri count: ${allObject.length}`);
133      index = allObject.findIndex((item: FileAsset) => item.uri == realUri);
134    }
135
136    return index;
137  }
138
139  async getItemByUri(uri: string): Promise<FileAsset> {
140    let object: FileAsset = null;
141    let fetchOpt = AlbumDefine.getFileFetchOptByUri(uri);
142    object = await this.getObject(fetchOpt);
143    return object;
144  }
145
146  getThumbnailSafe(sourceUri: string, path: string, size?): string {
147    try {
148      if (size) {
149        if (size.width != 0 && size.height != 0) {
150          return `${sourceUri}?oper=thumbnail&width=${size.width}&height=${size.height}&path=${path}`;
151        } else {
152          Log.warn(TAG, 'getThumbnailSafe with width==0 and height==0, so do not use thumbnail' + JSON.stringify(size));
153          return `${sourceUri}`;
154        }
155      } else {
156        return `${sourceUri}?oper=thumbnail&width=${BrowserDataImpl.THUMBNAIL_WIDTH}&height=${BrowserDataImpl.THUMBNAIL_WIDTH}&path=${path}`;
157      }
158    } catch (err) {
159      Log.warn(TAG, `get Thumbnail Failed! msg:${err}`);
160      return null;
161    }
162  }
163}