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 constexpr int STORAGE_MANAGER_MANAGER_ID = 5003;
31 static const std::string CLOUD_BASE_URI = "datashareproxy://generic.cloudstorage/";
32 static const std::string CLOUD_DATASHARE_URI = CLOUD_BASE_URI + "cloud_sp?Proxy=true";
33 static const std::string CLOUD_URI = CLOUD_DATASHARE_URI + "&key=useMobileNetworkData";
34 static const std::string CLOUD_AGING_URI = CLOUD_DATASHARE_URI + "&key=dataAgingPolicy";
35 static const std::string CLOUD_SYNC_URI = CLOUD_BASE_URI + "sync_module_data?Proxy=true";
36 static const std::string CLOUD_SYNC_SWITCH_URI = CLOUD_BASE_URI + "sync_switch&bundleName=generic.cloudstorage";
37 static const std::string MOBILE_NETWORK_STATUS_ON = "1";
38 
GetCloudHelper(const std::string & uri)39 static std::shared_ptr<DataShare::DataShareHelper> GetCloudHelper(const std::string &uri)
40 {
41     if (uri.empty()) {
42         MEDIA_ERR_LOG("uri is empty.");
43         return nullptr;
44     }
45     auto saMgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
46     if (saMgr == nullptr) {
47         MEDIA_ERR_LOG("Failed to get SystemAbilityManagerClient");
48         return nullptr;
49     }
50     OHOS::sptr<OHOS::IRemoteObject> remoteObject = saMgr->CheckSystemAbility(STORAGE_MANAGER_MANAGER_ID);
51     if (remoteObject == nullptr) {
52         MEDIA_ERR_LOG("Token is null.");
53         return nullptr;
54     }
55     return DataShare::DataShareHelper::Creator(remoteObject, uri);
56 }
57 
IsUnlimitedTrafficStatusOn()58 bool CloudSyncUtils::IsUnlimitedTrafficStatusOn()
59 {
60     std::shared_ptr<DataShare::DataShareHelper> cloudHelper = GetCloudHelper(CLOUD_DATASHARE_URI);
61     if (cloudHelper == nullptr) {
62         MEDIA_INFO_LOG("cloudHelper is null");
63         return false;
64     }
65 
66     DataShare::DataSharePredicates predicates;
67     predicates.EqualTo("key", "useMobileNetworkData");
68     Uri cloudUri(CLOUD_URI);
69     vector<string> columns = { "value" };
70     shared_ptr<DataShare::DataShareResultSet> resultSet = cloudHelper->Query(cloudUri, predicates, columns);
71     if (resultSet == nullptr) {
72         MEDIA_INFO_LOG("resultSet is nullptr");
73         return false;
74     }
75     string switchOn = "0";
76     if (resultSet->GoToNextRow() == E_OK) {
77         resultSet->GetString(0, switchOn);
78     }
79     return switchOn == MOBILE_NETWORK_STATUS_ON;
80 }
81 
IsCloudSyncSwitchOn()82 bool CloudSyncUtils::IsCloudSyncSwitchOn()
83 {
84     std::shared_ptr<DataShare::DataShareHelper> cloudHelper = GetCloudHelper(CLOUD_SYNC_URI);
85     if (cloudHelper == nullptr) {
86         MEDIA_INFO_LOG("cloudHelper is null");
87         return false;
88     }
89 
90     DataShare::DataSharePredicates predicates;
91     predicates.EqualTo("bundleName", "generic.cloudstorage");
92     Uri cloudUri(CLOUD_SYNC_SWITCH_URI);
93     vector<string> columns = { "isSwitchOn" };
94     shared_ptr<DataShare::DataShareResultSet> resultSet = cloudHelper->Query(cloudUri, predicates, columns);
95     if (resultSet == nullptr) {
96         MEDIA_INFO_LOG("resultSet is nullptr");
97         return false;
98     }
99 
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 
IsCloudDataAgingPolicyOn()107 bool CloudSyncUtils::IsCloudDataAgingPolicyOn()
108 {
109     std::shared_ptr<DataShare::DataShareHelper> cloudHelper = GetCloudHelper(CLOUD_DATASHARE_URI);
110     if (cloudHelper == nullptr) {
111         MEDIA_INFO_LOG("cloudHelper is null");
112         return false;
113     }
114 
115     DataShare::DataSharePredicates predicates;
116     predicates.EqualTo("key", "dataAgingPolicy");
117     Uri cloudUri(CLOUD_AGING_URI);
118     vector<string> columns = { "value" };
119     shared_ptr<DataShare::DataShareResultSet> resultSet = cloudHelper->Query(cloudUri, predicates, columns);
120     if (resultSet == nullptr) {
121         MEDIA_INFO_LOG("resultSet is nullptr");
122         return false;
123     }
124     string switchOn = "0";
125     if (resultSet->GoToNextRow() == E_OK) {
126         resultSet->GetString(0, switchOn);
127     }
128     return switchOn == MOBILE_NETWORK_STATUS_ON;
129 }
130 } // namespace Media
131 } // namespace OHOS