1 /* 2 * Copyright (C) 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 */ 15 #ifndef OHOS_MEDIALIBRARY_NOTIFY_H 16 #define OHOS_MEDIALIBRARY_NOTIFY_H 17 18 #include <cstddef> 19 #include <string> 20 #include <queue> 21 #include <thread> 22 #include <unordered_map> 23 24 #include "dataobs_mgr_client.h" 25 #include "file_asset.h" 26 #include "medialibrary_async_worker.h" 27 #include "parcel.h" 28 #include "rdb_predicates.h" 29 #include "timer.h" 30 #include "uri.h" 31 #include "userfile_manager_types.h" 32 33 namespace OHOS { 34 namespace Media { 35 #define EXPORT __attribute__ ((visibility ("default"))) 36 class NotifyTaskData : public AsyncTaskData { 37 public: NotifyTaskData(const std::string & uri,const NotifyType & notifyType,const int albumId,const bool hiddenOnly)38 NotifyTaskData(const std::string &uri, const NotifyType ¬ifyType, const int albumId, const bool hiddenOnly) 39 : uri_(std::move(uri)), notifyType_(notifyType), albumId_(albumId), hiddenOnly_(hiddenOnly) {} 40 virtual ~NotifyTaskData() override = default; 41 std::string uri_; 42 NotifyType notifyType_; 43 int albumId_; 44 bool hiddenOnly_ = false; 45 }; 46 constexpr size_t MNOTIFY_TIME_INTERVAL = 100; 47 class MediaLibraryNotify { 48 public: 49 EXPORT static std::shared_ptr<MediaLibraryNotify> GetInstance(); 50 EXPORT virtual ~MediaLibraryNotify(); 51 EXPORT int32_t Notify( 52 const std::string &uri, const NotifyType notifyType, const int albumId = 0, bool hiddenOnly = false); 53 EXPORT int32_t Notify(const std::shared_ptr<FileAsset> &closeAsset); 54 EXPORT int32_t GetAlbumIdBySubType(const PhotoAlbumSubType subType); 55 static void GetNotifyUris(const NativeRdb::AbsRdbPredicates &predicates, std::vector<std::string> ¬ifyUris); 56 57 static std::atomic<uint16_t> counts_; 58 static std::atomic<uint16_t> thumbCounts_; 59 static std::mutex mutex_; 60 static std::unordered_map<std::string, std::unordered_map<NotifyType, std::list<Uri>>> nfListMap_; 61 private: 62 MediaLibraryNotify(); 63 int32_t Init(); 64 int32_t GetDefaultAlbums(std::unordered_map<PhotoAlbumSubType, int> &outAlbums); 65 static std::shared_ptr<MediaLibraryNotify> instance_; 66 std::unordered_map<PhotoAlbumSubType, int> defaultAlbums_; 67 }; 68 69 class NotifyTaskWorker { 70 public: 71 virtual ~NotifyTaskWorker(); GetInstance()72 static std::unique_ptr<NotifyTaskWorker>& GetInstance() 73 { 74 static std::unique_ptr<NotifyTaskWorker> instance(new NotifyTaskWorker()); 75 return instance; 76 }; 77 ASYNC_WORKER_API_EXPORT int32_t AddTask(const std::shared_ptr<MediaLibraryAsyncTask> &task); 78 79 private: 80 COMPILE_HIDDEN NotifyTaskWorker(); 81 COMPILE_HIDDEN void StartWorker(); 82 COMPILE_HIDDEN std::shared_ptr<MediaLibraryAsyncTask> GetTask(); 83 COMPILE_HIDDEN bool IsQueueEmpty(); 84 COMPILE_HIDDEN void StartThread(); 85 86 COMPILE_HIDDEN std::atomic<bool> isThreadRunning_; 87 88 COMPILE_HIDDEN std::mutex taskLock_; 89 COMPILE_HIDDEN std::queue<std::shared_ptr<MediaLibraryAsyncTask>> taskQueue_; 90 91 COMPILE_HIDDEN bool WaitForTask(); 92 COMPILE_HIDDEN std::mutex cvLock_; 93 COMPILE_HIDDEN std::condition_variable taskCv_; 94 95 COMPILE_HIDDEN std::thread thread_; 96 }; 97 } // namespace Media 98 } // namespace OHOS 99 #endif // OHOS_MEDIALIBRARY_NOTIFY_H