1 /* 2 * Copyright (C) 2022 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_RDB_UTILS_H 17 #define OHOS_MEDIALIBRARY_RDB_UTILS_H 18 19 #include <atomic> 20 #include <functional> 21 #include <memory> 22 #include <string> 23 #include <map> 24 25 #include "medialibrary_rdbstore.h" 26 #include "rdb_predicates.h" 27 #include "rdb_store.h" 28 #include "userfile_manager_types.h" 29 #include "datashare_values_bucket.h" 30 #include "shooting_mode_column.h" 31 namespace OHOS::Media { 32 #define EXPORT __attribute__ ((visibility ("default"))) 33 34 enum NotifyAlbumType : uint16_t { 35 NO_NOTIFY = 0x00, 36 SYS_ALBUM = 0x01, 37 SYS_ALBUM_HIDDEN = 0X02, 38 USER_ALBUM = 0X04, 39 SOURCE_ALBUM = 0X08, 40 ANA_ALBUM = 0X10, 41 }; 42 43 enum AlbumOperationType : int32_t { 44 DEFAULT = 0, 45 DELETE_PHOTO, 46 RECOVER_PHOTO, 47 HIDE_PHOTO, 48 UNHIDE_PHOTO, 49 }; 50 51 struct AlbumCounts { 52 int count; 53 int hiddenCount; 54 int imageCount; 55 int videoCount; 56 }; 57 58 struct UpdateAlbumData { 59 int32_t albumId; 60 int32_t albumSubtype; 61 int32_t hiddenCount; 62 int32_t albumCount; 63 int32_t albumImageCount; 64 int32_t albumVideoCount; 65 std::string hiddenCover; 66 std::string albumCoverUri; 67 uint8_t isCoverSatisfied; 68 int32_t newTotalCount { 0 }; 69 bool shouldNotify { false }; 70 bool hasChanged {false}; 71 bool shouldUpdateDateModified { false }; 72 int32_t coverUriSource {0}; 73 int64_t coverDateTime; 74 int64_t hiddenCoverDateTime; 75 std::string albumName; 76 }; 77 78 struct UpdateAllAlbumsData { 79 NotifyAlbumType type = NotifyAlbumType::NO_NOTIFY; 80 bool isBackUpAndRestore = false; 81 AlbumOperationType albumOperationType = AlbumOperationType::DEFAULT; 82 bool shouldUpdateDateModified = false; 83 }; 84 85 class MediaLibraryRdbUtils { 86 public: 87 // Update count, cover_uri of albums. 88 // If album id / subtype is specified, only update the specified album. Otherwise update all albums of the type. 89 EXPORT static void UpdateSystemAlbumInternal(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 90 const std::vector<std::string> &subtypes = {}, bool shouldNotify = false); 91 EXPORT static void UpdateUserAlbumInternal(std::shared_ptr<MediaLibraryRdbStore> rdbStore, 92 const std::vector<std::string> &userAlbumIds = {}, bool shouldNotify = false, 93 bool shouldUpdateDateModified = false); 94 EXPORT static void UpdateSourceAlbumInternal(std::shared_ptr<MediaLibraryRdbStore> rdbStore, 95 const std::vector<std::string> &sourceAlbumIds = {}, bool shouldNotify = false, 96 bool shouldUpdateDateModified = false); 97 EXPORT static void UpdateCommonAlbumInternal(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 98 const std::vector<std::string> &albumIds = {}, bool shouldNotify = false, 99 bool shouldUpdateDateModified = false); 100 EXPORT static void UpdateAnalysisAlbumInternal(const std::shared_ptr<MediaLibraryRdbStore>& rdbStore, 101 const std::vector<std::string> &userAlbumIds = {}); 102 103 // Update hidden_count, hidden_cover of albums. 104 // If album id / subtype is specified, only update the specified album. Otherwise update all albums of the type. 105 EXPORT static void UpdateSysAlbumHiddenState(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 106 const std::vector<std::string> &subtypes = {}); 107 EXPORT static void UpdateUserAlbumHiddenState(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 108 const std::vector<std::string> &userAlbumIds = {}); 109 EXPORT static void UpdateSourceAlbumHiddenState(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 110 const std::vector<std::string> &sourceAlbumIds = {}); 111 112 // Update count, cover_uri, hidden_count, hidden_cover of albums. 113 // Only update albums related with specified uri assets. 114 // The specified uri assets must exist at the time of calling these functions. 115 // If uris is empty, all albums of the type will be updated. 116 EXPORT static void UpdateSystemAlbumsByUris(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 117 AlbumOperationType albumOperationType, const std::vector<std::string> &uris = {}, 118 NotifyAlbumType type = NotifyAlbumType::NO_NOTIFY); 119 EXPORT static void UpdateUserAlbumByUri(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 120 const std::vector<std::string> &uris, bool shouldNotify = false, bool shouldUpdateDateModified = false); 121 EXPORT static void UpdateSourceAlbumByUri(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 122 const std::vector<std::string> &uris, bool shouldNotify = false, bool shouldUpdateDateModified = false); 123 static void UpdateCommonAlbumByUri(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 124 const std::vector<std::string> &uris, bool shouldNotify = false, bool shouldUpdateDateModified = false); 125 EXPORT static void UpdateAnalysisAlbumByUri(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 126 const std::vector<std::string> &uris); 127 EXPORT static void UpdateAllAlbums(std::shared_ptr<MediaLibraryRdbStore> rdbStore, 128 const std::vector<std::string> &uris = {}, 129 const UpdateAllAlbumsData &updateAlbumsData = UpdateAllAlbumsData()); 130 131 EXPORT static int32_t QueryAnalysisAlbumIdOfAssets(const std::vector<std::string>& assetIds, 132 std::set<std::string>& albumIds); 133 134 static void AddQueryFilter(NativeRdb::AbsRdbPredicates &predicates); 135 136 EXPORT static void UpdateAnalysisAlbumByFile(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 137 const std::vector<std::string> &fileIds, const std::vector<int> &albumTypes); 138 139 EXPORT static int32_t RefreshAllAlbums(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 140 std::function<void(PhotoAlbumType, PhotoAlbumSubType, int)> refreshProcessHandler, 141 std::function<void()> refreshCallback); 142 EXPORT static int32_t IsNeedRefreshByCheckTable(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 143 bool &signal); 144 145 EXPORT static void UpdateSystemAlbumCountInternal(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 146 const std::vector<std::string> &subtypes = {}); 147 EXPORT static void UpdateUserAlbumCountInternal(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 148 const std::vector<std::string> &userAlbumIds = {}); 149 EXPORT static void UpdateAnalysisAlbumCountInternal(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 150 const std::vector<std::string> &subtypes = {}); 151 EXPORT static void UpdateAllAlbumsForCloud(const std::shared_ptr<MediaLibraryRdbStore> rdbStore); 152 EXPORT static void UpdateAllAlbumsCountForCloud(const std::shared_ptr<MediaLibraryRdbStore> rdbStore); 153 154 EXPORT static bool IsNeedRefreshAlbum(); 155 EXPORT static void SetNeedRefreshAlbum(bool isNeedRefresh); 156 EXPORT static bool IsInRefreshTask(); 157 EXPORT static int32_t GetAlbumIdsForPortrait(const std::shared_ptr<MediaLibraryRdbStore>& rdbStore, 158 std::vector<std::string> &portraitAlbumIds); 159 EXPORT static int32_t GetAlbumSubtypeArgument(const NativeRdb::RdbPredicates &predicates); 160 EXPORT static void AddVirtualColumnsOfDateType(std::vector<std::string>& columns); 161 EXPORT static void AddQueryIndex(NativeRdb::AbsPredicates& predicates, const std::vector<std::string>& columns); 162 EXPORT static bool HasDataToAnalysis(const std::shared_ptr<MediaLibraryRdbStore> rdbStore); 163 EXPORT static int32_t UpdateTrashedAssetOnAlbum(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 164 NativeRdb::RdbPredicates &predicates); 165 EXPORT static int32_t UpdateOwnerAlbumId(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 166 const std::vector<DataShare::DataShareValuesBucket> &values, std::vector<int32_t> &updateIds, bool &hidden); 167 EXPORT static int32_t UpdateRemovedAssetToTrash(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 168 const std::vector<std::string> &whereIdArgs); 169 EXPORT static int32_t UpdateThumbnailRelatedDataToDefault(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 170 const int64_t fileId); 171 EXPORT static void TransformAppId2TokenId(const std::shared_ptr<MediaLibraryRdbStore> &store); 172 EXPORT static int32_t FillOneAlbumCountAndCoverUri(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 173 int32_t albumId, PhotoAlbumSubType subtype, std::string &sql); 174 EXPORT static void UpdateSystemAlbumExcludeSource(bool shouldNotify = false); 175 EXPORT static int32_t UpdateHighlightPlayInfo(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 176 const std::string &albumId); 177 EXPORT static bool AnalyzePhotosData(); 178 EXPORT static int32_t GetUpdateValues(const std::shared_ptr<MediaLibraryRdbStore> rdbStore, 179 UpdateAlbumData &data, NativeRdb::ValuesBucket &values, const bool hiddenState); 180 EXPORT static bool QueryShootingModeAlbumIdByType(ShootingModeAlbumType type, int32_t& albumId); 181 EXPORT static bool QueryAllShootingModeAlbumIds(std::vector<int32_t>& albumIds); 182 EXPORT static void TransformOwnerAppIdToTokenId(const std::shared_ptr<MediaLibraryRdbStore> &rdbStore); 183 EXPORT static void CleanAmbiguousColumn(std::vector<std::string> &columns, 184 DataShare::DataSharePredicates &predicates, const std::string tableName); 185 EXPORT static int32_t GetAlbumIdBySubType(PhotoAlbumSubType subtype); 186 EXPORT static bool ExecuteDatabaseQuickCheck(const std::shared_ptr<MediaLibraryRdbStore> &rdbStore); 187 188 private: 189 static std::atomic<bool> isNeedRefreshAlbum; 190 static std::atomic<bool> isInRefreshTask; 191 static std::mutex sRefreshAlbumMutex_; 192 static std::map<PhotoAlbumSubType, int32_t> subType2AlbumIdMap; 193 }; 194 } // namespace OHOS::Media 195 #endif // OHOS_MEDIALIBRARY_RDB_UTILS_H 196