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 <string>
17 #include <vector>
18 
19 #include "cloud_sync_switch_observer.h"
20 #include "media_analysis_helper.h"
21 #include "medialibrary_unistore_manager.h"
22 #include "parameters.h"
23 #include "result_set_utils.h"
24 
25 namespace OHOS {
26 namespace Media {
27 const std::string QUERY_URI = "datashareproxy://";
28 DataShare::CreateOptions options;
29 
OnChange()30 void CloudSyncSwitchObserver::OnChange()
31 {
32     MEDIA_INFO_LOG("Cloud Sync Switch Status change");
33     auto uniStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
34     if (uniStore == nullptr) {
35         MEDIA_ERR_LOG("uniStore is nullptr!");
36         return;
37     }
38 
39     //delete index
40     const std::string queryIdToDeleteIndex = "SELECT file_id FROM tab_analysis_search_index WHERE photo_status = -1";
41     auto resultSet = uniStore->QuerySql(queryIdToDeleteIndex);
42     if (resultSet == nullptr) {
43         MEDIA_ERR_LOG("resultSet is nullptr!");
44     }
45     std::vector<std::string> idToDeleteIndex;
46     while (resultSet != nullptr && resultSet->GoToNextRow() == NativeRdb::E_OK) {
47         idToDeleteIndex.push_back(to_string(GetInt32Val("file_id", resultSet)));
48     }
49     MEDIA_INFO_LOG("idToDeleteIndex size: %{public}zu", idToDeleteIndex.size());
50     if (!idToDeleteIndex.empty()) {
51         MediaAnalysisHelper::AsyncStartMediaAnalysisService(
52             static_cast<int32_t>(MediaAnalysisProxy::ActivateServiceType::START_DELETE_INDEX), idToDeleteIndex);
53     }
54 
55     //update index
56     const std::string queryIdToUpdateIndex = "SELECT file_id FROM tab_analysis_search_index WHERE photo_status = 2";
57     auto resultSetUpdateIndex = uniStore->QuerySql(queryIdToUpdateIndex);
58     if (resultSetUpdateIndex == nullptr) {
59         MEDIA_ERR_LOG("resultSetUpdateIndex is nullptr!");
60         return;
61     }
62     std::vector<std::string> idToUpdateIndex;
63     while (resultSetUpdateIndex->GoToNextRow() == NativeRdb::E_OK) {
64         idToUpdateIndex.push_back(to_string(GetInt32Val("file_id", resultSetUpdateIndex)));
65     }
66     MEDIA_INFO_LOG("idToUpdateIndex size: %{public}zu", idToUpdateIndex.size());
67     if (!idToUpdateIndex.empty()) {
68         MediaAnalysisHelper::AsyncStartMediaAnalysisService(
69             static_cast<int32_t>(MediaAnalysisProxy::ActivateServiceType::START_UPDATE_INDEX), idToUpdateIndex);
70     }
71 }
72 
RegisterObserver()73 void CloudSyncSwitchManager::RegisterObserver()
74 {
75     options.enabled_ = true;
76     auto dataShareHelper = DataShare::DataShareHelper::Creator(QUERY_URI, options);
77     if (dataShareHelper == nullptr) {
78         MEDIA_ERR_LOG("dataShareHelper is nullptr");
79         return;
80     }
81 
82     const string photos = "persist.kernel.bundle_name.photos";
83     const string clouddrive = "persist.kernel.bundle_name.clouddrive";
84     const std::string GALLERY_BUNDLE_NAME = system::GetParameter(photos, "");
85     const std::string CLOUDDRIVE_BUNDLE_NAME = system::GetParameter(clouddrive, "");
86     if (GALLERY_BUNDLE_NAME == "") {
87         MEDIA_ERR_LOG("can't get gallery bundle name");
88         return;
89     }
90     if (CLOUDDRIVE_BUNDLE_NAME == "") {
91         MEDIA_ERR_LOG("can't get clouddrive bundle name");
92         return;
93     }
94     std::string queryUri = QUERY_URI + CLOUDDRIVE_BUNDLE_NAME + "/sync_switch?bundleName=" + GALLERY_BUNDLE_NAME;
95 
96     sptr<CloudSyncSwitchObserver> switchObserver(new (std::nothrow) CloudSyncSwitchObserver());
97     Uri observerUri(queryUri);
98     dataShareHelper->RegisterObserver(observerUri, switchObserver);
99 }
100 
UnRegisterObserver()101 void CloudSyncSwitchManager::UnRegisterObserver()
102 {
103     MEDIA_ERR_LOG("CloudSyncSwitchManager UnRegisterObserver");
104 }
105 } // namespace Media
106 } // namespace OHOS
107