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 #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
25 using namespace std;
26 using namespace OHOS::DataShare;
27
28 namespace OHOS {
29 namespace Media {
30 static const std::string CLOUD_BASE_URI = "datashareproxy://generic.cloudstorage";
31 static const std::string CLOUD_DATASHARE_URI = CLOUD_BASE_URI + "/cloud_sp";
32 static const std::string CLOUD_SYNC_SWITCH_URI = CLOUD_BASE_URI + "/sync_switch";
33 static const std::string MOBILE_NETWORK_STATUS_ON = "1";
34
GetCloudHelper(const std::string & uri)35 static std::shared_ptr<DataShare::DataShareHelper> GetCloudHelper(const std::string &uri)
36 {
37 if (uri.empty()) {
38 MEDIA_ERR_LOG("uri is empty.");
39 return nullptr;
40 }
41 CreateOptions options;
42 options.enabled_ = true;
43 return DataShare::DataShareHelper::Creator(uri, options);
44 }
45
IsUnlimitedTrafficStatusOn()46 bool CloudSyncUtils::IsUnlimitedTrafficStatusOn()
47 {
48 std::shared_ptr<DataShare::DataShareHelper> cloudHelper = GetCloudHelper(CLOUD_BASE_URI);
49 if (cloudHelper == nullptr) {
50 MEDIA_INFO_LOG("cloudHelper is null");
51 return false;
52 }
53
54 DataShare::DataSharePredicates predicates;
55 predicates.EqualTo("key", "useMobileNetworkData");
56 Uri cloudUri(CLOUD_DATASHARE_URI);
57 vector<string> columns = { "value" };
58 shared_ptr<DataShare::DataShareResultSet> resultSet = cloudHelper->Query(cloudUri, predicates, columns);
59 if (resultSet == nullptr) {
60 MEDIA_INFO_LOG("resultSet is nullptr");
61 return false;
62 }
63 string switchOn = "0";
64 if (resultSet->GoToNextRow() == E_OK) {
65 resultSet->GetString(0, switchOn);
66 }
67 return switchOn == MOBILE_NETWORK_STATUS_ON;
68 }
69
IsCloudSyncSwitchOn()70 bool CloudSyncUtils::IsCloudSyncSwitchOn()
71 {
72 std::shared_ptr<DataShare::DataShareHelper> cloudHelper = GetCloudHelper(CLOUD_BASE_URI);
73 CHECK_AND_RETURN_RET_LOG(cloudHelper != nullptr, false, "cloudHelper is null");
74
75 DataShare::DataSharePredicates predicates;
76 predicates.EqualTo("bundleName", "generic.cloudstorage");
77 Uri cloudUri(CLOUD_SYNC_SWITCH_URI);
78 vector<string> columns = { "isSwitchOn" };
79 shared_ptr<DataShare::DataShareResultSet> resultSet = cloudHelper->Query(cloudUri, predicates, columns);
80 CHECK_AND_RETURN_RET_LOG(resultSet != nullptr, false, "resultSet is null");
81
82 string switchOn = "0";
83 if (resultSet->GoToNextRow() == E_OK) {
84 resultSet->GetString(0, switchOn);
85 }
86 return switchOn == MOBILE_NETWORK_STATUS_ON;
87 }
88
IsCloudDataAgingPolicyOn()89 bool CloudSyncUtils::IsCloudDataAgingPolicyOn()
90 {
91 std::shared_ptr<DataShare::DataShareHelper> cloudHelper = GetCloudHelper(CLOUD_BASE_URI);
92 CHECK_AND_RETURN_RET_LOG(cloudHelper != nullptr, false, "cloudHelper is null");
93
94 DataShare::DataSharePredicates predicates;
95 predicates.EqualTo("key", "dataAgingPolicy");
96 Uri cloudUri(CLOUD_DATASHARE_URI);
97 vector<string> columns = { "value" };
98 shared_ptr<DataShare::DataShareResultSet> resultSet = cloudHelper->Query(cloudUri, predicates, columns);
99 CHECK_AND_RETURN_RET_LOG(resultSet != nullptr, false, "resultSet is nullptr");
100 string switchOn = "0";
101 if (resultSet->GoToNextRow() == E_OK) {
102 resultSet->GetString(0, switchOn);
103 }
104 return switchOn == MOBILE_NETWORK_STATUS_ON;
105 }
106 } // namespace Media
107 } // namespace OHOS