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 #include "analysis_handler.h"
17
18 #include "medialibrary_unistore_manager.h"
19 #include "medialibrary_rdb_utils.h"
20 #include "photo_album_column.h"
21 #include "photo_map_column.h"
22 #include "result_set_utils.h"
23 #include "vision_column.h"
24
25 using namespace std;
26
27 namespace OHOS {
28 namespace Media {
29
30 using ChangeType = DataShare::DataShareObserver::ChangeType;
31
GetFileIds(const CloudSyncHandleData & handleData)32 static vector<string> GetFileIds(const CloudSyncHandleData &handleData)
33 {
34 vector<string> fileIds;
35 for (auto &uri : handleData.orgInfo.uris) {
36 string uriString = uri.ToString();
37 auto index = uriString.rfind('/');
38 if (index == string::npos) {
39 continue;
40 }
41 auto fileIdStr = uriString.substr(index + 1);
42 fileIds.push_back(fileIdStr);
43 }
44 return fileIds;
45 }
46
GetUpdateAnalysisAlbumsInfo(const shared_ptr<MediaLibraryRdbStore> rdbStore,const vector<string> & fileIds)47 static shared_ptr<NativeRdb::ResultSet> GetUpdateAnalysisAlbumsInfo(const shared_ptr<MediaLibraryRdbStore> rdbStore,
48 const vector<string> &fileIds)
49 {
50 vector<string> columns = {
51 "DISTINCT (map_album)"
52 };
53 NativeRdb::RdbPredicates predicates(ANALYSIS_PHOTO_MAP_TABLE);
54 predicates.In(PhotoMap::ASSET_ID, fileIds);
55
56 return rdbStore->Query(predicates, columns);
57 }
58
UpdateAnalysisAlbumsForCloudSync(const shared_ptr<MediaLibraryRdbStore> rdbStore,const shared_ptr<NativeRdb::ResultSet> & resultSet,const vector<string> & fileIds)59 static list<Uri> UpdateAnalysisAlbumsForCloudSync(const shared_ptr<MediaLibraryRdbStore> rdbStore,
60 const shared_ptr<NativeRdb::ResultSet> &resultSet, const vector<string> &fileIds)
61 {
62 vector<string> albumIds;
63
64 while (resultSet->GoToNextRow() == E_OK) {
65 albumIds.push_back(get<string>(ResultSetUtils::GetValFromColumn(
66 ANALYSIS_PHOTO_MAP_TABLE + "." + PhotoMap::ALBUM_ID, resultSet, TYPE_STRING)));
67 }
68 resultSet->Close();
69 MediaLibraryRdbUtils::UpdateAnalysisAlbumInternal(rdbStore, albumIds, fileIds);
70
71 list<Uri> sendUris;
72 for (auto albumId : albumIds) {
73 sendUris.push_back(Uri(PhotoAlbumColumns::ANALYSIS_ALBUM_URI_PREFIX + albumId));
74 }
75
76 return sendUris;
77 }
78
AddNewNotify(CloudSyncHandleData & handleData,const list<Uri> & sendUris)79 static void AddNewNotify(CloudSyncHandleData &handleData, const list<Uri> &sendUris)
80 {
81 if (sendUris.size() <= 0) {
82 return;
83 }
84 ChangeType changeType = static_cast<ChangeType>(NotifyType::NOTIFY_UPDATE);
85 if (handleData.notifyInfo.find(changeType) == handleData.notifyInfo.end()) {
86 handleData.notifyInfo[changeType] = sendUris;
87 } else {
88 handleData.notifyInfo[changeType].insert(
89 handleData.notifyInfo[changeType].end(), sendUris.begin(), sendUris.end());
90 }
91 return;
92 }
93
Handle(const CloudSyncHandleData & handleData)94 void AnalysisHandler::Handle(const CloudSyncHandleData &handleData)
95 {
96 auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
97 if (rdbStore == nullptr) {
98 MEDIA_ERR_LOG("Can not get rdbstore");
99 return;
100 }
101
102 vector<string> fileIds;
103 if (handleData.orgInfo.type == ChangeType::OTHER) {
104 MEDIA_INFO_LOG("Update the AnalysisAlbum for ChangeType being OTHER");
105 MediaLibraryRdbUtils::UpdateAnalysisAlbumInternal(rdbStore);
106 } else {
107 fileIds = GetFileIds(handleData);
108 }
109
110 CloudSyncHandleData newHandleData = handleData;
111 if (!fileIds.empty()) {
112 shared_ptr<NativeRdb::ResultSet> resultSet = GetUpdateAnalysisAlbumsInfo(rdbStore, fileIds);
113 if (resultSet == nullptr) {
114 MEDIA_ERR_LOG("Failed query AnalysisAlbum");
115 return;
116 };
117 int32_t count = -1;
118 int32_t err = resultSet->GetRowCount(count);
119 if (err != E_OK) {
120 MEDIA_ERR_LOG("Failed to get count, err: %{public}d", err);
121 return;
122 }
123 if (count > 0) {
124 MEDIA_INFO_LOG("%{public}d analysis album need update", count);
125 list<Uri> sendUris = UpdateAnalysisAlbumsForCloudSync(rdbStore, resultSet, fileIds);
126 AddNewNotify(newHandleData, sendUris);
127 }
128 } else {
129 string uriString = newHandleData.orgInfo.uris.front().ToString();
130 MEDIA_INFO_LOG("refresh: %{public}s, type: %{public}d", uriString.c_str(),
131 static_cast<int32_t>(newHandleData.orgInfo.type));
132 refreshAlbumsFunc_(true);
133 }
134
135 if (nextHandler_ != nullptr) {
136 nextHandler_->Handle(newHandleData);
137 }
138 }
139 } //namespace Media
140 } //namespace OHOS
141