• 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::OwnerAlbumInfoCalculation"
17 
18 #include "owner_album_info_calculation.h"
19 #include "album_asset_helper.h"
20 #include "accurate_debug_log.h"
21 
22 using namespace std;
23 
24 namespace OHOS {
25 namespace Media::AccurateRefresh {
26 
CalOwnerAlbumRefreshInfo(const std::vector<PhotoAssetChangeData> & assetChangeDatas)27 unordered_map<int32_t, AlbumRefreshInfo> OwnerAlbumInfoCalculation::CalOwnerAlbumRefreshInfo(
28     const std::vector<PhotoAssetChangeData> &assetChangeDatas)
29 {
30     unordered_map<int32_t, AlbumRefreshInfo> ownerAlbumInfos;
31     for (auto &assetChangeData : assetChangeDatas) {
32         auto initAlbumId = assetChangeData.infoBeforeChange_.ownerAlbumId_;
33         auto modifiedAlbumId = assetChangeData.infoAfterChange_.ownerAlbumId_;
34 
35         // 无效数据
36         if (initAlbumId == INVALID_INT32_VALUE && modifiedAlbumId == INVALID_INT32_VALUE) {
37             MEDIA_WARN_LOG("assset change data invalid albumId.");
38             continue;
39         }
40 
41         // 更新initAlbumId
42         if (initAlbumId != INVALID_INT32_VALUE) {
43             UpdateOwnerRefreshInfo(initAlbumId, assetChangeData, ownerAlbumInfos);
44         }
45 
46         // 更新modifiedAlbumId; 如果和initAlbumId相同,则不需要再次处理
47         if (modifiedAlbumId != INVALID_INT32_VALUE && initAlbumId != modifiedAlbumId) {
48             UpdateOwnerRefreshInfo(modifiedAlbumId, assetChangeData, ownerAlbumInfos);
49         }
50     }
51     return ownerAlbumInfos;
52 }
53 
UpdateOwnerRefreshInfo(int32_t albumId,const PhotoAssetChangeData & assetChangeData,std::unordered_map<int32_t,AlbumRefreshInfo> & ownerAlbumInfos)54 void OwnerAlbumInfoCalculation::UpdateOwnerRefreshInfo(int32_t albumId, const PhotoAssetChangeData &assetChangeData,
55     std::unordered_map<int32_t, AlbumRefreshInfo> &ownerAlbumInfos)
56 {
57     auto refreshInfoIter = ownerAlbumInfos.find(albumId);
58     if (refreshInfoIter != ownerAlbumInfos.end()) {
59         CalOwnerAlbumRefreshInfo(assetChangeData, albumId, refreshInfoIter->second);
60     } else {
61         AlbumRefreshInfo refreshInfo;
62         if (CalOwnerAlbumRefreshInfo(assetChangeData, albumId, refreshInfo)) {
63             ownerAlbumInfos.emplace(albumId, refreshInfo);
64         }
65     }
66 }
67 
IsOwnerAlbumAsset(const PhotoAssetChangeInfo & assetInfo,int32_t albumId)68 bool OwnerAlbumInfoCalculation::IsOwnerAlbumAsset(const PhotoAssetChangeInfo &assetInfo, int32_t albumId)
69 {
70     return assetInfo.ownerAlbumId_ == albumId && AlbumAssetHelper::IsCommonSystemAsset(assetInfo, false);
71 }
72 
IsOwnerAlbumHiddenAsset(const PhotoAssetChangeInfo & assetInfo,int32_t albumId)73 bool OwnerAlbumInfoCalculation::IsOwnerAlbumHiddenAsset(const PhotoAssetChangeInfo &assetInfo, int32_t albumId)
74 {
75     return assetInfo.ownerAlbumId_ == albumId && AlbumAssetHelper::IsCommonSystemAsset(assetInfo, true);
76 }
77 
IsOwnerAlbumVideoAsset(const PhotoAssetChangeInfo & assetInfo,int32_t albumId)78 bool OwnerAlbumInfoCalculation::IsOwnerAlbumVideoAsset(const PhotoAssetChangeInfo &assetInfo, int32_t albumId)
79 {
80     return IsOwnerAlbumAsset(assetInfo, albumId) && AlbumAssetHelper::IsVideoAsset(assetInfo);
81 }
82 
IsNewerAsset(const PhotoAssetChangeInfo & compareAssetInfo,const PhotoAssetChangeInfo & currentAssetInfo,int32_t albumId)83 bool OwnerAlbumInfoCalculation::IsNewerAsset(const PhotoAssetChangeInfo &compareAssetInfo,
84     const PhotoAssetChangeInfo &currentAssetInfo, int32_t albumId)
85 {
86     return IsOwnerAlbumAsset(compareAssetInfo, albumId) && IsOwnerAlbumAsset(currentAssetInfo, albumId)
87         && AlbumAssetHelper::IsNewerByDateTaken(compareAssetInfo, currentAssetInfo, false);
88 }
89 
IsNewerHiddenAsset(const PhotoAssetChangeInfo & compareAssetInfo,const PhotoAssetChangeInfo & currentAssetInfo,int32_t albumId)90 bool OwnerAlbumInfoCalculation::IsNewerHiddenAsset(const PhotoAssetChangeInfo &compareAssetInfo,
91     const PhotoAssetChangeInfo &currentAssetInfo, int32_t albumId)
92 {
93     return IsOwnerAlbumAsset(compareAssetInfo, albumId) && IsOwnerAlbumAsset(currentAssetInfo, albumId)
94         && AlbumAssetHelper::IsNewerByHiddenTime(compareAssetInfo, currentAssetInfo);
95 }
96 
UpdateCover(const PhotoAssetChangeData & assetChangeData,function<bool (const PhotoAssetChangeInfo &,int32_t)> isAsset,int32_t albumId,function<bool (const PhotoAssetChangeInfo &,const PhotoAssetChangeInfo &,int32_t)> isNewerAsset,PhotoAssetChangeInfo & addCover,unordered_set<int32_t> & removeFileIds)97 bool OwnerAlbumInfoCalculation::UpdateCover(const PhotoAssetChangeData &assetChangeData,
98     function<bool(const PhotoAssetChangeInfo&, int32_t)> isAsset, int32_t albumId,
99     function<bool(const PhotoAssetChangeInfo&, const PhotoAssetChangeInfo&, int32_t)> isNewerAsset,
100     PhotoAssetChangeInfo &addCover, unordered_set<int32_t> &removeFileIds)
101 {
102     function<bool(const PhotoAssetChangeInfo&)> isAlbumAsset =
103         [&] (const PhotoAssetChangeInfo& assetChangeData) -> bool {
104             return isAsset(assetChangeData, albumId);
105     };
106     function<bool(const PhotoAssetChangeInfo&, const PhotoAssetChangeInfo&)> isAlbumNewerAsset =
107         [&] (const PhotoAssetChangeInfo &compare, const PhotoAssetChangeInfo &current) -> bool {
108             return isNewerAsset(compare, current, albumId);
109     };
110     return AlbumAssetHelper::UpdateCover(assetChangeData, isAlbumAsset, isAlbumNewerAsset, addCover, removeFileIds);
111 }
112 
UpdateCount(const PhotoAssetChangeData & assetChangeData,std::function<bool (const PhotoAssetChangeInfo &,int32_t)> isAsset,int32_t albumId,int32_t & count)113 bool OwnerAlbumInfoCalculation::UpdateCount(const PhotoAssetChangeData &assetChangeData,
114     std::function<bool(const PhotoAssetChangeInfo&, int32_t)> isAsset, int32_t albumId, int32_t &count)
115 {
116     function<bool(const PhotoAssetChangeInfo&)> isAlbumAsset =
117         [&] (const PhotoAssetChangeInfo& assetChangeData) -> bool {
118             return isAsset(assetChangeData, albumId);
119     };
120     return AlbumAssetHelper::UpdateCount(assetChangeData, isAlbumAsset, count);
121 }
122 
CalOwnerAlbumRefreshInfo(const PhotoAssetChangeData & assetChangeData,int32_t albumId,AlbumRefreshInfo & refreshInfo)123 bool OwnerAlbumInfoCalculation::CalOwnerAlbumRefreshInfo(const PhotoAssetChangeData &assetChangeData, int32_t albumId,
124     AlbumRefreshInfo &refreshInfo)
125 {
126     bool ret = false;
127     AlbumRefreshTimestamp assetTimestamp(assetChangeData.infoBeforeChange_.timestamp_,
128         assetChangeData.infoAfterChange_.timestamp_);
129     // 用户相册信息变化
130     if (IsOwnerAlbumInfoChange(assetChangeData, IsOwnerAlbumAsset, albumId)) {
131         function<bool(AlbumRefreshInfo&)> calRefreshInfoFunc = [&assetChangeData, albumId]
132             (AlbumRefreshInfo &refreshInfo) -> bool {
133                 return OwnerAlbumInfoCalculation::UpdateRefreshNormalInfo(assetChangeData, albumId, refreshInfo);
134         };
135         ret = AlbumAssetHelper::CalAlbumRefreshInfo(calRefreshInfoFunc, refreshInfo, albumId, false, assetTimestamp);
136     }
137     // 用户相册隐藏信息变化
138     if (IsOwnerAlbumInfoChange(assetChangeData, IsOwnerAlbumHiddenAsset, albumId)) {
139         function<bool(AlbumRefreshInfo&)> calHiddenRefreshInfoFunc = [&assetChangeData, albumId]
140             (AlbumRefreshInfo &refreshInfo) -> bool {
141                 return OwnerAlbumInfoCalculation::UpdateRefreshHiddenInfo(assetChangeData, albumId, refreshInfo);
142         };
143         ret = AlbumAssetHelper::CalAlbumRefreshInfo(calHiddenRefreshInfoFunc, refreshInfo, albumId, true,
144             assetTimestamp) || ret;
145     }
146     return ret;
147 }
148 
UpdateRefreshNormalInfo(const PhotoAssetChangeData & assetChangeData,int32_t albumId,AlbumRefreshInfo & refreshInfo)149 bool OwnerAlbumInfoCalculation::UpdateRefreshNormalInfo(const PhotoAssetChangeData &assetChangeData, int32_t albumId,
150     AlbumRefreshInfo& refreshInfo)
151 {
152     bool ret = false;
153     if (UpdateCount(assetChangeData, IsOwnerAlbumAsset, albumId, refreshInfo.deltaCount_)) {
154         refreshInfo.assetModifiedCnt_++;
155         ret = true;
156     }
157     if (UpdateCount(assetChangeData, IsOwnerAlbumVideoAsset, albumId,
158         refreshInfo.deltaVideoCount_)) {
159         ret = true;
160     }
161     if (UpdateCover(assetChangeData, IsOwnerAlbumAsset, albumId, IsNewerAsset,
162         refreshInfo.deltaAddCover_, refreshInfo.removeFileIds)) {
163         ret = true;
164     }
165     return ret;
166 }
167 
UpdateRefreshHiddenInfo(const PhotoAssetChangeData & assetChangeData,int32_t albumId,AlbumRefreshInfo & refreshInfo)168 bool OwnerAlbumInfoCalculation::UpdateRefreshHiddenInfo(const PhotoAssetChangeData &assetChangeData, int32_t albumId,
169     AlbumRefreshInfo& refreshInfo)
170 {
171     bool ret = false;
172     if (UpdateCount(assetChangeData, IsOwnerAlbumHiddenAsset, albumId,
173         refreshInfo.deltaHiddenCount_)) {
174         refreshInfo.hiddenAssetModifiedCnt_++;
175         ret = true;
176     }
177     if (UpdateCover(assetChangeData, IsOwnerAlbumHiddenAsset, albumId, IsNewerHiddenAsset,
178         refreshInfo.deltaAddHiddenCover_, refreshInfo.removeHiddenFileIds)) {
179         ret = true;
180     }
181     return ret;
182 }
183 
IsOwnerAlbumInfoChange(const PhotoAssetChangeData & assetChangeData,std::function<bool (PhotoAssetChangeInfo,int32_t)> isAlbumAsset,int32_t albumId)184 bool OwnerAlbumInfoCalculation::IsOwnerAlbumInfoChange(const PhotoAssetChangeData &assetChangeData,
185     std::function<bool(PhotoAssetChangeInfo, int32_t)> isAlbumAsset, int32_t albumId)
186 {
187     return isAlbumAsset(assetChangeData.infoBeforeChange_, albumId) !=
188         isAlbumAsset(assetChangeData.infoAfterChange_, albumId);
189 }
190 
191 } // namespace Media
192 } // namespace OHOS