1/* 2 * Copyright (c) 2022 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 */ 15 16import createOrGet from '../utils/SingleInstanceUtils'; 17import { Log } from '../utils/Log'; 18import { MediaConstants } from '../constants/MediaConstants'; 19import mediaModel from '../model/MediaModel'; 20import type { MediaObserverCallback } from '../interface/MediaObserverCallback'; 21 22const TAG = "MediaObserver" 23 24class MediaObserver { 25 callbacks: MediaObserverCallback[] = []; 26 private static readonly OBSERVER_IMAGE_CHANGE: string = 'imageChange'; 27 private static readonly OBSERVER_VIDEO_CHANGE: string = 'videoChange'; 28 private static readonly OBSERVER_DEVICE_CHANGE: string = 'deviceChange'; 29 private static readonly OBSERVER_ALBUM_CHANGE: string = 'albumChange'; 30 private static readonly OBSERVER_REMOTE_FILE_CHANGE: string = 'remoteFileChange'; 31 32 registerObserver(callback: MediaObserverCallback) { 33 Log.info(TAG, 'registerObserver'); 34 if (callback == null) { 35 Log.warn(TAG, 'registerObserver with empty callback'); 36 return; 37 } 38 39 if (this.callbacks.indexOf(callback) < 0) { 40 this.callbacks.push(callback); 41 } else { 42 Log.info(TAG, 'registerObserver already exist'); 43 return; 44 } 45 46 if (this.callbacks.length == 1) { 47 Log.info(TAG, 'registerObserver register media'); 48 try { 49 mediaModel.getMediaLibrary().on('imageChange', () => { 50 Log.info(TAG, 'registerObserver on image'); 51 this.sendNotify(MediaConstants.MEDIA_TYPE_IMAGE); 52 }) 53 mediaModel.getMediaLibrary().on('videoChange', () => { 54 Log.info(TAG, 'registerObserver on video'); 55 this.sendNotify(MediaConstants.MEDIA_TYPE_VIDEO); 56 }) 57 mediaModel.getMediaLibrary().on('deviceChange', () => { 58 Log.info(TAG, 'registerObserver on device'); 59 this.sendNotify(MediaConstants.MEDIA_TYPE_DEVICE); 60 }) 61 mediaModel.getMediaLibrary().on('albumChange', () => { 62 Log.info(TAG, 'registerObserver on album'); 63 this.sendNotify(MediaConstants.MEDIA_TYPE_ALBUM); 64 }) 65 mediaModel.getMediaLibrary().on('remoteFileChange', () => { 66 Log.info(TAG, 'registerObserver on remoteFile'); 67 this.sendNotify(MediaConstants.MEDIA_TYPE_REMOTE); 68 }) 69 } catch (err) { 70 Log.error(TAG, `registerObserver faild, err: ${JSON.stringify(err)}`); 71 } 72 } 73 } 74 75 sendNotify(mediaType: string) { 76 Log.info(TAG, `registerObserver sendNotify size: ${this.callbacks.length}`); 77 for (let callback of this.callbacks) { 78 callback.onChange(mediaType); 79 } 80 } 81 82 unregisterObserver(callback: MediaObserverCallback) { 83 Log.info(TAG, 'unregisterObserver'); 84 const pos = this.callbacks.indexOf(callback); 85 if (pos >= 0) { 86 this.callbacks.splice(pos, 1); 87 } 88 } 89} 90 91let mediaObserver = createOrGet(MediaObserver, TAG); 92 93export default mediaObserver as MediaObserver;