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 #define MLOG_TAG "AccurateRefresh::AlbumAssetHelper"
16
17 #include "album_asset_helper.h"
18 #include "userfile_manager_types.h"
19 #include "medialibrary_type_const.h"
20 #include "accurate_debug_log.h"
21
22 namespace OHOS {
23 namespace Media::AccurateRefresh {
24
IsCommonSystemAsset(const PhotoAssetChangeInfo & assetInfo,bool isHiddenAsset)25 bool AlbumAssetHelper::IsCommonSystemAsset(const PhotoAssetChangeInfo &assetInfo, bool isHiddenAsset)
26 {
27 return assetInfo.syncStatus_ == static_cast<int32_t> (SyncStatusType::TYPE_VISIBLE) &&
28 assetInfo.cleanFlag_ == static_cast<int32_t> (CleanType::TYPE_NOT_CLEAN) &&
29 assetInfo.dateTrashedMs_ == 0 &&
30 assetInfo.isHidden_ == isHiddenAsset &&
31 assetInfo.timePending_ == 0 &&
32 !assetInfo.isTemp_ &&
33 assetInfo.burstCoverLevel_ == static_cast<int32_t> (BurstCoverLevelType::COVER);
34 }
35
IsVideoAsset(const PhotoAssetChangeInfo & assetInfo)36 bool AlbumAssetHelper::IsVideoAsset(const PhotoAssetChangeInfo &assetInfo)
37 {
38 return assetInfo.mediaType_ == static_cast<int32_t> (MediaType::MEDIA_TYPE_VIDEO);
39 }
40
IsImageAsset(const PhotoAssetChangeInfo & assetInfo)41 bool AlbumAssetHelper::IsImageAsset(const PhotoAssetChangeInfo &assetInfo)
42 {
43 return assetInfo.mediaType_ == static_cast<int32_t> (MediaType::MEDIA_TYPE_IMAGE);
44 }
45
IsNewerByDateAdded(const PhotoAssetChangeInfo & compareAssetInfo,const PhotoAssetChangeInfo & currentAssetInfo,bool isAsc)46 bool AlbumAssetHelper::IsNewerByDateAdded(const PhotoAssetChangeInfo &compareAssetInfo,
47 const PhotoAssetChangeInfo ¤tAssetInfo, bool isAsc)
48 {
49 return compareAssetInfo.dateAddedMs_ > currentAssetInfo.dateAddedMs_ ||
50 (compareAssetInfo.dateAddedMs_ == currentAssetInfo.dateAddedMs_ &&
51 (isAsc ? compareAssetInfo.fileId_ < currentAssetInfo.fileId_ :
52 compareAssetInfo.fileId_ > currentAssetInfo.fileId_));
53 }
54
IsNewerByDateTaken(const PhotoAssetChangeInfo & compareAssetInfo,const PhotoAssetChangeInfo & currentAssetInfo,bool isAsc)55 bool AlbumAssetHelper::IsNewerByDateTaken(const PhotoAssetChangeInfo &compareAssetInfo,
56 const PhotoAssetChangeInfo ¤tAssetInfo, bool isAsc)
57 {
58 return compareAssetInfo.dateTakenMs_ > currentAssetInfo.dateTakenMs_ ||
59 (compareAssetInfo.dateTakenMs_ == currentAssetInfo.dateTakenMs_ &&
60 (isAsc ? compareAssetInfo.fileId_ < currentAssetInfo.fileId_ :
61 compareAssetInfo.fileId_ > currentAssetInfo.fileId_));
62 }
63
IsNewerByHiddenTime(const PhotoAssetChangeInfo & compareAssetInfo,const PhotoAssetChangeInfo & currentAssetInfo,bool isAsc)64 bool AlbumAssetHelper::IsNewerByHiddenTime(const PhotoAssetChangeInfo &compareAssetInfo,
65 const PhotoAssetChangeInfo ¤tAssetInfo, bool isAsc)
66 {
67 return compareAssetInfo.hiddenTime_ > currentAssetInfo.hiddenTime_ ||
68 (compareAssetInfo.hiddenTime_ == currentAssetInfo.hiddenTime_ &&
69 (isAsc ? compareAssetInfo.fileId_ < currentAssetInfo.fileId_ :
70 compareAssetInfo.fileId_ > currentAssetInfo.fileId_));
71 }
72
IsInvalidAsset(const PhotoAssetChangeInfo & assetInfo)73 bool AlbumAssetHelper::IsInvalidAsset(const PhotoAssetChangeInfo &assetInfo)
74 {
75 return assetInfo.fileId_ == INVALID_INT32_VALUE;
76 }
77
UpdateCover(const PhotoAssetChangeData & assetChangeData,std::function<bool (const PhotoAssetChangeInfo &)> isAlbumAsset,std::function<bool (const PhotoAssetChangeInfo &,const PhotoAssetChangeInfo &)> isNewerAsset,PhotoAssetChangeInfo & addCover,std::unordered_set<int32_t> & removeFileIds)78 bool AlbumAssetHelper::UpdateCover(const PhotoAssetChangeData &assetChangeData,
79 std::function<bool(const PhotoAssetChangeInfo&)> isAlbumAsset,
80 std::function<bool(const PhotoAssetChangeInfo&, const PhotoAssetChangeInfo&)> isNewerAsset,
81 PhotoAssetChangeInfo &addCover, std::unordered_set<int32_t> &removeFileIds)
82 {
83 bool before = isAlbumAsset(assetChangeData.infoBeforeChange_);
84 bool after = isAlbumAsset(assetChangeData.infoAfterChange_);
85 bool ret = false;
86 if (!before && after) {
87 if (AlbumAssetHelper::IsInvalidAsset(addCover) ||
88 isNewerAsset(assetChangeData.infoAfterChange_, addCover)) {
89 addCover = assetChangeData.infoAfterChange_;
90 ret = true;
91 }
92 } else if (before && !after) {
93 removeFileIds.insert(assetChangeData.infoBeforeChange_.fileId_);
94 }
95
96 return ret;
97 }
98
UpdateCount(const PhotoAssetChangeData & assetChangeData,std::function<bool (PhotoAssetChangeInfo)> isAlbumAsset,int32_t & count)99 bool AlbumAssetHelper::UpdateCount(const PhotoAssetChangeData &assetChangeData,
100 std::function<bool(PhotoAssetChangeInfo)> isAlbumAsset, int32_t &count)
101 {
102 bool before = isAlbumAsset(assetChangeData.infoBeforeChange_);
103 bool after = isAlbumAsset(assetChangeData.infoAfterChange_);
104 bool ret = false;
105 if (!before && after) {
106 count++;
107 ret = true;
108 }
109 if (before && !after) {
110 count--;
111 ret = true;
112 }
113
114 return ret;
115 }
116
CalAlbumRefreshInfo(std::function<bool (AlbumRefreshInfo & refreshInfo)> calFunc,AlbumRefreshInfo & refreshInfo,int32_t albumId,bool isHidden,AlbumRefreshTimestamp & assetTimestamp)117 bool AlbumAssetHelper::CalAlbumRefreshInfo(std::function<bool(AlbumRefreshInfo &refreshInfo)> calFunc,
118 AlbumRefreshInfo &refreshInfo, int32_t albumId, bool isHidden, AlbumRefreshTimestamp &assetTimestamp)
119 {
120 auto &isForceRefresh = isHidden ? refreshInfo.isHiddenForceRefresh_ : refreshInfo.isForceRefresh_;
121 if (isForceRefresh) {
122 ACCURATE_DEBUG("already force refresh[%{public}d, %{public}d]", albumId, isHidden);
123 return true;
124 }
125 // refreshInfo记录计算时的album全量刷新时间戳
126 auto &albumRefreshTimestamp = isHidden ? refreshInfo.albumHiddenRefreshTimestamp_ :
127 refreshInfo.albumRefreshTimestamp_;
128 if (albumRefreshTimestamp.start_ == INVALID_INT64_VALUE || albumRefreshTimestamp.end_ == INVALID_INT64_VALUE) {
129 albumRefreshTimestamp = AlbumAccurateRefreshManager::GetInstance().GetRefreshTimestamp(albumId, isHidden);
130 }
131 auto action = AlbumAccurateRefreshManager::GetInstance().GetRefreshAction(albumRefreshTimestamp, assetTimestamp);
132 ACCURATE_DEBUG("set force refresh[%{public}d, %{public}d] albumRefreshTimestamp: %{public}s, "
133 "assetTimestamp: %{public}s, action: %{public}d", albumId, isHidden, albumRefreshTimestamp.ToString().c_str(),
134 assetTimestamp.ToString().c_str(), action);
135 if (action == AssetRefreshAlbumAction::ACCURATE_REFRESH) {
136 return calFunc(refreshInfo);
137 } else if (action == AssetRefreshAlbumAction::IGNORE) {
138 return false;
139 }
140
141 // AssetRefreshAlbumAction::FORCE_REFRESH
142 isForceRefresh = true;
143 ACCURATE_DEBUG("set force refresh[%{public}d, %{public}d] ", albumId, isHidden);
144 return true;
145 }
146
147 } // namespace Media
148 } // namespace OHOS