* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_MEDIALIBRARY_ALBUM_REFRESH_EXECUTION_H
#define OHOS_MEDIALIBRARY_ALBUM_REFRESH_EXECUTION_H
#include <functional>
#include <unordered_map>
#include <vector>
#include <set>
#include <sstream>
#include "medialibrary_rdb_utils.h"
#include "photo_asset_change_info.h"
#include "album_change_info.h"
#include "userfile_manager_types.h"
#include "album_accurate_refresh.h"
#include "system_album_info_calculation.h"
#include "dfx_refresh_manager.h"
namespace OHOS {
namespace Media::AccurateRefresh {
class AlbumRefreshExecution {
public:
int32_t RefreshAlbum(const std::vector<PhotoAssetChangeData> &assetChangeDatas,
NotifyAlbumType notifyAlbumType = NO_NOTIFY, bool isRefreshWithDateModified = true);
int32_t Notify();
int32_t RefreshAllAlbum(std::unordered_set<int32_t> albumIds,
NotifyAlbumType notifyAlbumType, bool isRefreshWithDateModified = true);
void SetDfxRefreshManager(std::shared_ptr<DfxRefreshManager> dfxRefreshManager)
{
dfxRefreshManager_ = dfxRefreshManager;
}
private:
std::vector<int32_t> GetAlbumIds();
int32_t CalRefreshInfos(const std::vector<PhotoAssetChangeData> &assetChangeDatas);
int32_t CalAlbumsInfos();
bool CalAlbumInfos(AlbumChangeInfo &albumInfo, const AlbumRefreshInfo &refreshInfo, int32_t subType);
bool CalAlbumInfo(AlbumChangeInfo &albumInfo, const AlbumRefreshInfo &refreshInfo, int32_t subType);
bool CalHiddenAlbumInfo(AlbumChangeInfo &albumInfo, const AlbumRefreshInfo &refreshInfo, int32_t subType);
bool CalAlbumCount(AlbumChangeInfo &albumInfo, const AlbumRefreshInfo &refreshInfo);
bool CalAlbumHiddenCount(AlbumChangeInfo &albumInfo, const AlbumRefreshInfo &refreshInfo);
bool CalAlbumCover(AlbumChangeInfo &albumInfo, const AlbumRefreshInfo &refreshInfo, int32_t subType);
bool CalAlbumHiddenCover(AlbumChangeInfo &albumInfo, const AlbumRefreshInfo &refreshInfo);
bool CalCoverSetCover(AlbumChangeInfo &albumInfo, const AlbumRefreshInfo &refreshInfo);
int32_t UpdateAllAlbums(NotifyAlbumType notifyAlbumType);
int32_t AccurateUpdateAlbums(NotifyAlbumType notifyAlbumType);
int32_t ForceUpdateAlbums(int32_t albumId, bool isHidden, NotifyAlbumType notifyAlbumType);
int32_t GetUpdateValues(NativeRdb::ValuesBucket &values, const AlbumChangeInfo &albumInfo, bool isHidden,
OHOS::Media::NotifyType &type);
void GetUpdateValuesAndNotifyType(const AccurateRefresh::AlbumChangeInfo &initAlbumInfo,
AccurateRefresh::AlbumChangeInfo &albumInfo, NativeRdb::ValuesBucket &values, NotifyType &type);
int32_t SetForceSelectCoverValues(NativeRdb::ValuesBucket &values, const AlbumChangeInfo &albumInfo,
bool isHidden);
bool IsValidCover(const PhotoAssetChangeInfo &assetInfo);
void ClearAlbumInfo(AlbumChangeInfo &albumInfo);
void ClearHiddenAlbumInfo(AlbumChangeInfo &albumInfo);
void ClearAlbumCoverInfo(AlbumChangeInfo &albumInfo);
void ClearHiddenAlbumCoverInfo(AlbumChangeInfo &albumInfo);
void CheckNotifyOldNotification(NotifyAlbumType notifyAlbumType, const AlbumChangeInfo &albumInfo,
OHOS::Media::NotifyType type);
void CheckHiddenAlbumInfo(NativeRdb::ValuesBucket &values, std::stringstream &ss);
void CheckAlbumInfo(NativeRdb::ValuesBucket &values, std::stringstream &ss);
void CheckUpdateAlbumInfo(const AlbumChangeInfo &albumInfo, bool isHidden);
void CheckUpdateValues(const AlbumChangeInfo &albumInfo, const AlbumRefreshInfo &refreshInfo,
NativeRdb::ValuesBucket &values);
bool CheckSetHiddenAlbumInfo(AlbumChangeInfo &albumInfo);
void CheckInitSystemCalculation();
void CheckNotifyAlbum();
private:
static std::unordered_map<PhotoAlbumSubType, SystemAlbumInfoCalculation> systemTypeAlbumCalculations_;
static std::unordered_map<int32_t, SystemAlbumInfoCalculation> systemAlbumCalculations_;
std::unordered_map<int32_t, AlbumRefreshInfo> systemAlbumRefreshInfos_;
std::unordered_map<int32_t, AlbumRefreshInfo> ownerAlbumRefreshInfos_;
std::unordered_map<int32_t, AlbumRefreshInfo> albumRefreshInfos_;
std::unordered_map<int32_t, AlbumChangeInfo> initAlbumInfos_;
AlbumAccurateRefresh albumRefresh_;
std::shared_ptr<DfxRefreshManager> dfxRefreshManager_;
std::unordered_map<int32_t, std::pair<AlbumRefreshInfo, AlbumChangeInfo>> refreshAlbums_;
std::set<int32_t> forceRefreshAlbums_;
std::set<int32_t> forceRefreshHiddenAlbums_;
std::unordered_map<int32_t, bool> hasSentOldNotifications_;
static std::mutex albumRefreshMtx_;
bool isRefreshWithDateModified_ = true;
size_t affectedAlbumCount_ = 0;
};
}
}
#endif