• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #include "medialibrary_device.h"
17 
18 namespace OHOS {
19 namespace Media {
20 using namespace std;
21 using namespace OHOS::AppExecFwk;
22 
MediaLibraryDevice()23 MediaLibraryDevice::MediaLibraryDevice()
24 {
25     if (mediaLibraryDeviceOperations_ == nullptr) {
26         mediaLibraryDeviceOperations_ = std::make_unique<MediaLibraryDeviceOperations>();
27     }
28 
29     if (mediaLibraryDeviceHandler_ == nullptr) {
30         auto runner = AppExecFwk::EventRunner::Create("MediaLibraryDevice");
31         mediaLibraryDeviceHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
32     }
33 }
34 
~MediaLibraryDevice()35 MediaLibraryDevice::~MediaLibraryDevice()
36 {
37     mediaLibraryDeviceOperations_ = nullptr;
38     dataAbilityhelper_ = nullptr;
39 }
40 
GetInstance()41 MediaLibraryDevice *MediaLibraryDevice::GetInstance()
42 {
43     static MediaLibraryDevice mediaLibraryDevice;
44     return &mediaLibraryDevice;
45 }
46 
SetAbilityContext(const std::shared_ptr<Context> & context)47 void MediaLibraryDevice::SetAbilityContext(const std::shared_ptr<Context> &context)
48 {
49     dataAbilityhelper_ = OHOS::AppExecFwk::DataAbilityHelper::Creator(context);
50     MEDIA_INFO_LOG("MediaLibraryDevice::SetAbilityContext create dataAbilityhelper %{public}d",
51         (dataAbilityhelper_ != nullptr));
52 }
53 
GetAllDeviceId(std::vector<OHOS::DistributedHardware::DmDeviceInfo> & deviceList,std::string & bundleName)54 void MediaLibraryDevice::GetAllDeviceId(
55     std::vector<OHOS::DistributedHardware::DmDeviceInfo> &deviceList, std::string &bundleName)
56 {
57     MEDIA_INFO_LOG("MediaLibraryDevice::GetAllDeviceId IN");
58     std::string extra = "";
59     auto &deviceManager = OHOS::DistributedHardware::DeviceManager::GetInstance();
60     deviceManager.GetTrustedDeviceList(bundleName, extra, deviceList);
61     MEDIA_INFO_LOG("MediaLibraryDevice::GetAllDeviceId OUT");
62 }
63 
OnDeviceOnline(const OHOS::DistributedHardware::DmDeviceInfo & deviceInfo,const std::string & bundleName)64 void MediaLibraryDevice::OnDeviceOnline(
65     const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo, const std::string &bundleName)
66 {
67     MEDIA_INFO_LOG("OnDeviceOnline deviceId");
68 
69     if (mediaLibraryDeviceHandler_ == nullptr) {
70         MEDIA_ERR_LOG("OnDeviceOnline mediaLibraryDeviceHandler null");
71         return;
72     }
73     auto nodeOnline = [this, deviceInfo, bundleName]() {
74         // 更新数据库
75         if (mediaLibraryDeviceOperations_ != nullptr) {
76             OHOS::Media::MediaLibraryDeviceInfo mediaLibraryDeviceInfo;
77             GetMediaLibraryDeviceInfo(deviceInfo, mediaLibraryDeviceInfo, bundleName);
78             if (!mediaLibraryDeviceOperations_->InsertDeviceInfo(rdbStore_, mediaLibraryDeviceInfo, bundleName)) {
79                 MEDIA_ERR_LOG("OnDeviceOnline InsertDeviceInfo failed!");
80                 return;
81             }
82             lock_guard<mutex> autoLock(deviceLock_);
83             deviceInfoMap_[deviceInfo.deviceId] = mediaLibraryDeviceInfo;
84         } else {
85             MEDIA_ERR_LOG("OnDeviceOnline InsertDeviceInfo failed mediaLibraryDeviceOperations_ = null !");
86             return;
87         }
88         // 设备变更通知
89         NotifyDeviceChange();
90     };
91     if (!mediaLibraryDeviceHandler_->PostTask(nodeOnline)) {
92         MEDIA_ERR_LOG("OnDeviceOnline handler postTask failed");
93     }
94 }
95 
OnDeviceOffline(const OHOS::DistributedHardware::DmDeviceInfo & deviceInfo,const std::string & bundleName)96 void MediaLibraryDevice::OnDeviceOffline(
97     const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo, const std::string &bundleName)
98 {
99     MEDIA_INFO_LOG("OnDeviceOffline deviceId");
100 
101     if (mediaLibraryDeviceHandler_ == nullptr) {
102         MEDIA_ERR_LOG("OnDeviceOffline mediaLibraryDeviceHandler null");
103         return;
104     }
105     auto nodeOffline = [this, deviceInfo, bundleName]() {
106         lock_guard<mutex> autoLock(deviceLock_);
107         std::string devId = deviceInfo.deviceId;
108         auto info = deviceInfoMap_.find(devId);
109         if (info == deviceInfoMap_.end()) {
110             MEDIA_ERR_LOG("OnDeviceOffline can not find deviceId");
111             return;
112         }
113         if (mediaLibraryDeviceOperations_ != nullptr) {
114             mediaLibraryDeviceOperations_->UpdateDeviceInfo(rdbStore_, info->second, bundleName);
115         }
116         deviceInfoMap_.erase(devId);
117 
118         // 设备变更通知
119         NotifyDeviceChange();
120     };
121     if (!mediaLibraryDeviceHandler_->PostTask(nodeOffline)) {
122         MEDIA_ERR_LOG("OnDeviceOffline handler postTask failed");
123     }
124 }
125 
OnDeviceChanged(const OHOS::DistributedHardware::DmDeviceInfo & deviceInfo)126 void MediaLibraryDevice::OnDeviceChanged(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo)
127 {
128     MEDIA_INFO_LOG("MediaLibraryDevice OnDeviceChanged called deviceId");
129 }
130 
OnDeviceReady(const OHOS::DistributedHardware::DmDeviceInfo & deviceInfo)131 void MediaLibraryDevice::OnDeviceReady(const OHOS::DistributedHardware::DmDeviceInfo &deviceInfo)
132 {
133     MEDIA_INFO_LOG("MediaLibraryDevice OnDeviceReady called deviceId");
134 }
135 
ClearAllDevices()136 void MediaLibraryDevice::ClearAllDevices()
137 {
138     lock_guard<mutex> autoLock(deviceLock_);
139     deviceInfoMap_.clear();
140     excludeMap_.clear();
141 }
142 
NotifyDeviceChange()143 void MediaLibraryDevice::NotifyDeviceChange()
144 {
145     auto contextUri = make_unique<Uri>(MEDIALIBRARY_DEVICE_URI);
146     if (dataAbilityhelper_ != nullptr) {
147         dataAbilityhelper_->NotifyChange(*contextUri);
148         MEDIA_INFO_LOG("MediaLibraryDevice NotifyDeviceChange complete");
149     }
150 }
151 
NotifyRemoteFileChange()152 void MediaLibraryDevice::NotifyRemoteFileChange()
153 {
154     auto contextUri = make_unique<Uri>(MEDIALIBRARY_REMOTEFILE_URI);
155     if (dataAbilityhelper_ != nullptr) {
156         dataAbilityhelper_->NotifyChange(*contextUri);
157         MEDIA_INFO_LOG("MediaLibraryDevice NotifyRemoteFileChange complete");
158     }
159 }
160 
IsHasDevice(string deviceUdid)161 bool MediaLibraryDevice::IsHasDevice(string deviceUdid)
162 {
163     map<string, MediaLibraryDeviceInfo>::iterator iter =  deviceInfoMap_.begin();
164     while (iter != deviceInfoMap_.end()) {
165         if (deviceUdid.compare(iter->second.deviceUdid) == 0) {
166             return true;
167         }
168         iter++;
169     }
170     return false;
171 }
172 
InitDeviceRdbStore(const shared_ptr<NativeRdb::RdbStore> & rdbStore,std::string & bundleName)173 bool MediaLibraryDevice::InitDeviceRdbStore(const shared_ptr<NativeRdb::RdbStore> &rdbStore, std::string &bundleName)
174 {
175     MEDIA_INFO_LOG("MediaLibraryDevice InitDeviceRdbStore IN");
176     rdbStore_ = rdbStore;
177     if (!QueryDeviceTable()) {
178         MEDIA_ERR_LOG("MediaLibraryDevice InitDeviceRdbStore QueryDeviceTable fail!");
179         return false;
180     }
181     // 获取同一网络中的所有设备Id
182     std::vector<OHOS::DistributedHardware::DmDeviceInfo> deviceList;
183     GetAllDeviceId(deviceList, bundleName);
184     MEDIA_INFO_LOG("MediaLibraryDevice InitDeviceRdbStore deviceList size = %{public}d", (int) deviceList.size());
185     for (auto& deviceInfo : deviceList) {
186         OHOS::Media::MediaLibraryDeviceInfo mediaLibraryDeviceInfo;
187         GetMediaLibraryDeviceInfo(deviceInfo, mediaLibraryDeviceInfo, bundleName);
188         if (mediaLibraryDeviceOperations_ != nullptr &&
189             mediaLibraryDeviceOperations_->InsertDeviceInfo(rdbStore_, mediaLibraryDeviceInfo, bundleName)) {
190             lock_guard<mutex> autoLock(deviceLock_);
191             deviceInfoMap_[deviceInfo.deviceId] = mediaLibraryDeviceInfo;
192         }
193     }
194 
195     std::vector<OHOS::Media::MediaLibraryDeviceInfo> deviceDataBaseList;
196     mediaLibraryDeviceOperations_->GetAllDeviceDatas(rdbStore, deviceDataBaseList);
197     MEDIA_INFO_LOG("MediaLibraryDevice InitDeviceRdbStore deviceDataBaseList size = %{public}d",
198         (int) deviceDataBaseList.size());
199     for (OHOS::Media::MediaLibraryDeviceInfo deviceInfo : deviceDataBaseList) {
200         if (!IsHasDevice(deviceInfo.deviceUdid)) {
201             if (mediaLibraryDeviceOperations_ != nullptr) {
202                 mediaLibraryDeviceOperations_->UpdateDeviceInfo(rdbStore_, deviceInfo, bundleName);
203             }
204         }
205     }
206     MEDIA_INFO_LOG("MediaLibraryDevice InitDeviceRdbStore OUT deviceInfoMap size = %{public}d",
207         (int) deviceInfoMap_.size());
208     return true;
209 }
210 
UpdateDevicieSyncStatus(const std::string & deviceId,int32_t syncStatus,const std::string & bundleName)211 bool MediaLibraryDevice::UpdateDevicieSyncStatus(
212     const std::string &deviceId, int32_t syncStatus, const std::string &bundleName)
213 {
214     if (mediaLibraryDeviceOperations_ != nullptr) {
215         lock_guard<mutex> autoLock(deviceLock_);
216         auto info = deviceInfoMap_.find(deviceId);
217         if (info == deviceInfoMap_.end()) {
218             MEDIA_ERR_LOG("UpdateDevicieSyncStatus can not find deviceId");
219             return false;
220         }
221         return mediaLibraryDeviceOperations_->UpdateSyncStatus(rdbStore_, info->second.deviceUdid, syncStatus,
222                                                                bundleName);
223     }
224     return false;
225 }
226 
GetDevicieSyncStatus(const std::string & deviceId,int32_t & syncStatus,const std::string & bundleName)227 bool MediaLibraryDevice::GetDevicieSyncStatus(const std::string &deviceId, int32_t &syncStatus,
228                                               const std::string &bundleName)
229 {
230     if (mediaLibraryDeviceOperations_ != nullptr) {
231         lock_guard<mutex> autoLock(deviceLock_);
232         auto info = deviceInfoMap_.find(deviceId);
233         if (info == deviceInfoMap_.end()) {
234             MEDIA_ERR_LOG("GetDevicieSyncStatus can not find deviceId");
235             return false;
236         }
237         return mediaLibraryDeviceOperations_->GetSyncStatusById(rdbStore_, deviceId, syncStatus,
238                                                                 bundleName);
239     }
240     return false;
241 }
242 
GetUdidByNetworkId(const std::string & deviceId,const std::string & bundleName)243 std::string MediaLibraryDevice::GetUdidByNetworkId(const std::string &deviceId, const std::string &bundleName)
244 {
245     auto &deviceManager = OHOS::DistributedHardware::DeviceManager::GetInstance();
246     std::string deviceUdid;
247     auto ret = deviceManager.GetUdidByNetworkId(bundleName, deviceId, deviceUdid);
248     if (ret != 0) {
249         MEDIA_INFO_LOG("GetDeviceUdid error deviceId");
250         return std::string();
251     }
252     return deviceUdid;
253 }
254 
GetMediaLibraryDeviceInfo(const OHOS::DistributedHardware::DmDeviceInfo & dmInfo,OHOS::Media::MediaLibraryDeviceInfo & mlInfo,const std::string & bundleName)255 void MediaLibraryDevice::GetMediaLibraryDeviceInfo(const OHOS::DistributedHardware::DmDeviceInfo &dmInfo,
256                                                    OHOS::Media::MediaLibraryDeviceInfo& mlInfo,
257                                                    const std::string &bundleName)
258 {
259     mlInfo.deviceId = dmInfo.deviceId;
260     mlInfo.deviceName = dmInfo.deviceName;
261     mlInfo.deviceTypeId = dmInfo.deviceTypeId;
262     mlInfo.deviceUdid = GetUdidByNetworkId(mlInfo.deviceId, bundleName);
263 }
264 
GetNetworkIdBySelfId(const std::string & selfId,const std::string & bundleName)265 string MediaLibraryDevice::GetNetworkIdBySelfId(const std::string &selfId, const std::string &bundleName)
266 {
267     MEDIA_INFO_LOG("GetNetworkIdBySelfId can not find selfId:%{public}s", selfId.c_str());
268 
269     map<string, MediaLibraryDeviceInfo>::iterator iter = deviceInfoMap_.begin();
270     while (iter != deviceInfoMap_.end()) {
271         MEDIA_INFO_LOG("GetNetworkIdBySelfId iter->second.selfId:%{public}s", iter->second.selfId.c_str());
272         if (selfId.compare(iter->second.selfId) == 0) {
273             return iter->second.deviceId;
274         }
275         iter++;
276     }
277     return "";
278 }
279 
QueryDeviceTable()280 bool MediaLibraryDevice::QueryDeviceTable()
281 {
282     if (rdbStore_ != nullptr && mediaLibraryDeviceOperations_ != nullptr) {
283         excludeMap_.clear();
284         return mediaLibraryDeviceOperations_->QueryDeviceTable(rdbStore_, excludeMap_);
285     }
286     return false;
287 }
288 } // namespace Media
289 } // namespace OHOS
290