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 &notifyType, 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 MAX_NOTIFY_LIST_SIZE = 32;
47 constexpr size_t MNOTIFY_TIME_INTERVAL = 100;
48 class MediaLibraryNotify {
49 public:
50     EXPORT static std::shared_ptr<MediaLibraryNotify> GetInstance();
51     EXPORT virtual ~MediaLibraryNotify();
52     EXPORT int32_t Notify(
53         const std::string &uri, const NotifyType notifyType, const int albumId = 0, bool hiddenOnly = false);
54     EXPORT int32_t Notify(const std::shared_ptr<FileAsset> &closeAsset);
55     EXPORT int32_t GetAlbumIdBySubType(const PhotoAlbumSubType subType);
56     static void GetNotifyUris(const NativeRdb::AbsRdbPredicates &predicates, std::vector<std::string> &notifyUris);
57 
58     Utils::Timer timer_;
59     static uint32_t timerId_;
60     static std::mutex mutex_;
61     static std::unordered_map<std::string, std::unordered_map<NotifyType, std::list<Uri>>> nfListMap_;
62 private:
63     MediaLibraryNotify();
64     int32_t Init();
65     int32_t GetDefaultAlbums(std::unordered_map<PhotoAlbumSubType, int> &outAlbums);
66     static std::shared_ptr<MediaLibraryNotify> instance_;
67     std::unordered_map<PhotoAlbumSubType, int> defaultAlbums_;
68 };
69 
70 class NotifyTaskWorker {
71 public:
72     virtual ~NotifyTaskWorker();
GetInstance()73     static std::unique_ptr<NotifyTaskWorker>& GetInstance()
74     {
75         static std::unique_ptr<NotifyTaskWorker> instance(new NotifyTaskWorker());
76         return instance;
77     };
78     ASYNC_WORKER_API_EXPORT int32_t AddTask(const std::shared_ptr<MediaLibraryAsyncTask> &task);
79 
80 private:
81     COMPILE_HIDDEN NotifyTaskWorker();
82     COMPILE_HIDDEN void StartWorker();
83     COMPILE_HIDDEN std::shared_ptr<MediaLibraryAsyncTask> GetTask();
84     COMPILE_HIDDEN bool IsQueueEmpty();
85     COMPILE_HIDDEN void StartThread();
86 
87     COMPILE_HIDDEN std::atomic<bool> isThreadRunning_;
88 
89     COMPILE_HIDDEN std::mutex taskLock_;
90     COMPILE_HIDDEN std::queue<std::shared_ptr<MediaLibraryAsyncTask>> taskQueue_;
91 
92     COMPILE_HIDDEN bool WaitForTask();
93     COMPILE_HIDDEN std::mutex cvLock_;
94     COMPILE_HIDDEN std::condition_variable taskCv_;
95 
96     COMPILE_HIDDEN std::thread thread_;
97 };
98 } // namespace Media
99 } // namespace OHOS
100 #endif // OHOS_MEDIALIBRARY_NOTIFY_H