• 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 <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 constexpr int32_t SYNC_INTERVAL = 20000;
30 // LCOV_EXCL_START
OnChange()31 void CloudSyncSwitchObserver::OnChange()
32 {
33     MEDIA_INFO_LOG("Cloud Sync Switch Status change");
34     lock_guard<mutex> lock(syncMutex_);
35         if (!isPending_) {
36             MEDIA_INFO_LOG("CloudSyncSwitchObserver set timer handle index");
37             std::thread([this]() {
38                 this->HandleIndex();
39             }).detach();
40             isPending_ = true;
41         }
42 }
43 
HandleIndex()44 void CloudSyncSwitchObserver::HandleIndex()
45 {
46     std::this_thread::sleep_for(std::chrono::milliseconds(SYNC_INTERVAL));
47     lock_guard<mutex> lock(syncMutex_);
48     auto uniStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
49     CHECK_AND_RETURN_LOG(uniStore != nullptr, "uniStore is nullptr!");
50 
51     //delete index
52     const std::string queryIdToDeleteIndex = "SELECT file_id FROM tab_analysis_search_index WHERE photo_status = -1";
53     auto resultSet = uniStore->QuerySql(queryIdToDeleteIndex);
54     CHECK_AND_PRINT_LOG(resultSet != nullptr, "resultSet is nullptr!");
55 
56     std::vector<std::string> idToDeleteIndex;
57     while (resultSet != nullptr && resultSet->GoToNextRow() == NativeRdb::E_OK) {
58         idToDeleteIndex.push_back(to_string(GetInt32Val("file_id", resultSet)));
59     }
60     MEDIA_INFO_LOG("idToDeleteIndex size: %{public}zu", idToDeleteIndex.size());
61     if (!idToDeleteIndex.empty()) {
62         MediaAnalysisHelper::AsyncStartMediaAnalysisService(
63             static_cast<int32_t>(MediaAnalysisProxy::ActivateServiceType::START_DELETE_INDEX), idToDeleteIndex);
64     }
65 
66     //update index
67     const std::string queryIdToUpdateIndex = "SELECT file_id FROM tab_analysis_search_index WHERE photo_status = 2";
68     auto resultSetUpdateIndex = uniStore->QuerySql(queryIdToUpdateIndex);
69     CHECK_AND_RETURN_LOG(resultSetUpdateIndex != nullptr, "resultSetUpdateIndex is nullptr!");
70     std::vector<std::string> idToUpdateIndex;
71     while (resultSetUpdateIndex->GoToNextRow() == NativeRdb::E_OK) {
72         idToUpdateIndex.push_back(to_string(GetInt32Val("file_id", resultSetUpdateIndex)));
73     }
74     MEDIA_INFO_LOG("idToUpdateIndex size: %{public}zu", idToUpdateIndex.size());
75     if (!idToUpdateIndex.empty()) {
76         MediaAnalysisHelper::AsyncStartMediaAnalysisService(
77             static_cast<int32_t>(MediaAnalysisProxy::ActivateServiceType::START_UPDATE_INDEX), idToUpdateIndex);
78     }
79     isPending_ = false;
80 }
81 
RegisterObserver()82 void CloudSyncSwitchManager::RegisterObserver()
83 {
84     options.enabled_ = true;
85     auto dataShareHelper = DataShare::DataShareHelper::Creator(QUERY_URI, options);
86     CHECK_AND_RETURN_LOG(dataShareHelper != nullptr, "dataShareHelper is nullptr");
87 
88     const string photos = "persist.kernel.bundle_name.photos";
89     const string clouddrive = "persist.kernel.bundle_name.clouddrive";
90     const std::string GALLERY_BUNDLE_NAME = system::GetParameter(photos, "");
91     const std::string CLOUDDRIVE_BUNDLE_NAME = system::GetParameter(clouddrive, "");
92     CHECK_AND_RETURN_LOG(GALLERY_BUNDLE_NAME != "", "can't get gallery bundle name");
93     CHECK_AND_RETURN_LOG(CLOUDDRIVE_BUNDLE_NAME != "", "can't get clouddrive bundle name");
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     CHECK_AND_RETURN(switchObserver != nullptr);
98     Uri observerUri(queryUri);
99     dataShareHelper->RegisterObserver(observerUri, switchObserver);
100 }
101 
UnRegisterObserver()102 void CloudSyncSwitchManager::UnRegisterObserver()
103 {
104     MEDIA_ERR_LOG("CloudSyncSwitchManager UnRegisterObserver");
105 }
106 // LCOV_EXCL_STOP
107 } // namespace Media
108 } // namespace OHOS
109