1 /*
2  * Copyright (C) 2024 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 #ifndef OHOS_MEDIALIBRARY_ALBUM_FUSION_UTILS_H
17 #define OHOS_MEDIALIBRARY_ALBUM_FUSION_UTILS_H
18 
19 #include <vector>
20 #include <mutex>
21 
22 #include "media_column.h"
23 #include "media_file_utils.h"
24 #include "media_log.h"
25 #include "medialibrary_album_compatibility_fusion_sql.h"
26 #include "medialibrary_errno.h"
27 #include "photo_map_column.h"
28 #include "source_album.h"
29 #include "medialibrary_rdb_transaction.h"
30 #include "medialibrary_rdb_utils.h"
31 #include "medialibrary_rdbstore.h"
32 #include "medialibrary_unistore.h"
33 #include "medialibrary_unistore_manager.h"
34 
35 namespace OHOS {
36 namespace Media {
37 
38 enum class AlbumFusionState {
39     START = 1,
40     SUCCESS = 2,
41     FAILED = 3
42 };
43 class MediaLibraryAlbumFusionUtils {
44 public:
45     EXPORT static int32_t RemoveMisAddedHiddenData(const std::shared_ptr<MediaLibraryRdbStore> upgradeStore);
46     EXPORT static int32_t HandleMatchedDataFusion(const std::shared_ptr<MediaLibraryRdbStore> upgradeStore);
47     EXPORT static int32_t HandleNotMatchedDataFusion(const std::shared_ptr<MediaLibraryRdbStore> upgradeStore);
48     EXPORT static int32_t HandleNotMatchedDataMigration(const std::shared_ptr<MediaLibraryRdbStore> upgradeStore,
49         std::multimap<int32_t, std::vector<int32_t>> &notMatchedMap);
50     EXPORT static int32_t HandleSingleFileCopy(const std::shared_ptr<MediaLibraryRdbStore> upgradeStore,
51         const int32_t &assetId, const int32_t &ownerAlbumId, int64_t &newAssetId);
52     EXPORT static int32_t RebuildAlbumAndFillCloudValue(const std::shared_ptr<MediaLibraryRdbStore> upgradeStore);
53     EXPORT static int32_t HandleChangeNameAlbum(const std::shared_ptr<MediaLibraryRdbStore> upgradeStore);
54     EXPORT static int32_t CompensateLpathForLocalAlbum(const std::shared_ptr<MediaLibraryRdbStore> upgradeStore);
55     EXPORT static int32_t MergeClashSourceAlbum(const std::shared_ptr<MediaLibraryRdbStore> upgradeStore,
56         std::shared_ptr<NativeRdb::ResultSet> &resultSet, const int32_t &sourceAlbumId, const int64_t &targetAlbumId);
57     EXPORT static void SetParameterToStopSync();
58     EXPORT static void SetParameterToStartSync();
59     EXPORT static int32_t CleanInvalidCloudAlbumAndData();
60     EXPORT static int32_t CloneSingleAsset(const int64_t &assetId, const std::string title);
61     EXPORT static int32_t CopyLocalSingleFile(const std::shared_ptr<MediaLibraryRdbStore> upgradeStore,
62         const int32_t &ownerAlbumId, std::shared_ptr<NativeRdb::ResultSet> &resultSet,
63         int64_t &newAssetId, std::string displayName = "");
64     EXPORT static int32_t CopyCloudSingleFile(const std::shared_ptr<MediaLibraryRdbStore> upgradeStore,
65         const int32_t &assetId, const int32_t &ownerAlbumId, std::shared_ptr<NativeRdb::ResultSet> &resultSet,
66         int64_t &newAssetId);
67     EXPORT static int32_t DeleteAlbumAndUpdateRelationship(const std::shared_ptr<MediaLibraryRdbStore> upgradeStore,
68         const int32_t &oldAlbumId, const int64_t &newAlbumId, bool isCloudAblum);
69     EXPORT static bool IsCloudAlbum(std::shared_ptr<NativeRdb::ResultSet> resultSet);
70     EXPORT static void BuildAlbumInsertValuesSetName(const std::shared_ptr<MediaLibraryRdbStore>& upgradeStore,
71         NativeRdb::ValuesBucket &values, std::shared_ptr<NativeRdb::ResultSet> &resultSet,
72         const std::string &newAlbumName);
73     EXPORT static int32_t RefreshAllAlbums();
74     EXPORT static int32_t GetAlbumFuseUpgradeStatus();
75     EXPORT static int32_t SetAlbumFuseUpgradeStatus(int32_t upgradeStatus);
76     EXPORT static void ReportAlbumFusionData(int64_t albumFusionTag, AlbumFusionState albumFusionState,
77         const std::shared_ptr<MediaLibraryRdbStore> rdbStore);
78 
79 private:
80     static void SetRefreshAlbum(bool needRefresh);
81     static int32_t HandleRestData(const std::shared_ptr<MediaLibraryRdbStore> upgradeStore, const int32_t &assetId,
82         const std::vector<int32_t> &restOwnerAlbumIds, int32_t &handledCount);
83     static int32_t HandleNoOwnerData(const std::shared_ptr<MediaLibraryRdbStore> upgradeStore);
84     static int32_t HandleExpiredAlbumData(const std::shared_ptr<MediaLibraryRdbStore> upgradeStore);
85     static int32_t QueryNoMatchedMap(const std::shared_ptr<MediaLibraryRdbStore> upgradeStore,
86         std::multimap<int32_t, std::vector<int32_t>> &notMatchedMap, bool isUpgrade);
87     static int32_t HandleNewCloudDirtyData(const std::shared_ptr<MediaLibraryRdbStore> upgradeStore,
88         std::multimap<int32_t, std::vector<int32_t>> &notMatchedMap);
89     static int32_t HandleDuplicateAlbum(const std::shared_ptr<MediaLibraryRdbStore> upgradeStore);
90     static int32_t HandleMisMatchScreenRecord(const std::shared_ptr<MediaLibraryRdbStore> upgradeStore);
91 
92 private:
93     static std::mutex cloudAlbumAndDataMutex_;
94     static std::atomic<bool> isNeedRefreshAlbum;
95 };
96 }
97 }
98 #endif // OHOS_MEDIALIBRARY_ALBUM_FUSION_UTILS_H