• 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 
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     if (uniStore == nullptr) {
50         MEDIA_ERR_LOG("uniStore is nullptr!");
51         return;
52     }
53 
54     //delete index
55     const std::string queryIdToDeleteIndex = "SELECT file_id FROM tab_analysis_search_index WHERE photo_status = -1";
56     auto resultSet = uniStore->QuerySql(queryIdToDeleteIndex);
57     if (resultSet == nullptr) {
58         MEDIA_ERR_LOG("resultSet is nullptr!");
59     }
60     std::vector<std::string> idToDeleteIndex;
61     while (resultSet != nullptr && resultSet->GoToNextRow() == NativeRdb::E_OK) {
62         idToDeleteIndex.push_back(to_string(GetInt32Val("file_id", resultSet)));
63     }
64     MEDIA_INFO_LOG("idToDeleteIndex size: %{public}zu", idToDeleteIndex.size());
65     if (!idToDeleteIndex.empty()) {
66         MediaAnalysisHelper::AsyncStartMediaAnalysisService(
67             static_cast<int32_t>(MediaAnalysisProxy::ActivateServiceType::START_DELETE_INDEX), idToDeleteIndex);
68     }
69 
70     //update index
71     const std::string queryIdToUpdateIndex = "SELECT file_id FROM tab_analysis_search_index WHERE photo_status = 2";
72     auto resultSetUpdateIndex = uniStore->QuerySql(queryIdToUpdateIndex);
73     if (resultSetUpdateIndex == nullptr) {
74         MEDIA_ERR_LOG("resultSetUpdateIndex is nullptr!");
75         return;
76     }
77     std::vector<std::string> idToUpdateIndex;
78     while (resultSetUpdateIndex->GoToNextRow() == NativeRdb::E_OK) {
79         idToUpdateIndex.push_back(to_string(GetInt32Val("file_id", resultSetUpdateIndex)));
80     }
81     MEDIA_INFO_LOG("idToUpdateIndex size: %{public}zu", idToUpdateIndex.size());
82     if (!idToUpdateIndex.empty()) {
83         MediaAnalysisHelper::AsyncStartMediaAnalysisService(
84             static_cast<int32_t>(MediaAnalysisProxy::ActivateServiceType::START_UPDATE_INDEX), idToUpdateIndex);
85     }
86     isPending_ = false;
87 }
88 
RegisterObserver()89 void CloudSyncSwitchManager::RegisterObserver()
90 {
91     options.enabled_ = true;
92     auto dataShareHelper = DataShare::DataShareHelper::Creator(QUERY_URI, options);
93     if (dataShareHelper == nullptr) {
94         MEDIA_ERR_LOG("dataShareHelper is nullptr");
95         return;
96     }
97 
98     const string photos = "persist.kernel.bundle_name.photos";
99     const string clouddrive = "persist.kernel.bundle_name.clouddrive";
100     const std::string GALLERY_BUNDLE_NAME = system::GetParameter(photos, "");
101     const std::string CLOUDDRIVE_BUNDLE_NAME = system::GetParameter(clouddrive, "");
102     if (GALLERY_BUNDLE_NAME == "") {
103         MEDIA_ERR_LOG("can't get gallery bundle name");
104         return;
105     }
106     if (CLOUDDRIVE_BUNDLE_NAME == "") {
107         MEDIA_ERR_LOG("can't get clouddrive bundle name");
108         return;
109     }
110     std::string queryUri = QUERY_URI + CLOUDDRIVE_BUNDLE_NAME + "/sync_switch?bundleName=" + GALLERY_BUNDLE_NAME;
111 
112     sptr<CloudSyncSwitchObserver> switchObserver(new (std::nothrow) CloudSyncSwitchObserver());
113     if (switchObserver == nullptr) {
114         return;
115     }
116     Uri observerUri(queryUri);
117     dataShareHelper->RegisterObserver(observerUri, switchObserver);
118 }
119 
UnRegisterObserver()120 void CloudSyncSwitchManager::UnRegisterObserver()
121 {
122     MEDIA_ERR_LOG("CloudSyncSwitchManager UnRegisterObserver");
123 }
124 } // namespace Media
125 } // namespace OHOS
126