1 /*
2  * Copyright (C) 2024 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 
16 #ifndef FRAMEWORKS_SERVICES_MEDIA_MTP_INCLUDE_PTP_MEDIA_SYNC_OBSERVER_H_
17 #define FRAMEWORKS_SERVICES_MEDIA_MTP_INCLUDE_PTP_MEDIA_SYNC_OBSERVER_H_
18 
19 #include <condition_variable>
20 #include <ios>
21 #include <mutex>
22 #include <vector>
23 
24 #include "datashare_helper.h"
25 #include "mtp_constants.h"
26 #include "media_column.h"
27 #include "mtp_packet_tools.h"
28 #include "mtp_operation_context.h"
29 #include "media_mtp_utils.h"
30 #include "medialibrary_async_worker.h"
31 #include "property.h"
32 #include "userfilemgr_uri.h"
33 
34 namespace OHOS {
35 namespace Media {
36 
37 struct MediaSyncNotifyInfo {
38     std::list<Uri> uris;
39     DataShare::DataShareObserver::ChangeType type;
40 };
41 
42 class MediaSyncObserver : public DataShare::DataShareObserver {
43 public:
44     MediaSyncObserver() = default;
45     ~MediaSyncObserver() = default;
46 
47     void OnChange(const ChangeInfo &changeInfo) override;
48     std::shared_ptr<MtpOperationContext> context_ = nullptr;
49     std::shared_ptr<DataShare::DataShareHelper> dataShareHelper_ = nullptr;
50     void OnChangeEx(const ChangeInfo &changeInfo);
51     void StartNotifyThread();
52     void StopNotifyThread();
53     void ChangeNotifyThread();
54 private:
55     void SendEventPackets(uint32_t objectHandle, uint16_t eventCode);
56     void SendEventPacketAlbum(uint32_t objectHandle, uint16_t eventCode);
57     void SendPhotoEvent(ChangeType changeType, std::string suffixString);
58     std::vector<int32_t> GetHandlesFromPhotosInfoBurstKeys(std::vector<std::string> &handle);
59     void SendEventToPTP(int32_t suff_int, ChangeType changeType);
60     std::vector<std::string> GetAllDeleteHandles();
61     std::shared_ptr<DataShare::DataShareResultSet> GetAlbumInfo();
62     void GetAddEditPhotoHandles(int32_t handle);
63     int32_t GetAddEditAlbumHandle(int32_t handle);
64     void AddPhotoHandle(int32_t handle);
65     void SendPhotoRemoveEvent(std::string &suffixString);
66 private:
67     std::thread notifythread_;
68     std::queue<ChangeInfo> changeInfoQueue_;
69     std::mutex mutex_;
70     std::condition_variable cv_;
71     std::atomic<bool> isRunning_ {false};
72 };
73 
74 class MediaSyncNotifyData : public AsyncTaskData {
75 public:
MediaSyncNotifyData(const MediaSyncNotifyInfo & info)76     MediaSyncNotifyData(const MediaSyncNotifyInfo &info):notifyInfo_(info) {};
77     virtual ~MediaSyncNotifyData() override = default;
78     MediaSyncNotifyInfo notifyInfo_;
79 };
80 } // namespace Media
81 } // namespace OHOS
82 
83 #endif //FRAMEWORKS_SERVICES_MEDIA_MTP_INCLUDE_PTP_MEDIA_SYNC_OBSERVER_H_
84