• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// @ts-nocheck
2/*
3 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import fileIO from '@ohos.fileio';
18import { stashOrGetObject } from '../utils/SingleInstanceUtils';
19import { Log } from '../utils/Log';
20import { hiSysEventDataQueryTimedOut } from '../utils/hisysEventUtil';
21import photoAccessHelper from '@ohos.file.photoAccessHelper';
22import dataSharePredicates from '@ohos.data.dataSharePredicates';
23import { MediaConstants } from '../constants/MediaConstants'
24import { getSystemAlbumDisplayName } from './UserFileDataHelper';
25import { SimpleAlbumDataItem } from '../common/SimpleAlbumDataItem';
26import bundleManager from '@ohos.bundle.bundleManager';
27
28const TAG = 'UserFileModel';
29
30export class UserFileModelItem {
31  fileAsset: photoAccessHelper.PhotoAsset;
32  counts: number;
33}
34
35class UserFileModel {
36  private userFileMgr: photoAccessHelper.PhotoAccessHelper = undefined;
37
38  constructor() {
39  }
40
41  onCreate(context): void {
42    if (this.userFileMgr == undefined) {
43      this.userFileMgr = photoAccessHelper.getPhotoAccessHelper(context);
44    }
45  }
46
47  getUserFileMgr(): photoAccessHelper.PhotoAccessHelper {
48    return this.userFileMgr;
49  }
50
51  async createOne(displayName: string, albumUri: string): Promise<photoAccessHelper.PhotoAsset> {
52    Log.info(TAG, 'createOne displayName:' + displayName + ' albumUri: ' + albumUri);
53    let fileAsset = await this.userFileMgr.createAsset(displayName, albumUri);
54    let album = await this.getUserAlbumItemByUri(albumUri);
55    await album.addAssets([fileAsset]);
56    return fileAsset;
57  }
58
59  async getUserAlbumItemByUri(uri: string): Promise<photoAccessHelper.Album> {
60    let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = null;
61    let album: photoAccessHelper.Album = null;
62    try {
63      Log.info(TAG, 'getUserAlbumItemByUri');
64      let predicates = new dataSharePredicates.DataSharePredicates();
65      predicates.equalTo(photoAccessHelper.AlbumKeys.URI, uri);
66      let fetchOptions = {
67        fetchColumns: MediaConstants.EMPTY_FETCH_COLUMNS,
68        predicates: predicates
69      };
70      fetchResult = await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
71      Log.info(TAG, 'get Album fetchResult, count: ' + fetchResult.getCount());
72      if (fetchResult.getCount() > 0) {
73        album = fetchResult.getFirstObject();
74      }
75    } catch (err) {
76      Log.error(TAG, 'get Album fetchResult failed with err: ' + err);
77    } finally {
78      if (fetchResult != null) {
79        fetchResult.close();
80      }
81    }
82    return album;
83  }
84
85  async copyOne(source: photoAccessHelper.PhotoAsset, target: photoAccessHelper.PhotoAsset): Promise<void> {
86    Log.info(TAG, 'copy start: src:' + source.uri + ' target: ' + target.uri);
87    let fd: Number = await this.openAsset('R', source);
88    if (fd <= 0) {
89      throw new Error('fd is invalid');
90    }
91
92    let targetFd: Number = await this.openAsset('RW', target);
93    if (targetFd <= 0) {
94      throw new Error('targetFd is invalid');
95    }
96
97    await fileIO.copyFile(fd, targetFd);
98
99    await this.closeAsset(fd, source);
100    await this.closeAsset(targetFd, target);
101
102    Log.debug(TAG, 'copy end');
103  }
104
105  async deleteOne(uri: string): Promise<void> {
106    Log.debug(TAG, 'deleteAsset uri: ' + uri);
107    try {
108      await this.userFileMgr.deleteAssets([uri]);
109    } catch (err) {
110      Log.error(TAG, 'deleteOne with error: ' + err);
111    }
112  }
113
114  async recover(fileAsset: photoAccessHelper.PhotoAsset): Promise<void> {
115    let albumFetchResult = null;
116    try {
117      Log.debug(TAG, 'recoverPhotoAssetsDemoPromise:' + fileAsset.displayName);
118      albumFetchResult = await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.TRASH);
119      let album = await albumFetchResult.getFirstObject();
120      Log.debug(TAG, 'album  get');
121      album.recoverAssets([fileAsset]).then(() => {
122        Log.debug(TAG, 'album recoverPhotoAssets successfully');
123      }).catch((err) => {
124        Log.info(TAG, 'album recoverPhotoAssets failed with error: ' + err);
125      });
126    } catch (err) {
127      Log.error(TAG, 'recoverPhotoAssetsDemoPromise failed with error: ' + err);
128    } finally {
129      if (albumFetchResult != null) {
130        albumFetchResult.close();
131      }
132    }
133  }
134
135  async permanentDelete(fileAsset: photoAccessHelper.PhotoAsset): Promise<void> {
136    let albumFetchResult = null;
137    try {
138      Log.debug(TAG, 'permanentDelete');
139      albumFetchResult = await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.TRASH);
140      let album = await albumFetchResult.getFirstObject();
141      album.deleteAssets([fileAsset]).then(() => {
142        Log.debug('album deletePhotoAssets successfully');
143      }).catch((err) => {
144        Log.error(TAG, 'album deletePhotoAssets failed with error: ' + err);
145      });
146    } catch (err) {
147      Log.error(TAG, 'permanentDelete failed with error: ' + err);
148    } finally {
149      if (albumFetchResult != null) {
150        albumFetchResult.close();
151      }
152    }
153  }
154
155  async createAlbum(albumName: string): Promise<SimpleAlbumDataItem> {
156    let newAlbum: SimpleAlbumDataItem = undefined;
157    try {
158      let album = await this.userFileMgr.createAlbum(albumName);
159      newAlbum = new SimpleAlbumDataItem(MediaConstants.ALBUM_ID_USER, albumName, album.albumUri,
160        '', '', MediaConstants.ALBUM_TYPE_USER, MediaConstants.ALBUM_SUBTYPE_USER_GENERIC);
161    } catch (err) {
162      Log.error(TAG, 'createAlbum failed with error: ' + err);
163    }
164    return newAlbum;
165  }
166
167  async deleteAlbum(albumName: string): Promise<void> {
168    let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = null;
169    try {
170      let predicates = new dataSharePredicates.DataSharePredicates();
171      predicates.equalTo(photoAccessHelper.AlbumKeys.ALBUM_NAME, albumName);
172      let fetchOptions = {
173        fetchColumns: MediaConstants.EMPTY_FETCH_COLUMNS,
174        predicates: predicates
175      };
176      fetchResult = await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
177      for (let i = 0; i < fetchResult.getCount(); i++) {
178        let albumAsset = await fetchResult.getObjectByPosition(i);
179        Log.info(TAG, 'deleteAlbum albumUri' + albumAsset.albumUri);
180        Log.info(TAG, 'deleteAlbum albumName' + albumAsset.albumName);
181        this.userFileMgr.deleteAlbums([albumAsset]).then(() => {
182          Log.info(TAG, 'deletePhotoAlbumsPromise successfully');
183        }).catch((err) => {
184          Log.error(TAG, 'deletePhotoAlbumsPromise failed with err: ' + err);
185        });
186      }
187    } catch (err) {
188      Log.error(TAG, 'deletePhotoAlbumsPromise failed with error: ' + err);
189    } finally {
190      if (fetchResult != null) {
191        fetchResult.close();
192      }
193    }
194  }
195
196  async deleteAll(fetchOption: photoAccessHelper.FetchOptions): Promise<void> {
197    Log.info(TAG, 'deleteAll');
198    let fetchFileResult: photoAccessHelper.FetchResult = null;
199    try {
200      fetchFileResult = await this.userFileMgr.getAssets(fetchOption);
201      Log.debug(TAG, 'deleteAll getPhotoAssets');
202      let deleteAllGetAllObject = hiSysEventDataQueryTimedOut('deleteAllGetAllObject');
203      let fileAssets: photoAccessHelper.PhotoAsset[] = await fetchFileResult.getAllObject();
204      clearTimeout(deleteAllGetAllObject);
205      for (let i = 0;i < fileAssets.length; i++) {
206        await this.deleteOne(fileAssets[i].uri);
207      }
208      Log.debug(TAG, 'deleteAll getFirstObject');
209    } catch (err) {
210      Log.error(TAG, 'deleteAll error:' + JSON.stringify(err));
211    } finally {
212      if (fetchFileResult != null) {
213        fetchFileResult.close();
214      }
215    }
216    Log.debug(TAG, 'deleteAll finish');
217  }
218
219  async getAllMediaItems(): Promise<photoAccessHelper.PhotoAsset[]> {
220    let fileAssets: photoAccessHelper.PhotoAsset[] = [];
221    let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = null;
222    try {
223      let predicates = new dataSharePredicates.DataSharePredicates();
224      predicates.orderByDesc(photoAccessHelper.PhotoKeys.DATE_ADDED);
225      let emptyFetchOption = {
226        fetchColumns: MediaConstants.FILE_ASSET_FETCH_COLUMNS,
227        predicates: predicates
228      };
229      photoFetchResult = await this.userFileMgr.getAssets(emptyFetchOption);
230      Log.info(TAG, 'getAllMediaItems count: ' + photoFetchResult.getCount());
231      for (let i = 0;i < photoFetchResult.getCount(); i++) {
232        fileAssets.push(await photoFetchResult.getObjectByPosition(i));
233      }
234    } catch (err) {
235      Log.error(TAG, 'getAllMediaItems failed with err: ' + err);
236    } finally {
237      if (photoFetchResult != null) {
238        photoFetchResult.close();
239      }
240    }
241    return fileAssets;
242  }
243
244  async getAllMovingPhotoItems(): Promise<photoAccessHelper.PhotoAsset[]> {
245    let fileAssets: photoAccessHelper.PhotoAsset[] = [];
246    let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = null;
247    try {
248      let predicates = new dataSharePredicates.DataSharePredicates();
249      predicates.equalTo(MediaConstants.PHOTO_SUBTYPE, MediaConstants.MOVING_PHOTO);
250      predicates.orderByDesc(photoAccessHelper.PhotoKeys.DATE_ADDED);
251      let fetchOptions: photoAccessHelper.FetchOptions = {
252        fetchColumns: MediaConstants.FILE_ASSET_FETCH_COLUMNS,
253        predicates: predicates
254      };
255      photoFetchResult = await this.userFileMgr.getAssets(fetchOptions);
256      Log.info(TAG, 'getAllMovingPhotoItems count: ' + photoFetchResult.getCount());
257      for (let i = 0; i < photoFetchResult.getCount(); i++) {
258        fileAssets.push(await photoFetchResult.getObjectByPosition(i));
259      }
260    } catch (err) {
261      Log.error(TAG, 'getAllMovingPhotoItems failed with err: ' + err);
262    } finally {
263      if (photoFetchResult != null) {
264        photoFetchResult.close();
265      }
266    }
267    return fileAssets;
268  }
269
270  async getAllMediaItemsByType(type: number, subType: number, albumFetchOption, fileFetchOption): Promise<photoAccessHelper.PhotoAsset[]> {
271    let fileAssets: photoAccessHelper.PhotoAsset[] = [];
272    let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = null;
273    try {
274      Log.info(TAG, 'getAllMediaItemsByUserFile');
275      Log.info(TAG, 'type:' + type);
276      Log.info(TAG, 'subType:' + subType);
277      if (type === photoAccessHelper.AlbumType.USER && albumFetchOption != null) {
278        Log.info(TAG, 'albumFetchOption != null');
279        fetchResult = await this.userFileMgr.getAlbums(type, subType, albumFetchOption);
280      } else {
281        fetchResult = await this.userFileMgr.getAlbums(type, subType);
282      }
283      Log.info(TAG, 'get Album fetchResult, count: ' + fetchResult.getCount());
284      for (let i = 0; i < fetchResult.getCount(); i++) {
285        let albumAsset: photoAccessHelper.Album = await fetchResult.getObjectByPosition(i);
286        let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = null;
287        try {
288          photoFetchResult = await albumAsset.getAssets(fileFetchOption);
289          for (let i = 0; i < photoFetchResult.getCount(); i++) {
290            let photoAsset = await photoFetchResult.getObjectByPosition(i);
291            fileAssets.push(photoAsset);
292            Log.info(TAG, 'getPhotoAssets successfully, file displayName: ' + photoAsset.displayName);
293          }
294        } catch (err) {
295          Log.info(TAG, 'get Album getPhotoAssets failed with err: ' + err);
296        } finally {
297          if (photoFetchResult != null) {
298            photoFetchResult.close();
299          }
300        }
301      }
302    } catch (err) {
303      Log.error(TAG, 'get Album fetchResult failed with err: ' + err);
304    } finally {
305      if (fetchResult != null) {
306        fetchResult.close();
307      }
308    }
309    Log.info(TAG, 'fileAssets: ' + fileAssets.length);
310    return fileAssets;
311  }
312
313  async getMediaItemByUriFromTrash(uri: string): Promise<photoAccessHelper.PhotoAsset> {
314    Log.info(TAG, 'getMediaItemByUriFromTrash');
315    Log.info(TAG, 'uri:' + uri);
316    let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = null;
317    let fileAsset: photoAccessHelper.PhotoAsset = null;
318    try {
319      albumFetchResult = await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.TRASH);
320      let albumAsset = await albumFetchResult.getFirstObject();
321      let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = null;
322      try {
323        let predicates = new dataSharePredicates.DataSharePredicates();
324        let emptyFetchOption = {
325          fetchColumns: MediaConstants.FILE_ASSET_FETCH_COLUMNS,
326          predicates: predicates
327        };
328        photoFetchResult = await albumAsset.getAssets(emptyFetchOption);
329        for (let i = 0; i < photoFetchResult.getCount(); i++) {
330          let photoAsset = await photoFetchResult.getObjectByPosition(i);
331          if (photoAsset.uri === uri) {
332            fileAsset = photoAsset;
333            Log.info(TAG, 'getMediaItemByUriFromTrash success: ' + photoAsset.displayName);
334            break;
335          }
336        }
337      } catch (err) {
338        Log.info(TAG, 'getMediaItemByUriFromTrash getPhotoAssets failed with err: ' + err);
339      } finally {
340        if (photoFetchResult != null) {
341          photoFetchResult.close();
342        }
343      }
344    } catch (err) {
345      Log.error(TAG, 'getMediaItemByUriFromTrash failed with error: ' + err);
346    } finally {
347      if (albumFetchResult != null) {
348        albumFetchResult.close();
349      }
350    }
351    return fileAsset;
352  }
353
354  async getMediaItemByUri(uri: string): Promise<photoAccessHelper.PhotoAsset> {
355    Log.info(TAG, 'getMediaItemByUri');
356    Log.info(TAG, 'uri:' + uri);
357    let predicates = new dataSharePredicates.DataSharePredicates();
358    predicates.equalTo(photoAccessHelper.PhotoKeys.URI, uri);
359    let fetchOptions = {
360      fetchColumns: MediaConstants.FILE_ASSET_FETCH_COLUMNS,
361      predicates: predicates
362    };
363    let fileAsset: photoAccessHelper.PhotoAsset = null;
364    let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = null
365    try {
366      Log.info(TAG, 'fetchResult start');
367      fetchResult = await this.userFileMgr.getAssets(fetchOptions);
368      Log.info(TAG, 'fetchResult count ' + fetchResult.getCount());
369      fileAsset = await fetchResult.getFirstObject();
370      Log.info(TAG, 'fileAsset ' + fileAsset.displayName);
371    } catch (err) {
372      Log.error(TAG, 'getMediaItemByUri failed, message = ' + err);
373    } finally {
374      if (fetchResult != null) {
375        fetchResult.close();
376      }
377    }
378    return fileAsset;
379  }
380
381  async getMediaItemCountsByDisplayName(displayName: string): Promise<number> {
382    Log.info(TAG, 'getMediaItemCountsByDisplayName');
383    let count = 0;
384    let fetchFileResult: photoAccessHelper.FetchResult = null;
385    let predicates = new dataSharePredicates.DataSharePredicates();
386    predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, displayName);
387    let fetchOptions = {
388      fetchColumns: MediaConstants.FILE_ASSET_FETCH_COLUMNS,
389      predicates: predicates
390    };
391    try {
392      fetchFileResult = await this.userFileMgr.getAssets(fetchOptions);
393      Log.debug(TAG, 'getMediaItemCountsByDisplayName getPhotoAssets');
394      count = await fetchFileResult.getCount();
395    } catch (err) {
396      Log.error(TAG, 'getMediaItemCountsByDisplayName error:' + JSON.stringify(err));
397    } finally {
398      if (fetchFileResult != null) {
399        fetchFileResult.close();
400      }
401    }
402    Log.debug(TAG, 'getMediaItemCountsByDisplayName finish');
403    return count;
404  }
405
406  async getUserAlbumCountByName(albumName: string): Promise<number> {
407    Log.info(TAG, 'getUserAlbumCountByName');
408    Log.info(TAG, 'album_name:' + albumName);
409    let count = 0;
410    let systemAlbums = await getSystemAlbumDisplayName();
411    if (systemAlbums.indexOf(albumName) >= 0) {
412      return 1;
413    }
414    let fetchResult: photoAccessHelper.FetchResult = null;
415    try {
416      let predicates = new dataSharePredicates.DataSharePredicates();
417      predicates.equalTo(photoAccessHelper.AlbumKeys.ALBUM_NAME, albumName);
418      let fetchOptions = {
419        fetchColumns: MediaConstants.EMPTY_FETCH_COLUMNS,
420        predicates: predicates
421      };
422      fetchResult = await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
423      Log.info(TAG, 'fetchResult' + fetchResult.getCount());
424      count = fetchResult.getCount();
425    } catch (err) {
426      Log.error(TAG, 'getUserAlbumCountByName error:' + JSON.stringify(err));
427    } finally {
428      if (fetchResult != null) {
429        fetchResult.close();
430      }
431    }
432    Log.debug(TAG, 'getUserAlbumCountByName finish');
433    return count;
434  }
435
436  async getActivePeers(): Promise<photoAccessHelper.PeerInfo[]> {
437    Log.info(TAG, 'getActivePeers');
438    let peers: photoAccessHelper.PeerInfo[] = [];
439    try {
440      let getActivePeers = hiSysEventDataQueryTimedOut('getActivePeers')
441      peers = await this.userFileMgr.getActivePeers();
442      clearTimeout(getActivePeers);
443    } catch (err) {
444      Log.error(TAG, 'getActivePeers error:' + JSON.stringify(err));
445    }
446    Log.debug(TAG, 'getActivePeers finish');
447    return peers;
448  }
449
450  async getUserAlbumByName(albumName: string): Promise<photoAccessHelper.Album> {
451    Log.info(TAG, 'getUserAlbumByName');
452    Log.info(TAG, 'album_name' + albumName);
453    let fetchResult: photoAccessHelper.FetchResult = null;
454    let album = null;
455    try {
456      let predicates = new dataSharePredicates.DataSharePredicates();
457      predicates.equalTo(photoAccessHelper.AlbumKeys.ALBUM_NAME, albumName);
458      let fetchOptions = {
459        fetchColumns: MediaConstants.EMPTY_FETCH_COLUMNS,
460        predicates: predicates
461      };
462      fetchResult = await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
463      Log.info(TAG, 'fetchResult' + fetchResult.getCount());
464      if (fetchResult.getCount() > 0) {
465        album = fetchResult.getFirstObject();
466      }
467    } catch (err) {
468      Log.error(TAG, 'getUserAlbumByName error:' + JSON.stringify(err));
469    } finally {
470      if (fetchResult != null) {
471        fetchResult.close();
472      }
473    }
474    Log.debug(TAG, 'getUserAlbumByName finish');
475    return album;
476  }
477
478  async getAlbums(fetchOption: photoAccessHelper.FetchOptions): Promise<photoAccessHelper.Album[]> {
479    Log.info(TAG, 'getAlbums');
480    let albums: photoAccessHelper.Album[] = [];
481    try {
482      albums = await this.userFileMgr.getAlbums(fetchOption);
483      Log.info(TAG, 'getAlbums albums ' + albums.getCounts());
484    } catch (err) {
485      Log.error(TAG, 'getAlbums error:' + JSON.stringify(err));
486    }
487    Log.debug(TAG, 'getAlbums finish');
488    return albums;
489  }
490
491  async openAsset(mode: string, fileAsset: photoAccessHelper.PhotoAsset): Promise<number> {
492    Log.debug(TAG, 'openAsset start');
493    let fd: number = await fileAsset.open(mode);
494    Log.info(TAG, 'openAsset end. fd: ' + fd);
495    if (fd <= 0) {
496      Log.info(TAG, 'openAsset Fail');
497    }
498    return fd;
499  }
500
501  async closeAsset(fd: number, fileAsset: photoAccessHelper.PhotoAsset): Promise<void> {
502    Log.debug(TAG, 'closeAsset start');
503    await fileAsset.close(fd);
504  }
505
506  async addPhotoToAlbumByUserFileMgr(albumUri: string, uri: string): Promise<void> {
507    Log.info(TAG, 'addPhotoAssetsDemoPromise');
508    Log.info(TAG, 'albumUri' + albumUri);
509    Log.info(TAG, 'mediaItem.uri' + uri);
510    try {
511      let album = await this.getUserAlbumItemByUri(albumUri);
512      let predicates = new dataSharePredicates.DataSharePredicates();
513      predicates.equalTo(photoAccessHelper.PhotoKeys.URI, uri);
514      let fetchOptions = {
515        fetchColumns: MediaConstants.FILE_ASSET_FETCH_COLUMNS,
516        predicates: predicates
517      };
518      this.userFileMgr.getAssets(fetchOptions, async (err, fetchResult) => {
519        if (fetchResult != undefined) {
520          Log.info(TAG, 'fetchResult success');
521          let fileAsset = await fetchResult.getFirstObject();
522          if (fileAsset != undefined) {
523            Log.info(TAG, 'addPhotoToAlbumByUserFileMgr photo displayName : ' + fileAsset.displayName);
524            album.addAssets([fileAsset]).then(() => {
525              Log.info(TAG, 'album addPhotoAssets successfully');
526            }).catch((err) => {
527              Log.info(TAG, 'album addPhotoAssets failed with error: ' + err);
528            });
529          }
530          fetchResult.close();
531        } else {
532          Log.info(TAG, 'fetchResult fail' + err);
533        }
534      });
535    } catch (err) {
536      Log.error(TAG, 'addPhotoAssetsDemoPromise failed with error: ' + err);
537    }
538  }
539
540  async hideSensitives(type: number, uris: string[]): Promise<void> {
541    let flags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION;
542    let bundleInfo = bundleManager.getBundleInfoForSelfSync(flags);
543    let tokenId = bundleInfo.appInfo.accessTokenId;
544    for (let uri of uris) {
545      try {
546        let grantResult = await this.userFileMgr.grantPhotoUriPermission(tokenId, uri, 1, type);
547        Log.info(TAG, `grantPhotoUriPermission success, result: ${grantResult}`);
548      }catch (err) {
549        Log.error(TAG, 'grantPhotoUriPermission failed with error: ' + JSON.stringify(err));
550      }
551    }
552  }
553}
554
555export let userFileModel: UserFileModel = stashOrGetObject<UserFileModel>(new UserFileModel(), TAG);
556