1/* 2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 { Log } from '../utils/Log'; 17import { AlbumDataItem } from './AlbumDataItem'; 18import { MediaConstants } from '../constants/MediaConstants'; 19import { userFileModel } from '../base/UserFileModel'; 20import { getAlbumDisplayName } from '../base/UserFileDataHelper'; 21import photoAccessHelper from '@ohos.file.photoAccessHelper'; 22import dataSharePredicates from '@ohos.data.dataSharePredicates'; 23 24const TAG = 'AlbumDataImpl'; 25 26export class AlbumDataImpl { 27 private blackList: string[] = []; 28 private selectType: number = MediaConstants.SELECT_TYPE_ALL; 29 private deviceId: string = ''; 30 31 setBlackList(blackList: string[]): void { 32 this.blackList = blackList; 33 } 34 35 setSelectType(selectType: number): void { 36 this.selectType = selectType; 37 } 38 39 setDeviceId(deviceId: string): void { 40 this.deviceId = deviceId; 41 } 42 43 async reloadAlbumItemData(): Promise<AlbumDataItem[]> { 44 Log.info(TAG, 'reloadAlbumItemData'); 45 let albumDataItems: AlbumDataItem[] = []; 46 for (let i = 0;i < MediaConstants.ALBUM_DEFAULT_SORT_LIST.length; i++) { 47 await this.getAlbumItem(MediaConstants.ALBUM_DEFAULT_SORT_LIST[i], albumDataItems); 48 } 49 await this.getAlbumItem(MediaConstants.ALBUM_ID_USER, albumDataItems); 50 await this.getAlbumItem(MediaConstants.ALBUM_ID_RECYCLE, albumDataItems); 51 return albumDataItems; 52 } 53 54 private async getAlbumItem(id: string, albumDataItems: AlbumDataItem[]): Promise<void> { 55 Log.info(TAG, 'getAlbumItem:' + id); 56 if (this.blackList.indexOf(id) >= 0) { 57 Log.debug(TAG, 'no need as in black list'); 58 return; 59 } 60 if (this.deviceId.length > 0 && (id !== MediaConstants.ALBUM_ID_SNAPSHOT && id !== MediaConstants.ALBUM_ID_CAMERA)) { 61 Log.info(TAG, 'no need'); 62 return; 63 } 64 let albumType = MediaConstants.ALBUM_TYPE_SYSTEM; 65 let albumSubType = MediaConstants.ALBUM_SUBTYPE_USER_GENERIC; 66 switch (id) { 67 case MediaConstants.ALBUM_ID_FAVOR: 68 albumSubType = MediaConstants.ALBUM_SUBTYPE_FAVOR; 69 break; 70 case MediaConstants.ALBUM_ID_CAMERA: 71 albumSubType = MediaConstants.ALBUM_SUBTYPE_CAMERA; 72 break; 73 case MediaConstants.ALBUM_ID_RECYCLE: 74 albumSubType = MediaConstants.ALBUM_SUBTYPE_RECYCLE; 75 break; 76 case MediaConstants.ALBUM_ID_SNAPSHOT: 77 albumSubType = MediaConstants.ALBUM_SUBTYPE_SNAPSHOT; 78 break; 79 case MediaConstants.ALBUM_ID_VIDEO: 80 albumSubType = MediaConstants.ALBUM_SUBTYPE_VIDEO; 81 break; 82 case MediaConstants.ALBUM_ID_ALL: 83 await this.getAllPhotoAlbum(albumDataItems); 84 return; 85 case MediaConstants.ALBUM_ID_USER: 86 albumType = MediaConstants.ALBUM_TYPE_USER; 87 albumSubType = MediaConstants.ALBUM_SUBTYPE_USER_GENERIC; 88 break; 89 default: 90 break; 91 } 92 await this.getAlbumItemByUserFileMgr(id, albumType, albumSubType, albumDataItems); 93 } 94 95 private async getAlbumItemByUserFileMgr(id: string, type: photoAccessHelper.AlbumType, subType: photoAccessHelper.AlbumSubtype, albumDataItems: AlbumDataItem[]): Promise<void> { 96 let fetchResult:photoAccessHelper.FetchResult<photoAccessHelper.Album> = null; 97 try { 98 Log.info(TAG, 'getAlbumItemByUserFileMgr'); 99 fetchResult = await userFileModel.getUserFileMgr().getAlbums(type, subType); 100 Log.info(TAG, 'type:' + type); 101 Log.info(TAG, 'subType:' + subType); 102 Log.info(TAG, 'get Album fetchResult, count: ' + fetchResult.getCount()); 103 for (let i = 0; i < fetchResult.getCount(); i++) { 104 let albumAsset:photoAccessHelper.Album = await fetchResult.getObjectByPosition(i); 105 Log.info(TAG, 'albumAsset albumType: ' + i + '---' + albumAsset.albumType); 106 Log.info(TAG, 'albumAsset albumSubType: ' + i + '---' + albumAsset.albumSubtype); 107 Log.info(TAG, 'albumAsset albumName: ' + i + '---' + albumAsset.albumName); 108 Log.info(TAG, 'albumAsset albumUri: ' + i + '---' + albumAsset.albumUri); 109 if (this.blackList.indexOf(albumAsset.albumUri) >= 0) { 110 Log.debug(TAG, 'no need as in black list:'+albumAsset.albumUri); 111 continue; 112 } 113 Log.info(TAG, 'albumAsset count: ' + i + '---' + albumAsset.count); 114 Log.info(TAG, 'albumAsset coverUri: ' + i + '---' + albumAsset.coverUri); 115 let photoFetchResult:photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = null; 116 let fileAsset:photoAccessHelper.PhotoAsset = null; 117 let count = 0; 118 try { 119 let predicates = new dataSharePredicates.DataSharePredicates(); 120 let fetchOptions = { 121 fetchColumns: MediaConstants.FILE_ASSET_FETCH_COLUMNS, 122 predicates: predicates 123 }; 124 photoFetchResult = await albumAsset.getAssets(fetchOptions); 125 count = photoFetchResult.getCount(); 126 Log.info(TAG, 'photoFetchResult count: ' + count); 127 let displayName = 'unknown'; 128 if (id === MediaConstants.ALBUM_ID_USER) { 129 displayName = albumAsset.albumName; 130 } else { 131 displayName = await getAlbumDisplayName(id); 132 } 133 let albumType = albumAsset.albumType; 134 let albumSubType = albumAsset.albumSubtype; 135 let albumItem: AlbumDataItem = new AlbumDataItem(id, count, displayName, this.selectType, this.deviceId, albumType, albumSubType); 136 albumItem.uri = albumAsset.albumUri; 137 if (count > 0) { 138 fileAsset = await photoFetchResult.getFirstObject(); 139 Log.info(TAG, 'getFirstObject file displayName: ' + fileAsset.displayName); 140 await albumItem.update(fileAsset); 141 } 142 albumDataItems.push(albumItem); 143 } catch (err) { 144 Log.error(TAG, 'get Album getPhotoAssets failed with err: ' + err); 145 } finally { 146 if (photoFetchResult != null) { 147 photoFetchResult.close(); 148 } 149 } 150 } 151 } catch (err) { 152 Log.error(TAG, 'get Album fetchResult failed with err: ' + err); 153 } finally { 154 if (fetchResult != null) { 155 fetchResult.close(); 156 } 157 } 158 } 159 160 async getUserAlbumItemByUri(uri: string): Promise<photoAccessHelper.Album> { 161 let fetchResult:photoAccessHelper.FetchResult<photoAccessHelper.Album> = null; 162 let album: photoAccessHelper.Album = null; 163 try { 164 Log.info(TAG, 'getUserAlbumItemByUri'); 165 let predicates = new dataSharePredicates.DataSharePredicates(); 166 predicates.equalTo(photoAccessHelper.AlbumKeys.URI, uri); 167 let fetchOptions = { 168 fetchColumns: MediaConstants.EMPTY_FETCH_COLUMNS, 169 predicates: predicates 170 }; 171 fetchResult = await userFileModel.getUserFileMgr().getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions); 172 Log.info(TAG, 'get Album fetchResult, count: ' + fetchResult.getCount()); 173 if (fetchResult.getCount() > 0) { 174 album = await fetchResult.getFirstObject(); 175 } 176 } catch (err) { 177 Log.error(TAG, 'get Album fetchResult failed with err: ' + err); 178 } finally { 179 if (fetchResult != null) { 180 fetchResult.close(); 181 } 182 } 183 return album; 184 } 185 186 async removeFileFromAlbum(albumUri: string, uri: string): Promise<boolean> { 187 let album = await this.getUserAlbumItemByUri(albumUri); 188 let fileAsset = await userFileModel.getMediaItemByUri(uri); 189 if (album != null && fileAsset != null) { 190 try { 191 await album.removeAssets([fileAsset]); 192 return true; 193 } catch (err) { 194 Log.error(TAG, 'album removePhotoAssets failed with error: ' + err); 195 } 196 } 197 return false; 198 } 199 200 async getAllPhotoAlbum(albumDataItems: AlbumDataItem[]): Promise<void> { 201 let photoFetchResult = null; 202 try { 203 let predicates = new dataSharePredicates.DataSharePredicates(); 204 let fetchOptions = { 205 fetchColumns: MediaConstants.FILE_ASSET_FETCH_COLUMNS, 206 predicates: predicates 207 }; 208 photoFetchResult = await userFileModel.getUserFileMgr().getAssets(fetchOptions); 209 let count = photoFetchResult.getCount(); 210 Log.info(TAG, 'getAllPhotoAlbum count: ' + count); 211 let displayName = ''; 212 let id = MediaConstants.ALBUM_ID_ALL; 213 displayName = await getAlbumDisplayName(id); 214 let albumItem: AlbumDataItem = new AlbumDataItem(id, count, displayName, this.selectType, this.deviceId, -1, -1); 215 if (count > 0) { 216 let fileAsset = await photoFetchResult.getFirstObject(); 217 await albumItem.update(fileAsset); 218 Log.info(TAG, 'getFirstObject file displayName: ' + fileAsset.displayName); 219 } 220 albumDataItems.push(albumItem); 221 } catch (err) { 222 Log.error(TAG, 'get Album getPhotoAssets failed with err: ' + err); 223 } finally { 224 if (photoFetchResult != null) { 225 photoFetchResult.close(); 226 } 227 } 228 } 229 230 async reloadAlbumListItemData(): Promise<AlbumDataItem[]> { 231 Log.info(TAG, 'reloadAlbumListItemData start'); 232 let albumDataItems: AlbumDataItem[] = []; 233 for (let i = 0;i < MediaConstants.ALBUM_DEFAULT_SORT_LIST.length; i++) { 234 await this.getAlbumItem(MediaConstants.ALBUM_DEFAULT_SORT_LIST[i], albumDataItems); 235 } 236 await this.getCommonListAlbumItem(albumDataItems); 237 return albumDataItems; 238 } 239 240 private async getCommonListAlbumItem(albumDataItems: AlbumDataItem[]): Promise<void> { 241 let predicates = new dataSharePredicates.DataSharePredicates(); 242 let fetchOptions = { 243 fetchColumns: MediaConstants.EMPTY_FETCH_COLUMNS, 244 predicates: predicates 245 }; 246 let albums: photoAccessHelper.Album[] = await userFileModel.getAlbums(fetchOptions); 247 await this.getAlbumDataItem(albumDataItems, albums); 248 } 249 250 private async getAlbumDataItem(albumDataItems: AlbumDataItem[], albums: photoAccessHelper.Album[]): Promise<void> { 251 for (let i = 0;i < albums.length; i++) { 252 let album: photoAccessHelper.Album = albums[i]; 253 if (this.blackList.indexOf(album.albumUri.toString()) >= 0) { 254 continue; 255 } 256 let predicates = new dataSharePredicates.DataSharePredicates(); 257 let fetchOptions = { 258 fetchColumns: MediaConstants.FILE_ASSET_FETCH_COLUMNS, 259 predicates: predicates 260 }; 261 let fetchFileResult = await album.getAssets(fetchOptions); 262 try { 263 let count = fetchFileResult.getCount(); 264 if (count === 0) { 265 continue; 266 } 267 let item = new AlbumDataItem(MediaConstants.ALBUM_ID_USER, count, album.albumName, this.selectType, this.deviceId, 0, 0); 268 item.uri = album.albumUri; 269 await item.update(await fetchFileResult.getFirstObject()); 270 albumDataItems.push(item); 271 } catch (err) { 272 Log.error(TAG, 'on err: ' + JSON.stringify(err)); 273 } finally { 274 fetchFileResult.close(); 275 } 276 } 277 } 278} 279