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_operations.h"
17
18 using namespace std;
19 using namespace OHOS::NativeRdb;
20
21 namespace OHOS {
22 namespace Media {
CurrentTimeMillis()23 static int64_t CurrentTimeMillis()
24 {
25 auto now = std::chrono::system_clock::now();
26 auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
27 return static_cast<int64_t>(millisecs.count());
28 }
29
MediaLibraryDeviceOperations()30 MediaLibraryDeviceOperations::MediaLibraryDeviceOperations()
31 {
32 MEDIA_INFO_LOG("MediaLibraryDeviceOperations::MediaLibraryDeviceOperations create");
33 }
34
InsertDeviceInfo(const std::shared_ptr<NativeRdb::RdbStore> & rdbStore,const OHOS::Media::MediaLibraryDeviceInfo & deviceInfo,const std::string & bundleName)35 bool MediaLibraryDeviceOperations::InsertDeviceInfo(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore,
36 const OHOS::Media::MediaLibraryDeviceInfo &deviceInfo, const std::string &bundleName)
37 {
38 unique_ptr<AbsSharedResultSet> queryResultSet;
39 std::vector<std::string> columns;
40 AbsRdbPredicates mediaLibAbsPredDevice(DEVICE_TABLE);
41 MediaLibraryDeviceDb mediaLibraryDeviceDb;
42 std::string strQueryCondition =
43 DEVICE_DB_DEVICEID + "= '" + deviceInfo.deviceUdid + "'";
44 MEDIA_INFO_LOG("MediaLibraryDeviceOperations::InsertDeviceInfo deviceId");
45 mediaLibAbsPredDevice.SetWhereClause(strQueryCondition);
46 queryResultSet = rdbStore->Query(mediaLibAbsPredDevice, columns);
47
48 auto count = 0;
49 auto ret = queryResultSet->GetRowCount(count);
50 MEDIA_INFO_LOG("MediaLibraryDeviceOperations::InsertDeviceInfo ret = %{public}d, count = %{public}d", ret, count);
51 if (ret == NativeRdb::E_OK) {
52 if (count > 0) {
53 // 更新数据库
54 ValuesBucket valuesBucket;
55 valuesBucket.PutString(DEVICE_DB_DEVICEID, deviceInfo.deviceUdid);
56 valuesBucket.PutString(DEVICE_DB_NETWORK_ID, deviceInfo.deviceId);
57 valuesBucket.PutInt(DEVICE_DB_SYNC_STATUS, 0);
58 valuesBucket.PutLong(DEVICE_DB_DATE_MODIFIED, 0);
59 MEDIA_INFO_LOG("MediaLibraryDeviceOperations::InsertDeviceInfo UpdateDeviceInfo");
60 return mediaLibraryDeviceDb.UpdateDeviceInfo(valuesBucket, rdbStore) == DATA_ABILITY_SUCCESS;
61 } else {
62 // 插入数据库
63 int64_t now = CurrentTimeMillis();
64 ValuesBucket valuesBucket;
65 valuesBucket.PutString(DEVICE_DB_DEVICEID, deviceInfo.deviceUdid);
66 valuesBucket.PutString(DEVICE_DB_NETWORK_ID, deviceInfo.deviceId);
67 valuesBucket.PutString(DEVICE_DB_NAME, deviceInfo.deviceName);
68 valuesBucket.PutString(DEVICE_DB_IP, std::string());
69 valuesBucket.PutString(DEVICE_DB_SELF_ID, deviceInfo.selfId);
70 valuesBucket.PutInt(DEVICE_DB_TYPE, (int32_t) deviceInfo.deviceTypeId);
71 valuesBucket.PutString(DEVICE_DB_PREPATH, std::string());
72 valuesBucket.PutLong(DEVICE_DB_DATE_ADDED, now);
73 MEDIA_INFO_LOG("MediaLibraryDeviceOperations::InsertDeviceInfo InsertDeviceInfo");
74 return mediaLibraryDeviceDb.InsertDeviceInfo(valuesBucket, rdbStore) >= 0;
75 }
76 }
77 return false;
78 }
79
UpdateDeviceInfo(const std::shared_ptr<NativeRdb::RdbStore> & rdbStore,const OHOS::Media::MediaLibraryDeviceInfo & deviceInfo,const std::string & bundleName)80 bool MediaLibraryDeviceOperations::UpdateDeviceInfo(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore,
81 const OHOS::Media::MediaLibraryDeviceInfo &deviceInfo, const std::string &bundleName)
82 {
83 unique_ptr<AbsSharedResultSet> queryResultSet;
84 std::vector<std::string> columns;
85 AbsRdbPredicates mediaLibAbsPredDevice(DEVICE_TABLE);
86 MediaLibraryDeviceDb mediaLibraryDeviceDb;
87 std::string strQueryCondition =
88 DEVICE_DB_DEVICEID + " = '" + deviceInfo.deviceUdid + "'";
89 MEDIA_INFO_LOG("MediaLibraryDeviceOperations::UpdateDeviceInfo deviceId");
90 mediaLibAbsPredDevice.SetWhereClause(strQueryCondition);
91 queryResultSet = rdbStore->Query(mediaLibAbsPredDevice, columns);
92
93 auto count = 0;
94 auto ret = queryResultSet->GetRowCount(count);
95 if (ret == NativeRdb::E_OK) {
96 if (count > 0) {
97 int64_t now = CurrentTimeMillis();
98 // 更新数据库
99 ValuesBucket valuesBucket;
100 valuesBucket.PutString(DEVICE_DB_DEVICEID, deviceInfo.deviceUdid);
101 valuesBucket.PutString(DEVICE_DB_NETWORK_ID, "");
102 valuesBucket.PutString(DEVICE_DB_SELF_ID, deviceInfo.selfId);
103 valuesBucket.PutLong(DEVICE_DB_DATE_MODIFIED, now);
104 MEDIA_INFO_LOG("MediaLibraryDeviceOperations::UpdateDeviceInfo UpdateDeviceInfo");
105 return mediaLibraryDeviceDb.UpdateDeviceInfo(valuesBucket, rdbStore) == DATA_ABILITY_SUCCESS;
106 }
107 }
108 return false;
109 }
110
DeleteDeviceInfo(const std::shared_ptr<NativeRdb::RdbStore> & rdbStore,const std::string & deviceId)111 bool MediaLibraryDeviceOperations::DeleteDeviceInfo(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore,
112 const std::string &deviceId)
113 {
114 MediaLibraryDeviceDb mediaLibraryDeviceDb;
115 return mediaLibraryDeviceDb.DeleteDeviceInfo(deviceId, rdbStore);
116 }
117
UpdateSyncStatus(const std::shared_ptr<NativeRdb::RdbStore> & rdbStore,const std::string & deviceId,int32_t syncStatus,const std::string & bundleName)118 bool MediaLibraryDeviceOperations::UpdateSyncStatus(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore,
119 const std::string &deviceId, int32_t syncStatus, const std::string &bundleName)
120 {
121 unique_ptr<AbsSharedResultSet> queryResultSet;
122 std::vector<std::string> columns;
123 AbsRdbPredicates mediaLibAbsPredDevice(DEVICE_TABLE);
124 MediaLibraryDeviceDb mediaLibraryDeviceDb;
125
126 std::string strQueryCondition = DEVICE_DB_DEVICEID + " = '" + deviceId + "'";
127 mediaLibAbsPredDevice.SetWhereClause(strQueryCondition);
128 queryResultSet = rdbStore->Query(mediaLibAbsPredDevice, columns);
129
130 auto count = 0;
131 auto ret = queryResultSet->GetRowCount(count);
132 if (ret == NativeRdb::E_OK) {
133 if (count > 0) {
134 // 更新数据库
135 ValuesBucket valuesBucket;
136 valuesBucket.PutString(DEVICE_DB_DEVICEID, deviceId);
137 valuesBucket.PutInt(DEVICE_DB_SYNC_STATUS, syncStatus);
138 MEDIA_INFO_LOG("MediaLibraryDeviceOperations::UpdateSyncStatus");
139 return mediaLibraryDeviceDb.UpdateDeviceInfo(valuesBucket, rdbStore) == DATA_ABILITY_SUCCESS;
140 }
141 }
142 return false;
143 }
144
GetSyncStatusById(const std::shared_ptr<NativeRdb::RdbStore> & rdbStore,const std::string & deviceId,int32_t & syncStatus,const std::string & bundleName)145 bool MediaLibraryDeviceOperations::GetSyncStatusById(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore,
146 const std::string &deviceId,
147 int32_t &syncStatus, const std::string &bundleName)
148 {
149 unique_ptr<AbsSharedResultSet> queryResultSet;
150 std::vector<std::string> columns;
151 AbsRdbPredicates mediaLibAbsPredDevice(DEVICE_TABLE);
152
153 std::string strQueryCondition = DEVICE_DB_DEVICEID + " = '" + deviceId + "'";
154 mediaLibAbsPredDevice.SetWhereClause(strQueryCondition);
155 queryResultSet = rdbStore->Query(mediaLibAbsPredDevice, columns);
156 if (queryResultSet == nullptr) {
157 return false;
158 }
159
160 if (queryResultSet->GoToNextRow() == NativeRdb::E_OK) {
161 int32_t columnIndexId;
162 queryResultSet->GetColumnIndex(DEVICE_DB_SYNC_STATUS, columnIndexId);
163 queryResultSet->GetInt(columnIndexId, syncStatus);
164 }
165 MEDIA_INFO_LOG("MediaLibraryDeviceOperations::GetSyncStatusById syncStatus = %{public}d", syncStatus);
166 return true;
167 }
168
QueryDeviceTable(const std::shared_ptr<NativeRdb::RdbStore> & rdbStore,std::map<std::string,std::set<int>> & excludeMap)169 bool MediaLibraryDeviceOperations::QueryDeviceTable(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore,
170 std::map<std::string, std::set<int>> &excludeMap)
171 {
172 const int SHORT_UDID_LEN = 8;
173 unique_ptr<AbsSharedResultSet> queryResultSet;
174 std::vector<std::string> columns;
175 AbsRdbPredicates mediaLibAbsPredDevice(DEVICE_TABLE);
176 queryResultSet = rdbStore->Query(mediaLibAbsPredDevice, columns);
177 if (queryResultSet == nullptr) {
178 MEDIA_ERR_LOG("MediaLibraryDeviceOperations::QueryDeviceTable fail");
179 return false;
180 }
181
182 if (queryResultSet->GoToNextRow() == NativeRdb::E_OK) {
183 int32_t columnIndexId;
184 std::string selfId;
185 queryResultSet->GetColumnIndex(DEVICE_DB_SELF_ID, columnIndexId);
186 queryResultSet->GetString(columnIndexId, selfId);
187 if (selfId.length() > SHORT_UDID_LEN) {
188 std::string shortUdid = selfId.substr(0, SHORT_UDID_LEN);
189 std::string randNumber = selfId.substr(SHORT_UDID_LEN);
190 if (randNumber.length() > 0) {
191 auto &data = excludeMap[shortUdid];
192 data.insert(atoi(randNumber.c_str()));
193 }
194 }
195 }
196 return true;
197 }
198
GetValFromColumn(string columnName,shared_ptr<AbsSharedResultSet> & resultSet)199 variant<int32_t, string> GetValFromColumn(string columnName,
200 shared_ptr<AbsSharedResultSet> &resultSet)
201 {
202 int32_t index;
203 variant<int32_t, string> cellValue;
204 ColumnType type;
205 int32_t integerVal;
206 string stringVal;
207
208 resultSet->GetColumnIndex(columnName, index);
209 resultSet->GetColumnType(index, type);
210 switch (type) {
211 case ColumnType::TYPE_STRING:
212 resultSet->GetString(index, stringVal);
213 cellValue = stringVal;
214 break;
215 case ColumnType::TYPE_INTEGER:
216 resultSet->GetInt(index, integerVal);
217 cellValue = integerVal;
218 break;
219 default:
220 break;
221 }
222
223 return cellValue;
224 }
225
GetAllDeviceDatas(const std::shared_ptr<NativeRdb::RdbStore> & rdbStore,vector<MediaLibraryDeviceInfo> & outDeviceList)226 bool MediaLibraryDeviceOperations::GetAllDeviceDatas(
227 const std::shared_ptr<NativeRdb::RdbStore> &rdbStore,
228 vector<MediaLibraryDeviceInfo> &outDeviceList)
229 {
230 shared_ptr<AbsSharedResultSet> queryResultSet;
231 std::vector<std::string> columns;
232 AbsRdbPredicates mediaLibAbsPredDevice(DEVICE_TABLE);
233 queryResultSet = rdbStore->Query(mediaLibAbsPredDevice, columns);
234 if (queryResultSet == nullptr) {
235 MEDIA_ERR_LOG("MediaLibraryDeviceOperations::GetAllDeviceDatas fail");
236 return false;
237 }
238
239 while (queryResultSet->GoToNextRow() == NativeRdb::E_OK) {
240 MediaLibraryDeviceInfo deviceInfo;
241 deviceInfo.deviceId = get<string>(GetValFromColumn(DEVICE_DB_NETWORK_ID, queryResultSet));
242 deviceInfo.deviceName = get<string>(GetValFromColumn(DEVICE_DB_NAME, queryResultSet));
243 deviceInfo.deviceTypeId =
244 (DistributedHardware::DmDeviceType)(get<int32_t>(GetValFromColumn(DEVICE_DB_TYPE, queryResultSet)));
245 deviceInfo.deviceUdid = get<string>(GetValFromColumn(DEVICE_DB_DEVICEID, queryResultSet));
246 deviceInfo.selfId = get<string>(GetValFromColumn(DEVICE_DB_SELF_ID, queryResultSet));
247 outDeviceList.push_back(deviceInfo);
248 }
249 return true;
250 }
251 } // namespace Media
252 } // namespace OHOS
253