1 /*
2 * Copyright (C) 2024-2025 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 #define MLOG_TAG "MediaLibraryCloudSyncUtils"
17
18 #include "cloud_sync_utils.h"
19
20 #include "datashare_helper.h"
21 #include "iservice_registry.h"
22 #include "media_log.h"
23 #include "medialibrary_errno.h"
24 #include "settings_data_manager.h"
25
26 using namespace std;
27 using namespace OHOS::DataShare;
28
29 namespace OHOS {
30 namespace Media {
31 static const std::string CLOUD_BASE_URI = "datashareproxy://generic.cloudstorage";
32 static const std::string CLOUD_DATASHARE_URI = CLOUD_BASE_URI + "/cloud_sp";
33 static const std::string CLOUD_SYNC_SWITCH_URI = CLOUD_BASE_URI + "/sync_switch";
34 static const std::string MOBILE_NETWORK_STATUS_ON = "1";
35
GetCloudHelper(const std::string & uri)36 static std::shared_ptr<DataShare::DataShareHelper> GetCloudHelper(const std::string &uri)
37 {
38 if (uri.empty()) {
39 MEDIA_ERR_LOG("uri is empty.");
40 return nullptr;
41 }
42 CreateOptions options;
43 options.enabled_ = true;
44 return DataShare::DataShareHelper::Creator(uri, options);
45 }
46
IsUnlimitedTrafficStatusOn()47 bool CloudSyncUtils::IsUnlimitedTrafficStatusOn()
48 {
49 std::shared_ptr<DataShare::DataShareHelper> cloudHelper = GetCloudHelper(CLOUD_BASE_URI);
50 if (cloudHelper == nullptr) {
51 MEDIA_INFO_LOG("cloudHelper is null");
52 return false;
53 }
54
55 DataShare::DataSharePredicates predicates;
56 predicates.EqualTo("key", "useMobileNetworkData");
57 Uri cloudUri(CLOUD_DATASHARE_URI);
58 vector<string> columns = { "value" };
59 shared_ptr<DataShare::DataShareResultSet> resultSet = cloudHelper->Query(cloudUri, predicates, columns);
60 if (resultSet == nullptr) {
61 MEDIA_INFO_LOG("resultSet is nullptr");
62 return false;
63 }
64 string switchOn = "0";
65 if (resultSet->GoToNextRow() == E_OK) {
66 resultSet->GetString(0, switchOn);
67 }
68 return switchOn == MOBILE_NETWORK_STATUS_ON;
69 }
70
IsCloudSyncSwitchOn()71 bool CloudSyncUtils::IsCloudSyncSwitchOn()
72 {
73 auto switchStatus = SettingsDataManager::GetPhotosSyncSwitchStatus();
74 if (switchStatus != SwitchStatus::NONE) {
75 MEDIA_DEBUG_LOG("GetPhotosSyncSwitchStatus success, switchStatus: %{public}d", static_cast<int>(switchStatus));
76 return switchStatus != SwitchStatus::CLOSE;
77 }
78 MEDIA_DEBUG_LOG("GetPhotosSyncSwitchStatus fail, continue query old sync switch");
79
80 std::shared_ptr<DataShare::DataShareHelper> cloudHelper = GetCloudHelper(CLOUD_BASE_URI);
81 CHECK_AND_RETURN_RET_LOG(cloudHelper != nullptr, false, "cloudHelper is null");
82
83 DataShare::DataSharePredicates predicates;
84 predicates.EqualTo("bundleName", "generic.cloudstorage");
85 Uri cloudUri(CLOUD_SYNC_SWITCH_URI);
86 vector<string> columns = { "isSwitchOn" };
87 shared_ptr<DataShare::DataShareResultSet> resultSet = cloudHelper->Query(cloudUri, predicates, columns);
88 CHECK_AND_RETURN_RET_LOG(resultSet != nullptr, false, "resultSet is null");
89
90 string switchOn = "0";
91 if (resultSet->GoToNextRow() == E_OK) {
92 resultSet->GetString(0, switchOn);
93 }
94 return switchOn == MOBILE_NETWORK_STATUS_ON;
95 }
96
IsCloudDataAgingPolicyOn()97 bool CloudSyncUtils::IsCloudDataAgingPolicyOn()
98 {
99 std::shared_ptr<DataShare::DataShareHelper> cloudHelper = GetCloudHelper(CLOUD_BASE_URI);
100 CHECK_AND_RETURN_RET_LOG(cloudHelper != nullptr, false, "cloudHelper is null");
101
102 DataShare::DataSharePredicates predicates;
103 predicates.EqualTo("key", "dataAgingPolicy");
104 Uri cloudUri(CLOUD_DATASHARE_URI);
105 vector<string> columns = { "value" };
106 shared_ptr<DataShare::DataShareResultSet> resultSet = cloudHelper->Query(cloudUri, predicates, columns);
107 CHECK_AND_RETURN_RET_LOG(resultSet != nullptr, false, "resultSet is nullptr");
108 string switchOn = "0";
109 if (resultSet->GoToNextRow() == E_OK) {
110 resultSet->GetString(0, switchOn);
111 }
112 return switchOn == MOBILE_NETWORK_STATUS_ON;
113 }
114 } // namespace Media
115 } // namespace OHOS