• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 #define MLOG_TAG "AccurateRefresh::MediaLibraryNotifyUtils"
17 #include "medialibrary_notify_utils.h"
18 
19 #include "media_column.h"
20 #include "medialibrary_client_errno.h"
21 #include "medialibrary_errno.h"
22 #include "medialibrary_napi_log.h"
23 #include "medialibrary_napi_utils.h"
24 #include "medialibrary_tracer.h"
25 #include "photo_album_column.h"
26 
27 using namespace std;
28 
29 namespace OHOS {
30 namespace Media {
31 const std::string RegisterNotifyType::PHOTO_CHANGE = "photoChange";
32 const std::string RegisterNotifyType::HIDDEN_PHOTO_CHANGE = "hiddenPhotoChange";
33 const std::string RegisterNotifyType::TRASH_PHOTO_CHANGE = "trashedPhotoChange";
34 const std::string RegisterNotifyType::PHOTO_ALBUM_CHANGE = "photoAlbumChange";
35 const std::string RegisterNotifyType::HIDDEN_ALBUM_CHANGE = "hiddenAlbumChange";
36 const std::string RegisterNotifyType::TRASHED_ALBUM_CHANGE = "trashedAlbumChange";
37 
38 const std::map<std::string, Notification::NotifyUriType> MediaLibraryNotifyUtils::REGISTER_NOTIFY_TYPE_MAP = {
39     { RegisterNotifyType::PHOTO_CHANGE, Notification::NotifyUriType::PHOTO_URI },
40     { RegisterNotifyType::HIDDEN_PHOTO_CHANGE, Notification::NotifyUriType::HIDDEN_PHOTO_URI },
41     { RegisterNotifyType::TRASH_PHOTO_CHANGE, Notification::NotifyUriType::TRASH_PHOTO_URI },
42     { RegisterNotifyType::PHOTO_ALBUM_CHANGE, Notification::NotifyUriType::PHOTO_ALBUM_URI },
43     { RegisterNotifyType::HIDDEN_ALBUM_CHANGE, Notification::NotifyUriType::HIDDEN_ALBUM_URI },
44     { RegisterNotifyType::TRASHED_ALBUM_CHANGE, Notification::NotifyUriType::TRASH_ALBUM_URI },
45 };
46 
47 const std::map<Notification::NotifyUriType, Notification::NotifyUriType> MediaLibraryNotifyUtils::REGISTER_TYPE_MAP = {
48     { Notification::NotifyUriType::PHOTO_URI, Notification::NotifyUriType::PHOTO_URI },
49     { Notification::NotifyUriType::HIDDEN_PHOTO_URI, Notification::NotifyUriType::HIDDEN_PHOTO_URI },
50     { Notification::NotifyUriType::TRASH_PHOTO_URI, Notification::NotifyUriType::TRASH_PHOTO_URI },
51     { Notification::NotifyUriType::PHOTO_ALBUM_URI, Notification::NotifyUriType::PHOTO_ALBUM_URI },
52     { Notification::NotifyUriType::HIDDEN_ALBUM_URI, Notification::NotifyUriType::HIDDEN_ALBUM_URI },
53     { Notification::NotifyUriType::TRASH_ALBUM_URI, Notification::NotifyUriType::TRASH_ALBUM_URI },
54 };
55 
56 const std::map<Notification::NotifyUriType, std::string> MediaLibraryNotifyUtils::REGISTER_URI_MAP = {
57     { Notification::NotifyUriType::PHOTO_URI, RegisterNotifyType::PHOTO_CHANGE },
58     { Notification::NotifyUriType::HIDDEN_PHOTO_URI, RegisterNotifyType::HIDDEN_PHOTO_CHANGE },
59     { Notification::NotifyUriType::TRASH_PHOTO_URI, RegisterNotifyType::TRASH_PHOTO_CHANGE },
60     { Notification::NotifyUriType::PHOTO_ALBUM_URI, RegisterNotifyType::PHOTO_ALBUM_CHANGE },
61     { Notification::NotifyUriType::HIDDEN_ALBUM_URI, RegisterNotifyType::HIDDEN_ALBUM_CHANGE },
62     { Notification::NotifyUriType::TRASH_ALBUM_URI, RegisterNotifyType::TRASHED_ALBUM_CHANGE },
63 };
64 
65 const std::map<Notification::NotifyType, NotifyChangeType> MediaLibraryNotifyUtils::NOTIFY_CHANGE_TYPE_MAP = {
66     { Notification::NotifyType::NOTIFY_ASSET_ADD, NotifyChangeType::NOTIFY_CHANGE_ADD },
67     { Notification::NotifyType::NOTIFY_ASSET_UPDATE, NotifyChangeType::NOTIFY_CHANGE_UPDATE },
68     { Notification::NotifyType::NOTIFY_ASSET_REMOVE, NotifyChangeType::NOTIFY_CHANGE_REMOVE },
69     { Notification::NotifyType::NOTIFY_ALBUM_ADD, NotifyChangeType::NOTIFY_CHANGE_ADD },
70     { Notification::NotifyType::NOTIFY_ALBUM_UPDATE, NotifyChangeType::NOTIFY_CHANGE_UPDATE },
71     { Notification::NotifyType::NOTIFY_ALBUM_REMOVE, NotifyChangeType::NOTIFY_CHANGE_REMOVE },
72 };
73 
74 const std::unordered_map<int32_t, int32_t> ERROR_MAP = {
75     { E_PERMISSION_DENIED,     OHOS_PERMISSION_DENIED_CODE },
76     { -E_CHECK_SYSTEMAPP_FAIL, E_CHECK_SYSTEMAPP_FAIL },
77     { JS_E_PARAM_INVALID,      JS_E_PARAM_INVALID },
78     { OHOS_INVALID_PARAM_CODE, OHOS_INVALID_PARAM_CODE },
79 };
80 
GetRegisterNotifyType(const string & type,Notification::NotifyUriType & uriType)81 int32_t MediaLibraryNotifyUtils::GetRegisterNotifyType(const string &type, Notification::NotifyUriType &uriType)
82 {
83     if (REGISTER_NOTIFY_TYPE_MAP.find(type) == REGISTER_NOTIFY_TYPE_MAP.end()) {
84         NAPI_ERR_LOG("registerNotifyType is invalid");
85         return E_ERR;
86     }
87     uriType = REGISTER_NOTIFY_TYPE_MAP.at(type);
88     return E_OK;
89 }
90 
GetNotifyTypeAndUri(const Notification::NotifyUriType type,Notification::NotifyUriType & uriType,string & uri)91 int32_t MediaLibraryNotifyUtils::GetNotifyTypeAndUri(const Notification::NotifyUriType type,
92     Notification::NotifyUriType &uriType, string &uri)
93 {
94     if (REGISTER_TYPE_MAP.find(type) == REGISTER_TYPE_MAP.end()) {
95         NAPI_ERR_LOG("type is invalid");
96         return E_ERR;
97     }
98     uriType = REGISTER_TYPE_MAP.at(type);
99     if (REGISTER_URI_MAP.find(uriType) == REGISTER_URI_MAP.end()) {
100         NAPI_ERR_LOG("uriType is invalid");
101         return E_ERR;
102     }
103     uri = REGISTER_URI_MAP.at(uriType);
104     return E_OK;
105 }
106 
GetNotifyChangeType(const Notification::NotifyType & notifyType)107 int32_t MediaLibraryNotifyUtils::GetNotifyChangeType(const Notification::NotifyType &notifyType)
108 {
109     if (NOTIFY_CHANGE_TYPE_MAP.find(notifyType) == NOTIFY_CHANGE_TYPE_MAP.end()) {
110         NAPI_ERR_LOG("notifyType is invalid");
111         return E_ERR;
112     }
113     return static_cast<int32_t>(NOTIFY_CHANGE_TYPE_MAP.at(notifyType));
114 }
115 
SetValueInt32(const napi_env & env,const char * name,const int32_t intValue,napi_value & result)116 napi_status MediaLibraryNotifyUtils::SetValueInt32(const napi_env& env, const char* name, const int32_t intValue,
117     napi_value& result)
118 {
119     if (result == nullptr) {
120         NAPI_ERR_LOG("result is nullptr");
121         return napi_invalid_arg;
122     }
123 
124     napi_value value;
125     napi_status status = napi_create_int32(env, intValue, &value);
126     if (status != napi_ok) {
127         NAPI_ERR_LOG("Set value create int32 error! name: %{public}s, status: %{public}d, intValue: %{public}d",
128             name, status, intValue);
129         return status;
130     }
131     status = napi_set_named_property(env, result, name, value);
132     if (status != napi_ok) {
133         NAPI_ERR_LOG("Set int32 named property error! name: %{public}s, status: %{public}d, intValue: %{public}d",
134             name, status, intValue);
135     }
136     return status;
137 }
138 
SetValueString(const napi_env & env,const char * name,const string & stringValue,napi_value & result)139 napi_status MediaLibraryNotifyUtils::SetValueString(const napi_env& env, const char* name, const string& stringValue,
140     napi_value& result)
141 {
142     if (result == nullptr) {
143         NAPI_ERR_LOG("result is nullptr");
144         return napi_invalid_arg;
145     }
146 
147     napi_value value;
148     napi_status status = napi_create_string_utf8(env, stringValue.c_str(), NAPI_AUTO_LENGTH, &value);
149     if (status != napi_ok) {
150         NAPI_ERR_LOG("Set value create string error! name: %{public}s, status: %{public}d, stringValue: %{public}s",
151             name, status, stringValue.c_str());
152         return status;
153     }
154     status = napi_set_named_property(env, result, name, value);
155     if (status != napi_ok) {
156         NAPI_ERR_LOG("Set string named property error! name: %{public}s, status: %{public}d, stringValue: %{public}s",
157             name, status, stringValue.c_str());
158     }
159     return status;
160 }
161 
SetValueBool(const napi_env & env,const char * name,const bool boolValue,napi_value & result)162 napi_status MediaLibraryNotifyUtils::SetValueBool(const napi_env& env, const char* name, const bool boolValue,
163     napi_value& result)
164 {
165     if (result == nullptr) {
166         NAPI_ERR_LOG("result is nullptr");
167         return napi_invalid_arg;
168     }
169 
170     napi_value value;
171     napi_status status = napi_get_boolean(env, boolValue, &value);
172     if (status != napi_ok) {
173         NAPI_ERR_LOG("Set value create string error! name: %{public}s, status: %{public}d, boolValue: %{public}d",
174             name, status, boolValue);
175         return status;
176     }
177     status = napi_set_named_property(env, result, name, value);
178     if (status != napi_ok) {
179         NAPI_ERR_LOG("Set string named property error! name: %{public}s, status: %{public}d, boolValue: %{public}d",
180             name, status, boolValue);
181     }
182     return status;
183 }
184 
SetValueInt64(const napi_env & env,const char * name,const int64_t intValue,napi_value & result)185 napi_status MediaLibraryNotifyUtils::SetValueInt64(const napi_env& env, const char* name, const int64_t intValue,
186     napi_value& result)
187 {
188     if (result == nullptr) {
189         NAPI_ERR_LOG("result is nullptr");
190         return napi_invalid_arg;
191     }
192 
193     napi_value value;
194     napi_status status = napi_create_int64(env, intValue, &value);
195     if (status != napi_ok) {
196         NAPI_ERR_LOG("Set value create int64 error! name: %{public}s, status: %{public}d, intValue: %{public}" PRId64,
197             name, status, intValue);
198         return status;
199     }
200     status = napi_set_named_property(env, result, name, value);
201     if (status != napi_ok) {
202         NAPI_ERR_LOG("Set int64 named property error! name: %{public}s, status: %{public}d, intValue: %{public}" PRId64,
203             name, status, intValue);
204     }
205     return status;
206 }
207 
SetValueNull(const napi_env & env,const char * name,napi_value & result)208 napi_status MediaLibraryNotifyUtils::SetValueNull(const napi_env& env, const char* name, napi_value& result)
209 {
210     if (result == nullptr) {
211         NAPI_ERR_LOG("result is nullptr");
212         return napi_invalid_arg;
213     }
214 
215     napi_value nullValue = nullptr;
216     napi_status status = napi_get_null(env, &nullValue);
217     if (status != napi_ok) {
218         NAPI_ERR_LOG("napi get null error! name: %{public}s, status: %{public}d", name, status);
219         return status;
220     }
221 
222     status = napi_set_named_property(env, result, name, nullValue);
223     if (status != napi_ok) {
224         NAPI_ERR_LOG("Set null named property error! name: %{public}s, status: %{public}d", name, status);
225     }
226     return status;
227 }
228 
BuildPhotoAssetChangeInfo(napi_env env,const AccurateRefresh::PhotoAssetChangeInfo & photoAssetChangeInfo)229 napi_value MediaLibraryNotifyUtils::BuildPhotoAssetChangeInfo(napi_env env,
230     const AccurateRefresh::PhotoAssetChangeInfo &photoAssetChangeInfo)
231 {
232     if (photoAssetChangeInfo.fileId_ == AccurateRefresh::INVALID_INT32_VALUE) {
233         return nullptr;
234     }
235 
236     napi_value result = nullptr;
237     napi_create_object(env, &result);
238 
239     SetValueString(env, "uri", photoAssetChangeInfo.uri_.c_str(), result);
240     SetValueInt32(env, "mediaType", photoAssetChangeInfo.mediaType_, result);
241     SetValueString(env, "albumUri", photoAssetChangeInfo.ownerAlbumUri_.c_str(), result);
242     if (!MediaLibraryNapiUtils::IsSystemApp()) {
243         return result;
244     }
245     SetValueInt32(env, "fileId", photoAssetChangeInfo.fileId_, result);
246     SetValueString(env, "dateDay", photoAssetChangeInfo.dateDay_.c_str(), result);
247     SetValueBool(env, "isFavorite", photoAssetChangeInfo.isFavorite_, result);
248     SetValueBool(env, "isHidden", photoAssetChangeInfo.isHidden_, result);
249     SetValueInt32(env, "strongAssociation", photoAssetChangeInfo.strongAssociation_, result);
250     SetValueInt32(env, "thumbnailVisible", photoAssetChangeInfo.thumbnailVisible_, result);
251     SetValueInt64(env, "dateTrashedMs", photoAssetChangeInfo.dateTrashedMs_, result);
252     SetValueInt64(env, "dateAddedMs", photoAssetChangeInfo.dateAddedMs_, result);
253     SetValueInt64(env, "dateTakenMs", photoAssetChangeInfo.dateTakenMs_, result);
254     SetValueInt32(env, "position", photoAssetChangeInfo.position_, result);
255     SetValueString(env, "displayName", photoAssetChangeInfo.displayName_, result);
256     SetValueInt64(env, "size", photoAssetChangeInfo.size_, result);
257 
258     return result;
259 }
260 
BuildPhotoAssetChangeData(napi_env env,const AccurateRefresh::PhotoAssetChangeData & photoAssetChangeData)261 napi_value MediaLibraryNotifyUtils::BuildPhotoAssetChangeData(napi_env env,
262     const AccurateRefresh::PhotoAssetChangeData &photoAssetChangeData)
263 {
264     napi_value result = nullptr;
265     napi_create_object(env, &result);
266     napi_status status = napi_ok;
267 
268     napi_value assetBeforeChangeValue = BuildPhotoAssetChangeInfo(env, photoAssetChangeData.infoBeforeChange_);
269     if (assetBeforeChangeValue == nullptr) {
270         SetValueNull(env, "assetBeforeChange", result);
271     } else {
272         status = napi_set_named_property(env, result, "assetBeforeChange", assetBeforeChangeValue);
273         if (status != napi_ok) {
274             NAPI_ERR_LOG("set array named property error: assetBeforeChange");
275         }
276     }
277 
278     napi_value assetAfterChangeValue = BuildPhotoAssetChangeInfo(env, photoAssetChangeData.infoAfterChange_);
279     if (assetAfterChangeValue == nullptr) {
280         SetValueNull(env, "assetAfterChange", result);
281     } else {
282         status = napi_set_named_property(env, result, "assetAfterChange", assetAfterChangeValue);
283         if (status != napi_ok) {
284             NAPI_ERR_LOG("set array named property error: assetAfterChange");
285         }
286     }
287 
288     status = SetValueBool(env, "isContentChanged", photoAssetChangeData.isContentChanged_, result);
289     if (status != napi_ok) {
290         NAPI_ERR_LOG("set array named property error: isContentChanged");
291     }
292 
293     status = SetValueBool(env, "isDeleted", photoAssetChangeData.isDelete_, result);
294     if (status != napi_ok) {
295         NAPI_ERR_LOG("set array named property error: isDeleted");
296     }
297     if (!MediaLibraryNapiUtils::IsSystemApp()) {
298         return result;
299     }
300 
301     status = SetValueInt32(env, "thumbnailChangeStatus", photoAssetChangeData.thumbnailChangeStatus_, result);
302     if (status != napi_ok) {
303         NAPI_ERR_LOG("set array named property error: thumbnailChangeStatus");
304     }
305 
306     status = SetValueInt64(env, "version", photoAssetChangeData.version_, result);
307     if (status != napi_ok) {
308         NAPI_ERR_LOG("set array named property error: version");
309     }
310 
311     return result;
312 }
313 
BuildPhotoNapiArray(napi_env env,const vector<std::variant<AccurateRefresh::PhotoAssetChangeData,AccurateRefresh::AlbumChangeData>> & changeInfos)314 napi_value MediaLibraryNotifyUtils::BuildPhotoNapiArray(napi_env env,
315     const vector<std::variant<AccurateRefresh::PhotoAssetChangeData, AccurateRefresh::AlbumChangeData>> &changeInfos)
316 {
317     MediaLibraryTracer tracer;
318     tracer.Start("BuildPhotoNapiArray");
319     napi_value result = nullptr;
320     napi_status status = napi_create_array_with_length(env, changeInfos.size(), &result);
321     CHECK_COND_RET(status == napi_ok, nullptr, "Create array error!");
322     napi_value tmpValue = nullptr;
323     status = napi_create_array_with_length(env, 0, &tmpValue);
324     CHECK_COND_RET(status == napi_ok, nullptr, "Create array error!");
325 
326     size_t resultIndex = 0;
327     for (const auto &changeInfo : changeInfos) {
328         if (const auto changeInfoPtr = std::get_if<AccurateRefresh::PhotoAssetChangeData>(&changeInfo)) {
329             napi_value assetValue = BuildPhotoAssetChangeData(env, *changeInfoPtr);
330             if ((assetValue == nullptr) || (napi_set_element(env, result, resultIndex++, assetValue) != napi_ok)) {
331                 NAPI_ERR_LOG("failed to add element");
332                 return tmpValue;
333             }
334         } else {
335             NAPI_ERR_LOG("failed to get changeInfoPtr");
336             return nullptr;
337         }
338     }
339     return result;
340 }
341 
BuildPhotoAssetChangeInfos(napi_env env,const shared_ptr<Notification::MediaChangeInfo> & changeInfo)342 napi_value MediaLibraryNotifyUtils::BuildPhotoAssetChangeInfos(napi_env env,
343     const shared_ptr<Notification::MediaChangeInfo> &changeInfo)
344 {
345     MediaLibraryTracer tracer;
346     tracer.Start("BuildPhotoAssetChangeInfos");
347     if (changeInfo == nullptr) {
348         NAPI_ERR_LOG("Invalid changeInfo");
349         return nullptr;
350     }
351     napi_value result = nullptr;
352     napi_create_object(env, &result);
353     napi_status status = napi_ok;
354 
355     status = MediaLibraryNotifyUtils::SetValueInt32(env, "type", GetNotifyChangeType(changeInfo->notifyType), result);
356     if (status != napi_ok) {
357         NAPI_ERR_LOG("set array named property error: type");
358         return nullptr;
359     }
360 
361     napi_value assetResults = BuildPhotoNapiArray(env, changeInfo->changeInfos);
362     if (assetResults == nullptr) {
363         NAPI_ERR_LOG("Failed to build assetResults");
364         return nullptr;
365     }
366     status = napi_set_named_property(env, result, "assetChangeDatas", assetResults);
367     if (status != napi_ok) {
368         NAPI_ERR_LOG("set array named property error: assetChangeDatas");
369         return nullptr;
370     }
371 
372     status = MediaLibraryNotifyUtils::SetValueBool(env, "isForRecheck", changeInfo->isForRecheck, result);
373     if (status != napi_ok) {
374         NAPI_ERR_LOG("set array named property error: isForRecheck");
375         return nullptr;
376     }
377 
378     return result;
379 }
380 
BuildAlbumChangeInfo(napi_env env,const AccurateRefresh::AlbumChangeInfo & albumChangeInfo)381 napi_value MediaLibraryNotifyUtils::BuildAlbumChangeInfo(napi_env env,
382     const AccurateRefresh::AlbumChangeInfo &albumChangeInfo)
383 {
384     if (albumChangeInfo.albumId_ == AccurateRefresh::INVALID_INT32_VALUE) {
385         return nullptr;
386     }
387 
388     napi_value result = nullptr;
389     napi_create_object(env, &result);
390 
391     SetValueInt32(env, "albumType", albumChangeInfo.albumType_, result);
392     SetValueInt32(env, "albumSubtype", albumChangeInfo.albumSubType_, result);
393     SetValueString(env, "albumName", albumChangeInfo.albumName_, result);
394     SetValueString(env, "albumUri", albumChangeInfo.albumUri_, result);
395     SetValueInt64(env, "imageCount", albumChangeInfo.imageCount_, result);
396     SetValueInt64(env, "videoCount", albumChangeInfo.videoCount_, result);
397     SetValueInt64(env, "count", albumChangeInfo.count_, result);
398     SetValueString(env, "coverUri", albumChangeInfo.coverUri_, result);
399     if (!MediaLibraryNapiUtils::IsSystemApp()) {
400         return result;
401     }
402     SetValueInt64(env, "hiddenCount", albumChangeInfo.hiddenCount_, result);
403     SetValueString(env, "hiddenCoverUri", albumChangeInfo.hiddenCoverUri_, result);
404     SetValueBool(env, "isCoverChanged", albumChangeInfo.isCoverChange_, result);
405     SetValueBool(env, "isHiddenCoverChanged", albumChangeInfo.isHiddenCoverChange_, result);
406     SetValueInt32(env, "orderSection", albumChangeInfo.orderSection_, result);
407     SetValueInt32(env, "albumOrder", albumChangeInfo.albumOrder_, result);
408 
409     napi_status status = napi_ok;
410     napi_value coverInfoValue = BuildPhotoAssetChangeInfo(env, albumChangeInfo.coverInfo_);
411     if (coverInfoValue != nullptr) {
412         status = napi_set_named_property(env, result, "coverInfo", coverInfoValue);
413         if (status != napi_ok) {
414             NAPI_ERR_LOG("set array named property error: coverInfo");
415         }
416     }
417 
418     napi_value hiddenCoverInfoValue = BuildPhotoAssetChangeInfo(env, albumChangeInfo.hiddenCoverInfo_);
419     if (hiddenCoverInfoValue != nullptr) {
420         status = napi_set_named_property(env, result, "hiddenCoverInfo", hiddenCoverInfoValue);
421         if (status != napi_ok) {
422             NAPI_ERR_LOG("set array named property error: hiddenCoverInfo");
423         }
424     }
425     return result;
426 }
427 
BuildAlbumChangeData(napi_env env,const AccurateRefresh::AlbumChangeData & albumChangeData)428 napi_value MediaLibraryNotifyUtils::BuildAlbumChangeData(napi_env env,
429     const AccurateRefresh::AlbumChangeData &albumChangeData)
430 {
431     napi_value result = nullptr;
432     napi_create_object(env, &result);
433     napi_status status = napi_ok;
434 
435     napi_value albumBeforeChangeValue = BuildAlbumChangeInfo(env, albumChangeData.infoBeforeChange_);
436     if (albumBeforeChangeValue == nullptr) {
437         SetValueNull(env, "albumBeforeChange", result);
438     } else {
439         status = napi_set_named_property(env, result, "albumBeforeChange", albumBeforeChangeValue);
440         if (status != napi_ok) {
441             NAPI_ERR_LOG("set array named property error: albumBeforeChange");
442         }
443     }
444 
445     napi_value albumAfterChangeValue = BuildAlbumChangeInfo(env, albumChangeData.infoAfterChange_);
446     if (albumAfterChangeValue == nullptr) {
447         SetValueNull(env, "albumAfterChange", result);
448     } else {
449         status = napi_set_named_property(env, result, "albumAfterChange", albumAfterChangeValue);
450         if (status != napi_ok) {
451             NAPI_ERR_LOG("set array named property error: albumAfterChange");
452         }
453     }
454 
455     if (!MediaLibraryNapiUtils::IsSystemApp()) {
456         return result;
457     }
458 
459     status = SetValueInt64(env, "version", albumChangeData.version_, result);
460     if (status != napi_ok) {
461         NAPI_ERR_LOG("set array named property error: version");
462     }
463 
464     return result;
465 }
466 
BuildAlbumNapiArray(napi_env env,const vector<std::variant<AccurateRefresh::PhotoAssetChangeData,AccurateRefresh::AlbumChangeData>> & changeInfos)467 napi_value MediaLibraryNotifyUtils::BuildAlbumNapiArray(napi_env env,
468     const vector<std::variant<AccurateRefresh::PhotoAssetChangeData, AccurateRefresh::AlbumChangeData>> &changeInfos)
469 {
470     MediaLibraryTracer tracer;
471     tracer.Start("BuildAlbumNapiArray");
472     napi_value result = nullptr;
473     napi_status status = napi_create_array_with_length(env, changeInfos.size(), &result);
474     CHECK_COND_RET(status == napi_ok, nullptr, "Create array error!");
475     napi_value tmpValue = nullptr;
476     status = napi_create_array_with_length(env, 0, &tmpValue);
477     CHECK_COND_RET(status == napi_ok, nullptr, "Create array error!");
478 
479     size_t resultIndex = 0;
480     for (const auto &changeInfo : changeInfos) {
481         if (const auto changeInfoPtr = std::get_if<AccurateRefresh::AlbumChangeData>(&changeInfo)) {
482             napi_value assetValue = BuildAlbumChangeData(env, *changeInfoPtr);
483             if ((assetValue == nullptr) || (napi_set_element(env, result, resultIndex++, assetValue) != napi_ok)) {
484                 NAPI_ERR_LOG("failed to add element");
485                 return tmpValue;
486             }
487         } else {
488             NAPI_ERR_LOG("failed to get changeInfoPtr");
489             return nullptr;
490         }
491     }
492     return result;
493 }
494 
BuildAlbumChangeInfos(napi_env env,const shared_ptr<Notification::MediaChangeInfo> & changeInfo)495 napi_value MediaLibraryNotifyUtils::BuildAlbumChangeInfos(napi_env env,
496     const shared_ptr<Notification::MediaChangeInfo> &changeInfo)
497 {
498     MediaLibraryTracer tracer;
499     tracer.Start("BuildAlbumChangeInfos");
500     if (changeInfo == nullptr) {
501         NAPI_ERR_LOG("Invalid changeInfo");
502         return nullptr;
503     }
504     napi_value result = nullptr;
505     napi_create_object(env, &result);
506     napi_status status = napi_ok;
507 
508     status = MediaLibraryNotifyUtils::SetValueInt32(env, "type", GetNotifyChangeType(changeInfo->notifyType), result);
509     if (status != napi_ok) {
510         NAPI_ERR_LOG("set array named property error: type");
511         return nullptr;
512     }
513 
514     napi_value albumResults = BuildAlbumNapiArray(env, changeInfo->changeInfos);
515     if (albumResults == nullptr) {
516         NAPI_ERR_LOG("Failed to build albumResults");
517         return nullptr;
518     }
519     status = napi_set_named_property(env, result, "albumChangeDatas", albumResults);
520     if (status != napi_ok) {
521         NAPI_ERR_LOG("set array named property error: albumChangeDatas");
522         return nullptr;
523     }
524 
525     status = MediaLibraryNotifyUtils::SetValueBool(env, "isForRecheck", changeInfo->isForRecheck, result);
526     if (status != napi_ok) {
527         NAPI_ERR_LOG("set array named property error: isForRecheck");
528         return nullptr;
529     }
530 
531     return result;
532 }
533 
BuildPhotoAssetRecheckChangeInfos(napi_env env)534 napi_value MediaLibraryNotifyUtils::BuildPhotoAssetRecheckChangeInfos(napi_env env)
535 {
536     MediaLibraryTracer tracer;
537     tracer.Start("BuildPhotoAssetRecheckChangeInfos");
538 
539     napi_value result = nullptr;
540     napi_create_object(env, &result);
541     napi_status status = napi_ok;
542     // 发送全查的通知,默认通知类型为UPDATE
543     status = MediaLibraryNotifyUtils::SetValueInt32(env, "type",
544         static_cast<int32_t>(NotifyChangeType::NOTIFY_CHANGE_UPDATE), result);
545     if (status != napi_ok) {
546         NAPI_ERR_LOG("set array named property error: type");
547         return nullptr;
548     }
549 
550     status = SetValueNull(env, "assetChangeDatas", result);
551     if (status != napi_ok) {
552         NAPI_ERR_LOG("set array named property error: assetChangeDatas");
553         return nullptr;
554     }
555 
556     status = MediaLibraryNotifyUtils::SetValueBool(env, "isForRecheck", true, result);
557     if (status != napi_ok) {
558         NAPI_ERR_LOG("set array named property error: isForRecheck");
559         return nullptr;
560     }
561 
562     return result;
563 }
564 
BuildAlbumRecheckChangeInfos(napi_env env)565 napi_value MediaLibraryNotifyUtils::BuildAlbumRecheckChangeInfos(napi_env env)
566 {
567     MediaLibraryTracer tracer;
568     tracer.Start("BuildAlbumRecheckChangeInfos");
569 
570     napi_value result = nullptr;
571     napi_create_object(env, &result);
572     napi_status status = napi_ok;
573     // 发送全查的通知,默认通知类型为UPDATE
574     status = MediaLibraryNotifyUtils::SetValueInt32(env, "type",
575         static_cast<int32_t>(NotifyChangeType::NOTIFY_CHANGE_UPDATE), result);
576     if (status != napi_ok) {
577         NAPI_ERR_LOG("set array named property error: type");
578         return nullptr;
579     }
580 
581     status = SetValueNull(env, "albumChangeDatas", result);
582     if (status != napi_ok) {
583         NAPI_ERR_LOG("set array named property error: albumChangeDatas");
584         return nullptr;
585     }
586 
587     status = MediaLibraryNotifyUtils::SetValueBool(env, "isForRecheck", true, result);
588     if (status != napi_ok) {
589         NAPI_ERR_LOG("set array named property error: isForRecheck");
590         return nullptr;
591     }
592 
593     return result;
594 }
595 
ConvertToJsError(int32_t innerErr)596 int32_t MediaLibraryNotifyUtils::ConvertToJsError(int32_t innerErr)
597 {
598     int32_t err = JS_E_INNER_FAIL;
599     if (ERROR_MAP.find(innerErr) != ERROR_MAP.end()) {
600         err = ERROR_MAP.at(innerErr);
601     }
602     return err;
603 }
604 }  // namespace Media
605 }  // namespace OHOS
606