• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-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 userFileManager from '@ohos.filemanagement.userFileManager';
16import dataSharePredicates from '@ohos.data.dataSharePredicates';
17import type { FetchOptions } from '../../access/UserFileManagerAccess';
18import {
19  AlbumFetchOptionBuilder,
20  FileFetchOptionBuilder,
21  UserFileManagerAccess
22} from '../../access/UserFileManagerAccess';
23import { Log } from '../../utils/Log';
24
25const TAG: string = 'AlbumDefine';
26
27export class AlbumDefine {
28  static readonly ALBUM_ID_ALL: string = 'default_all';
29  static readonly ALBUM_ID_CAMERA: string = 'default_camera';
30  static readonly ALBUM_ID_VIDEO: string = 'default_video';
31  static readonly ALBUM_ID_RECYCLE: string = 'default_recycle';
32  static readonly ALBUM_ID_FAVOR: string = 'default_favor';
33  static readonly ALBUM_ID_SNAPSHOT: string = 'default_snapshot';
34  static readonly ALBUM_ID_REMOTE: string = 'default_remote';
35
36  // 需要过滤的媒体文件类型
37  static readonly FILTER_MEDIA_TYPE_ALL: string = 'FILTER_MEDIA_TYPE_ALL';
38  static readonly FILTER_MEDIA_TYPE_IMAGE: string = 'FILTER_MEDIA_TYPE_IMAGE';
39  static readonly FILTER_MEDIA_TYPE_VIDEO: string = 'FILTER_MEDIA_TYPE_VIDEO';
40
41  static getFileFetchOpt(startIndex?: number, count?: number, filterMediaType?: string): FetchOptions {
42    let builder = new FileFetchOptionBuilder();
43    builder
44      .order(UserFileManagerAccess.FILE_KEY_DATE_TAKEN.toString(), false)
45    if (filterMediaType) {
46      AlbumDefine.setFilterMediaType(builder, filterMediaType)
47    }
48    if (startIndex != undefined && count != undefined && startIndex >= 0 && count >= 0) {
49      builder.select(startIndex, count);
50    }
51    return builder.build();
52  }
53
54  static getFileFetchOptWithEmptyColumn(startIndex?: number, count?: number, filterMediaType?: string): FetchOptions {
55    let fetchOption: userFileManager.FetchOptions = {
56      predicates: new dataSharePredicates.DataSharePredicates(),
57      fetchColumns: []
58    };
59    let builder = new FileFetchOptionBuilder(fetchOption);
60    builder.order(UserFileManagerAccess.FILE_KEY_DATE_TAKEN.toString(), false);
61    if (filterMediaType) {
62      AlbumDefine.setFilterMediaType(builder, filterMediaType);
63    }
64    if (startIndex != undefined && count != undefined && startIndex >= 0 && count >= 0) {
65      builder.select(startIndex, count);
66    }
67    return builder.build();
68  }
69
70  static getFavoriteFetchOpt(filterMediaType?: string) {
71    let builder = new FileFetchOptionBuilder();
72    if (filterMediaType == undefined) {
73      filterMediaType = AlbumDefine.FILTER_MEDIA_TYPE_ALL;
74    }
75    AlbumDefine.setFilterMediaType(builder, filterMediaType)
76      .order(UserFileManagerAccess.FILE_KEY_DATE_TAKEN.toString(), false);
77
78    return builder.build();
79  }
80
81  static getTimelineGroupFetchOpt() {
82    let builder = new FileFetchOptionBuilder();
83    builder.groupBy();
84    return builder.build();
85  }
86
87  static getFileFetchOptByUri(uri: string) {
88    let builder = new FileFetchOptionBuilder().uri(uri);
89    return builder.build();
90  }
91
92  static getFileFetchOptByName(displayName: string) {
93    let builder = new FileFetchOptionBuilder().displayName(displayName);
94    return builder.build();
95  }
96
97  static getAlbumFetchOptByName(name: string) {
98    let builder = new AlbumFetchOptionBuilder()
99    builder.albumName(name);
100    return builder.build();
101  }
102
103  static getAlbumFetchOptByUri(albumUri: string) {
104    let builder = new AlbumFetchOptionBuilder();
105    if (albumUri) {
106      builder.albumUri(albumUri);
107    }
108    return builder.build();
109  }
110
111  static setFilterMediaType(builder: FileFetchOptionBuilder, filterMediaType: string): FileFetchOptionBuilder {
112    if (filterMediaType == AlbumDefine.FILTER_MEDIA_TYPE_IMAGE) {
113      builder.media(UserFileManagerAccess.MEDIA_TYPE_IMAGE.toString())
114    } else if (filterMediaType == AlbumDefine.FILTER_MEDIA_TYPE_VIDEO) {
115      builder.media(UserFileManagerAccess.MEDIA_TYPE_VIDEO.toString())
116    }
117    return builder;
118  }
119}