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 { stashOrGetObject } from '../utils/SingleInstanceUtils'; 17import { Log } from '../utils/Log'; 18import { MediaConstants } from '../constants/MediaConstants'; 19import { userFileModel } from './UserFileModel'; 20import { UserFileObserverCallback } from './UserFileObserverCallback'; 21import photoAccessHelper from '@ohos.file.photoAccessHelper'; 22 23const TAG = 'UserFileObserver'; 24 25class UserFileObserver { 26 callbacks: UserFileObserverCallback[] = []; 27 private static readonly OBSERVER_IMAGE_CHANGE: string = 'imageChange'; 28 private static readonly OBSERVER_VIDEO_CHANGE: string = 'videoChange'; 29 private static readonly OBSERVER_DEVICE_CHANGE: string = 'deviceChange'; 30 private static readonly OBSERVER_ALBUM_CHANGE: string = 'albumChange'; 31 private static readonly OBSERVER_REMOTE_FILE_CHANGE: string = 'remoteFileChange'; 32 33 registerObserver(callback: UserFileObserverCallback | null): void { 34 Log.info(TAG, 'registerObserver'); 35 if (callback == null) { 36 Log.warn(TAG, 'registerObserver with empty callback'); 37 return; 38 } 39 40 if (this.callbacks.indexOf(callback) < 0) { 41 this.callbacks.push(callback); 42 } else { 43 Log.info(TAG, 'registerObserver already exist'); 44 return; 45 } 46 47 if (this.callbacks.length === 1) { 48 Log.info(TAG, 'registerObserver register media'); 49 try { 50 userFileModel.getUserFileMgr().registerChange(photoAccessHelper.DefaultChangeUri.DEFAULT_PHOTO_URI, true, (): void => { 51 Log.info(TAG, 'registerObserver on image'); 52 this.sendNotify(MediaConstants.MEDIA_TYPE_IMAGE); 53 }); 54 userFileModel.getUserFileMgr().registerChange(photoAccessHelper.DefaultChangeUri.DEFAULT_ALBUM_URI, true, (): void => { 55 Log.info(TAG, 'registerObserver on album'); 56 this.sendNotify(MediaConstants.MEDIA_TYPE_ALBUM); 57 }); 58 } catch (err) { 59 Log.error(TAG, 'registerObserver faild, err: ' + JSON.stringify(err)); 60 } 61 } 62 } 63 64 sendNotify(mediaType: string): void { 65 Log.info(TAG, 'registerObserver sendNotify size: ' + this.callbacks.length); 66 for (let callback of this.callbacks) { 67 callback.onChange(mediaType); 68 } 69 } 70 71 unregisterObserver(callback: UserFileObserverCallback | null): void { 72 Log.info(TAG, 'unregisterObserver'); 73 const pos = this.callbacks.indexOf(callback); 74 if (pos >= 0) { 75 this.callbacks.splice(pos, 1); 76 } 77 } 78} 79 80export let userFileObserver: UserFileObserver = stashOrGetObject<UserFileObserver>(new UserFileObserver(), TAG); 81