• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define MLOG_TAG "CloudAlbum"
17 
18 #include "cloud_album_handler.h"
19 
20 #include "medialibrary_unistore_manager.h"
21 #include "medialibrary_album_operations.h"
22 #include "photo_album_column.h"
23 #include "medialibrary_rdb_utils.h"
24 #include "medialibrary_photo_operations.h"
25 #include "result_set_utils.h"
26 #include "medialibrary_notify.h"
27 #include "rdb_predicates.h"
28 #include "media_file_utils.h"
29 #include "photo_query_filter.h"
30 #include "accurate_common_data.h"
31 #include "album_accurate_refresh.h"
32 
33 using namespace std;
34 
35 namespace OHOS {
36 namespace Media {
37 
38 using ChangeType = DataShare::DataShareObserver::ChangeType;
39 
GetIds(const CloudSyncHandleData & handleData)40 static vector<string> GetIds(const CloudSyncHandleData &handleData)
41 {
42     vector<string> fileIds;
43     for (auto &uri : handleData.orgInfo.uris) {
44         string uriString = uri.ToString();
45         auto index = uriString.rfind('/');
46         if (index == string::npos) {
47             continue;
48         }
49         auto fileIdStr = uriString.substr(index + 1);
50         fileIds.push_back(fileIdStr);
51     }
52     return fileIds;
53 }
54 
UpdateCloudAlbum(const string & id,int32_t count)55 static void UpdateCloudAlbum(const string &id, int32_t count)
56 {
57     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
58     CHECK_AND_RETURN_LOG(rdbStore != nullptr, "Can not get rdbstore");
59     int changeRows = 0;
60     NativeRdb::ValuesBucket valuesNew;
61     valuesNew.PutInt(PhotoAlbumColumns::ALBUM_DIRTY, static_cast<int32_t>(DirtyTypes::TYPE_NEW));
62     valuesNew.PutInt(PhotoAlbumColumns::ALBUM_COUNT, count);
63     NativeRdb::RdbPredicates rdbPredicates(PhotoAlbumColumns::TABLE);
64     rdbPredicates.EqualTo(PhotoAlbumColumns::ALBUM_ID, id);
65     rdbStore->Update(changeRows, valuesNew, rdbPredicates);
66     CHECK_AND_PRINT_LOG(changeRows >= 0, "Failed to update cloudAlbum , ret = %{public}d", changeRows);
67 }
GetCloudAlbumCount(const string & id)68 static int32_t GetCloudAlbumCount(const string &id)
69 {
70     const std::vector<std::string> columnInfo = {"count(*) AS count"};
71 
72     NativeRdb::RdbPredicates predicates(PhotoColumn::PHOTOS_TABLE);
73     predicates.EqualTo(PhotoColumn::PHOTO_OWNER_ALBUM_ID, id);
74     PhotoQueryFilter::ModifyPredicate(PhotoQueryFilter::Option::FILTER_VISIBLE, predicates);
75 
76     auto resultSet = MediaLibraryRdbStore::Query(predicates, columnInfo);
77     if (resultSet == nullptr || resultSet->GoToFirstRow() != E_OK) {
78         MEDIA_ERR_LOG("GetCloudAlbumCount error: %{public}d", errno);
79         return E_HAS_DB_ERROR;
80     }
81     return GetInt32Val("count", resultSet);
82 }
83 
UpdateSourcePath(const shared_ptr<MediaLibraryRdbStore> rdbStore,NativeRdb::RdbPredicates & predicates)84 static void UpdateSourcePath(const shared_ptr<MediaLibraryRdbStore> rdbStore,
85     NativeRdb::RdbPredicates &predicates)
86 {
87     for (auto albumId: predicates.GetWhereArgs()) {
88         const std::string QUERY_FILE_ASSET_INFO = "SELECT file_id FROM Photos WHERE owner_album_id = " + albumId +
89             " AND clean_flag =0 AND hidden =0";
90         shared_ptr<NativeRdb::ResultSet> resultSet = rdbStore->QuerySql(QUERY_FILE_ASSET_INFO);
91         vector<string> fileAssetsIds, fileAssetsUri;
92         while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
93             int32_t fileId = GetInt32Val(MediaColumn::MEDIA_ID, resultSet);
94             fileAssetsIds.push_back(to_string(fileId));
95         }
96         MediaLibraryPhotoOperations::UpdateSourcePath(fileAssetsIds);
97     }
98 }
99 
DeletePhotoAlbum(NativeRdb::RdbPredicates & predicates)100 static int32_t DeletePhotoAlbum(NativeRdb::RdbPredicates &predicates)
101 {
102     constexpr int32_t AFTER_AGR_SIZE = 2;
103     std::shared_ptr<AccurateRefresh::AlbumAccurateRefresh> albumRefresh =
104         std::make_shared<AccurateRefresh::AlbumAccurateRefresh>();
105     CHECK_AND_RETURN_RET_LOG(albumRefresh != nullptr, E_RDB_STORE_NULL, "DeletePhotoAlbum Failed to get albumRefresh");
106     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
107     if (rdbStore == nullptr) {
108         MEDIA_ERR_LOG("DeletePhotoAlbum failed. rdbStore is null");
109         return E_HAS_DB_ERROR;
110     }
111     UpdateSourcePath(rdbStore, predicates);
112     predicates.And()->BeginWrap();
113     predicates.BeginWrap()->EqualTo(PhotoAlbumColumns::ALBUM_TYPE, to_string(PhotoAlbumType::USER));
114     predicates.EqualTo(PhotoAlbumColumns::ALBUM_SUBTYPE, to_string(PhotoAlbumSubType::USER_GENERIC));
115     predicates.EndWrap();
116     predicates.Or()->EqualTo(PhotoAlbumColumns::ALBUM_TYPE, to_string(PhotoAlbumType::SOURCE));
117     predicates.EndWrap();
118     int deleteRow = -1;
119     auto ret = albumRefresh->Delete(deleteRow, predicates);
120     if (ret != AccurateRefresh::ACCURATE_REFRESH_RET_OK || deleteRow <= 0) {
121         MEDIA_ERR_LOG("DeletePhotoAlbum failed, errCode = %{public}d, deleteRow = %{public}d", ret, deleteRow);
122     }
123     auto watch = MediaLibraryNotify::GetInstance();
124     CHECK_AND_RETURN_RET_LOG(watch != nullptr, E_ERR, "Can not get MediaLibraryNotify Instance");
125     const vector<string> &notifyUris = predicates.GetWhereArgs();
126     size_t count = notifyUris.size() - AFTER_AGR_SIZE;
127     for (size_t i = 0; i < count; i++) {
128         if (deleteRow > 0) {
129             watch->Notify(MediaFileUtils::GetUriByExtrConditions(PhotoAlbumColumns::ALBUM_URI_PREFIX,
130                 notifyUris[i]), NotifyType::NOTIFY_REMOVE);
131         }
132     }
133     albumRefresh->Notify();
134     return deleteRow;
135 }
136 
DeleteOrUpdateCloudAlbums(const vector<string> & ids)137 void CloudAlbumHandler::DeleteOrUpdateCloudAlbums(const vector<string> &ids)
138 {
139     for (const auto &id : ids) {
140         auto count = GetCloudAlbumCount(id);
141         if (count == 0) {
142             NativeRdb::RdbPredicates rdbPredicate(PhotoAlbumColumns::TABLE);
143             rdbPredicate.EqualTo(PhotoAlbumColumns::ALBUM_ID, id);
144             if (DeletePhotoAlbum(rdbPredicate) > 0) {
145                 MEDIA_INFO_LOG("delete Album {%{public}s} succ", id.c_str());
146             } else {
147                 MEDIA_INFO_LOG("delete Album {%{public}s} fail", id.c_str());
148             }
149         } else {
150             MEDIA_INFO_LOG("Album {%{public}s} not empty, setcount %{public}d", id.c_str(), count);
151             UpdateCloudAlbum(id, count);
152         }
153     }
154 }
155 
Handle(const CloudSyncHandleData & handleData)156 void CloudAlbumHandler::Handle(const CloudSyncHandleData &handleData)
157 {
158     if (handleData.orgInfo.type == ChangeType::DELETE) {
159         vector<string> fileIds;
160         fileIds = GetIds(handleData);
161         DeleteOrUpdateCloudAlbums(fileIds);
162     }
163     if (nextHandler_ != nullptr) {
164         nextHandler_->Handle(handleData);
165     }
166 }
167 
168 } //namespace Media
169 } //namespace OHOS
170