• 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 #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     MediaLibraryRdbUtils::UpdateAnalysisAlbumInternal(rdbStore, albumIds, fileIds);
69 
70     list<Uri> sendUris;
71     for (auto albumId : albumIds) {
72         sendUris.push_back(Uri(PhotoAlbumColumns::ANALYSIS_ALBUM_URI_PREFIX + albumId));
73     }
74 
75     return sendUris;
76 }
77 
AddNewNotify(CloudSyncHandleData & handleData,const list<Uri> & sendUris)78 static void AddNewNotify(CloudSyncHandleData &handleData, const list<Uri> &sendUris)
79 {
80     if (sendUris.size() <= 0) {
81         return;
82     }
83     ChangeType changeType = static_cast<ChangeType>(NotifyType::NOTIFY_UPDATE);
84     if (handleData.notifyInfo.find(changeType) == handleData.notifyInfo.end()) {
85         handleData.notifyInfo[changeType] = sendUris;
86     } else {
87         handleData.notifyInfo[changeType].insert(
88             handleData.notifyInfo[changeType].end(), sendUris.begin(), sendUris.end());
89     }
90     return;
91 }
92 
Handle(const CloudSyncHandleData & handleData)93 void AnalysisHandler::Handle(const CloudSyncHandleData &handleData)
94 {
95     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
96     if (rdbStore == nullptr) {
97         MEDIA_ERR_LOG("Can not get rdbstore");
98         return;
99     }
100 
101     vector<string> fileIds;
102     if (handleData.orgInfo.type == ChangeType::OTHER) {
103         MEDIA_INFO_LOG("Update the AnalysisAlbum for ChangeType being OTHER");
104         MediaLibraryRdbUtils::UpdateAnalysisAlbumInternal(rdbStore);
105     } else {
106         fileIds = GetFileIds(handleData);
107     }
108 
109     CloudSyncHandleData newHandleData = handleData;
110     if (!fileIds.empty()) {
111         shared_ptr<NativeRdb::ResultSet> resultSet = GetUpdateAnalysisAlbumsInfo(rdbStore, fileIds);
112         if (resultSet == nullptr) {
113             MEDIA_ERR_LOG("Failed query AnalysisAlbum");
114             return;
115         };
116         int32_t count = -1;
117         int32_t err = resultSet->GetRowCount(count);
118         if (err != E_OK) {
119             MEDIA_ERR_LOG("Failed to get count, err: %{public}d", err);
120             return;
121         }
122         if (count > 0) {
123             MEDIA_INFO_LOG("%{public}d analysis album need update", count);
124             list<Uri> sendUris = UpdateAnalysisAlbumsForCloudSync(rdbStore, resultSet, fileIds);
125             AddNewNotify(newHandleData, sendUris);
126         }
127     } else {
128         string uriString = newHandleData.orgInfo.uris.front().ToString();
129         MEDIA_INFO_LOG("refresh: %{public}s, type: %{public}d", uriString.c_str(),
130             static_cast<int32_t>(newHandleData.orgInfo.type));
131         refreshAlbumsFunc_(true);
132     }
133 
134     if (nextHandler_ != nullptr) {
135         nextHandler_->Handle(newHandleData);
136     }
137 }
138 } //namespace Media
139 } //namespace OHOS
140